SQL练习题-牛客网

Posted RRRR-cord

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL练习题-牛客网相关的知识,希望对你有一定的参考价值。

牛客网在线编程网址:https://www.nowcoder.com/activity/oj

(默认使用SQLite)

 

题目1:

删除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);
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练习题-牛客网的主要内容,如果未能解决你的问题,请参考以下文章

牛客网sql练习

数据库练习题(牛客网sql入门篇部分)

SQL日常练习2-进阶篇-牛客网

SQL日常练习-牛客网

SQL日常练习1-基础篇-牛客网

SQL日常练习2-进阶篇-牛客网