程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> 經典SQL語句集錦(收藏版)

經典SQL語句集錦(收藏版)

編輯:關於SqlServer
下列語句部分是MsSql語句,不可以在Access中使用。

SQL分類:
DDL—數據定義語言(CREATE,ALTER,DROP,DECLARE)
DML—數據操縱語言(SELECT,DELETE,UPDATE,INSERT)
DCL—數據控制語言(GRANT,REVOKE,COMMIT,ROLLBACK)

首先,簡要介紹基礎語句:
1、說明:創建數據庫
CREATE DATABASE database-name
2、說明:刪除數據庫
drop database dbname
3、說明:備份SQL Server
--- 創建 備份數據的 device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
--- 開始 備份
BACKUP DATABASE pubs TO testBack
4、說明:創建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根據已有的表創建新表:
A:create table tab_new like tab_old (使用舊表創建新表)
B:create table tab_new as select col1,col2… from tab_old definition only
5、說明:刪除新表
drop table tabname
6、說明:增加一個列
Alter table tabname add column col type
注:列增加後將不能刪除。DB2中列加上後數據類型也不能改變,唯一能改變的是增加varchar類型的長度。
7、說明:添加主鍵: Alter table tabname add primary key(col)
說明:刪除主鍵: Alter table tabname drop primary key(col)
8、說明:創建索引:create [unique] index idxname on tabname(col….)
刪除索引:drop index idxname
注:索引是不可更改的,想更改必須刪除重新建。
9、說明:創建視圖:create view vIEwname as select statement
刪除視圖:drop view vIEwname
10、說明:幾個簡單的基本的sql語句
選擇:select * from table1 where 范圍
插入:insert into table1(field1,fIEld2) values(value1,value2)
刪除:delete from table1 where 范圍
更新:update table1 set fIEld1=value1 where 范圍
查找:select * from table1 where fIEld1 like ’%value1%’ ---like的語法很精妙,查資料!
排序:select * from table1 order by field1,fIEld2 [desc]
總數:select count * as totalcount from table1
求和:select sum(fIEld1) as sumvalue from table1
平均:select avg(fIEld1) a
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved