Article directory
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;
Create the database:
create database 数据库名;
Use database:
use database 数据库名;
Check out which tables are in the database:
show tables;
Delete database:
drop database 数据库名;
View table structure:
desc 表名;
View the data in the table:
select * from 表名;
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 表名;
Query the employee's annual salary (fields can be involved in mathematical operations):
select enname,sal * 12 from emp;
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;
Query all fields:
select * from emp;
//实际开发中 不建议使用*,效率较低.
Hint: Any SQL statement ends with ";".
See the statement that creates the table:
select create table 表名;
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;
Check SMITH's salary:
select sal from emp where name = 'SMITH';
Find employees with salary greater than 3000:
select enname ,sal from emp where sal > 3000;
Find employees whose salary is not equal to 3000:
select enname,sal from emp where sal <> 3000;
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.
Find out those who have no allowance not null:
select enname ,sal ,comm from emp where comm is not null;
Find out which jobs are employees of MABAGER and SALESMAN:
select enname,job from emp where job = 'MABAGER' or 'SALESMAN';
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);
//当优先级不确定的时候加小括号
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 后面不是区间 是具体的值
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 '\_';
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;//降序
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;
//越靠前面的字段越起到引导作用,只有当前面的字段无法排序的时候.才会启用后面的字段
select enname,sal from tmp order by 2;
//这里的2表示的是第二列;
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
//按照顺序依次执行;
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;
//找出最高工资
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;
Calculate the annual salary of each employee:
select enname,(sal+comm)*12 as yearsal from emp;
//所有数据库中,只要是有null的运算,计算结果都是NULL
Correct spelling:
select enname,(sal+if null(comm,0))*12 as yearsal from emp;
count(*) and count (specifically a field)
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;
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;
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;
Step 2: Identify employees with higher than average salary:
select enname,sal from emp where sal > 2073.214286;
Combine nested:
//子查询
select enname,sal from emp where sal > (select avg(sal) from emp);
Average salary per job position:
select job ,avg(sal) from emp group by job;
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;
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.
Step 2: Find the salary greater than 2900.
The above method is inefficient;
Correct spelling:
select max(sal),deptno from emp where sal > 2900 group by deptno;
//效率较高,建议能够使用where 过滤的尽量使用where
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;
Step 2: Ask to display data with salary greater than 2000.
select max(sal),deptno from emp group by having avg(sal) > 2000;
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;
Note: distinct can only appear at the top of all fields.
select distinct deptno,job from emp;
Case: Count the number of jobs:
select count(distinct job) from emp;
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.
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.