SQL – 语法

SQL – 语法


SQL 后面是一组独特的规则和指南,称为语法。本教程通过列出所有基本 SQL 语法,让您快速开始使用 SQL。

所有 SQL 语句都以任何关键字开头,例如 SELECT、INSERT、UPDATE、DELETE、ALTER、DROP、CREATE、USE、SHOW,并且所有语句都以分号 (;) 结尾。

这里需要注意的最重要的一点是 SQL 不区分大小写,这意味着 SELECT 和 select 在 SQL 语句中具有相同的含义。而 MySQL 在表名上有所不同。因此,如果您正在使用 MySQL,那么您需要给出数据库中存在的表名。

SQL 中的各种语法

本教程中给出的所有示例都已在 MySQL 服务器上进行了测试。

SQL SELECT 语句

SELECT column1, column2....columnN
FROM   table_name;

SQL DISTINCT 子句

SELECT DISTINCT column1, column2....columnN
FROM   table_name;

SQL WHERE 子句

SELECT column1, column2....columnN
FROM   table_name
WHERE  CONDITION;

SQL AND/OR 子句

SELECT column1, column2....columnN
FROM   table_name
WHERE  CONDITION-1 {AND|OR} CONDITION-2;

SQL IN 子句

SELECT column1, column2....columnN
FROM   table_name
WHERE  column_name IN (val-1, val-2,...val-N);

SQL BETWEEN 子句

SELECT column1, column2....columnN
FROM   table_name
WHERE  column_name BETWEEN val-1 AND val-2;

SQL LIKE 子句

SELECT column1, column2....columnN
FROM   table_name
WHERE  column_name LIKE { PATTERN };

SQL ORDER BY 子句

SELECT column1, column2....columnN
FROM   table_name
WHERE  CONDITION
ORDER BY column_name {ASC|DESC};

SQL GROUP BY 子句

SELECT SUM(column_name)
FROM   table_name
WHERE  CONDITION
GROUP BY column_name;

SQL 计数子句

SELECT COUNT(column_name)
FROM   table_name
WHERE  CONDITION;

SQL HAVING 子句

SELECT SUM(column_name)
FROM   table_name
WHERE  CONDITION
GROUP BY column_name
HAVING (arithematic function condition);

SQL CREATE TABLE 语句

CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);

SQL DROP TABLE 语句

DROP TABLE table_name;

SQL CREATE INDEX 语句

CREATE UNIQUE INDEX index_name
ON table_name ( column1, column2,...columnN);

SQL DROP INDEX 语句

ALTER TABLE table_name
DROP INDEX index_name;

SQL DESC 语句

DESC table_name;

SQL TRUNCATE TABLE 语句

TRUNCATE TABLE table_name;

SQL ALTER TABLE 语句

ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data_ype};

SQL ALTER TABLE 语句(重命名)

ALTER TABLE table_name RENAME TO new_table_name;

SQL INSERT INTO 语句

INSERT INTO table_name( column1, column2....columnN)
VALUES ( value1, value2....valueN);

SQL 更新语句

UPDATE table_name
SET column1 = value1, column2 = value2....columnN=valueN
[ WHERE  CONDITION ];

SQL DELETE 语句

DELETE FROM table_name
WHERE  {CONDITION};

SQL CREATE DATABASE 语句

CREATE DATABASE database_name;

SQL DROP DATABASE 语句

DROP DATABASE database_name;

SQL USE 语句

USE database_name;

SQL COMMIT 语句

COMMIT;

SQL ROLLBACK 语句

ROLLBACK;

觉得文章有用?

点个广告表达一下你的爱意吧 !😁