作业要求:
作业实现:
首先,创建worker表并插入相关数据
CREATE TABLE `worker` (
`部门号` int(11) NOT NULL,
`职工号` int(11) NOT NULL,
`工作时间` date NOT NULL,
`工资` float(8,2) NOT NULL,
`政治面貌` varchar(10) NOT NULL DEFAULT '群众',
`姓名` varchar(20) NOT NULL,
`出生日期` date NOT NULL,
PRIMARY KEY (`职工号`))
ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
INSERT INTO `worker` VALUES (101, 1001, '2015-5-4', 3500.00, '群众', '张三', '1990-7-1');
INSERT INTO `worker` VALUES (101, 1002, '2017-2-6', 3200.00, '团员', '李四', '1997-2-8');
INSERT INTO `worker` VALUES (102, 1003, '2011-1-4', 8500.00, '党员', '王亮', '1983-6-8');
INSERT INTO `worker` VALUES (102, 1004, '2016-10-10', 5500.00, '群众', '赵六', '1994-9-5');
INSERT INTO `worker` VALUES (102, 1005, '2014-4-1', 4800.00, '党员', '钱七', '1992-12-30');
INSERT INTO `worker` VALUES (102, 1006, '2017-5-5', 4500.00, '党员', '孙八', '1996-9-2');
1、显示所有职工的基本信息。
select * from worker;
select distinct `部门号` from worker;
select count(姓名) from worker;
select max(工资) as '最高工资',min(工资) as '最低工资' from worker;
select avg(工资) as '平均工资', sum(工资) as '总工资' from worker;
6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。
create table `workdate` select `职工号`, `姓名` from worker;
select * from workdate;
7、显示所有女职工的年龄。(因为worker表中目前没有年龄以及性别属性,所以需先增加年龄性别属性,并插入年龄性别的数据后再进行查询)
*增加年龄性别属性后插入数据时报错了,这里原因是因为版本较新的MySQL是在safe-updates模式下进行sql操作的,这个模式会导致在非主键条件下无法执行update或者delete 修改方式。这里用的解决方式是在执行update前输入 set sql_safe_updates=0命令。
alter table worker add `年龄` tinyint default 18;
alter table worker add `性别` char(1) check(`性别` in ('男', '女'));
set sql_safe_updates=0;
update worker set `年龄`=34,`性别`='男' where `姓名`='张三';
update worker set `年龄`=27,`性别`='女' where `姓名`='李四';
update worker set `年龄`=41,`性别`='女' where `姓名`='王亮';
update worker set `年龄`=30,`性别`='男' where `姓名`='赵六';
update worker set `年龄`=32,`性别`='女' where `姓名`='钱七';
update worker set `年龄`=28,`性别`='女' where `姓名`='孙八';
select `年龄` from worker where `性别` = '女';
select `职工号`, `姓名`, `出生日期` from worker where `姓名` = '刘%';
select `姓名`, `工作时间` from worker where `出生日期` < '1960-1-1';
select `姓名` from worker where `工资` between 1000 and 2000;
select `姓名` from worker where `姓名` = '陈%' or `姓名` = '李%';
select `职工号`, `姓名` from worker where `政治面貌` != '党员' and `部门号` in (102, 103);
select `姓名`, `出生日期` from worker order by(出生日期);
select `职工号`, `姓名` from worker order by(工资) desc limit 0, 3;
select `部门号`, count(姓名) from worker where `政治面貌` = '党员' group by`部门号`;
select `部门号`, sum(工资),avg(工资) from worker group by`部门号`;
select `部门号`, count(姓名) from worker group by(部门号) having count(姓名) > 4;