Mysql basics-Teacher Han Shunping (based on mysql5.7 version)

1. MySQL database (based on mysql5.7 version)

Insert image description here

1. MYSQL installation and configuration (refer to video p730-)

Insert image description here
Insert image description here

2. Database

a. Three-tier structure of database

Insert image description here

b. SQL statement classification

Insert image description here

c. Create database

Insert image description here

#创建数据库db01
CREATE DATABASE IF NOT EXISTS db01;
#创建一个使用utf8字符集的db02数据库
CREATE DATABASE IF NOT EXISTS db02 CHARACTER SET utf8;
#创建一个使用utf8字符集的db02数据库并带校对规则
CREATE DATABASE IF NOT EXISTS db03 CHARACTER SET utf8 COLLATE utf8_bin; 

d. View and delete database

Insert image description here

#查看当前数据库服务器中所有的数据库
SHOW DATABASES; 
#查看前面创建的数据库定义信息
SHOW CREATE DATABASE db01

#一般来说,创建数据库或表时,名要用反引号引着“``”
CREATE DATABASE IF NOT EXISTS `db02`;

#删除数据库
DROP DATABASE db01

e. Database backup and recovery=

Insert image description here

  • Notice:
-- 1、备份数据库时,写上“ -B ”时,是包含表所在的数据库,恢复时不再需要手动创建数据库.
-- 2、若不写“ -B ”时,将不包含表所在的数据库,恢复时需要手动建数据库然后才能恢复欧克.

Insert image description here

  • If you only want to back up a certain table in a database
    Insert image description here

3. Table

a. Create table

Insert image description here

b. Create table exercises

Insert image description here

-- 创建员工emp表
CREATE TABLE IF NOT EXISTS emp(
	`id` INT,
	`name` VARCHAR(15),
	`sex` CHAR(1),
	`birthday` DATE,
	`entry_date` DATE,
	`jop` VARCHAR(30),
	`salary` DOUBLE,
	`resume` TEXT

)CHARSET utf8;

--添加数据
INSERT INTO `emp` VALUES(1,'tom','男','2023-01-27','2023-02-28','Java开发',8000.0,'继续加油,不要迷失自我');
-- look the data
SELECT *FROM `emp`

c. Modify table

i. Introduction

Insert image description here

ii. Application examples

Insert image description here
Insert image description here

d. Table copy

Insert image description here
Insert image description here
Insert image description here

i. Table skills
  • Use the like keyword to quickly create a table
create table tb01 like emp;-- emp时已经存在的表
  • Use table.* to display all columns of the specified table
SELECT t.*,e.deptno FROM emp e,dept t 
			WHERE e.deptno=t.deptno

4. MySQL data types

a. Introduction to commonly used data types

Insert image description here
Insert image description here

b. Basic use of data types (integers)

Insert image description here
Insert image description here

#创建t1表,类型为tinyint,默认不设置符号,能添加的数据:-128~127
CREATE TABLE t1(id TINYINT)
SELECT * FROM t1
INSERT INTO t1 VALUES(-128);
INSERT INTO t1 VALUES(127);
#创建t2表,类型为tinyint,设置符号,能添加的数据:0~255
CREATE TABLE t2(id TINYINT UNSIGNED);
INSERT INTO t2 VALUES(0);
INSERT INTO t2 VALUES(255);
SELECT *FROM t2;

c. Use of bit

Insert image description here
Insert image description here

d. Basic use of decimal point

Insert image description here

e. Basic use of strings

Insert image description here

i. Usage details one:

Insert image description here

ii. Details 2:

Insert image description here

iii. Details three:

Insert image description here

iv. Details four:

Insert image description here

f. Basic use of date classes

Insert image description here

5、CRUD

Insert image description here

a. insert statement

Insert image description here

-- 创建goods商品表
CREATE TABLE IF NOT EXISTS `goods`(
	id INT,
	goods_name VARCHAR(10),
	price DOUBLE
);
DESC goods;
-- 添加数据
INSERT INTO goods(id,goods_name,price) VALUES(1,'薯片',8.0),(2,'可乐',6.0);
-- 查看数据
SELECT *FROM goods;
i. Usage details

Insert image description here

-- 修改某列的 默认值
ALTER TABLEMODIFY 表中的某列 对应的类型 DEFAULT;

b. update statement

Insert image description here

-- 新建employee表
CREATE TABLE IF NOT EXISTS `employee`(
	id INT,
	`name` VARCHAR(30),
	`sex` CHAR(1),
	`sal` DOUBLE
);
-- 添加数据
INSERT INTO `employee` 
VALUES(1,'tom','男',5000),
	(2,'小妖怪','女',5500),
	(3,'大妖怪','男',6000);
	
-- 1、将所有员工薪水修改为5000元
UPDATE `employee` SET sal=5000;
SELECT *FROM employee;
-- 2、将姓名为 小妖怪 的员工薪水修改为3000
UPDATE employee SET sal=3000 WHERE `name`='小妖怪';
-- 3、将 老妖怪 的薪水在原有基础上增加1000元
UPDATE employee SET sal=sal+1000 WHERE `name`='老妖怪'

i. Usage details

Insert image description here

c. delete statement

Insert image description here

-- 1、删除表中名为‘老妖怪’的记录
DELETE FROM employee WHERE `name`='老妖怪';
-- 2、删除表中所有记录
DELETE FROM employee;
i. Usage details

Insert image description here

d. select statement 1 (single table query)

i. Basic grammar 1

Insert image description here

  • small exercise
    Insert image description here
-- ****创建新的表(student)********
CREATE TABLE student(
id INT NOT NULL DEFAULT 1,
NAME VARCHAR(20)NOT NULL DEFAULT '',
chinese FLOAT NOT NULL DEFAULT 0.0,
english FLOAT NOT NULL DEFAULT 0.0,
math FLOAT NOT NULL DEFAULT 0.0
);
-- 添加数据
INSERT INTO student(id,NAME,chinese,english,math)
VALUES(1,'韩顺平',89,78,90);
INSERT INTO student (id,NAME,chinese,english,math)
VALUES(2,'张飞',67,98,56);
INSERT INTO student (id,NAME,chinese,english,math)
VALUES(3,'宋江',87,78,77);
INSERT INTO student (id,NAME,chinese,english,math)
VALUES(4,'关羽',88,98,90);
INSERT INTO student (id,NAME,chinese,english,math)
VALUES(5,'赵云',82,84,67);
INSERT INTO student (id,NAME,chinese,english,math)
VALUES(6,'欧阳锋',55,85,45);
INSERT INTO student (id,NAME,chinese,english,math)
VALUES(7,'黄蓉',75,65,30);

-- 查看数据
SELECT * FROM student;

DELETE FROM student

-- 练习1、查询表中所有学生的信息
SELECT * FROM student;

-- 练习2、查询表中所有学生的姓名和对应的英语成绩
SELECT `name`,english FROM student;
-- 练习3、过滤表中重复的数据【distinct】
SELECT DISTINCT english FROM student;
-- 练习4、要查询的记录,每个字段都相同,才去重
SELECT `name`,english FROM student;

ii. Basic grammar 2

Insert image description here

  • small exercise
    Insert image description here
-- 练习1、统计每个学生的总分
SELECT `name`,(chinese+math+english) FROM student;
-- 练习2、在所有学生总分加10分的情况
SELECT `name`,(chinese+math+english+10) FROM student;
-- 练习3、使用别名表示学生分数
SELECT `name`,(chinese+math+english+10) AS 'sum' FROM student;

iii. Grammar 3: Operators used in where clause

Insert image description here

  • small exercise
    Insert image description here
-- 练习1、查询姓名为赵云的学生成绩
SELECT * FROM student WHERE `name`='赵云'
-- 练习2、查询英语成绩大于90分的同学
SELECT * FROM student WHERE english>90
-- 练习3、查询总分大于200 分的所有同学
SELECT * FROM student WHERE (chinese+math+english)>200

-- 查询math大于60并且id大于90的学生成绩
SELECT * FROM student WHERE math >60 AND id >90
-- 英语成绩大于语文成绩的同学
SELECT `name` FROM student WHERE english > chinese
-- 查询总分大于200分 并且 数学成绩小于语文成绩的姓韩的学生
SELECT * FROM student WHERE (chinese+math+english)>200 AND math < chinese AND `name` LIKE '%韩%'
  • Exercise 2
    Insert image description here
-- 课堂练习
-- 1.查询英语分数在80-90之间的同学。
SELECT * FROM student WHERE english BETWEEN 80 AND 90;
-- 2.查询数学分数为89,90,91的同学。
SELECT * FROM student WHERE math IN(89,90,91);
-- 3.查询所有姓李的学生成绩。
SELECT * FROM student WHERE NAME LIKE '李%'
-- 4.查询数学分>80,语文分>80的同学。
SELECT * FROM student WHERE math >80 AND chinese >80;

SELECT (chinese+math+english) FROM student 
-- 课堂练习[学员自己练习]
-- 1.查询语文分数在70-80之间的同学。
SELECT* FROM student WHERE chinese BETWEEN 70 AND 80;
-- 2.查询总分为189,190,191的同学。
SELECT * FROM student WHERE (chinese+math+english) IN (189,190,191);
-- 3.查询所有姓李或者姓宋的学生成绩。
SELECT * FROM student WHERE NAME LIKE '李%' OR NAME LIKE '宋%';
-- 4.查询数学比语文多30分的同学。
SELECT * FROM student WHERE math=chinese+30;
iv. Basic Grammar 4: order by sorting

Insert image description here

-- 课堂练习:orderby.sql
-- 对数学成绩排序后输出【升序】。
SELECT * FROM student ORDER BY math;
-- 对总分按从高到低的顺序输出
SELECT `name`,(chinese+math+english) AS he FROM student ORDER BY he DESC;
-- 对姓李的学生成绩排序输出(升序)
SELECT * ,(chinese+math+english) AS he FROM student ORDER BY he ASC;

e. Select statement 2 (single table query enhancement)

i. Query enhancement

Insert image description here
Insert image description here

ii. Paging query

Insert image description here

iii. Group enhancement

Insert image description here
Insert image description here

f. select statement 3 (multi-table query)

i. Introduction

Insert image description here

Insert image description here

ii. Self-connection

Insert image description here

iii. Subquery

Insert image description here
Insert image description here

iv. Subquery temporary table

Insert image description here
Insert image description here

v. all operator in multi-row subquery

Insert image description here

vi. any operator in multi-row subquery

Insert image description here

vii. Multi-column subquery

Insert image description here
Insert image description here

viii. Subquery small exercises
  • Exercise 1
    Insert image description here
  • Exercise 2
    Insert image description here
  • Exercise 3
    Insert image description here
ix, merge query union all (without deduplication) and union
  • union all (no duplication)
    Insert image description here
    Insert image description here
  • union (removal of duplicates)
    Insert image description here
x. Table outer join: left and right outer join (table 1 left/right join table 2 on condition)

Insert image description here
Insert image description here

  • small exercise
    Insert image description here

6. Function

a. Statistics/total functions

i. Total function-count

Insert image description here
Insert image description here

ii. Total function - sum

Insert image description here

-- 统计一个班级数学总成绩
SELECT SUM(math) FROM student;
-- 统计一个班级语文、英语、数学各科的总成绩
SELECT SUM(chinese) '语文',SUM(english)'英语',SUM(math)'数学' FROM student;
-- 统计一个班级语文、英语、数学的成绩总和
SELECT SUM(chinese+math+english) 'he' FROM student;
-- 统计一个班级语文成绩平均分
SELECT SUM(chinese)/COUNT(*)AS'平均分' FROM student;

iii. Total function - avg

Insert image description here

-- 求一个班级数学平均分
SELECT AVG(math) FROM student;
-- 求一个班级总分平均分
SELECT AVG(chinese+math+english) FROM student;
iv. Total function-max/min

Insert image description here

-- 求班级中总分最大值和最小值
SELECT MAX(math+chinese+english)AS '最高分',MIN(math+chinese+english)AS '最低分' FROM student;
v. Group statistics-group by

Insert image description here

-- group by 练习
-- 1、建部门表
CREATE TABLE IF NOT EXISTS dept(
	deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,
	dname VARCHAR(20) NOT NULL DEFAULT '',
	loc VARCHAR(13) NOT NULL DEFAULT ''

);
-- add data
INSERT INTO dept VALUES
	(10,'ACCOUNTING','NEW YORK'),
	(20,'RESEARCH','DALLAS'),
	(30,'SALES','CHICAGO'),
	(40,'OPERATIONS','BOSTON');
SELECT * FROM `dept`;
-- 2、建雇员表emp
CREATE TABLE emp
(empno  MEDIUMINT UNSIGNED  NOT NULL  DEFAULT 0, /*编号*/
ename VARCHAR(20) NOT NULL DEFAULT "", /*名字*/
job VARCHAR(9) NOT NULL DEFAULT "",/*工作*/
mgr MEDIUMINT UNSIGNED ,/*上级编号*/
hiredate DATE NOT NULL,/*入职时间*/
sal DECIMAL(7,2)  NOT NULL,/*薪水*/
comm DECIMAL(7,2) ,/*红利 奖金*/
deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0 /*部门编号*/
);
-- add data
 INSERT INTO emp VALUES(7369, 'SMITH', 'CLERK', 7902, '1990-12-17', 800.00,NULL , 20), 
(7499, 'ALLEN', 'SALESMAN', 7698, '1991-2-20', 1600.00, 300.00, 30),  
(7521, 'WARD', 'SALESMAN', 7698, '1991-2-22', 1250.00, 500.00, 30),  
(7566, 'JONES', 'MANAGER', 7839, '1991-4-2', 2975.00,NULL,20),  
(7654, 'MARTIN', 'SALESMAN', 7698, '1991-9-28',1250.00,1400.00,30),  
(7698, 'BLAKE','MANAGER', 7839,'1991-5-1', 2850.00,NULL,30),  
(7782, 'CLARK','MANAGER', 7839, '1991-6-9',2450.00,NULL,10),  
(7788, 'SCOTT','ANALYST',7566, '1997-4-19',3000.00,NULL,20),  
(7839, 'KING','PRESIDENT',NULL,'1991-11-17',5000.00,NULL,10),  
(7844, 'TURNER', 'SALESMAN',7698, '1991-9-8', 1500.00, NULL,30),  
(7900, 'JAMES','CLERK',7698, '1991-12-3',950.00,NULL,30),  
(7902, 'FORD', 'ANALYST',7566,'1991-12-3',3000.00, NULL,20),  
(7934,'MILLER','CLERK',7782,'1992-1-23', 1300.00, NULL,10);

SELECT * FROM emp;
-- 3、建工资级别表
CREATE TABLE salgrade
(
grade MEDIUMINT UNSIGNED NOT NULL DEFAULT 0, /*工资级别*/ 
losal DECIMAL(17,2)  NOT NULL, /* 该级别的最低工资 */
hisal DECIMAL(17,2)  NOT NULL /* 该级别的最高工资*/
);

INSERT INTO salgrade VALUES (1,700,1200);
INSERT INTO salgrade VALUES (2,1201,1400);
INSERT INTO salgrade VALUES (3,1401,2000);
INSERT INTO salgrade VALUES (4,2001,3000);
INSERT INTO salgrade VALUES (5,3001,9999);
SELECT * FROM salgrade;


-- 练习1、显示每个部门的平均工资和最高工资
SELECT AVG(sal),MAX(sal),deptno FROM emp GROUP BY deptno ;
-- 显示每个部门的每种岗位的平均工资和最低工资
SELECT AVG(sal),MIN(sal),job,deptno FROM emp GROUP BY deptno , job
-- 显示平均工资低于2000的部门号和它的平均工资
SELECT deptno,AVG(sal)AS p FROM emp GROUP BY deptno HAVING p < 2000;

b. String function

Insert image description here

i. Method explanation

Insert image description here

ii. Small exercises
  • Exercise: Display the names of all employees in the emp table with the first letter lowercase
--方式一:
SELECT CONCAT(LCASE(SUBSTRING(ename,1,1)),SUBSTRING(ename,2)) AS new_name FROM emp;
--方式二:
LECT CONCAT(LEFT(ename,1),SUBSTRING(ename,2)) AS new_name FROM emp;

c. Mathematical functions

i. Method explanation

Insert image description here
Insert image description here

d. Time function

Insert image description here

i. Time function 1

Insert image description here
Insert image description here
Insert image description here

ii. Time function 2

Insert image description here
Insert image description here

Insert image description here

iii. Time function 3

Insert image description here
Insert image description here
Insert image description here

e. Encryption and system functions

Insert image description here
Insert image description here

Insert image description here

f. Process control function

i. Demand

Insert image description here
Insert image description here

ii. Introduction

Insert image description here
Insert image description here

7. Table constraints

a. Introduction to constraints

Insert image description here

b. Introduction to primary keys

Insert image description hereInsert image description here

c. Details of primary key usage

Insert image description here
Insert image description here

d. unique

Insert image description here
Insert image description here

e. Foreign key

Insert image description here
Insert image description here
Insert image description here

  • Usage details
    Insert image description here

f、check

Insert image description here

g. Self-increment (auto_increment)

Insert image description here
Insert image description here

h. Store vending system table design case

Insert image description here
Insert image description here

8. Index

Insert image description here
Insert image description here

a. Principle of index

Insert image description here
Insert image description here

b. Index type

Insert image description here

c. Use of index

Insert image description here
Insert image description here

d. Indexing exercises

Insert image description here

e. Index summary

Insert image description here

9. Affairs

a. Lead to affairs

Insert image description here

b. Business introduction

Insert image description here
Insert image description here

i. Pay attention to details

Insert image description here
Insert image description here

ii. Transaction practice

Insert image description here

c. Transaction isolation level

i. Introduction to isolation

Insert image description here
Insert image description here

ii. Isolation level (please refer to Station B: Teacher Han Shunping’s video)

Insert image description here
Insert image description here

d. ACID characteristics of transactions

Insert image description here

e. Small exercises

Insert image description here

10. Storage engine

Insert image description here

a. Main storage engine/table type characteristics

Insert image description here

b. Usage details

Insert image description here
Insert image description here

c. Modify the storage engine

Insert image description here

d. How to choose a storage engine

exist
Insert image description here

11. View

a. Demand

Insert image description here

b. Basic concepts

Insert image description here
Insert image description here

c. Basic use of views

Insert image description here
Insert image description here

d. View details

Insert image description here

e. Best practices for views

Insert image description here

f. Practice

i. Exercise 1

Insert image description here
Insert image description here

12. mysql management

a. mysql user

Insert image description here

b. Create user

Insert image description here
Insert image description here
Insert image description here

c. Permissions in MySQL

Insert image description here

d. User authorization, recycling and other operations

Insert image description here
Insert image description here

e. Detailed description

Insert image description here
Insert image description here

f. Practice

Insert image description here
Insert image description here

13. mysql exercises

a. Multiple choice questions

Insert image description here

b. Hands-on questions 1

Insert image description here
Insert image description here

c. Operation question 2

Insert image description here
Insert image description here
Insert image description here

e. Operation question 3

Insert image description here
Insert image description here
Insert image description here

f. Operation question 4

Insert image description here
Insert image description here
Insert image description here

g. Operation question 5

Insert image description here
Insert image description here
Insert image description here

h. Operation question 6

Insert image description here
Insert image description here
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/qq_45066822/article/details/129178471