1 Create 唯一索引
and普通索引
The table structure is as follows:
demand
To first_name
create 唯一索引uniq_idx_firstname
, to last_name
create 普通索引idx_lastname
.
SQL statement
create unique index uniq_idx_firstname on actor(first_name);
create index idx_lastname on actor(last_name);
2 转义符
use
The table structure is as follows
demand
Connect all employees' last_name
sums first_name
with '
(single quotes).
SQL statement
select concat(last_name, '\'', first_name) as name from employees;
select concat(last_name, '''', first_name) as name from employees;
select concat(last_name, "'", first_name) as name from employees;
The above three statements can all be processed. The difference is that '
the processing method is different, and the commonly used one is to use backslash escape \
.
operation result
3 subqueries `nested
The table structure is as follows
demand
Find belongs to Action分类
all the movies of the corresponding title
, description
.
SQL statement
select title, description from film
where film_id in (select film_id from film_category where category_id =
(select category_id from category where name = "Action"));
Here's a query involves three tables title
, description
only the film表
relevant, film_id
then with category表
and film_category表
related to the need 两重嵌套
relationships.
operation result
4 Insert a record,忽略重复项
The table structure is as follows
demand
Insert a piece of data for the table actor, if the data already exists, please ignore
actor_id | first_name | last_name | last_update |
---|---|---|---|
‘3’ | ‘ED’ | ‘CHASE’ | ‘2006-02-15 12:34:33’ |
SQL statement
insert ignore into actor values ('3', 'ED', 'CHASE', '2006-02-15 12:34:33');
Use ignore
keywords to avoid the problem of primary key duplication
operation result
5 Insert the query result as a record into the table
The table structure is as follows
Table actor
Table actor_name
demand
The actor表
all first_name
and last_name
importing actor_name表
.
SQL statement
insert into actor_name select first_name, last_name from actor;