简体   繁体   中英

Foreign key error in SQL Server 2014

Sorry I had to be more specific in here. So following is SQL my code. Copy and paste in SQL. Basically, I am creating 13 tables that are linked together.

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

Now the problem with the above code is that it will execute perfectly first time, but when I execute second time it throws me following error.

Could not drop object 'EventType' because it is referenced by a FOREIGN KEY constraint.

Msg 2714, Level 16, State 6, Line 132
There is already an object named 'EventType' in the database.

Msg 4902, Level 16, State 1, Line 143
Cannot find the object "Event" because it does not exist or you do not have permissions.

Msg 1767, Level 16, State 0, Line 153
Foreign key 'FK__EventLoca__Event__6265874F' references invalid table 'Event'. Msg 1750, Level 16, State 0, Line 153

Could not create constraint or index. See previous errors.

Is there a better way to code the foreign key or am I doing something wrong.

Thanks in advance for help!

You should create the table first:

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);

Or you can create your tables as:

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)
);

Update:

Could not drop object 'EventType' because it is referenced by a FOREIGN KEY constraint. Msg 2714, Level 16, State 6, Line 132 There is already an object named 'EventType' in the database. Msg 4902, Level 16, State 1, Line 143 Cannot find the object "Event" because it does not exist or you do not have permissions. Msg 1767, Level 16, State 0, Line 153 Foreign key 'FK__EventLoca__Event__6265874F' references invalid table 'Event'. Msg 1750, Level 16, State 0, Line 153 Could not create constraint or index. See previous errors.

This error msg is clear, you can't drop a table referenced in another table, so you have to drop the Event table first, then you can drop the EventsType table.

尝试创建表EventType之前,先将其用作表Event的引用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM