Search code examples
sqlms-access-2007create-table

I cannot create a simple table


I try to Write the SQL code to create the table named ‘EMP_1’. This table is a subset of the EMPLOYEE table, and the structure of the table is summarized as shown below. This is the information:

Attribute Name    Data Type    Remarks
EMP_NUM           CHAR(3)      PK
EMP_LNAME         VARCHAR(15)  Not Null
EMP_FNAME         VARCHAR(15)  Not Null
EMP_INITIAL       CHAR(1)    
EMP_HIREDATE      DATE    
JOB_CODE          CHAR(3)      FK (from JOB table)

My code:

CREATE TABLE EMP_1
(
EMP_NUM  CHAR(3)     PRIMARY KEY,
EMP_LNAME   VARCHAR(15)  Not Null,
EMP_FNAME    VARCHAR(15)    Not Null,
EMP_INITIAL  CHAR(1)    ,
EMP_HIREDATE    DATETIME,
JOB_CODE CHAR(3) FOREIGN KEY (JOB_CODE) REFERENCES JOB(JOB_CODE)
);

I keep getting CONSTRAINT error


Solution

  • I think you might be missing a comma before the constraint. This worked when I tried it:

    CREATE TABLE EMP_1
    (
      EMP_NUM CHAR(3) PRIMARY KEY,
      EMP_LNAME VARCHAR(15) Not Null,
      EMP_FNAME VARCHAR(15) Not Null,
      EMP_INITIAL CHAR(1),
      EMP_HIREDATE DATETIME,
      JOB_CODE CHAR(3), 
      CONSTRAINT FK_JOBS FOREIGN KEY (JOB_CODE) REFERENCES JOB(JOB_CODE)
    );