MySQL --- Basic entry to proficient (1)

Overview of the database

1. What are SQL DB DBMSs? The relationship between them?

DB:
DataBase (database, the database actually exists in the form of a file on the hard disk)
DMBS:
DataBase Management System (database management system, common are: MySQL, Oracle, DB2, SqlSever...)
SQL:
Structured query language, is a It is a standard common language, and standard SQL is applicable to all database products.
SQL is a high-level language, as long as you can understand English words, the written SQL can be read
. Compile, and then execute SQL (the compilation of SQL statements is done by DBMS)
DBMS is responsible for executing SQL statements, and operates the data in the DB through the executed SQL statements.

Classification of SQL Statements

DQL (data query statement): query statement, all select statements are DQL; DML (data manipulation language): insert delete
update, add, delete and modify the data in the target; DDL (data definition language): create drop alter, on the table Addition, deletion and modification of structure;
TCL (transaction control statement): commit commits the transaction, rollback rolls back the transaction; DCL (data control language): grant
s authorization, revoke revoke authority, etc.;

MySQL common commands

View database:

show databases;

insert image description here

Create the database:

create database 数据库名;

insert image description here

Use database:

use database 数据库名;

insert image description here

Check out which tables are in the database:

show tables;

insert image description here
Delete database:

drop database 数据库名;

insert image description here

View table structure:

desc 表名;

insert image description hereinsert image description hereinsert image description here
View the data in the table:

select * from 表名;

insert image description here
insert image description here
insert image description here

Check which database is currently in use:

select database();

Check the MySQL version number:

select version();

End a statement:

\C

Quit MySQL:

exit

See the statement that creates the table:

show create table 表名;

Note: The above is not a SQL statement, but a SQL command;

SQL statement

Simple query

Syntax format:

select 字段名1,字段名2,字段名3,......from 表名;

insert image description here
insert image description here
Query the employee's annual salary (fields can be involved in mathematical operations):

select enname,sal * 12 from emp;

insert image description here
Column renaming of query results:

select ENNAME ,sal * 12 as yearsal from emp;
//如果含有中文字符需要用单引号括起来:
select ENNAME ,sal * 12 as '年薪' from emp;
//as 关键字可以省略
select ENNAME ,sal * 12 yearsal from emp;

insert image description here
insert image description here
Query all fields:

select * from emp;
//实际开发中 不建议使用*,效率较低.

insert image description here

Hint: Any SQL statement ends with ";".

See the statement that creates the table:

select create table 表名;

insert image description here

Conditional query

Syntax format:

select 
    字段,字段,...
from
    表名
where
    条件;
执行顺序:先from,然后where,最后select.

Query the name of the employee whose salary is equal to 5000:

select enname from emp where sal = 5000;

insert image description here
Check SMITH's salary:

select sal from emp where name = 'SMITH';

insert image description here
Find employees with salary greater than 3000:

select enname ,sal from emp where sal > 3000;

insert image description here
Find employees whose salary is not equal to 3000:

select enname,sal from emp where sal <> 3000;

insert image description here
Find employees with salaries between 1100 and 3000, including 1100 and 3000:

select enname ,sal from emp where sal >= 3000 and sal <=1100;
select enname ,sal from emp where sal between 1100 and 3000;
//between...and... 是闭区间[1100 ~ 3000]
//between...and...除了应在数字方面, 也可以用在字符串方面.

Conditional query is null and is not null

Find out who doesn't have a stipend:

select enname ,sal ,comm from emp where comm is null;
//在数据库中null不是一个值,代表什么也没有,为空,空不是一个值,不能赢等号衡量,必须是is null 或 is not null.

insert image description here
Find out those who have no allowance not null:

select enname ,sal ,comm from emp where comm is not null;

insert image description here
Find out which jobs are employees of MABAGER and SALESMAN:

select enname,job from emp where job = 'MABAGER' or 'SALESMAN';

insert image description here

and and or priority issues

Find employees whose salary is greater than 1000 and whose department number is 20 or 30:

select enname,sal depyno from emp where sal > 1000 and (deptno = 20 or deptno = 30);
//当优先级不确定的时候加小括号

insert image description here

Conditional query In

Find out which jobs are employees of MABAGER and SALESMAN:

select enname,job from emp where job in ('MABAGER' , 'SALESMAN');
//in 等同于 or.
//in 后面不是区间 是具体的值

insert image description here

fuzzy query like

Find out the second letter is A:

select enname from emp where enname like '_A%';
//如果要查找name列中含有'_'的,需要加上转义字符'\';
//select enname from emp where enname like '\_';

insert image description here

Sort data (ascending, descending)

select enname,sal from emp order by sal;//默认升序
select enname,sal from emp order by sal asc;//升序
select enname,sal from emp order by sal desc;//降序

insert image description here
insert image description here
Sort by salary in descending order, and then by name in ascending order when the salary is the same:

select enname,sal from tmp order by sal desc,enname asc;
//越靠前面的字段越起到引导作用,只有当前面的字段无法排序的时候.才会启用后面的字段

insert image description here

select enname,sal from tmp order by 2;
//这里的2表示的是第二列;

insert image description here
Find out the employees whose jobs are SALESMAN, and the requirements are in descending order of salary:

select 
    enname,job,sal      //3
from
    emp                 //1
where
    job = 'SALESMAN'    //2
order by 
    sal desc;           //4
    //按照顺序依次执行;

insert image description here

grouping function

count: count
sum: sum
avg: average
max: maximum value
min: minimum value
Note: All grouping functions operate on a certain set of data

Summing salaries:

select sum(sal) from emp;

insert image description here

//找出最高工资
select max(sal) from emp;
//找出最低工资
select min(sal) from emp;
//找出平均工资
select avg(sal) from emp;
//找出总人数
select count(*) from emp;
select count(enname) from emp;

Grouping functions automatically ignore NULLs:

select count(comm) from emp;

insert image description here
Calculate the annual salary of each employee:

select enname,(sal+comm)*12 as yearsal from emp;
//所有数据库中,只要是有null的运算,计算结果都是NULL

insert image description here
Correct spelling:

select enname,(sal+if null(comm,0))*12 as yearsal from emp;

insert image description here

count(*) and count (specifically a field)

insert image description here

count(*) : Instead of counting the number of data in a field, twenty counts the total number of records. (It has nothing to do with a field)
count(comm): Represents the total number of data that is not null in the comm field. Grouping Functions can also be combined:

select count(*),sum(sal),avg(sal),max(sal),min(sal) from emp;

insert image description here

select enname,sal from emp where sal > avg(sal);//ERROR

The above SQL statement error message: Invalid use of the grouping function. Reason: There is a syntax rule in the SQL statement, the grouping function cannot be used directly in the where clause. Because group
by is executed after the where is executed.

group by 和 having

group by: Group by a field or some fields. having: having is to filter the data after grouping again.

Example: Find the maximum salary for each job position:

select max(sal),job from emp group by job;

insert image description here
insert image description here

Note: The grouping function is generally used in conjunction with group by, which is why it is called a grouping function. And any grouping function (count max min
sum avg) will be executed after the execution of the group by statement. When a SQL statement does not have group by, the data in the entire table will form a group by itself.

Find employees with higher wages than average:
Step 1: Find average wages

select avg(sal) from emp;

insert image description here
Step 2: Identify employees with higher than average salary:

select enname,sal from emp where sal > 2073.214286;

insert image description here

Combine nested:

//子查询
select enname,sal from emp where sal > (select avg(sal) from emp);

insert image description here
Average salary per job position:

select job ,avg(sal) from emp group by job;

insert image description here

select enname, max(sal), job from emp group by job;
the above fields are in MySQL, and the query results are available, but the results are meaningless, and an error will be reported in the Oracle database, and the syntax is wrong.
Oracle's syntax rules are better than MySQL's syntax The rules are more rigorous.
Remember a rule: when there is group by in a statement, select can only be followed by the grouping function and the fields involved in the grouping.

Can multiple fields be combined and grouped together?
Case: Find the highest salary for different jobs in each department

select 
    deptno,job,max(sal)
from
    emp
group by 
    deptno,job;

insert image description here

The choice of having and where

Find out the maximum salary of each department and ask to display data with salary greater than 2900.
Step 1: Find the maximum salary of each department.
insert image description here
Step 2: Find the salary greater than 2900.
insert image description hereThe above method is inefficient;

Correct spelling:

select max(sal),deptno from emp where sal > 2900 group by deptno;
//效率较高,建议能够使用where 过滤的尽量使用where

insert image description here
Find out the maximum salary of each department, and ask to display data with salary greater than 2000:
Step 1: Find the maximum salary of each department

select max(sal),deptno from emp group by deptno;

insert image description here
Step 2: Ask to display data with salary greater than 2000.

select max(sal),deptno from emp group by having avg(sal) > 2000;

insert image description here

The grouping function cannot be used after the where: select max(sal),deptno from emp where avg(sal) > 2000
group by deptno; //Error. In this case, only having filtering can be used.

Deduplication of query result sets

1、

select distinct job from emp;

insert image description here
Note: distinct can only appear at the top of all fields.

select distinct deptno,job from emp;

insert image description here
Case: Count the number of jobs:

select count(distinct job) from emp;

insert image description here

connection query

What is a join query? In actual development, in most cases, it is queried from a single table. Generally, it is a joint query of multiple tables to remove the final result.
In actual development, generally a business will correspond to multiple tables, such as: Province and class, at least two tables.
insert image description here
Students and classes are stored in one table, the result is the same as above, there will be a lot of duplication of data, resulting in data redundancy.

Guess you like

Origin blog.csdn.net/m0_62764440/article/details/124144842