41.
insert into titles_test values
(‘1’, ‘10001’, ‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’),
(‘2’, ‘10002’, ‘Staff’, ‘1996-08-03’, ‘9999-01-01’),
(‘3’, ‘10003’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’),
(‘4’, ‘10004’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’),
(‘5’, ‘10001’, ‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’),
(‘6’, ‘10002’, ‘Staff’, ‘1996-08-03’, ‘9999-01-01’),
(‘7’, ‘10003’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’);
update titles_test
set to_date=NULL,from_date='2001-01-01'
where to_date='9999-01-01'
42.
insert into titles_test values
(‘1’, ‘10001’, ‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’),
(‘2’, ‘10002’, ‘Staff’, ‘1996-08-03’, ‘9999-01-01’),
(‘3’, ‘10003’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’),
(‘4’, ‘10004’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’),
(‘5’, ‘10001’, ‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’),
(‘6’, ‘10002’, ‘Staff’, ‘1996-08-03’, ‘9999-01-01’),
(‘7’, ‘10003’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’);
replace into titles_test(id,emp_no,title,from_date,to_date)
values(5,10005,'Senior Engineer','1986-06-26','9999-01-01')
方法二:运用REPLACE(X,Y,Z)函数。其中X是要处理的字符串,Y是X中将要被替换的字符串,Z是用来替换Y的字符串,最终返回替换后的字符串。以下语句用 UPDATE和REPLACE 配合完成,用REPLACE函数替换后的新值复制给 id=5 的 emp_no。REPLACE的参数为整型时也可通过。
UPDATE titles_test
SET emp_no = REPLACE(emp_no,10001,10005)
WHERE id = 5
43.
insert into titles_test values
(‘1’, ‘10001’, ‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’),
(‘2’, ‘10002’, ‘Staff’, ‘1996-08-03’, ‘9999-01-01’),
(‘3’, ‘10003’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’),
(‘4’, ‘10004’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’),
(‘5’, ‘10001’, ‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’),
08-03’, ‘9999-01-01’),
(‘7’, ‘10003’, ‘Senior Engineer’, ‘1995-12-03’, ‘9999-01-01’);
alter table titles_test rename to titles_2017
44.
CREATE TABLE audit(
EMP_no INT NOT NULL,
create_date datetime NOT NULL
);
DROP TABLE audit;
CREATE TABLE audit(
EMP_no INT NOT NULL,
create_date datetime NOT NULL,
FOREIGN KEY(EMP_no) REFERENCES employees_test(ID));
45.
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, emp_no
)); select employees.*
from employees,emp_v
where employees.emp_no=emp_v.emp_no
46.
题目描述
将所有获取奖金的员工当前的薪水增加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
));
题目解答
要先选出符合条件的 emp_no,即用 INNER JOIN 连接 salaries 和 emp_bonus,且用 s.to_date = ‘9999-01-01’ 表示当前薪水,然后再用 UPDATE … SET … WHERE … IN … 语句来更新表中数据。
UPDATE salaries SET salary = salary * 1.1
WHERE emp_no IN
(SELECT s.emp_no FROM salaries AS s INNER JOIN emp_bonus AS eb
ON s.emp_no = eb.emp_no AND s.to_date = '9999-01-01')
47.
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, emp_no
)); dept_emp
( emp_no
int(11) NOT NULL, dept_no
char(4) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,dept_no
)); dept_manager
( dept_no
char(4) NOT NULL, emp_no
int(11) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,dept_no
)); salaries
( emp_no
int(11) NOT NULL, salary
int(11) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,from_date
)); SELECT "select count(*) from " || name || ";" AS cnts
FROM sqlite_master WHERE type = 'table'
48.
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, emp_no
)); SELECT last_name || "'" || first_name FROM employees
49.
sELECT (length("10,A,B")-length(replace("10,A,B",",","")))/length(",") AS cnt
50.
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, emp_no
)); select first_name
from employees
ORDER BY substr(first_name,-2)