Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将模式指定为约束名称的一部分是个好主意吗?_Sql_Sql Server - Fatal编程技术网

Sql 将模式指定为约束名称的一部分是个好主意吗?

Sql 将模式指定为约束名称的一部分是个好主意吗?,sql,sql-server,Sql,Sql Server,在我的SQL Server数据库中,我按模式对表进行分组,以帮助组织它们的用途 当我为这些表上的列创建约束时,我已经将模式作为名称的一部分。例如,如果我有一个表,例如Com.WebUser和Com.Company: PK_Com_WebUser --Primary key constraint FK_Com_WebUser_Com_Company --Foreign key to Com.Company table UQ_Com_WebUs

在我的SQL Server数据库中,我按模式对表进行分组,以帮助组织它们的用途

当我为这些表上的列创建约束时,我已经将模式作为名称的一部分。例如,如果我有一个表,例如
Com.WebUser
Com.Company

PK_Com_WebUser                     --Primary key constraint
FK_Com_WebUser_Com_Company         --Foreign key to Com.Company table
UQ_Com_WebUser_CompanyID_Username  --Unique constraint on CompanyID & Username
我想,在这个例子中,我曾经在不同的模式中有另一个同名的表,将该模式放在约束名称中会让事情更清楚,但名称有点冗长


有没有命名这些对象的最佳实践?

我认为添加模式名是一个很好的实践,因为您已经提到了(跨模式重复表名),我不担心约束名有多冗长,因为您很少需要引用这些约束。

一种可能性是为每个表指定一个简短的表别名,例如

Com.Customer => cst
Com.Company =>  com
Com.WebUser =>  wus
并使用约束名称,如

PK_wus
FK_wus_com
UQ_wus_CompanyID_Username
如果添加了新表,请为它们指定新的唯一别名

我在一个项目中工作,其中列名的前缀是这个表别名

com_CompanyID
com_Name

等等。

从技术上讲,约束属于与其基表相同的模式。也不能在不指定表的情况下引用约束。您可以在以下代码段中看到:

CREATE SCHEMA s1;
GO
CREATE SCHEMA s2;
GO
CREATE TABLE s1.T(i int CONSTRAINT tpk PRIMARY KEY);
GO
CREATE TABLE s2.T(i int CONSTRAINT tpk PRIMARY KEY);
GO
SELECT OBJECT_SCHEMA_NAME(object_id) SchemaName,name,type_desc FROM sys.objects WHERE schema_id IN (SCHEMA_ID('s1'),SCHEMA_ID('s2'));
GO
唯一的例外是OBJECT_ID函数。在其中,您可以引用约束,而无需指定其基表。但在使用此函数时,您也应始终指定模式:

SELECT OBJECT_ID('s1.tpk'),OBJECT_ID('s2.tpk');

由于以上种种原因,我认为把模式名放进更名中是多余的重复。所以,坚持干燥原则,你不应该这样做。

我意识到这是一条古老的线索,但我需要一个答案,所以也许其他人也需要

显然,相同的键/索引名称和检查约束名称确实可以在同一数据库中的不同模式中重复,因此我同意上面的评论,不认为将模式名称添加为约束名称的一部分有什么意义

例如,以下代码在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