SQL Server 2012:创建表

时间:2016-04-15 00:19:02

标签: sql sql-server-2012

CREATE TABLE Schedule 
(
     Section DATETIME NOT NULL PRIMARY KEY(CourseID, Section, EmployeeID),
     CourseID VARCHAR(10) REFERENCES Course(CourseID) NOT NULL,
     EmployeeID VARCHAR(20) NOT NULL REFERENCES Employee(EmployeeID),
     StartTime TIME NULL,
     Days DATE NULL,
     Length TIME NULL
)

CREATE TABLE Enrollment 
(
     StudentID INT Primary key (StudentID, CourseID, Section) NOT NULL,
     CourseID VARCHAR(10) REFERENCES Course(CourseID) NOT NULL,
     Section DATETIME NOT NULL REFERENCES Schedule(Section)
)

第二张表没有创建,我哪里出错了?

1 个答案:

答案 0 :(得分:0)

这是因为您将Schedule表中的主键设置为自然/组合键。

请尝试为此目的创建独立列。我在下面列举了一个显示差异的例子。

CREATE TABLE DBO.PK_TEST (
                       Col_A INT NOT NULL
                      ,Col_B INT NOT NULL
                      ,Primary Key(Col_A,Col_B)
                      )


Create table DBO.PK_TEST_2 (
                            Col_A int NOT NULL
                           ,Col_B int NOT NULL
                           ,Col_C as (cast(Col_A as nvarchar) 
                           + cast(Col_B as nvarchar)) PERSISTED NOT NULL
                           ,primary key(Col_C)
                           )

enter image description here