总起:常用高级sql回忆总结。(写了一年的前端,得练练手额)
join:
MySQL 数据库不支持 SELECT … INTO 语句,但支持 INSERT INTO … SELECT 。
1,查找最晚入职员工的所有信息,为了减轻入门难度,目前所有的数据里员工入职的日期都不是同一天
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL comment '员工编号',
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
mysql:
指定按hire_date降序desc,并取第一行。 (默认升序)
select * from employees order by hire_date desc limit 0,1
考虑最晚入职可能有很多:
select * from employees where hire_date =(select max(hire_date) from employees)
2,查找入职员工时间排名倒数第三的员工所有信息,为了减轻入门难度,目前所有的数据里员工入职的日期都不是同一天
从第二个开始取1个:
select * from employees order by hire_date desc limit 2,1
3,查找各个部门当前(dept_manager.to_date=‘9999-01-01’)领导当前(salaries.to_date=‘9999-01-01’)薪水详情以及其对应部门编号dept_no(请注意输出结果,dept_no列是最后一列)
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL comment '部门编号',
`emp_no` int(11) NOT NULL comment '员工编号',
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL comment '员工编号',
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
条件关联:
select s.*,d.dept_no from salaries s,dept_manager d
where d.emp_no = s.emp_no
and d.to_date='9999-01-01'
and s.to_date='9999-01-01'
jion关联:
select s.*,d.dept_no from salaries s left join dept_manager d
where d.emp_no = s.emp_no
and d.to_date='9999-01-01'
and s.to_date='9999-01-01'
查找所有已经分配部门的员工的last_name和first_name以及dept_no(请注意输出描述里各个列的前后顺序)
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
条件关联:
select e.last_name,e.first_name,d.dept_no
from employees e,dept_emp d
where e.emp_no = d.emp_no
join关联:(两个要都有,不能用left right join等。)
select e.last_name,e.first_name,d.dept_no from
employees e inner join dept_emp d on e.emp_no=d.emp_no
查找所有员工的last_name和first_name以及对应部门编号dept_no,也包括暂时没有分配具体部门的员工(请注意输出描述里各个列的前后顺序):
select e.last_name,e.first_name,d.dept_no from employees e left join dept_emp d
on e.emp_no = d.emp_no
查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_no进行逆序(请注意,一个员工可能有多次涨薪的情况)
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
显然这是一对多的情况,解决方案两种:1,找到hire_date时给的工资;2,找到最小时间给的工资。
1,找到hire_date时给的工资:
select s.emp_no,s.salary from employees e left join salaries s
on e.emp_no = s.emp_no
where e.hire_date = s.from_date
order by s.emp_no desc
2,找到最小时间给的工资:(不需要用到employees表,典型一对多处理)
一对多的分组emp_no有多个salaries。根据emp_no分组,取最小值,再根据emp_no排序。
select emp_no,salary from salaries
group by emp_no having min(from_date)
order by emp_no desc
查找薪水变动超过15次的员工号emp_no以及其对应的变动次数t。
select distinct emp_no, count(emp_no) as t from salaries
group by emp_no having count(emp_no) > 15
找出所有员工当前(to_date=‘9999-01-01’)具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示.
select distinct salary from salaries
where to_date='9999-01-01'
order by salary desc
获取所有部门当前(dept_manager.to_date=‘9999-01-01’)manager的当前(salaries.to_date=‘9999-01-01’)薪水情况,给出dept_no, emp_no以及salary(请注意,同一个人可能有多条薪水情况记录)
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
去重即可。
select distinct d.dept_no,s.emp_no,s.salary from dept_manager d,salaries s
where d.emp_no = s.emp_no and d.to_date='9999-01-01'
and s.to_date='9999-01-01'
获取所有非manager的员工emp_no
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
思路还是最开始的图。简单来说,就是集合处理。
select e.emp_no from employees e left join dept_manager d
on d.emp_no = e.emp_no
where d.emp_no is null
获取所有员工当前的(dept_manager.to_date=‘9999-01-01’)manager,如果员工是manager的话不显示(也就是如果当前的manager是自己的话结果不显示)。输出结果第一列给出当前员工的emp_no,第二列给出其manager对应的emp_no。(难理解对应关系)
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL comment '所有的员工编号',
`dept_no` char(4) NOT NULL comment '部门编号',
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL comment '部门编号',
`emp_no` int(11) NOT NULL comment '经理编号',
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
考虑了下找到不到一种join能做到。那就条件关联吧。、
员工 <--n:1-->部门<--n:1>部门经理
条件,当前员工的经理,所以在相同部门:de.dept_no = dm.dept_no
在相同部门前提下,区分当前员工的当前经理的条件,de.emp_no <> dm.emp_no
select de.emp_no as emp_no,dm.emp_no as manager_no
from dept_emp de,dept_manager dm
where de.dept_no = dm.dept_no
and de.emp_no <> dm.emp_no
and dm.to_date='9999-01-01'
获取所有部门中当前(dept_emp.to_date = ‘9999-01-01’)员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
聚合函数:
select de.dept_no,de.emp_no,sa.salary from dept_emp de left join salaries sa
on de.emp_no = sa.emp_no
where de.to_date = '9999-01-01'
group by de.dept_no
having max(sa.salary)
从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。
CREATE TABLE IF NOT EXISTS "titles" (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);
select title, count(*) as t from titles
group by title having count(title) >= 2
或者
select title, count(*) as t from titles
group by title having t >= 2
从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。
注意对于重复的emp_no进行忽略(即emp_no重复的title不计算,title对应的数目t不增加)。
CREATE TABLE IF NOT EXISTS `titles` (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);
COUNT(DISTINCT emp_no)可以统计同一title值且不包含重复emp_no值的记录条数:
select title, count(distinct emp_no) as t from titles
group by title having t >= 2
查找employees表所有emp_no为奇数,且last_name不为Mary(注意大小写)的员工信息,并按照hire_date逆序排列(题目不能使用mod函数)
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
题目不能使用mod函数:
select * from employees where last_name <> 'Mary'
and emp_no % 2 = 1 order by hire_date desc
统计出当前各个title类型对应的员工当前(to_date=‘9999-01-01’)薪水对应的平均工资。结果给出title以及平均工资avg。
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
CREATE TABLE IF NOT EXISTS "titles" (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);
题目要读请,当前各个title类型对应的员工当前薪水。这是两个条件。
select title,avg(s.salary) as avg from salaries s,titles t
where s.emp_no = t.emp_no
and s.to_date='9999-01-01'
and t.to_date='9999-01-01'
group by t.title
获取当前(to_date=‘9999-01-01’)薪水第二多的员工的emp_no以及其对应的薪水salary
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
从第一个开始取,取一个。
select emp_no, salary from salaries
where to_date='9999-01-01'
order by salary desc limit 1,1
查找当前薪水(to_date=‘9999-01-01’)排名第二多的员工编号emp_no、薪水salary、last_name以及first_name,你可以不使用order by完成吗
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
不能用order,用not in,也可以用操作子集等方案。
select e.emp_no,max(s.salary) as salary,e.last_name,e.first_name from
employees e left join salaries s on e.emp_no = s.emp_no
where s.to_date='9999-01-01'
and s.salary not in
(select max(salary) from salaries where to_date='9999-01-01')
查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工
CREATE TABLE `departments` (
`dept_no` char(4) NOT NULL,
`dept_name` varchar(40) NOT NULL,
PRIMARY KEY (`dept_no`));
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
三表简单关联:
select e.last_name,e.first_name,dt.dept_name from
employees e left join dept_emp de on e.emp_no = de.emp_no
left join departments dt on dt.dept_no = de.dept_no
查找员工编号emp_no为10001其自入职以来的薪水salary涨幅(总共涨了多少)growth(可能有多次涨薪,没有降薪)
CREATE TABLE salaries
(
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
由于没有降薪:
select (max(salary) - min(salary)) as growth from salaries
where emp_no = '10001'
如果有降薪:一个降序,一个升序即可
select ((select salary from salaries where emp_no = 10001 order by salary desc limit 0,1) -
(select salary from salaries where emp_no = 10001 order by salary asc limit 0,1)) as growth
查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_no以及其对应的薪水涨幅growth,并按照growth进行升序
(注:可能有employees表和salaries表里存在记录的员工,有对应的员工编号和涨薪记录,但是已经离职了,离职的员工salaries表的最新的to_date!=‘9999-01-01’,这样的数据不显示在查找结果里面)
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL comment '入职时间',
PRIMARY KEY (`emp_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL comment '一条薪水记录开始时间',
`to_date` date NOT NULL comment '一条薪水记录结束时间',
PRIMARY KEY (`emp_no`,`from_date`));
涨幅等=当前工资 - 初始工资。
select scur.emp_no,(scur.salary - sinit.salary) as growth from
/* 当前工资 */
(select s.emp_no,s.salary from salaries s left join employees e on s.emp_no = e.emp_no
where s.to_date ='9999-01-01') as scur
inner join
/* 初始工资 */
(select s.emp_no,s.salary from salaries s left join employees e on s.emp_no = e.emp_no
where s.from_date = e.hire_date) as sinit
on scur.emp_no = sinit.emp_no
order by growth
统计各个部门的工资记录数,给出部门编码dept_no、部门名称dept_name以及次数sum
CREATE TABLE `departments` (
`dept_no` char(4) NOT NULL,
`dept_name` varchar(40) NOT NULL,
PRIMARY KEY (`dept_no`));
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
题目条件给的不算完整:
select dt.dept_no,dt.dept_name,count(sa.salary) as sum
from departments dt,dept_emp de,salaries sa
where dt.dept_no = de.dept_no
and de.emp_no = sa.emp_no
group by dt.dept_no
对所有员工的当前(to_date=‘9999-01-01’)薪水按照salary进行按照1-N的排名,相同salary并列且按照emp_no升序排列
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
这个提用存储过程最简单。这里不能用。就只能采用复用表。一张表去排序,一张表取统计rank
SELECT s1.emp_no, s1.salary, COUNT(DISTINCT s2.salary) AS rank
FROM salaries AS s1, salaries AS s2
WHERE s1.to_date = '9999-01-01' AND s2.to_date = '9999-01-01' AND s1.salary <= s2.salary
GROUP BY s1.emp_no
ORDER BY s1.salary DESC, s1.emp_no
获取所有非manager员工当前的薪水情况,给出dept_no、emp_no以及salary ,当前表示to_date=‘9999-01-01’
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
not in 过滤掉非manager:
SELECT de.dept_no, e.emp_no, s.salary
FROM employees AS e INNER JOIN dept_emp AS de ON e.emp_no = de.emp_no
INNER JOIN salaries AS s ON e.emp_no = s.emp_no
WHERE s.to_date='9999-01-01'
AND e.emp_no NOT IN(
SELECT emp_no FROM dept_manager WHERE to_date='9999-01-01'
);
获取员工其当前的薪水比其manager当前薪水还高的相关信息,当前表示to_date=‘9999-01-01’,
结果第一列给出员工的emp_no,
第二列给出其manager的manager_no,
第三列给出该员工当前的薪水emp_salary,
第四列给该员工对应的manager当前的薪水manager_salary
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
复杂问题简单分解再根据条件组合
/*复杂场景采用分合思想*/
SELECT e.emp_no AS emp_no, m.emp_no AS manager_no,
e.salary AS emp_salary, m.salary AS manager_salary
/*1、当前员工的工号、部门号、工资*/
FROM (SELECT s.salary, s.emp_no, de.dept_no FROM salaries s INNER JOIN dept_emp de
ON s.emp_no = de.emp_no AND s.to_date = '9999-01-01' ) AS e,
/*2、当前manager的工号、部门号、工资*/
(SELECT s.salary, s.emp_no, dm.dept_no FROM salaries s INNER JOIN dept_manager dm
ON s.emp_no = dm.emp_no AND s.to_date = '9999-01-01' ) AS m
/*3、部门匹配、条件匹配*/
WHERE e.dept_no = m.dept_no AND e.salary > m.salary
给出每个员工每年薪水涨幅超过5000的员工编号emp_no、薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列。
提示:在sqlite中获取datetime时间对应的年份函数为strftime(’%Y’, to_date)
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
条件复用处理。
/*重点:复用和时间条件匹配*/
select s1.emp_no,s1.from_date,(s1.salary - s2.salary) as salary_growth from
salaries s1,salaries s2
where s1.emp_no = s2.emp_no
and s1.salary - s2.salary > 5000
and strftime('%Y', s1.to_date) - strftime('%Y', s2.to_date) = 1
order by salary_growth desc
获取select * from employees对应的执行计划
/*mysql*/
explain select * from employees
/*oracle*/
explain plan select * from employees
针对如下表actor结构创建索引:
(注:在 SQLite 中,除了重命名表和在已有的表中添加列,ALTER TABLE 命令不支持其他操作)
CREATE TABLE IF NOT EXISTS actor (
actor_id smallint(5) NOT NULL PRIMARY KEY,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
last_update timestamp NOT NULL DEFAULT (datetime('now','localtime')))
对first_name创建唯一索引uniq_idx_firstname,对last_name创建普通索引idx_lastname
create unique index uniq_idx_firstname on actor(first_name);
create index idx_lastname on actor(last_name);
针对actor表创建视图actor_name_view,只包含first_name以及last_name两列,并对这两列重新命名,first_name为first_name_v,last_name修改为last_name_v:
CREATE TABLE IF NOT EXISTS actor (
actor_id smallint(5) NOT NULL PRIMARY KEY,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
last_update timestamp NOT NULL DEFAULT (datetime('now','localtime')))
create view actor_name_view (first_name_v,last_name_v) as
select first_name,last_name from actor
针对salaries表emp_no字段创建索引idx_emp_no,查询emp_no为10005, 使用强制索引。
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
create index idx_emp_no on salaries(emp_no);
/*mysql
SELECT * FROM salaries FORCE INDEX idx_emp_no WHERE emp_no = 10005;
*/
/*sqllitte*/
SELECT * FROM salaries INDEXED BY idx_emp_no WHERE emp_no = 10005;
存在actor表,包含如下列信息:
CREATE TABLE IF NOT EXISTS actor (
actor_id smallint(5) NOT NULL PRIMARY KEY,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
last_update timestamp NOT NULL DEFAULT (datetime('now','localtime')));
现在在last_update后面新增加一列名字为create_date, 类型为datetime, NOT NULL,默认值为’0000-00-00 00:00:00’
alter table actor add 'create_date' datetime not null default '0000-00-00 00:00:00'
构造一个触发器audit_log,在向employees_test表中插入一条数据的时候,触发插入相关的数据到audit中。
CREATE TABLE employees_test(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
CREATE TABLE audit(
EMP_no INT NOT NULL,
NAME TEXT NOT NULL
);
/*after/before为触发器触发条件*/
create trigger audit_log after insert on employees_test
begin
insert into audit values(new.id,new.name);
end;
删除emp_no重复的记录,只保留最小的id对应的记录。
CREATE TABLE IF NOT EXISTS titles_test (
id int(11) not null primary key,
emp_no int(11) NOT NULL,
title varchar(50) NOT NULL,
from_date date NOT NULL,
to_date date DEFAULT NULL);
/*思路选出最小,将不是最小全部删除*/
delete from titles_test where id not in
(select min(id) from titles_test group by emp_no)
将titles_test表名修改为titles_2017。
alter table titles_test rename to titles_2017
/*
mysql:rename sqllite rename to
alter table titles_test rename titles_2017
*/
将所有获取奖金的员工当前的薪水增加10%。
create table emp_bonus(
emp_no int not null,
recevied datetime not null,
btype smallint not null);
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL, PRIMARY KEY (`emp_no`,`from_date`));
利用in:
update salaries set salary = salary*1.1
where emp_no in (select emp_no from emp_bonus)