SQL基础问题整理(1)——你答对了多少?
在程序中,数据库操作是必不可少的部分,所以我们要备足数据库相关知识才能去应付程序中出现的种种问题。基于此,我特地在国外网站、博客上整理了一些问题,并附带了答案和解释、参考。为了保证“原汁原味”,我就保留了英文。大家也来看看你答对了多少?
1.SQL Server 2008 Backup
题目:Is it possible to restore a SQL Server 2008 Enterprise Edition compressed backup to a SQL Server 2008 Standard Edition?
答案:yes
解释:RESTORE from compressed backups on SQL Server 2008 Standard Edition is possible, although the backup compression feature is not supported in SQL Server 2008 Standard Edition.
参考:备份压缩 (SQL Server)
2.Identity
题目:We want to insert record into table a
create table a (a int identity(1,1))
Which statement will work?
insert into a default values
insert into a values (default)
insert into a values (1)
could not insert explicit for identity column
答案:insert into a default values
解释:An insert statement with "default values" works.
参考:INSERT
3.Dynamic SQL
题目:Sam has to run a query dynamically & get the count in a variable and do some processing based on the count. Which of the following queries will return expected output?
declare @tablevariable varchar(100)
set @tablevariable = 'Employees'
A)
declare @sql varchar(100)
Declare @cnt int
Set @sql = 'Select ' + Convert(varchar(100),@cnt) + ' =count(*) from ' + @tablevariable
Exec (@sql) select @cnt
B)
declare @sql varchar(100)
Declare @cnt int
Set @sql = 'Select count(*) from ' + @tablevariable
@cnt = Exec (@sql) select @cnt
C)
DECLARE @sql nvarchar(4000), @params nvarchar(4000), @count int
SELECT @sql = N' SELECT @cnt = COUNT(*) from dbo.' + quotename(@tablevariable)
SELECT @params = N'@cnt int OUTPUT'
EXEC sp_executesql @sql, @params, @cnt = @count OUTPUT select @count
答案:C
解释:For getting a variable as output in a dynamic statement, we need to use sp_executeSQL.
参考:Introducing Dynamic SQL
4.T-SQL Output Clause
题目:Executing the
相关文档:
public class SqlCheck
{
public SqlCheck()
{
//
// TODO: 在此处添加构造函数逻辑
& ......
如果能从备份文件中只恢复一个表的数据,那不是很好吗?比如,你备份了AdventureWorks数据库,现的你只恢复里面Vendor表数据。不幸的是,SQL Server本身并不支持这样还原,你需要从第三方提供的工具中来执行这样的任务。
提供这种功能的程序都是一些SQL Server第三方备份工具。它们可以让你从备份文件中抽取或是读取单个表 ......
一、sql语句的执行步骤:
1)语法分析,分析语句的语法是否符合规范,衡量语句中各表达式的意义。
2) 语义分析,检查语句中涉及的所有数据库对象是否存在,且用户有相应的权限。
3)视图转换,将涉及视图的查询语句转换为相应的对基表查询语句。
4)表达式转换, 将复杂的 SQL 表达式转换为较简单的等效连接表达式 ......
想使用PL/SQL开发工具,但不想安装那个几百兆的oracle客户端,于是安装了oracle 10g inistant client,40多M吧。
安装后PL/SQL可以用了,但是查询出记录里面的中文却是乱码。折腾了好久才找出解决方法:
设置环境变量:NLS_LANG,值为Oracle数据库设置的字符集,在我的系统里面设置是:SIMPLIFIED CHINESE_CHINA.ZHS16GBK ......
1.一道SQL语句面试题,关于group by
表内容:
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负
如果要生成下列结果, 该如何写sql语句?
胜 负
2005-05-09 2 2
2005-05-10 1 2
------ ......