MYSQL表操作(DML,DDL)

建表并插入数据:

sql 复制代码
mysql> create table worker(
    -> dept_id int(11) not null,
    -> emp_id int (11) not null,
    -> work_time date not null,
    -> salary float(8,2) not null,
    -> poli_face varchar(10) not null default '群众',
    -> name varchar(20) not null,
    -> birth date not null,
    -> primary key(emp_id)
    -> )engine=innodb default charset=utf8 row_format=dynamic;

mysql> insert into worker values(101,1001,'2015-5-4',3500.00,'群众','张三','1990-7-1');
mysql> insert into worker values(101,1002,'2017-2-6',3200.00,'团员','李四','1997-2-8');
mysql> insert into worker values(102,1003,'2011-2-4',8500.00,'党员','王亮','1983-6-8');
mysql> insert into worker values(102,1004,'2016-10-10',5500.00,'群众','赵六','1994-9-5');
mysql> insert into worker values(102,1005,'2014-4-1',4800.00,'党员','钱七','1992-12-30');
mysql> insert into worker values(102,1006,'2017-5-5',4500.00,'党员','孙八','1996-9-2');

1、显示所有职工的基本信息:

sql 复制代码
mysql> select *from worker;

2、查询所有职工所属部门的部门号,不显示重复的部门号。

sql 复制代码
mysql> select distinct dept_id from worker;

3、求出所有职工的人数。

sql 复制代码
mysql> select count(name) from worker;

4、列出最高工和最低工资。

sql 复制代码
mysql> select max(salary) as '最高工资',min(salary) as '最低工资' from worker;

5、列出职工的平均工资和总工资。

sql 复制代码
mysql> select avg(salary) as '平均工资',sum(salary) as '平均工资' from worker;

6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。

7、显示所有女职工的年龄。(worker表中先增加年龄性别属性,再将数据插入,然后查询。)

sql 复制代码
mysql> alter table worker add age tinyint default 18;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table worker add gender char(1) check(gender in ('F','M'));
Query OK, 6 rows affected (0.03 sec)
Records: 6  Duplicates: 0  Warnings: 0


mysql> update worker set age=43,gender='M' where name='张三';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update worker set age=39,gender='F' where name='李四';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update worker set age=47,gender='F' where name='王亮';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update worker set age=37,gender='M' where name='赵六';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update worker set age=36,gender='F' where name='钱七';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update worker set age=30,gender='F' where name='孙八';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

8、列出所有姓刘的职工的职工号、姓名和出生日期。

sql 复制代码
mysql> select emp_id,name,birth from worker where name='刘%';
Empty set (0.00 sec)

9、列出1960年以前出生的职工的姓名、参加工作日期。

sql 复制代码
mysql> select name,work_time from worker where birth < '1960-1-1';
Empty set (0.00 sec)

10、列出工资在1000一2000之间的所有职工姓名。

sql 复制代码
mysql> select name from worker where salary between 1000 and 2000;
Empty set (0.01 sec)

11、列出所有陈姓和李姓的职工姓名。

sql 复制代码
mysql> select name from worker where name like '李%' or name like '陈%';
+--------+
| name   |
+--------+
| 李四   |
+--------+
1 row in set (0.00 sec)

12、列出所有部门号为2和3的职工号、姓名、党员否。

sql 复制代码
mysql> select emp_id,name from worker where poli_face != '党员' and dept_id in (102,103);
+--------+--------+
| emp_id | name   |
+--------+--------+
|   1004 | 赵六   |
+--------+--------+
1 row in set (0.01 sec)

13、将职工表worker中的职工按出生的先后顺序排序。

sql 复制代码
mysql> select name,birth from worker order by(birth);
+--------+------------+
| name   | birth      |
+--------+------------+
| 王亮   | 1983-06-08 |
| 张三   | 1990-07-01 |
| 钱七   | 1992-12-30 |
| 赵六   | 1994-09-05 |
| 孙八   | 1996-09-02 |
| 李四   | 1997-02-08 |
+--------+------------+
6 rows in set (0.00 sec)

14、显示工资最高的前3名职工的职工号和姓名。

sql 复制代码
mysql> select emp_id,name from worker  order by(salary) desc limit 0,3;
+--------+--------+
| emp_id | name   |
+--------+--------+
|   1003 | 王亮   |
|   1004 | 赵六   |
|   1005 | 钱七   |
+--------+--------+
3 rows in set (0.00 sec)

15、求出各部门党员的人数。

sql 复制代码
mysql> select dept_id ,count(name) from worker where poli_face='党员' group by(dept_id);
+---------+-------------+
| dept_id | count(name) |
+---------+-------------+
|     102 |           3 |
+---------+-------------+
1 row in set (0.01 sec)

16、统计各部门的工资和平均工资。

sql 复制代码
mysql> select dept_id,sum(salary),avg(salary) from worker group by(dept_id);
+---------+-------------+-------------+
| dept_id | sum(salary) | avg(salary) |
+---------+-------------+-------------+
|     101 |     6700.00 | 3350.000000 |
|     102 |    23300.00 | 5825.000000 |
+---------+-------------+-------------+
2 rows in set (0.00 sec)

17、列出总人数大于4的部门号和总人数。

sql 复制代码
mysql> select dept_id,count(name) from worker group by(dept_id) having count(name) > 4;
Empty set (0.00 sec)
相关推荐
小陈工1 小时前
Python Web开发入门(十七):Vue.js与Python后端集成——让前后端真正“握手言和“
开发语言·前端·javascript·数据库·vue.js·人工智能·python
0xDevNull5 小时前
MySQL数据冷热分离详解
后端·mysql
科技小花5 小时前
数据治理平台架构演进观察:AI原生设计如何重构企业数据管理范式
数据库·重构·架构·数据治理·ai-native·ai原生
一江寒逸6 小时前
零基础从入门到精通MySQL(中篇):进阶篇——吃透多表查询、事务核心与高级特性,搞定复杂业务SQL
数据库·sql·mysql
D4c-lovetrain6 小时前
linux个人心得22 (mysql)
数据库·mysql
阿里小阿希6 小时前
CentOS7 PostgreSQL 9.2 升级到 15 完整教程
数据库·postgresql
荒川之神6 小时前
Oracle 数据仓库雪花模型设计(完整实战方案)
数据库·数据仓库·oracle
做个文艺程序员6 小时前
MySQL安全加固十大硬核操作
数据库·mysql·安全
不吃香菜学java7 小时前
Redis简单应用
数据库·spring boot·tomcat·maven
一个天蝎座 白勺 程序猿7 小时前
Apache IoTDB(15):IoTDB查询写回(INTO子句)深度解析——从语法到实战的ETL全链路指南
数据库·apache·etl·iotdb