SQL小提琴中的桥表错误

时间:2014-07-04 17:49:01

标签: sql sql-server bridge sqlfiddle

我在Sql Fiddle中收到以下错误:

Schema Creation Failed: Incorrect syntax near the keyword 'FOREIGN'.:   

以下是我的CREATE TABLE脚本:

CREATE TABLE Employees(
      UserName_ID varchar(255) NOT NULL,
      Password    varchar(255) NOT NULL,
      F_Name      varchar(255) NOT NULL,
      L_Name      varchar(255) NOT NULL,
      Email       varchar(255) NOT NULL,
      CONSTRAINT Employees_PK  PRIMARY KEY(UserName_ID));

    CREATE TABLE Training_Sessions (
        Session_ID          int IDENTITY (1,1) NOT NULL ,
        Session_Name        varchar(255)       NOT NULL,
        Session_Description varchar(255)       NOT NULL,
        Session_Date        varchar(255)       NOT NULL,
        Organizer           varchar(255)       NOT NULL,
        CONSTRAINT Training_Sessions_PK        PRIMARY KEY(Session_ID));

    CREATE TABLE Enrolled
    (
        UserName_ID varchar(255) NOT NULL,
        Session_ID  int          NOT NULL,
        CONSTRAINT PK_Enrolled   PRIMARY KEY
        (
            UserName_ID,
            Session_ID
        )
        FOREIGN KEY (UserName_ID) REFERENCES Employees(UserName_ID),
        FOREIGN KEY (Session_ID)  REFERENCES Training_Sessions(Session_ID);
    )

我正在开发一个项目,在该项目中尚未在服务器上设置数据库并尝试将某些脚本排除在外。根据{{​​3}},我的语法对于SQL Server

是正确的

1 个答案:

答案 0 :(得分:0)

尝试:

CREATE TABLE Employees(
  UserName_ID varchar(255) NOT NULL,
  Password    varchar(255) NOT NULL,
  F_Name      varchar(255) NOT NULL,
  L_Name      varchar(255) NOT NULL,
  Email       varchar(255) NOT NULL,
  CONSTRAINT Employees_PK  PRIMARY KEY(UserName_ID));

CREATE TABLE Training_Sessions (
    Session_ID          int IDENTITY (1,1) NOT NULL ,
    Session_Name        varchar(255)       NOT NULL,
    Session_Description varchar(255)       NOT NULL,
    Session_Date        varchar(255)       NOT NULL,
    Organizer           varchar(255)       NOT NULL,
    CONSTRAINT Training_Sessions_PK        PRIMARY KEY(Session_ID));

CREATE TABLE Enrolled
(
    UserName_ID varchar(255) NOT NULL,
    Session_ID  int          NOT NULL,
    CONSTRAINT PK_Enrolled   PRIMARY KEY
    (
        UserName_ID,
        Session_ID
    ),
    CONSTRAINT user_fk FOREIGN KEY (UserName_ID) REFERENCES Employees(UserName_ID),
    CONSTRAINT sess_fk FOREIGN KEY (Session_ID)  REFERENCES Training_Sessions(Session_ID)
);