当前位置:主页 > 查看内容

MySQL数据库进阶版

发布时间:2021-08-09 00:00| 位朋友查看

简介:MySQL数据库进阶版 MySQL数据库进阶版是在MySQL数据库的基础操作的基础上推出的 MySQL数据库的基础操作文章链接 MySQL数据库的基础操作 一、数据库的约束 1. NULL约束 指定属性的语句不能为NULL //建表准备工作 mysql drop table if exists student ; Query……

MySQL数据库进阶版

  • MySQL数据库进阶版是在MySQL数据库的基础操作的基础上推出的
  • MySQL数据库的基础操作文章链接:MySQL数据库的基础操作

一、数据库的约束

  • 1.NULL约束

指定属性的语句不能为NULL

//建表,准备工作
mysql> drop table if exists student;
Query OK, 0 rows affected (0.05 sec)

mysql> create table student(
->id int,
->sn int,
->name varchar(20) not null,
->qq_mail varchar(20)
->);
Query OK, 0 rows affected (0.15 sec)

mysql> show tables;
+-------------------------+
| Tables_in_huashanzhizai |
+-------------------------+
| student                 |
+-------------------------+
1 row in set (0.00 sec)

//此时准备往表里插入元素
mysql> insert into student(id,sn,name,qq_mail)
-> values(1,101,NULL,'123@qq.com');
ERROR 1048 (23000): Column 'name' cannot be null

//但是报错了,原因如下
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | YES  |     | NULL    |       |
| sn      | int(11)     | YES  |     | NULL    |       |
| name    | varchar(20) | NO   |     | NULL    |       |
| qq_mail | varchar(20) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
//name这一列不能为NULL,否则报错,这就是NULL约束
  • 2.唯一约束 unique

指定属性的语句不能插入两次

//建表,准备工作
mysql> drop table if exists student;

Query OK, 0 rows affected (0.02 sec)mysql> create table student(
-> id int,
-> sn int unique,
-> name varchar(20) NOT NULL,
-> qq_mail varchar(20)
-> );
Query OK, 0 rows affected (0.04 sec)

//添加第一条语句
mysql> insert into student(id,sn,name,qq_mail)
-> values(1,101,'bit','123@qq.com');
Query OK, 1 row affected (0.03 sec)

//添加后的效果
mysql> select * from student;
+------+------+------+------------+
| id   | sn   | name | qq_mail    |
+------+------+------+------------+
| 1    | 101  | bit  | 123@qq.com |
+------+------+------+------------+
1 row in set (0.00 sec)

//添加sn和第一条语句相同的第二条语句时:
mysql> insert into student(id,sn,name,qq_mail)
-> values(2,101,'bit2','1232@qq.com');
ERROR 1062 (23000): Duplicate entry '101' for key 'sn'
  • 3.默认值约束 default:

在声明属性的时候就赋值,如果后面添加成员变量,则系统会默认赋值

//建表,准备工作
mysql> drop table if exists student;
Query OK, 0 rows affected (0.03 sec)

mysql> create table student(
-> id int,
-> sn int unique,
-> name varchar(20) NOT NULL,
-> qq_mail varchar(20) default '110@qq.com'
-> );
Query OK, 0 rows affected (0.05 sec)

//第一次给qq_mail赋值的情况:
mysql> insert into student(id,sn,name,qq_mail)
-> values(1,101,'bit','123@qq.com');
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+------+------+------+------------+
| id   | sn   | name | qq_mail    |
+------+------+------+------------+
| 1    | 101  | bit  | 123@qq.com |
+------+------+------+------------+
1 row in set (0.00 sec)

//第二次给qq_mail赋值为NULL的情况:
mysql> insert into student(id,sn,name,qq_mail)
-> values(2,102,'bit2',NULL);
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+------+------+------+------------+
| id   | sn   | name | qq_mail    |
+------+------+------+------------+
| 1    | 101  | bit  | 123@qq.com |
| 2    | 102  | bit2 | NULL       |
+------+------+------+------------+
2 rows in set (0.00 sec)

//第三次不给qq_mail赋值的情况:
mysql> insert into student(id,sn,name)
-> values(3,103,'bit3');
Query OK, 1 row affected (0.02 sec)

mysql> select * from student;
+------+------+------+------------+
| id   | sn   | name | qq_mail    |
+------+------+------+------------+
| 1    | 101  | bit  | 123@qq.com |
| 2    | 102  | bit2 | NULL       |
| 3    | 103  | bit3 | 110@qq.com |
+------+------+------+------------+
3 rows in set (0.00 sec)
//发现不给qq_mail赋值时,系统会自动给qq_mail附上我们最开始设置的默认值
  • 4.主键约束 primary key

是NOT NULL 和 unique的结合, 也就是说,当一个字段被primary key
修饰后,那么这个字段就是不能为空且是独一无二的!!! 一般搭配:auto_increment;

//建表,准备工作
mysql> drop table if exists student;
Query OK, 0 rows affected (0.02 sec)

mysql> create table student(
-> id int primary key auto_increment,
-> sn int unique,
-> name varchar(20) NOT NULL,
-> qq_mail varchar(20) DEFAULT '110@qq.com'
-> );
Query OK, 0 rows affected (0.04 sec)

//当创建好表之后,表中没有任何的数据,当第一次执行插入的时候,当前主键,也就是ID,会自动从1开始
mysql> insert into student(sn,name,qq_mail)
-> values(101,'bit','123@qq.com');
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+------+------+------------+
| id | sn   | name | qq_mail    |
+----+------+------+------------+
| 1  | 101  | bit  | 123@qq.com |
+----+------+------+------------+
1 row in set (0.00 sec)

//清除数据
mysql> delete from student where id = 1;
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
Empty set (0.00 sec)

//当我将刚刚插入的数据删除后,再次进行插入的时候,就会在原来的基础,也就是上一次最后插入的语句的ID上开始加1
mysql> insert into student(sn,name,qq_mail)
-> values(101,'bit','123@qq.com');
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+------+------+------------+
| id | sn   | name | qq_mail    |
+----+------+------+------------+
| 2  | 101  | bit  | 123@qq.com |
+----+------+------+------------+
1 row in set (0.00 sec)
//如果再想让id=1,则需要删除表,重新来!!!
  • 5.外键约束 foreign key

外键用于关联其他表的主键或唯一键

  • 语法:foreign key (字段名) references 主表(列)
//1、建表
mysql> drop table if exists classes;
Query OK, 0 rows affected, 1 warning (0.00 sec)

//先建主表
mysql> create table classes(
-> id int primary key auto_increment,
-> name varchar(20),
-> `desc` varchar(30)
-> );
Query OK, 0 rows affected (0.04 sec)

mysql> drop table if exists student;
Query OK, 0 rows affected, 1 warning (0.00 sec)

//再建附表
mysql> create table student(
-> id int PRIMARY KEY AUTO_INCREMENT,
-> sn int unique,
-> name varchar(20) NOT NULL,qq_mail varchar(20) DEFAULT '110@qq.com',
-> classes_id int,
-> foreign key (classes_id) references classes(id)
-> );
Query OK, 0 rows affected (0.05 sec)

//建表成功
mysql> show tables;
+-------------------------+
| Tables_in_huashanzhizai |
+-------------------------+
| classes				  |
| student				  |
+-------------------------+
2 rows in set (0.00 sec)

//2、插入(可以自增长)
//先插入主表
mysql> insert into classes (name,`desc`) values('java18','今天也要加油鸭!');
Query OK, 1 row affected (0.00 sec)

mysql> select * from classes;
+----+--------+--------------------------+
| id | name   | desc					 |
+----+--------+--------------------------+
| 1  | java18 | 今天也要加油鸭!			 |
+----+--------+--------------------------+
1 row in set (0.00 sec)

//再插入附表
mysql> insert into student (sn,name,qq_mail,classes_id) values
(101,'bit','123@qq.com',1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+------+------+------------+------------+
| id | sn   | name | qq_mail    | classes_id |
+----+------+------+------------+------------+
| 1  | 101  | bit  | 123@qq.com | 1          |
+----+------+------+------------+------------+
1 row in set (0.00 sec)

//附表再次插入
mysql> insert into student (sn,name,qq_mail,classes_id) values
(102,'bit','365@qq.com',1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+------+------+------------+------------+
| id | sn   | name | qq_mail    | classes_id |
+----+------+------+------------+------------+
| 1  | 101  | bit  | 123@qq.com | 1          |
| 2  | 102  | bit  | 365@qq.com | 1          |  //开始自增长
+----+------+------+------------+------------+
2 rows in set (0.00 sec)

//3、删除
//先删除子表中与主表想关联的,或者删除没有与子表关联的主表
mysql> delete from student where id = 1 or id = 2;
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
Empty set (0.00 sec)

//再删除主表
mysql> delete from classes where id = 1;
Query OK, 1 row affected (0.01 sec)

mysql> select * from classes;
Empty set (0.00 sec)
  • 6.check约束
mysql> drop table if exists test_user;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create table test_user (
-> id int,
-> name varchar(20),
-> sex varchar(1),
-> check (sex ='男' or sex='女') //表示在sex只能插入‘男’or‘女’,否则系统报错
-> );
Query OK, 0 rows affected (0.04 sec)

mysql> show tables;
+-------------------------+
| Tables_in_huashanzhizai |
+-------------------------+
| classes                 |
| student                 |
| test_user               |
+-------------------------+
3 rows in set (0.00 sec)

mysql> insert into test_user values(1,'bit','哈');
Query OK, 1 row affected (0.01 sec)
//但是在MySQL上并不会报错!!!

二、进阶查询

建表,准备工作

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| huashanzhizai      |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use huashanzhizai;
Database changed

mysql> show tables;
+-------------------------+
| Tables_in_huashanzhizai |
+-------------------------+
| classes                 |
| exam                    |
| student                 |
| test_user               |
+-------------------------+
4 rows in set (0.00 sec)

mysql> select * from exam;
Empty set (0.00 sec)

mysql> DROP TABLE IF EXISTS exam;
Query OK, 0 rows affected (0.03 sec)

mysql> CREATE TABLE exam (
-> id INT,
-> name VARCHAR(20),
-> chinese DECIMAL(3,1),
-> math DECIMAL(3,1),
-> english DECIMAL(3,1)
-> );
Query OK, 0 rows affected (0.10 sec)

mysql> INSERT INTO exam (id,name, chinese, math, english) VALUES
-> (1,'唐三藏', 67, 98, 56),
-> (2,'孙悟空', 87.5, 78, 77),-> (3,'猪悟能', 88, 98.5, 90),
-> (4,'曹孟德', 82, 84, 67),
-> (5,'刘玄德', 55.5, 85, 45),
-> (6,'孙权', 70, 73, 78.5),
-> (7,'宋公明', 75, 65, 30);
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> select * from exam;
+------+-----------+---------+------+---------+
| id   | name      | chinese | math | english |
+------+-----------+---------+------+---------+
| 1    | 唐三藏     | 67.0    | 98.0 | 56.0    |
| 2    | 孙悟空     | 87.5    | 78.0 | 77.0    |
| 3    | 猪悟能     | 88.0    | 98.5 | 90.0    |
| 4    | 曹孟德     | 82.0    | 84.0 | 67.0    |
| 5    | 刘玄德     | 55.5    | 85.0 | 45.0    |
| 6    | 孙权       | 70.0    | 73.0 | 78.5    |
| 7    | 宋公明     | 75.0    | 65.0 | 30.0    |
+------+-----------+---------+------+---------+
7 rows in set (0.00 sec)
  • 1.聚合函数
//1、查询数据的数量
mysql> select count(*) from exam;
+----------+
| count(*) |
+----------+
| 7		   |
+----------+
1 row in set (0.01 sec)

mysql> select count(0) from exam;
+----------+
| count(0) |
+----------+
| 7        |
+----------+
1 row in set (0.00 sec)

mysql> select count(1) from exam;
+----------+
| count(1) |
+----------+
| 7        |
+----------+
1 row in set (0.00 sec)

//count(*)中*并不特指
//又例如:
mysql> insert into exam (id,name,chinese,math,english) values
-> (1,'唐三藏',67,98,56);
Query OK, 1 row affected (0.01 sec)

mysql> select count(*) from exam;
+----------+
| count(*) |
+----------+
| 8        |
+----------+
1 row in set (0.00 sec)

//2、对计数的列进行去重
mysql> select * from exam;
+------+-----------+---------+------+---------+
| id   | name      | chinese | math | english |
+------+-----------+---------+------+---------+
| 1    | 唐三藏     | 67.0    | 98.0 | 56.0    |
| 2    | 孙悟空     | 87.5    | 78.0 | 77.0    |
| 3    | 猪悟能     | 88.0    | 98.5 | 90.0    |
| 4    | 曹孟德     | 82.0    | 84.0 | 67.0    |
| 5    | 刘玄德     | 55.5    | 85.0 | 45.0    |
| 6    | 孙权       | 70.0    | 73.0 | 78.5    |
| 7    | 宋公明     | 75.0    | 65.0 | 30.0    |
| 1    | 唐三藏     | 67.0    | 98.0 | 56.0    |
+------+-----------+---------+------+---------+
8 rows in set (0.00 sec)

mysql> select count(distinct id) from exam; **先去重再计数
+--------------------+
| count(distinct id) |
+--------------------+
| 7                  |
+--------------------+
1 row in set (0.01 sec)

//3、sum 加权求和
mysql> select sum(math) from exam;
+-----------+
| sum(math) |
+-----------+
| 679.5     |
+-----------+
1 row in set (0.00 sec)

mysql> select sum(math)from exam where math < 70;
+-----------+
| sum(math) |
+-----------+
| 65.0      |
+-----------+
1 row in set (0.00 sec)

//4、avg 查询平均成绩
mysql> select avg(math) from exam;
+-----------+
| avg(math) |
+-----------+
| 84.93750  |
+-----------+
1 row in set (0.00 sec)

//5、max 查询最大值
mysql> select max(math) from exam;
+-----------+
| max(math) |
+-----------+
| 98.5      |
+-----------+
1 row in set (0.00 sec)

//6、min 查询最小值
mysql> select min(math) from exam;
+-----------+
| min(math) |
+-----------+
| 65.0      |
+-----------+
1 row in set (0.00 sec)

//注意点:在where后面,不要出现聚合函数
mysql> select id,name from exam where max(math) > 60;
ERROR 1111 (HY000): Invalid use of group function
  • 2.分组 group by
    准备数据
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| huashanzhizai      |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use huashanzhizai;
Database changed

mysql> drop tables if exists emp;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> create table emp(
-> id int primary key auto_increment,
-> name varchar(20) not null,
-> role varchar(20) not null,
-> salary numeric(11,2)
-> );
Query OK, 0 rows affected (0.08 sec)

mysql> insert into emp(name, role, salary) values
-> ('鹏哥','讲师', 1000.20),
-> ('高博','讲师', 2000.99),
-> ('老汤','讲师', 999.11),
-> ('静静','班主任', 333.5),
-> ('莎莎姐','班主任', 700.33),
-> ('隔壁老王','市场', 12000.66);
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> select * from emp;
+----+--------------+-----------+
| id | name | role  | salary    |       
+----+--------------+-----------+
| 1  | 鹏哥 | 讲师   | 1000.20   |
| 2  | 高博 | 讲师   | 2000.99   |
| 3  | 老汤 | 讲师   | 999.11    |
| 4  | 静静 | 班主任 | 333.50    |
| 5  | 莎莎姐 | 班主任 | 700.33  |
| 6 | 隔壁老王 | 市场 | 12000.66 |
+----+--------------+----------+
6 rows in set (0.00 sec)

查询每个角色的最高工资、最低工资和平均工资

mysql> select role,max(salary),min(salary),avg(salary) from emp group by role;
+-----------+-------------+-------------+--------------+
| role      | max(salary) | min(salary) | avg(salary)  |
+-----------+-------------+-------------+--------------+
| 市场       | 12000.66    | 12000.66    | 12000.660000 |
| 班主任     | 700.33      | 333.50      | 516.915000   | //同为班主任,这个类的最高,最低,平均
| 讲师       | 2000.99     | 999.11      | 1333.433333  |
+-----------+-------------+-------------+--------------+
3 rows in set (0.03 sec)
//不正确表达
//区别
mysql> select max(salary),min(salary),avg(salary)from emp;
+-------------+-------------+-------------+
| max(salary) | min(salary) | avg(salary) |
+-------------+-------------+-------------+
| 12000.66    | 333.50      | 2839.131667 | //这个是在一个表中的最高,最低,平均
+-------------+-------------+-------------+
1 row in set (0.00 sec)
  • 过滤条件 having
mysql> select role,max(salary),min(salary),avg(salary) from emp group by role
having avg(salary) < 1500;
+-----------+-------------+-------------+-------------+
| role      | max(salary) | min(salary) | avg(salary) |
+-----------+-------------+-------------+-------------+
| 班主任     | 700.33      | 333.50      | 516.915000  |
| 讲师       | 2000.99     | 999.11      | 1333.433333 |
+-----------+-------------+-------------+-------------+
2 rows in set (0.00 sec)
  • 3.联合查询

( 两张表或者两张以上的表,进行连接查询)

准备工作

mysql> drop database if exists test0311;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create database test0311;
Query OK, 1 row affected (0.01 sec)

mysql> use test0311;
Database changed

mysql> drop table if exists classes;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create table classes(
-> id int primary key auto_increment,
-> name varchar(50),
-> `desc` varchar(50)
-> );
Query OK, 0 rows affected (0.07 sec)

mysql> insert into classes(name, `desc`) values
-> ('计算机系2019级1班', '学习了计算机原理、C和Java语言、数据结构和算法'),
-> ('中文系2019级3班','学习了中国传统文学'),
-> ('自动化2019级5班','学习了机械自动化');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> drop table if exists student;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create table student(
-> id int primary key auto_increment,
-> sn int,
-> name varchar(30),
-> qq_mail varchar(30),
-> classes_id int
-> );
Query OK, 0 rows affected (0.09 sec)

mysql> insert into student(sn, name, qq_mail, classes_id) values
-> ('09982','黑旋风李逵','xuanfeng@qq.com',1),
-> ('00835','菩提老祖',null,1),
-> ('00391','白素贞',null,1),
-> ('00031','许仙','xuxian@qq.com',1),
-> ('00054','不想毕业',null,1),
-> ('51234','好好说话','say@qq.com',2),
-> ('83223','tellme',null,2),
-> ('09527','老外学中文','foreigner@qq.com',2);
Query OK, 8 rows affected (0.01 sec)
Records: 8 Duplicates: 0 Warnings: 0

mysql> drop table if exists course;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create table course(
-> id int primary key auto_increment,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.05 sec)

mysql> insert into course(name) values
-> ('Java'),('中国传统文化'),('计算机原理'),('语文'),('高阶数学'),('英文');
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> drop table if exists score;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create table score(
-> id int primary key auto_increment,
-> score DECIMAL,
-> student_id int,
-> course_id int
-> );
Query OK, 0 rows affected (0.04 sec)

mysql> insert into score(score, student_id, course_id) values
-> -- 黑旋风李逵
-> (70.5, 1, 1),(98.5, 1, 3),(33, 1, 5),(98, 1, 6),
-> -- 菩提老祖-> (60, 2, 1),(59.5, 2, 5),
-> -- 白素贞
-> (33, 3, 1),(68, 3, 3),(99, 3, 5),
-> -- 许仙
-> (67, 4, 1),(23, 4, 3),(56, 4, 5),(72, 4, 6),
-> -- 不想毕业
-> (81, 5, 1),(37, 5, 5),
-> -- 好好说话
-> (56, 6, 2),(43, 6, 4),(79, 6, 6),
-> -- tellme
-> (80, 7, 2),(92, 7, 6);
Query OK, 20 rows affected, 3 warnings (0.01 sec)
Records: 20 Duplicates: 0 Warnings: 3

//查看已建好的表
mysql> show tables;
+--------------------+
| Tables_in_test0311 |
+--------------------+
| classes            |
| course             |
| score              |
| student            |
+--------------------+
4 rows in set (0.00 sec)
mysql> select * from classes;
+----+-------------------------+-------------------------------------------------------------------+
| id | name                    |desc 															   |
+----+-------------------------+-------------------------------------------------------------------+
| 1 | 计算机系20191| 学习了计算机原理、C和Java语言、数据结构和算法						   |
| 2 | 中文系20193| 学习了中国传统文学												   |
| 3 | 自动化20195| 学习了机械自动化												   |
+----+-------------------------+-------------------------------------------------------------------+
3 rows in set (0.00 sec)

mysql> select * from course;
+----+--------------------+
| id | name               |
+----+--------------------+
| 1  | Java               |
| 2  | 中国传统文化        |
| 3  | 计算机原理          |
| 4  | 语文               |
| 5  | 高阶数学            |
| 6  | 英文                |
+----+--------------------+
6 rows in set (0.00 sec)
mysql> select * from score;
+----+-------+------------+-----------+
| id | score | student_id | course_id |
+----+-------+------------+-----------+
| 1  | 71    | 1          | 1         |
| 2  | 99    | 1          | 3         |
| 3  | 33    | 1          | 5         |
| 4  | 98    | 1          | 6         |
| 5  | 60    | 2          | 1         |
| 6  | 60    | 2          | 5         |
| 7  | 33    | 3          | 1         |
| 8  | 68    | 3          | 3         |
| 9  | 99    | 3          | 5         |
| 10 | 67    | 4          | 1         |
| 11 | 23    | 4          | 3         |
| 12 | 56    | 4          | 5         |
| 13 | 72    | 4          | 6         |
| 14 | 81    | 5          | 1         |
| 15 | 37    | 5          | 5         |
| 16 | 56    | 6          | 2         |
| 17 | 43    | 6          | 4         |
| 18 | 79    | 6          | 6         |
| 19 | 80    | 7          | 2         |
| 20 | 92    | 7          | 6         |
+----+-------+------------+-----------+
20 rows in set (0.00 sec)

mysql> select * from student;
+----+-------+-----------------+------------------+------------+
| id | sn    | name            | qq_mail          | classes_id |
+----+-------+-----------------+------------------+------------+
| 1  | 9982  | 黑旋风李逵        | xuanfeng@qq.com | 1         |
| 2  | 835   | 菩提老祖         | NULL             | 1         |
| 3  | 391   | 白素贞           | NULL             | 1         |
| 4  | 31    | 许仙            | xuxian@qq.com     | 1         |
| 5  | 54    | 不想毕业         | NULL             | 1         |
| 6  | 51234 | 好好说话         | say@qq.com       | 2         |
| 7  | 83223 | tellme          | NULL             | 2         |
| 8  | 9527  | 老外学中文       | foreigner@qq.com | 2         |
+----+-------+-----------------+------------------+------------+
8 rows in set (0.00 sec)

内连接

语法1:select 字段 from 表1 别名1 [inner] join 表2 别名2 on 连接条件 and 其他条件;
语法2:select 字段 from 表1 别名1,表2 别名2 where 连接条件 and 其他条 件;

查询“许仙”同学的 成绩(逐渐增加约束条件)

语法1:

//查询student和score两个表所有的笛卡尔积
mysql> select * from student inner join score;
...//太多,就不一一展示了
...
...
160 rows in set (0.01 sec)

//显示student表中id和score表中student_id两个相等的对应的值
mysql> select * from student inner join score on student.id = score.student_id;
+----+-------+-----------------+-----------------+------------+----+-------+------------+-----------+
| id | sn    | name            | qq_mail         | classes_id | id | score |student_id  | course_id |
+----+-------+-----------------+-----------------+------------+----+-------+------------+-----------+
| 1  | 9982  | 黑旋风李逵 	   | xuanfeng@qq.com | 1		  | 1  | 71    | 1  		| 1 		|
| 1  | 9982  | 黑旋风李逵 	   | xuanfeng@qq.com | 1          | 2  | 99    | 1  		| 3			|
| 1  | 9982  | 黑旋风李逵	   | xuanfeng@qq.com | 1 		  | 3  | 33    | 1   		| 5  		|
| 1  | 9982  | 黑旋风李逵 	   | xuanfeng@qq.com | 1 		  | 4  | 98    | 1   		| 6  		|
| 2  | 835   | 菩提老祖 		   | NULL 			 | 1		  | 5  | 60    | 2   		| 1  		|
| 2  | 835   | 菩提老祖 		   | NULL 			 | 1 		  | 6  | 60    | 2   		| 5  		|
| 3  | 391   | 白素贞 		   | NULL		     | 1 		  | 7  | 33    | 3   		| 1  		|
| 3  | 391   | 白素贞 		   | NULL 			 | 1 		  | 8  | 68    | 3  		| 3  		|
| 3  | 391   | 白素贞		   | NULL 			 | 1		  | 9  | 99    | 3   	    | 5  		|
| 4  | 31    | 许仙 		   | xuxian@qq.com   | 1 		  | 10 | 67    | 4  		| 1  		|
| 4  | 31    | 许仙 		   | xuxian@qq.com   | 1		  | 11 | 23    | 4  		| 3  		|
| 4  | 31    | 许仙			   | xuxian@qq.com   | 1 		  | 12 | 56    | 4 		    | 5  		|
| 4  | 31    | 许仙			   | xuxian@qq.com   | 1		  | 13 | 72    | 4 		    | 6  		|
| 5  | 54    | 不想毕业		   | NULL			 | 1		  | 14 | 81    | 5  		| 1  		| 
| 5  | 54    | 不想毕业		   | NULL 			 | 1 		  | 15 | 37    | 5  		| 5  		|
| 6  | 51234 | 好好说话		   | say@qq.com 	 | 2		  | 16 | 56	   | 6  		| 2  		|
| 6  | 51234 | 好好说话 		   | say@qq.com 	 | 2 		  | 17 | 43    | 6  		| 4  		|
| 6  | 51234 | 好好说话 		   | say@qq.com 	 | 2 		  | 18 | 79    | 6  		| 6  		|
| 7  | 83223 | tellme  	 	   | NULL			 | 2 		  | 19 | 80    | 7  		| 2  		|
| 7  | 83223 | tellme 		   | NULL			 | 2 		  | 20 | 92    | 7  		| 6  		|
+----+-------+-----------------+-----------------+------------+----+-------+------------+-----------+
20 rows in set (0.01 sec)

//显示student和score两个表的所有内容
mysql> select * from student inner join score on student.id = score.student_id
and student.id = 4;
+----+------+--------+---------------+------------+----+-------+------------+-----------+
| id | sn   | name   | qq_mail       | classes_id | id | score | student_id |course_id  |
+----+------+--------+---------------+------------+----+-------+------------+-----------+
| 4  | 31   | 许仙   | xuxian@qq.com  | 1          | 10 | 67   | 4			| 1  		|
| 4  | 31   | 许仙   | xuxian@qq.com  | 1          | 11 | 23   | 4			| 3  		|
| 4  | 31   | 许仙   | xuxian@qq.com  | 1          | 12 | 56   | 4			| 5  		|
| 4  | 31   | 许仙   | xuxian@qq.com  | 1          | 13 | 72   | 4			| 6  		|
+----+------+--------+---------------+------------+----+-------+------------+-----------+
4 rows in set (0.01 sec)

//显示student和score两个表的选定的内容
mysql> select student.id,student.name,score.score from student inner join score
on student.id = score.student_id and student.id = 4;
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
| 4  | 许仙   | 67    |
| 4  | 许仙   | 23    |
| 4  | 许仙   | 56    | 
| 4  | 许仙   | 72    |
+----+--------+-------+
4 rows in set (0.00 sec)

//用score表的course_id将course表也连接起来
mysql> select student.id,student.name,score.score,course.name from student inner
join score on student.id = score.student_id inner join course on score.course_id
= course.id and student.id = 4;
+----+--------+-------+-----------------+
| id | name   | score | name            |
+----+--------+-------+-----------------+
| 4  | 许仙   | 67     | Java 			|
| 4  | 许仙   | 23     | 计算机原理 		|
| 4  | 许仙   | 56     | 高阶数学 		|
| 4  | 许仙   | 72     | 英文 			|
+----+--------+-------+-----------------+
4 rows in set (0.00 sec)

//修改名字
mysql> select student.id,student.name as 姓名,score.score as 分数,course.name as 课程表
from student inner join score on student.id = score.student_id inner join course
on score.course_id = course.id and student.id = 4;
+----+--------+--------+-----------------+
| id | 姓名    | 分数   | 课程表           |
+----+--------+--------+-----------------+
| 4  | 许仙    | 67    | Java 			 |
| 4  | 许仙    | 23    | 计算机原理 		 |
| 4  | 许仙    | 56    | 高阶数学		 |
| 4  | 许仙    | 72    | 英文			 |
+----+--------+--------+-----------------+
4 rows in set (0.00 sec)

语法2:

mysql> select student.id,student.name as 姓名,score.score as 分数,course.name as 课程表
-> from student,score,course where student.id = score.student_id
-> and score.course_id = course.id
-> and student.id = 4;
+----+--------+--------+-----------------+
| id | 姓名   | 分数    | 课程表			 |
+----+--------+--------+-----------------+
| 4  | 许仙   | 67     | Java 			 |
| 4  | 许仙   | 23	   | 计算机原理		 |
| 4  | 许仙   | 56 	   | 高阶数学 		 |
| 4  | 许仙   | 72 	   | 英文 			 |
+----+--------+--------+-----------------+
4 rows in set (0.00 sec)

外连接

( 外连接分为左外连接和右外连接。如果联合查询,左侧的表完全显示我们就说是左 外连接;右侧的表完全显示我们就说是右外连接)

查询所有同学的成绩,及同学的个人信息,如果该同学没有成绩,也需要显示

①左外连接

mysql> select * from student,score;
...
...//结果太多不一一展示
...
160 rows in set (0.01 sec)

//通过student表中的id进行排序
mysql> select * from student,score group by student.id;
+----+-------+-----------------+------------------+------------+----+-------+------------+-----------+
| id | sn    | name            | qq_mail 		  | classes_id | id | score |student_id  | course_id |
+----+-------+-----------------+------------------+------------+----+-------+------------+-----------+
| 1 | 9982   | 黑旋风李逵 	   | xuanfeng@qq.com  | 1 			| 1 | 71	| 1 		 | 1 		 |
| 2 | 835    | 菩提老祖 		   | NULL 			  | 1 			| 1 | 71	| 1			 | 1 	     |
| 3 | 391    | 白素贞 	       | NULL 			  | 1 			| 1 | 71	| 1 		 | 1 		 |
| 4 | 31     | 许仙 		   | xuxian@qq.com    | 1 			| 1 | 71	| 1 		 | 1 		 |
| 5 | 54     | 不想毕业 		   | NULL 			  | 1 			| 1 | 71	| 1 		 | 1 	     |
| 6 | 51234  |  好好说话        | say@qq.com		  | 2 			| 1 | 71	| 1 		 | 1 	 	 |
| 7 | 83223  | tellme		   | NULL 			  | 2 			| 1 | 71	| 1 		 | 1 		 |
| 8 | 9527   | 老外学中文	   | foreigner@qq.com | 2 			| 1 | 71	| 1 		 | 1 		 |
+----+-------+-----------------+------------------+------------+----+-------+------------+-----------+
8 rows in set (0.01 sec)

//此时发现student表中的name老外学中文这一项没有了,即没有score.student_id与之匹配
mysql> select * from student,score where student.id = score.student_id group by
student.id;
+----+-------+-----------------+-----------------+------------+----+-------+------------+-----------+
| id | sn    | name 		   | qq_mail 		 | classes_id | id | score |student_id  | course_id |
+----+-------+-----------------+-----------------+------------+----+-------+------------+-----------+
| 1  | 9982  | 黑旋风李逵	   | xuanfeng@qq.com | 1		  | 1  | 71    | 1 			| 1 		|
| 2  | 835   | 菩提老祖 		   | NULL			 | 1		  | 5  | 60		| 2			 | 1 		|
| 3  | 391   | 白素贞		   | NULL			 | 1 		  | 7  | 33		| 3			 | 1 		|
| 4  | 31    | 许仙 		   | xuxian@qq.com   | 1 		  | 10 | 67		| 4 		| 1 		|		
| 5  | 54    | 不想毕业		   | NULL 			 | 1 		  | 14 | 81		| 5 		| 1 		|
| 6  | 51234 | 好好说话 		   | say@qq.com      | 2 		  | 16 | 56		| 6 		| 2		    |
| 7  | 83223 | tellme		   | NULL 			 | 2		  | 19 | 80		| 7 		| 2 		|
+----+-------+-----------------+-----------------+------------+----+-------+------------+-----------+
7 rows in set (0.00 sec)//利用左外连接可以将其进行显示

mysql> select * from student left join score on student.id = score.student_id
group by student.id;
+----+-------+-----------------+------------------+------------+------+-------+------------+-----------+
| id | sn    | name 		   | qq_mail		  | classes_id | id   | score |student_id  | course_id |
+----+-------+-----------------+------------------+------------+------+-------+------------+-----------+
| 1  | 9982  | 黑旋风李逵 	   | xuanfeng@qq.com  | 1 			| 1   | 71	  | 1 			| 1 		|
| 2  | 835   | 菩提老祖 		   | NULL 			  | 1 			| 5   | 60	  | 2 			| 1 		|
| 3  | 391   | 白素贞 		   | NULL 			  | 1 			| 7   | 33	  | 3 			| 1 		|
| 4  | 31    | 许仙 		   | xuxian@qq.com    | 1 			| 10  | 67	  | 4 			| 1 		|
| 5  | 54    | 不想毕业 		   | NULL 			  | 1 			| 14  | 81	  | 5 			| 1 		|
| 6  | 51234 | 好好说话 		   | say@qq.com       | 2			 | 16 | 56	  | 6 			| 2 		|
| 7  | 83223 | tellme		   | NULL 			  | 2 			 | 19 | 80	  | 7 			| 2 		|
| 8  | 9527  | 老外学中文 	   | foreigner@qq.com | 2 			| NULL | NULL | NULL 		| NULL 		|
+----+-------+-----------------+------------------+------------+------+-------+------------+-----------+
8 rows in set (0.00 sec)

②右外连接

mysql> select * from score right join student on student.id = score.student_id
group by student.id;
+------+-------+------------+-----------+----+-------+-----------------+------------------+------------+
| id   | score | student_id | course_id | id | sn    |  name 			|qq_mail 		  | classes_id |
+------+-------+------------+-----------+----+-------+-----------------+------------------+------------+
| 1    | 71 	| 1 		| 1 		| 1  | 9982  | 黑旋风李逵 		|xuanfeng@qq.com  | 1			|
| 5    | 60 	| 2 		| 1 		| 2  | 835   | 菩提老祖 			|NULL 			  | 1		   |
| 7    | 33 	| 3 		| 1 		| 3  | 391   | 白素贞			 |NULL 			  | 1 			|
| 10   | 67 	| 4 		| 1 		| 4  | 31    | 许仙 			|xuxian@qq.com    | 1 			|
| 14   | 81 	| 5 		| 1 		| 5  | 54    | 不想毕业 			|NULL 			  | 1 			|
| 16   | 56 	| 6 		| 2 		| 6  | 51234 | 好好说话 			|say@qq.com 	  | 2 			|
| 19   | 80 	| 7 		| 2 		| 7  | 83223 | tellme 			|NULL			  | 2 			|
| NULL | NULL   | NULL 	    | NULL 		| 8  | 9527  | 老外学中文 		|foreigner@qq.com | 2 			|
+------+-------+------------+-----------+----+-------+-----------------+------------------+------------+
8 rows in set (0.00 sec)

自连接 ( 自连接是指在同一张表连接自身进行查询)

显示所有“计算机原理”成绩比“Java”成绩高的成绩信息

mysql> select * from score as s1,score as s2 //这时候因为两个表名字一样,所以需要起别名
-> where s1.student_id = 1
-> and s2.student_id = 3
-> and s1.score < s2.score;
+----+-------+------------+-----------+----+-------+------------+-----------+
| id | score | student_id | course_id | id | score | student_id | course_id |
+----+-------+------------+-----------+----+-------+------------+-----------+
| 3  | 33 	 | 1 		  | 5		  | 8  | 68    | 3		    | 3 		|
| 1  | 71	 | 1 		  | 1 		  | 9  | 99    | 3 		    | 5 		|
| 3  | 33	 | 1 		  | 5 		  | 9  | 99    | 3		    | 5 		|
| 4  | 98 	 | 1		  | 6 		  | 9  | 99    | 3		    | 5 		|
+----+-------+------------+-----------+----+-------+------------+-----------+
4 rows in set (0.00 sec)

//改进
mysql> select s2.* from score as s1,score as s2
->where s1.student_id = 1
->and s2.student_id = 3
->and s1.score < s2.score;
+----+-------+------------+-----------+
| id | score | student_id | course_id |
+----+-------+------------+-----------+
| 8  | 68    | 3 		  | 3 		  |
| 9  | 99 	 | 3 		  | 5		  |
| 9  | 99	 | 3		  | 5		  |
| 9  | 99 	 | 3 		  | 5		  |
+----+-------+------------+-----------+
4 rows in set (0.00 sec)

子查询( 子查询是指嵌入在其他sql语句中的select语句,也叫嵌套查询)

查询与“不想毕业” 同学的同班同学

mysql> select * from student where classes_id = (select classes_id from student
where name = '不想毕业');
+----+------+-----------------+-----------------+------------+
| id | sn   | name 			  | qq_mail 		| classes_id |
+----+------+-----------------+-----------------+------------+
| 1  | 9982 | 黑旋风李逵 	  | xuanfeng@qq.com | 1 		 |
| 2  | 835  | 菩提老祖 		  | NULL 		    | 1 		 |
| 3  | 391  | 白素贞			  | NULL 			| 1			 |
| 4  | 31   | 许仙 			  | xuxian@qq.com   | 1 		 |
| 5  | 54   | 不想毕业		  | NULL 			| 1 		 |
+----+------+-----------------+-----------------+------------+
5 rows in set (0.01 sec)

//这句话等价于
mysql> select * from student where classes_id = 1;
+----+------+-----------------+-----------------+------------+
| id | sn   | name 			  | qq_mail 		| classes_id |
+----+------+-----------------+-----------------+------------+
| 1  | 9982 | 黑旋风李逵		  | xuanfeng@qq.com | 1 		 |
| 2  | 835  | 菩提老祖 		  | NULL 			| 1			 |
| 3  | 391  | 白素贞 		  | NULL		    | 1			 |
| 4  | 31   | 许仙 			  | xuxian@qq.com   | 1			 |
| 5  | 54   | 不想毕业 		  | NULL 		    | 1			 |
+----+------+-----------------+-----------------+------------+
5 rows in set (0.00 sec)

//多行子查询:返回多行记录的子查询,查询“语文”或“英文”课程的成绩信息使用in
mysql> select * from score where course_id in(select id from course where name =
'语文' or name='英文');
+----+-------+------------+-----------+
| id | score | student_id | course_id |
+----+-------+------------+-----------+
| 4  | 98    | 1		  | 6 		  |
| 13 | 72    | 4 		  | 6		  |
| 17 | 43    | 6 		  | 4		  |
| 18 | 79    | 6		  | 6		  |
| 20 | 92    | 7		  | 6		  |
+----+-------+------------+-----------+
5 rows in set (0.00 sec)

//使用 not in
mysql> select * from score where course_id not in(select id from course where
name != '语文' and name !='英文');
+----+-------+------------+-----------+
| id | score | student_id | course_id |
+----+-------+------------+-----------+
| 4  | 98    | 1 		  | 6		  |
| 13 | 72    | 4 		  | 6		  |
| 17 | 43    | 6 	 	  | 4		  |
| 18 | 79    | 6 		  | 6		  |
| 20 | 92    | 7 		  | 6		  |
+----+-------+------------+-----------+
5 rows in set (0.00 sec)

//使用not exists(只要括号内的表达式为true,则就执行括号外的语句)
mysql> select * from student where exists(select id from student where id = 1);
+----+-------+-----------------+------------------+------------+
| id | sn    | name 		   | qq_mail		  | classes_id |
+----+-------+-----------------+------------------+------------+
| 1  | 9982  | 黑旋风李逵 	   | xuanfeng@qq.com  | 1 		   |
| 2  | 835   | 菩提老祖 		   | NULL 			  | 1		   |
| 3  | 391   | 白素贞		   | NULL 			  | 1 		   |
| 4  | 31    | 许仙			   | xuxian@qq.com    | 1 		   |
| 5  | 54    | 不想毕业		   | NULL 			  | 1 		   |
| 6  | 51234 | 好好说话 		   | say@qq.com       | 2 		   |
| 7  | 83223 | tellme 		   | NULL 		      | 2 		   |
| 8  | 9527  | 老外学中文 	   | foreigner@qq.com | 2 		   |
+----+-------+-----------------+------------------+------------+
8 rows in set (0.02 sec)

//联合查询(求交集)
mysql> select * from student where id <= 3
-> union
-> select * from student where name = '白素贞';
+----+------+-----------------+-----------------+------------+
| id | sn   | name 			  | qq_mail  		| classes_id |
+----+------+-----------------+-----------------+------------+
| 1  | 9982 | 黑旋风李逵		  | xuanfeng@qq.com | 1 		 |
| 2  | 835  | 菩提老祖 		  | NULL		    | 1 		 |
| 3  | 391  | 白素贞 		  | NULL 		    | 1			 |
+----+------+-----------------+-----------------+------------+
3 rows in set (0.01 sec)

mysql> select * from student where id <= 3
-> union
-> select * from student where name = '好好说话';
+----+-------+-----------------+-----------------+------------+
| id | sn    | name 		   | qq_mail  	     | classes_id |
+----+-------+-----------------+-----------------+------------+
| 1  | 9982  | 黑旋风李逵  	   | xuanfeng@qq.com | 1 		  |
| 2  | 835   | 菩提老祖 		   | NULL		     | 1 		  |
| 3  | 391   | 白素贞 		   | NULL			 | 1 		  |
| 6  | 51234 | 好好说话 		   | say@qq.com	     | 2		  |
+----+-------+-----------------+-----------------+------------+
4 rows in set (0.00 sec)

对于数据库来说,用的最多的就是数据库的查询。即使在工作中,使用的最多的也将是数据库的查询,因为数据库的添加和删除工作风险系数都会很高,同时参与的机会也不多。最后,希望大家再接再厉。

;原文链接:https://blog.csdn.net/huashanzhizai/article/details/115819740
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐