当前位置:Gxlcms > 数据库问题 > SQL练习题(5)-牛客网

SQL练习题(5)-牛客网

时间:2021-07-01 10:21:17 帮助过:3人阅读

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);
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);
delete from titles_test
where id not in
(
    select min(id) from titles_test group by emp_no
);

 

题目2:

将所有to_date为9999-01-01的全部更新为NULL,且 from_date更新为2001-01-01。

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);
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;

 

题目3:

将id=5以及emp_no=10001的行数据替换成id=5以及emp_no=10005,其他数据保持不变,使用replace实现。

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 emp_no = replace(emp_no, 10001, 10005) where id = 5;

http://sqlite.org/lang_corefunc.html#replace

replace(X,Y,Z)

The replace(X,Y,Z) function returns a string formed by substituting string Z for every occurrence of string Y in string X. The BINARY collating sequence is used for comparisons. If Y is an empty string then return X unchanged. If Z is not initially a string, it is cast to a UTF-8 string prior to processing.

 

题目4:

将titles_test表名修改为titles_2017。

ALTER table titles_test rename to titles_2017

 

题目5:

在audit表上创建外键约束,其emp_no对应employees_test表的主键id。

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,
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)
);

如果是在mysql中,可以直接用

Alter table audit add foreign key(emp_no) references employees_test(id);

见w3school.

SQL练习题(5)-牛客网

标签:html   eal   sqli   text   org   blog   修改   varchar   span   

人气教程排行