约束
1.主键约束
举例,创建一张student表,将学号设置为主键创建完表之后,通过shwo columns from student查看表结构
create table student( id int primary key, stu_name varchar(20) );
2.唯一约束
举例,创建一个teacher表,id字段自增,tea_name唯一
create table teacher( id int auto_increment primary key, tea_name varchar(20) not null unique key );
3.默认约束
举例,创建一张course表,默认上课时间为40分钟
create table course( id int auto_increment primary key, cou_name varchar(20) not null unique key, time int default 40 ); insert into course(cou_name) values('语文');
4.非空约束
举例,在 "persons" 表创建时在 id列、name列创建 not null 约束:
create table persons( id int not null, p_name varchar(20) not null, deparment varchar(20), address varchar(20), telnum varchar(20) ) desc persons;
5.外键约束
1.父表与子表必须使用相同的存储引擎,而且禁止使用临时表。
2.数据表的存储引擎只能是innodb
3.外键列和参照列必须具有相似的数据类型。其中数字的长度或是否有符号位必须相同;而字符的长度则可以不同。
4.外键列和参照列必须创建索引。如果外键列不存在索引的话,mysql将自动创建索引。
create table school( id int auto_increment primary key, sname varchar(20) not null ); create table student2( id int auto_increment primary key, sid int, foreign key (sid) references school(id) );
外键约束的参照操作
create table student3( id int auto_increment primary key, sid int, foreign key (sid) references school(id) on delete cascade );
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。如果你想了解更多相关内容请查看下面相关链接
如对本文有疑问, 点击进行留言回复!!
MySql添加新用户及为用户创建数据库和给用户分配权限方法介绍
Navicat出现无法远程连接MySql服务器问题的解决办法
网友评论