首先,由上图创建表
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;
Query OK, 0 rows affected, 4 warnings (0.03 sec)
然后,向表内插入数据
mysql> insert into worker values(101,1001,'2015-5-4', 3500.00,'群众','张三','1990-7-1');
Query OK, 1 row affected (0.00 sec)mysql> insert into worker values(101,1002,'2017-2-6', 3200.00,'团员','李四','1990-2-8');
Query OK, 1 row affected (0.01 sec)mysql> insert into worker values(102,1003,'2011-2-4',8500.00,'党员','王亮','1983-6-8');
Query OK, 1 row affected (0.01 sec)mysql> insert into worker values(102,1004,'2016-10-10',5500.00,'群众','赵六','1994-9-5');
Query OK, 1 row affected (0.00 sec)mysql> insert into worker values(102,1005,'2014-4-1',4800.00,'党员','钱七','1992-12-30');
Query OK, 1 row affected (0.00 sec)mysql> insert into worker values(102,1006,'2017-5-5',4500.00,'党员','孙八','1996-9-2');
Query OK, 1 row affected (0.00 sec)
1,显示所有职工的基本信息
2,查询所有职工所属部门的部门号,不显示重复的部门号
3,求出所有职工的人数
4,列入最高工和最低工资
5,列入职工的平均工资和总工资
6,创建一个只有职工号,姓名和参加工作的新表,名为工作日期表
mysql> create table workdate select emp_id,name from worker;
Query OK, 6 rows affected (0.02 sec)
Records: 6 Duplicates: 0 Warnings: 0mysql> select *from workdate;
+--------+--------+
| emp_id | name |
+--------+--------+
| 1001 | 张三 |
| 1002 | 李四 |
| 1003 | 王亮 |
| 1004 | 赵六 |
| 1005 | 钱七 |
| 1006 | 孙八 |
+--------+--------+
6 rows in set (0.00 sec)
7,显示所有女职工的年龄(要给表中增加性别和年龄,然后给每人插入年龄和性别)
mysql> alter table worker add age tinyint default 18;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> alter table worker add gender char(1) check(gender in ('F','M'));
Query OK, 6 rows affected (0.04 sec)
Records: 6 Duplicates: 0 Warnings: 0mysql> update worker set age=19,gender='F' where name='张三';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> update worker set age=20,gender='F' where name='李四';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> update worker set age=21,gender='M' where name='王亮';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> update worker set gender='M' where name='赵六';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> update worker set gender='M' where name='钱七';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> update worker set gender='M' where name='孙八';
Query OK, 1 row affected (0.00 sec)
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的部门号和总人数