On MySQL few small problems

- t_user 1. Create a table, field id (auto-increment primary key) name (user name) age (age of) sex (gender)

- 2 inserted into the data in Table
- name = Zhang San, age = 124 sex = M
- name = zhangwuji, age = 18 sex = M
- name = John Doe, age = 33 sex = M
- name = Zhao six, age = 20 sex = female
INSERT INTO t_user VALUES (null, 'Chi Master', 124, 'M'), (null, 'zhangwuji', 18, 'M'), (null, 'John Doe', 33 , 'male'), (null, 'Zhao six', 20, 'female');

- Zhao Six 3. Place the names of the person's age into a 60-year-old
UPDATE t_user SET age = 60 WHERE name = ' Zhao six';

- the number of all query 4. Zhang human
SELECT COUNT (*) FROM t_user WHERE name LIKE ' Double%';

- 5. Delete all men older than 30 years old
DELETE FROM t_user WHERE age> 30;

CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20),
score DOUBLE,
sex VARCHAR(1)
)

INSERT INTO student VALUES (NULL, 'John Doe 1', 60, 'M');
INSERT INTO student VALUES (NULL, 'John Doe 2', 70, 'F');
INSERT INTO student VALUES (NULL, 'John Doe 3 ', 77,' F ');
the INSERT the INTO Student the VALUES (NULL, "Zhang 4', 80, 'M');
the INSERT the INTO Student the VALUES (NULL," Zhang 5 ', 90,' M ');
INSERT INTO student VALUES (NULL, 'John Doe 6', 100, 'F');
INSERT INTO student VALUES (NULL, 'Joe Smith 7', 65, 'M');
INSERT INTO student VALUES (NULL, 'John Doe 8 ', 68,' F ');
the INSERT the INTO Student the VALUES (NULL, "Joe Smith 9', 74, 'M');

INSERT INTO student VALUES (NULL, 'John Doe 11', 90, 'in');
INSERT INTO student VALUES (NULL, 'John Doe 12', NULL, 'medium');
INSERT INTO student VALUES (NULL, 'John Doe 13 ', 60,' in ');

- grouped by gender. Queries were average male and female students -
the SELECT Sex, AVG (Score) the GROUP BY the FROM Student Sex;

- grouped by gender. Queries were male and female students average number -
the number of AS SELECT sex, AVG (score) , COUNT (*) FROM student GROUP BY sex;

- grouped by gender. Queries were male and female students of average, the number of requirements: Score 70 points lower than people who do not participate in groups -
the number of AS SELECT sex, AVG (score) , COUNT (*) FROM student1 WHERE score> 70 GROUP BY sex;


- grouped by gender. Queries were male and female students of average, the number of requirements: Score 70 points lower than people who do not participate in packet after packet. 2 is greater than the number of individuals -
the SELECT Sex, the AVG (Score) Average the AS, COUNT (*) Number of AS FROM student1 WHERE score> 70 GROUP BY sex HAVING COUNT (*)> = 2;

- acquiring data in the first 5 data -
the SELECT * Student the LIMIT the FROM 0,5;

- the department table
the CREATE TABLE the dept (
the above mentioned id INT PRIMARY KEY PRIMARY KEY, - departments the above mentioned id
DNAME VARCHAR (50), - name of the department
loc VARCHAR (50) - Department location
);

- Add four departments
INSERT INTO the dept (the above mentioned id, DNAME, LOC) VALUES
(10, 'Teaching and Research', 'Beijing'),
(20, 'work department', 'Shanghai'),
(30, 'Sales '' Canton '),
(40,' Finance Department ',' Shenzhen ');

 

- table position, job title, job description
the CREATE TABLE Job (
ID a PRIMARY KEY the INT,
jname VARCHAR (20 is),
Description VARCHAR (50)
);

- Add four positions
INSERT INTO the Job (the above mentioned id, jname, the Description) VALUES
(1, 'chairman', 'management of the entire company, orders'),
(2, 'manager', 'management and employees'),
( 3, 'salesman', 'sell products to the guests'),
(4, 'clerks', 'use of office software');

 

- Employee table
the CREATE TABLE emp (
the above mentioned id INT PRIMARY KEY, - employees the above mentioned id
ename VARCHAR (50), - employee name
job_id INT, - post the above mentioned id
MGR INT, - superiors
joindate DATE, - the date of the entry
salary DECIMAL (7,2), - salary
bonus DECIMAL (7,2), - bonuses
dept_id INT, - where the department number
CONSTRAINT emp_jobid_ref_job_id_fk a FOREIGN KEY (job_id) the REFERENCES the Job (the above mentioned id),
CONSTRAINT emp_deptid_ref_dept_id_fk a FOREIGN KEY (dept_id) the REFERENCES the dept (ID)
);

- Adding staff
INSERT INTO emp (the above mentioned id, ename, job_id, MGR, joindate, salary, Bonus, dept_id) VALUES
(1001, 'Monkey King', 4,1004, '2000-12-17', '8000.00', NULL, 20),
(1002, 'Junyi', 3,1006, '2001-02-20', '16000.00', '3000.00', 30),
(1003, 'Lin', 3,1006, '2001-02-22 ',' 12500.00 ',' 5000.00 ', 30),
(1004,' monk ', 2,1009,' 2001-04-02 ',' 29750.00 ', NULL, 20 is),
(1005,' Kui ', 4, 1006, '2001-09-28', '12500.00', '14000.00', 30),
(1006, 'Sung', 2,1009, '2001-05-01', '28500.00', NULL, 30),
( 1007, 'Bei', 2,1009, '2001-09-01', '24500.00', NULL, 10),
(1008, 'pig', 4,1004, '2007-04-19', '30000.00' NULL, 20 is),
(1009, 'Luo Guanzhong',. 1, NULL, '2001-11-17', '50000.00', NULL, 10),
(1010, "Wu", 3,1006, '2001-09-08', '15000.00', '0.00', 30),
(1011, 'Drifting', 4,1004, '2007-05-23', '11000.00', NULL, 20),
(1012, 'Kui', 4,1006, '2001-12-03', '9500.00', NULL, 30),
(1013, 'small white dragon', 4,1004, '2001-12-03', ' 30000.00 ', NULL, 20 is),
(1014.,' Guan ', 4,1007,' 2002-01-23 ', ' 13000.00 ', NULL, 10);

 

- wage scale
the CREATE TABLE salarygrade (
Grade INT PRIMARY KEY, - level
losalary INT, - the minimum wage
hisalary INT - the highest wages
);

- Add 5 pay grade
INSERT INTO salarygrade (Grade, losalary, hisalary) VALUES
(1,7000,12000),
(2,12010,14000),
(3,14010,20000),
(4,20010,30000),
(5,30010,99990);

- Demand:

- 1. queries all employee information. Queries employee number, employee name, salary, job title, job description
SELECT e.id, ename, salary, jname , description FROM job j, emp e WHERE j.id = e.job_id;

- 2. Query the employee number, employee name, salary, job title, job description, department name, department, position
SELECT e.id, ename, salary, jname , description, dname, loc FROM job j, emp e, dept d WHERE j.id = e.job_id and d.id = e.dept_id;

- 3. Query employee name, salary, wage scales
SELECT ename, salary, grade FROM emp e, salarygrade s WHERE e.salary BETWEEN s.losalary and s.hisalary;

- 4. Inquiries employee name, salary, job title, job description, department name, department, position, salary grade
SELECT ename, salary, jname, description , dname, loc, grade FROM job j, emp e, dept d, salarygrade s WHERE j.id = e.job_id and d.id = e.dept_id and e.salary BETWEEN s.losalary and s.hisalary;

- 5. check out the department number, department name, department, location, number of departments
SELECT d.id, d.dname, d.loc, COUNT (*) FROM dept d, emp e WHERE d.id = e.dept_id GROUP BY d.id;

- 6. Name the query names of all employees and their immediate superiors, no leading employees also need to query
SELECT e1.ename AS employees, e2.ename AS leadership from emp e1 LEFT JOIN emp e2 ON e1.mgr = e2.id ;

- 7. Query the number of sectors greater than three sectors average wage
SELECT AVG (salary) FROM dept d , emp e WHERE d.id = e.dept_id GROUP BY d.id HAVING COUNT (*)> 3;

- 8. query the highest salary man's name and department
SELECT e.ename, d.dname FROM emp e, dept d where d.id = e.dept_id and e.salary = (SELECT MAX (salary) FROM emp )

- 9. check out wage scales for employees of the 5 name and job title
SELECT e.ename, j.description FROM emp e, job j, salarygrade s WHERE j.id = e.job_id AND e.salary BETWEEN s.losalary and s.hisalary and s.grade = 5;

Guess you like

Origin www.cnblogs.com/chengzhuo/p/11205571.html