扫码一下
查看教程更方便
SQL 是由一系列特定的规则和标识符组成的,这些被称为语法。本教程通过列出所有基本 SQL 语法,让你可以快速开始使用 SQL。
所有 SQL 语句都以任何特定的关键字开头,例如 SELECT、INSERT、UPDATE、DELETE、ALTER、DROP、CREATE、USE、SHOW,并且所有语句都以分号 (;) 结尾。
需要注意的最重要的一点是 SQL 不区分大小写,这意味着 SELECT 和 select 在 SQL 语句中具有相同的含义。而 MySQL 在表名上有所不同。因此,如果你是在使用 MySQL,那么需要给出数据库中存在的表名。
本教程中给出的所有示例都已在 MySQL 服务器上进行了测试。
SELECT column1, column2....columnN
FROM table_name;
SELECT DISTINCT column1, column2....columnN
FROM table_name;
SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION;
SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION-1 {AND|OR} CONDITION-2;
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 BETWEEN val-1 AND val-2;
SELECT column1, column2....columnN
FROM table_name
WHERE column_name LIKE { PATTERN };
SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION
ORDER BY column_name {ASC|DESC};
SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name;
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);
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
DROP TABLE table_name;
SQL CREATE INDEX 语句
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;
INSERT INTO table_name( column1, column2....columnN)
VALUES ( value1, value2....valueN);
UPDATE table_name
SET column1 = value1, column2 = value2....columnN=valueN
[ WHERE CONDITION ];
DELETE FROM table_name
WHERE {CONDITION};
CREATE DATABASE database_name;
DROP DATABASE database_name;
USE database_name;
COMMIT;
ROLLBACK;