1.SELECT TOP 50 PERCENT
* FROM Persons ~~~ "Persons" 表中选取 50% 的记录;
SELECT TOP 2
* FROM Persons~~~"Persons" 表中选取头两条记录。
2.通配符:
3.SELECT * FROM Persons WHERE LastName IN ('Adams','Carter');~~~在 WHERE 子句中规定多个值;
4.SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons, Orders
WHERE Persons.Id_P = Orders.Id_P;
======等价:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNoFROM Persons
INNER JOIN Orders
ON Persons.Id_P = Orders.Id_P
ORDER BY Persons.LastName;
JOIN 类型:
· JOIN: 如果表中有至少一个匹配,则返回行
· LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行
· RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行
· FULL JOIN: 只要其中一个表中存在匹配,就返回行
网址:http://www.w3school.com.cn/sql/sql_join_inner.asp;
5.UNION &&UNION All ,例如:
SELECT E_Name FROM Employees_China
UNION
SELECT E_Name FROM Employees_USA;
UNION ALL列出所有内容,包括重复;
6.数据库备份:
SELECT
*
INTO
Persons IN
'Backup.mdb'
FROM Persons
将表persons备份到数据库Backup.mdb中的表Persons;
7.创建数据库:CREATE DATABASE database_name
8.数据库表格:
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
Id_P int,
PRIMARY KEY (O_Id),
FOREIGN KEY (Id_P) REFERENCES Persons(Id_P)
)
9.check约束:
CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CHECK (Id_P>0)
)
多个check约束:
CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT chk_Person CHECK (Id_P>0 AND City='Sandnes')
)
10.自增:AUTO_INCREMENT
CREATE TABLE Persons
(
P_Id int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
11.GROUP BY:SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer;
按客户的不同分组,查出所属客户的名称和订单总额。
12.在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000