SQL语句的用法

目录

关系统型数据库相

联系类型

数据库的正规化分析

第一范式:1NF  范式主要就是减产冗余

第二范式:2NF

第三范式:3NF

 字符串(char,varchar,text)

char和varchar的比较:

修饰符, 主键,唯一键

常见命令 

 创建表:

借鉴一个老表创建一个新表,的两种方式

​编辑

增删改查 (批量导入)

增 删

在name下面增加phone文件

在下面增加内容

改 

查 

select的用法     

内连接  inner join

联合查询union

交叉连接等cross join

左外连接left outer join

 右外连接right outer join

取没有交际的表

完全外连接 

自连接, 即表自身连接自身 

三个表内连接查询

别名


关系统型数据库相

  • 关系Relational :关系就是二维表,其中:表中的行、列次序并不重要
  • 行row:表中的每一行,又称为一条记录record
  • 列column:表中的每一列,称为属性,字段,域
  • 主键Primary key只能有一个,内容不能相同
  •        复合主键: 多列表加起来生成的主键
  • 唯一键Unique key可以有多个 内容不能相同 空置可以null
  • 域domain :男女 年龄 0-150等

联系类型

  • 一对一联系(1:1): 在表A或表B中创建一个字段﹐存储另一个表的主键值 如: 一个人只有一个身份证
  • 一对多联系(1:n):外键, 如: 部门和员工
  • 多对多联系(m:n):增加第三张表, 如: 学生和课

数据库的正规化分析

目前关系数据库有六种范式:第一范式(1NF)、第二范式(2NF)、第三范式(3NF)、巴德斯科范式(BCNF)、第四范式(4NF)和第五范式(5NF,又称完美范式)。

满足最低要求的范式是第一范式(1NF)。在第一范式的基础上进一步满足更多规范要求的称为第二范式(2NF),其余范式以次类推。一般数据库只需满足第三范式(3NF)即可

  • 规则是死的,人是活的,所以范式是否必须遵守,要看业务需要而定
  • 掌握范式的目的是为了在合适的场景下违反范式
第一范式:1NF  范式主要就是减产冗余
  • 无重复的列,每一列都是不可分割的基本数据项,同一列中不能有多个值,即实体中的某个属性不能有多个值或者不能有重复的属性,确保每一列的原子性。除去同类型的字段,就是无重复的列
  • 说明:第一范式(1NF)是对关系模式的基本要求,不满足第一范式(1NF)的数据库就不是关系数据库
第二范式:2NF
  • 第二范式必须先满足第一范式,属性完全依赖于主键,要求表中的每个行必须可以被唯一地区分,通常为表加上每行的唯一标识主键PK,非PK的字段需要与整个PK有直接相关性,即非PK的字段不能依赖于部分主键
第三范式:3NF
  • 满足第三范式必须先满足第二范式属性,非主键属性不依赖于其它非主键属性。第三范式要求一个数据表中不包含已在其它表中已包含的非主关键字信息,非PK的字段间不能有从属关系    (这主要是为了防止数据冗余,与其非主键依赖关系不如在创一个表)

 字符串(char,varchar,text)

  • char(n) 固定长度,最多255个字符,注意不是字节
  • varchar(n) 可变长度,最多65535个字符
  • tinytext 可变长度,最多255个字符
  • text 可变长度,最多65535个字符
  • mediumtext 可变长度,最多2的24次方-1个字符
  • longtext 可变长度,最多2的32次方-1个字符
  • BINARY(M) 固定长度,可存二进制或字符,长度为0-M字节
  • VARBINARY(M) 可变长度,可存二进制或字符,允许长度为0-M字节
  • 内建类型:ENUM枚举, SET集合
char和varchar的比较:

char是指定大小,varchar是动态大小,char快,varchar节省空间,根据情况定义,如果字节大小类似可以varchat,

修饰符, 主键,唯一键

适用所有类型的修饰符:

NULL                       数据列可包含NULL值,默认值
NOT NULL              数据列不允许包含NULL值,相当于网站注册表中的 * 为必填选项
DEFAULT                默认值
PRIMARY KEY       主键,所有记录中此字段的值不能重复,且不能为NULL
UNIQUE KEY          唯一键,所有记录中此字段的值不能重复,但可以为NULL
CHARACTER SET name       指定一个字符集

适用数值型的修饰符:    auto_increment

AUTO_INCREMENT      自动递增,适用于整数类型, 必须作用于某个 key 的字段,比如primary key
UNSIGNED                   无符号

常见命令 

  •  status;               \s;              查看线程版本之类的 
  • show databases                  查看数据库
  • show tables;                        查看库里几个表
  • desc xxx;                             查看表格创建信息
  •  select * from students;       *代表着字段所有 查看所有表
  • select database();              查看当前数据库
  • select user();                      查看当前用户
  • show create table student;                            查看以前创建表用的命令
  • show table status like 'student'\G;                 查看表的详细信息存储过程,创建日期等
  • \G    竖过来显示

  • use mysql                        切换数据库
  • create database db1;       创建数据库
  •  drop table <表格名>        删除表格
  • drop database <数据库名>;    删除库

 创建表:

:  

CREATE TABLE student (                                            创建表,名字
id int UNSIGNED AUTO_INCREMENT PRIMARY KEY, 名字,4字,全整,自动增加,主键
name VARCHAR(20) NOT NULL,                                    名字,限长20,不能为空,
age tinyint UNSIGNED,                                                    年龄,1字节,取整(最大255)
#height DECIMAL(5,2),                     
gender ENUM('M','F') default 'M'         

enum 限定值的取值范围,比如(男,女,未知)等    ENUM('M','F') default 'M'    男或女,默认男
)AUTO_INCREMENT=10;                   自动增长默认10开始,

CREATE TABLE students (
id int UNSIGNED AUTO_INCREMENT PRIMARY KEY comment "学生编号",
name VARCHAR(20) NOT NULL,
age tinyint UNSIGNED,
#height DECIMAL(5,2),
gender ENUM('M','F') default 'M'
)AUTO_INCREMENT=10 ;

 insert student (name,age)values('xiaoming',20);      写入到student里数据,小明,20岁

comment ‘描述干什么的’                                   描述干什么的可以加在最后

列:   id int UNSIGNED AUTO_INCREMENT PRIMARY KEY comment "学生编号",

借鉴一个老表创建一个新表,的两种方式

  • create table custom like student;       
  • 创建一个新表借鉴一个老表,表格内容不会集成下来,但是格式规则都会继承下来

  • create table xxx as select * from students;   
  • 创建一个新表借鉴一个老表,内容都会继承下来,个别规则会丢掉,如自动递增,主键等

增删改查 (批量导入)

增 删

尽量不要跟更改表结构,容易出问题

在name下面增加phone文件
  • ALTER TABLE students ADD COLUMN phone varchar(11) AFTER name;
  • 更改,表格,    x,       增加  列     (创建名字) 最多位  谁的下面(name下面phone文件)
  • ALTER TABLE s1 DROP COLUMN mobile;  更改,表格,x,删除,列,x
#修改表名
ALTER TABLE students RENAME s1;
#添加字段
ALTER TABLE s1 ADD phone varchar(11) AFTER name;
#修改字段类型
ALTER TABLE s1 MODIFY phone int;
#修改字段名称和类型
ALTER TABLE s1 CHANGE COLUMN phone mobile char(11);
#删除字段
ALTER TABLE s1 DROP COLUMN mobile;
#修改字符集
ALTER TABLE s1 character set utf8;
#修改数据类型和字符集
ALTER TABLE s1 change name name varchar(20) character set utf8;
#添加字段
ALTER TABLE students ADD gender ENUM('m','f');
alter table student modify is_del bool default false;
#修改字段名和类型
ALETR TABLE students CHANGE id sid int UNSIGNED NOT NULL PRIMARY KEY;
#删除字段
ALTER TABLE students DROP age;
#查看表结构
DESC students;
#新建表无主键,添加和删除主键
CREATE TABLE t1 SELECT * FROM students
ALTER TABLE t1 add primary key (stuid);
ALTER TABLE t1 drop primary key ;
#添加外键
ALTER TABLE students add foreign key(TeacherID) references teachers(tid);
#删除外键
SHOW CREATE TABLE students #查看外键名
ALTER TABLE students drop foreign key <外键名>;

虽然有自动增长,但是如果不写的话,就需要,写清楚赋值对象  需要一一对应,一行也可以加多行记录

在下面增加内容

列:

  • insert into students value(26,'wei',18,'M',1,null);   
  • insert students set name='wsd',age=22,gender='M';   这样亦可以

insert students (name, age, gender) select name, age, gender from teachers 

把teachers的 name, age, gender 插入到students的最后面   

改 

update students set age=21,gender='F' where name='wei';

  • 把wei的年龄改成21,性别女,指定修改name=wei的    where指定
  • 也可以 where age >20;   把20岁以上的都改了

改后

 update students set teacherid=1 where TeacherID is null;

  • 把teacherid的空值(null)改为1 ,这里需要用is 不能用=;

delete from students where stuid=30;          删除指定行 

查 
select的用法     
  • 不能使用正则表达式,否则有的索引会失效

select * from students;   *代表着字段所有(就是把字段的所有内容打印粗来)可以把*换成列名

select 类似于echo有类似打印功能,如图;  如果后面跟文件名就是打印的行长

查看students表中年龄是10~20岁的stuid ,name,gender,age字段信息

  •  select stuid ,name,gender,age from students where age between 18 and 20;
  • select stuid ,name,gender,age from students where age >=18 and age <= 20;

  • order  by age;  从小到大排序
  • order  by age desc   排序从打到小
  • avg(age)  统计括号里的平均年龄
  • max(stuid)显示stuid最大值的编号
  • group by gender 分组
mysql> select stuid,name,gender,age from students;
+-------+---------------+--------+-----+
| stuid | name          | gender | age |
+-------+---------------+--------+-----+
|     1 | Shi Zhongyu   | M      |  22 |
|     2 | Shi Potian    | M      |  22 |
|     3 | Xie Yanke     | M      |  53 |
|     4 | Ding Dian     | M      |  32 |
|     5 | Yu Yutong     | M      |  26 |        查询students下的stuid,name,gender,age信息
|     6 | Shi Qing      | M      |  46 |
|     7 | Xi Ren        | F      |  19 |
|     8 | Lin Daiyu     | F      |  17 |
|     9 | Ren Yingying  | F      |  20 |
|    10 | Yue Lingshan  | F      |  19 |
|    11 | Yuan Chengzhi | M      |  23 |
|    12 | Wen Qingqing  | F      |  19 |
|    13 | Tian Boguang  | M      |  33 |
|    14 | Lu Wushuang   | F      |  17 |
|    15 | Duan Yu       | M      |  19 |
|    16 | Xu Zhu        | M      |  21 |
|    17 | Lin Chong     | M      |  25 |
|    18 | Hua Rong      | M      |  23 |
|    19 | Xue Baochai   | F      |  18 |
|    20 | Diao Chan     | F      |  19 |
|    21 | Huang Yueying | F      |  22 |
|    22 | Xiao Qiao     | F      |  20 |
|    23 | Ma Chao       | M      |  23 |mysql> select stuid ,name,gender,age from students where gender='F';
+-------+---------------+--------+-----+
| stuid | name          | gender | age |
+-------+---------------+--------+-----+
|     7 | Xi Ren        | F      |  19 |
|     8 | Lin Daiyu     | F      |  17 |
|     9 | Ren Yingying  | F      |  20 |         查询这里所有信息都是女的
|    10 | Yue Lingshan  | F      |  19 |
|    12 | Wen Qingqing  | F      |  19 |
|    14 | Lu Wushuang   | F      |  17 |
|    19 | Xue Baochai   | F      |  18 |
|    20 | Diao Chan     | F      |  19 |
|    21 | Huang Yueying | F      |  22 |
|    22 | Xiao Qiao     | F      |  20 |
|    26 | wei           | F      |  21 |
+-------+---------------+--------+-----+(root@localhost) [hellodb]> select stuid ,name,gender,age from students where name like '%x%';
+-------+-------------+--------+-----+
| stuid | name        | gender | age |
+-------+-------------+--------+-----+
|     3 | Xie Yanke   | M      |  53 |
|     7 | Xi Ren      | F      |  19 |     模糊查询
|    16 | Xu Zhu      | M      |  21 |     x%代表x开头的name模糊查询
|    19 | Xue Baochai | F      |  18 |     %x%  代表有x的模糊查询
|    22 | Xiao Qiao   | F      |  20 |
|    24 | Xu Xian     | M      |  27 |
+-------+-------------+--------+-----+root@localhost) [hellodb]> select distinct age from students;
+-----+
| age |
+-----+
|  22 |
|  53 |
|  32 |                 distinct   合并去重查询
|  26 |
|  46 |
|  19 |
|  17 |
|  20 |
|  23 |
|  33 |
|  21 |
|  25 |
|  18 |
|  27 |
| 100 |
|  45 |
|  94 |
|  77 |
+-----+(root@localhost) [hellodb]> select distinct age from students order  by age desc;
+-----+
| age |
+-----+
| 100 |
|  94 |
|  77 |
|  53 |            order  by age;  从小到大排序
|  46 |
|  45 |         order  by age desc   排序从打到小
|  33 |
|  32 |
|  27 |
|  26 |
|  25 |
|  23 |
|  22 |
|  21 |
|  20 |
|  19 |
|  18 |
|  17 |
+-----+
18 rows in set (0.00 sec)(root@localhost) [hellodb]> select distinct age from students order  by age;
+-----+
| age |
+-----+
|  17 |
|  18 |
|  19 |
|  20 |     order  by age;  从小到大排序
|  22 |
|  23 |
|  25 |
|  26 |
|  27 |
|  32 |
|  33 |
|  45 |
|  46 |
|  53 |
|  77 |
|  94 |
| 100 |
+-----+
18 rows in set (0.00 sec)
、root@localhost) [hellodb]> select gender,avg(age) from students group by gender;
+--------+----------+
| gender | avg(age) |       avg(age)  统计括号里的平均年龄
+--------+----------+       gender 显示年龄大小
| F      |  19.1818 |
| M      |  32.3125 |
+--------+----------+select gender,max(stuid),avg(age) from students grouproup by gender;
+--------+------------+----------+
| gender | max(stuid) | avg(age) |
+--------+------------+----------+     max(stuid)显示stuid最大值的编号
| M      |         28 |  37.2941 |    
| F      |         29 |  24.0000 |   
+--------+------------+----------+(root@localhost) [hellodb]> select gender,avg(age) from students group by gender having gender = 'M';
+--------+----------+
| gender | avg(age) |
+--------+----------+      group by gender 分组
| M      |  32.3125 |      having gender = 'M'; 分组后过滤,需要把他加在后面
+--------+----------+mysql> select gender,avg(age) from students where gender = 'M' group by gender;
+--------+----------+
| gender | avg(age) |        where gender = 'M'  分组前过滤,需要把他加在过滤前面
+--------+----------+        
| M      |  37.2941 |
+--------+----------+(root@localhost) [hellodb]> select classid,gender,max(age) from students group by classid,gender;
+---------+--------+----------+
| classid | gender | max(age) |
+---------+--------+----------+
|       2 | M      |       53 |
|       1 | M      |       22 |   order by classid 在后面加这个可以对于班级进行排序
|       4 | M      |       32 |   取每个班性别的最大值 
|       3 | M      |       26 |
|       5 | M      |       46 |
|       3 | F      |       19 |     
|       7 | F      |       19 |    
|       6 | F      |       22 |
|       6 | M      |       23 |
|       1 | F      |       21 |
|       7 | M      |       23 |
|    NULL | M      |      100 |
|    NULL | F      |       77 |
+---------+--------+----------+mysql> select * from students limit 3;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name        | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu |  22 | M      |       2 |         3 |    取前三
|     2 | Shi Potian  |  22 | M      |       1 |         7 |
|     3 | Xie Yanke   |  53 | M      |       2 |        16 |
+-------+-------------+-----+--------+---------+-----------+select * from students order by age desc limit 3; 
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|    25 | Sun Dasheng   | 100 | M      |    NULL |         1 |      排序取前三
|    28 | Zhang Sanfeng |  94 | M      |    NULL |         1 |
|    29 | Miejue Shitai |  77 | F      |    NULL |         1 |
+-------+---------------+-----+--------+---------+-----------+
select * from students order by age desc limit 3,4;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name         | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
|     3 | Xie Yanke    |  53 | M      |       2 |        16 |
|     6 | Shi Qing     |  46 | M      |       5 |         1 |   跳过前三,显示后4
|    27 | Song Jiang   |  45 | M      |    NULL |         1 |
|    13 | Tian Boguang |  33 | M      |       2 |         1 |
+-------+--------------+-----+--------+---------+-----------+
select * from students where age > (select avg(age) from teachers);
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|    25 | Sun Dasheng   | 100 | M      |    NULL |         1 |
|    28 | Zhang Sanfeng |  94 | M      |    NULL |         1 |
+-------+---------------+-----+--------+---------+-----------+学生表的年龄比老师平均年龄大的是select stuid,name,age from students union select tid,name,age from teachers;
+-------+---------------+-----+
| stuid | name          | age |
+-------+---------------+-----+
|     1 | Shi Zhongyu   |  22 |
|     2 | Shi Potian    |  22 |
|     3 | Xie Yanke     |  53 |
|     4 | Ding Dian     |  32 |
|     5 | Yu Yutong     |  26 |
|     6 | Shi Qing      |  46 |
|     7 | Xi Ren        |  19 |
|     8 | Lin Daiyu     |  17 |
|     9 | Ren Yingying  |  20 |
|    10 | Yue Lingshan  |  19 |
|    11 | Yuan Chengzhi |  23 |
|    12 | Wen Qingqing  |  19 |
|    13 | Tian Boguang  |  33 |
|    14 | Lu Wushuang   |  17 |       合并学生表和老师表的指定列
|    15 | Duan Yu       |  19 |
|    16 | Xu Zhu        |  21 |
|    17 | Lin Chong     |  25 |
|    18 | Hua Rong      |  23 |
|    19 | Xue Baochai   |  18 |
|    20 | Diao Chan     |  19 |
|    21 | Huang Yueying |  22 |
|    22 | Xiao Qiao     |  20 |
|    23 | Ma Chao       |  23 |
|    24 | Xu Xian       |  27 |
|    25 | Sun Dasheng   | 100 |
|    26 | wei           |  21 |
|    27 | Song Jiang    |  45 |
|    28 | Zhang Sanfeng |  94 |
|    29 | Miejue Shitai |  77 |
|     1 | Song Jiang    |  45 |
|     2 | Zhang Sanfeng |  94 |
|     3 | Miejue Shitai |  77 |
|     4 | Lin Chaoying  |  93 |
+-------+---------------+-----+mysql> select * from students where age > (select avg(age) from teachers);
#从名为students的表中选择所有年龄大于teachers表中年龄平均值的记录。
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name        | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
|    25 | Sun Dasheng | 100 | M      |    NULL |      NULL |
+-------+-------------+-----+--------+---------+-----------+
mysql> update students set age=(select avg(age)from teachers) where stuid=25;
Query OK, 1 row affected (0.00 sec)
#将teachers表中age字段的平均值更新到students表中stuid为25的记录的age字段。
mysql> select * from students order by age desc;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|    25 | Sun Dasheng   |  77 | M      |    NULL |      NULL |

联合查询union
交叉连接 cross join
内连接  inner join 
外连接  left outer join  right outer join 

内连接  inner join

inner join 内连接取多个表的交集

mysql> select * from students inner join teachers on students.gender=teachers.gender;
+-------+---------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
| 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      |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |   1 | Song Jiang    |  45 | M      |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |   2 | Zhang Sanfeng |  94 | M      |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |   1 | Song Jiang    |  45 | M      |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |   2 | Zhang Sanfeng |  94 | M      |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |   1 | Song Jiang    |  45 
联合查询union

联合查询 Union 实现的条件,多个表的字段数量相同,字段名和数据类型可以不同,但一般数据类型是相同的.

mysql> select stuid,name,age from students-> union-> select tid,name,age from teachers;
#取两个表的tid,name,age 合并在一起  ,这样搞容易错乱
mysql> select stuid,name,age from students union select tid,name,age from teachers;
+-------+---------------+-----+
| stuid | name          | age |
+-------+---------------+-----+
|     1 | Shi Zhongyu   |  22 |
|     2 | Shi Potian    |  22 |
|     3 | Xie Yanke     |  53 |
|     4 | Ding Dian     |  32 |
|     5 | Yu Yutong     |  26 |
|     6 | Shi Qing      |  46 |
|     7 | Xi Ren        |  19 |
|     8 | Lin Daiyu     |  17 |
|     9 | Ren Yingying  |  20 |
|    10 | Yue Lingshan  |  19 |
|    11 | Yuan Chengzhi |  23 |
|    12 | Wen Qingqing  |  19 |
|    13 | Tian Boguang  |  33 |
|    14 | Lu Wushuang   |  17 |
|    15 | Duan Yu       |  19 |
|    16 | Xu Zhu        |  21 |
|    17 | Lin Chong     |  25 |
|    18 | Hua Rong      |  23 |
|    19 | Xue Baochai   |  18 |
|    20 | Diao Chan     |  19 |
|    21 | Huang Yueying |  22 |
|    22 | Xiao Qiao     |  20 |
|    23 | Ma Chao       |  23 |
|    24 | Xu Xian       |  27 |
|    25 | Sun Dasheng   |  77 |
|    26 | wei           |  21 |
|    27 | wsd           |  22 |
|     1 | Song Jiang    |  45 |
|     2 | Zhang Sanfeng |  94 |
|     3 | Miejue Shitai |  77 |
|     4 | Lin Chaoying  |  93 |
+-------+---------------+-----+
select * from students where age > (select avg(age) from teachers);
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|    25 | Sun Dasheng   | 100 | M      |    NULL |         1 |
|    28 | Zhang Sanfeng |  94 | M      |    NULL |         1 |
+-------+---------------+-----+--------+---------+-----------+学生表的年龄比老师平均年龄大的是select stuid,name,age from students union select tid,name,age from teachers;
+-------+---------------+-----+
| stuid | name          | age |
+-------+---------------+-----+
|     1 | Shi Zhongyu   |  22 |
|     2 | Shi Potian    |  22 |
|     3 | Xie Yanke     |  53 |
|     4 | Ding Dian     |  32 |
|     5 | Yu Yutong     |  26 |
|     6 | Shi Qing      |  46 |
|     7 | Xi Ren        |  19 |
|     8 | Lin Daiyu     |  17 |
|     9 | Ren Yingying  |  20 |
|    10 | Yue Lingshan  |  19 |
|    11 | Yuan Chengzhi |  23 |
|    12 | Wen Qingqing  |  19 |
|    13 | Tian Boguang  |  33 |
|    14 | Lu Wushuang   |  17 |       合并学生表和老师表的指定列
|    15 | Duan Yu       |  19 |
|    16 | Xu Zhu        |  21 |
|    17 | Lin Chong     |  25 |
|    18 | Hua Rong      |  23 |
|    19 | Xue Baochai   |  18 |
|    20 | Diao Chan     |  19 |
|    21 | Huang Yueying |  22 |
|    22 | Xiao Qiao     |  20 |
|    23 | Ma Chao       |  23 |
|    24 | Xu Xian       |  27 |
|    25 | Sun Dasheng   | 100 |
|    26 | wei           |  21 |
|    27 | Song Jiang    |  45 |
|    28 | Zhang Sanfeng |  94 |
|    29 | Miejue Shitai |  77 |
|     1 | Song Jiang    |  45 |
|     2 | Zhang Sanfeng |  94 |
|     3 | Miejue Shitai |  77 |
|     4 | Lin Chaoying  |  93 |
+-------+---------------+-----+mysql> select * from students where age > (select avg(age) from teachers);
#从名为students的表中选择所有年龄大于teachers表中年龄平均值的记录。
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name        | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
|    25 | Sun Dasheng | 100 | M      |    NULL |      NULL |
+-------+-------------+-----+--------+---------+-----------+
mysql> update students set age=(select avg(age)from teachers) where stuid=25;
Query OK, 1 row affected (0.00 sec)
#将teachers表中age字段的平均值更新到students表中stuid为25的记录的age字段。
mysql> select * from students order by age desc;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|    25 | Sun Dasheng   |  77 | M      |    NULL |      NULL |
交叉连接等cross join


cross join 即多表的记录之间做笛卡尔乘积组合,并且多个表的列横向合并相加, "雨露均沾"
比如: 第一个表3行4列,第二个表5行6列,cross join后的结果为3*5=15行,4+6=10列
交叉连接生成的记录可能会非常多,建议慎用

union 横行结合 #横向合并,交叉连接(横向笛卡尔)交叉连接 100和100的数据 是100x100的合成数据 尽量千万别这样做
select * from students inner join teachers on students.gender=teachers.gender;+-------+---------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
| StuID | Name          | Age | Gender | ClassID | TeacherID | TID | Name          | Age | Gender |
+-------+---------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |   4 | Lin Chaoying  |  93 | F      |
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |   3 | Miejue Shitai |  77 | F      |
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |   2 | Zhang Sanfeng |  94 | M      |
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |   1 | Song Jiang    |  45 | M      |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |   4 | Lin Chaoying  |  93 | F      |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |   3 | Miejue Shitai |  77 | F      |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |   2 | Zhang Sanfeng |  94 | M      |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |   1 | Song Jiang    |  45 | M      |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |   4 | Lin Chaoying  |  93 | F      |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |   3 | Miejue Shitai |  77 | F      |``````````````````````````#从students表和teachers表中选择所有的列,并且只返回那些在teacherid列和tid列上具有相同值的行。
SELECT 列名
FROM 表1
INNER JOIN 表2
ON 表1.列名 = 表2.列名;mysql> select *from students inner join teachers on students.teacherid=teachers.tid;
+-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
| StuID | Name        | Age | Gender | ClassID | TeacherID | TID | Name          | Age | Gender |
+-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
|     5 | Yu Yutong   |  26 | M      |       3 |         1 |   1 | Song Jiang    |  45 | M      |
|     1 | Shi Zhongyu |  22 | M      |       2 |         3 |   3 | Miejue Shitai |  77 | F      |
|     4 | Ding Dian   |  32 | M      |       4 |         4 |   4 | Lin Chaoying  |  93 | F      |
+-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+-
左外连接left outer join
学生表对应的老师表id
#获取所有学生的信息,并且将他们所属的教师信息也一并获取。如果某个学生没有对应的教师信息,则该学生的教师信息会显示为NULL。
mysql> select * from students left outer join teachers on students.teacherid=teachers.tid;
+-------+---------------+-----+--------+---------+-----------+------+---------------+------+--------+
| StuID | Name          | Age | Gender | ClassID | TeacherID | TID  | Name          | Age  | Gender |
+-------+---------------+-----+--------+---------+-----------+------+---------------+------+--------+
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |    1 | Song Jiang    |   45 | M      |
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |    3 | Miejue Shitai |   77 | F      |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |    4 | Lin Chaoying  |   93 | F      |
|     2 | Shi Potian    |  22 | M      |       1 |         7 | NULL | NULL          | NULL | NULL   |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 | NULL | NULL          | NULL | NULL   |
|     6 | Shi Qing      |  46 | M      |       5 |      NULL | NULL | NULL          | NULL···················· 
 右外连接right outer join
  • 获取所有教师的信息,并且将他们所教授的学生信息也一并获取。如果某个教师没有对应的学生信息,则该教师的学生信息会显示为NULL。
mysql> select * from students right outer join teachers on students.teacherid=teachers.tid;
+-------+-------------+------+--------+---------+-----------+-----+---------------+-----+--------+
| StuID | Name        | Age  | Gender | ClassID | TeacherID | TID | Name          | Age | Gender |
+-------+-------------+------+--------+---------+-----------+-----+---------------+-----+--------+
|     1 | Shi Zhongyu |   22 | M      |       2 |         3 |   3 | Miejue Shitai |  77 | F      |
|     4 | Ding Dian   |   32 | M      |       4 |         4 |   4 | Lin Chaoying  |  93 | F      |
|     5 | Yu Yutong   |   26 | M      |       3 |         1 |   1 | Song Jiang    |  45 | M      |
|  NULL | NULL        | NULL | NULL   |    NULL |      NULL |   2 | Zhang Sanfeng |  94 | M      |
+-------+-------------+------+--------+---------+-----------+-----+---------------+-----+--------+
取没有交际的表
mysql> select * from students left outer join teachers on students.teacherid=teachers.tid is null;
+-------+---------------+-----+--------+---------+-----------+------+---------------+------+--------+
| StuID | Name          | Age | Gender | ClassID | TeacherID | TID  | Name          | Age  | Gender |
+-------+---------------+-----+--------+---------+-----------+------+---------------+------+--------+
|     6 | Shi Qing      |  46 | M      |       5 |      NULL |    1 | Song Jiang    |   45 | M      |
|     7 | Xi Ren        |  19 | F      |       3 |      NULL |    1 | Song Jiang    |   45 | M      |
|     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |    1 | Song Jiang    |   45 | M      |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |    1 | Song Jiang    |   45 | M      |
|    10 | Yue Lingshan  |  19 | F      |       3 |      NULL |    1 | Song Jiang    |   45 | M      |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |    1 | Song Jiang    |   45 | M      |
|    12 | Wen Qingqing  |  19 | F      |       1 |      NULL |    1 | Song Jiang    |   45 | M      |
|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |    1 | Song Jiang    |   45 | M      |
完全外连接 

就是左连接加右链接组成的

ysql> select * from students left outer join teachers on students.teacherid=teachers.tid-> union-> select * from students right join teachers on students.teacherid=teachers.tid-> ;
+-------+---------------+------+--------+---------+-----------+------+---------------+------+--------+
| StuID | Name          | Age  | Gender | ClassID | TeacherID | TID  | Name          | Age  | Gender |
+-------+---------------+------+--------+---------+-----------+------+---------------+------+--------+
|     5 | Yu Yutong     |   26 | M      |       3 |         1 |    1 | Song Jiang    |   45 | M      |
|     1 | Shi Zhongyu   |   22 | M      |       2 |         3 |    3 | Miejue Shitai |   77 | F      |
|     4 | Ding Dian     |   32 | M      |       4 |         4 |    4 | Lin Chaoying  |   93 | F      |
|     2 | Shi Potian    |   22 | M      |       1 |         7 | NULL | NULL          | NULL | NULL   |
|     3 | Xie Yanke     |   53 | M      |       2 |        16 | NULL | NULL          | NULL | NULL   |
|     6 | Shi Qing      |   46 | M      |       5 |      NULL | NULL | NULL          | NULL | NULL   |
|     7 | Xi Ren        |   19 | F      |       3 |      NULL | NULL | NULL          | NULL | NULL   |
|     8 | Lin Daiyu     |   17 | F      |       7 |      NULL | NULL | NULL          | NULL | NULL   |
自连接, 即表自身连接自身 

创建

写入

合并 e.leader_id=l.emp_id的对比  一个表格取俩个代号;

mysql> create table emp (emp_id int auto_increment primary key ,name varchar(10),leader_id int);
mysql> insert emp(name,leader_id)value('mage',null),('zhangsir',1),('wang',1),('li',3),('zhao',2);
mysql> select * from emp;
+--------+----------+-----------+
| emp_id | name     | leader_id |
+--------+----------+-----------+
|      1 | mage     |      NULL |
|      2 | zhangsir |         1 |
|      3 | wang     |         1 |
|      4 | li       |         3 |
|      5 | zhao     |         2 |···········
合并
mysql> select * from emp e inner join emp l on e.leader_id=l.emp_id;
+--------+----------+-----------+--------+----------+-----------+
| emp_id | name     | leader_id | emp_id | name     | leader_id |
+--------+----------+-----------+--------+----------+-----------+
|      2 | zhangsir |         1 |      1 | mage     |      NULL |
|      3 | wang     |         1 |      1 | mage     |      NULL |
|      4 | li       |         3 |      3 | wang     |         1 |
|      5 | zhao     |         2 |      2 | zhangsir |         1 |

 null换名(无上级)

mysql> select e.name emp_name, l.name leader_name from emp e left join emp l on e.leader_id=l.emp_id;
+----------+-------------+
| emp_name | leader_name |
+----------+-------------+
| mage     | NULL        |
| zhangsir | mage        |
| wang     | mage        |
| li       | wang        |
| zhao     | zhangsir    |
| mage     | NULL        |
| zhangsir | mage        |
| wang     | mage        |
| li       | wang        |
| zhao     | zhangsir    |
+----------+-------------+
10 rows in set (0.00 sec)mysql> select e.name emp_name, IFNULL(l.name, '无上级' ) leader_name from emp e left join emp l on e.leader_id=l.emp_id;
+----------+-------------+
| emp_name | leader_name |
+----------+-------------+
| mage     | 无上级      |
| zhangsir | mage        |
| wang     | mage        |
| li       | wang        |
| zhao     | zhangsir    |

三个表内连接查询

mysql> select * from scores;      #分数表                                                                                             
+----+-------+----------+-------+
| ID | StuID | CourseID | Score |
+----+-------+----------+-------+
|  1 |     1 |        2 |    77 |
|  2 |     1 |        6 |    93 |
|  3 |     2 |        2 |    47 |
|  4 |     2 |        5 |    97 |
|  5 |     3 |        2 |    88 |
|  6 |     3 |        6 |    75 |
|  7 |     4 |        5 |    71 |
|  8 |     4 |        2 |    89 |
|  9 |     5 |        1 |    39 |
| 10 |     5 |        7 |    63 |
| 11 |     6 |        1 |    96 |
| 12 |     7 |        1 |    86 |
| 13 |     7 |        7 |    83 |
| 14 |     8 |        4 |    57 |
| 15 |     8 |        3 |    93 |
+----+-------+----------+-------+mysql> select * from courses;  #课程表
+----------+----------------+
| CourseID | Course         |
+----------+----------------+
|        1 | Hamo Gong      |
|        2 | Kuihua Baodian |
|        3 | Jinshe Jianfa  |
|        4 | Taiji Quan     |
|        5 | Daiyu Zanghua  |
|        6 | Weituo Zhang   |
|        7 | Dagou Bangfa   |
+----------+----------------+mysql> select * from students;;    学生表
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |
|     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   |  77 | M      |    NULL |      NULL |
|    26 | wei           |  21 | F      |       1 |      NULL |
|    27 | wsd           |  22 | M      |    NULL |      NULL |
+-------+---------------+-----+--------+---------+-----------+
27 rows in set (0.00 sec)mysql> select students.name, Course, Score from students inner join scores on students.stuid = scores.stuid inner join courses on scores.CourseID = courses.CourseID;
+-------------+----------------+-------+
| name        | Course         | Score |
+-------------+----------------+-------+
| Shi Zhongyu | Kuihua Baodian |    77 |
| Shi Zhongyu | Weituo Zhang   |    93 |
| Shi Potian  | Kuihua Baodian |    47 |
| Shi Potian  | Daiyu Zanghua  |    97 |
| Xie Yanke   | Kuihua Baodian |    88 |
| Xie Yanke   | Weituo Zhang   |    75 |
| Ding Dian   | Daiyu Zanghua  |    71 |
| Ding Dian   | Kuihua Baodian |    89 |
| Yu Yutong   | Hamo Gong      |    39 |
| Yu Yutong   | Dagou Bangfa   |    63 |
| Shi Qing    | Hamo Gong      |    96 |
| Xi Ren      | Hamo Gong      |    86 |
| Xi Ren      | Dagou Bangfa   |    83 |
| Lin Daiyu   | Taiji Quan     |    57 |
| Lin Daiyu   | Jinshe Jianfa  |    93 |
+-------------+----------------+-------+

FUNCTION 函数

count 统计多少行

 select count(*) from students;

 

mysql> select count(TeacherID ) from students;    #TeacherID的是数值空不算
+-------------------+
| count(TeacherID ) |
+-------------------+
|                 5 |
+-------------------+

 定义别名,执行别名

create view v_students_coures_scores as 定义view v_students_coures_scores为  别名​​​​​​​

mysql> create view v_students_coures_scores as select students.name, Course, Score from students inner join scores on students.stuid = scores.stuid inner join courses on scores.CourseID = courses.CourseID;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+--------------------------+
| Tables_in_hellodb        |
+--------------------------+
| classes                  |
| coc                      |
| courses                  |
| emp                      |
| scores                   |
| students                 |
| teachers                 |
| toc                      |
| v_students_coures_scores |
+--------------------------+mysql> select * from v_students_coures_scores;
+-------------+----------------+-------+
| name        | Course         | Score |
+-------------+----------------+-------+
| Shi Zhongyu | Kuihua Baodian |    77 |
| Shi Zhongyu | Weituo Zhang   |    93 |
| Shi Potian  | Kuihua Baodian |    47 |
| Shi Potian  | Daiyu Zanghua  |    97 |
| Xie Yanke   | Kuihua Baodian |    88 |
| Xie Yanke   | Weituo Zhang   |    75 |
| Ding Dian   | Daiyu Zanghua  |    71 |
| Ding Dian   | Kuihua Baodian |    89 |
| Yu Yutong   | Hamo Gong      |    39 |
| Yu Yutong   | Dagou Bangfa   |    63 |
| Shi Qing    | Hamo Gong      |    96 |
| Xi Ren      | Hamo Gong      |    86 |
| Xi Ren      | Dagou Bangfa   |    83 |
| Lin Daiyu   | Taiji Quan     |    57 |
| Lin Daiyu   | Jinshe Jianfa  |    93 |
+-------------+----------------+-------+

VIEW视图查看 

mysql> show table status like 'v_students_coures_scores'\G;
*************************** 1. row ***************************Name: v_students_coures_scoresEngine: NULLVersion: NULLRow_format: NULLRows: NULLAvg_row_length: NULLData_length: NULL
Max_data_length: NULLIndex_length: NULLData_free: NULLAuto_increment: NULLCreate_time: NULLUpdate_time: NULLCheck_time: NULLCollation: NULLChecksum: NULLCreate_options: NULLComment: VIEW
1 row in set (0.00 sec)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/214088.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

界面组件DevExpress Reporting v23.1 - Web报表设计器功能升级

DevExpress Reporting是.NET Framework下功能完善的报表平台&#xff0c;它附带了易于使用的Visual Studio报表设计器和丰富的报表控件集&#xff0c;包括数据透视表、图表&#xff0c;因此您可以构建无与伦比、信息清晰的报表 界面组件DevExpress Reporting v23.1已经发布一段…

Vue19 列表过滤

直接上代码 以下代码使用了两种实现方式&#xff0c;监视属性和计算属性 当能用计算属性实现时&#xff0c;推荐使用计算属性 <!DOCTYPE html> <html><head><meta charset"UTF-8" /><title>列表过滤</title><script type&q…

儿童在线学习系统 宝宝云幼儿园服务微信小程序的设计与实现

综合运用所学的程序设计基础、数据结构、数据库原理及应用、高级语言程序设计、面向对象程序设计、软件需求分析与建模、软件设计与体系结构、软件测试等课程知识&#xff0c;设计开发一个较实用的应用系统。 通过该设计可以巩固并提高软件工程专业学生的软件需求分析、设计、开…

杰发科技AC7801——Flash模拟EEP内存分布情况

简介 本文记录了在使用AutoChips芯片Flash模拟EEP过程中的一些理解 核心代码如下 #include <stdlib.h> #include "ac780x_sweeprom.h" #include "ac780x_debugout.h"#define SWEEPROM_SIZE (2048UL) /* Ssoftware eeprom size(Byte) */ #define TE…

Lubuntu 23.10用户可使用LXQt 1.4桌面

导读在众多 Lubuntu 用户的要求下&#xff0c;Lubuntu 开发人员决定将 LXQt 1.4 桌面环境向后移植到最新的 Lubuntu 23.10 &#xff08;Mantic Minotaur&#xff09; 版本。 是的&#xff0c;您没看错&#xff0c;您现在可以使用官方的 Lubuntu Backports PPA&#xff08;个人软…

从文本生成到数据增强:探索 AI 前沿的开源套件 | 开源专题 No.44

Significant-Gravitas/AutoGPT Stars: 150.4k License: MIT AutoGPT 是开源 AI 代理生态系统的核心工具包。它采用模块化和可扩展的框架&#xff0c;使您能够专注于以下方面&#xff1a; 构建 - 为惊人之作打下基础。测试 - 将您的代理调整到完美状态。查看 - 观察进展成果呈…

【2023 云栖】阿里云刘一鸣:Data+AI 时代大数据平台建设的思考与发布

云布道师 本文根据 2023 云栖大会演讲实录整理而成&#xff0c;演讲信息如下&#xff1a; 演讲人&#xff1a;刘一鸣 | 阿里云自研大数据产品负责人 演讲主题&#xff1a;DataAI 时代大数据平台应该如何建设 今天分享的主题是 DataAI 时代大数据平台应该如何建设&#xff0…

docker安装nacos,实现和mysql容器的通信

1.下载nacos镜像 docker pull nacos/nacos-server2. 启动nacos 启动命令如下&#xff1a; docker run -d -p 8848:8848 --name nacos \ -e JVM_XMS256m \ -e JVM_XMX256m \ -e MODEstandalone \ -e SPRING_DATASOURCE_PLATFORMmysql \ -e MYSQL_SERVICE_HOST192.168.131.223…

JVM-基础

jdk7及以前&#xff1a; 通过-XX:PermSize 来设置永久代初始分配空间&#xff0c;默认值是20.75m -XX:MaxPermSize来设定永久代最大可分配空间&#xff0c;32位是64m&#xff0c;64位是82m jdk8及之后&#xff1a; 通过-XX:MetaspaceSize 来设置永久代初始分配空间&#xff…

ubuntu22.04 arrch64版在线安装maven

脚本 if type -p mvn; thenecho "maven has been installed."elsecd /home/zenglgwget https://dlcdn.apache.org/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.tar.gz --no-check-certificatetar vxf apache-maven-3.9.5-bin.tar.gz rm -rf /usr/local/mav…

2023年亚太杯数学建模A题水果采摘机器人的图像识别功能(免费思路)

中国是世界上最大的苹果生产国&#xff0c;年产量约为 3500 万吨。同时&#xff0c;中国也是世界上最大的苹果出口国&#xff0c;世界上每两个苹果中就有一个出口到国。世界上每两个苹果中就有一个来自中国&#xff0c;中国出口的苹果占全球出口量的六分之一以上。来自中国。中…

HandBrake 1.7 近日发布

导读HandBrake 1.7 近日发布&#xff0c;作为这个开源、免费和跨平台视频转码器应用程序的重大更新&#xff0c;适用于 GNU/Linux、macOS 和 Windows 系统。 在 HandBrake 1.6 发布近一年后&#xff0c;HandBrake 1.7 版本为 Linux 用户提供了许多好处&#xff0c;包括视频摘要…