尝试在SQL Server Management Studio中向我的数据库添加一个表,但它正在摇摆不定。我确信它真的很简单,但我的大脑已经变得糊里糊涂,我找不到问题。基本上它告诉我数据库已经存在,但它显然没有。
错误(S):
Msg 3701,Level 11,State 5,Line 2
无法删除表'MySchema.mix_Case_Study-Module',因为它不存在或您没有权限。Msg 2714,Level 16,State 5,Line 4
数据库中已经有一个名为“mix_Case_Study-Module”的对象。Msg 1750,Level 16,State 0,Line 4
无法创建约束。查看以前的错误。Msg 4902,Level 16,State 1,Line 2
找不到对象“MySchema.mix_Case_Study-Module”,因为它不存在或者您没有权限。
SQL:
USE [MyDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP TABLE [MySchema].[mix_Case_Study-Module]
CREATE TABLE [MySchema].[mix_Case_Study-Module](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Active] [bit] NOT NULL,
[Case Study ID] [int] NOT NULL,
[Module ID] [int] NOT NULL,
[Position] [int] NOT NULL,
CONSTRAINT [mix_Case_Study-Module] PRIMARY KEY CLUSTERED (
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [MySchema].[mix_Case_Study-Module] ADD CONSTRAINT [DF_mix_Case_Study-Module_Active] DEFAULT ((1)) FOR [Active]
GO
任何帮助表示感谢。
答案 0 :(得分:5)
您的约束名称和表名称相同。
CREATE TABLE [MySchema].[mix_Case_Study-Module]
和
CONSTRAINT [mix_Case_Study-Module] PRIMARY KEY CLUSTERED
答案 1 :(得分:0)
您遇到问题是因为您首先尝试删除不存在的表。
你应该使用这样的东西:
USE [MyDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID([MySchema].[mix_Case_Study-Module], 'u') IS NOT NULL
DROP TABLE [MySchema].[mix_Case_Study-Module]
CREATE TABLE [MySchema].[mix_Case_Study-Module](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Active] [bit] NOT NULL,
[Case Study ID] [int] NOT NULL,
[Module ID] [int] NOT NULL,
[Position] [int] NOT NULL,
CONSTRAINT [mix_Case_Study-Module_PK] PRIMARY KEY CLUSTERED (
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [MySchema].[mix_Case_Study-Module] ADD CONSTRAINT [DF_mix_Case_Study-Module_Active] DEFAULT ((1)) FOR [Active]
GO