本博客全面指导最重要的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中文网其他相关文章!