约束的补充
unsigned 数字
not null 非空约束
default 设置默认值
unique
唯一
联合唯一
auto_increment
自增 针对int
自带 not null
前提 : 需要设置unique
primary key
相当于 非空 + 唯一
一张表只能有一个,并且必须有一个
联合主键
foreign key
外键约束
约束的字段至少unique
级联删除 on delete cascade
级联更新 on update cascade
foreign关联外键 先创等关联的
#外键约束 : 对应外表中的字段至少是unique的,推荐使用主键作为关联字段
foreign key(字段) references 表名(字段) 左边自己的字段 右边向要关联 另一张表的字段
实列
foreign key(cid) references class2(id)
注意
cid 关联 class2 的id 先 创立 class2 不然报错
create table class3(
id int primary key auto_increment ,
cname char(12) not null unique,
start_date date,
period char(12) ,
course char(12),
teacher char(12)
);
create table student8(
id int primary key auto_increment,
name char(12) not null,
gender enum('male','female'),
cid int,
foreign key(cid) references class3(id)
);
级联删除 更新 可以一起 设置 同步一起更新
在设置关联时 加上 delete cascade或 update cascade 设置 同步一起更新 删除
create table student3(
id int primary key auto_increment,
name char(12) not null,
gender enum('male','female'),
cid int,
foreign key(cid) references class3(id) on delete cascade on update cascade);
数据的插入操作
# 数据的插入操作
将class数据 插入class2数据中#
# insert into class3 select * from class;
# insert into class3(id,name) select id,cname from class;
总结
# 约束
# unsigned 数字
# not null 非空约束
# default 设置默认值
# unique
# 唯一
# 联合唯一
# auto_increment
# 自增 针对int
# 自带 not null
# 前提 : 需要设置unique
# primary key
# 相当于 非空 + 唯一
# 一张表只能有一个,并且必须有一个
# 联合主键
# foreign key
# 外键约束
# 约束的字段至少unique
# 级联删除 on delete cascade
# 级联更新 on update cascade
表与表关系
一对多
先 创立 一张重要的 多(foreign key)关联一这张表
# 校区表 班级表 一对多
# 校区表 一个校区可以有多个班级 一对多
# 班级表 一个班级可不可以对应多个校区
# 校区表 校区id 校区名称 校区城市 校区地址
# 班级表 班级id 班级名称 开班日期 班主任 校区id
# 多(foreign key)关联一这张表
# 班级表创建foreign key关联校区表的校区id字段
多对多
先键完2张表 之后 在 键第3张表 用第3张表关联与 其2张
# 学生表 班级表 多对多
# 站在学生的角度上 一个学生属于多个班级 一对多
# 站在班级的角度上 一个班级可以有多个学生么 多对一
# 学生表 学生id 学生姓名 ...
# 班级表 班级id 班级名称 ...
# 产生第三张表
# 一个字段是外键关联另一个表的主键
# 另一个字段外键关联另一张表的主键
第3张表 可以创建2个外键
一对1
# 学生表 客户表 一对一
# 一个客户对应一个学生
# 学生表gid foreign key 关联客户表id主键
# 并且gid还要设置为unique
作业
分析步骤:
#1、先站在左表的角度去找
是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id)
#2、再站在右表的角度去找
是否右表的多条记录可以对应左表的一条记录,如果是,则证明右表的一个字段foreign key 左表一个字段(通常是id)
#3、总结:
#多对一:
如果只有步骤1成立,则是左表多对一右表
如果只有步骤2成立,则是右表多对一左表
#多对多
如果步骤1和2同时成立,则证明这两张表时一个双向的多对一,即多对多,需要定义一个这两张表的关系表来专门存放二者的关系
#一对一:
如果1和2都不成立,而是左表的一条记录唯一对应右表的一条记录,反之亦然。这种情况很简单,就是在左表foreign key右表的基础上,将左表的外键字段设置成unique即可
先键 班级 在键学生(学生关联与班级)
create table class(cid int primary key auto_increment,
caption char(12));
insert into class values(1,'三年二班'),(2,'一年三班'),(3,'三年一班');
create table student(sid int primary key auto_increment,
sname char(12),
gender set('男','女'),
class_id int,
foreign key(class_id) references class(cid)
);
insert into student values(1,'刚蛋','女',1),
(2,'铁锤','女',1),
(3,'山炮','男',2);
create table teacher(tid int unsigned primary key auto_increment,
tname char(12)
);
insert into teacher values(1,'波多'),
(2,'苍空'),
(3,'饭岛');
create table course(cid int unsigned primary key auto_increment,
tname char(12),
tearch_id int unsigned,
foreign key(tearch_id) references teacher(tid)
);
insert into course values(1,'生物',1),
(2,'体育',1),
(3,'物理',2);
单表查询
# select user(); #查询用户名
# select database();#查询所在库名
# select now();查询当前时间
select 字段名,字段名,字段名 from 表名;
去重 distinct
语法
# select distinct 字段 from 表;
# 对查出来的字段进行去重
列题
# select distinct class_id from student;
联合去重
select distinct sex,post from student;
可以运算
# select emp_name,salary*12 from 表
# 字段salary参与了四则运算
拼接 分隔 没卵用
内存级别的拼接 不会修改表的内容concat(字段,'字符串2',字段)
拼接 语法
# select concat(字段,'字符串2',字段) from 表
实列
# select concat(' : ',salary) as info from employee;
# select concat(emp_name,' : ',salary) info from employee;
分隔 语法
# select concat_ws('分隔符',字符串,字段1,字段2) info from employee;
实列
# select concat_ws('|','信息',emp_name,salary) info from employee;
判断语句
# select(
# case#开始
# when emp_name = 'alex' then #相当于if
# concat(emp_name,'BIGSB')
# when emp_name = 'jingliyang' then #相当于if
# emp_name
# else #相当于elif
# concat(emp_name,'sb')
# end#结束
# ) as new_name#更改表名
# from employee;
where 筛选行
# select * from 表 where 条件
# 范围查询
# > < >= <= = !=/<>
# between a and b
# in (1,2,3,4) n选1
# 模糊查询
# like
# % 一个百分号代表任意长度的任意字符
# 'a%'
# '%ing'
# '%a%'
# _ 一个下划线代表一个任意字符
# 'a__'
# regexp
# '^a'
# '\d+'
# is is not
# is null
# is not null
# 逻辑运算
# and
# or
# not