目录
SQL DELETE 语句
DELETE 语句
DELETE 语句用于删除表中的记录。
【DELETE 语法】
DELETE FROM table_name WHERE condition;
注: 请注意 SQL DELETE 语句中的 WHERE 子句!WHERE 子句规定哪条记录或者哪些记录需要删除。如果您省略了 WHERE 子句,所有的记录都将被删除!
演示数据库
以下是从示例数据库的 "客户(Customers)" 表中查询的内容:
| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
|---|---|---|---|---|---|---|
| 1 |
Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
| 4 |
Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
| 5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
DELETE 实例
以下 SQL 语句从 客户(customer) 表中删除客户 "Alfreds Futterkiste":
【实例】
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
客户(Customers) 表现在如下所示:
| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
|---|---|---|---|---|---|---|
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
| 4 |
Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
| 5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
删除所有行
可以在不删除表的情况下删除所有的行。这意味着表的结构、属性和索引都是完整的:
DELETE FROM table_name;
以下 SQL 语句删除 "Customers" 表中的所有行,而不删除该表:
【实例】
DELETE FROM Customers;
SQL TOP, LIMIT, ROWNUM 子句
TOP 子句
TOP 子句用于规定要返回的记录的数目。
TOP 子句对于包含数千条记录的大型表很有用。返回大量记录可能会影响性能。
注: 并非所有数据库系统都支持 SELECT TOP子句。MySQL 使用 LIMIT,而 Oracle 使用 ROWNUM。
【SQL Server / MS Access 的语法:】
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
【MySQL 语法:】
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
【Oracle 语法:】
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
演示数据库
以下是从示例数据库的 "客户(Customers)" 表中查询的内容:
| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
|---|---|---|---|---|---|---|
| 1 |
Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
| 4 |
Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
| 5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
SQL TOP、LIMIT 和 ROWNUM 示例
以下 SQL 语句从 "Customers" 表中选择前三条记录(用于 SQL Server/MS Access):
【实例】
SELECT TOP 3 * FROM Customers;
下面的 SQL 语句显示使用 LIMIT 子句的等效示例(用于 MySQL):
【实例】
SELECT * FROM Customers
LIMIT 3;
下面的 SQL 语句显示使用 ROWNUM 子句的等效示例(用于 Oracle):
【实例】
SELECT * FROM Customers
WHERE ROWNUM <= 3;
SQL TOP PERCENT 实例
在 Microsoft SQL Server 中还可以使用百分比作为参数。
以下 SQL 语句从 "Customers" 表中选择前 50% 的记录(用于SQL Server/MS Access):
【实例】
SELECT TOP 50 PERCENT * FROM Customers;
添加WHERE子句
以下 SQL 语句从 "Customers" 表中选择前三条记录,其中 country 为 "Germany"(用于SQL Server/MS Access):
【实例】
SELECT TOP 3 * FROM Customers
WHERE Country='Germany';
下面的 SQL 语句显示了使用 LIMIT 子句的等效示例(用于 MySQL):
【实例】
SELECT * FROM Customers
WHERE Country='Germany'
LIMIT 3;
下面的 SQL 语句显示了使用 ROWNUM 子句的等效示例(用于 Oracle):
【实例】
SELECT * FROM Customers
WHERE Country='Germany' AND ROWNUM <= 3;
SQL MIN() 和 MAX() 函数
MIN() 和 MAX() 函数
MIN 函数返回一列中的最小值。NULL 值不包括在计算中。
MAX 函数返回一列中的最大值。NULL 值不包括在计算中。
【MIN() 语法】
SELECT MIN(column_name)
FROM table_name
WHERE condition;
【MAX() 语法】
SELECT MAX(column_name)
FROM table_name
WHERE condition;
演示数据库
以下是从示例数据库的 "Products" 表中选择的内容:
| ProductID | ProductName | SupplierID | CategoryID | Unit | Price |
|---|---|---|---|---|---|
| 1 | Chais | 1 | 1 | 10 boxes x 20 bags | 18 |
| 2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19 |
| 3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10 |
| 4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22 |
| 5 | Chef Anton's Gumbo Mix | 2 | 2 | 36 boxes | 21.35 |
MIN() 实例
以下 SQL 语句查找最便宜产品的价格:
【实例】
SELECT MIN(Price) AS SmallestPrice
FROM Products;
MAX() 实例
以下 SQL 语句查找最昂贵产品的价格:
【实例】
SELECT MAX(Price) AS LargestPrice
FROM Products;
SQL COUNT(), AVG() 和 SUM() 函数
COUNT(), AVG() 和 SUM() 函数
COUNT() 函数返回匹配指定条件的行数。
AVG 函数返回数值列的平均值。NULL 值不包括在计算中。
SUM 函数返回数值列的总数(总额)。
【COUNT() 语法】
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
【AVG() 语法】
SELECT AVG(column_name)
FROM table_name
WHERE condition;
【SUM() 语法】
SELECT SUM(column_name)
FROM table_name
WHERE condition;
演示数据库
以下是从示例数据库的 "Products" 表中选择的内容:
| ProductID | ProductName | SupplierID | CategoryID | Unit | Price |
|---|---|---|---|---|---|
| 1 | Chais | 1 | 1 | 10 boxes x 20 bags | 18 |
| 2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19 |
| 3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10 |
| 4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22 |
| 5 | Chef Anton's Gumbo Mix | 2 | 2 | 36 boxes | 21.35 |
COUNT() 实例
以下 SQL 语句查找产品的数量:
【实例】
SELECT COUNT(ProductID)
FROM Products;
注: 不计算 NULL 空值。
AVG() 实例
以下 SQL 语句查找所有产品的平均价格:
【实例】
SELECT AVG(Price)
FROM Products;
注: 忽略 NULL 空值。
演示数据库
以下是从示例数据库的 "OrderDetails" 表中选择的内容:
| OrderDetailID | OrderID | ProductID | Quantity |
|---|---|---|---|
| 1 | 10248 | 11 | 12 |
| 2 | 10248 | 42 | 10 |
| 3 | 10248 | 72 | 5 |
| 4 | 10249 | 14 | 9 |
| 5 | 10249 | 51 | 40 |
SUM() 实例
以下 SQL 语句查找 "OrderDetails" 表中 "Quantity" 字段的总和:
【实例】
SELECT SUM(Quantity)
FROM OrderDetails;
注: 忽略 NULL 空值。
SQL LIKE 操作符
SQL LIKE 操作符
LIKE 操作符在 WHERE 子句中用于搜索列中的指定模式。
有两个通配符经常与 LIKE 操作符一起使用:
- % - 百分号表示零个、一个或多个字符
- _ - 下划线表示单个字符
注: MS Access使用星号 (*) 代替百分号 (%),使用问号 (?) 代替下划线 (_)。百分号和下划线也可以组合使用!
【LIKE 语法】
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
注: 还可以使用 AND 或 OR 运算符组合任意数量的条件。
以下是一些示例,显示了使用'%' 和 '_'通配符的不同 LIKE 运算符:
| LIKE Operator | 描述 |
|---|---|
| WHERE CustomerName LIKE 'a%' | Finds any values that start with "a" |
| WHERE CustomerName LIKE '%a' | Finds any values that end with "a" |
| WHERE CustomerName LIKE '%or%' | Finds any values that have "or" in any position |
| WHERE CustomerName LIKE '_r%' | Finds any values that have "r" in the second position |
| WHERE CustomerName LIKE 'a_%' | Finds any values that start with "a" and are at least 2 characters in length |
| WHERE CustomerName LIKE 'a__%' | Finds any values that start with "a" and are at least 3 characters in length |
| WHERE ContactName LIKE 'a%o' | Finds any values that start with "a" and ends with "o" |
演示数据库
下表显示了样本数据库中完整的客户(Customers) 表:
| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
|---|---|---|---|---|---|---|
| 1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
| 4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
| 5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
| 6 | Blauer See Delikatessen | Hanna Moos | Forsterstr. 57 | Mannheim | 68306 | Germany |
| 7 | Blondel père et fils | Frédérique Citeaux | 24, place Kléber | Strasbourg | 67000 | France |
| 8 | Bólido Comidas preparadas | Martín Sommer | C/ Araquil, 67 | Madrid | 28023 | Spain |
| 9 | Bon app' | Laurence Lebihans | 12, rue des Bouchers | Marseille | 13008 | France |
| 10 | Bottom-Dollar Marketse | Elizabeth Lincoln | 23 Tsawassen Blvd. | Tsawassen | T2F 8M4 | Canada |
| 11 | B's Beverages | Victoria Ashworth | Fauntleroy Circus | London | EC2 5NT | UK |
| 12 | Cactus Comidas para llevar | Patricio Simpson | Cerrito 333 | Buenos Aires | 1010 | Argentina |
| 13 | Centro comercial Moctezuma | Francisco Chang | Sierras de Granada 9993 | México D.F. | 05022 | Mexico |
| 14 | Chop-suey Chinese | Yang Wang | Hauptstr. 29 | Bern | 3012 | Switzerland |
| 15 | Comércio Mineiro | Pedro Afonso | Av. dos Lusíadas, 23 | São Paulo | 05432-043 | Brazil |
| 16 | Consolidated Holdings | Elizabeth Brown | Berkeley Gardens 12 Brewery | London | WX1 6LT | UK |
| 17 | Drachenblut Delikatessend | Sven Ottlieb | Walserweg 21 | Aachen | 52066 | Germany |
| 18 | Du monde entier | Janine Labrune | 67, rue des Cinquante Otages | Nantes | 44000 | France |
| 19 | Eastern Connection | Ann Devon | 35 King George | London | WX3 6FW | UK |
| 20 | Ernst Handel | Roland Mendel | Kirchgasse 6 | Graz | 8010 | Austria |
| 21 | Familia Arquibaldo | Aria Cruz | Rua Orós, 92 | São Paulo | 05442-030 | Brazil |
| 22 | FISSA Fabrica Inter. Salchichas S.A. | Diego Roel | C/ Moralzarzal, 86 | Madrid | 28034 | Spain |
| 23 | Folies gourmandes | Martine Rancé | 184, chaussée de Tournai | Lille | 59000 | France |
| 24 | Folk och fä HB | Maria Larsson | Åkergatan 24 | Bräcke | S-844 67 | Sweden |
| 25 | Frankenversand | Peter Franken | Berliner Platz 43 | München | 80805 | Germany |
| 26 | France restauration | Carine Schmitt | 54, rue Royale | Nantes | 44000 | France |
| 27 | Franchi S.p.A. | Paolo Accorti | Via Monte Bianco 34 | Torino | 10100 | Italy |
| 28 | Furia Bacalhau e Frutos do Mar | Lino Rodriguez | Jardim das rosas n. 32 | Lisboa | 1675 | Portugal |
| 29 | Galería del gastrónomo | Eduardo Saavedra |

该博客是SQL教程,介绍了DELETE、TOP、LIMIT、ROWNUM等语句,以及MIN()、MAX()、COUNT()等函数,还讲解了LIKE、通配符、IN、BETWEEN等操作符,同时涉及别名和联接的使用,包含各部分语法及实例。
&spm=1001.2101.3001.5002&articleId=135827283&d=1&t=3&u=cfff38b9345145b5a9a3ca7ea89ff30d)
773

被折叠的 条评论
为什么被折叠?



