本部落格全面指導最重要的SQL指令和操作。它涵蓋了基本查詢、聯結、子查詢、索引和更高級的概念。
SELECT column1, column2 FROM table_name WHERE condition ORDER BY column LIMIT n;
/* This is a multi-line comment */
CREATE TABLE table_name ( column1 datatype [constraints], column2 datatype [constraints], ... );
範例:
CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(100), age INT, hire_date DATE );
ALTER TABLE table_name ADD column_name datatype;
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
ALTER TABLE old_table_name RENAME TO new_table_name;
DROP TABLE table_name;
CREATE INDEX index_name ON table_name (column_name);
DROP INDEX index_name;
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
範例:
INSERT INTO employees (id, name, age, hire_date) VALUES (1, 'John Doe', 30, '2022-01-01');
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
範例:
UPDATE employees SET age = 31 WHERE id = 1;
DELETE FROM table_name WHERE condition;
範例:
DELETE FROM employees WHERE id = 1;
SELECT column1, column2, ... FROM table_name WHERE condition ORDER BY column LIMIT n;
範例:
SELECT * FROM employees; SELECT name, age FROM employees WHERE age > 30;
範例:
SELECT * FROM employees WHERE name LIKE 'J%';
GRANT permission ON object TO user;
範例:
GRANT SELECT, INSERT ON employees TO 'user1';
REVOKE permission ON object FROM user;
範例:
REVOKE SELECT ON employees FROM 'user1';
當兩個表中存在匹配項時傳回行。
SELECT column1, column2 FROM table_name WHERE condition ORDER BY column LIMIT n;
傳回左表中的所有行以及右表中的符合行。如果不匹配,則右表中的欄位將顯示 NULL 值。
/* This is a multi-line comment */
傳回右表中的所有行以及左表中的符合行。如果不匹配,左表中的列將顯示 NULL 值。
CREATE TABLE table_name ( column1 datatype [constraints], column2 datatype [constraints], ... );
當其中一個表中有匹配項時傳回行。
CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(100), age INT, hire_date DATE );
ALTER TABLE table_name ADD column_name datatype;
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
ALTER TABLE old_table_name RENAME TO new_table_name;
DROP TABLE table_name;
確保一列(或一組列)中的所有值都是唯一的。
CREATE INDEX index_name ON table_name (column_name);
計算符合特定條件的行數。
DROP INDEX index_name;
傳回列中值的總和。
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
傳回列中位數的平均值。
INSERT INTO employees (id, name, age, hire_date) VALUES (1, 'John Doe', 30, '2022-01-01');
傳回列中的最小值和最大值。
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
將具有相同值的行分組為總計行。
UPDATE employees SET age = 31 WHERE id = 1;
套用 GROUP BY 後過濾群組。
DELETE FROM table_name WHERE condition;
依升序或降序對結果集進行排序。
DELETE FROM employees WHERE id = 1;
SELECT column1, column2, ... FROM table_name WHERE condition ORDER BY column LIMIT n;
SELECT * FROM employees; SELECT name, age FROM employees WHERE age > 30;
SELECT * FROM employees WHERE name LIKE 'J%';
查詢中的條件邏輯。
SELECT column1, column2 FROM table_name WHERE condition ORDER BY column LIMIT n;
/* This is a multi-line comment */
此 SQL 備忘單涵蓋了使用關聯式資料庫所需的所有基本 SQL 指令和技術。無論您是查詢、插入、更新還是連接數據,本指南都將幫助您更有效地使用 SQL。
CREATE TABLE table_name ( column1 datatype [constraints], column2 datatype [constraints], ... );
以上是SQL 快速參考:簡化資料庫管理的詳細內容。更多資訊請關注PHP中文網其他相關文章!