SQL引用另一个表作为外键

时间:2013-05-06 17:45:10

标签: sql sql-server sql-server-2012

我正在使用Microsoft的SQL Server 2012,并使用另一个表的主键作为外键创建一个表,但它表示存在语法错误

    CREATE TABLE [dbo].[STUDENT]
    (
         STUDENT_ID  int not null Identity,
         SCHOOL_ID INT NOT NULL,
         CONSTRAINT PKEY PRIMARY KEY (STUDENT_ID),
         CONSTRAINT FKEY FOREIGN KEY (USER_ID)  REFERENCES USER (USER_ID)

     );

它给了我sql80001 error请帮助

1 个答案:

答案 0 :(得分:5)

USERreserved keyword in SQL Server。使用[]来转义名称

CREATE TABLE [dbo].[STUDENT]
(
     STUDENT_ID  int not null Identity,
     SCHOOL_ID INT NOT NULL,
     CONSTRAINT PKEY PRIMARY KEY (STUDENT_ID),
     CONSTRAINT FKEY FOREIGN KEY (USER_ID)  REFERENCES [USER] (USER_ID)
)