1. 程式人生 > >【sql server常用操作{增刪改查}】

【sql server常用操作{增刪改查}】

use DB_x
  go
  drop database DB_y
  create database DB_y --建立資料庫
  on primary --指定主資料檔案
  (
  name=db, --邏輯名
  filename='d:\db.mdf', --檔案位置
  size=3MB, --初始大小
  maxsize=10MB, --最大增長
  filegrowth=1MB --增長方式
  )
  log on --指定日誌檔案
  (
  name=ldb,
  filename='d:\ldb.ldf',
  size=3MB,
  maxsize=10MB,
  filegrowth=1MB
  )
  alter database DB_y --修改資料庫
  add file --新增主資料檔案或日誌等儲存檔案
  (
  name=dbs,
  filename='d:\dbs.ndf',
  size=1MB,
  maxsize=10MB,
  filegrowth=2%
  )
   
  create partition function x(int) --建立分割槽函式
  as range left for values(10,20,30,50)
   
  create partition scheme x --建立分割槽方案
  as partition x
  to (group1,group2,group3,PRIMARY)
   
  drop table tb_x
  create table tb_x --為表格分割槽
  (
  id int not null ,
  name char(10)
  )
  on x
   
  alter table tb_x add num int
  use DB_x
  select * from tb_x
  union all
  select * from tb
  alter table tb add constraint PK_id primary key(id) --insert兩種插入資料方法
  insert into tb_x values(10,'a',5)
  insert into tb_x(id) select id from tb
  --檢視檢視定義
  exec sp_helptext vi_x
   
  select * from tb
   
  declare @x char(10) --宣告變數
  set @x='hello world' --為變數賦值
  print @x --列印變數
   
  declare @y int
  select @y=id from tb where name='tindy' --用select選擇欄位為變數賦值
  print @y
  select * from tb
  --insert into tb values(1,'b','women')
  update tb set id=1 where id=2
  if @@ERROR=2627 --全域性變數@@ERROR自定義錯誤資訊
  print '你的查詢有錯誤'
  select @@version
  --版本號及開發資訊
  declare @x_x int,@y_y int --區域性變數結合運算子表示式的運算
  set @x_x=10
  set @y_y=90
  print @x_x[email protected]_y
   
  select tb.id ,tb_x.name --T-sql 同mysql處理多表連線
  from tb
  full join tb_x
  on tb.id=tb_x.id
   
  select * from tb
  select sex='man' from tb where not exists( --T-sql子查詢not|exists語句共存判斷條件
  select id='1' from tb
  )
  set showplan_all on --開啟查詢過程索引連線搜尋功能
  select * from tb_x
  set showplan_all off
  --整理資料磁碟檔案
  set statistics IO off
  DBCC showcontig(tb) with fast
   
  if exists(select sex='mans' from tb) --exists執行判斷是否滿足select選擇性刪除索引
  drop index tb_x.in_x
   
  select * from tb
  alter table tb add constraint CK_tb check(id>0) --檢查約束
   
   
   
  begin --begin...end:sql程式碼塊開始與結束控制
  declare @x int,@y char(10),@z char(10)
  set @x=10
  set @y='my'
  set @z='sql'
  print @x
  end
  print @y[email protected]
   
   
  declare @x int,@y int --if..else分支判斷語句
  set @x=0
  set @y=0
  if @x>0
  print '@[email protected]在一四象限'
  else
  print '@[email protected]在座標原點'
  if @x<0
  print '@[email protected]在二三象限'
  else
  print '@[email protected]在座標原點'
   
  select * from tb
   
  select id= --case迴圈分支語句
  case
  when id>1 and id<3 then 'jark'
  when id>3 and id<6 then 'tindy'
  else 'a'
  end
  from tb
   
  declare @i int,@sum int --while迴圈語句
  set @i=1
  set @sum=0
  while @i<=10
  begin
  print '死迴圈'
  /*
  if @i%2=0
  --set @[email protected][email protected]
  --set @[email protected]+1
  continue
  --else
  --print '查詢有誤'
  set @[email protected][email protected]
  set @[email protected]+1
  */
  end
  print '1-100的和是:'
  print @sum
   
  declare @x int
  set @x=10
  print @x
  return --語句返回,忽視下面語句
  print '還可以輸出嗎'
   
   
  declare @y int
  select @y=1
  example:
  print @y
  select @y[email protected]+1
  if @y%2=0
  while @y<10 goto example --跳轉
   
   
  waitfor delay'00:00:01' --語句延遲和執行時間
  drop table tb
  print '現在刪除DB_x庫裡的tb表'
   
  exec sp_addtype y , 'char(10)','not null' --自定義資料型別
  --形參
  create function fun_max(@x int ,@y int ) --自定義函式
  returns int --返回新的資料型別
  as
  begin
  if @x<@y
  set @x[email protected]
  return @x
  end
   
  declare @a int,@b int
  set @a=2
  set @b=10
  print fun_max(@a,@b ) --呼叫自定義的最大值函式,代入實參
   
   
  create function find(@z int) --自定義篩選函式
  returns table
  as
  return(select * from tb_x where id>@z)
  select * from find(4) --賦形參為4,篩選id>4的行
   
  use DB_x
  create procedure pro_tb_x --儲存過程建立
  as
  select * from tb_x
   
  exec sp_helptext pro_tb_x --執行和檢視儲存過程
  use DB_x
  drop table tb_x
  create trigger tri_x --觸發器建立
  on database --對於庫的觸發
  for DROP_TABLE,ALTER_TABLE --觸發條件
  as
  begin
  print '你確定要觸發這個觸發器'
  rollback
  end
   
  drop trigger tri_y
  create trigger tri_y
  on all server --對於伺服器的觸發
  for create_login
  as
  begin
  print '你沒有許可權建立使用者'
  end
   
  create login u1 with password='123'
   
  create trigger tri_z
  on
  for drop_table
  as
  begin
  print '刪除了一張表'
  end