MySQL common syntax and MySQL gadgets

//MySQL syntax
/*data type
int
double 
decimal
char
varchar
text
blob binary type maximum 4G
date
time
timestamp timestamp type
*/
login server
mysql -uroot -p123 -hlocalhost


exit
exit, quit


switch database
use databasename;


display all databases
show databases;


display all tables
show the tables;


DDL statement to


query table structure
desc tablename;


create table
create table stu
(
number char(11),
name varchar(50),
age int,
gender varchar(10)
);


Add statement
insert into tablename (attribute name...) values ​​(attribute value...);
or
insert into tablename values ​​(all attribute values);


update statement
update tablename set attribute name=attribute value... where conditions


delete data
delete from tablename where conditions


DCL statement user management


to create a user, created with the default root
the create the user username @ip address identified by 'password';
as
create user zhangsan @ localhost identified by ' 123';


to authorized users
grant Permission 1, permission 2... on database.* to username@ip address;
such as
grant all on databasename.* to zhangsan@localhost; (all permissions for a database)
permissions such as
create, update, delete, insert ,drop,alter,grant create...


revocation permission (must be root user)
1 revoke privileges, rights 2, 3 ... on permission * from the database user name @ip address;.
For example
revoke delete on * from zhangsan @ localhost databasename;.


view permissions
show grants for username @ip address


delete user
drop user user Name@ipaddress;


DQL statement (query)


query all columns
select * from table name;


query some columns
select attribute name 1, attribute name 2, ... from table name;


remove duplicate rows
select distinct attribute name 1, attribute name 2 , ... from table;


column computation
select 1 * 1.5 from property name table;
select name attribute 1 + 2 from property name table;


string as a continuous operation, concat () which can have multiple attributes
query property name 1, with the addition of '$' symbol
select concat ( "$", the attribute name 1) from table


converts a null value
if the attribute is empty, output 0
SELECT IFNULL (attribute name, 0) from the table names


to column names from Alias
select an attribute name from an alias table


controlled conditions
with a where clause
in
the any
All
IS written not null null =
or
and


fuzzy inquiry like
_ matches one character
% match any number of characters
where properties like 'Double _' begin with the query sheets and a two-word;
where properties like 'Double%' queries all of Zhang
where properties like '% Double%' queries all the sheets contained


sort
order by statement
order by attribute
order by ascending properties asc
order by descending attribute desc


a plurality of attributes can sort
attributes an ascending, ascending at an attribute property 2 descending
order by ASC property 1, property 2 desc;


aggregation function
count (*) the number (COUNT if written as (1), the result is the same )
sum(attribute) sum
max(attribute) maximum
min(attribute) minimum
avg (attribute) the average of


grouping queries (in accordance with an attribute grouping, grouped, generally show with a group of related attributes and aggregate functions)
Group by attributes
such as number of employees working a query
select job, count (*) from emp group by job;


general execution order, where only aggregate functions can be used in having, and cannot be aggregate functions in where
select 
from 
where
group by
having
order by


paging query (very useful)
limit (dialect, only mysql has, sqlserver can be used top) For
example, starting from row 5, query 3 rows.
select * from emp limit 4, 3;


query pagecount of a
page as the current page, pagecount is how many rows to display
select * from emp limit (page-1)*pagecount , pagecount;


encountered problems when encoding operations in the cMD (but can not change the encoding cmd)


MySql backup and restore


backups, back up the database to the c drive in a.sql
mysqldump -uroot -p123 database name> c: /a.sql


reduction, you must first create a database, and then restore the
create database database name
mysql -uroot -p123 database name <c: /a.sql;


constraint


primary key constraint primary key
created table When adding the constraint
number int primary key
or
primary key (number)


when modifying the table, adding
alter table emp
add primary key (number)


deleting the primary key constraint
alter table emp drop primary key


primary key self-growth (cannot be a string)
sid int primary key auto_increment


non Null constraint
not null


unique constraint
unique


foreign key constraint 
constraint constraint name foreign key (attribute) reference table name (attribute)
such as dno reference deptno of dept
constraint fk_emp_dept foreign key (dno) reference dept (deptno)


were combined result set
union removing duplicate rows
union all without removing duplicate rows


such as
select * from emp1
union 
select * from EMP2


connected to
the connecting 
inner join on
such
criteria select * from table 1 alias 1 inner join table 2 alias 2 ON condition
dialect select * from table 1 alias 1, table 2 alias 2 WHERE condition


NATURAL connection (to find the same name of the same type)
select * from table 1 alias 1 natural join table 2 2 alias


outer connecting
left outer connecting
left outer join on
the right outer join
right outer join on


the whole outer connecting
combined left and right outer connecting
select * from table 1 a left outer join on alias table 2 alias 2 ON condition
union
select * from table name alias 1 1 right outer join on table 2 Alias 2 on conditions of


sub-query
is generally used from, where in


practice
a single-table exercises


1. check out the department number for all employees 30
select * from emp where deptno='30'


2. The names, numbers, and department numbers of all salespersons
select name, empno, deptno from emp where job='salesperson'


3. Find employees whose bonuses are higher than their wages
select * from emp where sal>comm


4. Employees whose bonus is higher than 60% of salary
select * from emp where comm>sal*0.6


5. The details of all managers in the department number 10, and all the salespeople in the department number 20
select * from emp where (job='manager' and deptno='10') or (job='salesperson' and deptno='20')


6. The department number is all managers in 10, and the department number is all sales in 20 Employees, as well as the details of all employees who are neither managers nor salespersons but whose salary is greater than or equal to 20,000
select * from emp select * from emp where (job='经理' and deptno='10') or (job='销售员' and deptno='20') 
or (job not in('manager','salesman') and sal>=20000)


7. Employees with no bonus or bonus less than 1000
select * from emp where comm is null or comm<1000


8. Employees whose names consist of 3 characters
select * from emp where name like'___'


9. Employees who joined in 2000
select * from emp where hiberanate like '2000-%'


10. All employee details, sorted in ascending order by number
select * from emp order by empno


11. The detailed information of all employees, the salary is sorted in descending order, if the salary is the same, use the ascending order of the entry date
select * from emp order by sal desc, hiberanate asc


12. The average salary of each department
select deptno, avg(sal) average salary from emp group by deptno


13. The number of employees in each department
select deptno,count(*) quantity from emp group by deptno


14. The maximum wage, minimum wage, number of jobs for each job
select job, max(sal) maximum wage, min(sal) minimum wage, count(*) number of people from emp group by job




Multi-table exercise
1. Find out the department with at least one employee, display the department number, department name, department location , The number of departments
select d.deptno, d.deptname, d.loc, e.num number of departments
from dept d, (selet deptno,count(*) num from emp group by deptno) e
where d.deptno=e.deptno


2. List the names of all employees and the names of their immediate superiors
select e1.name,e2.name
from emp e1, emp e2
where e1.mgr=e2.empno


select e1.name,ifnull(e2.name, 'BOSS')
from emp e1 left outer join emp e2
where e1.mgr=e2.empno


3. List the number, name, and department name of all employees whose employment date is earlier than their immediate superior
select e1.empno, e1.name, d.deptname
from emp e1, emp e2, dept d
  where e1.mgr=e2.empno and e1.hiberanate>e2.hiberanate and d.deptno=e1.deptno


4. List the department name and the employee information of these departments, and also list those Department without employees
select *
from emp right outer join dept
on emp.deptno = dept.deptno


5. List various jobs with a minimum salary greater than 15000 and the number of employees engaged in this job
select job, count(*) number of people
from emp
group by job
having min(sal)> 15000


6. List the names of employees working in the sales department, assuming that the department number of the sales department is not known
select e.name
from emp e, dept d
where e.deptno=d.deptno and d.name='Sales Department'


7. List the information of all employees whose salary is higher than the average salary of the company, the name of the department, the superior, and the salary level
select e.*, d.name, m.name, s.grade
from emp e left outer join dept d on e.deptno=d.deptno
left outer join emp m on e.mgr=m.empno
left outer join salgrade s on e.sal between s.losal and s.hisal
where e.sal>(select avg(sal) from emp)


8. List the names of all employees and departments engaged in the same work as Pang Tong
select e.*, d.deptname
from emp e, dept d
where e.job=(select job from emp where name='Pang Tong') and e.deptno = d.deptno


9. List the names and salaries of all employees whose salary is higher than that of department 30. Department name
select e.name,e.sal,d.deptname
from emp e, dept d
where e.sal > all(select sal from emp where deptno='30') and e.deptno=d.deptno


or
select e.name,e.sal,d.deptname
from emp e, dept d
where e.sal> (select max(sal) from emp where deptno='30') and e.deptno=d.deptno


10. Find out the year. Profit, annual growth
select y1.year, ifnull(concat((y2.num-y1.num)/y1.num*100,'%'), '0%') increase
from yea y1 left outer join yea y2
on y1.year=y2.year-1





Guess you like

Origin blog.csdn.net/qq_31281327/article/details/78554529