About | NetLify | NeoCities | Project | TEST | 管理

<<常用的 SQL 語法>>

常用的 SQL 語法說明[1]:

結構化查詢語言(Structured Query Language,SQL),是一種資料庫程式語言,用於資料庫中的標準資料查詢語言,IBM公司最早使用在其開發的資料庫系統中。1986年10月,美國國家標準學會(ANSI)對SQL進行規範後,以此作為關聯式資料庫管理系統的標準語言(ANSI X3. 135-1986),1987年得到國際標準組織的支援下成為國際標準。不過各種通行的資料庫系統在其實踐過程中都對SQL規範作了某些修改和擴充。

所以,實際上不同資料庫系統之間的SQL不能完全相互通用。本文會說明常用且標準的通用SQL語法,再撰寫SQL語法時應儘量依詢ANSI-92標準,讓資料庫的轉換難度降低,進而才能支撐我所強調主張的「以資料庫為開發核心」的開發理念。

 

SQL語法主要包含3個部分[1]:

「資料定義語言」(DDL : Data Definition Language)

「資料操縱語言」(DML : Data Manipulation Language)

「資料控制語言」(DCL : Data Control Language)

 

SQL 中最重要的 DDL 語句[1]:

CREATE DATABASE -創建新資料庫

ALTER DATABASE -修改資料庫

CREATE TABLE - 創建新表

ALTER TABLE - 變更(改變)資料庫表

DROP TABLE - 刪除表

CREATE INDEX - 創建索引(搜索鍵)

DROP INDEX - 刪除索引

 

SQL DML 語句[1]:

SELECT - 從資料庫表中獲取資料

UPDATE - 更新資料庫表中的資料

DELETE - 從資料庫表中刪除資料

INSERT INTO - 向資料庫表中插入資料

 

SQL 語句範例[1]:

建立新資料庫

語法:CREATE DATABASE database-name
實例:CREATE DATABASE SampleDB

刪除資料庫

語法:DROP DATABASE database-name
實例:DROP DATABASE SampleDB

新增資料表以在前面的系列文中實作,此處不在贅述。

刪除表(Table)

語法:DROP TABLE tabname
實例:DROP TABLE MEMBER

增加一個欄位

語法:alter table tabname add column col type
實例:alter table member add newfield varchar(20) default ''

增刪主鍵

添加主鍵: alter table tabname add primary key(col);
刪除主鍵: alter table tabname drop primary key(col);

選取資料

select * from member where memberid >= '0' and memberid <= '0000000100'
select * from member where memberid like '%10%'

新增資料

insert into member (memberid,membername) values('m099', 'michael chen');
insert into member(memberid,membername)
select custid,coprcname from bascustomer where custid like 'm%'

刪除資料

delete from member where memberid > '0000000900';

更新資料

update member set memberlavel = '00' where memberid <= '0000000100;

排序

select * from member order by memberid;

計算筆數

select count(*) as totalcount from member;

合計

select productid,sum(quantity) as sumvalue from opoorder1_d group by productid;

求平均值

select billno,avg(totalamount) as avgvalue from opoorder1_m group by billno;

取最大值

select custid,max(totalamount) as maxvalue from opoorder1_m group by custid;

取最小值

select custid,min(billdate) as maxvalue from opoorder1_m group by custid;

 

UNION 及 LEFT JOIN

union [all] union 運算子通過組合其他兩個結果表(例如 table1 和 table2)並消去表中任何重複行而派生出一個結果表。當 all 隨 union 一起使用時(即 union all),不消除重複行。

select 1 as flag,a.billno,b.billdate,a.subaqmount
from opoorder1_d a left join opoorder1_m b on a.billno = b.billno
union all

select 2 as flag,a.billno,b.billdate,a.subaqmount
from opoorder2_d a left join opoorder2_m b on a.billno = b.billno;

 

子查詢

select * from member where memberid
IN (select custid from bascustomer where custid like 'M0%')

select * from member where memberid IN ('M001','M009','M012')

select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

 

動態SQL語法執行

declare @start int,@end int
@sql nvarchar(600)
set @sql=’select top’+str(@end-@start+1)+’+from T where rid not in(select top’+str(@str-1)+’Rid from T where Rid>-1)’
exec sp_executesql @sql

 

資料庫取回分頁資料

/* 選擇從10到15的記錄 */

select top 5 * from (select top 15 * from table order by id asc) table_別名 order by id desc

 

參考文獻:

https://ithelp.ithome.com.tw/articles/10203697

 

Jplopsoft | THI | Netlify | NeoCities | LionFree

加密工具 | 提交歸檔 | QRCODE產生器 | 密碼產生器

アクセスカウンター