詳解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>
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信