sql-server – 如果在SQL Server中存在外键约束,我如何删除表?

前端之家收集整理的这篇文章主要介绍了sql-server – 如果在SQL Server中存在外键约束,我如何删除表?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下几点:
  1. DROP TABLE [dbo].[ExtraUserInformation];
  2. DROP TABLE [dbo].[UserProfile];
  3. DROP TABLE [dbo].[webpages_Membership];
  4. DROP TABLE [dbo].[webpages_OAuthMembership];
  5. DROP TABLE [dbo].[webpages_Roles];
  6. DROP TABLE [dbo].[webpages_UsersInRoles];
  7.  
  8. CREATE TABLE [dbo].[ExtraUserInformation] (
  9. [Id] INT IDENTITY (1,1) NOT NULL,[UserId] INT NOT NULL,[FullName] NVARCHAR (MAX) NULL,[Link] NVARCHAR (MAX) NULL,[Verified] BIT NULL,CONSTRAINT [PK_dbo.ExtraUserInformation] PRIMARY KEY CLUSTERED ([Id] ASC)
  10. );
  11.  
  12. CREATE TABLE [dbo].[webpages_UsersInRoles] (
  13. [UserId] INT NOT NULL,[RoleId] INT NOT NULL,PRIMARY KEY CLUSTERED ([UserId] ASC,[RoleId] ASC),CONSTRAINT [fk_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[UserProfile] ([UserId]),CONSTRAINT [fk_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [dbo].[webpages_Roles] ([RoleId])
  14. );

但是,这是一个失败的消息说:

  1. Msg 3726,Level 16,State 1,Line 6
  2. Could not drop object 'dbo.UserProfile' because it is referenced by a FOREIGN KEY constraint.
  3. Msg 3726,Line 9
  4. Could not drop object 'dbo.webpages_Roles' because it is referenced by a FOREIGN KEY constraint.
  5. Msg 2714,State 6,Line 27
  6. There is already an object named 'UserProfile' in the database.
  7. Checking identity information: current identity value 'NULL',current column value 'NULL'.
  8. DBCC execution completed. If DBCC printed error messages,contact your system administrator.

在这种情况下,我该怎么放桌子?

解决方法

您必须删除约束才能删除表.其他明智的规则违规.
如何让外键关系看到这个老问题.
SQL DROP TABLE foreign key constraint

猜你在找的MsSQL相关文章