创建一个job数据库,并在此数据库里创建departments(部门表)、employees(雇员表)、salaries(工资表)、dept_emp(雇员与部门关系表)
要求:departments表中含有dept_no(部门编码)、dept_name(部门名称)两个字段,其中,dept_no 为定长字符型,长度为4,dept_name 为不定长字符型,长度为40,不能为空,主键为dept_no,唯一键为dept_name。
create table departments(
dept_no char(4) primary key ,
dept_name varchar(40) not null unique
);
employees表中含有emp_no(雇员ID)、birth_date(雇员生日)、name(雇员名字)、hire_date(入住时间),其中,emp_no 为整型,birth_date和hire_date均为日期类型,不能为空,name为不定长字符型,长度为14,不能为空。emp_no含有主键。
create table employees(
emp_no int primary key,
birth_date datetime not null,
name varchar(14) not null,
hire_date datetime not null
);
salaries表中含有emp_no(雇员ID),salary(工资)、month(月份)、level(工资等级),字段均为整型且不能为空,其中当前表中的emp_no存在外键关联到employees表中的emp_no字段。
create table salaries(
emp_no int not null,
salary int not null,
month int not null,
level int not null,
constraint sala_empl foreign key (emp_no) references employees(emp_no)
);
dept_emp表中含有dept_no(部门编码)、emp_no(雇员ID),其中,dept_no 为定长字符型,长度为4,emp_no 为整型。且均不能为空。当前表中的emp_no存在外键关联到employees表中的emp_no字段。当前表中的dept_no存在外键关联到departments表中的dept_no字段。主键为dept_no和emp_no。
create table dept_emp(
dept_no char(4) not null,
emp_no int not null,
primary key(dept_no,emp_no),
constraint dep_emp_fk foreign key (emp_no) references employees(emp_no),
constraint dep_depar_fk foreign key (dept_no) references departments(dept_no)
);
向departments表中插入以下几条数据:插入字段顺序为:dept_no,dept_name
('1001','A'), ('1002','B'), ('1003','C'), ('1004','D')
insert into departments(dept_no,dept_name) values('1001','A'),('1002','B'),('1003','C'),('1004','D')
向employees表中插入以下几条数据:插入字段顺序为:emp_no,birth_date,name,hire_date
('100','1990-08-19','JACK','20160811'),('101','1970-08-12','TOM','20100606'),('102','1996-03-19','JAMES','20140101'), ('103','1987-04-28','KETTY','20130910'), ('104','1983-05-19','JIM','20160418');
insert into employees(emp_no,birth_date,name,hire_date) values('100','1990-08-19','JACK','20160811'),('101','1970-08-12','TOM','20100606'),('102','1996-03-19','JAMES','20140101'), ('103','1987-04-28','KETTY','20130910'), ('104','1983-05-19','JIM','20160418');
向salaries表中插入以下几条数据:插入字段顺序为:emp_no,salary,month,level
('100','12000','201601',2),('101','9000','201601',1),('102','90000','201601',10),('103','2300','201601',1),('104','4000','201601',1),('100','12000','201602',2),('101','9000','201602',1),('102','90000','201602',10),('103','2300','201602',1),('104','4000','201602',1),('100','12000','201603',2),('101','9000','201603',1),('102','90000','201603',10),('103','2300','201603',1),('104','4000','201603',1),('100','12000','201604',2),('101','9000','201604',1),('102','90000','201604',10),('103','2300','201604',1),('104','4000','201604',1);
insert into salaries(emp_no,salary,month,level) values('100','12000','201601',2),('101','9000','201601',1),('102','90000','201601',10),('103','2300','201601',1),('104','4000','201601',1),('100','12000','201602',2),('101','9000','201602',1),('102','90000','201602',10),('103','2300','201602',1),('104','4000','201602',1),('100','12000','201603',2),('101','9000','201603',1),('102','90000','201603',10),('103','2300','201603',1),('104','4000','201603',1),('100','12000','201604',2),('101','9000','201604',1),('102','90000','201604',10),('103','2300','201604',1),('104','4000','201604',1);
向dept_emp表中插入以下几条数据:插入字段顺序为:emp_no,dept_no
('100','1001'), ('101','1001'), ('102','1002'), ('103','1003'), ('104','1004');
insert into dept_emp(emp_no,dept_no) values('100','1001'), ('101','1001'), ('102','1002'), ('103','1003'), ('104','1004');
分别查看部门表和员工表中的所有记录
select * from departments;
select * from employees;
将雇员表按照出生日期进行降序排列,并找到前三条数据
select name,birth_date from employees order by birth_date desc limit 3;
查询2016年1月份的工资平均值
select avg(salary) from salaries where month=201601;
查询雇员ID小于103且名字以J开头的员工信息
select * from employees where emp_no<103 and name like "J%";
查询雇员ID在101-103之间的员工信息(两种方式)
select * from employees where emp_no>=101 and emp_no<=103;
select * from employees where emp_no between 101 and 103;
查询所有名字以M结尾的雇员信息,并按照入职时间进行倒叙排序
select * from employees where name LIKE "%M" order by hire_date desc;
查询每个员工所属的部门
select name,departments.dept_no,departments.dept_name
from employees,departments,dept_emp
where dept_emp.dept_no=departments.dept_no and employees.emp_no=dept_emp.emp_no;
select d1.dept_name,e.name from employees e LEFT JOIN dept_emp d on e.emp_no=d.emp_no LEFT JOIN departments d1 on d.dept_no=d1.dept_no;
查询属于A部门的员工
select *
from employees,departments,dept_emp
where dept_emp.dept_no=departments.dept_no and employees.emp_no=dept_emp.emp_no and departments.dept_name='A';
select * from employees e LEFT JOIN dept_emp d ON e.emp_no=d.emp_no INNER JOIN departments d1 ON d.dept_no=d1.dept_no AND d1.dept_name='A';
查询属于A部门员工的平均工资
select avg(salary)
from employees,departments,dept_emp,salaries
where dept_emp.dept_no=departments.dept_no and employees.emp_no=dept_emp.emp_no and departments.dept_name='A';
select avg(s.salary) from employees e LEFT JOIN dept_emp d ON e.emp_no=d.emp_no INNER JOIN departments d1 ON d.dept_no=d1.dept_no AND d1.dept_name='A' INNER JOIN salaries s on e.emp_no=s.emp_no;
查询所有员工的薪水总和
select sum(salary) from salaries;
select SUM(s.salary) from employees e LEFT JOIN dept_emp d ON e.emp_no=d.emp_no INNER JOIN departments d1 ON d.dept_no=d1.dept_no INNER JOIN salaries s ON s.emp_no=e.emp_no;
或者
select SUM(salary) from salaries;
查询各个部门的员工平均工资
select avg(salary),departments.dept_name
from employees,departments,dept_emp,salaries
where dept_emp.dept_no=departments.dept_no and employees.emp_no=dept_emp.emp_no and employees.emp_no=salaries.emp_no group by departments.dept_name;
![]()
select AVG(s.salary) from departments d LEFT JOIN dept_emp d1 ON d.dept_no=d1.dept_no LEFT JOIN salaries s ON d1.emp_no=s.emp_no GROUP BY d.dept_name;
外键约束的几种方式及区别
1. CASCADE: 从父表中删除或更新对应的行,同时自动的删除或更新自表中匹配的行。ON DELETE CANSCADE和ON UPDATE CANSCADE都被InnoDB所支持。
2. SET NULL: 从父表中删除或更新对应的行,同时将子表中的外键列设为空。注意,这些在外键列没有被设为NOT NULL时才有效。ON DELETE SET NULL和ON UPDATE SET SET NULL都被InnoDB所支持。
3. NO ACTION: InnoDB拒绝删除或者更新父表。
4. RESTRICT: 拒绝删除或者更新父表。指定RESTRICT(或者NO ACTION)和忽略ON DELETE或者ON UPDATE选项的效果是一样的。
5. SET DEFAULT: InnoDB目前不支持。
-- 第一种方式
create table t1(
id int primary key auto_increment,
name VARCHAR(20) not null
);
create table t2(
id int primary key auto_increment,
name VARCHAR(20) not null,
t1_id int,
CONSTRAINT t1_t2_fk FOREIGN KEY(t1_id) REFERENCES t1(id)
);
-- 第二种方式
create table t3(
id int primary key auto_increment,
name VARCHAR(20) not null
);
create table t4(
id int primary key auto_increment,
name VARCHAR(20) not null,
t3_id int
);
ALTER TABLE t4 add CONSTRAINT t4_t3_fk FOREIGN key(t3_id) REFERENCES t3(id);