Search code examples
sql-servert-sqlsql-server-2014

How to DROP table in correct order in SQL SERVER 2014?


I have following 5 tables and 1 table is connected as Foreign key in 2 tables. My dilemma is that I cannot figure out which table to drop first. below is the drop table code I am trying and I have created tables in following order

Updated Drop Table Code:

 IF EXISTS (SELECT*FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='[Student Major]')
    BEGIN
        DROP TABLE [Student Major]
    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(
    [Student ID] INT IDENTITY PRIMARY KEY
    ,[First Name] varchar (50) NOT NULL
    ,[Last Name] varchar (30) NOT NULL
    )
GO

CREATE TABLE Country(
    [Country ID] int identity PRIMARY KEY NOT NULL
    ,[Country of Birth] varchar (10)
    ,[Student ID] int FOREIGN KEY REFERENCES Student([Student ID]) NOT NULL
    )
GO

CREATE TABLE Citizenship(
    [Citizenship ID] int identity PRIMARY KEY
    ,[Country of Citizenship1] varchar (10)
    ,[Country of Citizenship2] varchar (10)
    ,[Student ID] int FOREIGN KEY REFERENCES Student([Student ID]) NOT NULL
    ,[Country ID] int FOREIGN KEY REFERENCES Country([Country ID]) NOT NULL
    )
GO
CREATE TABLE Major(
    [Major ID] int identity PRIMARY KEY
    ,[Major Name] varchar(30) NOT NULL
    )
GO

CREATE TABLE [Student Major](
    [Student MajorID] int identity 
    ,[Student ID] int FOREIGN KEY REFERENCES Student([Student ID])
     ,[Major ID] int FOREIGN KEY REFERENCES Major([Major ID])
     ,[Graduated Major] varchar (30) NOT NULL
    )
GO

I Would like to drop table in correct order

Errors: Could not drop object 'Student' because it is referenced by a FOREIGN KEY constraint. Also,Please provide explanation of dropping the table. I am new to SQL. Thanks in advance!


Solution

  •  DROP TABLE Student_Major; GO 
       SELECT 1 FROM Citizenship; GO
     DROP TABLE Citizenship; GO
       SELECT 1 FROM Major; GO
     DROP TABLE Major; GO
       SELECT 1 FROM Country; GO
     DROP TABLE Country; GO
       SELECT 1 FROM Student; GO
     DROP TABLE Student; GO
    

    It's just like you said, it has to be in the order so that any table that is referenced by a FK is not dropped, as long as the FK is there.

    EDIT: GO forces the end of the batch

    EDIT: I notice you have the GO in there already. You could use Information_schema to find the constraints associated with the table and drop them first, but I feel like you shouldn't need to do that. What if you injected a dummy query imbetween each DROP statement. It's hacky but if you don't feel like having to understand all the information_schema stuff it might be easier if it works.