SQL Server数据库SP命令祥解

sp_renamedb ’pubs’,’pub’  --将名为pubs的数据库改名为pub

(3)sp_dboption:显示或更改数据库选项。不能在 master 或 tempdb 数据库上使用 sp_dboption。
例:sp_dboption ’Recruitment’,’READ ONLY’,’TRUE’  --设置Recruitment数据库为READ ONLY

2、表

(1)sp_help:当前数据库中查找对象。
例:sp_help Employee    --显示Employee表的结构

(2)sp_rename:更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称。
例:sp_rename ’employee’,’employ’

3、用户自定义数据类型

(1)sp_addtype:创建用户定义的数据类型。 
例:sp_addtype birthday, datetime, ’NULL’   
--创建了一个名为 birthday 的用户定义数据类型(基于 datetime),该数据类型允许空值

(2)sp_droptype: 删除用户定义的数据类型。
例: sp_droptype ’birthday’    --删除用户定义的数据类型 birthday

(3)sp_help:查看用户自定义数据类型的情况
例:sp_help birthday    --查看用户定义的数据类型 birthday的信息

4、规则

(1)sp_bindrule:将规则绑定到列或用户定义的数据类型。
例:sp_bindrule rulType,’Titles.Type’  --将规则rulType绑定到Titles表的Type列

(2)sp_unbindrule:在当前数据库中为列或用户定义数据类型解除规则绑定。
例:sp_unbindrule ’Titles.Type’  --取消绑定到Tiltes表中的Type列的规则

(3)sp_helptext:查看规则的详细信息。
例:sp_helptext rulType   --查看规则rulType的详细信息

5、缺省

(1)sp_bindefault:将默认值绑定到列或用户定义的数据类型。
(2)sp_unbindefault:在当前数据库中为列或者用户定义数据类型解除(删除)默认值绑定。
(3)sp_helptext:查看默认值的信息。

6、索引

sp_helpindex:报告有关表或视图上索引的信息。
例:sp_helpindex employee   --查看employee表中索引信息

7、视图

sp_helptext:查看视图文本

8、存储过程

sp_helptext:查看存储过程文本

9、触发器

(1)sp_help <触发器名>:查看指定触发器是否存在
(2)sp_helptrigger:返回指定表中定义的当前数据库的触发器类型。

10、其他

sp_help:报告有关数据库对象、用户定义数据类型或 数据类型的信息。
sp_helptext:显示规则、默认值、未加密的存储过程、用户定义函数、触发器或视图的文本。
sp_rename:更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称


约束

exec sp_help             --查看默认约束名

exec sp_helpconstraint '表名'

exec sp_helptext

--(1)主键约束:

alter table student add constraint pk_stuid primary key(stuid)     --创建主键约束

alter table student drop constraint pk_stuid           --删除主键约束

alter table student add constraint pk_stuid_stuname primary key(stuid,stuname)    --设组合键为主键

--(2)外键约束

alter table student add constraint fk_class_num foreign key(class_num) references class(classnum)

alter table student drop constraint fk_class_num

/*特别注意:  外键必须是被引用表的主键*/

--(3)唯一约束

alter table student add constraint uq_stuname unique(stuname)

alter table student drop constraint uq_stuname

--(4)一次增加多个约束

alter table student add constraint pk_stuid primary key(stuid),constraint uq_stuname unique(stuname)

--(5)检查约束

alter table student add constraint check_age check(stuage>0 and stuage<100)

--(6)default 约束

alter table student add constraint df_sex default '男' for sex

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值