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. 增加一個(gè)列
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 rowfrom #temp)delete t where row>1
12.case when的用法(修改表名稱id,當(dāng) t 的名字不為空,則還是 t 的名字,否則變?yōu)楸砻Q的名字,把被修改的數(shù)據(jù)輸出到臨時(shí)表)
update pnset pn.id=case when t.id>'' then t.id else pn.id endoutput deleted.id into 臨時(shí)表from 表名稱 pn with(nolock)join #temp t
13.查詢、插入、刪除、求和、平均、最大值
select * from tableinsert into new_table(id,name)values(1,'張三')delete from table where 范圍select sum(field1) as sumvalue from table1select avg (field1) as avgvalue from table1select max(field1) as maxvalue from table1
通過以上內(nèi)容給大家詳解Sql基礎(chǔ)語法,希望本文介紹能夠給大家?guī)韼椭?/p>