C# 面试图解(数据库)

1.列举ADO.NET中的五个主要对象,并简单描述

答:Connection连接对象,Command执行命令和存储过程,DataReader向前只读的数据流,DataAdapter适配器,支持增删查询(自动打开),DataSet数据级对象,相当与内存里的一张或多张表。


什么是事务?什么是锁?

答:事务是指一个工作单元,它包含了一组数据操作命令,并且所有的命令作为一个整体一起向系统提交或撤消请求操作,即这组命令要么都执行,要么都不执行。

  锁是在多用户环境中对数据的访问的限制。SqlServer自动锁定特定记录、字段或文件,防止用户访问,以维护数据安全或防止并发数据操作问题,锁可以保证事务的完整性和并发性。


什么是索引,有什么优点?

答:索引象书的目录类似,索引使数据库程序无需扫描整个表,就可以在其中找到所需要的数据,索引包含了一个表中包含值的列表,其中包含了各个值的行所存储的位置,索引可以是单个或一组列,索引提供的表中数据的逻辑位置,合理划分索引能够大大提高数据库性能。

什么是存储过程?有什么优点?

答:存储过程是一组予编译的SQL语句

    它的优点:1.允许模块化程序设计,就是说只需要创建一次过程,以后在程序中就可以调用该过程任意次。

           2.允许更快执行,如果某操作需要执行大量SQL语句或重复执行,存储过程比SQL语句执行的要快。

           3.减少网络流量,例如一个需要数百行的SQL代码的操作有一条执行语句完成,不需要在网络中发送数百行代码。

          4.更好的安全机制,对于没有权限执行存储过程的用户,也可授权他们执行存储过程。


什么是触发器?

答:触发器是一种特殊类型的存储过程,出发器主要通过事件触发而被执行的,

  触发器的优点:1.强化约束,触发器能够提供比CHECK约束;

         2.跟踪变化,触发器可以跟踪数据库内的操作,从而不允许未经允许许可的更新和变化;

         3.联级运算,比如某个表上的触发器中包含对另一个表的数据操作,而该操作又导致该表上的触发器被触发


1.create table 表名
(
列名 类型 null,
列名 类型 primaryt key not null,--主键列
列名 类型 identity(1,1) not null,--自动增长,只能用于数据的类型:int,bigint,float
)
例子
create table tb_01
(
ID int primary key identity(1,1) not bull,
Name varchar not null
)



 增 删  改

->2.简单的查询: Select 列名  From 表明   where 条件表达式 select getdate
->插入数据: Insert Into 表名(列名) values(列值)

insert into students(name,age) value('张三',25)
 insert into newtable (name,class) select name,class form oldtable
->删除数据:Delete

delete from <表名> [ where <删除条件>] 
delete [ from ] tableName where search_conditions
 3、drop删除   删除表中所有行,表结构也删除了
drop table <表名>    drop table tableinfo

->修改数据:Update

update <表名> set <列名=更新值> [ where <更新条件>]

set 后面可以紧随多个数据列的更新值(非数字要引号)
1.将Product表中"啤酒"的价格改为4元
update product set price=4 where productName='啤酒'
update tableinfo set age=12 where name = 'atm1'


3.查

 一般的查询

1).查询所有数据
     select * from tableinfo
   2).查询部分行列 --条件查询
     select name ,age   from   tableinfo   where age=11;
   3).在查询中使用AS更改列名
     select name as 姓名  from a where   age=11;
   4).查询空行
     select name from tableinf  where class is null
     5).查询返回限制行数(关键字: top )
     select top 6 name from tableinfo
显示列 name 的前6行,oracle 中用rownum替代( select    *   from    a where    rownum<6  
SELECT TOP n <列名表> FROM <表名> [查询条件]
SELECT TOP 20 * FROM student  WHERE sage > 23
SELECT TOP 20 * FROM student ORDER BY sage DESC
选择从10-15的记录
  分页 select top 5 * from( select top15 * from table order by id)
   6).查询排序(关键字: orderby , asc , desc
    例: select name from tableinfo where age>=11 order by desc (默认为ASC升序)
--select * from tblScore order by score1 desc,score2 desc
select ID,Name,Age from Students order by Age // 按年龄排序
Select ID,Name,Age from Students order by Age ASC
SELECT ID,Name,Score FROM Students ORDER BY Score DESC ,ID ASC

7 )分组
select title,count(*),sum(customer) 此时将分组的信息展现
from salesLt.Custom 第一步找到表
group by Title 第二部,最这个表进行分组
只要用了grounpBy ,select后面只能跟group by 后面的字段或者是集合函数
8) 类型转换 convert 目标类型,转换的表达式,格式规范
cast 拼接 表达 as 类型
select convert(nvarchar(32),customerId)+Title from salesLt.customer
select convert(customerId as nvarchar(32))+Title from salesLt.customer
2.模糊查询
   1).使用 like进 行模糊查询
  2).使用between在某个范围内进行查询
select * from tableinfo where age between 11 and 22
3)使用 in在列举值内进行查询(in后是多个的数据)
select name from tableinfo where name in ( 'atm' , 'atm1' , 'atm2' )
3.Count(*)
select count(*) from Product --count(*) :找表中最短的列进行统计行数数
SELECT COUNT("列名") FROM "表格名"
SELECT COUNT(store_name) FROM Store_Information WHERE store_name is not NULL
如果有distinct 以后就不用加条件了,除去重复
SELECT COUNT(DISTINCT store_name) FROM Store_Information

4.order by


概率极大的几组题

 top 先分清楚

 select  * from table 查询全部

select top  10  * from table 取前10条,返回无序集合

select * from table order by id desc -- 取所有数据,按id逆序返回有序列表

select top n * from table order by id desc-- 先按id逆序,再取前n条,返回按id排序的有序

(1) 分页的使用(top) 查询40-50的记录数

 select top 10 * from table where ID  not in

( select  top 40  ID from table order by  id ) 

order by id

写出一条Sql语句:取出表A中第31到第40记录(SQLServer,以自动增长的ID作为主键,注意:ID可能不是
select top 10 * from A where id not in (select top 30 id from A)

grid 重写OnItemDataBound事件,


为管理业务培训信息,建立3个表:(出现频率高)

S(S#,SN,SD,SA)S#,SN,SD,SA分别代表学号(S#),学员姓名(SN),所属单位(SD),学员年龄(SA)

C(C#,CN)C#,CN分别代表课程编号(C#),课程名称(CN)

SC(S#,C#,G) S#,C#,G分别代表学号(S#),所选的课程编号(C#),学习成绩(G)

(1)使用标准SQL嵌套语句查询选修课程名称为’ 税收基础 ’的学员学号和姓名?

select S# ,SN  from S Where S#  in (select   S#    from C, SC  Where  C.c#=SC.C#   and   CN='税收基础')

(2) 使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位?

select sn,sd from S, SC     Where S.s#=sc.s# and  SC.c#='C2'

(3) 使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位?

SELECT  SN,SD  FROM S WHERE S# IN( select  S# from  SC where C#='C5')

(4)查询选修了课程的学员人数

 select  学员人数=count( distinct S#) from SC

(5) 查询选修课程超过5门的学员学号和所属单位?

select  sn,sd from s where  s# in( select s# from sc  group by s# having count (distinct C#)>5)

在面试过程中多次碰到两道SQL查询的题目,一是查询A(ID,Name)表中第31至40条记录,ID作为主键可能是不是连续增长的列,完整的查询语句如下:

select  top 10 * from A where  ID>( select max(ID) from ( select  top 30 ID  from A order by A)T) order by   A

 select top 10 * from tableA where ID not in (select top 30 ID from tableA)

select top 40 * from tableA  except select top 30 * from tableA 

表1 Student(学生表)

S#(学号)     Sname(学生姓名)    Sage(学生年龄)   Ssex(学生性别)

表二Course(课程表)

C#( 课程编号)   Cname(课程名称)  T#(老师编号)  

表三SC(成绩表)

S#(学号)       C#(课程编号)   Score(成绩)

表四 Teacher(教师表)

T# 教师编号  Tname(教师名称)

数据库50道面试题

1、查询“001”课程比“002”课程成绩高的所有学生的学号
 select  a.s# from(select S#, score from sc where C#='001')a,(select s#,score from sc where C#='002')b where a.score>b.score and a.s#=b.s#

2、查询平均成绩大于60分的同学的学号和平均成绩
select s#,avg(score) from sc group by s# having avg(score)>60
4、查询姓‘李’的老师的个数:
select  count(distinct(Tname)) from teacher where tname like '李%'
5、查询没有学过“叶平”老师可的同学的学号、姓名:
select  student.s#,student.sname  
from student
 where  s# not in ( select distinct (sc.s#) from SC,Course,Teacher 
 where sc.c#=course.C#  and  teacher.T#=course.T# and Teacher .Tname='叶平')
小结:先查后面的,然后 设置条件进去,
数据库面试题


DataGrid的使用,一定要数据绑定这里和前面的一一对应


 List<UserInfo> userList =new List<UserInfo>();
                    foreach (DataRow dataRow in dt.Rows)
                    {
                        Console.WriteLine(dataRow["UserId"] +"   "+dataRow[1]);
                        //把每一行数据封装成 UesrInfo对象。
                        userList.Add(new UserInfo()
                        {
                            Id=int.Parse(dataRow["UserId"].ToString()),
                            UserName = dataRow["UserName"].ToString(),
                            UserAge =int.Parse(dataRow["UserAge"].ToString())
                        });
                    } //一定要和上面一一对应


DataGrideView 单行删除 Selection: FullRowSelet 选中全行

 多行选中 MultiSelect true


数据库操作大致步骤

1.在APP.config 里面

2. Constr,

3.定义一个list

5.如果用SqlDataAdapter new 

6.填充之前,给 SelectCommand赋参数

7.表dataTable  填充

8. foreach (DataRow dataRow in dt.Rows)

9.转换为UserInfo 对象

 赋值给他

 分页查询


分页SQL语句
->Row_Number函数的分页使用
->双Order排序 分页法【越过多少条,取多少条数据】

第一种:分页的方法sql
--一页 :3条   第二页   
-- 越过多少条(2-1)*3   取多少条 3
--select top 3  * from UserInfo where UserId not in
--(
-- select top ((2-1)*3) UserId from UserInfo order by UserId
--) --越过的数据的id的集合
--order by UserId

--第二种:分页sql  一页3条, 取第 4页row_number
--select * from (
--
select  *,ROW_NUMBER() over(order by userid) as num  from UserInfo
--) as T-- 增加行号
--where T.num between ((4-1)*3+1) and  (4*3)


--03:临时表
--临时表  在表名前面 加了  #,临时表  和普通的表:
----会话结束的时候,那么临时表就结束了。
create table #TestTb

-临时表可以用于 高并发的情况的优化

--join 

数据库:锁概念。
--  锁的级别在表级别。
--  X:排它锁    S:共享锁
--  insert、update、delete 会在表上加   X
--  select   在表上添加  S锁。  

--假设:员工表:10000000000000   部门:10亿条
--使用临时表。加锁

select * into #Emp  from Employee

删除临时表#

全局临时表##

存储过程
Create procedure  pro_name
[{@参数数据类型} [=默认值] [output],
{@参数数据类型} [=默认值] [output],
......
]
as Sql_statement
 go
插入
IF OBJECT_ID (N'PROC_INSERT_STUDENT', N'P') IS NOT NULL
    DROP procedure PROC_INSERT_STUDENT;



CREATE procedure PROC_INSERT_STUDENT
    @id int,
    @name nvarchar(20),
    @age int,
    @city nvarchar(20)
AS 
    INSERT INTO Students(ID,Name,Age,City) VALUES(@id,@name,@age,@city)
GO


修改
IF OBJECT_ID (N'PROC_INSERT_STUDENT', N'P') IS NOT NULL
    DROP procedure PROC_INSERT_STUDENT;


CREATE procedure PROC_UPDATE_STUDENT
    @id int,
    @name nvarchar(20),
    @age int,
    @city nvarchar(20)
AS 
    UPDATE Students SET Name=@name,Age=@age,City=@city WHERE ID=@id
GO


删除
--3、存储过程:删除学生信息
IF OBJECT_ID (N'PROC_DELETE_STUDENT_BY_ID', N'P') IS NOT NULL
    DROP procedure PROC_DELETE_STUDENT_BY_ID;
GO
CREATE procedure PROC_DELETE_STUDENT_BY_ID
    @id int
AS 
    DELETE FROM  Students WHERE ID=@id
GO








  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值