MySQL syntax format emphasizes consistency, readability, and maintainability. Here are detailed guidelines and examples for formatting various MySQL statements:
1. SELECT Statement
sqlSELECT column1, column2, ... FROM table_name WHERE condition GROUP BY column1 HAVING condition ORDER BY column1 [ASC|DESC] LIMIT number;
Example:
sqlSELECT first_name, last_name, birth_date FROM employees WHERE hire_date > '2020-01-01' GROUP BY department_id HAVING COUNT(*) > 5 ORDER BY last_name ASC LIMIT 10;
2. INSERT Statement
sqlINSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
Example:
sqlINSERT INTO employees (first_name, last_name, birth_date, hire_date) VALUES ('Jane', 'Smith', '1990-02-15', '2023-06-01');
3. UPDATE Statement
sqlUPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Example:
sqlUPDATE employees SET hire_date = '2023-07-01' WHERE employee_id = 1;
4. DELETE Statement
sqlDELETE FROM table_name WHERE condition;
Example:
sqlDELETE FROM employees WHERE employee_id = 1;
5. CREATE TABLE Statement
sqlCREATE TABLE table_name ( column1 datatype [constraints], column2 datatype [constraints], ... );
Example:
sqlCREATE TABLE employees ( employee_id INT AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, birth_date DATE, hire_date DATETIME DEFAULT CURRENT_TIMESTAMP );
6. ALTER TABLE Statement
sqlALTER TABLE table_name ADD column_name datatype [constraints];
Example:
sqlALTER TABLE employees ADD email VARCHAR(100) NOT NULL;
7. DROP TABLE Statement
sqlDROP TABLE table_name;
Example:
sqlDROP TABLE employees;
8. CREATE INDEX Statement
sqlCREATE INDEX index_name ON table_name (column1, column2, ...);
Example:
sqlCREATE INDEX idx_last_name ON employees (last_name);
9. DROP INDEX Statement
sqlDROP INDEX index_name ON table_name;
Example:
sqlDROP INDEX idx_last_name ON employees;
10. CREATE VIEW Statement
sqlCREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
Example:
sqlCREATE VIEW active_employees AS SELECT first_name, last_name FROM employees WHERE status = 'active';
11. DROP VIEW Statement
sqlDROP VIEW view_name;
Example:
sqlDROP VIEW active_employees;
12. Transaction Statements
- START TRANSACTION: Begins a new transaction.
- COMMIT: Saves the changes made in the transaction.
- ROLLBACK: Reverts the changes made in the transaction.
Example:
sqlSTART TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE account_id = 1; UPDATE accounts SET balance = balance + 100 WHERE account_id = 2; COMMIT;
13. GRANT Statement
sqlGRANT privilege_name ON object_name TO user [WITH GRANT OPTION];
Example:
sqlGRANT SELECT, INSERT ON employees TO 'user'@'localhost';
14. REVOKE Statement
sqlREVOKE privilege_name ON object_name FROM user;
Example:
sqlREVOKE SELECT, INSERT ON employees FROM 'user'@'localhost';
Formatting Guidelines
- Consistent Capitalization: Use uppercase for SQL keywords (
SELECT
,FROM
,WHERE
) and lowercase for column and table names (table_name
,column_name
). - Indentation: Use indentation to align SQL statements and enhance readability, especially in complex queries.
- Line Breaks: Place each clause (
SELECT
,FROM
,WHERE
,JOIN
,ORDER BY
) on a new line. - Comments: Use comments to explain complex logic or important details within the code. Use
--
for single-line comments and/* ... */
for multi-line comments.
By following these guidelines and using the provided formats, you can write MySQL statements that are clear, efficient, and easy to maintain.