数据库操作
数据库的创建
MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.00 sec)
查看数据库列表
MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
| testdb |
+--------------------+
5 rows in set (0.00 sec)
进入testdb数据库
MariaDB [(none)]> use testdb;
Database changed
MariaDB [testdb]>
数据库表操作
创建表
- 方法一:常用
MariaDB [testdb]> create table student (
-> id int unsigned auto_increment primary key,
-> name char(10) not null,
-> age tinyint unsigned,
-> gender enum('m','f') default 'm',
-> mobile char(11) unique );
Query OK, 0 rows affected (0.01 sec)
MariaDB [testdb]> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| student |
+------------------+
1 row in set (0.00 sec)
#查看表结构
MariaDB [testdb]> desc student;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | char(10) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| gender | enum('m','f') | YES | | m | |
| mobile | char(11) | YES | UNI | NULL | |
+--------+---------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
- 方法二:根据已有表进行创建,里面有数据
MariaDB [testdb]> create table user select user,host,password from mysql.user;
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [testdb]> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| student |
| user |
+------------------+
2 rows in set (0.00 sec)
MariaDB [testdb]> desc user;
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| user | char(80) | NO | | | |
| host | char(60) | NO | | | |
| password | char(41) | NO | | | |
+----------+----------+------+-----+---------+-------+
3 rows in set (0.01 sec)
MariaDB [testdb]> select * from user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | ::1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)
- 方法三:根据已有表进行创建,里面无数据
MariaDB [testdb]> create table newuser like user;
Query OK, 0 rows affected (0.00 sec)
MariaDB [testdb]> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| newuser |
| student |
| user |
+------------------+
3 rows in set (0.00 sec)
MariaDB [testdb]> desc newuser;
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| user | char(80) | NO | | | |
| host | char(60) | NO | | | |
| password | char(41) | NO | | | |
+----------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
MariaDB [testdb]> select * from newuser;
Empty set (0.00 sec)
修改表
表名修改
MariaDB [test]> desc student;
+--------+---------------------+------+-----+---------+----------------
| Field | Type | Null | Key | Default | Extra
+--------+---------------------+------+-----+---------+----------------
| id | int(10) unsigned | NO | PRI | NULL | auto_increment
| name | char(10) | NO | | NULL |
| age | tinyint(3) unsigned | YES | | NULL |
| gender | enum('m','f') | YES | | m |
+--------+---------------------+------+-----+---------+----------------
4 rows in set (0.00 sec)
MariaDB [test]> alter table student rename teacher;
Query OK, 0 rows affected (0.01 sec)
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| teacher |
+----------------+
1 row in set (0.00 sec)
删除表
MariaDB [test]> drop table teacher;
Query OK, 0 rows affected (0.01 sec)
MariaDB [test]> show tables;
Empty set (0.00 sec)
表字段操作
添加字段
MariaDB [test]> alter table student add gender enum('m','f') default 'm' after age;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [test]> desc student;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| gender | enum('m','f') | YES | | m | |
+--------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
修改字段
MariaDB [test]> alter table student change name name char(10) not null;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [test]> desc student;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | char(10) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| gender | enum('m','f') | YES | | m | |
+--------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
删除字段
MariaDB [test]> alter table student drop gender;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [test]> desc student;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
表中数据操作
插入数据 insert
- 方法一 常用
MariaDB [testdb]> insert student (name,age,mobile,gender) values('zhao',30,10086,'m');
Query OK, 1 row affected (0.00 sec)
MariaDB [testdb]> select * from student;
+----+------+------+--------+--------+
| id | name | age | gender | mobile |
+----+------+------+--------+--------+
| 1 | zhao | 30 | m | 10086 |
+----+------+------+--------+--------+
1 row in set (0.00 sec)
- 方法二
MariaDB [testdb]> insert student set name='qian',age=28,mobile='10010';
Query OK, 1 row affected (0.00 sec)
MariaDB [testdb]> select * from student;
+----+------+------+--------+--------+
| id | name | age | gender | mobile |
+----+------+------+--------+--------+
| 1 | zhao | 30 | m | 10086 |
| 2 | qian | 28 | m | 10010 |
+----+------+------+--------+--------+
2 rows in set (0.01 sec)
MariaDB [testdb]> insert student set name='su',age=18,mobile='10000',gender='f';
Query OK, 1 row affected (0.01 sec)
MariaDB [testdb]> select * from student;
+----+------+------+--------+--------+
| id | name | age | gender | mobile |
+----+------+------+--------+--------+
| 1 | zhao | 30 | m | 10086 |
| 2 | qian | 28 | m | 10010 |
| 3 | su | 18 | f | 10000 |
+----+------+------+--------+--------+
3 rows in set (0.00 sec)
- 方法三
# 根据student表,生成一张teacher的表
MariaDB [testdb]> create table teacher like student;
Query OK, 0 rows affected (0.00 sec)
MariaDB [testdb]> select * from teacher;
Empty set (0.00 sec)
MariaDB [testdb]> select * from student;
+----+------+------+--------+--------+
| id | name | age | gender | mobile |
+----+------+------+--------+--------+
| 1 | zhao | 30 | m | 10086 |
| 2 | qian | 28 | m | 10010 |
| 3 | su | 18 | f | 10000 |
+----+------+------+--------+--------+
3 rows in set (0.00 sec)
# 将查询到的数据添加到teacher表中
MariaDB [testdb]> insert teacher select * from student;
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [testdb]> select * from teacher;
+----+------+------+--------+--------+
| id | name | age | gender | mobile |
+----+------+------+--------+--------+
| 1 | zhao | 30 | m | 10086 |
| 2 | qian | 28 | m | 10010 |
| 3 | su | 18 | f | 10000 |
+----+------+------+--------+--------+
3 rows in set (0.01 sec)
更新数据 updata
MariaDB [testdb]> select * from student;
+----+------+------+--------+--------+
| id | name | age | gender | mobile |
+----+------+------+--------+--------+
| 1 | zhao | 30 | m | 10086 |
| 2 | qian | 28 | m | 10010 |
| 3 | su | 18 | f | 10000 |
+----+------+------+--------+--------+
3 rows in set (0.00 sec)
MariaDB [testdb]> update student set age=24 where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [testdb]> select * from student;
+----+------+------+--------+--------+
| id | name | age | gender | mobile |
+----+------+------+--------+--------+
| 1 | zhao | 30 | m | 10086 |
| 2 | qian | 24 | m | 10010 |
| 3 | su | 18 | f | 10000 |
+----+------+------+--------+--------+
3 rows in set (0.00 sec)
-
注update使用时注意不要忘记加where条件,不加表示修改所有字段
-
可以使用 -U选项,
mysql -U -u root -p 'passwd'
-
可以将mysql -U 添加为别名
mysql='mysql -U'
-
或在配置文件的[mysql]中添加
safe-updates
项
删除数据
- 只能删除表中数据,不能删除表
delete
- 不加where,只删除表中数据,不删除表
MariaDB [testdb]> select * from teacher;
+----+------+------+--------+--------+
| id | name | age | gender | mobile |
+----+------+------+--------+--------+
| 1 | zhao | 30 | m | 10086 |
| 2 | qian | 28 | m | 10010 |
| 3 | su | 18 | f | 10000 |
+----+------+------+--------+--------+
3 rows in set (0.00 sec)
MariaDB [testdb]> delete from teacher;
Query OK, 3 rows affected (0.00 sec)
MariaDB [testdb]> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| newuser |
| student |
| teacher |
| user |
+------------------+
4 rows in set (0.00 sec)
MariaDB [testdb]> select * from teacher;
Empty set (0.00 sec)
- 加where,只删除与where条件匹配的数据
MariaDB [testdb]> select * from student;
+----+------+------+--------+--------+
| id | name | age | gender | mobile |
+----+------+------+--------+--------+
| 1 | zhao | 30 | m | 10086 |
| 2 | qian | 24 | m | 10010 |
| 3 | su | 18 | f | 10000 |
+----+------+------+--------+--------+
3 rows in set (0.00 sec)
MariaDB [testdb]> delete from student where id=3;
Query OK, 1 row affected (0.00 sec)
MariaDB [testdb]> select * from student;
+----+------+------+--------+--------+
| id | name | age | gender | mobile |
+----+------+------+--------+--------+
| 1 | zhao | 30 | m | 10086 |
| 2 | qian | 24 | m | 10010 |
+----+------+------+--------+--------+
2 rows in set (0.00 sec)
- 如果加入-U选项,则删除数据必须加where,否则报错
truncate 快速删表
truncate table teacher;
查询数据 select
- 特殊字符
as 对显示的字段起别名
* 代表所有字段
单表查询
- 查询学生表中的学生姓名,年龄,性别,班级id等数据,并且学生姓名显示为stu_name
MariaDB [hellodb]> select Name as stu_name,age,Gender,ClassID from students;
+---------------+-----+--------+---------+
| stu_name | age | Gender | ClassID |
+---------------+-----+--------+---------+
| Shi Zhongyu | 22 | M | 2 |
| Shi Potian | 22 | M | 1 |
| Xie Yanke | 53 | M | 2 |
| Ding Dian | 32 | M | 4 |
| Yu Yutong | 26 | M | 3 |
| Shi Qing | 46 | M | 5 |
| Xi Ren | 19 | F | 3 |
| Lin Daiyu | 17 | F | 7 |
| Ren Yingying | 20 | F | 6 |
| Yue Lingshan | 19 | F | 3 |
| Yuan Chengzhi | 23 | M | 6 |
| Wen Qingqing | 19 | F | 1 |
| Tian Boguang | 33 | M | 2 |
| Lu Wushuang | 17 | F | 3 |
| Duan Yu | 19 | M | 4 |
| Xu Zhu | 21 | M | 1 |
| Lin Chong | 25 | M | 4 |
| Hua Rong | 23 | M | 7 |
| Xue Baochai | 18 | F | 6 |
| Diao Chan | 19 | F | 7 |
| Huang Yueying | 22 | F | 6 |
| Xiao Qiao | 20 | F | 1 |
| Ma Chao | 23 | M | 4 |
| Xu Xian | 27 | M | NULL |
| Sun Dasheng | 100 | M | NULL |
+---------------+-----+--------+---------+
25 rows in set (0.00 sec)
- 查询学生年龄小于等于20岁,或者年龄是27岁的所有记录
MariaDB [hellodb]> select * from students where age <= 20 or age = 27 ;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
+-------+--------------+-----+--------+---------+-----------+
11 rows in set (0.00 sec)
- 查询学生年龄小于等于20岁,并且年龄不是19岁的所有记录
MariaDB [hellodb]> select * from students where age <= 20 and age <> 19 ;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
+-------+--------------+-----+--------+---------+-----------+
5 rows in set (0.00 sec)
- 查询没有教师的学生的所有数据
MariaDB [hellodb]> select * from students where teacherid is null;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
20 rows in set (0.00 sec)
* 为空:is null
* 非空:is not null
- 查询年龄在20到30之间(包含20 30)的学生的所有信息
MariaDB [hellodb]> select * from students where age between 20 and 27;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
12 rows in set (0.00 sec)
- 查询年龄是18 20 22岁的学生的所有信息
MariaDB [hellodb]> select * from students where age in (18,20,22);
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
+-------+---------------+-----+--------+---------+-----------+
6 rows in set (0.00 sec)
- 查询学生的年龄,去除重复(distinct)
MariaDB [hellodb]> select distinct age from students;
+-----+
| age |
+-----+
| 22 |
| 53 |
| 32 |
| 26 |
| 46 |
| 19 |
| 17 |
| 20 |
| 23 |
| 33 |
| 21 |
| 25 |
| 18 |
| 27 |
| 100 |
+-----+
15 rows in set (0.00 sec)
- 查询所有姓shi的学生信息
MariaDB [hellodb]> select * from students where name like 'shi%';
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.01 sec)
- 聚合函数
avg(), max(), min(), count(), sum()…
-
group by 与 条件语句的位置关系
where在前,group by 在后
或者 group by 在前 having 在后 -
统计学生表中男女学生分别的平均年龄
MariaDB [hellodb]> select gender,avg(age) from students group by gender;
+--------+----------+
| gender | avg(age) |
+--------+----------+
| F | 19.0000 |
| M | 33.0000 |
+--------+----------+
2 rows in set (0.01 sec)
- 统计每个班的男女生数
MariaDB [hellodb]> select classid,gender,count(*) from students group by classid,gender;
+---------+--------+----------+
| classid | gender | count(*) |
+---------+--------+----------+
| NULL | M | 2 |
| 1 | F | 2 |
| 1 | M | 2 |
| 2 | M | 3 |
| 3 | F | 3 |
| 3 | M | 1 |
| 4 | M | 4 |
| 5 | M | 1 |
| 6 | F | 3 |
| 6 | M | 1 |
| 7 | F | 2 |
| 7 | M | 1 |
+---------+--------+----------+
12 rows in set (0.01 sec)
- 统计每个班的男女生数,以班级id排序
MariaDB [hellodb]> select classid,gender,count(*) from students group by classid,gender order by classid;
+---------+--------+----------+
| classid | gender | count(*) |
+---------+--------+----------+
| NULL | M | 2 |
| 1 | F | 2 |
| 1 | M | 2 |
| 2 | M | 3 |
| 3 | F | 3 |
| 3 | M | 1 |
| 4 | M | 4 |
| 5 | M | 1 |
| 6 | F | 3 |
| 6 | M | 1 |
| 7 | F | 2 |
| 7 | M | 1 |
+---------+--------+----------+
12 rows in set (0.00 sec)
- 统计每个班的男女生数,以班级id排序,并将NULL值放到最后
MariaDB [hellodb]> select classid,gender,count(*) from students group by classid,gender order by -classid desc;
+---------+--------+----------+
| classid | gender | count(*) |
+---------+--------+----------+
| 1 | M | 2 |
| 1 | F | 2 |
| 2 | M | 3 |
| 3 | M | 1 |
| 3 | F | 3 |
| 4 | M | 4 |
| 5 | M | 1 |
| 6 | F | 3 |
| 6 | M | 1 |
| 7 | F | 2 |
| 7 | M | 1 |
| NULL | M | 2 |
+---------+--------+----------+
12 rows in set (0.00 sec)
- 对学生年龄进行正序排序
MariaDB [hellodb]> select * from students order by age;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)
- 对学生年龄进行倒序排序
MariaDB [hellodb]> select * from students order by age desc;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)
- 挑出年龄最小的3个学生的信息
MariaDB [hellodb]> select * from students order by age limit 3;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
- 跳过年龄最小的2个学生,往后取出3个学生的信息
MariaDB [hellodb]> select * from students order by age limit 2,3;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
+-------+--------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
多表查询
纵向合并 union
-
union 如果查询中有重复记录则会去重显示
-
union all 不论是否有重复记录都显示,不去重
-
合并时两个表中要合并显示的字段数必须一致
MariaDB [hellodb]> select stuid as id,name,age,gender from students union select tid as id, name,age,gender from teachers;
+----+---------------+-----+--------+
| id | name | age | gender |
+----+---------------+-----+--------+
| 1 | Shi Zhongyu | 22 | M |
| 2 | Shi Potian | 22 | M |
| 3 | Xie Yanke | 53 | M |
| 4 | Ding Dian | 32 | M |
| 5 | Yu Yutong | 26 | M |
| 6 | Shi Qing | 46 | M |
| 7 | Xi Ren | 19 | F |
| 8 | Lin Daiyu | 17 | F |
| 9 | Ren Yingying | 20 | F |
| 10 | Yue Lingshan | 19 | F |
| 11 | Yuan Chengzhi | 23 | M |
| 12 | Wen Qingqing | 19 | F |
| 13 | Tian Boguang | 33 | M |
| 14 | Lu Wushuang | 17 | F |
| 15 | Duan Yu | 19 | M |
| 16 | Xu Zhu | 21 | M |
| 17 | Lin Chong | 25 | M |
| 18 | Hua Rong | 23 | M |
| 19 | Xue Baochai | 18 | F |
| 20 | Diao Chan | 19 | F |
| 21 | Huang Yueying | 22 | F |
| 22 | Xiao Qiao | 20 | F |
| 23 | Ma Chao | 23 | M |
| 24 | Xu Xian | 27 | M |
| 25 | Sun Dasheng | 100 | M |
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
+----+---------------+-----+--------+
29 rows in set (0.00 sec)
横向合并 cross join
MariaDB [hellodb]> select * from students cross join teachers limit 10;
+-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
| StuID | Name | Age | Gender | ClassID | TeacherID | TID | Name | Age | Gender |
+-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 | 1 | Song Jiang | 45 | M |
| 1 | Shi Zhongyu | 22 | M | 2 | 3 | 2 | Zhang Sanfeng | 94 | M |
| 1 | Shi Zhongyu | 22 | M | 2 | 3 | 3 | Miejue Shitai | 77 | F |
| 1 | Shi Zhongyu | 22 | M | 2 | 3 | 4 | Lin Chaoying | 93 | F |
| 2 | Shi Potian | 22 | M | 1 | 7 | 1 | Song Jiang | 45 | M |
| 2 | Shi Potian | 22 | M | 1 | 7 | 2 | Zhang Sanfeng | 94 | M |
| 2 | Shi Potian | 22 | M | 1 | 7 | 3 | Miejue Shitai | 77 | F |
| 2 | Shi Potian | 22 | M | 1 | 7 | 4 | Lin Chaoying | 93 | F |
| 3 | Xie Yanke | 53 | M | 2 | 16 | 1 | Song Jiang | 45 | M |
| 3 | Xie Yanke | 53 | M | 2 | 16 | 2 | Zhang Sanfeng | 94 | M |
+-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
10 rows in set (0.00 sec)
MariaDB [hellodb]> select * from teachers cross join students limit 10;
+-----+---------------+-----+--------+-------+-------------+-----+--------+---------+-----------+
| TID | Name | Age | Gender | StuID | Name | Age | Gender | ClassID | TeacherID |
+-----+---------------+-----+--------+-------+-------------+-----+--------+---------+-----------+
| 1 | Song Jiang | 45 | M | 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Zhang Sanfeng | 94 | M | 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 3 | Miejue Shitai | 77 | F | 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 4 | Lin Chaoying | 93 | F | 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 1 | Song Jiang | 45 | M | 2 | Shi Potian | 22 | M | 1 | 7 |
| 2 | Zhang Sanfeng | 94 | M | 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Miejue Shitai | 77 | F | 2 | Shi Potian | 22 | M | 1 | 7 |
| 4 | Lin Chaoying | 93 | F | 2 | Shi Potian | 22 | M | 1 | 7 |
| 1 | Song Jiang | 45 | M | 3 | Xie Yanke | 53 | M | 2 | 16 |
| 2 | Zhang Sanfeng | 94 | M | 3 | Xie Yanke | 53 | M | 2 | 16 |
+-----+---------------+-----+--------+-------+-------------+-----+--------+---------+-----------+
10 rows in set (0.00 sec)
内连接(表交集) inner join
图1实现
- 方法一
MariaDB [hellodb]> select s.stuid as sid,s.name as s_name, t.tid,t.name as t_name from students as s inner join teachers as t on s.teacherid=t.tid;
+-----+-------------+-----+---------------+
| sid | s_name | tid | t_name |
+-----+-------------+-----+---------------+
| 5 | Yu Yutong | 1 | Song Jiang |
| 1 | Shi Zhongyu | 3 | Miejue Shitai |
| 4 | Ding Dian | 4 | Lin Chaoying |
+-----+-------------+-----+---------------+
3 rows in set (0.00 sec)
- 方法二
MariaDB [hellodb]> select s.stuid as sid,s.name as s_name, t.tid,t.name as t_name from students as s,teachers as t where s.teacherid=t.tid;
+-----+-------------+-----+---------------+
| sid | s_name | tid | t_name |
+-----+-------------+-----+---------------+
| 5 | Yu Yutong | 1 | Song Jiang |
| 1 | Shi Zhongyu | 3 | Miejue Shitai |
| 4 | Ding Dian | 4 | Lin Chaoying |
+-----+-------------+-----+---------------+
3 rows in set (0.00 sec)
外连接
左外连接 left outer join
- 图2实现
MariaDB [hellodb]> select tid,t.name as t_name, stuid, s.name as s_name from teachers as t left outer join students as s on t.tid=s.teacherid;
+-----+---------------+-------+-------------+
| tid | t_name | stuid | s_name |
+-----+---------------+-------+-------------+
| 3 | Miejue Shitai | 1 | Shi Zhongyu |
| 4 | Lin Chaoying | 4 | Ding Dian |
| 1 | Song Jiang | 5 | Yu Yutong |
| 2 | Zhang Sanfeng | NULL | NULL |
+-----+---------------+-------+-------------+
4 rows in set (0.00 sec)
MariaDB [hellodb]> select s.stuid as sid,s.name as s_name, t.tid,t.name as t_name from students as s left outer join teachers as t on t.tid=s.teacherid;
+-----+---------------+------+---------------+
| sid | s_name | tid | t_name |
+-----+---------------+------+---------------+
| 1 | Shi Zhongyu | 3 | Miejue Shitai |
| 2 | Shi Potian | NULL | NULL |
| 3 | Xie Yanke | NULL | NULL |
| 4 | Ding Dian | 4 | Lin Chaoying |
| 5 | Yu Yutong | 1 | Song Jiang |
| 6 | Shi Qing | NULL | NULL |
| 7 | Xi Ren | NULL | NULL |
| 8 | Lin Daiyu | NULL | NULL |
| 9 | Ren Yingying | NULL | NULL |
| 10 | Yue Lingshan | NULL | NULL |
| 11 | Yuan Chengzhi | NULL | NULL |
| 12 | Wen Qingqing | NULL | NULL |
| 13 | Tian Boguang | NULL | NULL |
| 14 | Lu Wushuang | NULL | NULL |
| 15 | Duan Yu | NULL | NULL |
| 16 | Xu Zhu | NULL | NULL |
| 17 | Lin Chong | NULL | NULL |
| 18 | Hua Rong | NULL | NULL |
| 19 | Xue Baochai | NULL | NULL |
| 20 | Diao Chan | NULL | NULL |
| 21 | Huang Yueying | NULL | NULL |
| 22 | Xiao Qiao | NULL | NULL |
| 23 | Ma Chao | NULL | NULL |
| 24 | Xu Xian | NULL | NULL |
| 25 | Sun Dasheng | NULL | NULL |
+-----+---------------+------+---------------+
25 rows in set (0.00 sec)
- 图4实现
MariaDB [hellodb]> select tid,t.name as t_name, stuid, s.name as s_name from teachers as t left outer join students as s on t.tid=s.teacherid where teacherid is null;
+-----+---------------+-------+--------+
| tid | t_name | stuid | s_name |
+-----+---------------+-------+--------+
| 2 | Zhang Sanfeng | NULL | NULL |
+-----+---------------+-------+--------+
1 row in set (0.00 sec)
右外连接 right outer join
- 图3实现
MariaDB [hellodb]> select stuid,s.name as s_name, tid,t.name as t_name from teachers as t right outer join students as s on t.tid=s.teacherid;
+-------+---------------+------+---------------+
| stuid | s_name | tid | t_name |
+-------+---------------+------+---------------+
| 1 | Shi Zhongyu | 3 | Miejue Shitai |
| 2 | Shi Potian | NULL | NULL |
| 3 | Xie Yanke | NULL | NULL |
| 4 | Ding Dian | 4 | Lin Chaoying |
| 5 | Yu Yutong | 1 | Song Jiang |
| 6 | Shi Qing | NULL | NULL |
| 7 | Xi Ren | NULL | NULL |
| 8 | Lin Daiyu | NULL | NULL |
| 9 | Ren Yingying | NULL | NULL |
| 10 | Yue Lingshan | NULL | NULL |
| 11 | Yuan Chengzhi | NULL | NULL |
| 12 | Wen Qingqing | NULL | NULL |
| 13 | Tian Boguang | NULL | NULL |
| 14 | Lu Wushuang | NULL | NULL |
| 15 | Duan Yu | NULL | NULL |
| 16 | Xu Zhu | NULL | NULL |
| 17 | Lin Chong | NULL | NULL |
| 18 | Hua Rong | NULL | NULL |
| 19 | Xue Baochai | NULL | NULL |
| 20 | Diao Chan | NULL | NULL |
| 21 | Huang Yueying | NULL | NULL |
| 22 | Xiao Qiao | NULL | NULL |
| 23 | Ma Chao | NULL | NULL |
| 24 | Xu Xian | NULL | NULL |
| 25 | Sun Dasheng | NULL | NULL |
+-------+---------------+------+---------------+
25 rows in set (0.00 sec)
MariaDB [hellodb]> select t.tid,t.name as t_name,s.stuid as sid,s.name as s_name from students as s right outer join teachers as t on t.tid=s.teacherid;
+-----+---------------+------+-------------+
| tid | t_name | sid | s_name |
+-----+---------------+------+-------------+
| 3 | Miejue Shitai | 1 | Shi Zhongyu |
| 4 | Lin Chaoying | 4 | Ding Dian |
| 1 | Song Jiang | 5 | Yu Yutong |
| 2 | Zhang Sanfeng | NULL | NULL |
+-----+---------------+------+-------------+
4 rows in set (0.00 sec)
- 图5 实现
MariaDB [hellodb]> select t.tid,t.name as t_name,s.stuid as sid,s.name as s_name from students as s right outer join teachers as t on t.tid=s.teacherid where s.stuid is null;
+-----+---------------+------+--------+
| tid | t_name | sid | s_name |
+-----+---------------+------+--------+
| 2 | Zhang Sanfeng | NULL | NULL |
+-----+---------------+------+--------+
1 row in set (0.00 sec)
完全外连接 full outer join
-
mysql 不支持完全外连接语法
-
可以用左外连接 union 右外连接 实现相同用法
-
MariaDB中实现图6
MariaDB [hellodb]> select stuid as sid,s.name as s_name,tid,t.name as t_name from students as s left outer join teachers as t on s.teacherid=t.tid union select stuid as sid,s.name as s_name,tid,t.name as t_name from students as s right outer join teachers as t on s.teacherid=t.tid;
+------+---------------+------+---------------+
| sid | s_name | tid | t_name |
+------+---------------+------+---------------+
| 1 | Shi Zhongyu | 3 | Miejue Shitai |
| 2 | Shi Potian | NULL | NULL |
| 3 | Xie Yanke | NULL | NULL |
| 4 | Ding Dian | 4 | Lin Chaoying |
| 5 | Yu Yutong | 1 | Song Jiang |
| 6 | Shi Qing | NULL | NULL |
| 7 | Xi Ren | NULL | NULL |
| 8 | Lin Daiyu | NULL | NULL |
| 9 | Ren Yingying | NULL | NULL |
| 10 | Yue Lingshan | NULL | NULL |
| 11 | Yuan Chengzhi | NULL | NULL |
| 12 | Wen Qingqing | NULL | NULL |
| 13 | Tian Boguang | NULL | NULL |
| 14 | Lu Wushuang | NULL | NULL |
| 15 | Duan Yu | NULL | NULL |
| 16 | Xu Zhu | NULL | NULL |
| 17 | Lin Chong | NULL | NULL |
| 18 | Hua Rong | NULL | NULL |
| 19 | Xue Baochai | NULL | NULL |
| 20 | Diao Chan | NULL | NULL |
| 21 | Huang Yueying | NULL | NULL |
| 22 | Xiao Qiao | NULL | NULL |
| 23 | Ma Chao | NULL | NULL |
| 24 | Xu Xian | NULL | NULL |
| 25 | Sun Dasheng | NULL | NULL |
| NULL | NULL | 2 | Zhang Sanfeng |
+------+---------------+------+---------------+
26 rows in set (0.00 sec)
- MariaDB中实现图7
MariaDB [hellodb]> select * from (select stuid as sid,s.name as s_name,tid,t.name as t_name from students as s left outer join teachers as t on s.teacherid=t.tid union select stuid as sid,s.name as s_name,tid,t.name as t_name from students as s right outer join teachers as t on s.teacherid=t.tid) as s_t where s_t.sid is null or s_t.tid is null;
+------+---------------+------+---------------+
| sid | s_name | tid | t_name |
+------+---------------+------+---------------+
| 2 | Shi Potian | NULL | NULL |
| 3 | Xie Yanke | NULL | NULL |
| 6 | Shi Qing | NULL | NULL |
| 7 | Xi Ren | NULL | NULL |
| 8 | Lin Daiyu | NULL | NULL |
| 9 | Ren Yingying | NULL | NULL |
| 10 | Yue Lingshan | NULL | NULL |
| 11 | Yuan Chengzhi | NULL | NULL |
| 12 | Wen Qingqing | NULL | NULL |
| 13 | Tian Boguang | NULL | NULL |
| 14 | Lu Wushuang | NULL | NULL |
| 15 | Duan Yu | NULL | NULL |
| 16 | Xu Zhu | NULL | NULL |
| 17 | Lin Chong | NULL | NULL |
| 18 | Hua Rong | NULL | NULL |
| 19 | Xue Baochai | NULL | NULL |
| 20 | Diao Chan | NULL | NULL |
| 21 | Huang Yueying | NULL | NULL |
| 22 | Xiao Qiao | NULL | NULL |
| 23 | Ma Chao | NULL | NULL |
| 24 | Xu Xian | NULL | NULL |
| 25 | Sun Dasheng | NULL | NULL |
| NULL | NULL | 2 | Zhang Sanfeng |
+------+---------------+------+---------------+
23 rows in set (0.00 sec)
自连接
MariaDB [hellodb]> create table emp(id int, name char(20), leader int);
Query OK, 0 rows affected (0.00 sec)
MariaDB [hellodb]> insert emp values(1,'张三',null),(2,'李四',1),(3,'王二',2),(4,'麻子',3);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
MariaDB [hellodb]> select * from emp;
+------+--------+--------+
| id | name | leader |
+------+--------+--------+
| 1 | 张三 | NULL |
| 2 | 李四 | 1 |
| 3 | 王二 | 2 |
| 4 | 麻子 | 3 |
+------+--------+--------+
4 rows in set (0.00 sec)
MariaDB [hellodb]> select e.name as 员工姓名,l.name as 领导姓名 from emp as e left join emp as l on e.leader=l.id;
+--------------+--------------+
| 员工姓名 | 领导姓名 |
+--------------+--------------+
| 李四 | 张三 |
| 王二 | 李四 |
| 麻子 | 王二 |
| 张三 | NULL |
+--------------+--------------+
4 rows in set (0.00 sec)
子查询
- 查找学生年龄小于平均年龄的学生信息
MariaDB [hellodb]> select * from students where age < (select avg(age) from students);
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
20 rows in set (0.00 sec)
数据库其他操作
-
show character set; 查看所有字符集
-
show variables like ‘%character%’; 查看当前数据库使用的字符集
-
show collation; 查看所有排序规则
-
create database [if not exists] ‘db_name’; 创建数据库
-
show create database db_name; 查看数据库创建过程
-
show table status like ‘student’\G; 查看表的状态,\G 表示结果竖着显示
-
show engines; 查看数据库支持的引擎
-
数据库中表名大小写敏感,表中数据大小写不敏感
-
导入sql文件
$mysql -u root -p < hellodb_innodb.sql
Enter password: