mysql建表

MySQL常见的建表选项及约束:

1、create table选项

  1、指定列选项:default、comment

  2、指定表选项:engine、auto_increment、comment

2、create table约束 

  1、not null:非空约束

  2、unique:唯一约束

  3、primary key:主键约束

  4、foreign key:外键

  5、enum、set:枚举

 

DEFAULT:定义列的默认值

  当插入一个新行到表中并且没有给该列明确赋值时,如果定义了列的默认值,将自动得到默认值 ;如果没有,则为null。

create table student(id int, name char(10) default '小明') #建学生表里面有字段id,name,name的默认值为小明

   函数default(column)可以得到一个列的默认值:

select defalut(列名) from

 

COMMENT:给列添加注释

mysql> create table student(id int comment '学号',name char(10) comment '名字');

#查看注释
mysql> select column_name,column_comment from information_schema.columns where table_name = 'student';

 

 

NOT NULL:非空约束,指定某列不能为空

  作用:用于确保当前列的值不为空。

create table student(id int not null,name char(10));

 

PRIMARY KEY:主键约束

  primary key = not null + unique

  主键:用来唯一的标示表中的每一行(类型一般为整型或者字符串)

  具有主键约束的列不允许有null值,并且不允许有重复值;

  每个表最多只允许一个主键(可定义联合主键),主键名总是PRIMARY。

create table student(id int not null primary key);

 

UNIQUE:唯一约束,指定某列和几列组合的数据不能重复

  1.唯一约束是指定table的列或列组合不能重复,保证数据的唯一性,约束的列不允许有重复值;

  2.唯一约束不允许出现重复的值,但是可以为多个null;

  3.同一个表可以有多个唯一约束,多个列组合的约束

  

create table student(id int, name char(10)unique);

insert into student values(1,'a');
insert into student values(2,null);
insert into student values(3,null);

不可以插入重复值但是可以插入多个null

 

auto_increment:自增

create table student (id int not null primary key auto_increment, name char(10)); #创建学生表主键id自增
注意:只有主键才能设置自增

 

foreign key外键约束

外键约束:

  参照完整性约束,保证一个或两个表之间的参照完整性,外键是构建于一个表的两个字段或是两个表的两个字段之间的参照关系。

注意:

  1)具有外键约束的列的值不能随便给,必须满足外键所引用的主键的取值;

  2)一张表中可以定义多个外键;

  3)外键列默认可以给null值。

按照定义,外键必须引用一个主键或者唯一键,引用的主键一般在另外一张表中,也可以是本表的主键(后者称为“自引用”)。

父子表:

  外键所在的表叫做子表、从表

  外键所引用的主键所在的表叫做父表、主表

示例:创建外键约束

父表学生表:
create table student (
id int not null primary key,
name char(10)
);

子表成绩表:
create table score (
id int not null primary key,
grades int, 
student_id int,
foreign key (student_id) references student(id) on delete cascade on update cascade
);

数据:
insert into student values (1,'张三');
insert into student values (2,'李四');
insert into student values (3,'王五');
insert into score values(1,60,1);
insert into score values(2,50,3);
insert into score values(3,60,2);

ON DELETE/ ON UPDATE:级联更新 级联删除接在外键后面
级联更新:更新父表的数据子表也会更新
级联删除: 删除副标的数据子表也会删除
公式:
create 父表(
id int not null primary key,
name char(10)
)

create 子表(
id int not null primary key,
score int,
#外键名 数据类型,
foreign key (外键名) references 主表(关联的外键一般为主键)
);

注意:删表必须先删子表,且外键只支持innodb存储引擎

 

 

enum、set

两者的区别是:

  使用ENUM,只能选一个值;

  使用SET,可以选多个值;

create table student(
id int not null primary key,
name enum('小红','张三','李四')
);
insert into student values(1,'小红')
等同
insert into student values(1,1)
create table student( id int not null primary key, name set('小红','张三','李四') );
insert into student values(1,'小红,李四');
insert into student values(1,'小红');




 

练习

1.
  建表员工表字段有(id,name,age,sex)id为主键且不能为空 name唯一约束 sex为enum类型 age为int类型并不为空 2.
  建工资表,字段有(id,wage)并有一个外键与员工表关联,员工表数据的更新删除能影响到工资表

 

posted @ 2022-03-14 16:30  Ccdjun  阅读(694)  评论(0编辑  收藏  举报