詳解Sql基礎(chǔ)語法
1.創(chuàng)建數(shù)據(jù)庫
create database 數(shù)據(jù)庫名稱
2.刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名稱
3.備份sql server
創(chuàng)建備份數(shù)據(jù)的device
use master
exec sp_addumpdevice '名稱','新的名稱','路徑'
開始備份
backup database pubs to 新的名稱
4.創(chuàng)建表
create table 表名(列名1 類型,列名2 類型)
5.根據(jù)已有表創(chuàng)建新表
create table 新表名稱 like 舊表名稱
create table 新表名稱 as select 列名 from 舊表名稱 defintion only
6. 增加一個列
Alter table 表名稱 add 列名稱 數(shù)據(jù)類型
7.添加主鍵
alter table 表名稱 add primary key(列名稱)
8.自增id屬性從1開始每次加1
identity (1,1)
9.創(chuàng)建索引
create index 索引名 on 表名(列名)
10.刪除索引
drop index idx_name
11.CTE查詢
;with t as( select openid,ROW_NUMBER()over(partition by openid order byopenid)as row from #temp)delete t where row>1
12.case when的用法(修改表名稱id,當(dāng) t 的名字不為空,則還是 t 的名字,否則變?yōu)楸砻Q的名字,把被修改的數(shù)據(jù)輸出到臨時表)
update pn set pn.id=case when t.id>'' then t.id else pn.id end output deleted.id into 臨時表 from 表名稱 pn with(nolock) join #temp t
13.查詢、插入、刪除、求和、平均、最大值
select * from table insert into new_table(id,name)values(1,'張三') delete from table where 范圍 select sum(field1) as sumvalue from table1 select avg (field1) as avgvalue from table1 select max(field1) as maxvalue from table1
通過以上內(nèi)容給大家詳解Sql基礎(chǔ)語法,希望本文介紹能夠給大家?guī)韼椭?/p>
相關(guān)文章
Sql Server中Substring函數(shù)的用法實例解析
在sqlserver中substring函數(shù)是用來處理字符串的,常用于字符串截取了,下面我來給大家介紹下Sql Server中Substring函數(shù)的用法實例解析,需要的朋友參考下吧2016-12-12
SQL批量插入數(shù)據(jù)幾種方案的性能詳細(xì)對比
昨天下午快下班的時候,無意中聽到公司兩位同事在探討批量向數(shù)據(jù)庫插入數(shù)據(jù)的性能優(yōu)化問題,頓時來了興趣,把自己的想法向兩位同事說了一下,于是有了本文。2010-03-03

