面试集锦Sql部分

主要用于面试回答,主要是从网上摘录,有些答案是加入自己的理解,如果有哪些答案不正确,请指出谢谢。

一Sql概念部分

1什么是触发器

触发器是一种特殊类型的存储过程,不由用户直接调用。创建触发器时会对其进行定义,以便在对特定表或列作特定类型的数据修改时执行
 

 

2什么是存储过程

将常用的或很复杂的工作,预先用SQL语句写好并用一个指定的名称存储起来,  

存储过程的优点:只编译一次,当遇到复杂的数据库操作时可以用存储过程封装起来和事务在一起使用。可以重复使用,安全性高

 

3什么是索引,优缺点

索引用来快速地寻找那些具有特定值的记录,普通索引,唯一性索引,主键,单列索引和多列索引,全文索引

优点:数据的唯一性,加快数据检索速度,提高系统性能

缺点:创建索引需要时间,索引需要占物理空间,表数据发生改变时,索引需要动态维护

 

4维护数据库完整性,一致性,你喜欢用触发器还是自写逻辑,为什么

尽可能用约束(包括CHECK、主键、唯一键、外键、非空字段)实现,这种方式的效率最好;其次用触发器,这种方式可以保证无论何种业务系统访问数据库都能维持数据库的完整性、一致性;最后再考虑用自写业务逻辑实现, 自己写业务逻辑在应用程序中定义,容易遗漏,定义时必须按照关系顺序,否则会报错,在处理大量关系是对程序员的细心程度要求比较高

 

5什么是事务,什么是锁

事务(TRANSACTION)是作为单个逻辑工作单元执行的一系列操作,这些操作作为一个整体一起向系统提交,要么都执行,要么都不执行,事务是一个不可分割的工作逻辑单元

事务应该具有4个属性:原子性、一致性、隔离性、持续性。这四个属性通常称为ACID特性
锁是实现事务的关键,可以保证事务的完整性和并发性,它可以是某些数据的拥有者,在某段时间内不能使用某些数据和数据结构


6什么是视图,什么是游标

视图是一种虚拟表,对视图进行增改查操作,使得我们获得数据相比子表查询更容易。

游标提供了一种对从表中检索出的数据进行操作的灵活手段,就本质而言,游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制

 

7SqlServer中使用的一些数据对象

表,视图,函数,存储过程,触发器等

 

8Null是什么意思

表示UnKnown是一个空值,比较使用isnull操作符

 

9主键是什么,外键是什么

主键能够唯一表示数据表中的每个记录的字段或者字段的组合就称为主码(主键)。 

外键是一个用来建立在两个表格之间的关系。一般都涉及到一个表格里的主键和另一个表格相关连的一系列字段,那么这些相关连的字段就是外键。

 

10如何处理几十万条并发数据

用存储过程或事务。取得最大标识的时候同时更新,主键不是自增长方式。并发的时段是不会有重复主键的,取得最大标识要有一个存储过程来截获

 

11写出一条Sql语句:取出表A中第31到第40记录(SQLServer,以自动增长的ID作为主键,注意:ID可能不是连续的

select top 10 * from a where id not in (select top 30 id from a)

select top 10 * from a where id>(select max(id) from (select top 30 id from a ) as a)

 

12 表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。

select (case when a>b then a else b end), (case when b>c then b esle c end)  from table_name

13防止sql注入式攻击

 

二Sql语句部分

1.表text 有id,name字段,如果name相同的记录只能留下一条,其余的删除有没有这样的sql语句,如何做。

a 将重复的记录记入临时表temp1

 

b 将不重复的记录记入temp1

 

c 作一个包含所有不重复的表

 

d deltect表明

 

e insert表

 

f 删除temp1和temp2

 

 

2学生表

student表 s-学号 sn-姓名 sd-单位 sa-年龄

course表 c-课程号 cn课程名

student-course表 s-学号 c-课程号 g-成绩

 

create table student
(
s varchar(4) primary key not null,

sn varchar(20) not null,
sd varchar(20) not null,
sa int not null
)
insert into student values('0001','aaa','aaa',23)
insert into student values('0002','bbb','bbb',22)
insert into student values('0003','ccc','ccc',24)
insert into student values('0004','ddd','ddd',26)
insert into student values('0005','eee','eee',27)
insert into student values('0006','fff','fff',28)
insert into student values('0007','ggg','ggg',29)
insert into student values('0008','hhh','hhh',30)

 

create table course
(
c varchar(4) primary key not null,
cn varchar(20) not null
)
insert into course values('c1','税收基础')
insert into course values('c2','大学英语')
insert into course values('c3','大学语文')
insert into course values('c4','高等数学')
insert into course values('c5','马列')
insert into course values('c6','毛思')
insert into course values('c7','邓理')
insert into course values('c8','计算机')

 

create table studentcourse
(
s varchar(4) foreign key references student(s),
c varchar(4) foreign key references course(c),
g int not null
)
insert into studentcourse values('0001','c1',70)
insert into studentcourse values('0002','c1',90)
insert into studentcourse values('0002','c3',70)

insert into studentcourse values('0003','c1',96)
insert into studentcourse values('0003','c2',71)
insert into studentcourse values('0003','c3',72)
insert into studentcourse values('0003','c4',73)

insert into studentcourse values('0003','c5',88)
insert into studentcourse values('0003','c6',74)
insert into studentcourse values('0003','c7',75)
insert into studentcourse values('0003','c8',77)
insert into studentcourse values('0004','c2',80)
insert into studentcourse values('0004','c5',89)
insert into studentcourse values('0005','c1',60)
insert into studentcourse values('0005','c2',67)
insert into studentcourse values('0005','c3',57)
insert into studentcourse values('0005','c4',77)
insert into studentcourse values('0005','c5',66)
insert into studentcourse values('0006','c3',65)
insert into studentcourse values('0006','c4',69)
insert into studentcourse values('0006','c5',62)
insert into studentcourse values('0007','c3',82)
insert into studentcourse values('0007','c4',88)
insert into studentcourse values('0007','c8',77)
insert into studentcourse values('0008','c6',55)
insert into studentcourse values('0008','c8',78)

 

a 查询选择课程名为“税收基础”的学号和姓名

select * from student where s in (select s from course,studentcourse where course.c=studentcourse.c and cn='税收基础')

 

b 查询选修课程号为“c2”的学号,姓名和所有单位

select sn,sd from student,studentcourse where student.s=studentcourse.s and studentcourse.c='c2'

 

c 不选修课程号为“c5”的学号,姓名和所有单位

select sn,sd from student where s not in (select s from studentcourse where c='c5')

 

d 查询选修了课程的学号人数

select 学员人数=count(distinct s) from studentcourse

 

e 查询选修课程超过5门的学号和所有单位

select sn,sd from student where s in (select s from studentcourse group by s having count(distinct c)>5)

 

f 选修全部课程的学号和所有单位(还没搞懂)

select sn,sd from student where s in
(select s from studentcourse right join course on studentcourse.c=course.c group by s having count(*)=count(s))

 

 

3学生表2

student表 sno学号 sname姓名

course表    cno课程号 cname课程名 cteacher任课老师

student course表 sno学号 cno课程号 scgrade成绩

 

create table s
(
sno varchar(4) primary key not null,
sname varchar(20) not null,
)
insert into s values('0001','aaa')
insert into s values('0002','bbb')
insert into s values('0003','ccc')
insert into s values('0004','ddd')
insert into s values('0005','eee')
insert into s values('0006','fff')
insert into s values('0007','ggg')
insert into s values('0008','hhh')

 

create table c
(
cno varchar(4) primary key not null,
cname varchar(20) not null,
cteacher varchar(20) not null
)
insert into c values('c1','税收基础','李明')
insert into c values('c2','大学英语','张平')
insert into c values('c3','大学语文','陈定梅')
insert into c values('c4','高等数学','张海峰')
insert into c values('c5','马列','卢祥宇')
insert into c values('c6','毛思','李书生')
insert into c values('c7','邓理','王稼祥')
insert into c values('c8','计算机','李明')

 

create table sc
(
sno varchar(4) foreign key references s(sno),
cno varchar(4) foreign key references c(cno),
scgrade int not null
)

insert into sc values('0001','c1',50)
insert into sc values('0002','c1',59)
insert into sc values('0003','c1',70)
insert into sc values('0004','c1',96)
insert into sc values('0005','c1',41)
insert into sc values('0006','c1',52)
insert into sc values('0007','c1',43)
insert into sc values('0008','c1',88)
insert into sc values('0002','c2',45)
insert into sc values('0003','c2',85)
insert into sc values('0004','c2',47)
insert into sc values('0005','c2',50)
insert into sc values('0008','c2',89)
insert into sc values('0003','c3',60)
insert into sc values('0004','c3',67)
insert into sc values('0005','c3',57)
insert into sc values('0006','c3',77)

 

a 没有选择李明老师教授课程的所有学生姓名

select sname from s
where  s.sno not in (select sno from sc,c  where sc.cno=c.cno and c.cteacher='李明')

 

b 列出两门以上(含两门)不及格课程的学生姓名及平均成绩

select s.sno,s.sname,avgscgrade=avg(sc.scgrade) from s,sc,
(select sno from sc where scgrade<60 group by sno having count(distinct cno)>=2) a
where s.sno=a.sno and a.sno=sc.sno group by s.sno,s.sname

 

c 列出选过c1号课程又选过c2号课程的所有学生的学号

select s.sno,sname from s,
(select sno from sc,c where sc.cno=c.cno and c.cno in('c1','c2') group by sno having count(distinct sc.cno)=2)
sc where s.sno=sc.sno

 

d 列出c1号成绩比c2号成绩高的所有学生的学号

select s.sno,s.sname from s,
(select distinct sc1.sno from sc sc1,c c1, sc sc2,c c2 where sc1.cno=c1.cno and c1.cname='税收基础'and
sc2.cno=c2.cno and c2.cname='大学英语' and sc1.scgrade>sc2.scgrade)sc where s.sno=sc.sno

 

e 列出c1好成绩比c2号成绩高所有学号和c1号和c2号课程的成绩

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(面试集锦Sql部分)