(本文是參考w3c中的sql教程內(nèi)容,再結(jié)合其他相關材料整理的)
一,、插入(復制)表數(shù)據(jù) 1,、INSERT INTO 語句 (1)插入新的一行數(shù)據(jù) INSERT INTO Persons VALUES ('Gates', 'Bill', 'Xuanwumen 10', 'Beijing'); (2)在指定的列中插入數(shù)據(jù)INSERT INTO Persons (LastName, Address) VALUES ('Wilson', 'Champs-Elysees'); 2、SQL SELECT INTO 語句可用于創(chuàng)建表的備份復件(1)在建表時復制所有數(shù)據(jù): create table userinfo_new as select * from userinfo;
(2)在建表時復制部分數(shù)據(jù):create table userinfo_new1 as select id,username from userinfo;
(3)在添加時復制所有數(shù)據(jù):insert into userinfo_new select * from userinfo;
(4)在添加時復制部分數(shù)據(jù):insert into userinfo_new(id,username) select id,username from userinfo;
二,、修改表數(shù)據(jù)1,、Update 語句 (1)無條件更新: update userinfo set userpwd='111',email='[email protected]'; (2)有條件更新:update userinfo set userpwd='123456' where username='xxx';
三、刪除表數(shù)據(jù)1,、DELETE 語句 無條件刪除: dalete from userinfo; 有條件刪除:delete from userinfo where username='yyy'; 四,、查詢表數(shù)據(jù)1、SELECT 語句 (1)查詢所有字段: select * from users; (2)查詢指定字段:select username,salary from users; 2,、SELECT DISTINCT 語句如需從 Company" 列中僅選取唯一不同的值,,我們需要使用 SELECT DISTINCT 語句: SELECT DISTINCT Company FROM Orders;
3、SQL where 選取居住在城市 "Beijing" 中的人,,我們需要向 SELECT 語句添加 WHERE 子句:
SELECT * FROM Persons WHERE City='Beijing'; 注意:SQL 使用單引號來環(huán)繞文本值(大部分數(shù)據(jù)庫系統(tǒng)也接受雙引號),。如果是數(shù)值,請不要使用引號,。
4,、SQL AND & OR (1)使用 AND 來顯示所有姓為 "Carter" 并且名為 "Thomas" 的人: SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter'; (2)使用 OR 來顯示所有姓為 "Carter" 或者名為 "Thomas" 的人:
SELECT * FROM Persons WHERE firstname='Thomas' OR lastname='Carter'; 5、ORDER BY 語句用于對結(jié)果集進行排序,。 (1)以字母順序顯示公司名稱:
SELECT Company, OrderNumber FROM Orders ORDER BY Company; (2)以字母順序顯示公司名稱(Company),,并以數(shù)字順序顯示順序號(OrderNumber):
SELECT Company, OrderNumber FROM Orders ORDER BY Company, OrderNumber; (3)以逆字母順序顯示公司名稱:
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC; (4)以逆字母順序顯示公司名稱,并以數(shù)字順序顯示順序號:
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC;
6,、TOP 子句 SQL Server 的語法: 從"Persons" 表中選取頭兩條記錄:
SELECT TOP 2 * FROM Persons;
從"Persons" 表中選取 50% 的記錄: SELECT TOP 50 PERCENT * FROM Persons;
MySQL 語法:從"Persons" 表中選取頭兩條記錄:SELECT * FROM Persons LIMIT 2;
Oracle 語法:從"Persons" 表中選取頭兩條記錄: SELECT * FROM Persons WHERE ROWNUM <= 2;
7,、LIKE 操作符、SQL 通配符 (1)從"Persons" 表中選取居住在以 "N" 開始的城市里的人: SELECT * FROM Persons WHERE City LIKE 'N%';
(2) 從"Persons" 表中選取居住在以 "g" 結(jié)尾的城市里的人:
SELECT * FROM Persons WHERE City LIKE '%g';
(3) 從 "Persons" 表中選取居住在包含 "lon" 的城市里的人:SELECT * FROM Persons WHERE City LIKE '%lon% ;
(4) 從 "Persons" 表中選取居住在不包含 "lon" 的城市里的人:SELECT * FROM Persons WHERE City NOT LIKE '%lon%';
(5) 從"Persons" 表中選取名字的第一個字符之后是 "eorge" 的人:SELECT * FROM Persons WHERE FirstName LIKE '_eorge';
(6) 從"Persons" 表中選取的這條記錄的姓氏以 "C" 開頭,,然后是一個任意字符,,然后是 "r",然后是任意字符,,然后是 "er":
SELECT * FROM Persons WHERE LastName LIKE 'C_r_er';
(7) 從"Persons" 表中選取居住的城市以 "A" 或 "L" 或 "N" 開頭的人:SELECT * FROM Persons WHERE City LIKE '[ALN]%';
(8) 從"Persons" 表中選取居住的城市不以 "A" 或 "L" 或 "N" 開頭的人:SELECT * FROM Persons WHERE City LIKE '[!ALN]%';
8,、IN 操作符 從表中選取姓氏為 Adams 和 Carter 的人:SELECT * FROM Persons WHERE LastName IN ('Adams','Carter');
9、BETWEEN 操作符 以字母順序顯示介于 "Adams"(包括)和 "Carter"(不包括)之間的人:SELECT * FROM Persons WHERE LastName BETWEEN 'Adams' AND 'Carter';
注意:不同的數(shù)據(jù)庫對 BETWEEN...AND 操作符的處理方式是有差異的,。某些數(shù)據(jù)庫會列出介于 "Adams" 和 "Carter" 之間的人,,但不包括 "Adams" 和 "Carter" ;某些數(shù)據(jù)庫會列出介于 "Adams" 和 "Carter" 之間并包括 "Adams" 和 "Carter" 的人,;而另一些數(shù)據(jù)庫會列出介于 "Adams" 和 "Carter" 之間的人,,包括 "Adams" ,,但不包括 "Carter" 。 所以,,請檢查你的數(shù)據(jù)庫是如何處理 BETWEEN....AND 操作符的,! 10、 為列名稱和表名稱指定別名(Alias)(1)表的 SQL Alias 語法
SELECT po.OrderID, p.LastName, p.FirstName FROM Persons AS p, Product_Orders AS po WHERE p.LastName='Adams' AND p.FirstName='John';
(2)列的 SQL Alias 語法 SELECT LastName AS Family, FirstName AS Name FROM Persons;
10,、Join(Inner Join),、Left Join(Left Outer Join)、Right Join(Right Outer Join),、Full Join(Full Outer Join) "Persons" 表: Id_P | LastName | FirstName | Address | City |
---|
1 | Adams | John | Oxford Street | London | 2 | Bush | George | Fifth Avenue | New York | 3 | Carter | Thomas | Changan Street | Beijing |
"Orders" 表: Id_O | OrderNo | Id_P |
---|
1 | 77895 | 3 | 2 | 44678 | 3 | 3 | 22456 | 1 | 4 | 24562 | 1 | 5 | 34764 | 65 |
(1)Join 用where 聯(lián)表查詢: SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons, Orders WHERE Persons.Id_P = Orders.Id_P ;
用Join(Inner Join)查詢:INNER JOIN 關鍵字在表中存在至少一個匹配時返回行,。如果 "Persons" 中的行在 "Orders" 中沒有匹配,就不會列出這些行,。 SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons INNER JOIN Orders ON Persons.Id_P = Orders.Id_P ORDER BY Persons.LastName;
LastName | FirstName | OrderNo |
---|
Adams | John | 22456 | Adams | John | 24562 | Carter | Thomas | 77895 | Carter | Thomas | 44678 |
(2)左外連接Left Join(Left Outer Join):LEFT JOIN 關鍵字會從左表 (Persons) 那里返回所有的行,,即使在右表 (Orders) 中沒有匹配的行。 SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons LEFT JOIN Orders ON Persons.Id_P=Orders.Id_P ORDER BY Persons.LastName;
LastName | FirstName | OrderNo |
---|
Adams | John | 22456 | Adams | John | 24562 | Carter | Thomas | 77895 | Carter | Thomas | 44678 | Bush | George | |
(3)右外連接Right Join(Right Outer Join):RIGHT JOIN 關鍵字會從右表 (Orders) 那里返回所有的行,,即使在左表 (Persons) 中沒有匹配的行,。 SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons RIGHT JOIN Orders ON Persons.Id_P=Orders.Id_P ORDER BY Persons.LastName;
LastName | FirstName | OrderNo |
---|
Adams | John | 22456 | Adams | John | 24562 | Carter | Thomas | 77895 | Carter | Thomas | 44678 | | | 34764 |
(4)全連接Full Join(Full Outer Join):FULL JOIN 關鍵字會從左表 (Persons) 和右表 (Orders) 那里返回所有的行。如果 "Persons" 中的行在表 "Orders" 中沒有匹配,,或者如果 "Orders" 中的行在表 "Persons" 中沒有匹配,,這些行同樣會列出。 SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons FULL JOIN Orders ON Persons.Id_P=Orders.Id_P ORDER BY Persons.LastName;
LastName | FirstName | OrderNo |
---|
Adams | John | 22456 | Adams | John | 24562 | Carter | Thomas | 77895 | Carter | Thomas | 44678 | Bush | George | | | | 34764 |
11,、Union:UNION 操作符用于合并兩個或多個 SELECT 語句的結(jié)果集,。 注意:UNION 內(nèi)部的 SELECT 語句必須擁有相同數(shù)量的列,。列也必須擁有相似的數(shù)據(jù)類型,。同時,每條 SELECT 語句中的列的順序必須相同,。UNION 結(jié)果集中的列名總是等于 UNION 中第一個 SELECT 語句中的列名,。 Employees_China:E_ID | E_Name |
---|
01 | Zhang, Hua | 02 | Wang, Wei | 03 | Carter, Thomas | 04 | Yang, Ming |
Employees_USA:E_ID | E_Name |
---|
01 | Adams, John | 02 | Bush, George | 03 | Carter, Thomas | 04 | Gates, Bill |
(1)UNION命令列出所有在中國和美國的不同的雇員名: SELECT E_Name FROM Employees_China UNION SELECT E_Name FROM Employees_USA;
E_Name |
---|
Zhang, Hua | Wang, Wei | Carter, Thomas | Yang, Ming | Adams, John | Bush, George | Gates, Bill |
(2)UNION ALL 命令列出在中國和美國的所有的雇員: SELECT E_Name FROM Employees_China UNION ALL SELECT E_Name FROM Employees_USA;
E_Name |
---|
Zhang, Hua | Wang, Wei | Carter, Thomas | Yang, Ming | Adams, John | Bush, George | Carter, Thomas | Gates, Bill |
12、SQL 的 NULL 值處理 (1)選取在 "Address" 列中帶有 NULL 值的記錄: SELECT LastName,FirstName,Address FROM Persons WHERE Address IS NULL; (2)選取在 "Address" 列中不帶有 NULL 值的記錄:
SELECT LastName,FirstName,Address FROM Persons WHERE Address IS NOT NULL;
13,、SQL ISNULL(),、NVL()、IFNULL() 和 COALESCE() 函數(shù) P_Id | ProductName | UnitPrice | UnitsInStock | UnitsOnOrder |
---|
1 | computer | 699 | 25 | 15 | 2 | printer | 365 | 36 | | 3 | telephone | 280 | 159 | 57 |
在統(tǒng)計時,,上表中 UnitsOnOrder字段值如果為null不利于計算,,所以要用函數(shù)將null值當做0計算。SELECT ProductName,UnitPrice*(UnitsInStock+ISNULL(UnitsOnOrder,0)) FROM Products;
Oracle:SELECT ProductName,UnitPrice*(UnitsInStock+NVL(UnitsOnOrder,0)) FROM Products;
MySQL:SELECT ProductName,UnitPrice*(UnitsInStock+IFNULL(UnitsOnOrder,0)) FROM Products;
或 SELECT ProductName,UnitPrice*(UnitsInStock+COALESCE(UnitsOnOrder,0)) FROM Products;
|