SQL語法
SQL其次是獨特的一套所謂的語法規則和準則。本教學為您提供了一個快速入門使用SQL列出所有的基本SQL語法:
所有SQL語句開始以類似關鍵字 SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, CREATE, USE, SHOW 並且所有的語句用分號(;)結束。
重要的一點要注意的是,MySQL是不區分大小寫的,這意味著SELECT和select在SQL語句中具有相同的含義,但MySQL在表名有差異。如果使用MySQL的話,那麼需要存在於數據庫中給定的表名。
所有在本教學中所舉的例子已經過在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 COUNT 子句:
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 語句:
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;