微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

转载的一篇sqlserver文章

公司组织sql水平考试,看了写教材,写了点总结发上来跟大家分享
我公司使用的是Sybase ASE12.5,所以下面的一些特性是针对Sybase ASE的。
一:sql Bisic
1:sql(Structured Quary Language)特性:
a:标准化
b:非过程化的
c:可优化的
d:面向集合操作的

2:ASE中的数据类型
a:Numberic
b:Character
c:Date/Time
d:Lobs

3: convert(varchar,textColumn),如果不指定varchar(n)n那么认是30

4:where 在sql中的作用
a:过滤数据
b:做表连接(sql92以前)
c:选择索引

5:whare 和 having的区别
where语句把过滤好的数据插入到work table中
having语句从work table中对数据进行在过滤以得到最后的结果。

6:一个select语句的执行顺序
a:from clause
b:where clause
c:group by clause
d:select clause
e:having clause
f:order by clause

7:Union VS Union All
a:Union 会把两个结果集排序,并且除去重复的元素(效率差,轻易不要用)
b:Union All仅仅是把两个结果集合并,没有排序,也不去除重复元素(效率好)

二:索引和查询参数
1:ASE中有三种access数据方式
a:clustered Index
b:nonclustered Index
c:table scan

2:Covered Query
一个Covered Query 仅仅从索引中得到数据,不用去扫描数据库表,这是最快的数据查询方式。
限制1:只能在selece中生效
限制2:所有被引用的列必须在同一个nonclustered index中

3:functional index
在ASE15.0以后才被支持,也就是说在ASE15.0以前的版本,下列语句是可定不会用上索引的

 
  1. select  column1  
  2. from  table1  
  3. where   upper (column2) = 'IVANL'  


4:如何查看执行计划

 
  1. set  showplan  on   
  2. go  
  3. your sql  
  4. go  
  5. set  showplan  off   
  6. go  


5: 如何查看IO

 
  1. set   statistics  io  on   
  2. set   statistics   time   on   
  3. go  
  4. you sql  
  5. go  
  6. set   statistics  io  off   
  7. set   statistics   time   off   
  8. go  


6:使用Index的建议
a:使用那些经常在where语句中使用的字段做index
b:使index中包含的字段越少越好
c:drop掉没用的index

三:表连接
1:什么是表连接
表连接是从多表中查询数据,或者是从一个表中多次取数据。
(A join is a Transanct-sql operation than access rows from multi-tables or from a single talbe multi-times)

2:表连接的类别
a:inner join
b:outer join
c:cross join(full join)

3:ASE中不支持full join但是通过union可以模拟full join

 
  1. select  t1.colu1, t2.column2  
  2. from  t1, t2  
  3. where  t1.id *= t2.id  
  4. union   
  5. select  t1.colu1, t2  
  6. where  t1.id =* t2.id  


(不建议使用,效率很差)

4:ASE中最多支持50个table做表连接,ASE的查询优化器做的不是很好,Sybase推荐join表不超过4个(-_-~!)

5:数据库中有三种方式来实现表连接
a:nested loop join
b:merge join
c:hash join
(可以使用show plan来查看数据库选用哪种join来实现join语句)

6:对表连接的建议:
a:用showplan 看使用了那种用join方式
b:在join的列上加Index
c:把多表的join才分成几个小表的join
d:避免产生笛卡儿积

四:使用Case语句
1:case语句的两种形式

 
  1. a:  
  2. case   
  3.   when  search_condition  then  expression  
  4.   [when  search_condition  then  expression]  
  5.   [else  exproestion]  
  6. end   
  7. b:  
  8. case  expression  
  9.   when  expression  then  expression  
  10.   [when  exproession  then  expression]  
  11.   [else  expression]  
  12. end     



2:case的用途
a:decoding column

 
  1. select  cust_id, cust_name  
  2. case  cust_type  
  3.   when  'R'  then  'Relation'  
  4.   when  'I'  then  'International'  
  5.   when  's'  then  'Small'  
  6.   else   'Other'  
  7. end   as  customer_type  


b:conditionally displaying columns or values

 
  1. select  title_id, total_sales,  
  2. case   
  3.   when  total_sales > 5000  then  'hight'  
  4.   when  total_sales < 100  then  'low'  
  5.   else  '   '  
  6. end   as  ' column '  


c:horizontal frequency table and summary calculation

 
  1. select   sum ( case  type  when  'adv'  then  1  else  0  end  )  as  adv  
  2. sum case  type  when  'cus'  then  1  else  0  end as  cus  
  3. from  customer  


d:updating on variable conditions

 
  1. update  customer  
  2. set  cust_charge = cust_charte +  case  cust_type  
  3. when  'd'  then  1  
  4. when  'c'  then  2  
  5. when  'e'  then  3  
  6. else  0  
  7. end   
  8. [/code]  
  9. e:rules and   check  constraints  
  10. [code]  
  11. create   table  cust_order_info  
  12. (  
  13.   order_num int ,  
  14.   order_taker int ,  
  15.   order_date char (7)  default   
  16.     case   
  17.       when  datepart(dw, getDate())  between  2  and  6  then  'weekday'  
  18.       else  'weekend'  
  19.     end   
  20. )  



五:事务和锁
1:ASE中有两种事务模式
a: Chained Mode
b:unChained Mode(Sybase认)
unchained mode显示的开始一个事务,chained隐式的开始一个事务
unchained mode 使用'commint tran','rollback tran'
chained mode 使用'commint work ','rollback work'
unchained mode 支持嵌套事务,chained mode不支持

2:Locking schema
a: All pages table,will lock data and index as they are accessed(可以有clustered index)
b: A Datapages table will lock datpages as they are accessed,index will not be locked(无clustered index)
c: A DaTarow table will lock datpages as they are accessed,index will not be locked(无clustered index)

3:Locking type
ASE中最重要的三种lock type是
a:shared locks(select,fetch)
b:update locks(fetch,update,delete)
c:exclusive locks(insert,delete)

4:隔离级别
ASE中一共有四种隔离级别
a:isolation level 0 (read uncommited),允许胀读
b:isolation level 1 (read comminted)(ASE DEFAULT),不允许胀读
c:isolation level 2 (repeatable read),可重复读
d:isolation level 3 (serializable),不允许幻影读

 
  1. set   transaction   isolation   level  {0|1|2|3}  
  2. or   
  3. select  ...  
  4. at   isolation  {0|1|2|3}  



5:如何编写高效的transaction
For OLTP transaction
a:使transaction尽可能的短
b:使用index来随机访问数据
c:只有在必要的时候才使用transaction
d:选取合适的Lock type和隔离级别
e:使用乐观锁

六:数据处理
1:除以0
使用coalesce()和nullif()
先使用nullif()把0转换成null,在用coalesce()处理null的情况

 
  1. select   coalesce (total_sales/ nullif (sales,0),0) 
  2. -- coalesce(ex1,ex2,ex3...)返回第一个不是Null的表达式
    -- nullif(expre,value)如果expre=value,则返回null




2:找到重复的数据

 
  1. select  type,  count (*)  
  2. from   table   
  3. where  ..  
  4. group   by  type  
  5. having   count (*) > 1  



3:找出重复次数最多的数据

 
  1. select  type,  count (*)  
  2. from   table   
  3. where  ..  
  4. group   by  type  
  5. having   count (*) =  max ( count (*))  


4:数据累加

java 代码
  1. select t1.title_id, t1.advice, sum(t2.advice) as cumulative_total  
  2. from title t1, title t2  
  3. where t1.title_id >= t2.title_id  
  4. group by t1.title_id, t1.advice  



5:ranking data

 
  1. select  rank = identity(10), title_id, total_sales  
  2. into  # top   from  titles  
  3. where  ..  
  4. order   by  total_sales  desc   
  5. go  
  6. select  *  from  # top   
  7. go  
  8. drop   table  # top   
  9. go  



6:conver between julian Date and gregorian date

 
  1. select  datepart(yy, @ date )*1000+datepart(dy, @ date as  julina_date  
  2. select  dateadd(dd, juliandate%1000, '12/31/'+ convert ( char (4),juliandate/1000 -1))  as  gregorian_date  



7:计算本月有多少天

 
  1. datepart(dd,  
  2. dateadd(dd,-1           --last day of this month   
  3. datead(mm,1             --add a month   
  4. dateadd(dd              --   
  5. ,  
  6. 1-datepart(dd,getdate() --1-today   
  7. getDate()))))              --get today   


8:是否是闰年

 
  1. select  datepart(dy, '03/01/'|| convert ( char (4),datepart(yy,getdate())))  
  2. --= 61 是闰年   
  3. --= 60 不是闰年    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐