MYSQL表操作(DML,DDL)

MYSQL表操作(DML,DDL)_第1张图片

建表并插入数据:

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');

MYSQL表操作(DML,DDL)_第2张图片 1、显示所有职工的基本信息:

 

mysql> select *from worker;

MYSQL表操作(DML,DDL)_第3张图片

 

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

mysql> select distinct dept_id from worker;

MYSQL表操作(DML,DDL)_第4张图片

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

mysql> select count(name) from worker;

MYSQL表操作(DML,DDL)_第5张图片

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

mysql> select max(salary) as '最高工资',min(salary) as '最低工资' from worker;

MYSQL表操作(DML,DDL)_第6张图片

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

mysql> select avg(salary) as '平均工资',sum(salary) as '平均工资' from worker;

MYSQL表操作(DML,DDL)_第7张图片6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。

MYSQL表操作(DML,DDL)_第8张图片

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

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

MYSQL表操作(DML,DDL)_第9张图片

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

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

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

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

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

mysql> select name from worker where salary between 1000 and 2000;
Empty set (0.01 sec)

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

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

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

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中的职工按出生的先后顺序排序。

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名职工的职工号和姓名。

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、求出各部门党员的人数。

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、统计各部门的工资和平均工资。

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的部门号和总人数。

mysql> select dept_id,count(name) from worker group by(dept_id) having count(name) > 4;
Empty set (0.00 sec)

 

 

 

你可能感兴趣的:(mysql,数据库)