SQL数据库开发知识总结:提高篇

  1.联合结果集

(1) 简单的结果集联合

  select FNumber,FName,Fage from T_Employee\

  union

  select FidCardNumber,FName,Fage from T_Employee

(2) 查询的基本原则是:每个结果集必须有相同的列数,没个结果集的列必须类型相同。

  select FNumber,FName,Fage,FDepartment from T_Employee

  union

  select FidNumber,FName,Fage,’临时工,无部门’ from T_Employee

  1. union All

(1)  select FName,Fage from T_Employee

union

select FName,Fage from T_Emoloyee

   (*) union合并两个查询结果集,并且将其中完全重复的数据行合并为一条记录。

(2) select FName,Fage from T_Employee

  union All

  select FName,Fage from T_Employee

(*) union因为要进行重复值的扫描,所以效率低,因此如果不是确定要合并重复行,那么推荐用union All

  1. 数据库函数

(1) 数字函数

 1) ABS():求绝对值    select ABS(-3)

 2) ceiling():舍入到最大整数。例如:3.33将被舍入为4,2.89将被舍入为3,-3.61将被舍入为-3。ceiling的英文意思是天花板。

 3) floor():舍入到最小整数。例如:3.33将被舍入为3,2.89将被舍入为2,-3.61将被舍入为-4。floor的英文意思是:地板。

 4) round():四舍五入。舍入到“离我半径最近的数”。round的英文意思是:半径。

例如:select Round(-3.1415926,3)   执行结果是:-3.1420000

(2) 字符串函数

 1) len():计算字符串长度,   select len(‘234’)   执行结果是3。例如:

   select FName,len(FName) from T_Employee。

 2) lower(),upper():转小写,转大写。

 3) ltrim():字符串左侧的空格去掉。例如:去掉两边的空格:

   rtrim():字符串右侧的空格去掉。

  例如:去掉两边的空格:ltrim(rtrim(‘   韩迎龙    ’))

 4) SubString(string,start_position,length),参数string为主字符串,start_position为字符串在主字符串中的起始位置,length为子字符串的最大长度。例如:

  select substring(‘abcdefghijk’,2,3)   执行结果为:bcd。

  select FName,Substring(FName,2,2) from T_Employee。

(3) 日期函数

 1) getdate():取得当前日期的时间。

 2) dateadd(datepart,number,date),计算增加以后的时间,参数date为待计算的日期,参数number为增量,参数datepart为计量单位。例如:

  dateadd(day,3,date)为计算日期date的3天后的日期。

  dateadd(month,-3,date)为计算日期date的8个月前的日期。

  select dateadd(day,-3,getdate()) 执行结果是:当前时间的前三天。

 3) datediff(datepart,startdate,enddate):计算两个日期之间的差额。datepart为计量单位。

   select datetdiff(hh,getdate(),dateAdd(day,-3,getdate()))  执行结果为-72。

   select FName,FInDate,DateDiff(year,FInDate,getdate()) from T_Employee 员工的工龄。

 4) datepart(datepart,date):返回一个日期的特定部分。例如:

  select datepart(year,getdate()),datepart(month,getdate())  执行结果:2012  4 

(4) 类型转换函数

 1) cast(expression as data_type)

 2) Convert(date_type,express)

  例如:select cast (’123’as int),cast(‘2012/4/9’ as dattime),Convert(datetime,’2012/4/9’)

  1. 空值处理函数

(1) isNull(express,value):如果express不为空则返回express,否则返回value。例如:

  select isNull(Fname,’佚名’) as 姓名 from T_Employee

  1. Case函数用法

(1) 单值判断,相当于switch case,语法为:

 case expression

when value1 then returnvalue1

when value2 then returnvalue2

when value3 then returnvalue3

else defaultreturnvalue

end

举例如下:select FName,

         (

                     when 1 then ‘普通用户’

                     when 2 then ‘会员’

                     when 3 then ‘VIP’

              else ‘未知客户类型’

              end

       ) as 客户类型 from T_Customer

  1. 索引Index

(1) 全表扫描,对数据进行检索(select)效率最差的就是全表扫描,就是一条一条的找。

(2) 如果没有目录,查询汉语字典就要一页页的翻,而有了目录,只要查询目录即可,为了提高检索的速度,可以为经常检索的列添加索引,相当于创建目录。

(3) 创建索引的方式,在表设计器中单击右键,选择”索引/键”到添加到在列中选择索引包含的列。

(4) 使用索引能够提高查询效率,但是索引也是占据空间的,而且添加,删除,更新数据的时候也需要同步更新索引,因此会降低insert,update,delete的速度,只有在经常检索的字段(where)上创建索引。

(5) (*)即使创建了索引,任然有可能全表扫描,比如:link,函数,类型转换等。

  1. 表连接ioin

(1) select o.BillName,c.Name,c.Age

 from T_Orders as o join T_Customers as c on o.customerID=c.ID

注:简单的join可以将两张表按照一定的规律连接起来,使我们可以再两张甚至更多的表之间进行查询。

  1. 子查询

(1)  将一个查询语句做成一个结果集供其它SQL语句使用,就像使用普通的表一样,被当做结果集的查询语句被称为子查询。所有可以使用表的地方几乎都可以使用子查询来代替。

  select * from (select * from T2 where FAge<30)。

原文地址:https://www.cnblogs.com/hanyinglong/p/2438684.html