码迷,mamicode.com
首页 > 数据库 > 详细

一份数据库笔试题

时间:2016-06-02 06:09:57      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

(在Mysql数据库上面进行测试)

create table a (ID int,Name char(20));

create table b (ID int,Name char(20));

1.当b表存在a表相同的ID字段的时候,将a表的Name更新到b表的Name。

update b,a set b.Name=a.Name where a.ID=b.ID;

2.当b表不存在a表ID的记录时,将a表的记录插入到b表中

insert into b select * from a where a.ID not in(select id from B);

3.去掉表a中重复的Name的记录。

delete from a where a.name in (select b.name from(select d.name from a d group by d.name having count(d.name)>1)b) and a.id not in (select id from (select max(c.id) id from a c group by c.name having count(c.name)>1)f);

这里select d.name from a d group by d.name having count(d.name)>1改成(select b.name from(select d.name from a d group by d.name having count(d.name)>1)b) 的原因是Mysql数据库,如果不改成这样会出现错误:

1093 - You can‘t specify target table ‘a‘ for update in FROM clause。

文档“Currently, you cannot update a table and select from the same table in a subquery.”

数据库这样子设计性能会降低吧?

4.设置表a的ID字段为主键

alter table a add primary key(ID);

 删除主键alter table a drop primary key(ID);

5.在表a添加ReMark描述字段

alter table a add ReMark char(50);

6.

一份数据库笔试题

标签:

原文地址:http://www.cnblogs.com/avaj/p/5551210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!