如果需要修改Sql主键约束,该SQL语句应该怎么写呢?下面就将教您SQL主键约束的修改语句写法,如果您遇到过类似的问题,不妨一看。
–修改SQL主键约束用SQL
–获取SQL主键约束名字 declare @csname varchar(100)
set @csname=”
select @csname=name
FROM sysobjects

WHERE xtype=’PK’ and parent_obj=object_id(‘表名’)
–删除约束
exec(‘alter table表名 drop constraint ‘ + @csname)
–禁用约束(不校验)
exec(‘alter table 表名 nocheck constraint ‘ + @csname)
–启用约束(校验)
exec(‘alter table 表名 check constraint ‘ + @csname) –添加约束 alter table 表名 add constraint主键约束名 primary Key (列名) 例: if not exists ( SELECT *
from syscolumns
where id = object_id(‘accPF_RefFAcctID’)
and name = ‘id’)
ALTER TABLE accPF_RefFAcctID
ADD id INT IDENTITY(1,1) CONSTRAINT PK_accPF_RefFAcctID PRIMARY KEY (id)
【编辑推荐】
如何定义SQL主键
SQL隐性事务实例
SQL事务的操作语法
SQL中CONVERT函数的使用
SQL中NOT EXISTS的使用
SQL 如何给一个表中的一个列中添加多个约束(一个主键约束,一个检查约束),怎么写
不能这么写吧添加完复合主键之后再添加unique约束alter table borrow add cosntraintPK_ID primary key(Rid,bid,lenddate)--添加表级复合主键alter table borrow add constraint CK_ID check(ID>0)--添加标级check约束你那个写法,只能在CREATE table 时候这么写吧,我测试没有通过,只有create时候好用create table tb(co1 int not null)alter table tb add constraint PK_co1 primary key(co1)alter table tb add constraint CK_co1 check(co1<>10)drop table tbcreate table tb(co1 int not null)create table tb(co1 int not null constraintPK_co1 primary key(co1) constraint CK_co1 check(co1<>10))
如何在sql server中设置两个主键
复合主键?1、建表的话,直接primary key (colname1,colname2)2、修改的话alter table tablename add constraint pk_name primary key (colname1,colname2)
数据库表中的主键能不能修改?
可以修改,可以一般不会去修改。 因为主键是数据表中的唯一标识符,不是所有的字段都可以用来当主键的。 所以一般不会去修改它。 一般的方法是先删除主键约束,然后再重新添加。 alter table 表名 drop constraint 主键名修改主键:alter table 表名 add constraint 主键名 primary key (column1,column2,....,column)
发表评论