#数据库SQL实战#【Day9】
--------------------------------------------------
学习用,欢迎讨论。
--------------------------------------------------
查看详细题目的方法:
复制以下题目内容;
Ctrl+F查找刚刚复制的题目即可。
--------------------------------------------------
题目35:批量插入数据,如果数据已经存在,请忽略,不使用replace***作
insert or ignore into actor values ( 3, 'ED', 'CHASE', '2006-02-15 12:34:33' )
在SQLite中用“insert or ignore”来插入记录或者忽略与表内unique字段都相同的记录,用“insert or replace”来插入记录或者替代与表内unique字段都相同的记录。
--------------------------------------------------
题目36:创建一个actor_name表,将actor表中的所有first_name以及last_name导入
create table actor_name as select first_name, last_name from actor
当然,本题也可以分两步完成:首先创建表,然后插入数据。
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:对first_name创建唯一索引uniq_idx_firstname,对last_name创建普通索引idx_lastname
create unique index uniq_idx_firstname on actor(first_name); create index idx_lastname on actor(last_name);
创建索引和唯一索引
key word: index [http://www.runoob.com/sqlite/sqlite-index.html]
一个神奇的现象:如下代码竟然不能通过
create unique index uniq_idx_firstname on actor(first_name); create index idx_lastname on actor(last_name);
--------------------------------------------------
题目38:针对actor表创建视图actor_name_view
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
key word: view [http://www.runoob.com/sqlite/sqlite-view.html]
--------------------------------------------------
题目39:针对上面的salaries表emp_no字段创建索引idx_emp_no,查询emp_no为10005
select * from salaries indexed by idx_emp_no where emp_no = 10005
考察索引的使用
key word: indexed by [http://www.runoob.com/sqlite/sqlite-indexed-by.html]
--------------------------------------------------
题目40:在last_update后面新增一列名字为create_date
alter table actor add column create_date datetime not null default ('0000-00-00 00:00:00')
修改表,上述语句中“column”可以省略。
key word: alter [http://www.runoob.com/sqlite/sqlite-alter-command.html]
【注】在SQLite中,除了重命名表和在已有的表中添加列,alter table命令不支持其他***作。
--------------------------------------------------
题目41:构造一个触发器audit_log,在向employees表中插入一条数据的时候,触发插入相关的数据到audit中
create trigger audit_log after insert on employees_test begin insert into audit values (new.id, new.name); end;
创建触发器,关于触发器的详细说明见[http://www.runoob.com/sqlite/sqlite-trigger.html]
--------------------------------------------------