一、实例表创建
创建数据库
create database school;
use school;
1、班级表
create table class(
id int(11) not null auto_increment,
name varchar(10) default null,
primary key (id)
) engine=innoDB charset=utf8mb4
2、老师表
create table teacher(
id int(11) not null auto_increment,
name varchar(10) default null,
phone char(11) default null,
primary key (id)
);
3、学生表
create table student(
id int auto_increment primary key,
name varchar (10),
age int(3),
class_id int,
foreign key(class_id) references class(id)
);
4、多对多关系表
create table class2teacher(
id int auto_increment primary key,
class_id int,
teacher_id int,
foreign key(teacher_id) references teacher(id),
foreign key(class_id) references class(id)
);
实例表添加
class(班级表)
insert into class(name) values
("云计算1810"),
("云计算1901"),
("云计算1902");
teacher(老师表)
insert into teacher(name, age, phone) values
("晗哥", 18, "13733878989"),
("强哥", 28, "15633878989"),
("磊哥", 30, "13933878989"),
("闫老师", 18, "13633878989");
student(学生表)
insert into student(name,age) values
("黛玉", 18, 3), ("钦文", 19, 3),("马邦德", 30, 1),
("九筒", 48, 1),("六子", 36, 2),("汤师爷", 18, 2),
("麻匪", 18,2),
("黛玉", 18,2);
class2teacher(班级到老师多对多关系表)
into class2teacher(class_id,teacher_id) values
(1,1),(1,2),(2,1),(2,2),(2,3),(3,1),(3,3);
单表查询
基础查询
select * from 表
select * from 表 where id > 2
select id,name,age as gg from 表 where id > 2
高级查询
a、条件
select * from 表 where id > 1 and name != '王麻子' and age = 18;
select * from 表 where id between 5 and 16;
select * from 表 where id in (11,22,33)
select * from 表 where id not in (11,22,33)
select * from 表 where id in (select id from 表)
b、通配符
select * from 表 where name like 'sha%' - sha开头的所有(多个字符串)
select * from 表 where name like 'shar_' - sha开头的所有(一个字符)
c、限制
select * from 表 limit 5; - 获取前 5 行
select * from 表 limit 0,2; - 从第 1 行开始, 取出 2 行, 包含第 1 行
select * from 表 limit 2 offset 0 - 从第 1 行开始, 取出 2 行, 包含第 1 行
d、排序
select * from 表 order by 列 asc - 根据 “列” 从小到大排列
select * from 表 order by 列 desc - 根据 “列” 从大到小排列
select * from 表 order by 列1 desc,列2 asc - 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序
e、分组
select age from 表 group by age
select age,id from 表 group by age,id
select age,id from 表 where id > 10 group by age,id order id desc
select age,id,count(*),sum(age),max(age),min(age) from 表 group by age,id
select age from 表 group by age having max(id) > 10
特别的:group by 必须在where之后,order by之前
f、嵌套查询
select * from (select name from t1 where age>18 and age < 25 order by id desc limit 2 ) as tt order by id;
多表查询
微信图片_20190306215745.png
捕获.JPG
创建有外键的表
create table class(
id int not null auto_increment primary key,
class_name varchar(8),
create_date DATE
);
create table teacher(
id int not null auto_increment primary key,
name varchar(12) not null,
class_id int(2) not null,
foreign key (class_id) references class(id) /*mysql 自动给外键起名字teacher_ibfk_1*/
/*给外键命名*/
/*constraint fk_class foreign key (class_id) references class(id)*/
);
增加数据
mysql> insert into class ( class_name, create_date ) values ( '云计算1805', '2018-05-16' );
mysql> insert into class ( class_name, create_date) values ( '云计算1806', '2018.06.16' );
mysql> insert into class ( class_name, create_date ) values ( '云计算1807', '2018_07_16' ),('云计算1808','20180816');
insert into teacher (name, class_id) values ('杨哥', 1);
insert into teacher (name, class_id) values ('强哥', 3);
insert into teacher (name, class_id) values ('磊哥', 02);
连表
/*无对应关系则不显示*/
select A.class_name, B.name
from class as A, teacher as B
Where A.id = B.class_id
/* 内连接 无对应关系则不显示*/
select A.class_name, B.name
from class as A inner join teacher as B
on A.id = B.class_id
/* 左连接 左边的表(A)所有显示,如果右边的表(B)中无对应关系,则值为null*/
select A.class_name, B.name
from class as A left join teacher as B
on A.id = B.class_id
/* 右连接 右边的(B)表所有显示,如果左边的表(A)中无对应关系,则值为 NULL*/
select A.name, B.name
from class as A right join teacher as B
on A.id = B.class_id
Example
select a.class_name,b.name
from class a,teacher b
where a.id=b.class_id;
select a.class_name,b.name
from class a inner join teacher b
on a.id=b.class_id;
select a.class_name,b.name
from class a left join teacher b
on a.id=b.class_id;
select a.class_name,b.name
from class a right join teacher b
on a.id=b.class_id;
组合
/*组合,自动处理重合*/
select class_name
from class
union
select name
from teacher;
/*组合,不处理重合*/
select class_name
from class
union all
select name
from teacher;
拓展语句(可掌握)
select name 名字,age 年龄,count(*) as 总计 from student where id !=7 group by name,age having age=18;
select * from student where id >1 and name != "六子" and age=18;
select * from student where name like '%玉';
select * from student where name like '_玉';
select * from student where name like '__玉';
select * from student where name in ("黛玉","麻匪");
select * from student where id not in (3,5,8,2);
select * from student limit 0,6;
select * from student limit 1,2;
select * from student limit 2,6;
select * from student limit 5 offset 2;
select * from student limit 5 offset 1;
select * from student order by age;
select * from student order by age desc;
select * from student order by age desc,class_id;
select * from student order by name ,class_id desc;
select * from student order by age ,class_id desc;
select age,id from student group by age,id;
select name,age,id from student group by name,age,id;
select name 姓名,age as 年龄 from student group by name,age;
select name 姓名,age as 年龄,count(age) 计数 from student group by name,age;
select name 姓名,age as 年龄,count(age) 计数 from student group by age,name;
select age,id,count(*),sum(age),max(age),min(age) from student group by age,id;
select age from student group by age having max(id) > 10;
select age from student group by age having max(id) >10;
select name,age,count(*) from student where id !=7 group by name,age having age=18;
select name from student where age>18 and age < 25 order by id desc limit 2;
select * from (select name from student where age>18 and age < 25 order by id desc limit 2 ) as tt order by id;;
连表查询拓展练习
select * from class a,student b where a.id=b.id;
select * from class a,student b where a.id=b.class_id;
select a.name,b.name from class a,student b where a.id=b.class_id;
select a.name 班级,b.name 姓名 from class a,student b where a.id=b.class_id;
select * from class a inner join student b on a.id=b.class_id;
常用
select a.name,b.name from class a inner join student b on a.id=b.class_id;
select * from class a left join student b on a.id=b.class_id;
select a.name,b.name from class a left join student b on a.id=b.class_id;
select b.name from class a left join student b on a.id=b.class_id;
select * from class a left join student b on a.id=b.class_id;
select * from class a right join student b on a.id=b.class_id;
select * from student a right join class b on a.class_id=b.id;
/*左连接,不是外键,没有对应关系*/
insert into class (name) values ("云计算1903");
select * from class left join student on class.id=student.class_id;
+----+---------------+------+-----------+------+----------+
| id | name | id | name | age | class_id |
+----+---------------+------+-----------+------+----------+
| 1 | 云计算1810 | 3 | 马邦德 | 30 | 1 |
| 1 | 云计算1810 | 4 | 九筒 | 48 | 1 |
| 2 | 云计算1901 | 5 | 六子 | 36 | 2 |
| 2 | 云计算1901 | 6 | 汤师爷 | 18 | 2 |
| 2 | 云计算1901 | 7 | 麻匪 | 18 | 2 |
| 2 | 云计算1901 | 8 | 黛玉 | 18 | 2 |
| 3 | 云计算1902 | 1 | 黛玉 | 18 | 3 |
| 3 | 云计算1902 | 2 | 钦文 | 19 | 3 |
| 4 | 云计算1903 | NULL | NULL | NULL | NULL |
+----+---------------+------+-----------+------+----------+
select * from student a right join class b on a.class_id=b.id;
select * from student where name like '_玉';
select * from student where name like '__爷';
/*添加一个列的唯一属性*/ 列 原属性 唯一属性
alter table student2 modify column email varchar(32) unique;