我意識到這是一個古老的線程,但我需要一個答案,所以也許別人不會太...
顯然,相同的鍵/索引名和檢查約束名稱可以的確可以重複在同一個數據庫中的不同模式中,所以我同意上面的註釋,並且沒有看到將模式名稱添加爲約束名稱的一部分的要點
例如以下代碼適用於SQL 2012和2008 R2,無錯誤
-- create a table in the dbo schema, with primary key
CREATE TABLE dbo.Children (
id_Child int IDENTITY(1,1) NOT NULL,
ChildName varchar(50) NULL,
id_Parent int NOT NULL,
CONSTRAINT PK_Children PRIMARY KEY CLUSTERED (id_Child ASC)
)
GO
-- now an index and a check constraint
CREATE NONCLUSTERED INDEX IX_Children_ChildName ON dbo.Children (ChildName ASC)
GO
ALTER TABLE dbo.Children WITH CHECK ADD CONSTRAINT CK_Children_LongEnough CHECK (len([ChildName])>(3))
GO
-- now create another schema
CREATE SCHEMA test AUTHORIZATION dbo
GO
-- an indentical table in the other schema, with a PRIMARY KEY OF THE SAME NAME
CREATE TABLE test.Children (
id_Child int IDENTITY(1,1) NOT NULL,
ChildName varchar(50) NULL,
id_Parent int NOT NULL,
CONSTRAINT PK_Children PRIMARY KEY CLUSTERED (id_Child ASC)
)
GO
-- now an index and a check constraint on the alternate table in another schema, with
-- the IDENTICAL NAMES
CREATE NONCLUSTERED INDEX IX_Children_ChildName ON test.Children (ChildName ASC)
GO
ALTER TABLE test.Children WITH CHECK ADD CONSTRAINT CK_Children_LongEnough CHECK (len([ChildName])>(3))
GO