简体   繁体   中英

Drop a table with primary key having foregin key reference to another table

Is there any way to drop a table having primary key and referenced by foreign key on another table? I know, If I will try to write a simple DROP statement then SSMS will throw me an exception saying

Msg 3726, Level 16, State 1, Line 1
Could not drop object 'dbo.Dept' because it is referenced by a FOREIGN KEY constraint.

May be the answer is simply NO but, I am looking for any work around as recently I was asked this in a Interview.

You have to drop the CONSTRAINT on the (child) table. That keeps the child table, but breaks the 'link' to the parent table.

Which is why I like to name my constraints. ("FK_EmployeeToDepartment" in this case).

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[Employee]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
BEGIN DROP TABLE [dbo].[Employee] 
END 


IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[Department]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
BEGIN DROP TABLE [dbo].[Department] 
END 


CREATE TABLE [dbo].[Department](
    [DepartmentUUID] [uniqueidentifier] NOT NULL,
    [DepartmentName] [nvarchar](80) NULL,
    [CreateDate] [datetime] NOT NULL
    )


ALTER TABLE dbo.[Department] ADD CONSTRAINT PK_Department PRIMARY KEY NONCLUSTERED ([DepartmentUUID]) 
GO

ALTER TABLE [dbo].[Department] ADD CONSTRAINT CK_DepartmentName_Unique UNIQUE ([DepartmentName]) 
GO


CREATE TABLE [dbo].[Employee] ( 

    [EmployeeUUID] [uniqueidentifier] NOT NULL,
    [ParentDepartmentUUID] [uniqueidentifier] NOT NULL,
    [SSN] [nvarchar](11) NOT NULL,
    [LastName] [varchar](64) NOT NULL,
    [FirstName] [varchar](64) NOT NULL,
    [CreateDate] [datetime] NOT NULL,
    [HireDate] [datetime] NOT NULL
    )

GO

ALTER TABLE dbo.Employee ADD CONSTRAINT PK_Employee PRIMARY KEY NONCLUSTERED (EmployeeUUID) 
GO


ALTER TABLE [dbo].[Employee] ADD CONSTRAINT CK_SSN_Unique UNIQUE (SSN) 
GO

ALTER TABLE [dbo].[Employee] ADD CONSTRAINT FK_EmployeeToDepartment FOREIGN KEY (ParentDepartmentUUID) REFERENCES dbo.Department (DepartmentUUID) 
GO

/* this will fail here */
--DROP TABLE [dbo].[Department] 
GO

/* drop the constraint */
ALTER TABLE [dbo].[Employee] DROP CONSTRAINT FK_EmployeeToDepartment 
GO

/* now it will work */
DROP TABLE [dbo].[Department] 
GO

Drop the referenced table first and then parent table. That's why while creating reference you should choose ON DELETE CASCADE and/or ON UPDATE CASCADE .

It's purposefully throwing that error and stopping you from committing the mistake of making referenced table Orphan/Zombie.

If you really intend to drop a table that's referenced by a foreign key constraint, then the foreign key constraint is no longer meaningful, right? Drop the foreign key constraint first, then drop the table.

create table foo (
  foo_id integer primary key
);

create table bar (
  bar_id integer not null,
  foo_id integer not null,
  constraint bar_foo_id_fkey 
    foreign key (foo_id)
    references foo (foo_id),
  primary key (bar_id, foo_id)
);

drop table foo;  -- Results in an error because of the foreign key constraint.

alter table bar
drop constraint bar_foo_id_fkey;

drop table foo;  -- Drops table "foo".

You might still have work to do regarding existence of the column "bar"."foo_id", and regarding the primary key in "bar".

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