數(shù)據(jù)庫(kù)SQL實(shí)戰(zhàn)|SQL答案集合及解析(31-40)

牛客數(shù)據(jù)庫(kù)SQL實(shí)戰(zhàn)題(31-40題)

31、獲取select * from employees對(duì)應(yīng)的執(zhí)行計(jì)劃

牛客這個(gè)用的是SQLite,在SQLite數(shù)據(jù)庫(kù)中,可以用 "EXPLAIN" 關(guān)鍵字或 "EXPLAIN QUERY PLAN" 短語(yǔ),用于描述表的細(xì)節(jié)。
參考:??陀懻搮^(qū)

explain select * from employees;

32、將employees表的所有員工的last_name和first_name拼接起來(lái)作為Name,中間以一個(gè)空格區(qū)分

CREATE TABLE employees ( emp_no int(11) NOT NULL,
birth_date date NOT NULL,
first_name varchar(14) NOT NULL,
last_name varchar(16) NOT NULL,
gender char(1) NOT NULL,
hire_date date NOT NULL,
PRIMARY KEY (emp_no));

答案

不同數(shù)據(jù)庫(kù)連接字符串的方法不完全相同,MySQL、SQL Server、Oracle等數(shù)據(jù)庫(kù)支持CONCAT方法,而本題所用的SQLite數(shù)據(jù)庫(kù)只支持用連接符號(hào)"||"來(lái)連接字符串。
參考:牛客討論區(qū)

select last_name||" "||first_name as Name
from employees

33、創(chuàng)建一個(gè)actor表,包含如下列信息

列表 類(lèi)型 是否為NULL 含義
actor_id smallint(5) not null 主鍵id
first_name varchar(45) not null 名字
last_name varchar(45) not null 姓氏
last_update timestamp not null 最后更新時(shí)間,默認(rèn)是系統(tǒng)的當(dāng)前時(shí)間

答案

create table if not exists actor(
actor_id smallint(5) not null primary key,
first_name varchar(45) not null,
last_name varchar(45) not null,
last_update timestamp not null default(datetime('now','localtime')));

primary key也可以寫(xiě)在后面,寫(xiě)primary key(actor_id)。在last_update末尾加上DEFAULT設(shè)置默認(rèn)值,默認(rèn)值為(datetime('now','localtime')),即獲得系統(tǒng)時(shí)間。

34、批量插入數(shù)據(jù)

對(duì)于表actor批量插入如下數(shù)據(jù)

CREATE TABLE IF NOT EXISTS actor (
actor_id smallint(5) NOT NULL PRIMARY KEY,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
last_update timestamp NOT NULL DEFAULT (datetime('now','localtime')))
actor_id first_name last_name last_update
1 PENELOPE GUINESS 2006-02-15 12:34:33
2 NICK WAHLBERG 2006-02-15 12:34:33

答案

利用values:

insert into actor
values(1,'PENELOPE','GUINESS','2006-02-15 12:34:33'),
(2,'NICK','WAHLBERG','2006-02-15 12:34:33');

利用UNION SELECT批量插入:

insert into actor
select 1,'PENELOPE','GUINESS','2006-02-15 12:34:33'
union select 2,'NICK','WAHLBERG','2006-02-15 12:34:33';

35、批量插入數(shù)據(jù),不使用replace操作

對(duì)于表actor批量插入如下數(shù)據(jù),如果數(shù)據(jù)已經(jīng)存在,請(qǐng)忽略,不使用replace操作。

CREATE TABLE IF NOT EXISTS actor (
actor_id smallint(5) NOT NULL PRIMARY KEY,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
last_update timestamp NOT NULL DEFAULT (datetime('now','localtime')))
actor_id first_name last_name last_update
3 'ED' 'CHASE' '2006-02-15 12:34:33'

答案

在SQLite中,insert or ignore表示如果不存在則插入,如果存在則忽略;insert or replace表示如果不存在則插入,如果存在則替換。

insert or ignore into actor
values(3, 'ED', 'CHASE', '2006-02-15 12:34:33')
insert or replace into actor
values(3, 'ED', 'CHASE', '2006-02-15 12:34:33')

在MySQL中則是使用insert ingnore和insert replace。

36、創(chuàng)建一個(gè)actor_name表

對(duì)于如下表actor,其對(duì)應(yīng)的數(shù)據(jù)為:

actor_id first_name last_name last_update
1 PENELOPE GUINESS 2006-02-15 12:34:33
2 NICK WAHLBERG 2006-02-15 12:34:33

創(chuàng)建一個(gè)actor_name表,將actor表中的所有first_name以及l(fā)ast_name導(dǎo)入改表。 actor_name表結(jié)構(gòu)如下:

列表 類(lèi)型 是否為NULL 含義
first_name varchar(45) not null 名字
last_name varchar(45) not null 姓氏

答案

創(chuàng)建表和插入數(shù)據(jù)合并在一條語(yǔ)句中:

create table actor_name as
select first_name, last_name from actor;

先創(chuàng)建表再插入數(shù)據(jù):

create table actor_name(
first_name varchar(45) not null,
last_name varchar(45) not null
);
insert into actor_name
select first_name, last_name from actor;

37、對(duì)first_name創(chuàng)建唯一索引uniq_idx_firstname

針對(duì)如下表actor結(jié)構(gòu)創(chuàng)建索引:

CREATE TABLE IF NOT EXISTS actor (
actor_id smallint(5) NOT NULL PRIMARY KEY,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
last_update timestamp NOT NULL DEFAULT (datetime('now','localtime')))

對(duì)first_name創(chuàng)建唯一索引uniq_idx_firstname,對(duì)last_name創(chuàng)建普通索引idx_lastname

答案

create unique index uniq_idx_firstname on actor(first_name);
create index idx_lastname on actor(last_name);

38、針對(duì)actor表創(chuàng)建視圖actor_name_view

只包含first_name以及l(fā)ast_name兩列,并對(duì)這兩列重新命名,first_name為first_name_v,last_name修改為last_name_v:

CREATE TABLE IF NOT EXISTS actor (
actor_id smallint(5) NOT NULL PRIMARY KEY,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
last_update timestamp NOT NULL DEFAULT (datetime('now','localtime')))

答案

create view actor_name_view as
select first_name as first_name_v, last_name as last_name_v
from actor;
create view actor_name_view (first_name_v, last_name_v) as
select first_name, last_name
from actor;

create view ... as ...創(chuàng)建視圖。

39、針對(duì)上面的salaries表emp_no字段創(chuàng)建索引idx_emp_no

針對(duì)salaries表emp_no字段創(chuàng)建索引idx_emp_no,查詢(xún)emp_no為10005, 使用強(qiáng)制索引。

CREATE TABLE salaries (
emp_no int(11) NOT NULL,
salary int(11) NOT NULL,
from_date date NOT NULL,
to_date date NOT NULL,
PRIMARY KEY (emp_no,from_date));

答案

select * from salaries indexed by idx_emp_no where emp_no=10005

參考:索引
數(shù)據(jù)庫(kù)的索引問(wèn)題就是查找問(wèn)題。數(shù)據(jù)庫(kù)索引,是數(shù)據(jù)庫(kù)管理系統(tǒng)中一個(gè)排序的數(shù)據(jù)結(jié)構(gòu),以協(xié)助快速查詢(xún)、更新數(shù)據(jù)庫(kù)中表的數(shù)據(jù)。索引的實(shí)現(xiàn)通常使用B樹(shù)和變種的B+樹(shù)(mysql常用的索引就是B+樹(shù))。
除了數(shù)據(jù)之外,數(shù)據(jù)庫(kù)系統(tǒng)還維護(hù)為滿(mǎn)足特定查找算法的數(shù)據(jù)結(jié)構(gòu),這些數(shù)據(jù)結(jié)構(gòu)以某種方式引用數(shù)據(jù).這種數(shù)據(jù)結(jié)構(gòu)就是索引。
在MySQL中為force index:

select * from salaries force index (idx_emp_no) where emp_no = 10005

40、在last_update后面新增加一列名字為create_date

存在actor表,包含如下列信息:

CREATE TABLE IF NOT EXISTS actor (
actor_id smallint(5) NOT NULL PRIMARY KEY,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
last_update timestamp NOT NULL DEFAULT (datetime('now','localtime')));

現(xiàn)在在last_update后面新增加一列名字為create_date, 類(lèi)型為datetime, NOT NULL,默認(rèn)值為'0000 00:00:00'。(這里的默認(rèn)值寫(xiě)的有問(wèn)題,默認(rèn)值為'0000-00-00 00:00;00'才能通過(guò))

答案

alter table actor 
add column create_date datetime not null default '0000-00-00 00:00:00'

其中的column可以省略,not null 和default可以交換位置,default值可以加括號(hào)。

結(jié)尾

如果您發(fā)現(xiàn)我的文章有任何錯(cuò)誤,或?qū)ξ业奈恼掠惺裁春玫慕ㄗh,請(qǐng)聯(lián)系我!如果您喜歡我的文章,請(qǐng)點(diǎn)喜歡~*我是藍(lán)白絳,感謝你的閱讀!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容