我收到语法错误,不知道如何解决

时间:2019-06-11 19:55:59

标签: sql-server constraints

我遇到错误

  

','附近的语法不正确

,不知道如何解决。

我尝试重写查询,引用旧代码,似乎没有任何作用。

USE RewriteExam
GO

IF EXISTS(SELECT * FROM sys.objects
          WHERE object_id = OBJECT_ID('dbo.Vendors')
            AND type IN ('U'))
BEGIN
    PRINT 'Vendors table already exists, dropping it now!'
    DROP TABLE Vendors
END
GO

PRINT 'Creating the "Vendors" Table'

CREATE TABLE dbo.Vendors 
(
    VendorID INT PRIMARY KEY, 
    VendorName VARCHAR(45) NOT NULL,
    VendorAddress VARCHAR(45) NULL,
    VendorCity VARCHAR(45) NOT NULL,
    VendorState VARCHAR(45) NOT NULL,
    VendorZipCode VARCHAR(45) NOT NULL,
    VendorPhone VARCHAR(45) NULL,
    VendorContactLName VARCHAR(7) NULL,
    VendorContactFName VARCHAR(14) NULL,
    AcountNo INT NOT NULL,
    ModifiedDate SMALLDATETIME NOT NULL,
    AccountNo INT FOREIGN KEY,
    CONSTRAINT PK_Vendors_VendorID 
         PRIMARY KEY (VendorID)
    --REFERENCES Vendors (VendorID)
);
GO

PRINT 'Creating the "GLAccounts" Table'

CREATE TABLE dbo.GLAccounts 
(
    AccountNo INT PRIMARY KEY,
    AccountDescription VARCHAR(45) NOT NULL,
);
GO

麻烦在于约束。外键后面的逗号给了我错误以及PRIMARY KEY (VendorID)

1 个答案:

答案 0 :(得分:0)

只需在其后添加注释掉的部分

AccountNo INT FOREIGN KEY

并转换为

AccountNo INT FOREIGN KEY REFERENCES Vendors (VendorID)

并删除已经定义的约束PRIMARY KEY (VendorID)