您的位置:首页 > 其它

键的约束及操作

2015-07-29 22:30 134 查看

键的约束及操作

主键约束(不能为空,不能重复):

pk_id int primary key ,


外部添加主键约束:

alter table 表名 change pk_id pk_id int primary key;


使用自动增长:

pk_id int primary key auto_increment,


指定一个种子值和增量值:

pk_id  int identity(500,1) primary key not null;


改变自动增长的初始值:

alter table 表名 auto_increment =100;


删除主键约束:

alter table 表名 drop primary key;


唯一约束(不能重复,可以为空):

unique,


非空约束:

not null,


默认约束:

default "男",


外部添加默认约束:

alter table 表名 change gender gender char(3) default "女";


检查约束:

s_age int      check(s_age>18), --mysql不支持


外键约束:

先建立主表

create table t_class(
pk_id int primary key,
c_name varchar(20) not null
);


外键约束第一种写法

create table t_student8(
cls_id int, //外键名
constraint foreign key(cls_id) references t_class(pk_id)
);


外键约束第二种写法(推荐写法)

alter table 从表 add constraint 约束名字 foreign key(从表外键名)
references 主表(主表主键);


删除外键约束:

alter table t_student8 drop foreign key 外键名;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: