SQL server 2005如何设置一个或几个字段唯一约束?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL server 2005如何设置一个或几个字段唯一约束?相关的知识,希望对你有一定的参考价值。
唯一约束是该字段的值不允许重复吗?
参考技术A 可用sql语句添加唯一约束alter table 表名
add constraint 约束名 unique nonclustered(指定唯一约束的列名)
这样就可以了,我们都是用sql语句,在控制台也可以的! 参考技术B 唯一索引和约束
唯一索引和其它索引本质上并没有什么不同,唯一不同的是唯一索引不允许索引键中存在相同的值。因为索引中每一个条目都与表中的行对应。唯一索引不允许重复值被插入索引也就保证了对应的行不允许被插入索引所在的表,这也是为什么唯一索引能够实现主键和候选键。
为表声明主键或唯一约束时,SQL Server会自动创建与之对应的唯一索引。你可以在没有唯一约束的情况下创建唯一索引,但反之则不行。定义一个约束时,SQL Server会自动创建一个与之同名的索引,并且你不能在删除约束之前删除索引。但可以删除约束,删除约束也会导致与之关联的索引被删除。
每个表中可以包含多个唯一索引。比如说AdventureWorks的Product表,含有四个唯一索引,分别是ProductID,ProductNumber,rowguid和ProductNameColumn,设置Product表的人将ProductID作为主键,其它三个作为候选键。
可以通过Create INDEX语句创建唯一索引,比如:
CREATE UNIQUE NONCLUSTERED INDEX [AK_Product_Name] ON Production.Product (
[Name] );
也可以通过直接定义约束创建唯一索引:
ALTER TABLE Production.Product ADD CONSTRAINT PK_Product_ProductID PRIMARY KEY
CLUSTERED ( ProductID );
上面第一种方法,你Prodcut表中不能含有相同的ProductName,第二种情况表中不允许存在相同的ProductID。
因为定义一个主键或是定义约束会导致索引被创建,所以你必须在约束定义时就给出必要的索引信息,因此上面ALTER TABLE语句中包含了”CLUSTERED”关键字。
如果唯一索引或约束所约束的列在当前的表中已经含有了重复值,那么创建索引会失败。 参考技术C 都设成主键
???Sql Server???SQL SERVER ????????????
?????????int with ?????? serve span ?????? inner -- blog
??????:???Sql Server???SQL SERVER ??????????????????SQL SERVER 2005?????????????????????????????????????????????SQL SERVER 2005???????????????CTE?????????????????????CTE?????????????????????
??????CTE??????????????????Common Table Expression ???SQL SERVER 2005????????????????????????????????????
#??????????????????
1???sql
1 Create table GroupInfo([Id] int,[GroupName] nvarchar(50),[ParentGroupId] int) 2 3 Insert GroupInfo 4 5 select 0,??????????????????,null union all 6 7 select 1,??????????????????,0 union all 8 select 2,??????????????????,1 union all 9 select 3,??????????????????,1 union all 10 select 4,????????????????????????,2 union all 11 select 5,????????????????????????,2 union all 12 select 6,????????????????????????,3 union all 13 select 7,????????????????????????,3 union all 14 15 select 8, ???????????????,0 union all 16 select 9, ?????????????????????,8 union all 17 select 10,????????????????????????,8 union all 18 select 11,???????????????????????????,9 union all 19 select 12,???????????????????????????,9 union all 20 select 13,??????????????????????????????,10 union all 21 select 14,??????????????????????????????,10
2????????????
#????????????Demo
1???????????????????????????????????????????????????,???????????????????????????
1 --???????????????????????????????????????????????? 2 with 3 CTE 4 as 5 ( 6 select * from GroupInfo where Id=1 7 union all 8 select G.* from CTE inner join GroupInfo as G 9 on CTE.Id=G.ParentGroupId 10 ) 11 select * from CTE order by Id
1 --???????????????????????????????????????????????? 2 with 3 CTE 4 as 5 ( 6 select * from GroupInfo where Id=14 7 union all 8 select G.* from CTE inner join GroupInfo as G 9 on CTE.ParentGroupId=G.Id 10 ) 11 select * from CTE order by Id
2?????????????????????
1 --?????????????????? 2 with 3 CTE 4 as 5 ( 6 select Id,GroupName,ParentGroupId,GroupPath=CAST( GroupName as nvarchar(max)) from GroupInfo where Id=1 7 union all 8 select G.*,CAST(CTE.GroupPath+???//???+G.GroupName as nvarchar(max)) as GroupPath from CTE 9 inner join GroupInfo as G 10 on CTE.Id=G.ParentGroupId 11 ) 12 select * from CTE
3?????????????????????????????????????????????????????????
1 --??????id????????????????????????????????????sort??????????????????sort?????????????????????????????????????????????????????? 2 WITH 3 CTE 4 AS 5 ( 6 SELECT * ,CAST(RIGHT(???000??? + CAST([Id] AS VARCHAR), 3) AS VARCHAR(MAX)) AS sort FROM GroupInfo 7 WHERE ParentGroupId = 0 8 UNION ALL 9 SELECT GroupInfo.* ,CAST(sort + RIGHT(???000??? + CAST(GroupInfo.[Id] AS VARCHAR),3) AS VARCHAR(MAX)) AS sort 10 FROM CTE 11 INNER JOIN GroupInfo ON CTE.Id = GroupInfo.ParentGroupId 12 ) 13 SELECT * FROM CTE ORDER BY sort
4?????????????????????????????????????????????????????????
1 --?????????????????? 2 WITH CTE AS ( 3 SELECT *,1 AS [Level] FROM GroupInfo WHERE ParentGroupId=0 4 UNION ALL 5 SELECT G.*,CTE.Level+1 FROM GroupInfo as G 6 JOIN CTE ON CTE.Id =G.ParentGroupId 7 ) 8 SELECT * FROM CTE
以上是关于SQL server 2005如何设置一个或几个字段唯一约束?的主要内容,如果未能解决你的问题,请参考以下文章
SQL server中的nvarchar最大的长度是4000个字吗
如何避免从 C# 构建的 Sql Server 2005 参数化查询变慢
如何将 Visual Studio 2010 网站连接到 SQL Server 2005