SQL Command Overview
Command | Category | Description |
---|---|---|
SELECT | DML | Retrieves data from a table. |
INSERT | DML | Adds new records to a table. |
UPDATE | DML | Modifies existing data in a table. |
DELETE | DML | Removes data from a table. |
CREATE TABLE | DDL | Creates a new table. |
ALTER TABLE | DDL | Modifies the structure of an existing table. |
DROP TABLE | DDL | Deletes an existing table and its data. |
GRANT | DCL | Grants specific privileges to a user or role. |
REVOKE | DCL | Revokes specific privileges from a user or role. |
COMMIT | DML/DCL | Saves changes made through DML statements and ends a transaction (also related to DCL for transaction control). |
DML (Data Manipulation Language):
- SELECT – Retrieve data from a table.
- Syntax:
sql SELECT column1, column2 FROM table_name WHERE condition;
- Example:
sql SELECT product_name, price FROM products WHERE category = 'Electronics';
- INSERT – Insert data into a table.
- Syntax:
sql INSERT INTO table_name (column1, column2) VALUES (value1, value2);
- Example:
sql INSERT INTO customers (customer_name, email) VALUES ('John Doe', 'john.doe@example.com');
- UPDATE – Modify existing data in a table.
- Syntax:
sql UPDATE table_name SET column1 = new_value WHERE condition;
- Example:
sql UPDATE products SET price = 199.99 WHERE product_id = 101;
- DELETE – Remove data from a table.
- Syntax:
sql DELETE FROM table_name WHERE condition;
- Example:
sql DELETE FROM customers WHERE customer_id = 3;
DDL (Data Definition Language):
- CREATE TABLE – Create a new table.
- Syntax:
sql CREATE TABLE table_name ( column1 datatype, column2 datatype, ... );
- Example:
sql CREATE TABLE products ( product_id INT, product_name VARCHAR(255), price DECIMAL(10, 2) );
- ALTER TABLE – Modify the structure of an existing table.
- Syntax:
sql ALTER TABLE table_name ADD column_name datatype;
- Example:
sql ALTER TABLE products ADD quantity INT;
- DROP TABLE – Delete an existing table and its data.
- Syntax:
sql DROP TABLE table_name;
- Example:
sql DROP TABLE invoices;
DCL (Data Control Language):
- GRANT – Provide specific privileges to a user or role.
- Syntax:
sql GRANT privilege ON object TO user_or_role;
- Example:
sql GRANT SELECT, INSERT ON products TO sales_team;
- REVOKE – Revoke specific privileges from a user or role.
- Syntax:
sql REVOKE privilege ON object FROM user_or_role;
- Example:
sql REVOKE INSERT ON products FROM sales_team;
- COMMIT – Save changes to the database.
- Syntax:
sql COMMIT;
- Example:
sql -- After executing a series of INSERT, UPDATE, or DELETE statements COMMIT;
This cheat sheet covers the basic SQL commands for data manipulation, definition, and control. Use these examples as a starting point for your SQL operations in a database environment.