设为首页 加入收藏

TOP

数据库面试题(七)
2014-11-24 03:04:52 来源: 作者: 【 】 浏览:28
Tags:数据库 试题
库中文件的其它部分。每个数据库都有一个主要数据文件。主要数据文件的推荐文件扩展名是 .mdf。
次要数据文件
次要数据文件包含除主要数据文件外的所有数据文件。有些数据库可能没有次要数据文件,而有些数据库则有多个次要数据文件。次要数据文件的推荐文件扩展名是 .ndf。
日志文件
日志文件包含恢复数据库所需的所有日志信息。每个数据库必须至少有一个日志文件,但可以不止一个。日志文件的推荐文件扩展名是 .ldf。
7.请用一个sql语句得出结果
从table1,table2中取出如table3所列格式数据,注意提供的数据及结果不准确,只是作为一个格式向大家请教。
如使用存储过程也可以。
table1
月份mon 部门dep 业绩yj
——————————-
一月份 01 10
一月份 02 10
一月份 03 5
二月份 02 8
二月份 04 9
三月份 03 8
table2
部门dep 部门名称dname
——————————–
01 国内业务一部
02 国内业务二部
03 国内业务三部
04 国际业务部
table3 (result)
部门dep 一月份 二月份 三月份
————————————–
01 10 null null
02 10 8 null
03 null 5 8
04 null null 9
——————————————
1)
select a.部门名称dname,b.业绩yj as ‘一月份’,c.业绩yj as ‘二月份’,d.业绩yj as ‘三月份’
from table1 a,table2 b,table2 c,table2 d
where a.部门dep = b.部门dep and b.月份mon = ‘一月份’ and
a.部门dep = c.部门dep and c.月份mon = ‘二月份’ and
a.部门dep = d.部门dep and d.月份mon = ‘三月份’ and
2)
select a.dep,
sum(case when b.mon=1 then b.yj else 0 end) as ‘一月份’,
sum(case when b.mon=2 then b.yj else 0 end) as ‘二月份’,
sum(case when b.mon=3 then b.yj else 0 end) as ‘三月份’,
sum(case when b.mon=4 then b.yj else 0 end) as ‘四月份’,
sum(case when b.mon=5 then b.yj else 0 end) as ‘五月份’,
sum(case when b.mon=6 then b.yj else 0 end) as ‘六月份’,
sum(case when b.mon=7 then b.yj else 0 end) as ‘七月份’,
sum(case when b.mon=8 then b.yj else 0 end) as ‘八月份’,
sum(case when b.mon=9 then b.yj else 0 end) as ‘九月份’,
sum(case when b.mon=10 then b.yj else 0 end) as ‘十月份’,
sum(case when b.mon=11 then b.yj else 0 end) as ‘十一月份’,
sum(case when b.mon=12 then b.yj else 0 end) as ‘十二月份’,
from table2 a left join table1 b on a.dep=b.dep
8.华为一道面试题
一个表中的Id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数。
——————————————
select id, Count(*) from tb group by id having count(*)>1
select * from(select count(ID) as count from table group by ID)T where T.count>1
29、为什么group by和 order by会使查询变慢
group by 和 order by操作通常需要创建一个临时表来处理查询的结果,所以如果查询结果很多的话会严重影响性能。
30、SQL一些基础语法
1.增加字段
alter table docdsp add dspcode
char(200)
2.删除字段
ALTER TABLE table_NAME DROP COLUMN
column_NAME
3.修改字段类型
ALTER TABLE table_name ALTER COLUMN
column_name new_data_type
4.sp_rename 改名
更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称。
语法
sp_rename [ @objname = ]
'object_name' ,
[ @newname = ] 'new_name'
[ , [ @objtype =
] 'object_type' ]
如:EXEC sp_rename
'newname','PartStock'
5.sp_help 显示表的一些基本情况
sp_help 'object_name'
如:EXEC sp_help 'PartStock'
6.判断某一表PartStock中字段PartVelocity是否存在
if
exists (select * from syscolumns where
id=object_id('PartStock') and name='PartVelocity')
print
'PartVelocity exists'
else print 'PartVelocity not
exists'
另法:
判断表的存在性:
select count(*) from sysobjects where type='U'
and name='你的表名'
判断字段的存在性:
select count(*) from syscolumns
where
id = (select id from sysobjects where type='U' and name='你的表名')
and name = '你要判断的字段名'
一个小例子
--假设要处理的表名为:
tb
--判断要添加列的表中是否有主键
if exists(select 1 from sysobjects where
parent_obj=object_id('tb') and xtype='PK')
begin
print
'表中已经有主键,列只能做为普通列添加'
--添加int类型的列,默认值为0
alter table tb add列名 int defaul
首页 上一页 4 5 6 7 8 9 下一页 尾页 7/9/9
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇使用Mongodb的搜索拼音自动补全方.. 下一篇数据库死锁的解决办法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)
·索引堆及其优化 - 菜 (2025-12-24 20:18:50)
·Shell 中各种括号的 (2025-12-24 19:50:39)
·Shell 变量 - 菜鸟教 (2025-12-24 19:50:37)