Common SQL commands¶
SQL commands used by MySQL can be categorized into different types based on their purposes: Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL).
Data Manipulation Language (DML)¶
DML commands manage data within database tables.
Common DML commands include:
-
SELECT: This command retrieves data from one or more tables in the database.
mysql> SELECT * FROM customers;
-
INSERT: This command adds new records to a table.
mysql> INSERT INTO customers (name, email) VALUES ('John Doe', 'john@example.com');
-
UPDATE: This command modifies existing records in a table.
mysql> UPDATE customers SET email = 'newemail@example.com' WHERE id = 1;
-
DELETE: This command removes records from a table.
mysql> DELETE FROM customers WHERE id = 1;
Data Definition Language (DDL)¶
DDL commands define, modify, and remove database objects such as tables, indexes, and views.
Common DDL commands include:
-
CREATE: This command creates new database objects like tables, indexes, and views.
mysql> CREATE TABLE employees (id INT, name VARCHAR(50));
-
ALTER: This command modifies the structure of existing database objects.
mysql> ALTER TABLE employees ADD COLUMN email VARCHAR(100);
-
DROP: This command removes database objects from the database.
mysql> DROP TABLE employees;
Data Control Language (DCL)¶
DCL commands control access to database objects and define privileges.
Common DCL commands include:
-
GRANT: This command grants specific privileges to database users.
mysql> GRANT SELECT, INSERT ON employees TO 'user1'@'localhost';
-
REVOKE: This command revokes privileges from database users.
mysql> REVOKE INSERT ON employees FROM 'user2'@'localhost';
Transaction Control Language (TCL)¶
TCL commands manage transactions within a database.
Common TCL commands include:
-
COMMIT: This command saves changes made during the current transaction to the database.
mysql> COMMIT;
-
ROLLBACK: This command undoes changes made during the current transaction and restores the database to its previous state.
mysql> ROLLBACK;
Fundamental SQL links:
Get expert help¶
If you need assistance, visit the community forum for comprehensive and free database knowledge, or contact our Percona Database Experts for professional support and services.