牛客SQL 全部题目 SQL220--SQL232 10套代码及解析(3)

SQL220 汇总各个部门当前员工的title类型的分配数目

select
dept_no,
dept_name,
title,
count(title) as count
from departments
join dept_emp using(dept_no)
join titles using(emp_no)
group by dept_no,dept_name,title
order by dept_no,title

SQL223 使用ioin查询方式找出没有分类的电影id以及名称

select
film_id,
title
from film
left join film_category using(film_id)
left join category using(category_id)
where category_id is null

SQL224 使用子查询的方式找出属于Action分类的所有电影对应的title,description

select
title,
description
from (
    select
    title,
    description
    from film
    join film_category using(film_id)
    join category using(category_id)
    where name='Action'
    ) as a

SQL226 将employees表的所有员工的last name和first name拼接起来作为Name

  1. MySQL、SQL Server、Oracle等数据库支持CONCAT方法,
    而本题所用的SQLite数据库只支持用连接符号"||"来连接字符串
select last_name || ' ' || first_name from employees;
  1. 因为系统支持的MySQL,所以本题用sqlite的代码,会显示很多0,可以用下面的方法解答:
select
concat(last_name,' ',first_name) as Name 
from
employees

SQL227 创建一个actor表,包含如下列信息

  1. datetime(‘now’,‘localtime’)的意思是 获取系统默认时间
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'))
    )

SQL228 批量插入数据

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

SQL229 批量插入数据,不使用replace操作

  1. mysql中常用的三种插入数据的语句:
  2. insert into表示插入数据,数据库会检查主键,如果出现重复会报错;
  3. replace into表示插入替换数据,需求表中有PrimaryKey,或者unique索引,如果数据库已经存在数据,则用新数据替换,如果没有数据效果则和insert into一样;
  4. insert ignore表示,如果中已经存在相同的记录,则忽略当前新数据;
insert ignore into actor values("3","ED","CHASE","2006-02-15 12:34:33")

**SQL230 创建一个actor name表 **

  1. 本题要用两条语句完成,先用 CREATE TABLE 语句创建actor_name表,包含first_name与last_name字段,然后用 INSERT INTO … SELECT … 语句向actor_name表插入另一张表中的数据
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

SQL231 对first name创建唯一索引unig idx firstname

使用Alter创建索引

  1. 添加主键索引
    特点:数据列不允许重复,不能为null,一张表只能有一个主键;Mysql主动将该字段进行排序
ALTER TABLE 表名 ADD Primary key (col);
  1. 添加唯一索引
    特点:索引列是唯一的,可以null;Mysql主动将该字段进行排序
ALTER TABLE 表名 ADD unique <索引名> (col1, col2, ...col3);
  1. 添加普通索引
    特点:添加普通索引, 索引值不唯一,可为null
Alter table 表名 ADD index <索引名> (col1, col2, ...,);
  1. 添加全文索引
    特点:只能在文本类型CHAR,VARCHAR, TEXT类型字段上创建全文索引;
ALTER TABLE 表名 ADD Fulltext <索引名> (col)
  1. 添加多列索引
    特点:多列是唯一的
ALTER TABLE 表名 ADD UNIQUE (col1, col2, ..., )

使用Create创建索引

语法:create index 索引名 on 表名(字段)

  1. 添加普通索引
create index 索引名 on 表名(col1, col2, ..., )
  1. 添加唯一索引
create unique index 索引名 on 表名(col1, col2, ..., )

两种创建索引方式的区别

  1. Alter可以省略索引名。如果省略索引名,数据库会默认根据第一个索引列赋予一个名称;Create必须指定索引名称。

  2. Create不能用于创建Primary key索引;

  3. Alter允许一条语句同时创建多个索引;Create一次只能创建一个索引

ALTER TABLE 表名 ADD Primary key (id), ADD index <索引名> (col1, col2, ...,)
  1. 索引执行效率分析

主键索引 > 唯一性索引 > 普通索引

删除索引

  1. 第一种方式
drop index 索引名 on 表名;
  1. 第二种方式
Alter table 表名 drop index 索引名;
  1. 第三种方式
Alter table 表名 drop primary key

题目答案如下:

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

SQL232 针对actor表创建视图actor name view

  1. 方法一:注意 CREATE VIEW … AS … 的 AS 是创建视图语法中的一部分,而后面的两个 AS 只是为字段创建别名
create view actor_name_view (first_name_v,last_name_v) as
select first_name ,last_name from actor
  1. 方法二:直接在视图名的后面用小括号创建视图中的字段名
CREATE VIEW actor_name_view (fist_name_v, last_name_v) AS
SELECT first_name, last_name FROM actor 

猜你喜欢

转载自blog.csdn.net/qq118640594X/article/details/128054711