2023年8月3日发(作者:)

sqlserverupdate两列相加_SQL·19道表操作的实战题本⽂⼤部分知识点和前⼀篇⽂中相同,其他新知识点主要是关于表操作:1. 建表:设置主键和变量的默认值,Q42. 插⼊数据,Q5、Q63. ⽣成列索引,Q8,Q104. 创建视图,Q95. alter 语句表中插⼊⼀列,Q116. 构造触发器,Q127. delete 语句删除重复记录,Q138. 替换:update 语句、replace,Q14,Q159. alter 语句重命名,Q1610. 设置外键,Q1711. update ... set 批量修改,Q18-- 1、查找描述信息中包括robot的电影对应的分类名称以及电影数⽬,-- ⽽且还需要限定每个分类对应电影数量 >= 5部select name, count(_id)as mv_cntfrom film_category fcjoin category c on ry_id=ry_idjoin (select * from film f

where ption like '%robot%') f on _id=_idjoin (select *, count (_id)as mv_cnt

from film_category fc

group by category_id

having mv_cnt>=5) f cc on ry_id=ry_id;-- 2、使⽤join查询⽅式找出没有分类的电影id以及名称select _id, titlefrom film fleft join film_category fc on _id=_idwhere ry_id is null;-- 3、使⽤⼦查询的⽅式找出属于Action分类的所有电影对应的title,description

select title, description

from film where film_id in

(select _id from film_category fc join category c on ry_id=ry_id and name='Action' join film f on _id=_id);-- 4、创建⼀个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')) -- 外层括号不可省略 -- PRIMARY KEY(actor_id))-- 5、对于表actor批量插⼊数据insert into actorvalues (1,'PENELOPE','GUINESS','2006-02-15 12:34:33'), (2,'NICK','WAHLBERG','2006-02-15 12:34:33')-- 6、对于表actor批量插⼊如下数据,如果数据已经存在,请忽略,不使⽤replace操作insert or ignore into actorvalues(3,'ED','CHASE','2006-02-15 12:34:33');-- 7、创建⼀个actor_name表,将actor表中的所有first_name以及last_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;insert into actor_name select first_name,last_name from actor;-- 8、对first_name创建唯⼀索引uniq_idx_firstname,对last_name创建普通索引idx_lastnamecreate unique index uniq_idx_firstname on actor(first_name);create index idx_lastname on actor(last_name);-- 9、针对actor表创建视图actor_name_view,只包含first_name以及last_name两列,-- 并对这两列重新命名,first_name为first_name_v,last_name修改为last_name_vcreate view actor_name_view (first_name_v, last_name_v) asselect first_name,last_name

from actor;-- 10、针对salaries表emp_no字段创建索引idx_emp_no,查询emp_no为10005, 使⽤强制索引select * from salariesindexed by idx_emp_nowhere emp_no = '10005';-- 11、现在在last_update后⾯新增加⼀列名字为create_date, 类型为datetime, NOT NULL,-- 默认值为'0000 00:00:00'alter table actor add columns create_date datetime not null default '0000-00-00 00:00:00';-- 12、构造⼀个触发器audit_log,在向employees_test表中插⼊⼀条数据的时候,-- 触发插⼊相关的数据到audit中。create trigger audit_log after insert on employees_testbegin insert into audit values (, );end;-- 13、删除emp_no重复的记录,只保留最⼩的id对应的记录。delete from titles_testwhere id not in (select min(id) from titles_test group by emp_no);-- 14、将所有to_date为9999-01-01的全部更新为NULL,且 from_date更新为2001-01-01。update titles_test set to_date=null, from_date='2001-01-01' where to_date='9999-01-01';-- 15、将id=5以及emp_no=10001的⾏数据替换成id=5以及emp_no=10005,其他数据保持不变,-- 使⽤replace实现。replace into titles_test values(5, 10005, 'Senior Engineer', '1986-06-26', '9999-01-01');-- 16、将titles_test表名修改为titles_2017。alter table titles_test rename to titles_2017;-- 17、在audit表上创建外键约束,其emp_no对应employees_test表的主键id。drop table audit;create table audit( emp_no int not null, create_date datetime not null, foreign key(emp_no) references employees_test(ID));-- 18、将所有获取奖⾦的员⼯当前的薪⽔增加10%update salaries set salary = salary*1.1 where emp_no in (select emp_no from emp_bonus);-- 19、查找字符串'10,A,B' 中逗号','出现的次数cnt

-- 解法⽐较看sql的版本,题库⽤到的是sqliteselect (length("10,A,B") - length(replace("10,A,B", ",", ""))) as cnt;

2023年8月3日发(作者:)

sqlserverupdate两列相加_SQL·19道表操作的实战题本⽂⼤部分知识点和前⼀篇⽂中相同,其他新知识点主要是关于表操作:1. 建表:设置主键和变量的默认值,Q42. 插⼊数据,Q5、Q63. ⽣成列索引,Q8,Q104. 创建视图,Q95. alter 语句表中插⼊⼀列,Q116. 构造触发器,Q127. delete 语句删除重复记录,Q138. 替换:update 语句、replace,Q14,Q159. alter 语句重命名,Q1610. 设置外键,Q1711. update ... set 批量修改,Q18-- 1、查找描述信息中包括robot的电影对应的分类名称以及电影数⽬,-- ⽽且还需要限定每个分类对应电影数量 >= 5部select name, count(_id)as mv_cntfrom film_category fcjoin category c on ry_id=ry_idjoin (select * from film f

where ption like '%robot%') f on _id=_idjoin (select *, count (_id)as mv_cnt

from film_category fc

group by category_id

having mv_cnt>=5) f cc on ry_id=ry_id;-- 2、使⽤join查询⽅式找出没有分类的电影id以及名称select _id, titlefrom film fleft join film_category fc on _id=_idwhere ry_id is null;-- 3、使⽤⼦查询的⽅式找出属于Action分类的所有电影对应的title,description

select title, description

from film where film_id in

(select _id from film_category fc join category c on ry_id=ry_id and name='Action' join film f on _id=_id);-- 4、创建⼀个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')) -- 外层括号不可省略 -- PRIMARY KEY(actor_id))-- 5、对于表actor批量插⼊数据insert into actorvalues (1,'PENELOPE','GUINESS','2006-02-15 12:34:33'), (2,'NICK','WAHLBERG','2006-02-15 12:34:33')-- 6、对于表actor批量插⼊如下数据,如果数据已经存在,请忽略,不使⽤replace操作insert or ignore into actorvalues(3,'ED','CHASE','2006-02-15 12:34:33');-- 7、创建⼀个actor_name表,将actor表中的所有first_name以及last_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;insert into actor_name select first_name,last_name from actor;-- 8、对first_name创建唯⼀索引uniq_idx_firstname,对last_name创建普通索引idx_lastnamecreate unique index uniq_idx_firstname on actor(first_name);create index idx_lastname on actor(last_name);-- 9、针对actor表创建视图actor_name_view,只包含first_name以及last_name两列,-- 并对这两列重新命名,first_name为first_name_v,last_name修改为last_name_vcreate view actor_name_view (first_name_v, last_name_v) asselect first_name,last_name

from actor;-- 10、针对salaries表emp_no字段创建索引idx_emp_no,查询emp_no为10005, 使⽤强制索引select * from salariesindexed by idx_emp_nowhere emp_no = '10005';-- 11、现在在last_update后⾯新增加⼀列名字为create_date, 类型为datetime, NOT NULL,-- 默认值为'0000 00:00:00'alter table actor add columns create_date datetime not null default '0000-00-00 00:00:00';-- 12、构造⼀个触发器audit_log,在向employees_test表中插⼊⼀条数据的时候,-- 触发插⼊相关的数据到audit中。create trigger audit_log after insert on employees_testbegin insert into audit values (, );end;-- 13、删除emp_no重复的记录,只保留最⼩的id对应的记录。delete from titles_testwhere id not in (select min(id) from titles_test group by emp_no);-- 14、将所有to_date为9999-01-01的全部更新为NULL,且 from_date更新为2001-01-01。update titles_test set to_date=null, from_date='2001-01-01' where to_date='9999-01-01';-- 15、将id=5以及emp_no=10001的⾏数据替换成id=5以及emp_no=10005,其他数据保持不变,-- 使⽤replace实现。replace into titles_test values(5, 10005, 'Senior Engineer', '1986-06-26', '9999-01-01');-- 16、将titles_test表名修改为titles_2017。alter table titles_test rename to titles_2017;-- 17、在audit表上创建外键约束,其emp_no对应employees_test表的主键id。drop table audit;create table audit( emp_no int not null, create_date datetime not null, foreign key(emp_no) references employees_test(ID));-- 18、将所有获取奖⾦的员⼯当前的薪⽔增加10%update salaries set salary = salary*1.1 where emp_no in (select emp_no from emp_bonus);-- 19、查找字符串'10,A,B' 中逗号','出现的次数cnt

-- 解法⽐较看sql的版本,题库⽤到的是sqliteselect (length("10,A,B") - length(replace("10,A,B", ",", ""))) as cnt;