SQL Server 2014中的外键错误

时间:2017-11-12 20:07:20

标签: sql sql-server sql-server-2014

对不起,我必须在这里更具体。以下是SQL我的代码。复制并粘贴SQL。基本上,我创建了13个链接在一起的表。

IF EXISTS (SELECT*FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='EventType')
BEGIN
    DROP TABLE EventType
END
GO

IF EXISTS (SELECT*FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='EventLocation')
BEGIN
    DROP TABLE EventLocation
END
GO

IF EXISTS (SELECT*FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='StudentEvent')
BEGIN
    DROP TABLE StudentEvent
END
GO

IF EXISTS (SELECT*FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='Event')
BEGIN
    DROP TABLE Event
END
GO

IF EXISTS (SELECT*FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='CurrentAddress')
BEGIN
    DROP TABLE CurrentAddress
END
GO

IF EXISTS (SELECT*FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='PreviousAddress')
BEGIN
    DROP TABLE PreviousAddress
END
GO

IF EXISTS (SELECT*FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='ContactType')
BEGIN
    DROP TABLE ContactType
END
GO

IF EXISTS (SELECT*FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='Contact')
BEGIN
    DROP TABLE Contact
END
GO

IF EXISTS (SELECT*FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='StudentMajor')
BEGIN
    DROP TABLE StudentMajor
END
GO

IF EXISTS (SELECT*FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='Major')
BEGIN
    DROP TABLE Major
END
GO

IF EXISTS (SELECT*FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='Citizenship')
BEGIN
    DROP TABLE Citizenship
END
GO

IF EXISTS (SELECT*FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='Country')
BEGIN
    DROP TABLE Country
END

IF EXISTS (SELECT*FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='Student')
BEGIN
    DROP TABLE Student
END
GO

CREATE TABLE Student
(
    StudentID int identity PRIMARY KEY,
    FirstName varchar (50),
    LastName varchar (30)
)
GO

CREATE TABLE Country
(
    CountryID int identity PRIMARY KEY,
    CountryOfBirth varchar (10),
    StudentID int FOREIGN KEY REFERENCES Student(StudentID)
)
GO

CREATE TABLE Citizenship
(
    CitizenshipID int identity PRIMARY KEY,
    CountryOfCitizenship1 varchar (10),
    CountryOfCitizenship2 varchar (10),
    StudentID int FOREIGN KEY REFERENCES Student(StudentID),
    CountryID int FOREIGN KEY REFERENCES Country(CountryID)
)
GO

CREATE TABLE Major
(
    Majorid int identity PRIMARY KEY,
    MajorName varchar(30),
    GraduatedMajor varchar (30)
)

CREATE TABLE StudentMajor
(
    StudentMajorid int identity,
    StudentID int FOREIGN KEY REFERENCES Student(StudentID),
    MajorID int FOREIGN KEY REFERENCES Major(MajorID)
)
GO

ALTER TABLE StudentMajor
   ADD constraint uk_sm UNIQUE (StudentID,MajorID)

 CREATE TABLE Contact 
 (
    ContactId int identity PRIMARY KEY,
    StudentID int FOREIGN KEY REFERENCES Student(StudentID),
    ContactInfo varchar (8),
    ContactDate datetime
)
GO

CREATE TABLE ContactType
(
    ContactTypeId int identity PRIMARY KEY,
    ContactID int FOREIGN KEY REFERENCES Contact(ContactID)
)
GO

CREATE TABLE PreviousAddress
(
    PreviousAddressId int identity PRIMARY KEY,
    Address1  varchar (50),
    Address2 varchar (50),
    City varchar (20),
    State varchar (20),
    Pincode char (10),
    StudentID int FOREIGN KEY REFERENCES Student(StudentID)
)
GO

CREATE TABLE CurrentAddress
(
    CurrentAddressId int identity PRIMARY KEY,
    Address1  varchar (50),
    Address2 varchar (50),
    City varchar (20),
    State varchar (20),
    Pincode char (10),
    StudentID int FOREIGN KEY REFERENCES Student(StudentID)
)
GO

CREATE TABLE EventType
(
    EventTypeID int identity PRIMARY KEY
)

CREATE TABLE Event
(
    EventId int identity PRIMARY KEY,
    EventDate  datetime,
    EventTitle varchar (50),
    EventTime datetime,
    EventTypeID int
)
GO

ALTER TABLE Event
    ADD constraint fk_Event 
        FOREIGN KEY (EventTypeID) REFERENCES EventType(EventTypeID)

CREATE TABLE StudentEvent
(
    StudentEventId int identity PRIMARY KEY,
    StudentID int FOREIGN KEY REFERENCES Student(StudentID),
    EventID int FOREIGN KEY REFERENCES Event(EventID),
    Comment varchar(2000)
)
GO

CREATE TABLE EventLocation 
(
    EventLocationId int identity PRIMARY KEY,
    EventCountry varchar (50),
    EventState varchar (50),
    EventAddress1  varchar (100),
    EventAddress2 varchar (100),
    EventCity varchar (50),
    EventPincode char (10),
    EventID int FOREIGN KEY REFERENCES Event(EventID)
)
GO

现在上面代码的问题在于它会第一次完美执行,但是当我第二次执行时它会抛出我的错误。

  

无法删除对象' EventType'因为它是由FOREIGN KEY约束引用的。

     

Msg 2714,Level 16,State 6,Line 132
  已有一个名为' EventType'在数据库中。

     

Msg 4902,Level 16,State 1,Line 143
  找不到对象"事件"因为它不存在或您没有权限。

     

Msg 1767,Level 16,State 0,Line 153
  外键' FK__EventLoca__Event__6265874F'引用无效表'事件'。   Msg 1750,Level 16,State 0,Line 153

     

无法创建约束或索引。查看以前的错误。

是否有更好的方法来编写外键或我做错了什么。

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

您应首先创建表格:

CREATE TABLE Event
(
    EventId int identity PRIMARY KEY,
    EventDate datetime,
    EventTitle varchar (50),
    EventTime datetime,
    EventTypeID int
)
GO

CREATE TABLE EventType
(
    EventTypeID int identity PRIMARY KEY
);

ALTER TABLE Event
    ADD constraint fk_Event 
        FOREIGN KEY (EventTypeID) REFERENCES EventType(EventTypeID);

或者您可以将表格创建为:

CREATE TABLE EventType
(
    EventTypeID int identity PRIMARY KEY
);

CREATE TABLE Event
(
    EventId int identity PRIMARY KEY,
    EventDate datetime,
    EventTitle varchar (50),
    EventTime datetime,
    EventTypeID int,
    constraint fk_Event FOREIGN KEY (EventTypeID) REFERENCES EventType(EventTypeID)
);

更新

  

无法删除对象' EventType'因为它是由FOREIGN KEY约束引用的。 Msg 2714,Level 16,State 6,Line 132已经有一个名为' EventType'在数据库中。消息4902,级别16,状态1,行143无法找到对象"事件"因为它不存在或您没有权限。消息1767,等级16,状态0,行153外键' FK__EventLoca__Event__6265874F'引用无效表'事件'。消息1750,级别16,状态0,行153无法创建约束或索引。查看以前的错误。

这个错误消息很清楚,你不能删除另一个表中引用的表,所以你必须首先删除Event表,然后你可以删除EventsType表。 / p>

答案 1 :(得分:0)

尝试创建表EventType,然后将其用作表Event中的参考。