首页 > 数据库技术 > 详细

SQL练习题(5)-牛客网

时间:2017-11-12 10:49:44      阅读:484      评论:0      收藏:0      [点我收藏+]

牛客网在线编程网址: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练习题(5)-牛客网

原文:http://www.cnblogs.com/RRRRecord/p/7820517.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!