SQL(structured query language)
commands
1. Data Definition Language (DDL)
Defines and manages the database structure.
CREATE: Creates new objects like databases or tables.
Example:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
department VARCHAR(50)
);
ALTER: Modifies existing database structures.
Example:
ALTER TABLE employees ADD salary DECIMAL(10, 2);
DROP: Deletes a table or other objects.
Example:
DROP TABLE employees;
TRUNCATE: Removes all rows from a table, resetting it.
Example:
TRUNCATE TABLE employees;
2. Data Manipulation Language (DML)
Handles the data in the database.
SELECT: Retrieves data from tables.
Example:
SELECT name, age FROM employees WHERE department = 'HR';
INSERT: Adds new rows to a table.
Example:
INSERT INTO employees (id, name, age, department, salary)
VALUES (1, 'John Doe', 30, 'HR', 50000);
UPDATE: Modifies existing data in a table.
Example:
UPDATE employees
SET salary = 55000
WHERE id = 1;
DELETE: Removes specific rows from a table.
Example:
DELETE FROM employees WHERE age > 60;
3. Data Query Language (DQL)
Primarily involves data retrieval (subset of DML).
SELECT: Queries data from one or more tables.
Example:
SELECT * FROM employees;
4. Data Control Language (DCL)
Manages access and permissions.
GRANT: Grants permissions to users.
Example:
GRANT SELECT, INSERT ON employees TO user123;
REVOKE: Removes granted permissions.
Example:
REVOKE INSERT ON employees FROM user123;
5. Transaction Control Language (TCL)
Manages the state of transactions.
COMMIT: Saves changes made during the transaction.
Example:
INSERT INTO employees (id, name) VALUES (2, 'Jane Smith');
COMMIT;
ROLLBACK: Undoes changes made during the transaction.
Example:
DELETE FROM employees WHERE id = 2;
ROLLBACK;
SAVEPOINT: Sets a savepoint within a transaction.
Example:
SAVEPOINT before_update;
UPDATE employees SET age = 40 WHERE id = 1;
ROLLBACK TO before_update;
SET TRANSACTION: Sets properties for a transaction.
Example:
SET TRANSACTION READ ONLY;