mysql中的5大约束。
1. not null ,约束。
col_a decimal not null
可以使用alter table test111 modify col_a decimal null; 去掉约束。
2. unique 约束
col_a int unique;
或者 unique(col_a);
或者 constraint test_uk unique(col_a, col_b);
alter table test111 add unique(col_a, col_b);
alter table test111 drop index teset3_uk;
3, primary key 主键约束
列级主键约束只能定义一个列。
表级主键可以定义多个列为主键
使用语法同unique一样。
col_a decimal primary key
primary key(col_a);
constraint test_primary primary key(sssss,bbb);
alter table test111 drop primary key; 一个表只能有1个主键
alter table test1111 modify col_c int primary key;
alter table test1111 add primary key (a,b);
注: 自增长列 a int auto_increment
4 foreign key 外键
单列: references pxb(col_a);
组合列:foreign key(col_c) references test111(col_d) on delete cascade; or on delete set null; 前者删除主键的时候,自动删除从表中所有的列; 后者设置为null
加上外检名称 constraint test_fk foreign key (col_d) references teacher(col);
定义外键的时候,可以指定
5. check (emp_salary>0);
6.index 索引。索引可以加速查询速度,一个数据表可以有多个索引。
create index pxb on employ(id,name);
drop index index_name on employ;
mysql 一个表上的索引不同名。 oracle系统中所有表的索引不同名。