题目31

获取select * from employees对应的执行计划
题解:

explain select * from employees

一点总结:性能调优时,使用explain可以看到一些细节。


题目32

将employees表的所有员工的last_name和first_name拼接起来作为Name
题解

MySQL、SQL Server、Oracle等数据库支持CONCAT方法,
而本题所用的SQLite数据库只支持用连接符号"||"来连接字符串

CONCAT方法:
select CONCAT(CONCAT(last_name," "),first_name) as name  from employees
或者
select CONCAT(last_name," ",first_name) as name  from employees

本题中使用:
select last_name||" "||first_name as name  from employees

题目33:

创建一个actor表,包含如下列信息
图片说明
题解:

create table actor(
    actor_id smallint(5) not null,
    first_name varchar(45) not null,
    last_name varchar(45) not null,
    last_update timestamp not null default (datetime('now','localtime')),
    PRIMARY KEY(actor_id)
)
//MYSQL写法
create table actor (
    actor_id smallint(5)  primary key not null ,
    first_name varchar(45) not null,
    last_name varchar(45) not null,
    last_update timestamp not null default now()
)

题目34:

批量插入数据
插入的数据如下:
图片说明
题解:

INSERT INTO actor
VALUES (1, 'PENELOPE', 'GUINESS', '2006-02-15 12:34:33'),
       (2, 'NICK', 'WAHLBERG', '2006-02-15 12:34:33')

题目35:

批量插入数据,不使用replace操作
插入数据如下:
图片说明
题解:

//sqlite3
insert or ignore into actor
values(3,'ED','CHASE','2006-02-15 12:34:33');
//MYSQL写法
insert IGNORE into actor
values(3,'ED','CHASE','2006-02-15 12:34:33');

题目36:

创建一个actor_name表
图片说明
题解:

//如果是MYSQL,可以去掉as。
create table if not exists actor_name as 
select first_name, last_name from actor
//写法2
create table actor_name as
select first_name,last_name from actor;