Sql server 如何保证某些行是唯一的(一种辅助键)SQL
在SQL中是否有生成辅助密钥的方法 假设我有一个ID为SECID1、SECID2列的表 密钥是ID,但我还希望SECID1、SECID2作为密钥工作,以保证我没有两个具有相同SECID1和SECID1的条目Sql server 如何保证某些行是唯一的(一种辅助键)SQL,sql-server,Sql Server,在SQL中是否有生成辅助密钥的方法 假设我有一个ID为SECID1、SECID2列的表 密钥是ID,但我还希望SECID1、SECID2作为密钥工作,以保证我没有两个具有相同SECID1和SECID1的条目 如果可能,如何执行此操作?是的,您可以在“我使用唯一约束”下面添加复合列唯一约束或唯一索引 CREATE TABLE YourTable ( ID INT PRIMARY KEY, SECID1 INT, SECID2 INT, UNIQUE(SECID1,SECID2) ) 是否确实
如果可能,如何执行此操作?是的,您可以在“我使用唯一约束”下面添加复合列唯一约束或唯一索引
CREATE TABLE YourTable
(
ID INT PRIMARY KEY,
SECID1 INT,
SECID2 INT,
UNIQUE(SECID1,SECID2)
)
是否确实需要代理ID列?如果这是一个多对多关系表,则我通常只有一个2列PK,有时根据访问该表的查询的相反键顺序有一个唯一索引/约束是,您可以在下面添加一个复合列唯一约束或唯一索引。我使用唯一约束
CREATE TABLE YourTable
(
ID INT PRIMARY KEY,
SECID1 INT,
SECID2 INT,
UNIQUE(SECID1,SECID2)
)
是否确实需要代理ID列?如果这是一个多对多关系表,我通常只有一个2列的PK,有时还有一个唯一的索引/约束,其键顺序与访问该表的查询相反您可以创建任意多的唯一键约束,以实现此类唯一性检查/约束。您可以创建任意多的唯一键约束由于您希望实现这种唯一性检查/约束。这里有文档和说明:
有可能,这里有文档和说明: 您想要的是一个您想要的是一个1您可以使用或: 结果:
name index_id type type_desc is_unique is_primary_key
------------------- -------- ---- ------------ --------- --------------
PK_MyTestTable 1 1 CLUSTERED 1 1
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
----------------------- ----------- ---- ------------ --------- -------------- -----------
PK_MyTestTable 1 1 CLUSTERED 1 1 0
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B_#_C 4 2 NONCLUSTERED 1 0 0
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.Invoice' with unique index 'IUF_Invoice_ParentInvoiceID'.
The statement has been terminated.
name index_id type type_desc is_unique is_primary_key is_disabled
------------------ ----------- ---- ------------- --------- -------------- -----------
UQ_MyTestTable_A_B 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
-------------------- ----------- ---- ------------ --------- -------------- -----------
IUN_MyTestTable_A_B2 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
注1:您可以看到约束PK_MyTestTable创建为聚集唯一索引,约束UQ_MyTestTable_A_B UNIQUE Noncustered创建为“非聚集索引”
注2 SQL 2008+:对于我来说,在SQL Server 2008中,唯一非聚集约束和唯一非聚集索引之间的主要区别在于能够:
结果:
name index_id type type_desc is_unique is_primary_key
------------------- -------- ---- ------------ --------- --------------
PK_MyTestTable 1 1 CLUSTERED 1 1
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
----------------------- ----------- ---- ------------ --------- -------------- -----------
PK_MyTestTable 1 1 CLUSTERED 1 1 0
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B_#_C 4 2 NONCLUSTERED 1 0 0
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.Invoice' with unique index 'IUF_Invoice_ParentInvoiceID'.
The statement has been terminated.
name index_id type type_desc is_unique is_primary_key is_disabled
------------------ ----------- ---- ------------- --------- -------------- -----------
UQ_MyTestTable_A_B 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
-------------------- ----------- ---- ------------ --------- -------------- -----------
IUN_MyTestTable_A_B2 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
您可以使用创建唯一的非聚集索引。。。包括从执行计划中创建并消除KeyLookup或RID Lookup
注3 SQL 2008+:此外,您可能有
结果:
name index_id type type_desc is_unique is_primary_key
------------------- -------- ---- ------------ --------- --------------
PK_MyTestTable 1 1 CLUSTERED 1 1
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
----------------------- ----------- ---- ------------ --------- -------------- -----------
PK_MyTestTable 1 1 CLUSTERED 1 1 0
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B_#_C 4 2 NONCLUSTERED 1 0 0
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.Invoice' with unique index 'IUF_Invoice_ParentInvoiceID'.
The statement has been terminated.
name index_id type type_desc is_unique is_primary_key is_disabled
------------------ ----------- ---- ------------- --------- -------------- -----------
UQ_MyTestTable_A_B 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
-------------------- ----------- ---- ------------ --------- -------------- -----------
IUN_MyTestTable_A_B2 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
2您可以使用或:
结果:
name index_id type type_desc is_unique is_primary_key
------------------- -------- ---- ------------ --------- --------------
PK_MyTestTable 1 1 CLUSTERED 1 1
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
----------------------- ----------- ---- ------------ --------- -------------- -----------
PK_MyTestTable 1 1 CLUSTERED 1 1 0
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B_#_C 4 2 NONCLUSTERED 1 0 0
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.Invoice' with unique index 'IUF_Invoice_ParentInvoiceID'.
The statement has been terminated.
name index_id type type_desc is_unique is_primary_key is_disabled
------------------ ----------- ---- ------------- --------- -------------- -----------
UQ_MyTestTable_A_B 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
-------------------- ----------- ---- ------------ --------- -------------- -----------
IUN_MyTestTable_A_B2 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
或
结果:
name index_id type type_desc is_unique is_primary_key
------------------- -------- ---- ------------ --------- --------------
PK_MyTestTable 1 1 CLUSTERED 1 1
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
----------------------- ----------- ---- ------------ --------- -------------- -----------
PK_MyTestTable 1 1 CLUSTERED 1 1 0
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B_#_C 4 2 NONCLUSTERED 1 0 0
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.Invoice' with unique index 'IUF_Invoice_ParentInvoiceID'.
The statement has been terminated.
name index_id type type_desc is_unique is_primary_key is_disabled
------------------ ----------- ---- ------------- --------- -------------- -----------
UQ_MyTestTable_A_B 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
-------------------- ----------- ---- ------------ --------- -------------- -----------
IUN_MyTestTable_A_B2 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
1您可以使用或:
结果:
name index_id type type_desc is_unique is_primary_key
------------------- -------- ---- ------------ --------- --------------
PK_MyTestTable 1 1 CLUSTERED 1 1
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
----------------------- ----------- ---- ------------ --------- -------------- -----------
PK_MyTestTable 1 1 CLUSTERED 1 1 0
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B_#_C 4 2 NONCLUSTERED 1 0 0
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.Invoice' with unique index 'IUF_Invoice_ParentInvoiceID'.
The statement has been terminated.
name index_id type type_desc is_unique is_primary_key is_disabled
------------------ ----------- ---- ------------- --------- -------------- -----------
UQ_MyTestTable_A_B 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
-------------------- ----------- ---- ------------ --------- -------------- -----------
IUN_MyTestTable_A_B2 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
注1:您可以看到约束PK_MyTestTable创建为聚集唯一索引,约束UQ_MyTestTable_A_B UNIQUE Noncustered创建为“非聚集索引”
注2 SQL 2008+:对于我来说,在SQL Server 2008中,唯一非聚集约束和唯一非聚集索引之间的主要区别在于能够:
结果:
name index_id type type_desc is_unique is_primary_key
------------------- -------- ---- ------------ --------- --------------
PK_MyTestTable 1 1 CLUSTERED 1 1
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
----------------------- ----------- ---- ------------ --------- -------------- -----------
PK_MyTestTable 1 1 CLUSTERED 1 1 0
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B_#_C 4 2 NONCLUSTERED 1 0 0
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.Invoice' with unique index 'IUF_Invoice_ParentInvoiceID'.
The statement has been terminated.
name index_id type type_desc is_unique is_primary_key is_disabled
------------------ ----------- ---- ------------- --------- -------------- -----------
UQ_MyTestTable_A_B 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
-------------------- ----------- ---- ------------ --------- -------------- -----------
IUN_MyTestTable_A_B2 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
您可以使用创建唯一的非聚集索引。。。包括从执行计划中创建并消除KeyLookup或RID Lookup
注3 SQL 2008+:此外,您可能有
结果:
name index_id type type_desc is_unique is_primary_key
------------------- -------- ---- ------------ --------- --------------
PK_MyTestTable 1 1 CLUSTERED 1 1
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
----------------------- ----------- ---- ------------ --------- -------------- -----------
PK_MyTestTable 1 1 CLUSTERED 1 1 0
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B_#_C 4 2 NONCLUSTERED 1 0 0
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.Invoice' with unique index 'IUF_Invoice_ParentInvoiceID'.
The statement has been terminated.
name index_id type type_desc is_unique is_primary_key is_disabled
------------------ ----------- ---- ------------- --------- -------------- -----------
UQ_MyTestTable_A_B 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
-------------------- ----------- ---- ------------ --------- -------------- -----------
IUN_MyTestTable_A_B2 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
2您可以使用或:
结果:
name index_id type type_desc is_unique is_primary_key
------------------- -------- ---- ------------ --------- --------------
PK_MyTestTable 1 1 CLUSTERED 1 1
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
----------------------- ----------- ---- ------------ --------- -------------- -----------
PK_MyTestTable 1 1 CLUSTERED 1 1 0
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B_#_C 4 2 NONCLUSTERED 1 0 0
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.Invoice' with unique index 'IUF_Invoice_ParentInvoiceID'.
The statement has been terminated.
name index_id type type_desc is_unique is_primary_key is_disabled
------------------ ----------- ---- ------------- --------- -------------- -----------
UQ_MyTestTable_A_B 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
-------------------- ----------- ---- ------------ --------- -------------- -----------
IUN_MyTestTable_A_B2 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
或
结果:
name index_id type type_desc is_unique is_primary_key
------------------- -------- ---- ------------ --------- --------------
PK_MyTestTable 1 1 CLUSTERED 1 1
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
----------------------- ----------- ---- ------------ --------- -------------- -----------
PK_MyTestTable 1 1 CLUSTERED 1 1 0
UQ_MyTestTable_A_B 2 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B 3 2 NONCLUSTERED 1 0 0
IUN_MyTestTable_A_B_#_C 4 2 NONCLUSTERED 1 0 0
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.Invoice' with unique index 'IUF_Invoice_ParentInvoiceID'.
The statement has been terminated.
name index_id type type_desc is_unique is_primary_key is_disabled
------------------ ----------- ---- ------------- --------- -------------- -----------
UQ_MyTestTable_A_B 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0
name index_id type type_desc is_unique is_primary_key is_disabled
-------------------- ----------- ---- ------------ --------- -------------- -----------
IUN_MyTestTable_A_B2 1 1 CLUSTERED 1 0 0
PK_MyTestTable 2 2 NONCLUSTERED 1 1 0