使用alter命令将已存在的列设置为唯一。 [英] Setting a already existing colum to unique using alter command.

查看:95
本文介绍了使用alter命令将已存在的列设置为唯一。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我想将Student table中的emailId列设置为唯一。



i try

Hi all!!

I want to set my emailId column in Student table to unique.

i tried

ALTER TABLE Student
 MODIFY emailId   varchar(100)     not null unique







但它不起作用。我应该使用什么命令将其设置为唯一???




but it is not working. what command should i use to set it to unique???

推荐答案







Hi,


ALTER TABLE Student ADD  CONSTRAINT UQemail UNIQUE 
(
	emailId
)







祝你好运: - )




Best regards :-)


嗨cheetu2810 :),

如果它不起作用,可能是因为

该列已包含非唯一值?? :(。

您收到了什么错误消息?
Hi cheetu2810 :) ,
if it does not work it is probably because
the column already contains non-unique values ??:( .
What error message you get?


您不能将现有列设置为唯一,除非您确定列值都是唯一的,即使列中的单个字段是不同然后你不能改变列,如果是这样会抛出一个错误



如果所有都是该列中的唯一字段,那么你可以p手动为该列添加唯一约束



ALTER table tablename ADD CONSTRAINT

constraintname UNIQUE NON CLUSTERED





columnname

you cannot set already existing columns to unique unless you are sure that the column values are all unique, even a single field in the column if is different then you can not alter the column, if so it will throw an error

if all are unique fields in that column ,than you can put an "unique" constraint to that column manually

ALTER table tablename ADD CONSTRAINT
constraintname UNIQUE NON CLUSTERED
(

columnname
)


这篇关于使用alter命令将已存在的列设置为唯一。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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