浅谈SQL语句添加约束

sql中为表加约束的sql语句

–为表userinfo的loginName列加唯一约束

alter table userinfo add constraint UQ_loginName unique(loginName)

–为表userinfo的loginName列加主键约束

alter table userinfo add constraint PK_loginName primary key(loginName)

–为表userinfo的age列添加检查约束

alter table userinfo add constraint CK_age check(age between 0 and 100)

–为表userinfo的address列添加默认约束

alter table userinfo add constraint DF_address default(‘地址不详’) for address

–为表userinfo的classId添加对应classinfo表的classId主键的外键

alter table userinfo add constraint PK_classId foreign key(classId) references classinfo(classId)

–创建聚集索引

create clustered index Idx_Area_ID on Company(Area_ID)

–创建唯一聚集索引

create unique clustered index Idx_Un_Area_ID on Company(Area_ID)

–创建非聚集索引

create nonclustered index Idx_Area_ID on Company(Area_ID)

–删除约束

if exists(select * from sys.default_constraints where name=’DF_address’ and parent_object_id=object_id(‘userinfo’))

begin

alter table userinfo drop constraint DF_address

end
–添加列时设置indentity标识符
alte talbe userinfo add id int identity(种子,增量)

查询数据库名称:

SELECT name FROM master.dbo.sysdatabases

或者执行系统存储过程:exec sp_databases

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值