码迷,mamicode.com
首页 > 数据库 > 详细

SQL练习题(5)-牛客网

时间:2017-11-12 11:11:28      阅读:177      评论:0      收藏:0      [点我收藏+]

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

牛客网在线编程网址: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)-牛客网

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!