数据库相关题目

题1,如何删除掉一张表重复记录?

--创建一个没有指定主键的表,因此可以有多个重复记录;
create table cat(
catId int,
catName varchar(30))
--插入多个重复记录
insert into cat values(1,'aa') --执行多次
insert into cat values(2,'bb') --执行多次
--使用distinct关键字去重,并将结果导入到一个临时表#temp(表名可以#开头)
select distinct * into #temp from cat
--清除表cat中的所有数据
delete from cat
--将表#temp的数据导入到cat中
insert into cat select * from #temp
--删除临时表
drop table #temp


题2:显示公司每个员工和它上级名字

--用内连接
select worker.name,boss.name
from emp worker, emp boss
where worker.manager=boss.empNo

 
题3:在题3的基础上,要求没有上级人,名字也要显示出来。

--用左外连接:指左边的表的记录全部显示,如果没有匹配的记录就用null填充
select w.name, b.name
from emp w left join emp b
on w.manager=b.empNo

题4:实现主键自增长

create table test
(testId int primary key identity(1,1), --identity(1,1)表示从1开始自增长,增量为1
 testName varchar(30) not null, --约束是不为空
 testAge int
)

猜你喜欢

转载自ghost-face.iteye.com/blog/1815179