这里主要是整理 SQL 一些基础的“增删查改”的语法,方便自己不记得的时候可以快速查找。
SELECT Statement
/* general syntax */
SELECT column1, column2, ... FROM table1;
/* retrieve all columns */
SELECT * FROM table1;
/* filter result or get specific data */
SELECT * FROM table1 WHERE condition;
/* retrieve the number of rows */
SELECT COUNT(*) FROM table1;
/* retrieve the number of some values */
SELECT COUNT(column1) FROM table1 WHERE condition;
/* retrieve some values without repeated items */
SELECT DISTINCT column1 FROM table1;
/* retrieve the number of some values without repeated items */
SELECT COUNT(DISTINCT column1) FROM table1 WHERE condition;
/* retrieve the first n rows from the table */
SELECT * FROM table1 LIMIT n;
/* retrieve the first n rows from the table starting from row m */
SELECT * FROM table1 LIMIT n OFFSET m;
INSERT, UPDATE, DELETE Statement
/* general syntax */
INSERT INTO table1 (column1, column2, ...) VALUES (value1, value2, ... );
UPDATE table1 SET column1 = value1, column2 = value2, ... WHERE condition;
DELETE FROM table1 WHERE condition;
CREATE Statement
/* general syntax */
CREATE TABLE table1 (column1 datatype, column2 datatype, column3 datatype, ...);
ALTER Statement
/* add column */
ALTER TABLE table1 ADD COLUMN column1 datatype constraint;
/* delete column */
ALTER TABLE table1 DROP COLUMN column1;
/* change column data type */
ALTER TABLE table1 ALTER COLUMN column1 SET DATA TYPE datatype
/* rename column */
ALTER TABLE table1 RENAME COLUMN name1 TO name2;
TRUNCATE, DROP Statement
/* clear table */
TRUNCATE TABLE table1;
/* delete table */
DROP TABLE table1;