首先,由上图创建表

 
  1. mysql> create table worker(

  2. -> dept_id int(11) not null,

  3. -> emp_id int (11) not null,

  4. -> work_time date not null,

  5. -> salary float(8,2) not null,

  6. -> poli_face varchar(10) not null default '群众',

  7. -> name varchar(20) not null,

  8. -> birth date not null,

  9. -> primary key(emp_id)

  10. -> )engine=innodb default charset=utf8 row_format=dynamic;

  11. Query OK, 0 rows affected, 4 warnings (0.03 sec)

然后,向表内插入数据

 
  1. mysql> insert into worker values(101,1001,'2015-5-4', 3500.00,'群众','张三','1990-7-1');

  2. Query OK, 1 row affected (0.00 sec)

  3. mysql> insert into worker values(101,1002,'2017-2-6', 3200.00,'团员','李四','1990-2-8');

  4. Query OK, 1 row affected (0.01 sec)

  5. mysql> insert into worker values(102,1003,'2011-2-4',8500.00,'党员','王亮','1983-6-8');

  6. Query OK, 1 row affected (0.01 sec)

  7. mysql> insert into worker values(102,1004,'2016-10-10',5500.00,'群众','赵六','1994-9-5');

  8. Query OK, 1 row affected (0.00 sec)

  9. mysql> insert into worker values(102,1005,'2014-4-1',4800.00,'党员','钱七','1992-12-30');

  10. Query OK, 1 row affected (0.00 sec)

  11. mysql> insert into worker values(102,1006,'2017-5-5',4500.00,'党员','孙八','1996-9-2');

  12. Query OK, 1 row affected (0.00 sec)

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

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

 3,求出所有职工的人数

 4,列入最高工和最低工资

 5,列入职工的平均工资和总工资

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

  1. mysql> create table workdate select emp_id,name from worker;

  2. Query OK, 6 rows affected (0.02 sec)

  3. Records: 6 Duplicates: 0 Warnings: 0

  4. mysql> select *from workdate;

  5. +--------+--------+

  6. | emp_id | name |

  7. +--------+--------+

  8. | 1001 | 张三 |

  9. | 1002 | 李四 |

  10. | 1003 | 王亮 |

  11. | 1004 | 赵六 |

  12. | 1005 | 钱七 |

  13. | 1006 | 孙八 |

  14. +--------+--------+

  15. 6 rows in set (0.00 sec)

7,显示所有女职工的年龄(要给表中增加性别和年龄,然后给每人插入年龄和性别)

 
  1. mysql> alter table worker add age tinyint default 18;

  2. Query OK, 0 rows affected (0.03 sec)

  3. Records: 0 Duplicates: 0 Warnings: 0

  4. mysql> alter table worker add gender char(1) check(gender in ('F','M'));

  5. Query OK, 6 rows affected (0.04 sec)

  6. Records: 6 Duplicates: 0 Warnings: 0

  7. mysql> update worker set age=19,gender='F' where name='张三';

  8. Query OK, 1 row affected (0.01 sec)

  9. Rows matched: 1 Changed: 1 Warnings: 0

  10. mysql> update worker set age=20,gender='F' where name='李四';

  11. Query OK, 1 row affected (0.00 sec)

  12. Rows matched: 1 Changed: 1 Warnings: 0

  13. mysql> update worker set age=21,gender='M' where name='王亮';

  14. Query OK, 1 row affected (0.00 sec)

  15. Rows matched: 1 Changed: 1 Warnings: 0

  16. mysql> update worker set gender='M' where name='赵六';

  17. Query OK, 1 row affected (0.00 sec)

  18. Rows matched: 1 Changed: 1 Warnings: 0

  19. mysql> update worker set gender='M' where name='钱七';

  20. Query OK, 1 row affected (0.01 sec)

  21. Rows matched: 1 Changed: 1 Warnings: 0

  22. mysql> update worker set gender='M' where name='孙八';

  23. Query OK, 1 row affected (0.00 sec)

  24. Rows matched: 1 Changed: 1 Warnings: 0

查看表内容

 最后差询女职工的年龄

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

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

Empty set (0.00 sec)  没有符合条件的职工,所以为空

10,列出工资在1000-2000之间的所有职工姓名

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

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

 13,将职工表worker中的职工按先后顺序排序

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

 15,求出个部门党员的人数

16,统计个部门的工资和平均工资

 

17,统计总人数大于4的部门号和总人数

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐