1. MySQL practice
1. Create table:
Create employee table employee, the fields are as follows:
id (employee number)
name (employee name)
gender (employee gender)
salary (employee salary)
2. Insert data
1, 'Zhang San', 'Male', 2000
2 , 'Li Si', 'Male', 1000
3, 'Wang Wu', 'Female', 4000
3. Modify the table data
3.1 Change the salary of all employees to 5000 yuan
3.2 Change the salary of the employee named Zhang San to 3000 yuan
3.3 Change the salary of the employee named Li Si to 4,000 yuan, and the gener to female
3.4 Increase the salary of Wang Wu by 1,000 yuan on the original basis
operate
1. Create a table
CREATE TABLE employee (
id INT COMMENT '员工编号',
name VARCHAR(255) COMMENT '员工名字',
gender CHAR(2) DEFAULT '男' COMMENT '员工性别',
salary INT COMMENT '员工薪资'
);
2. Insert data
mysql> insert into employee values (1,'张三','男',2000),(2,'李四','男'1000),(3,'王五','女',4000);
3. Modify data
3.1 Modify the salary of all employees to 5000 yuan
mysql> update employee set salary = 5000;
3.2 Change the salary of the employee named Zhang San to 3,000 yuan
mysql> update employee set salary = 3000 where name='张三';
3.3 Change the salary of the employee named Li Si to 4,000 yuan, and change the gener to female
mysql> update employee set salary = 4000,gender='女' where name='李 四';
3.4 Increase Wang Wu's salary by 1,000 yuan on the original basis
mysql> update employee set salary = salary+1000 where name='王五';
2. Single table query practice
material:
CREATE TABLE `emp` (
`empno` int(4) NOT NULL,
`ename` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`job` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`mgr` int(4) NULL DEFAULT NULL,
`hiredate` date NOT NULL,
`sai` int(255) NOT NULL,
`comm` int(255) NULL DEFAULT NULL,
`deptno` int(2) NOT NULL,
PRIMARY KEY (`empno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `emp` VALUES (1001, '甘宁', '文员', 1013, '2000-12-17', 8000, NULL, 20);
INSERT INTO `emp` VALUES (1002, '黛绮丝', '销售员', 1006, '2001-02-20', 16000, 3000, 30);
INSERT INTO `emp` VALUES (1003, '殷天正', '销售员', 1006, '2001-02-22', 12500, 5000, 30);
INSERT INTO `emp` VALUES (1004, '刘备', '经理', 1009, '2001-04-02', 29750, NULL, 20);
INSERT INTO `emp` VALUES (1005, '谢逊', '销售员', 1006, '2001-09-28', 12500, 14000, 30);
INSERT INTO `emp` VALUES (1006, '关羽', '经理', 1009, '2001-05-01', 28500, NULL, 30);
INSERT INTO `emp` VALUES (1007, '张飞', '经理', 1009, '2001-09-01', 24500, NULL, 10);
INSERT INTO `emp` VALUES (1008, '诸葛亮', '分析师', 1004, '2007-04-19', 30000, NULL, 20);
INSERT INTO `emp` VALUES (1009, '曾阿牛', '董事长', NULL, '2001-11-17', 50000, NULL, 10);
INSERT INTO `emp` VALUES (1010, '韦一笑', '销售员', 1006, '2001-09-08', 15000, 0, 30);
INSERT INTO `emp` VALUES (1011, '周泰', '文员', 1006, '2007-05-23', 11000, NULL, 20);
INSERT INTO `emp` VALUES (1012, '程普', '文员', 1006, '2001-12-03', 9500, NULL, 30);
INSERT INTO `emp` VALUES (1013, '庞统', '分析师', 1004, '2001-12-03', 30000, NULL, 20);
INSERT INTO `emp` VALUES (1014, '黄盖', '文员', 1007, '2002-01-23', 13000, NULL, 10);
INSERT INTO `emp` VALUES (1015, '张三', '保洁员', 1001, '2013-05-01', 80000, 50000, 50);
– 1. Query all employees whose department number is 30
mysql> select * from emp where deptno = 30;
– 2. Names, numbers, and department numbers of all salespeople.
mysql> select ename,empno,deptno from emp;
– 3. Identify employees whose bonuses are higher than their salaries.
mysql> select * from emp where comm > sai;
– 4. Identify employees whose bonuses are 60% higher than their salary.
mysql> select * from emp where comm > (sai*0.6);
– 5. Find out the details of all managers in department number 10 and all salespersons in department number 20.
mysql> select * from emp where (deptno=10 and job='经理') or (deptno=30 and job='销售员');
– 6. Find out the details of all managers in department number 10, all salesmen in department number 20, and all employees whose salary is greater than or equal to 20000 but are neither managers nor salesmen.
mysql> select * from emp where (deptno=10 and job='经理') or (deptno=20 and job='销售员') or (job not in ('经理','销售员') and sai >= 20000 );
– 7. Employees with no bonus or bonus less than 1000.
mysql> select * from emp where comm is null or comm < 1000;
– 8. Search for employees whose names consist of three characters.
mysql> SELECT * FROM emp WHERE CHAR_LENGTH(ename) = 3;
或者
#三条下划线
mysql> SELECT * FROM emp WHERE ename like '___';
或者
mysql> select * from emp where length(ename)=9;
– 9. Query the employees who joined in 2000.
mysql> select * from emp where year(hiredate)=2000;
– 10. Query all employee details and sort them in ascending order by number
mysql> select * from emp order by empno asc;
– 11. Query all employee details, sort by salary in descending order, if the salary is the same, use entry date to sort in ascending order
mysql> select * from emp order by sai desc,date(hiredate) asc;
– 12. Query the average salary of each department
mysql> select deptno,avg(sai) from emp group by deptno;
– 13. Query the number of employees in each department
mysql> select deptno,count(*) from emp group by deptno;
– 14. Query the maximum wage, minimum wage, and number of people for each job
mysql> select job,max(sai),min(sai),count(*) from emp group by job;