sql用法

創(chuàng)建數(shù)據(jù)庫(kù)

創(chuàng)建之前判斷該數(shù)據(jù)庫(kù)是否存在

if exists (select * from sysdatabases where name=databasename) drop database databasename

go

create database database-name

刪除數(shù)據(jù)庫(kù)

drop database dbname

備份sql server

--- 創(chuàng)建備份數(shù)據(jù)的 device

use master

exec sp_addumpdevice disk, testback,

c:\mssql7backup\mynwind_1.dat

--- 開(kāi)始備份

backup database pubs to testback

創(chuàng)建新表

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

根據(jù)已有的表創(chuàng)建新表:

a:go

use 原數(shù)據(jù)庫(kù)名

go

select * into 目的數(shù)據(jù)庫(kù)名.dbo.目的表名 from 原表名(使用舊表創(chuàng)建新表)

b:create table tab_new as select col1,col2? from tab_old definition only

創(chuàng)建序列

create sequence simon_sequence

minvalue 1 -- 最小值

maxvalue 999999999999999999999999999 -- 最大值

start with 1 -- 開(kāi)始值

increment by 1 -- 每次加幾

cache 20;

刪除新表

drop table tabname

增加一個(gè)列

alter table tabname add colname coltype

刪除一個(gè)列

alter table tabname drop column colname

添加主鍵

alter table tabname add primary key(col)

說(shuō)明:刪除主鍵:alter table tabname drop primary key(col)? 創(chuàng)建索引

create [unique] index idxname on tabname(col?。)

刪除索引:drop index idxname on tabname

注:索引是不可更改的,想更改必須刪除重新建。

創(chuàng)建視圖

create view viewname as select statement

刪除視圖:drop view viewname

簡(jiǎn)單基本的sql語(yǔ)句

(1) 數(shù)據(jù)記錄篩選:

sql=select * from 數(shù)據(jù)表 where 字段名=字段值 order by 字段名

[desc]

sql=select * from 數(shù)據(jù)表 where 字段名 like %字段值% order by 字段名 [desc]

sql=select top 10 * from 數(shù)據(jù)表 where 字段名 order by 字段名

[desc]

sql=select * from 數(shù)據(jù)表 where 字段名 in (值1,值2,值3)

sql=select * from 數(shù)據(jù)表 where 字段名 between 值1 and 值2

(2) 更新數(shù)據(jù)記錄:

sql=update 數(shù)據(jù)表 set 字段名=字段值 where 條件表達(dá)式

sql=update 數(shù)據(jù)表 set 字段1=值1,字段2=值2 ?? 字段n=值n where 條件表達(dá)式

(3) 刪除數(shù)據(jù)記錄:

sql=delete from 數(shù)據(jù)表 where 條件表達(dá)式

sql=delete from 數(shù)據(jù)表 (將數(shù)據(jù)表所有記錄刪除)

(4) 添加數(shù)據(jù)記錄:

sql=insert into 數(shù)據(jù)表 (字段1,字段2,字段3 ?) values (值1,值2,值3 ?)

sql=insert into 目標(biāo)數(shù)據(jù)表 select * from 源數(shù)據(jù)表 (把源數(shù)據(jù)表的記錄添加到目標(biāo)數(shù)據(jù)表)

(5) 數(shù)據(jù)記錄統(tǒng)計(jì)函數(shù):

avg(字段名) 得出一個(gè)表格欄平均值

count(*;字段名) 對(duì)數(shù)據(jù)行數(shù)的統(tǒng)計(jì)或?qū)δ骋粰谟兄档臄?shù)據(jù)行數(shù)統(tǒng)計(jì) max(字段名) 取得一個(gè)表格欄最大的值

min(字段名) 取得一個(gè)表格欄最小的值

sum(字段名) 把數(shù)據(jù)欄的值相加

引用以上函數(shù)的方法:

sql=select sum(字段名) as 別名 from 數(shù)據(jù)表 where 條件表達(dá)式 set rs=conn.excute(sql)

用 rs(別名) 獲取統(tǒng)計(jì)的值,其它函數(shù)運(yùn)用同上。

查詢?nèi)コ貜?fù)值:select distinct * from table1

(5) 數(shù)據(jù)表的建立和刪除:

create table 數(shù)據(jù)表名稱(字段1 類型1(長(zhǎng)度),字段2 類型2(長(zhǎng)度) ?? )

幾個(gè)高級(jí)查詢運(yùn)算詞

a:union 運(yùn)算符

union 運(yùn)算符通過(guò)組合其他兩個(gè)結(jié)果表(例如table1 和table2)并消去表中任何重復(fù)行而派生出一個(gè)結(jié)果表。當(dāng) all 隨union 一起使用時(shí)(即union all),不消除重復(fù)行。兩種情況下,派生表的每一行不是來(lái)自table1 就是來(lái)自table2。

b: except 運(yùn)算符

except 運(yùn)算符通過(guò)包括所有在table1 中但不在table2 中的行并消除所有重復(fù)行而派生出一個(gè)結(jié)果表。當(dāng)all 隨except 一起使用時(shí)(except all),不消除重復(fù)行。

c:intersect 運(yùn)算符

intersect 運(yùn)算符通過(guò)只包括table1 和table2 中都有的行并消除所有重復(fù)行而派生出一個(gè)結(jié)果表。當(dāng)all 隨intersect 一起使用時(shí)

(intersect all),不消除重復(fù)行。

注:使用運(yùn)算詞的幾個(gè)查詢結(jié)果行必須是一致的。

使用外連接

a、left outer join:

左外連接(左連接):結(jié)果集既包括連接表的匹配行,也包括左連接表的所有行。

sql: select a.a, a.b, a.c, b.c, b.d, b.f from a left outer join b on a.a = b.c

b:right outer join:

右外連接(右連接):結(jié)果集既包括連接表的匹配連接行,也包括右連接表的所有行。

c:full outer join:

全外連接:不僅包括符號(hào)連接表的匹配行,還包括兩個(gè)連接表中的所有記錄。

編輯本段判斷對(duì)象是否存在 判斷數(shù)據(jù)庫(kù)是否存在

if exists (select* from sysdatabases wherename= 數(shù)據(jù)庫(kù)名) dropdatabase[數(shù)據(jù)庫(kù)名]

判斷表是否存在

if not exists (select * from sysobjects where [name] = 表名 and xtype=u)

begin

--這里創(chuàng)建表

end

判斷存儲(chǔ)過(guò)程是否存在

if exists (select* from sysobjects whereid = object_id(n[存儲(chǔ)過(guò)程名]) and objectproperty(id, nisprocedure) = 1) dropprocedure[存儲(chǔ)過(guò)程名]

判斷臨時(shí)表是否存在

if object_id(tempdb..#臨時(shí)表名) isnot null

droptable#臨時(shí)表名

判斷視圖是否存在

--sql server 2000

if exists (select* from sysviews whereobject_id = [dbo].[視圖名]

--sql server 2005

if exists (select* from sys.views whereobject_id = [dbo].[視圖名]

判斷函數(shù)是否存在

if exists (select* from dbo.sysobjects whereid =

object_id(n[dbo].[函數(shù)名]) and xtype in (nfn, nif, ntf)) dropfunction[dbo].[函數(shù)名]

獲取創(chuàng)建信息

select[name],[id],crdate from sysobjects where xtype=u /*

xtype 的表示參數(shù)類型,通常包括如下這些 c = check約束 d = 默認(rèn)值或default約束 f = foreignkey約束 l = 日志 fn = 標(biāo)量函數(shù) if = 內(nèi)嵌表函數(shù) p = 存儲(chǔ)過(guò)程 pk = primarykey約束(類型是k) rf = 復(fù)制篩選存儲(chǔ)過(guò)程 s = 系統(tǒng)表 tf = 表函數(shù) tr = 觸發(fā)器u = 用戶表 uq = unique約束(類型是k) v = 視圖 x = 擴(kuò)展存儲(chǔ)過(guò)程 */

判斷列是否存在

if exists(select* from syscolumns whereid=object_id(表名) and name=列名)

altertable表名dropcolumn列名

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文講解的知識(shí)點(diǎn)主要有 :公用表表達(dá)式(CTE)、窗口函數(shù)、SQL NULL 函數(shù)及SQL 條件判斷;針對(duì)人群:...
    freddyyao閱讀 1,297評(píng)論 0 0
  • 創(chuàng)建數(shù)據(jù)庫(kù) 創(chuàng)建之前判斷該數(shù)據(jù)庫(kù)是否存在 if exists (select * from sysdatabase...
    才気莮孒閱讀 493評(píng)論 0 1
  • 一、WITH AS 的含義WITH AS 短語(yǔ), 也叫做子查詢部分(subqery factoring),可以定義...
    是歸人不是過(guò)客閱讀 13,941評(píng)論 0 7
  • 一.數(shù)據(jù)控制語(yǔ)句(DML)部分 1.INSERT(往數(shù)據(jù)表里插入記錄的語(yǔ)句) INSERTINTO表名(字段名1,...
    浮浮塵塵閱讀 3,728評(píng)論 0 19
  • 數(shù)據(jù)庫(kù)概念 定義1 數(shù)據(jù)庫(kù)(database)是按照數(shù)據(jù)結(jié)構(gòu)來(lái)組織、存儲(chǔ)、管理數(shù)據(jù)的建立在計(jì)算機(jī)存儲(chǔ)設(shè)備上的倉(cāng)庫(kù) ...
    好好秦先生閱讀 1,231評(píng)論 0 3

友情鏈接更多精彩內(nèi)容