约束名的取名规则推荐采用:约束类型_约束列,当然你也可以不这么命名,这样单纯是比较方便
如:主键(Primary Key)约束:PK_UserId
唯一(Unique key)约束: UQ_UserCardId
默认(Default Key)约束 DF_UserPasswd
检查(Check Key)约束 CK_Gender
外键(Foreign Key)约束: FK_SortId
--以上加棕色段是我定义表中的列名称
打个比方:add constraint PK_UserId PRIMARY KEY (UserId)
--PK_UserId 是自定义约束名 , PRIMARY KEY 是约束类型, (UserId)是具体的约束声明create table student
(
s_id int,
s_name varchar(20),
s_age int,
s_籍贯 varchar(10) default '汉族',
s_sex char(2) not null,
)
alter table student add constraint Agecheck check(s_age>15 and s_age<30)
alter table student add constraint primaryid primary key(s_id)查询一个表的所有外键
SELECT 主键列ID=brkey
,主键列名=(SELECT name FROM syscolumns WHERE colid=brkey AND id=brkeyid)
,外键表ID=bfkeyid
,外键表名称=object_name(bfkeyid)
,外键列ID=bfkey
,外键列名=(SELECT name FROM syscolumns WHERE colid=bfkey AND id=bfkeyid)
,级联更新=ObjectProperty(aid,'CnstIsUpdateCascade')
,级联删除=ObjectProperty(aid,'CnstIsDeleteCascade')
FROM sysobjects a
join sysforeignkeys b on aid=bconstid
join sysobjects c on aparent_obj=cid
where axtype='f' AND cxtype='U'
and object_name(brkeyid)='titles'
SELECT
FROM information_schemacolumns
WHERE TABLE_CATALOG='数据库名'
AND TABLE_NAME = '表名'
AND COLUMN_NAME='列名'
select
from syscolumns
where id=object_id('tableName') and name='fieldName'
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)