SQL 細節知識積累

SQL 細節知識積累<script language="javascript" type="text/javascript"> document.title="SQL 細節知識積累 - "+document.title </script>

Transact_sql:

  TRUNCATE TABLE test   //用於刪除test表中的數據
  CREATE FUNCTION        //創建用戶自定義函數
  ALTER FUNCTION         //語句修改
  DROP FUNCTION          //除去用戶定義函數
  CREATE UNIQUE CLUSTERED INDEX client_id ON clients(client_id)

sql:
  (1).   group by ... with cube having sum(number)>=40
          下面兩語句是爲了保持前後兼容
         compute
         compute by
          如果使用COMPUTE BY,則必須也使用ORDER BY子句.
  (2).   JOIN ON 用法
           select book.name,tool.name from book JOIN tool ON (boo.id==tool.id)
         INNER JOIN ON (內聯接:用於消除與另一個表中的任何不匹配的行)
           select book.name,tool.name from book JOIN tool ON (boo.id==tool.id)          
         LEFT OUTER JOIN ON (左外聯接:)
         FULL OUTER JOIN (完整外部聯接:不管另一個表是否有匹配的值,此運算符都包括兩個表中所有行)
         CROSS JOIN (交叉連接:顯示可能的組合)

  (3).   使用@@ERROR全局變量處理錯誤:
         SQL Server的所有錯誤都存儲在系統表master.dbo.sysmessages中.用戶定義的消息也可以存儲在sysmessages中.如果需要,可以使用RAISERROR語句將這些用戶定義的錯誤返回到一個應用程序.若出現一個錯誤,則返回一條錯誤信息.
    例子: use test
        drop table text1
        go
        create table text1(c1 int,c2 text)
        exec sp_tableoption 'text1','text in row','on'
        insert text1 values(1,'This is a text.')
        go
        select * from text1
        go
  (4). 檢索ntext,text,image
       首先看例題: use test
          go
          create table text1(c1 int,c2 text)
          exec sp_tableoption 'text1','text in row','on'
          insert text1 values('1','This is a text.')
          go
          select * from text1
          go
    有幾種方法:
      1.在SELECT語句中引用該列
      2.使用TEXTTPTR函數
      3.使用SUBTRING函數
      4.使用PATINDEX函數
    例題:
      a. exec sp_tableoption 'text1','text in row','off'
         declare @ptrval varbinary(16)
         select @ptrval=TEXTPTR(c2)
            from text1
         READTEXT text1.c2 @ptrval 0 7
      b. select substring(c2,1,7) as c2
         from text1
      c. use text
         go
         select patindex('%a%',c2) as 起始位置
            from text1
         go
 (5).修改ntext,text或image值
     可以使用下面的幾種方式來修改:
        1. 使用數據庫API函數;
        2. 使用WRITETEXT語句重寫該列的整個數據值;
        3. 使用UPDATETEXT語句更新ntext,text或image列的特定數據塊.      
   example:
      1. use test
         go
         declare @ptrval varbinary(16)
         exec sp_tableoption 'text1','text in row','off'
         select @ptrval=textptr(c2) from text1
         writetext text1.c2 @ptrval 'This is a modified text.'
         go
         select * from text1
         go
     2.  use text
         go
         declare @ptrval varbinary(16)
         select @ptrval=textptr(c2)
              from text1
         updatetext text1.c2 @ptrval NULL 0 'this is an inserted text.'
         go
         select * from text1
         go
(6).顯示事務:需要顯示的定義事務的啓動和結束.它是通過BEGIN TRANSACTION,COMMIT TRANSACTION,COMMIT WORK,ROLLBACK TRANSACTION,ROLLBACK WORK 等Transact-SQL語句來完成的.
      1.啓動事務使用BEGIN TRANSACTION語句.如果遇到錯誤,以後的語句能自動回滾.
      2.結束事務使用COMMIT TRANSACTION語句.該事務中的所有修改都將永久有效.事務佔用的資源被釋放.
      3.回滾事務使用ROLLBACK TRANSACTION清除自事務的起點或到某個保存點所有數據修改.ROLLBACK不釋放由事務控制的資源.   
      4.保存點使用SAVE TRANSACTION語句.如果有條件地取消事務的一部分,事務可以返回的位置.
   example:
       use bookdb
       go
       begin tran MyTran  --啓動事務
       insert into book values(9,'Windows 2000 Professional 看圖速成',1,35,'2')
       save tran MySave  --保存點
       delete book where book_id=9  --刪除記錄
       rollback tran MySave  --回滾事務
       commit tran
       go
       select * from book   
    5. 標記事務用WITH MARK選項使事務名置於事務日誌中.將數據庫還原到早期狀態時,可使用標記事務替代日期和時間.
(7).自動提交事務
(8).自定義鎖
    自定義鎖超時:
     set lock_timeout 1800
     go
     declare @timeout int
     select @timeout=@@lock_timeout
     print @timeout
     go
    自定義隔離級別:
     use bookdb
     go
     set transaction isolation level serializable
     go
     begin transaction
     select *from book
     go
     dbcc useroptions--(顯示當隔離級別) 
     go
    鎖定提示:
      use bookdb
      go
      set transaction isolation level serializable
      go
      begin transaction
      select book_name from book with (nolock)
      exec sp_lock --(顯示鎖定)
      go
     返回數據庫被鎖定對象
      select object_name(725577623)
      go  
(9)遊標
   SQL Server支持四種API服務器遊標:
      * 靜態遊標
      * 動態遊標
      * 只進遊標
      * 鍵集驅動遊標
    ROWCOUNT返回上次操作影響的行數.
        use bookdb
        go
        set nocount on
        update book set book_name='LINUX教程'
           where book_id=20
        if @@ROWCOUNT=0
            print '沒有行被更新!'
        go
     遊標示例:
        --聲明遊標
        declare book_cursor cursor
            for select *from book
        open book_cursor
        --提取一行數據
        fetch next from book_cursor
        close book_cursor
        deallocate book_cursor  
(10)視圖
     use bookdb
     go
     create view dbo.book_total
     as
     select abo.book.book_name as 書名,
        abo.book.price as 價格,
        abo.orderform.book_number as 數量,
        abo.book.price*abo.orderform.book_number
           as 總額
      from abo.orderform inner join dbo.book on dbo.orderform.book_id=abo.book.book_id 
      go
    通過視圖修改數據 
 use test
        go
 /*如果表Table1存在,則刪除*/
 if exists(select table_name from information_schema.tables
            where table_name='Table1')                 
  drop table Table1
 go
 /*如果視圖View1存在,則刪除*/
 if exists(select table_name from information_schema.views
       where table_name='View1')
         drop view View1
 go
 /*創建表Table1*/
 create table Table1(column_1 int,column_2 varchar(30))
  go
 /*創建視圖View1*/
 create view View1 as select column_2,column_1
 from Table1
 go
 /*通過視圖View1插入一筆記錄*/
 insert into View1 values('Row1',1)
 /*檢查結果*/
 select * from Table1 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章