Home >Database >Mysql Tutorial >SQL Quick Reference: Simplifying Database Management
This blog comprehensively guides the most important SQL commands and operations. It covers basic queries, joins, subqueries, indexes, and more advanced concepts.
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], ... );
Example:
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, ...);
Example:
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;
Example:
UPDATE employees SET age = 31 WHERE id = 1;
DELETE FROM table_name WHERE condition;
Example:
DELETE FROM employees WHERE id = 1;
SELECT column1, column2, ... FROM table_name WHERE condition ORDER BY column LIMIT n;
Example:
SELECT * FROM employees; SELECT name, age FROM employees WHERE age > 30;
Example:
SELECT * FROM employees WHERE name LIKE 'J%';
GRANT permission ON object TO user;
Example:
GRANT SELECT, INSERT ON employees TO 'user1';
REVOKE permission ON object FROM user;
Example:
REVOKE SELECT ON employees FROM 'user1';
Returns rows when there is a match in both tables.
SELECT column1, column2 FROM table_name WHERE condition ORDER BY column LIMIT n;
Returns all rows from the left table, and matched rows from the right table. If no match, NULL values will appear for columns from the right table.
/* This is a multi-line comment */
Returns all rows from the right table, and matched rows from the left table. If no match, NULL values will appear for columns from the left table.
CREATE TABLE table_name ( column1 datatype [constraints], column2 datatype [constraints], ... );
Returns rows when there is a match in one of the tables.
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;
Ensures that all values in a column (or group of columns) are unique.
CREATE INDEX index_name ON table_name (column_name);
Counts the number of rows that match a specific condition.
DROP INDEX index_name;
Returns the sum of values in a column.
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
Returns the average of values in a column.
INSERT INTO employees (id, name, age, hire_date) VALUES (1, 'John Doe', 30, '2022-01-01');
Returns the minimum and maximum values in a column.
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Groups rows that have the same values into summary rows.
UPDATE employees SET age = 31 WHERE id = 1;
Filters groups after applying GROUP BY.
DELETE FROM table_name WHERE condition;
Sorts the result set in ascending or descending order.
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%';
Conditional logic inside a query.
SELECT column1, column2 FROM table_name WHERE condition ORDER BY column LIMIT n;
/* This is a multi-line comment */
This SQL cheatsheet covers all the essential SQL commands and techniques you’ll need for working with relational databases. Whether you are querying, inserting, updating, or joining data, this guide will help you work more effectively with SQL.
CREATE TABLE table_name ( column1 datatype [constraints], column2 datatype [constraints], ... );
The above is the detailed content of SQL Quick Reference: Simplifying Database Management. For more information, please follow other related articles on the PHP Chinese website!