當(dāng)前位置: 首頁IT技術(shù) → 10個(gè)常見TSQL語句 讓你工作事半功倍

10個(gè)常見TSQL語句 讓你工作事半功倍

更多

  1、先從建表開始吧:

  use ss

  create table lxp_table

  (

  coll1 char(50) not null,

  coll2 int,

  coll3 int identity(1, 1) not null 自動(dòng)增長1

  primary key (coll3) /*建立主鍵*/

  )

  create table lxp_b

  (

  b1 varchar not null,

  b2 varchar not null,

  b3 int identity(1,1) not null,

  primary key(b3)

  )

  2、修改表的名字

  EXEC sp_rename ‘lxp_table’, ‘lxp_a’

  3、修改列名

  Exec sp_rename ‘lxp_a.[coll1]‘,’a1′

  exec sp_rename ‘lxp_a.[coll2]‘,’a2′

  exec sp_rename ‘lxp_a.[coll3]‘,’a3′

  4、添加新列

  alter table lxp_a

  add a_3 varchar

  exec sp_rename ‘lxp_a.[a_3]‘,’a4′

  5、修改列的類型

  alter table lxp_a

  alter column a4 char(50)

  修改類型時(shí)只能向能轉(zhuǎn)換成的數(shù)據(jù)類型修改(修改類型時(shí)系統(tǒng)會(huì)自動(dòng)將此列數(shù)據(jù)轉(zhuǎn)換若無法轉(zhuǎn)換則無法修改)

  6、創(chuàng)建表時(shí)相應(yīng)的添加外鍵

  create table a_b

  (

  a_id int not null

  constraint aa foreign key(a_id) references lxp_a(a3), –創(chuàng)建表時(shí)相應(yīng)的添加外鍵

  b_id int not null

  )

  drop table a_b

  7、在已經(jīng)創(chuàng)建好的表中添加外鍵

  alter table a_b

  add constraint bb foreign key (b_id) references lxp_b(b3)

  8、在已經(jīng)創(chuàng)建好的表中刪除外鍵

  alter table a_b

  drop bb

  9、查詢出誰連接著數(shù)據(jù)庫

  select * from master..sysprocesses where hostname<>”

  exec sp_who

  10、查詢指定數(shù)據(jù)庫的相關(guān)信息

  select * from sysobjects where type = ‘U’;

  select name from sysobjects where type = ‘F’;

  select name from sysobjects where type = ‘P’;

  由于系統(tǒng)表sysobjects保存的都是數(shù)據(jù)庫對象,其中type表示各種對象的類型,具體包括:

  U = 用戶表

  S = 系統(tǒng)表

  C = CHECK 約束

  D = 默認(rèn)值或 DEFAULT 約束

  F = FOREIGN KEY 約束

  L = 日志

  FN = 標(biāo)量函數(shù)

  IF = 內(nèi)嵌表函數(shù)

  P = 存儲過程

  PK = PRIMARY KEY 約束(類型是 K)

  RF = 復(fù)制篩選存儲過程

  TF = 表函數(shù)

  TR = 觸發(fā)器

  UQ = UNIQUE 約束(類型是 K)

  V = 視圖

  X = 擴(kuò)展存儲過程及相關(guān)的對象信息。

  PS:打開數(shù)據(jù)庫

  use DNN_LH_493

  11、查詢出所有用戶數(shù)據(jù)庫

  exec sp_databases

  12、查詢出指定數(shù)據(jù)庫下的所有表

  use ss

  exec sp_tables

熱門評論
最新評論
發(fā)表評論 查看所有評論(0)
昵稱:
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)