将字段约束更改为表 [英] alter the field constraint to a table

查看:98
本文介绍了将字段约束更改为表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个表....但是现在我必须向电话号码字段添加唯一约束....该怎么做?

I have already created a table....but now i have to add unique constraint to field of phoneno....how to do?

推荐答案

请执行以下操作....

Do as follows....

ALTER TABLE MyTable
ADD UNIQUE (phoneno)


您不能在具有多于2条记录的表之后添加任何唯一列,因为唯一约束允许一个空值,并且永远不允许重复
如果您尝试此波纹管查询

you cant add any unique columns after the table having more then 2 records because unique constraint allow one null value and never allow duplicate
if u try this bellow query

alter table sales add snnno int unique



您会收到以下错误



u will get the below error

Server: Msg 1505, Level 16, State 1, Line 1
CREATE UNIQUE INDEX terminated because a duplicate key was found for index ID 43. Most significant primary key is '<NULL>'.
Server: Msg 1750, Level 16, State 1, Line 1
Could not create constraint. See previous errors.
The statement has been terminated.



因此,您为此列创建了一个子表
可能是使用归一化的方式来获取解决方案



so u create one child table for this columns
may be use normalization way u will get solution


像这样尝试

try like this

ALTER TABLE MyTable
ADD AddDate smalldatetime NULL
CONSTRAINT AddDateDflt
DEFAULT getdate() WITH VALUES



另请参见链接 [



Also see this Link[^]

Thanks
--RA


这篇关于将字段约束更改为表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆