表的约束条件
not null 与 default:
#not null + default
create table t15(
id int,
name char(16) not null,
sex enum('male','female','other') not null default "male"
);
alter table t15 modify name char(16) not null;
insert into t15(id,name) values
(1,'egon1'),
(2,'egon2'),
(3,'egon3');
_________________________________________________________________________
unique:
#unique:限制字段的值唯一
#单列唯一
create table t16(
id int unique,
name char(16)
);
# 联合唯一
create table server(
id int unique,
ip char(15),
port int,
unique(ip,port)
);
_____________________________________________________________________________________
primary key:
#primary key:单单从约束角度去看,primary key就等同于not null unique
#强调(******)
1、一张表中必须有,并且只能有一个主键
2、一张表中都应该有一个id字段,而且应该把id字段做成主键
create table t17(
id int primary key,
name char(16),
age int,
sex char(6)
)engine=innodb;
#联合主键
create table t19(
ip char(15),
port int,
primary key(ip,port)
);
# primary key auto_increment
create table t20(
id int primary key auto_increment,
name char(16)
)engine=innodb;
# auto_increment注意点:
1、通常与primary key连用,而且通常是给id字段加
2、auto_incremnt只能给被定义成key(unique key,primary key)的字段加