数据库通常包含一个或多个表。每个表都用一个名称标识(例如,"Customers"或"Orders")。该表包含带有数据(行)的记录。
在本教程中,我们将使用著名的Northwind示例数据库(包括MSAccess和MSSQLServer)。
下面是选自 "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 |
上面的表包含五条记录(每一条对应一个客户)和七个列(CustomerID、CustomerName、ContactName、Address、City、PostalCode 和 Country)。
您需要在数据库上执行的大部分操作都是使用SQL语句完成的。
以下SQL语句选择“Customers”表中的所有记录:
在本教程中,我们将向您解释各种不同的SQL语句。
句法:
SELECT column_name(s) FROM table_name
句法:
SELECT [*] FROM [TableName] WHERE [condition1]
句法:
SELECT [*] FROM [TableName] WHERE [condition1] [AND [OR]] [condition2]...
句法:
SELECT column_name()
FROM table_name
ORDER BY column_name() ASC or DESC
句法:
SELECT DISTINCT column1, column2....columnN
FROM table_name;
句法:
SELECT column1, column2....columnN
FROM table_name
WHERE column_name IN (val-1, val-2,...val-N);
句法:
SELECT column1, column2....columnN
FROM table_name
WHERE column_name LIKE { PATTERN };
句法:
SELECT COUNT(column_name)
FROM table_name
WHERE CONDITION;
句法:
SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name
HAVING (arithematic function condition);
句法:
INSERT INTO table_name (column, column1, column2, column3, ...)
VALUES (value, value1, value2, value3 ...)
句法:
UPDATE table_name
SET column=value, column1=value1,...
WHERE someColumn=someValue
句法:
DELETE FROM tableName
WHERE someColumn = someValue
句法:
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
句法:
DROP TABLE table_name;
句法:
CREATE UNIQUE INDEX index_name
ON table_name ( column1, column2,...columnN);
句法:
ALTER TABLE table_name
DROP INDEX index_name;
句法:
DESC table_name;
句法:
TRUNCATE TABLE table_name;
句法:
ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data_ype};
句法:
ALTER TABLE table_name RENAME TO new_table_name;
句法:
USE database_name;
句法:
COMMIT;
句法:
ROLLBACK;
现在,通过以下题目测测看您对“SQL 语法”的掌握程度吧!
SQL语法规则:点击此处进行测试>>
更多测试题请参考:《SQL测验》题库>>