最精簡的SQL教程

來源:互聯網
上載者:User

create/drop/alter/use/execute 
create table [tb_UserInfo]
(
    [UserID] [uniqueidentifier] NOT NULL  DEFAULT (newid()) PRIMARY KEY,
    [UserName] [nvarchar](50) NOT NULL,
    [PassWord] [nvarchar](50) NOT NULL,
)    
create table [tb_Message] 
(
    [MessageID] [uniqueidentifier] NOT NULL  DEFAULT (newid()) PRIMARY KEY,
    [UserID] [uniqueidentifier] NOT NULL,
    [MessageBody] [nvarchar](max) NOT NULL,
    [PostTime] [datetime] NOT NULL,
)
--單表操作
truncate table [tb_UserInfo] --Clear
insert into [tb_UserInfo] ([UserName], [PassWord]) values ('a','a')
insert into [tb_UserInfo] ([UserName], [PassWord]) values ('b','b')
insert into [tb_UserInfo] ([UserName], [PassWord]) values ('c','c')
alter table [tb_UserInfo] add Column_test int --drop column
alter table [tb_UserInfo] drop column Column_test
select * from [tb_UserInfo] where [UserName] not in ('a','b') --between and/like%/<>/用()組成複雜條件
select count(distinct [UserName]) from [tb_UserInfo]
select [UserName] from [tb_UserInfo] group by [UserName] having [UserName] <> 'a' --有group by就不能有where,而用having作為限制
--多表聯查
(select [UserID] from [tb_UserInfo]) union all (select [UserID] from [tb_Message]) --講2個語句的結果合并(只union會消除重複)/except/intersect取交集
select * from [tb_UserInfo], [tb_Message] where [tb_UserInfo].[UserID] <> [tb_Message].[UserID] --等值聯合和不等值聯合
select * from [tb_UserInfo] inner join [tb_Message] on [tb_UserInfo].[UserID] = [tb_Message].[UserID] --內部聯合和外部聯合/left、right以相應的為主,另一邊沒有則null
--
begin transaction trans_name
update [tb_UserInfo] set [UserName] = 'd', [PassWord] = 'd' where [UserName] = 'b'
delete from [tb_UserInfo] where [UserName] = 'c' 
commit --能執行的都執行/rollback全能執行才執行 

Create Trigger [tri_name]
      On [tb_UserInfo]                        
      for Update, insert, delete                        
    As                                       
      if Update([UserName])           
      begin
        select [UserName] + ',' + [UserName] from [tb_UserInfo]     
      end 
select * from sysobjects where xtype='TR' --查看觸發器
alter table [tb_UserInfo] enable trigger [tri_name] -- disable
create view [view_UserInfo] as select * from [tb_UserInfo] --視圖僅用於簡化複雜查詢可增刪改但限制很多
create unique clustered index [index_UserInfo] on [tb_UserInfo]([UserName], [PassWord]) -- unique = distinct/clustered群集
create table #temp (a int) --放在tempdb中/與session類似所以並行作業安全/##全域暫存資料表,用於不同session共用
--遊標和簡單編程
declare cursor_name scroll cursor for select [UserID] from [tb_UserInfo] for update
open cursor_name
declare @a [nvarchar](50)
fetch cursor_name into @a
while(@@fetch_status = 0)
begin
    print @a
    fetch cursor_name into @a
end
close cursor_name
deallocate cursor_name 
--scroll/insensitive 否則唯讀向前/在暫存資料表中建立不影響原有資料
--for read only/update

create procedure [dbo].[sp_test] 
(
    @UserName [nvarchar](50),
    @PassWord [nvarchar](50) output 
)
as
    select @PassWord = [PassWord]  from [tb_UserInfo] where [UserName] = @UserName
execute sp_test 'd','f'
drop procedure sp_test 

註:本文引用自http://www.cnblogs.com/hbreset/archive/2008/09/11/1289351.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.