0.win can use nacicat premium
https://www.cnblogs.com/webnote/p/5753996.html
1. Database
1.1 View the database
show databases;
1.2 Create a database
create database 数据库名;
1.3 Delete the database
drop database 数据库名;
1.4 Select the database
use 数据库名 //唯一一条不用加;的执行,不是SQL语句,是mysql语句
2. The database table
2.1 View Table
show tables;
2.2 Create a data table
create table 表名(字段名称1 字段类型1,字段名称2 字段类型2......)
2.3 Create a data table with constraints
1. The only constraint
unique
create table 表名(字段名称1 字段类型1 unique,字段名称2 字段类型2......)
2. Non-empty constraints (any null is not equal to another null)
not null
create table 表名(字段名称1 字段类型1 not null,字段名称2 字段类型2......)
3. Primary key constraint (unique and non-empty combination)
方法1:直接使用两个约束条件组合
create table 表名(字段名称1 字段类型1 unique not null,字段名称2 字段类型2......)
方法2:主键primary key
create table 表名(字段名称1 字段类型1 primary key,字段名称2 字段类型2......)
4. General digital type components cooperate with MySQL's automatic growth strategy
auto_increment
create table 表名(字段名称1 字段类型1 primary key auto_increment,字段名称2 字段类型2......)
由于自动增长,所以在insert into的时候就不用输入主键的values
5. Foreign key constraints
When there are two tables, one field of Table 1 is the primary key of Table 2 , you can use the primary key of Table 2 to constrain the fields of Table 1
foreign key (constrained field name) references table 2 (primary key)
create table class(num int primary key auto_increment, name varchar(11));
create table stu(num int primary key auto_increment, name varchar(11), classnum int, foreign key(classnum) references class(num))
2.4 View the data in the table
1. View the structure of the table:
desc 表名
2. View the data of the table
select * from 表名
2.5 Delete a table
drop table 表名
3. Modify the structure of the table
3.1 Add fields
alter table 表名 add 字段名称 字段类型;
3.2 Modify fields
alter table 表名 modify 字段名称 字段类型;
3.3. Delete Field
alter table 表名 drop 字段名称;
4. Modify the contents of the table
4.1 Insert data into the table
insert into 表名(字段名称1,字段名称2...) values(字段1的值,字段2的值)
上面的字段可以只插入部分,要填几个就写几个,其他为空,字段的值必须跟字段的类型一致
如果要插入全部的可以把所有的字段名称都写,或者将()都去掉不写,就代表全部
insert into 表名 values(所有字段的值)
4.2 Delete the data in the table
清空表
delete from 表名;
删除表中的某一条数据
delete from 表名 where 条件表达式(如:字段名称=字段值);
4.3 Modify the data in the table
修改所有记录的字段名称对应的字段值
update 表名 set 字段名称1=新的字段值1;
修改某一条记录的字段名称对应的字段值
update 表名 set 字段名称1=新的字段值1 字段名称2=新的字段值2... where 条件表达式;
4.4 Data in the lookup table
1. View all content
select * from 表名;
2. Field list, expression (arithmetic operation)
select 字段列表,表达式(算数运算),函数 from 表名
select sal form emp
selsct ename,sal from emp
selsct sal,sal*12 from emp
3.where conditional expression query
select * from 表名 where 条件表达式(可以多条件符合add or)
- Equivalence comparison =
- Greater than>
- Less than <
- Greater than or equal to> =
- Less than or equal to <=
- Not equal to <>
- And and
- 或 or
- Within the range in (excluding yourself)
- Not in
- Within and between (including yourself)
- The judgment of null value is null
- Non-null is worth judging is not null
4. Function
1. Mathematical functions
PI()
FLOOR(x) 返回小于x的最大整数(去掉小数部分取整)
CEILING(x) 返回大于x的最小整数(加1取整)
ROUND(x,y) 但会参数x的四舍五入的又y位小数的值
TRUNCATE(x,y) 返回数字x截断为y位小数的结果
2. Aggregation function (grouping function)
avg(col) 平均值
count(col) 返回指定列中非NULL值的个数
min(col) 最小值
max(col) 最大值
sum(col) 求和
3. String functions
concat(s1,s2...sn) 将s1,s2...sn连接成字符串
ltrim(str) 去掉字符串str开头的空格
rtrim(str) 去掉字符串str结尾的空格
trim(str) 去掉字符串str首部和尾部的空格
insert(str,x,y,instr) 将字符串str从第x位置看是,y个字符长度的子串替换为字符串instr
substring(str,x,y) 截取字符串x开始的y个子串
4. Date function
year(datatime)
month(datatime)
day(datatime)
hour(time)
minute(time)
second(time)
data(datatime)
time(datatime)
5. Alias
Can give fields, expressions, functions, tables
select 字段/表达式/函数 别名 form rmp
如果别名是中文特殊字符需要加‘’
6. Grouping
select field / expression / function form emp group by field name
selsct max(sal) max_sal from group by deptno
Note:
If you use a grouping function, fields that are not in the grouping function must exist after group by
正确:
selsct max(sal) max_sal,deptno from group by deptno
错误:
selsct max(sal) max_sal,ename from group by deptno
7. After grouping, query where condition at home
错误:
selsct max(sal) max_sal,deptno from group by deptno where max_sal > 2009
where条件只能判断数据表里面的字段名称,可以使用having就可以判断新的别名
正确:
selsct max(sal) max_sal,ename from group by deptno having max_sal > 2009
Want to inquire who is the highest salary of a department? Use subquery
7. Subquery
Query again by query result
setect ename,sal from emp where sal = (select max(sal) from emp)
Inquire who is the manager in the company
select mgr from emp
Remove duplicate distinct
select distinct mgr from emp
Remove NULL
select distinct mgr from emp where mgr is not null
Subquery
select ename,empno form emp where empno in(select distinct mgr from emp where mgr is not null)
Query the department with the largest average salary
Group functions cannot be nested.
Our query results can be used as a data source for another query as a table, which must be aliased in the process of being a table
select avg_sal,deptno from (select avg(sal)avg_sal, deptno from emp group by deptno) e where
avg_sal = (select max(avg_sal) from (select avg(sal)avg_sal, deptno from emp group by deptno) avg_table
8. Multi-table query
Query the name of the employee and the name of the employee's department
Involves two tables emp and dept
select ename, dname from emp, dept;
Cartesian product 14 * 4 = 56, 56 results will appear
select ename,dname from emp,dept where emp.deptno = dept.deptno
联合join
select ename,dname from emp join dept on emp.deptno = dept.deptno
三表查询
select ename,dname,grade from emp join dept join salgrade on emp.deptno = dept.deptno and emp.sal between losal and hisal
Query the name of the employee and the name of the employee manager
Divide the same table into two tables, so that you can query multiple tables select e.ename, m.ename from emp e join emp m on e.empno = m, mgr;
If there are records in the left outer union / left table that do not match and you want to display them,
select e.ename, m.ename from emp e left join emp m on e.empno = m, mgr;
If there is a record in the right outer union / right table that does not match and you want to display it,
select ename, dname from emp right join dept on emp.deptno = dept.deptno
operation
Department's average salary grade
Departmental salary grade average
Which of the employees are managers
Do not use group function to find the maximum salary
Department number with the highest average salary
Name of the department with the highest average salary
Name of the department with the lowest average salary
The name of the manager who is higher than the highest salary of ordinary employees