美文网首页
MySQL提高(外键约束)

MySQL提高(外键约束)

作者: 憨猜猜 | 来源:发表于2019-02-26 19:53 被阅读0次

外键约束

1.条件语句的写法
在sql中可以通过'where 条件语句' 来对操作对象进行筛选 -筛选
a.比较运算符:=,<>,<,>,<=,>=
注意:判断一个字段的值是否为空不用使用=和<>,而是使用'is null'和'is not null'

select number from t_course where title is null;  判断是否为null
select number from t_course where tirle='';  --判断是否是空串

b.逻辑运算:and,or,not
c.where 字段名 between 值一 and 值二;  -- 筛选指定的字段的值在值一和值二之间的数据
select title,birth from t_course where birth between '1990-1-1' and '1999-12-31';  

d.where 字段名 in 集合 --筛选出字段值是集合中的元素;(集合是使用括号括起来里面多个值)
select * from t_course where title in ('历史','高数','语文');

e.like操作,上一篇文章

2.数据类型
varchar(size):不定长字符串,size决定的是最大长度
char(size):定常字符
text: 不限长度
int/tinyint(-128~127): 
float(size,d)/double(size,d) - 这的size环绕d的值都有约束效果
bit: 只有0和1两个值
date/datetime/time: 值可以是时间函数的结果,也可以是时间字符串,计算或者是比较的时候内部是按时间处理的

3.去重
select distinct credits from t_course;

添加约束

1.创建表的时候添加约束
create table if not exists t_college
(
collid int,
collname varchar(20) not null,   -- 创建表的时候添加约束
website varchar(1024),
intro varchar(200),
primary key(collid)
)

2.通过添加约束索引的方式添加约束
alter table 表名 add constraint 索引名 约束 (字段名);
说明:索引名 - 自己随便命名约束(字段名);
说明:索引名 - 自己随便命名,用来指向当前添加的约束;约束 - 添加的约束
alter tabler t_college add constraint con_website unique (website); 
给t_college表中的website添加unique的约束,约束索引为con_website

b.删除约束
alter table 表名 drop index 约束索引;用来指向当前添加的约束
alter table t_college   drop index con_website;

外键和E.R图

1.什么是外键:表中的某个字段的值是根据其他表中主键的值来确定的,那么这个字段就是外键
1.1多对一的外键的添加:将外键添加到多的一方对应的表中。
   一对一的外键添加:将外键随便添加到哪一方,同时添加值唯一约束
  多对多的外键添加:关系型数据库中,两张没法实现多对多的关系,需要一个中键表(中间表有两个表的外键分别参照多对多的两个表的主键)

1.2怎么添加转眼间
a.外键对应的字段
alter table t_student add column collid int;

b.给设计好的外键对应的字段添加外键约束
alter table 表1 add constraint fk_collid_stu(##索引名) foreign key(表1字段名1) references 表2(字段2);
将表1中的字段1设置为外键,并且让这个外键的值参照表2中的字段2

c.删除外键约束
alter table 表名 drop  foreign key 外键索引名;
可以删除外键约束,但是外键索引还在,需要把额外的索引删除
注意:外键约束直接删除约束的索引无效,必须先将约束删掉,然后再删除索引
alter table tb_student drop foreign key fk_collid_stu;
alter table tb_student drop index fk_collid_stu;

d.多对多的外键约束

例子

-- =============1.学生表===============
CREATE TABLE IF NOT EXISTS tb_student
(
stuid int not NULL auto_increment,
stuname VARCHAR(20) not NULL,
tel CHAR(11) not NULL,
birth date DEFAULT '2019-10-21',
addr VARCHAR(100),
PRIMARY key(stuid)
);


-- ===============2.课程表================
CREATE TABLE IF NOT EXISTS tb_course
(
couid int NOT NULL auto_increment,
couname VARCHAR(20) NOT NULL,
startdate date NOT NULL DEFAULT '2019-10-21',
intro VARCHAR(200),
credit int NOT NULL,
PRIMARY key(couid)
);  


-- =================3.学院表====================
CREATE TABLE IF NOT EXISTS tb_college
(
collid int auto_increment,
website VARCHAR(200) UNIQUE,
collname VARCHAR(50),
PRIMARY KEY(collid)
); 

-- =================4.老师表====================
CREATE TABLE IF NOT EXISTS tb_teacher
(
teaid int NOT NULL auto_increment,
teaname VARCHAR(20) NOT NULL,
teaage INT,
tel char(11),
PRIMARY KEY(teaid)
);

1.什么是外键:表中的某个字段的值是根据其他表中主键的值来确定的.那么这个字段就是外键
-- 1.1多对一的外键的添加:将外键添加到多的一方对应的表中
-- 1.2怎么添加外键:
-- a.外键对应的字段
alter table t_student add column collid int;
-- b.给设计好的外键对应的字段添加外键约束
-- alter tabler 表1 add constraint fk_collid_stu(##索引名) FOREIGN KEY (表1字段名1)  references 表2(字段2);
-- 将表1中的字段1设置为外键,并且让这个外键的值参照表2中的字段2

-- c.删除外键约束
-- alter table 表名 drop foreign key 外键索引名;  - 可以删除外键约束,但是外键索引还在,需要额外的把索引删掉
-- 注意:外键约束直接删除约束的索引无效,必须先将约束删掉,然后再删除索引
alter table tb_student drop foreign key fk_collid_stu;
alter table tb_stubent drop index fk_collid_stu;


-- 一对多
alter table tb_student add column collid int;

alter table tb_student add CONSTRAINT fk_collid_stu FOREIGN key (collid) REFERENCES tb_college (collid);


alter table tb_teacher add column collid int;

ALTER TABLE tb_teacher add CONSTRAINT fk_collid_tea FOREIGN key (collid) REFERENCES tb_college (collid);

alter table tb_course add column teaid int;
alter table tb_course add CONSTRAINT fk_teaid_cou FOREIGN key (teaid) REFERENCES tb_teacher (teaid);

-- 多对多
create table if not EXISTS tb_score
(
scoreid int not null auto_increment,
mark FLOAT(4,1) comment '分数',
stuid int comment '学生外键',
couid int comment '课程外键',
PRIMARY KEY (scoreid)
); 

alter table tb_score add CONSTRAINT fk_stuid_score FOREIGN key (stuid) REFERENCES tb_student (stuid);

alter table tb_score add CONSTRAINT fk_couid_score FOREIGN key (couid) REFERENCES tb_teacher (couid);

-- 添加成绩
INSERT INTO tb_score (mark) VALUES (89),(45.5),(100),(95),(60),(77),(90),
(97),(49),(66),(56),(71),(80.5),(33),(87),(56),(74),(68),(80),(92),(34),(75); 

select mark from tb_score;  -- 获取表中的所有分数值 
select MAX(mark) as maxmark from tb_score; -- 获取表中所有分数的最大值
select min(mark) from tb_score;
select sum(mark) from tb_score;
select avg(mark) from tb_score;
select count(mark) from tb_score;

查询的高级操作

1.聚合:max/min/sum/avg平均数/count
select 聚合函数(字段) from 表名 where 条件;
按条件对表查询指定字段的数据,然后将查询到的结果做相应的聚合运算,聚合运算的结果是最后的结果
select mark from tb_score;
select max(mark) as maxmark from tb_score;
select min(mark) from tb_score;
select sum(mark) from tb_score;
select avg(mark) from tb_score; 
如果计算平均数的时候如果参与运算的对象是null,那么这个数据不会参与计算
select count(mark) from tb_score;

2.分组
select 字段操作 from 表名 where 条件 group by(字段2);
将指定表中的满足条件的记录按照指定字段进行指定的聚合操作
求每个学生的平均数
select stuid,avg(mark) from tb_score group by(stuid);
注意:a.字段操作的位置除了分组字段不用聚合,其他字段都必须聚合
      b.分组的时候where要放在分组前对需要分组的数据进行筛选

having:分组的时候,在分组后用having代替where来对分组后的数据进行筛选
获取平均成绩最高的学生的id
获取平均成绩大于60分的学生的id
select stuid,avg(mark) from tb_score group by(stuid) having avg(mark)>60;

3.子查询:将一个查询操作的结果作为另外一个查询的数据源
在tb_score表中获取成绩是大于90分的学生的id
select stuid from tb_score where mark>90 and stuid is not null;

获取成绩大于90分的学生的姓名 
select stuname from tb_student where stuid in
(select stuid from tb_score where mark>90 and stuid is not null);

将一个查询结果作为查询对象提供给另外一个查询,但是第一个查询结果需要重命名
select mark from (select stuid,mark from tb_score where mark>90 and stuid is not null)as t2;

TET%W$__[8~]}N%2Z1W706O.png

![FQ@242ABW2]0MR~ZWA%3L.png](https://img.haomeiwen.com/i14827772/53b3ae26058a2cb5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

{P{BCO4B87HY1@Y~Z7$O5VV.png

相关文章

  • MySQL提高(外键约束)

    外键约束 添加约束 外键和E.R图 例子 查询的高级操作 ![FQ@242ABW2]A%3L.png](https...

  • mysql约束

    1, mysql 不支持外键约束 ,条件约束约束分为列级约束,primary 主键约束check 条件约束for...

  • laravel 填充数据前用truncate清空有外键的表时报错

    原理就是在执行truncate之前告诉mysql取消所有外键约束检查,清空之后再恢复外键约束检查。 参见: htt...

  • MySQL外键约束

    1、概念 外键:从表中的公共字段称之为外键好处:保证数据库数据的完整性,不会存在数据丢失的情况缺点:对性能有影响,...

  • mysql 学习语句

    MySQL学习笔记 登录和退出MySQL服务器 基本语法 建表约束 主键约束 唯一主键 非空约束 默认约束 外键约...

  • mysql 添加外键约束错误 1215 Cannot add t

    mysql 添加外键约束 1215 Cannot add the foreign key constraint 1...

  • 设置外键,外键约束值的区别

    laravel 设置外键,并设置外键约束的方式 为联级删除 更新:在使用Navicat for mysql设计表时...

  • mysql 约束

    mysql 中常见的约束: 默认约束 非空约束 主键约束 唯一约束 外键约束 自增长约束 顾命思议,主要讲几个容易...

  • mysql创建外键约束

    语法: 外键约束常用的两种选项 父表更新同时更新子表,父表删除的时候如果子表不存在对应的数据删除成功,如果存在对应...

  • mysql-外键约束

    创建外键 表的结构已经建好,如何修改为外键 create table dep (id int primary ke...

网友评论

      本文标题:MySQL提高(外键约束)

      本文链接:https://www.haomeiwen.com/subject/jgqzyqtx.html