mysql query case dept, emp table data

mysql query case dept, emp table data

Department table

CREATE TABLE DEPT(
DEPTNO INT PRIMARY KEY, – department number
DNAME VARCHAR(14), – department name
LOC VARCHAR(13) – department address
) charset=utf8;

INSERT INTO DEPT VALUES (10,‘ACCOUNTING’,‘NEW YORK’);
INSERT INTO DEPT VALUES (20,‘RESEARCH’,‘DALLAS’);
INSERT INTO DEPT VALUES (30,‘SALES’,‘CHICAGO’);
INSERT INTO DEPT VALUES (40,‘OPERATIONS’,‘BOSTON’);

Employee table

CREATE TABLE EMP
(
EMPNO INT PRIMARY KEY, – Employee Number
ENAME VARCHAR(10), – Employee Name
JOB VARCHAR(9), – Work
MGR DOUBLE, – Direct Leader Number
HIREDATE DATE, – Entry Time
SAL DOUBLE, – Salary
COMM DOUBLE, – Bonus
DEPTNO INT, – department number
FOREIGN KEY(DEPTNO) REFERENCES DEPT(DEPTNO))charset=utf8;

SELECT * FROM emp;

INSERT INTO EMP VALUES
(7369,‘SMITH’,‘CLERK’,7902,‘1980-12-17’,800,NULL,20);
INSERT INTO EMP VALUES
(7499,‘ALLEN’,‘SALESMAN’,7698,‘1981-02-20’,1600,300,30);
INSERT INTO EMP VALUES
(7521,‘WARD’,‘SALESMAN’,7698,‘1981-02-22’,1250,500,30);
INSERT INTO EMP VALUES
(7566,‘JONES’,‘MANAGER’,7839,‘1981-04-02’,2975,NULL,20);
INSERT INTO EMP VALUES
(7654,‘MARTIN’,‘SALESMAN’,7698,‘1981-09-28’,1250,1400,30);
INSERT INTO EMP VALUES
(7698,‘BLAKE’,‘MANAGER’,7839,‘1981-05-01’,2850,NULL,30);
INSERT INTO EMP VALUES
(7782,‘CLARK’,‘MANAGER’,7839,‘1981-06-09’,2450,NULL,10);
INSERT INTO EMP VALUES
(7788,‘SCOTT’,‘ANALYST’,7566,‘1987-07-13’,3000,NULL,20);
INSERT INTO EMP VALUES
(7839,‘KING’,‘PRESIDENT’,NULL,‘1981-11-17’,5000,NULL,10);
INSERT INTO EMP VALUES
(7844,‘TURNER’,‘SALESMAN’,7698,‘1981-09-08’,1500,0,30);
INSERT INTO EMP VALUES
(7876,‘ADAMS’,‘CLERK’,7788,‘1987-07-13’,1100,NULL,20);
INSERT INTO EMP VALUES
(7900,‘JAMES’,‘CLERK’,7698,‘1981-12-03’,950,NULL,30);
INSERT INTO EMP VALUES
(7902,‘FORD’,‘ANALYST’,7566,‘1981-12-03’,3000,NULL,20);
INSERT INTO EMP VALUES
(7934,‘MILLER’,‘CLERK’,7782,‘1982-01-23’,1300,NULL,10);

Salary scale

CREATE TABLE SALGRADE
( GRADE INT, – 工资等级
LOSAL DOUBLE, – 最低工资
HISAL DOUBLE ) charset=utf8; – 最高工资
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);

Query case:

Query a single column
SELECT column name FROM table;
Query employee's name
SELECT ename FROM emp;
Query multiple columns
SELECT column 1, column 2, column 3... FROM table;
Query employee's name and job
SELECT ename, job FROM emp;
query all the columns
the SELECT * the FROM the table;
 query all the information employees
SELECT * FROM emp;
removing duplicate data
SELECT DISTINCT column 1, column 2 ... the FROM the table;
 query the emp table which trades
SELECT DISTINCT job FROM emp;
query Four arithmetic operations
Mysql supports four arithmetic operations directly in the query [+, - ,, /]
Query the annual salary of each employee
SELECT ename, sal
12 FROM emp;
query alias
Select column 1 AS alias 1, column 2 alias 2… FROM Table;
Query the annual salary of each employee
SELECT ename name, sal*12 AS annual salary FROM emp;
sort query
SELECT * FROM table ORDER BY column 1 ASC, column 2 DESC...
Query employee's name and salary, sort in descending order of salary
SELECT ename,sal FROM emp ORDER BY sal DESC;
Sort by multiple fields: query employee's name, entry date and salary, sort in ascending order of salary and entry date
SELECT ename ,emp.HIREDATE,sal FROM emp ORDER BY sal ASC, emp.HIREDATE ASC;
paging query
SELECT * FROM table LIMIT start,pagesize; Query
the first three records of the employee table
SELECT ename,emp.HIREDATE,sal FROM emp LIMIT 3; – Take out the first 3 records
 Query the three records in the middle of the employee table, starting from the fifth one, take
SELECT ename, emp.HIREDATE, sal FROM emp LIMIT 5, 3; – 5 is the row number, and the row number starts from 0
 Query The 10 records in the middle of the employee table are taken from the fifth,
SELECT ename, emp.HIREDATE, sal FROM emp ORDER BY sal LIMIT 5, 10 in ascending order of salary ;
use fully qualified column names to query
SELECT table 1. Column 1, Table 1. Column 2... FROM table; Query
employee name and salary
SELECT emp.ename, emp.sal FROM emp;
query table alias query
SELECT a. Column 1, a. Column 2… FROM Table a;

Query the employee's name and salary SELECT e.ename ,e.sal FROM emp e;
WHERE clause
Select * from table where condition order by column1…;

Use comparison operators to query
 Query employee information whose employee number is 7566
SELECT * FROM emp e WHERE e.EMPNO = 7566; Query
employee information that is not a salesperson
SELECT * FROM emp e WHERE e.JOB !='SALESMAN';
SELECT * FROM emp e WHERE e.JOB <>'SALESMAN'; Query
information of employees whose salary is less than 3200
SELECT * FROM emp e WHERE e.SAL <3200; Query
information of employees whose salary is greater than 3200
SELECT * FROM emp e WHERE e. SAL> 3200; Query
employee information with salary less than or equal to 2000
SELECT * FROM emp e WHERE e.SAL <= 2000; Query
employee information with salary greater than or equal to 2000
SELECT * FROM emp e WHERE e. SAL >= 2000;
scope Query Query
employee information with salary between 2000 and 3000
SELECT * FROM emp e WHERE e.SAL BETWEEN 2000 AND 3000;
null/non-empty query
Query employee information with bonus
SELECT * FROM emp e WHERE e.COMM IS NOT NULL;
Query employee information without bonus
SELECT * FROM emp e WHERE e.COMM IS NULL;
many conditions -AND
employee information query Seller  greater than 1200 Wages
the SELECT * the WHERE e.SAL the FROM EMP E> = e.JOB the AND 1250 'Salesman';
 query employee information entry date salesman in salary greater than 1200 prior to May 1981 of
SELECT * FROM emp e WHERE e.SAL> 1250 AND e.JOB = 'salesman' AND e.HIREDATE < '1981-5-1';
Multi-condition query- OR Query
information about employees whose salary is greater than 4500 or salesperson
SELECT * FROM emp e WHERE e.SAL> 4500 OR e.JOB ='salesman';
Query information about three employees: 7566, 7788, and 7956
SELECT * FROM emp e WHERE e.EMPNO = 7566 OR e.EMPNO = 7788 OR e.EMPNO = 7839;
IN keyword
query Query information of three employees : 7566, 7788, 7956
SELECT * FROM emp e WHERE e.EMPNO IN ( 7566,7788,7839,777);
NOT keyword query
The query is not the information of the three employees 7566, 7788, 7956
SELECT * FROM emp e WHERE e.EMPNO NOT IN (7566,7788,7839);
Fuzzy query Query
employee information with A in the name
SELECT * FROM emp e WHERE e.ENAME LIKE'%a%'; Query
name Employee information ending with A in
SELECT * FROM emp e WHERE e.ENAME LIKE'%a'; Querying
employee information starting with A in the name
SELECT * FROM emp e WHERE e.ENAME LIKE'a%'; Querying
name The second letter is the employee information of A
SELECT * FROM emp e WHERE e.ENAME LIKE ' a%';



The third letter of the query name is the employee information of A SELECT * FROM emp e WHERE e.ENAME LIKE'__a %'; Query employee information whose name is _ as the second letter SELECT * FROM emp e WHERE e.ENAME LIKE ' /_%' ESCAPE'/';
Combination query-combined result set

 Query information of employees whose salary is above 1200
SELECT ename, sal FROM emp e WHERE e.SAL> 1200;
 Query information of employees whose salary is above 3000
SELECT ename, sal FROM emp e WHERE e.SAL> 3000;


-Merge result set: Remove duplicate data SELECT ename,sal FROM emp e WHERE e.SAL> 1200
UNION
SELECT ename, sal FROM emp e WHERE e.SAL> 3000;

– Combined result set: do not remove duplicate data
SELECT ename, sal FROM emp e WHERE e.SAL> 1200
UNION ALL
SELECT ename, sal FROM emp e WHERE e.SAL> 2000 ORDER BY sal;
grouping function

– Sum function SUM(): Find the sum of all wages
SELECT SUM(sal) FROM emp;
– Find the sum of the wages of department 20
SELECT SUM(sal) FROM emp WHERE emp.DEPTNO = 20;
– Find the average function AVG(): find the average salary of everyone
SELECT AVG(sal) FROM emp;
– find the average salary of department 20
SELECT AVG(sal) FROM emp WHERE emp.DEPTNO = 20;
– find the maximum value function MAX(): query Maximum salary
SELECT MAX(sal) FROM emp;
– Query the maximum salary of department 20
SELECT MAX(sal) FROM emp WHERE emp.DEPTNO = 20;
– Find the minimum value function MIN(): Query the minimum salary
SELECT MIN(sal) FROM emp;
– Query the minimum wage of department 20
SELECT MIN(sal) FROM emp WHERE emp.DEPTNO = 20;
– Find the total number of rows function count(): query how many employees the company has
SELECT COUNT(empno) FROM emp;
– Query the number of people in department 20
SELECT COUNT(empno) FROM emp WHERE emp.DEPTNO = 20;

Group query
 Query the minimum wage of each department
SELECT deptno, MIN(sal) xyz FROM emp GROUP BY deptno;
 Query the maximum wage of each department
SELECT deptno, MAX(sal) xyz FROM emp GROUP BY deptno;
 query each The number of people in the department
SELECT deptno,COUNT(empno) xyz FROM emp GROUP BY deptno;
 Query how many people are in each
job category in each department SELECT deptno,job,COUNT(empno) FROM emp e GROUP BY e.DEPTNO,e.JOB ;
 Query the minimum wage and department number of each department
SELECT deptno, MIN(sal) FROM emp e GROUP BY e.DEPTNO;

Group function exercise: display the sum of the job name of non-sales staff and the monthly salary of employees engaged in the same job
SELECT e.JOB,SUM(sal) FROM emp e WHERE e.JOB <>'salesman' GROUP BY e.JOB;
grouping Query: Filter the grouping conditions. Filter
the data after grouping. Use the HAVING clause
-display the job name of non-sales staff and the sum of monthly wages of employees
engaged in the same job,-and satisfy that the sum of monthly wages for the same job is greater than 5000, the result Sort in ascending order of the total monthly salary. Take out the first record
SELECT e.JOB, SUM(sal) sum FROM emp e WHERE e.JOB <>'salesman'
GROUP BY e.JOB HAVING SUM(sal)> 5000 ORDER BY sum DESC LIMIT 1;

Summary:
-SQL statement sequence:
select column 1, column 2... from tableName [WHERE clause] [GROUP BY clause] [HAVING clause] [ORDER BY clause] [limit x,y]
Delete data
format: DELETE FROM tableName [WHERE condition];

 Delete with conditions, otherwise delete the data in the entire table
DELETE FROM salgrade; Delete
employee number 7369
DELETE FROM emp WHERE empno = 7369;
Delete 7521 or employee of department 20
DELETE FROM emp WHERE empno = 7521 OR deptno = 20;

If you really want to clear the data in the table, use the truncated table to be faster
TRUNCATE emp;
update the data
Format: UPDATE tableName SET column 1 = value 1, column 2 = value 2 [where condition]
Modify all department addresses to Shanghai
UPDATE dept SET loc ='Shanghai'; – The update must be conditional, otherwise the entire table
will be updated  Change the department address No. 10 to Beijing, and change the department name to the information department
UPDATE dept SET dname ='Information department', loc = ' Beijing' WHERE deptno = 10; The
third unit
Constraints
Primary key constraint
Primary key constraint: The data in the field that is constrained by the primary key constraint cannot be NULL, and cannot be repeated [Non-empty, unique]
Note: There can only be one in each table Primary key constraint

DROP TABLE dept;
When creating a table, add a primary key constraint
CREATE TABLE dept(
deptno INT PRIMARY KEY,-add a primary key constraint
dname VARCHAR(10),
address VARCHAR(100)
);
INSERT INTO dept VALUES(20, 'Research Department x','Dallas'); – The addition is successful because it does not violate the rules of primary key constraints.
INSERT INTO dept VALUES(20,'Research Department x','Dallas'); – The addition fails because 20 is repeated

Delete the primary key constraint
ALTER TABLE dept DROP PRIMARY KEY;
Set the primary key constraint for the created table
ALTER TABLE emp CHANGE empno empno INT(11) PRIMARY KEY; the
primary key increment
DROP TABLE dept;
CREATE TABLE dept(
deptno INT PRIMARY KEY AUTO_INCREMENT , – Add a primary key constraint to the deptno field
dname VARCHAR(10),
address VARCHAR(100)
);
– 0 and null use auto-increment value
INSERT INTO dept VALUES(0,'Research Department x','Dallas');
INSERT INTO dept VALUES(null,'Research Department x','Dallas');
INSERT INTO dept (dname,address) VALUES('Research Department x','Dallas');
– For specific values, use specific values
INSERT INTO dept VALUES(999 ,'Research Departmentx','Dallas');
Add primary key auto-increment to the created table
ALTER TABLE dept CHANGE deptno deptno INT(11) AUTO_INCREMENT;
Unique constraint The
value of the uniquely constrained field cannot be repeated.
DROP TABLE dept;
When creating a table, add a unique constraint
CREATE TABLE dept(
deptno INT PRIMARY KEY, – Add a primary key constraint to the deptno column
dname VARCHAR(10) NOT NULL, – Add a non-null constraint to the DNAME column,
address VARCHAR (100) UNIQUE NOT NULL-add unique and non-null constraints to address
);
Delete unique constraints: Unique constraints are a type of index, so use the syntax of deleting indexes to delete unique constraints.
ALTER TABLE dept DROP INDEX address;
or
ALTER TABLE emp CHANGE empno empno INT(11).
Add a unique constraint to the created table
ALTER TABLE dept CHANGE dname dname VARCHAR(10) NOT NULL UNIQUE;
non
-null constraint The value of the non-null constrained field cannot be NULL
DROP TABLE dept;
When creating the table, add Non-null constraint
CREATE TABLE dept(
deptno INT PRIMARY KEY,
-Add a primary key constraint to the deptno field dname VARCHAR(10) NOT NULL,-Add a non-null constraint to the DNAME field,
address VARCHAR(100)
);

INSERT INTO dept VALUES(10,'xxx','Dallas'); – Successful
INSERT INTO dept VALUES(20,'YYY','Dallas'); – Successful
INSERT INTO dept VALUES(30,NULL,'Dallas'); --Failed because of non-empty constraint violation

Delete the non-null constraint
ALTER TABLE dept CHANGE dname dname VARCHAR(10);
Add the non-null constraint to the created table
ALTER TABLE dept CHANGE dname dname VARCHAR(10) NOT NULL; the
default value
sets the field constrained by the default value , If you do not assign a value, use the default value
DROP TABLE dept;
 When creating the table, add the default value constraint
CREATE TABLE dept(
deptno INT PRIMARY KEY, – add the primary key constraint
dname VARCHAR(10) NOT NULL,-- to the deptno field DNAME field add non-null constraint,
address VARCHAR(100) UNIQUE NOT NULL DEFAULT'Shanghai'- add unique and non-null constraint to address
);
INSERT INTO dept VALUES(10,'xxx','Dallas');
INSERT INTO dept ( deptno,dname) VALUES(210,'xxxY');
INSERT INTO dept VALUES(30,'xxx','Dallas');
 delete default value constraint
ALTER TABLE dept CHANGE address address VARCHAR(10);
 add default value constraint
ALTER TABLE dept CHANGE address address VARCHAR(10) NOT NULL DEFAULT ‘上海’ ;

Guess you like

Origin blog.csdn.net/shuai_ge_feng/article/details/107357962