欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

荐 牛客网sql练习题解(43-51)

程序员文章站 2022-05-07 17:39:45
文章目录简介NO.43NO.44NO.45NO.46NO.47NO.48NO.49NO.50NO.51简介往期文章:牛客网sql练习题解 (1-11)牛客网sql练习题解(12-21)牛客网sql练习题解(22-32)牛客网sql练习题解(34-42)他来了他来了,他带着sql走来了。这次的知识点涵盖update用法replace函数replace into修改表名外键intersect用法下面我们直接上题目。NO.43update titles_test set t...

简介

往期文章:
牛客网sql练习题解 (1-11)
牛客网sql练习题解(12-21)
牛客网sql练习题解(22-32)
牛客网sql练习题解(34-42)

他来了他来了,他带着sql走来了。

这次的知识点涵盖

  • update用法
  • replace函数
  • replace into
  • 修改表名
  • 外键
  • intersect用法

下面我们直接上题目。

NO.43

荐
                                                        牛客网sql练习题解(43-51)

update titles_test set to_date=null, from_date='2001-01-01' where to_date='9999-01-01';

荐
                                                        牛客网sql练习题解(43-51)

NO.44

荐
                                                        牛客网sql练习题解(43-51)
这题考察replace into的用法.
使用replace两种方法,如果直接使用REPLACE INTO的话,就需要重新插入一条完整的新纪录,sql会自动代替id相同的记录;也可以使用UPDATE结合,REPLACE(colname,oldval,newval)来赋值给colnamejik

replace into titles_test values(5, 10005,  'Senior Engineer', '1986-06-26', '9999-01-01');

荐
                                                        牛客网sql练习题解(43-51)
或者

replace into titles_test
select 5, 10005, title, from_date, to_date
from titles_test
where id=5;

NO.45

荐
                                                        牛客网sql练习题解(43-51)

alter table titles_test rename to titles_2017;

荐
                                                        牛客网sql练习题解(43-51)

NO.46

荐
                                                        牛客网sql练习题解(43-51)

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

这种写法肯定是没有问题的啊,但是就是过不了,然后看了看其他同学的答案,原来OJ的问题。

然后写法二,这种写法我觉也就是为了过题 吧,真正用的话没人先删表。

drop table audit;

create table audit(
    emp_no int not null,
    create_date datetime not null,
    foreign key(emp_no) references employees_test(id)
);

荐
                                                        牛客网sql练习题解(43-51)

NO.47

荐
                                                        牛客网sql练习题解(43-51)

这题我真的没看懂,凭我的理解写了第一个答案:

select * from emp_v; 

骂街了,他在干嘛。

荐
                                                        牛客网sql练习题解(43-51)
要不用交集做?

select * from emp_v intersect select * from employees;

这题,我觉得没什么东西。

荐
                                                        牛客网sql练习题解(43-51)

NO.48

荐
                                                        牛客网sql练习题解(43-51)

update salaries
set salary = salary * 1.1 
where to_date = '9999-01-01' 
    and emp_no in (select emp_no 
                   from emp_bonus);

荐
                                                        牛客网sql练习题解(43-51)

跟着他的题目要求写没有什么困难。

NO.49

荐
                                                        牛客网sql练习题解(43-51)

select "select count(*) from" || name || ";" as cnts
from sqlite_master
where type = "table";

荐
                                                        牛客网sql练习题解(43-51)

其中的重点是怎么获取表明,也就是要知道不同的DBMS中的表名存储在哪里,
其中sqlite就找到sqlite_mater,如果是mysql就从information_schema.tables 获取表名。

NO.50

荐
                                                        牛客网sql练习题解(43-51)

这题我觉得只需要知道sqlite用的是||连接字符串就可以了。

select last_name || "'" || first_name as name
from employees;

荐
                                                        牛客网sql练习题解(43-51)

NO.51

荐
                                                        牛客网sql练习题解(43-51)

说实话我一开始并不太理解这题什么意思,然后一看这不就是两个吗,然后我就震惊了:

select 2 as cnt;

荐
                                                        牛客网sql练习题解(43-51)

这题确实我参考了其他的网友,因为平时replace用得少,而且没见过这种题,学习了。
考察replace和length函数的用法,
我们用length(原字符串) - length(replace(将,删掉)),这样就可以得到出现","的次数。

select (length('10,A,B') - length(replace('10,A,B',',',''))) as cnt;

荐
                                                        牛客网sql练习题解(43-51)

大家共勉~~

本文地址:https://blog.csdn.net/qq_40742298/article/details/107135282