MySQL-notes

MySQL 学习笔记:

  • 一、进入数据库:
  • 二、查看(database/table)
  • 三、使用数据库:
  • 四、创建(database/table):
  • 五、更改(表名/表中某栏目字段的数据类型)
  • 六、删除(数据库/表格):
  • 七、查询(表格内详细信息):
  • 八、插入数据(table):
  • 九、表格中删除某列表栏:
  • 十、日期和时间类型:
  • 十一、MySQL中的连接符:(concat/+)
  • 十二、查询:(表达)(逻辑)(模糊)
  • 十三、查看/更改字符类型:character
  • 十四、查看字符:show variables like "%char%";
  • 十五、函数(单行):
  • 十六、分组函数:
  • 十七、查看MySQL默认存储引擎:
  • 十八、常见数据类型:
  • 十九、查看MySQL中的字符集:
  • 二十、查看GBK字符集对应的排序规则:
  • 二十一、select database()
  • 二十二、主键约束:
  • 二十三、查看当前服务器使用的字符集:
  • 二十四、查看存储引擎:show engines;
  • 二十五、改变数据:update:
  • 二十六、导入文件:source
  • 二十七、改变字符类型:set names utf8/gbk;
  • 二十八、limit
  • 二十九、嵌套查询(子查询):
  • 三十、去重:distinct
  • 三十一、迪卡耳机错误:
  • 三十二、外键查询:
  • 三十三、select * from hero.attempt


一、进入数据库:

方式一:Windows+r ,输入cmd, 输入:mysql -uroot -p ,回车输入密码。
	实例如下:
	C:\Users\admin>mysql -uroot -p
	Enter password: ****
方式二:左下窗口找到mysql自带的命令窗口,进入窗口后输入密码即可。

二、查看(database/table)

数据库(databases):
	语法:show databases;(回车)
		实例如下:
			show databases;
			+--------------------+
			| Database           |
			+--------------------+
			| information_schema |
			| mysql              |
			| performance_schema |
			| sys                |
			+--------------------+
初次login:只包含这四个数据库
表(table):
	语法:> show tables;
		 > use hero(库名);
		实例如下:
		mysql> show tables;
		+-------------------+
		| Tables_in_ku_name |
		+-------------------+
		| hero              |
		+-------------------+

三、使用数据库:

语法:use 库名;

四、创建(database/table):

数据库:
	create database 库名;
		案例:mysql>create database ku_name;
创建表:
	方式一:
		语法:create table 表名(列表栏目名 类型,列表栏目名 类型,....);
			案例:mysql>create table hero(id int, name varchar(10),age int,salary varchar(10));
	方式二:
		语法:create table 表名(列表栏目名 类型,列表栏目名 类型,....) character set=utf8/charset=utf8;
			案例:mysql> create table trying(id int,name varchar(10),age int(2),salary bigint) character set=utf8;
				Query OK, 0 rows affected (0.06 sec)

五、更改(表名/表中某栏目字段的数据类型)

其一:表名:
	方式一:语法:rename tables oldName to newName;
			案例:rename tables hero to newhero;
	方式二:语法:alter table oldName rename newName;
			案例:mysql> alter table whitep rename white;
				Query OK, 0 rows affected (0.02 sec)
其二:修改表中某栏目字段的数据类型:
	语法:alter table 表名 modify 栏目名 数据类型 (first/after 栏目名);
		mysql> alter table trying modify id int(2);
		Query OK, 0 rows affected (0.04 sec)
		Records: 0  Duplicates: 0  Warnings: 0
其三:修改表中栏目名与数据类型
	语法:alter table 表名 change old栏目Name new栏目Name 数据类型;
		mysql> alter table trying change id numble int(3);
		Query OK, 0 rows affected (0.04 sec)
		Records: 0  Duplicates: 0  Warnings: 0
update的使用:
	更改表格中已存在的数据:
	mysql> update user set name='andy' where id=1;
	mysql> update user set name='andy1', age=9;

六、删除(数据库/表格):

删除数据库:
	语法:drop database 库名;
		例如:drop database ku_name;
删除表格:
	语法:drop table 表名;
		例如: drop table blackhero;
删除表格中栏目:
	语法:alter table 表名 drop 栏目名;
		mysql> alter table trying drop numble;
		Query OK, 0 rows affected (0.12 sec)
		Records: 0  Duplicates: 0  Warnings: 0

七、查询(表格内详细信息):

方式一:select * from 表名;
	实例如下:
	mysql> select * from employee;
	+--------+------+--------+-------+--------+------------------+------------+
	| name   | age  | gender | birth | salary | job              | department |
	+--------+------+--------+-------+--------+------------------+------------+
	| tey    |  101 | 男     | NULL  |  12300 | 湖南              |         12 |
	| oresd  |  101 | 男     | NULL  |  12300 | 湖南              |         12 |
	+--------+------+--------+-------+--------+------------------+------------+

方式二:desc 表名;
			案例:
				mysql> desc blackpeople;
				+-------+-------------+------+-----+---------+-------+
				| Field | Type        | Null | Key | Default | Extra |
				+-------+-------------+------+-----+---------+-------+
				| id    | int(11)     | YES  |     | NULL    |       |
				| name  | varchar(10) | YES  |     | NULL    |       |
				| age   | int(11)     | YES  |     | NULL    |       |
				+-------+-------------+------+-----+---------+-------+
方式三:show columns from 表名;
			案例:
				mysql> show columns from commodity;
				+-------+----------+------+-----+------------+-------+
				| Field | Type     | Null | Key | Default    | Extra |
				+-------+----------+------+-----+------------+-------+
				| id    | int(20)  | YES  |     | 4399       |       |
				| name  | char(10) | YES  |     | "Prudence" |       |
				| age   | int(1)   | YES  |     | 18         |       |
				+-------+----------+------+-----+------------+-------+
				3 rows in set (0.00 sec)

八、插入数据(table):

其一:语法格式:insert into 表名(列表名) values(数据);
		insert into blackpeople(id,name,age) values(1,"name",18);
		+------+------+------+
		| id   | name | age  |
		+------+------+------+
		|    1 | name |   18 |
		+------+------+------+
	可一次插入多条数据:mysql> insert into user(name,age)values('xiao1',23),('xiao2',12),('xiao3',35);

其二:语法:alter table 表名 add 列表栏目名 数据类型;
	实例:alter table blackpeople add salary bigint(10);
		结果显示:
		mysql> select * from blackpeople;
		+------+------+------+--------+
		| id   | name | age  | salary |
		+------+------+------+--------+
		|    1 | name |   18 |   NULL |
		+------+------+------+--------+

其三:语法:alter table 表名 add 列表栏目名 数据类型 after 表格中已有数据的栏目名;
		实例结果:
		mysql> alter table blackpeople add sex char(2) after name;
		Query OK, 0 rows affected (0.09 sec)
		Records: 0  Duplicates: 0  Warnings: 0
		mysql> select * from blackpeople;
		+------+------+------+------+--------+
		| id   | name | sex  | age  | salary |
		+------+------+------+------+--------+
		|    1 | name | NULL |   18 |   NULL |
		+------+------+------+------+--------+
		1 row in set (0.00 sec)____

其四:语法:alter table 表名 add 栏目名 数据类型 first;
		mysql> alter table trying add id int(2) first;
		Query OK, 0 rows affected (0.09 sec)
		Records: 0  Duplicates: 0  Warnings: 0

九、表格中删除某列表栏:

语法:alter table 表名 drop column 表中栏目名;
	案例:
		mysql> alter table blackpeople drop column address;
		Query OK, 0 rows affected (0.08 sec)
		Records: 0  Duplicates: 0  Warnings: 0
delete:语法:delete from 表名 where 栏目条件筛选;
	案例:mysql> delete from black where id!=3;
		Query OK, 4 rows affected (0.03 sec)

十、日期和时间类型:

	/*
	date 	2019-8-3
	datetime 	2019-8-3 10:05:30
	timestamp 	时间戳
	time 	10:05:30
	*/ 
		例如:
		mysql> create table Time(
			-> dateymd date,
			-> dateTime datetime,
			-> timestamp_ timestamp,
			-> time_ time);
		Query OK, 0 rows affected (0.05 sec)
		mysql> desc Time;
		+------------+-----------+------+-----+-------------------+-----------------------------+
		| Field      | Type      | Null | Key | Default           | Extra                       |
		+------------+-----------+------+-----+-------------------+-----------------------------+
		| dateymd    | date      | YES  |     | NULL              |                             |
		| dateTime   | datetime  | YES  |     | NULL              |                             |
		| timestamp_ | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
		| time_      | time      | YES  |     | NULL              |                             |
		+------------+-----------+------+-----+-------------------+-----------------------------+
		4 rows in set (0.01 sec)
	在日期类型中插入数据/显示:
		mysql> insert into Time(dateymd,dateTime,time_) values("2121.3.16","2021.03.16 20.30.59",083059);
		Query OK, 1 row affected (0.03 sec)
		mysql> select * from Time;
		+------------+---------------------+---------------------+----------+
		| dateymd    | dateTime            | timestamp_          | time_    |
		+------------+---------------------+---------------------+----------+
		| 2121-03-16 | 2021-03-16 20:30:59 | 2021-03-16 20:32:43 | 08:30:59 |
		+------------+---------------------+---------------------+----------+
		1 row in set (0.00 sec)

日期和时间类型:
	注释:
	# 对于所有的日期时间类型数据,在赋值时可以用双引号,也可用单引号,也可不用引号
	# 对于所有的日期时间类型数据,在赋值时年月日时分秒之间可以加“-”,“:”等符号,也可以不加任何符号(1990-01-01或19900101)
	# 对于timestamp数据类型,有默认值CURRENT_TIMESTAMP,就是当前时间
	# 对于所有的日期时间数据类型,赋值时都可用now()来赋当前时间
	# 对于date类型,赋值时可以用curdate()
	# 对于time类型,赋值时可以用curtime()
	# 对于datetime和timestamp数据类型,可以给默认值now()
	————————————————
		mysql> create table time_type(id int(10) primary key auto_increment,date date,time time,year year,datetime datetime,timestamp timestamp)engine=innodb default charset=utf8;
		Query OK, 0 rows affected (0.04 sec)

		mysql> desc time_type;
		+-----------+-----------+------+-----+-------------------+-----------------------------+
		| Field     | Type      | Null | Key | Default           | Extra                       |
		+-----------+-----------+------+-----+-------------------+-----------------------------+
		| id        | int(10)   | NO   | PRI | NULL              | auto_increment              |
		| date      | date      | YES  |     | NULL              |                             |
		| time      | time      | YES  |     | NULL              |                             |
		| year      | year(4)   | YES  |     | NULL              |                             |
		| datetime  | datetime  | YES  |     | NULL              |                             |
		| timestamp | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
		+-----------+-----------+------+-----+-------------------+-----------------------------+
		6 rows in set (0.00 sec)

		mysql> insert into time_type(date,time,year,datetime,timestamp)values(now(),'21:12:02',"2018",'1998-12-12 12:12:12','19800501111111');
		Query OK, 1 row affected, 1 warning (0.00 sec)

		mysql> select * from time_type;
		+----+------------+----------+------+---------------------+---------------------+
		| id | date       | time     | year | datetime            | timestamp           |
		+----+------------+----------+------+---------------------+---------------------+
		|  1 | 2019-12-26 | 21:12:02 | 2018 | 1998-12-12 12:12:12 | 1980-05-01 11:11:11 |
		+----+------------+----------+------+---------------------+---------------------+
		1 row in set (0.00 sec)

		mysql> insert into time_type(date,time,year,datetime)values(20191101,'21:12:02',"2018",20151201230102);
		Query OK, 1 row affected (0.00 sec)

		mysql> select * from time_type;
		+----+------------+----------+------+---------------------+---------------------+
		| id | date       | time     | year | datetime            | timestamp           |
		+----+------------+----------+------+---------------------+---------------------+
		|  1 | 2019-12-26 | 21:12:02 | 2018 | 1998-12-12 12:12:12 | 1980-05-01 11:11:11 |
		|  2 | 2019-11-01 | 21:12:02 | 2018 | 2015-12-01 23:01:02 | 2019-12-26 19:23:19 |
		+----+------------+----------+------+---------------------+---------------------+
		2 rows in set (0.00 sec)

十一、MySQL中的连接符:(concat/+)

其一:'+'号:用于运算:
	如:select 100+10;结果是:110;
		select 110+'abc';结果是:100
		select 110+null;结果是:null

其二:关键字:concat(用于连接)
	实例如下:
		mysql> select*from blackp;
		+------+----------+------+------+--------+
		| id   | name     | sex  | age  | salary |
		+------+----------+------+------+--------+
		|    1 | tyz      | NULL |   18 |   NULL |
		|    2 | prudence | N    |   18 | 110000 |
		+------+----------+------+------+--------+
		2 rows in set (0.00 sec)

		mysql> select concat(id,name,age) as ina from blackp;
		+-------------+
		| ina         |
		+-------------+
		| 1tyz18      |
		| 2prudence18 |
		+-------------+
		2 rows in set (0.00 sec)

十二、查询:(表达)(逻辑)(模糊)

方式一:按条件表达式查询(运算符的运用):
	'>';'<';'=';'!='的运用:
	案例:
		mysql> select *from blackp;
		+------+----------+------+------+--------+
		| id   | name     | sex  | age  | salary |
		+------+----------+------+------+--------+
		|    1 | name     | nu   |   18 | 444441 |
		|    2 | prudence | na   |   19 | 110000 |
		|    3 | supreme  | nu   |   24 | 123456 |
		+------+----------+------+------+--------+
		3 rows in set (0.00 sec)

		mysql> select * from blackp where salary>111111;
		+------+---------+------+------+--------+
		| id   | name    | sex  | age  | salary |
		+------+---------+------+------+--------+
		|    1 | name    | nu   |   18 | 444441 |
		|    3 | supreme | nu   |   24 | 123456 |
		+------+---------+------+------+--------+
		2 rows in set (0.00 sec)

方式二:逻辑运算符的使用:
	and(&&) or(||) not(!)
	between and;
	案例:
		mysql> select * from blackp where salary<222222 and id!=1;
		+------+----------+------+------+--------+
		| id   | name     | sex  | age  | salary |
		+------+----------+------+------+--------+
		|    2 | prudence | na   |   19 | 110000 |
		|    3 | supreme  | nu   |   24 | 123456 |
		+------+----------+------+------+--------+
		2 rows in set (0.00 sec)
		mysql> select * from blackp where salary>222222 or id!=1;
		+------+----------+------+------+--------+
		| id   | name     | sex  | age  | salary |
		+------+----------+------+------+--------+
		|    1 | tyz      | nu   |   18 | 444441 |
		|    2 | prudence | na   |   19 | 110000 |
		|    3 | supreme  | nu   |   24 | 123456 |
		+------+----------+------+------+--------+
		3 rows in set (0.00 sec)

方式三:模糊查询:...like
	关键字:in
		语法:select * from TABLE where Column in(匹配的条件);
		mysql> select * from emp where sal in(800,1500,3000);
		+-------+--------+------+------+------------+---------+------+--------+
		| EMPNO | ENAME  | JOB  | MGR  | HIREdate   | SAL     | COMM | DEPTNO |
		+-------+--------+------+------+------------+---------+------+--------+
		|     1 | 孙悟空 | 销售 |    4 | 1980-12-17 |  800.00 | NULL |      1 |
		|     9 | 白骨精 | 人事 |    8 | 1981-09-08 | 1500.00 | 0.00 |      2 |
		|    11 | 黑熊怪 | 市场 |    8 | 1981-12-03 | 3000.00 | NULL |      2 |
		+-------+--------+------+------+------------+---------+------+--------+
		3 rows in set (0.01 sec)
去重查询:select distinct *from tables;

十三、查看/更改字符类型:character

其一:查看database(数据库的字符类型)
	语法:show create database 库名;
	案例:
		mysql> show create database hero;
		+----------+-----------------------------------------------------------------+
		| Database | Create Database                                                 |
		+----------+-----------------------------------------------------------------+
		| hero     | CREATE DATABASE `hero` /*!40100 DEFAULT CHARACTER SET latin1 */ |
		+----------+-----------------------------------------------------------------+
		1 row in set (0.00 sec)
其二:查看table(表的字符类型)
	语法:show create table 表名;
	案例:
		mysql> show create table black;
		+-------+------------------------------------------------------------------+
		| Table | Create Table                                                                                                                                                                                                                 |
		+-------+------------------------------------------------------------------+
		| black | CREATE TABLE `black` (
		`id` int(11) DEFAULT NULL,
		`name` varchar(10) DEFAULT NULL,
		`sex` char(2) DEFAULT NULL,
		`age` int(11) DEFAULT NULL,
		`salary` bigint(10) DEFAULT NULL
		) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
		+-------+------------------------------------------------------------------+
		1 row in set (0.01 sec)

其三(更改):
	语法:alter table 表名 character set=utf8;
	案例:
		mysql> alter table commodity character set=utf8;
		Query OK, 0 rows affected (0.04 sec)
		Records: 0  Duplicates: 0  Warnings: 0

		mysql> show create table commodity;
		+-----------+------------------------------------------------------------------+
		| Table     | Create Table                                                                                                                                              |
		+-----------+------------------------------------------------------------------+
		| commodity | CREATE TABLE `commodity` (
		`id` int(2) DEFAULT NULL,
		`name` varchar(8) DEFAULT NULL,
		`age` int(2) DEFAULT NULL
		) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
		+-----------+------------------------------------------------------------------+
		1 row in set (0.01 sec)

十四、查看字符:show variables like “%char%”;

语法:show variables like "%char%";
	mysql> show variables like "%char%";
	+--------------------------+---------------------------------------------------------+
	| Variable_name            | Value                                                   |
	+--------------------------+---------------------------------------------------------+
	| character_set_client     | gbk                                                     |
	| character_set_connection | gbk                                                     |
	| character_set_database   | utf8                                                    |
	| character_set_filesystem | binary                                                  |
	| character_set_results    | gbk                                                     |
	| character_set_server     | latin1                                                  |
	| character_set_system     | utf8                                                    |
	| character_sets_dir       | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ |
	+--------------------------+---------------------------------------------------------+
	8 rows in set, 1 warning (0.00 sec)

十五、函数(单行):

其一:字符函数:
	连接函数:concat()
		案例:
		mysql> select concat(upper(name),lower(sex)) as "姓名+性别" from attempt;
		+--------------------+
		| 姓名+性别          |
		+--------------------+
		| 刮痧女性           |
		| 啥也不是男性       |
		| 次奥司男孩         |
		| 老树女孩           |
		| 枯藤女孩           |
		| 昏鸦女孩           |
		| 刮刮乐女性         |
		| 炊烟袅袅兮女性     |
		| 一闪一闪的光女孩   |
		| 大雨之中的告白男性 |
		| 悄悄女孩           |
		| 烂柯人男性         |
		| TYZfemale          |
		+--------------------+
		13 rows in set (0.00 sec)

	截取字符函数:substr()
		substr(str,number)【截取number后的字符】
		substr(str,number1,number2)【截取1—2中间的字符】
			案列:
			mysql> select *,substr(name,1,2) from attempt;
			+------+----------------+------+--------------+---------------------+------+------------------+
			| id   | name           | age  | month_salary | now_timestamp       | sex  | substr(name,1,2) |
			+------+----------------+------+--------------+---------------------+------+------------------+
			|    1 | 刮痧           |    9 |         1000 | 2021-03-27 17:40:42 | 女性 | 刮痧             |
			|    2 | 啥也不是       |    9 |         1000 | 2021-03-27 17:37:40 | 男性 | 啥也             |
			|    3 | 次奥司         |    9 |         1000 | 2021-03-27 17:27:21 | 男孩 | 次奥             |
			|    7 | 老树           |   10 |         1300 | 2021-03-27 18:48:09 | 女孩 | 老树             |
			|    5 | 枯藤           |   10 |         1300 | 2021-03-27 17:31:00 | 女孩 | 枯藤             |
			|    6 | 昏鸦           |   10 |         1300 | 2021-03-27 17:31:00 | 女孩 | 昏鸦             |
			|    4 | 刮刮乐         |   11 |         1900 | 2021-03-27 18:48:41 | 女性 | 刮刮             |
			|   10 | 炊烟袅袅兮     |   17 |         1400 | 2021-03-27 17:40:42 | 女性 | 炊烟             |
			|    9 | 一闪一闪的光   |   14 |         1600 | 2021-03-27 17:34:06 | 女孩 | 一闪             |
			|    8 | 大雨之中的告白 |   13 |         1500 | 2021-03-27 17:37:40 | 男性 | 大雨             |
			|   12 | 悄悄           |   15 |         1200 | 2021-03-27 17:34:06 | 女孩 | 悄悄             |
			|   11 | 烂柯人         |   19 |         NULL | 2021-03-27 17:37:40 | 男性 | 烂柯             |
			+------+----------------+------+--------------+---------------------+------+------------------+
			12 rows in set (0.00 sec)

	向上取整:ceil()
		mysql> select ceil(1.0002) as 'As';
		+----+
		| As |
		+----+
		|  2 |
		+----+
		1 row in set (0.03 sec)

	向下取整:
		mysql> select floor(1.904) as 'outPut';
		+--------+
		| outPut |
		+--------+
		|      1 |
		+--------+
		1 row in set (0.00 sec)

	获得字符的长度length();
		mysql> select length("tyzprudence") as outPut;
		+--------+
		| outPut |
		+--------+
		|     11 |
		+--------+
		1 row in set (0.00 sec)

	截断truncate();
		mysql> select truncate(1.9902,3) as Output;
		+--------+
		| Output |
		+--------+
		|  1.990 |
		+--------+
		1 row in set (0.00 sec)

其二:数字函数:
	(四舍五入)函数round();
		mysql> select round(10.2023,2);
		+------------------+
		| round(10.2023,2) |
		+------------------+
		|            10.20 |
		+------------------+
		1 row in set (0.00 sec)

	函数取余:mod();
		mysql> #select mod(10,3) as outPut;
		mysql> #select 10%3;
		mysql> select mod(10,3) as outPut;
		+--------+
		| outPut |
		+--------+
		|      1 |
		+--------+
		1 row in set (0.00 sec)

		mysql> select 10%3;
		+------+
		| 10%3 |
		+------+
		|    1 |
		+------+
		1 row in set (0.00 sec)

其三:日期函数:
	(返回当前系统日期+时间)函数NOW();
		mysql> select now();
		+---------------------+
		| now()               |
		+---------------------+
		| 2021-03-28 10:53:28 |
		+---------------------+
		1 row in set (0.00 sec)

	返回日期:
		mysql> select curdate();
		+------------+
		| curdate()  |
		+------------+
		| 2021-03-28 |
		+------------+
		1 row in set (0.03 sec)
	返回时间:
		mysql> select curtime();
		+-----------+
		| curtime() |
		+-----------+
		| 10:55:38  |
		+-----------+
		1 row in set (0.00 sec)

	获得时间:year,month,day,hour,minute,second
	获得月份:
		mysql> select month(now()) as 月;
		+------+
		| 月   |
		+------+
		|    3 |
		+------+
		1 row in set (0.00 sec)

		mysql> select monthname(now());
		+------------------+
		| monthname(now()) |
		+------------------+
		| March            |
		+------------------+
		1 row in set (0.03 sec)
		显示当前时间:
		mysql> select now();
		+---------------------+
		| now()               |
		+---------------------+
		| 2021-04-17 17:58:55 |
		+---------------------+
		1 row in set (0.00 sec)

其四:将字符通过指定的格式换成日期:
	mysql> select str_to_date("03-26-2020","%m-%d-%y") as outPut;
	+------------+
	| outPut     |
	+------------+
	| 2020-03-26 |
	+------------+
	1 row in set, 1 warning (0.03 sec)

流程控制函数:
if函数
	语法:if(条件判断,'true','false')
		mysql> select *,length(name) as 姓名,if(id is not null,"id有值","id无值") from attempt order by id; 
		+------+----------------+------+--------------+---------------------+--------+------+--------------------------------------+
		| id   | name           | age  | month_salary | now_timestamp       | sex    | 姓名 | if(id is not null,"id有值","id无值") |
		+------+----------------+------+--------------+---------------------+--------+------+--------------------------------------+
		| NULL | name			|   18 |         NULL | 2021-04-03 22:41:11 | FEMALE |    3 |   id无值                             |
		|    1 | 刮痧           |    9 |         1000 | 2021-03-27 17:40:42 | 女性   |    6 | id有值                               |
		|    2 | 啥也不是       |    9 |         1000 | 2021-03-27 17:37:40 | 男性   |   12 | id有值                               |
		|    3 | 次奥司         |    9 |         1000 | 2021-03-27 17:27:21 | 男孩   |    9 | id有值                               |
		|    4 | 刮刮乐         |   11 |         1900 | 2021-03-27 18:48:41 | 女性   |    9 | id有值                               |
		|    5 | 枯藤           |   10 |         1300 | 2021-03-27 17:31:00 | 女孩   |    6 | id有值                               |
		|    6 | 昏鸦           |   10 |         1300 | 2021-03-27 17:31:00 | 女孩   |    6 | id有值                               |
		|    7 | 老树           |   10 |         1300 | 2021-03-27 18:48:09 | 女孩   |    6 | id有值                               |
		|    8 | 大雨之中的告白 |   13 |         1500 | 2021-03-27 17:37:40 | 男性   |   21 | id有值                               |
		|    9 | 一闪一闪的光   |   14 |         1600 | 2021-03-27 17:34:06 | 女孩   |   18 | id有值                               |
		|   10 | 炊烟袅袅兮     |   17 |         1400 | 2021-03-27 17:40:42 | 女性   |   15 | id有值                               |
		|   11 | 烂柯人         |   19 |         NULL | 2021-03-27 17:37:40 | 男性   |    9 | id有值                               |
		|   12 | 悄悄           |   15 |         1200 | 2021-04-12 08:18:46 | 男性   |    6 | id有值                               |
		|   14 | 张三           |   12 |         NULL | 2021-04-03 22:38:14 | NULL   |    6 | id有值                               |
		+------+----------------+------+--------------+---------------------+--------+------+--------------------------------------+
		14 rows in set (0.00 sec)
case函数:
	(1)mysql> select*,month_salary as 原工资,
		-> case age 
		-> when 9 then month_salary*2
		-> when 10 then month_salary*3
		-> when 11 then month_salary*4
		-> else month_salary
		-> end as 新工资
		-> from attempt
		-> order by id asc;
	+------+----------------+--------+------+--------------+---------------------+--------+--------+
	| id   | name           | sex    | age  | month_salary | now_timestamp       | 原工资 | 新工资 |
	+------+----------------+--------+------+--------------+---------------------+--------+--------+
	| NULL | name			| FEMALE |   18 |         NULL | 2021-04-03 22:41:11 |   NULL |   NULL |
	|    1 | 刮痧           | 女性   |    9 |         1000 | 2021-03-27 17:40:42 |   1000 |   2000 |
	|    2 | 啥也不是       | 男性   |    9 |         1000 | 2021-03-27 17:37:40 |   1000 |   2000 |
	|    3 | 次奥司         | 男孩   |    9 |         1000 | 2021-03-27 17:27:21 |   1000 |   2000 |
	|    4 | 刮刮乐         | 女性   |   11 |         1900 | 2021-03-27 18:48:41 |   1900 |   7600 |
	|    5 | 枯藤           | 女孩   |   10 |         1300 | 2021-03-27 17:31:00 |   1300 |   3900 |
	|    6 | 昏鸦           | 女孩   |   10 |         1300 | 2021-03-27 17:31:00 |   1300 |   3900 |
	|    7 | 老树           | 女孩   |   10 |         1300 | 2021-03-27 18:48:09 |   1300 |   3900 |
	|    8 | 大雨之中的告白 | 男性   |   13 |         1500 | 2021-03-27 17:37:40 |   1500 |   1500 |
	|    9 | 一闪一闪的光   | 女孩   |   14 |         1600 | 2021-03-27 17:34:06 |   1600 |   1600 |
	|   10 | 炊烟袅袅兮     | 女性   |   17 |         1400 | 2021-03-27 17:40:42 |   1400 |   1400 |
	|   11 | 烂柯人         | 男性   |   19 |         NULL | 2021-03-27 17:37:40 |   NULL |   NULL |
	|   12 | 悄悄           | 男性   |   15 |         1200 | 2021-04-12 08:18:46 |   1200 |   1200 |
	|   14 | 张三           | NULL   |   12 |         NULL | 2021-04-03 22:38:14 |   NULL |   NULL |
	+------+----------------+--------+------+--------------+---------------------+--------+--------+
	14 rows in set (0.00 sec)		

	(2)mysql> select *,     
		-> case
		-> when month_salary>1001 then 'A'
		-> when month_salary>1301 then 'B'
		-> when month_salary>1401 then 'C'
		-> when month_salary>1500 then 'D'
		-> else 'S'
		-> end as 工资级别
		-> from attempt order by id asc;
	+------+----------------+--------+------+--------------+---------------------+----------+
	| id   | name           | sex    | age  | month_salary | now_timestamp       | 工资级别 |
	+------+----------------+--------+------+--------------+---------------------+----------+
	| NULL | name 			| FEMALE |   18 |         NULL | 2021-04-03 22:41:11 | S        |
	|    1 | 刮痧           | 女性   |    9 |         1000 | 2021-03-27 17:40:42 | S        |
	|    2 | 啥也不是       | 男性   |    9 |         1000 | 2021-03-27 17:37:40 | S        |
	|    3 | 次奥司         | 男孩   |    9 |         1000 | 2021-03-27 17:27:21 | S        |
	|    4 | 刮刮乐         | 女性   |   11 |         1900 | 2021-03-27 18:48:41 | A        |
	|    5 | 枯藤           | 女孩   |   10 |         1300 | 2021-03-27 17:31:00 | A        |
	|    6 | 昏鸦           | 女孩   |   10 |         1300 | 2021-03-27 17:31:00 | A        |
	|    7 | 老树           | 女孩   |   10 |         1300 | 2021-03-27 18:48:09 | A        |
	|    8 | 大雨之中的告白 | 男性   |   13 |         1500 | 2021-03-27 17:37:40 | A        |
	|    9 | 一闪一闪的光   | 女孩   |   14 |         1600 | 2021-03-27 17:34:06 | A        |
	|   10 | 炊烟袅袅兮     | 女性   |   17 |         1400 | 2021-03-27 17:40:42 | A        |
	|   11 | 烂柯人         | 男性   |   19 |         NULL | 2021-03-27 17:37:40 | S        |
	|   12 | 悄悄           | 男性   |   15 |         1200 | 2021-04-12 08:18:46 | A        |
	|   14 | 张三           | NULL   |   12 |         NULL | 2021-04-03 22:38:14 | S        |
	+------+----------------+--------+------+--------------+---------------------+----------+
	14 rows in set (0.03 sec)

十六、分组函数:

总结:
select表名
from表名
where普通字段名条件
GROUP BY
HAVING 
ORDER BY 
limit  
group by:将每个字段的相同的放在同一组;
	mysql> select avg(sal) from emp group by deptno;
	+-------------+
	| avg(sal)    |
	+-------------+
	| 2325.000000 |
	| 1816.666667 |
	| 2183.333333 |
	+-------------+
	3 rows in set (0.03 sec)
having:(聚合函数)
	mysql> select ename from emp having avg(sal)>2000;
	+--------+
	| ename  |
	+--------+
	| 孙悟空 |
	+--------+
	1 row in set (0.00 sec)
sum:
	mysql> select sum(month_salary) from attempt;
	+-------------------+
	| sum(month_salary) |  
	+-------------------+  
	|             14500 |  
	+-------------------+  
	1 row in set (0.03 sec)
avg:
	mysql> select avg(month_salary) as 工资平均值 from attempt;
	+------------+
	| 工资平均值 |
	+------------+
	|  1318.1818 |
	+------------+
	1 row in set (0.00 sec)
max:
	mysql> select max(month_salary) as 工资最大值 from attempt;
	+------------+
	| 工资最大值 |
	+------------+
	|       1900 |
	+------------+
	1 row in set (0.00 sec)
min:
	mysql> select min(month_salary) as 工资最小值 from attempt;
	+------------+
	| 工资最小值 |
	+------------+
	|       1000 |
	+------------+
	1 row in set (0.00 sec)
count:
	mysql> select count(month_salary) as 计数 from attempt;
	+------+
	| 计数 |
	+------+
	|   11 |
	+------+
	1 row in set (0.00 sec)
sqrt()求根:
	mysql> select sqrt(27+3*3);
	+--------------+
	| sqrt(27+3*3) |
	+--------------+
	|            6 |
	+--------------+
	1 row in set (0.03 sec)
sum,avg:(处理数值型,忽略null值)
max,min,count:(处理任何类型,忽略null值)
	mysql> select
		-> sum(month_salary) as 工资和,
		-> avg(month_salary) as 工资平均值,
		-> max(month_salary) as 工资最大自,
		-> min(month_salary) as 工资最小值,
		-> count(month_salary) as 计数 
		-> from attempt order by id asc;
	+--------+------------+------------+------------+------+
	| 工资和 | 工资平均值 | 工资最大自 | 工资最小值 | 计数 |
	+--------+------------+------------+------------+------+
	|  14500 |  1318.1818 |       1900 |       1000 |   11 |
	+--------+------------+------------+------------+------+
	1 row in set (0.00 sec)

十七、查看MySQL默认存储引擎:

语法:show variables like "default_storage_engine";
	mysql> show variables like "default_storage_engine";
	+------------------------+--------+
	| Variable_name          | Value  |
	+------------------------+--------+
	| default_storage_engine | InnoDB |
	+------------------------+--------+
	1 row in set, 1 warning (0.04 sec)

十八、常见数据类型:

	mysql> create table newtable(
		-> id int(11),
		-> name varchar(30),
		-> age tinyint(4),
		-> score float(4,1),
		-> sex enum('w','m'),
		-> hobby set('football','basketball','volleyball'),
		-> photo varbinary(255),
		-> spend decimal(5,1),
		-> address json,
		-> intro text) character set=utf8;
	Query OK, 0 rows affected (0.04 sec)

	mysql> select * from newtable;
	Empty set (0.01 sec)

	mysql> desc newtable;
	+---------+-------------------------------------------+------+-----+---------+-------+
	| Field   | Type                                      | Null | Key | Default | Extra |
	+---------+-------------------------------------------+------+-----+---------+-------+
	| id      | int(11)                                   | YES  |     | NULL    |       |
	| name    | varchar(30)                               | YES  |     | NULL    |       |
	| age     | tinyint(4)                                | YES  |     | NULL    |       |
	| score   | float(4,1)                                | YES  |     | NULL    |       |
	| sex     | enum('w','m')                             | YES  |     | NULL    |       |
	| hobby   | set('football','basketball','volleyball') | YES  |     | NULL    |       |
	| photo   | varbinary(255)                            | YES  |     | NULL    |       |
	| spend   | decimal(5,1)                              | YES  |     | NULL    |       |
	| address | json                                      | YES  |     | NULL    |       |
	| intro   | text                                      | YES  |     | NULL    |       |
	+---------+-------------------------------------------+------+-----+---------+-------+
	10 rows in set (0.00 sec)

十九、查看MySQL中的字符集:

语法:show character set;
	mysql> show character set;
	+----------+---------------------------------+---------------------+--------+
	| Charset  | Description                     | Default collation   | Maxlen |
	+----------+---------------------------------+---------------------+--------+
	| big5     | Big5 Traditional Chinese        | big5_chinese_ci     |      2 |
	| dec8     | DEC West European               | dec8_swedish_ci     |      1 |
	| cp850    | DOS West European               | cp850_general_ci    |      1 |
	| hp8      | HP West European                | hp8_english_ci      |      1 |
	| koi8r    | KOI8-R Relcom Russian           | koi8r_general_ci    |      1 |
	| latin1   | cp1252 West European            | latin1_swedish_ci   |      1 |
	| latin2   | ISO 8859-2 Central European     | latin2_general_ci   |      1 |
	| swe7     | 7bit Swedish                    | swe7_swedish_ci     |      1 |
	| ascii    | US ASCII                        | ascii_general_ci    |      1 |
	| ujis     | EUC-JP Japanese                 | ujis_japanese_ci    |      3 |
	| sjis     | Shift-JIS Japanese              | sjis_japanese_ci    |      2 |
	| hebrew   | ISO 8859-8 Hebrew               | hebrew_general_ci   |      1 |
	| tis620   | TIS620 Thai                     | tis620_thai_ci      |      1 |
	| euckr    | EUC-KR Korean                   | euckr_korean_ci     |      2 |
	| koi8u    | KOI8-U Ukrainian                | koi8u_general_ci    |      1 |
	| gb2312   | GB2312 Simplified Chinese       | gb2312_chinese_ci   |      2 |
	| greek    | ISO 8859-7 Greek                | greek_general_ci    |      1 |
	| cp1250   | Windows Central European        | cp1250_general_ci   |      1 |
	| gbk      | GBK Simplified Chinese          | gbk_chinese_ci      |      2 |
	| latin5   | ISO 8859-9 Turkish              | latin5_turkish_ci   |      1 |
	| armscii8 | ARMSCII-8 Armenian              | armscii8_general_ci |      1 |
	| utf8     | UTF-8 Unicode                   | utf8_general_ci     |      3 |
	| ucs2     | UCS-2 Unicode                   | ucs2_general_ci     |      2 |
	| cp866    | DOS Russian                     | cp866_general_ci    |      1 |
	| keybcs2  | DOS Kamenicky Czech-Slovak      | keybcs2_general_ci  |      1 |
	| macce    | Mac Central European            | macce_general_ci    |      1 |
	| macroman | Mac West European               | macroman_general_ci |      1 |
	| cp852    | DOS Central European            | cp852_general_ci    |      1 |
	| latin7   | ISO 8859-13 Baltic              | latin7_general_ci   |      1 |
	| utf8mb4  | UTF-8 Unicode                   | utf8mb4_general_ci  |      4 |
	| cp1251   | Windows Cyrillic                | cp1251_general_ci   |      1 |
	| utf16    | UTF-16 Unicode                  | utf16_general_ci    |      4 |
	| utf16le  | UTF-16LE Unicode                | utf16le_general_ci  |      4 |
	| cp1256   | Windows Arabic                  | cp1256_general_ci   |      1 |
	| cp1257   | Windows Baltic                  | cp1257_general_ci   |      1 |
	| utf32    | UTF-32 Unicode                  | utf32_general_ci    |      4 |
	| binary   | Binary pseudo charset           | binary              |      1 |
	| geostd8  | GEOSTD8 Georgian                | geostd8_general_ci  |      1 |
	| cp932    | SJIS for Windows Japanese       | cp932_japanese_ci   |      2 |
	| eucjpms  | UJIS for Windows Japanese       | eucjpms_japanese_ci |      3 |
	| gb18030  | China National Standard GB18030 | gb18030_chinese_ci  |      4 |
	+----------+---------------------------------+---------------------+--------+
	41 rows in set (0.03 sec)

二十、查看GBK字符集对应的排序规则:

语法:show collation like "gbk%";
	mysql> show collation like "gbk%"; 
	+----------------+---------+----+---------+----------+---------+
	| Collation      | Charset | Id | Default | Compiled | Sortlen |
	+----------------+---------+----+---------+----------+---------+
	| gbk_chinese_ci | gbk     | 28 | Yes     | Yes      |       1 |
	| gbk_bin        | gbk     | 87 |         | Yes      |       1 |
	+----------------+---------+----+---------+----------+---------+
	2 rows in set (0.02 sec)

二十一、select database()

查看已use的数据库
mysql> select database();
+------------+
| database() |
+------------+
| emp		 |
+------------+
1 row in set (0.00 sec)

二十二、主键约束:

unique(唯一+非空); not null; primary key auto_increment(不插入值时,系统自增);default:默认约束:
primary key:主键
auto_increment:自增必须钱有主键

mysql> create table t2( id int primary key auto_increment,name varchar(10)); 
Query OK, 0 rows affected (0.04 sec)
mysql> desc t2;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> create table newdb(
	-> id int(9) primary key auto_increment,
	-> name varchar(10) not null,
	-> phone varchar(30) unique key);
Query OK, 0 rows affected (0.05 sec)

mysql> desc newdb;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(9)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10) | NO   |     | NULL    |                |
| phone | varchar(30) | YES  | UNI | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

默认约束:default:用于设定某列的默认值
	mysql> create table newty(
		-> id int(10) primary key auto_increment,
		-> name varchar(10) not null,
		-> phone varchar(30) unique key,
		-> geder char(1) default 'A');
	Query OK, 0 rows affected (0.05 sec)

	mysql> desc newty;
	+-------+-------------+------+-----+---------+----------------+
	| Field | Type        | Null | Key | Default | Extra          |
	+-------+-------------+------+-----+---------+----------------+
	| id    | int(10)     | NO   | PRI | NULL    | auto_increment |
	| name  | varchar(10) | NO   |     | NULL    |                |
	| phone | varchar(30) | YES  | UNI | NULL    |                |
	| geder | char(1)     | YES  |     | A       |                |
	+-------+-------------+------+-----+---------+----------------+
	4 rows in set (0.01 sec)	

mysql> create table t3(
	-> id int(3) auto_increment);
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
mysql> create table t3(
	-> id int(3) primary key auto_increment); 
Query OK, 0 rows affected (0.05 sec)
mysql> create table t3(id int primary key auto_increment,name varchar(10) not null,address char(10))charset=utf8;
Query OK, 0 rows affected (0.05 sec)

mysql> desc t3;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> desc t3;    
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | varchar(10) | NO   |     | NULL    |                |
| address | char(10)    | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

二十三、查看当前服务器使用的字符集:

show variables like ‘character_set_server’;

mysql> show variables like 'character_set_server';
+----------------------+--------+
| Variable_name        | Value  |
+----------------------+--------+
| character_set_server | latin1 |
+----------------------+--------+
1 row in set, 1 warning (0.00 sec)

二十四、查看存储引擎:show engines;


语法:show engines;
	mysql> show engines;
	+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
	| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
	+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
	| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
	| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
	| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
	| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
	| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
	| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
	| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
	| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
	| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
	+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
	9 rows in set (0.01 sec)

二十五、改变数据:update:

语法:update userTable set 变量 where 变量;
	mysql> update tyz set sex='w' where ids=3;
	Query OK, 1 row affected (0.00 sec)
	Rows matched: 1  Changed: 1  Warnings: 0

	mysql> select*from tyz;
	+------+----------+------+---------------------+------------------+
	| ids  | name     | sex  | setTime             | address          |
	+------+----------+------+---------------------+------------------+
	|    1 | orele    | 男性  | 2021-04-19 00:11:11 | 湖南****大学	  |
	|    2 | yongzhou | m    | 2021-04-19 00:11:11 | 湖南岳阳          |
	|    3 | t13z     | w    | 2021-04-19 07:56:03 | NULL             |
	+------+----------+------+---------------------+------------------+

二十六、导入文件:source

语法:source path;
	mysql> source C:/Users/admin/Desktop/LEARNING/emp.sql;
	Query OK, 0 rows affected (0.00 sec)

	Query OK, 0 rows affected, 1 warning (0.00 sec)

	Query OK, 0 rows affected (0.00 sec)
	......
	Query OK, 0 rows affected (0.00 sec)

	mysql> show databases;
	+--------------------+
	| Database           |
	+--------------------+
	| information_schema |
	| hero               |
	| mysql              |
	| newdb3             |
	| performance_schema |
	| sys                |
	| emp				 |
	+--------------------+
	7 rows in set (0.00 sec)

二十七、改变字符类型:set names utf8/gbk;

mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> set names gbk;
Query OK, 0 rows affected (0.00 sec)

二十八、limit

limit 参数:begin:开始执行位置,默认为0;size:查询个数
mysql> select*from emp order by sal limit 0,3;
+-------+--------+------+------+------------+---------+--------+--------+
| EMPNO | ENAME  | JOB  | MGR  | HIREdate   | SAL     | COMM   | DEPTNO |
+-------+--------+------+------+------------+---------+--------+--------+
|     1 | 孙悟空 | 销售 |    4 | 1980-12-17 |  800.00 |   NULL |      1 |
|    10 | 蜘蛛精 | 人事 |    8 | 1981-12-03 |  950.00 |   NULL |      2 |
|     3 | 沙僧   | 销售 |    4 | 1981-02-22 | 1250.00 | 500.00 |      1 |
+-------+--------+------+------+------------+---------+--------+--------+
3 rows in set (0.00 sec)

二十九、嵌套查询(子查询):

mysql> select * from emp where sal>2325;
+-------+--------+----------+------+------------+---------+------+--------+
| EMPNO | ENAME  | JOB      | MGR  | HIREdate   | SAL     | COMM | DEPTNO |
+-------+--------+----------+------+------------+---------+------+--------+
|     4 | 唐僧   | 销售经理 |    8 | 1981-04-02 | 2975.00 | NULL |      1 |
|     6 | 关羽   | 程序员   |    5 | 1981-05-01 | 2850.00 | NULL |      3 |
|     7 | 张飞   | 程序员   |    5 | 1981-06-09 | 2450.00 | NULL |      3 |
|     8 | 观音   | CEO      | NULL | 1981-11-17 | 5000.00 | NULL |      1 |
|    11 | 黑熊怪 | 市场     |    8 | 1981-12-03 | 3000.00 | NULL |      2 |
+-------+--------+----------+------+------------+---------+------+--------+
5 rows in set (0.00 sec)

mysql> select * from emp where sal>(select avg(sal) from emp where deptno=1);
+-------+--------+----------+------+------------+---------+------+--------+
| EMPNO | ENAME  | JOB      | MGR  | HIREdate   | SAL     | COMM | DEPTNO |
+-------+--------+----------+------+------------+---------+------+--------+
|     4 | 唐僧   | 销售经理 |    8 | 1981-04-02 | 2975.00 | NULL |      1 |
|     6 | 关羽   | 程序员   |    5 | 1981-05-01 | 2850.00 | NULL |      3 |
|     7 | 张飞   | 程序员   |    5 | 1981-06-09 | 2450.00 | NULL |      3 |
|     8 | 观音   | CEO      | NULL | 1981-11-17 | 5000.00 | NULL |      1 |
|    11 | 黑熊怪 | 市场     |    8 | 1981-12-03 | 3000.00 | NULL |      2 |
+-------+--------+----------+------+------------+---------+------+--------+
5 rows in set (0.01 sec)

三十、去重:distinct

mysql> select distinct deptno from emp;

三十一、迪卡耳机错误:

mysql> select * from emp inner join dept;
	+-------+--------+----------+------+------------+---------+---------+--------+--------+--------+--------+
	| EMPNO | ENAME  | JOB      | MGR  | HIREdate   | SAL     | COMM    | DEPTNO | DEPTNO | DNAME  | LOC    |
	+-------+--------+----------+------+------------+---------+---------+--------+--------+--------+--------+
	|     1 | 孙悟空 | 销售     |    4 | 1980-12-17 |  800.00 |    NULL |      1 |      1 | 神仙   | 天庭   |
	|     1 | 孙悟空 | 销售     |    4 | 1980-12-17 |  800.00 |    NULL |      1 |      2 | 妖怪   | 盘丝洞 |
	|     1 | 孙悟空 | 销售     |    4 | 1980-12-17 |  800.00 |    NULL |      1 |      3 | 普通人 | 北京   |
	|     1 | 孙悟空 | 销售     |    4 | 1980-12-17 |  800.00 |    NULL |      1 |      4 | 赛亚人 | 外星球 |
	|     2 | 猪八戒 | 销售     |    4 | 1981-02-20 | 1600.00 |  300.00 |      1 |      1 | 神仙   | 天庭   |
	|     2 | 猪八戒 | 销售     |    4 | 1981-02-20 | 1600.00 |  300.00 |      1 |      2 | 妖怪   | 盘丝洞 |
	|     2 | 猪八戒 | 销售     |    4 | 1981-02-20 | 1600.00 |  300.00 |      1 |      3 | 普通人 | 北京   |
	|     2 | 猪八戒 | 销售     |    4 | 1981-02-20 | 1600.00 |  300.00 |      1 |      4 | 赛亚人 | 外星球 |
	|     3 | 沙僧   | 销售     |    4 | 1981-02-22 | 1250.00 |  500.00 |      1 |      1 | 神仙   | 天庭   |
	|     3 | 沙僧   | 销售     |    4 | 1981-02-22 | 1250.00 |  500.00 |      1 |      2 | 妖怪   | 盘丝洞 |
	|     3 | 沙僧   | 销售     |    4 | 1981-02-22 | 1250.00 |  500.00 |      1 |      3 | 普通人 | 北京   |
	|     3 | 沙僧   | 销售     |    4 | 1981-02-22 | 1250.00 |  500.00 |      1 |      4 | 赛亚人 | 外星球 |
	|     4 | 唐僧   | 销售经理 |    8 | 1981-04-02 | 2975.00 |    NULL |      1 |      1 | 神仙   | 天庭   |
	|     4 | 唐僧   | 销售经理 |    8 | 1981-04-02 | 2975.00 |    NULL |      1 |      2 | 妖怪   | 盘丝洞 |
	|     4 | 唐僧   | 销售经理 |    8 | 1981-04-02 | 2975.00 |    NULL |      1 |      3 | 普通人 | 北京   |
	|     4 | 唐僧   | 销售经理 |    8 | 1981-04-02 | 2975.00 |    NULL |      1 |      4 | 赛亚人 | 外星球 |
	|     5 | 刘备   | 项目经理 | NULL | 1981-09-28 | 1250.00 | 1400.00 |      3 |      1 | 神仙   | 天庭   |
	|     5 | 刘备   | 项目经理 | NULL | 1981-09-28 | 1250.00 | 1400.00 |      3 |      2 | 妖怪   | 盘丝洞 |
	|     5 | 刘备   | 项目经理 | NULL | 1981-09-28 | 1250.00 | 1400.00 |      3 |      3 | 普通人 | 北京   |
	|     5 | 刘备   | 项目经理 | NULL | 1981-09-28 | 1250.00 | 1400.00 |      3 |      4 | 赛亚人 | 外星球 |
	|     6 | 关羽   | 程序员   |    5 | 1981-05-01 | 2850.00 |    NULL |      3 |      1 | 神仙   | 天庭   |
	|     6 | 关羽   | 程序员   |    5 | 1981-05-01 | 2850.00 |    NULL |      3 |      2 | 妖怪   | 盘丝洞 |
	|     6 | 关羽   | 程序员   |    5 | 1981-05-01 | 2850.00 |    NULL |      3 |      3 | 普通人 | 北京   |
	|     6 | 关羽   | 程序员   |    5 | 1981-05-01 | 2850.00 |    NULL |      3 |      4 | 赛亚人 | 外星球 |
	|     7 | 张飞   | 程序员   |    5 | 1981-06-09 | 2450.00 |    NULL |      3 |      1 | 神仙   | 天庭   |
	|     7 | 张飞   | 程序员   |    5 | 1981-06-09 | 2450.00 |    NULL |      3 |      2 | 妖怪   | 盘丝洞 |
	|     7 | 张飞   | 程序员   |    5 | 1981-06-09 | 2450.00 |    NULL |      3 |      3 | 普通人 | 北京   |
	|     7 | 张飞   | 程序员   |    5 | 1981-06-09 | 2450.00 |    NULL |      3 |      4 | 赛亚人 | 外星球 |
	|     8 | 观音   | CEO      | NULL | 1981-11-17 | 5000.00 |    NULL |      1 |      1 | 神仙   | 天庭   |
	|     8 | 观音   | CEO      | NULL | 1981-11-17 | 5000.00 |    NULL |      1 |      2 | 妖怪   | 盘丝洞 |
	|     8 | 观音   | CEO      | NULL | 1981-11-17 | 5000.00 |    NULL |      1 |      3 | 普通人 | 北京   |
	|     8 | 观音   | CEO      | NULL | 1981-11-17 | 5000.00 |    NULL |      1 |      4 | 赛亚人 | 外星球 |
	|     9 | 白骨精 | 人事     |    8 | 1981-09-08 | 1500.00 |    0.00 |      2 |      1 | 神仙   | 天庭   |
	|     9 | 白骨精 | 人事     |    8 | 1981-09-08 | 1500.00 |    0.00 |      2 |      2 | 妖怪   | 盘丝洞 |
	|     9 | 白骨精 | 人事     |    8 | 1981-09-08 | 1500.00 |    0.00 |      2 |      3 | 普通人 | 北京   |
	|     9 | 白骨精 | 人事     |    8 | 1981-09-08 | 1500.00 |    0.00 |      2 |      4 | 赛亚人 | 外星球 |
	|    10 | 蜘蛛精 | 人事     |    8 | 1981-12-03 |  950.00 |    NULL |      2 |      1 | 神仙   | 天庭   |
	|    10 | 蜘蛛精 | 人事     |    8 | 1981-12-03 |  950.00 |    NULL |      2 |      2 | 妖怪   | 盘丝洞 |
	|    10 | 蜘蛛精 | 人事     |    8 | 1981-12-03 |  950.00 |    NULL |      2 |      3 | 普通人 | 北京   |
	|    10 | 蜘蛛精 | 人事     |    8 | 1981-12-03 |  950.00 |    NULL |      2 |      4 | 赛亚人 | 外星球 |
	|    11 | 黑熊怪 | 市场     |    8 | 1981-12-03 | 3000.00 |    NULL |      2 |      1 | 神仙   | 天庭   |
	|    11 | 黑熊怪 | 市场     |    8 | 1981-12-03 | 3000.00 |    NULL |      2 |      2 | 妖怪   | 盘丝洞 |
	|    11 | 黑熊怪 | 市场     |    8 | 1981-12-03 | 3000.00 |    NULL |      2 |      3 | 普通人 | 北京   |
	|    11 | 黑熊怪 | 市场     |    8 | 1981-12-03 | 3000.00 |    NULL |      2 |      4 | 赛亚人 | 外星球 |
	+-------+--------+----------+------+------------+---------+---------+--------+--------+--------+--------+
	44 rows in set (0.04 sec)

三十二、外键查询:

alter table 表名 add constraint (外键名称) foreign key(外键列名称) REFERENCES 主表名称(主表列名称);
内连接:mysql> select e.ename,e.job from dept d join emp e on e.deptno=d.deptno where job="程序员";
+-------+--------+
| ename | job    |
+-------+--------+
| 关羽  | 程序员 |
| 张飞  | 程序员 |
+-------+--------+
2 rows in set (0.00 sec)
等值连接与内连接查询到的数据都是两张表的交集数据:

外连接:(一张表的全部和另外一张表的部分,一定会除少一次)
select 字段信息 from A left/right join B
on 关联条件
where 普通的条件;
外联查询:mysql> select dept.dname,emp.ename from emp left join dept on emp.deptno=dept.deptno;
	+--------+--------+
	| dname  | ename  |
	+--------+--------+
	| 神仙   | 孙悟空 |
	| 神仙   | 猪八戒 |
	| 神仙   | 沙僧   |
	| 神仙   | 唐僧   |
	| 普通人 | 刘备   |
	| 普通人 | 关羽   |
	| 普通人 | 张飞   |
	| 神仙   | 观音   |
	| 妖怪   | 白骨精 |
	| 妖怪   | 蜘蛛精 |
	| 妖怪   | 黑熊怪 |
	+--------+--------+
	11 rows in set (0.00 sec)
	如果查询数据时需要查询多张表的数据,此时需要用关联查询。
	如果查询两张表的交集数据时,使用等值连接或者内连接(首先内连接)。
	如果查询的是一张表的全部数据和另外一张表的交集数据时使用外连接。

三十三、select * from hero.attempt

语法:select * from DATABASE.TABLE;
mysql> select * from hero.attempt;
+------+----------------+--------+------+--------------+---------------------+
| id   | name           | sex    | age  | month_salary | now_timestamp       |
+------+----------------+--------+------+--------------+---------------------+
|    1 | 刮痧           | 女性   |    9 |         1000 | 2021-03-27 17:40:42 |
|    2 | 啥也不是       | 男性   |    9 |         1000 | 2021-03-27 17:37:40 |
|    3 | 次奥司         | 男孩   |    9 |         1000 | 2021-03-27 17:27:21 |
|    7 | 老树           | 女孩   |   10 |         1300 | 2021-03-27 18:48:09 |
|    5 | 枯藤           | 女孩   |   10 |         1300 | 2021-03-27 17:31:00 |
|    6 | 昏鸦           | 女孩   |   10 |         1300 | 2021-03-27 17:31:00 |
|    4 | 刮刮乐         | 女性   |   11 |         1900 | 2021-03-27 18:48:41 |
|   10 | 炊烟袅袅兮     | 女性   |   17 |         1400 | 2021-03-27 17:40:42 |
|    9 | 一闪一闪的光   | 女孩   |   14 |         1600 | 2021-03-27 17:34:06 |
|    8 | 大雨之中的告白 | 男性   |   13 |         1500 | 2021-03-27 17:37:40 |
|   12 | 悄悄           | 男性   |   15 |         1200 | 2021-04-12 08:18:46 |
|   11 | 烂柯人         | 男性   |   19 |         5201 | 2021-04-26 09:11:53 |
| NULL | t12           | FEMALE |   18 |         1231 | 2021-04-26 09:25:45 |
|   14 | 张三           | NULL   |   12 |         1231 | 2021-04-26 09:26:49 |
|   12 | 孙小空         | 男性   |  500 |        13123 | 2021-04-26 09:25:34 |
|   16 | 猪八戒         | NULL   | NULL |         NULL | 2021-04-26 09:24:35 |
|   17 | 杨戬           | 男性   | NULL |         NULL | 2021-04-26 09:25:00 |
|   21 | 4399           | NULL   |  112 |         1231 | 2021-04-26 09:25:30 |
+------+----------------+--------+------+--------------+---------------------+
18 rows in set (0.00 sec)

之后学习会再更新,,,Good Luck!

你可能感兴趣的:(mysql,mysql,数据库,sql)