SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
Let's delve into these SQL command categories again, focusing on Data Definition Language (DDL), Data Manipulation Language (DML), Transaction Control Language (TCL), and Data Control Language (DCL).
DDL commands define and manage the structure of database objects.
Common DDL commands include:
CREATE: Used to create database objects such as tables, views, or indexes.
CREATE TABLE table_name (column1 datatype, column2 datatype, ...);
ALTER: Used to modify the structure of an existing database object, such as adding a column to a table.
ALTER TABLE table_name ADD column_name datatype;
DROP: Deletes database objects.
DROP TABLE table_name;
TRUNCATE: Empties a table, removing all its rows.
TRUNCATE TABLE table_name;
DML commands manipulate and handle the data within the database.
Common DML commands include:
SELECT: Retrieves data from a table.
SELECT column1, column2 FROM table_name WHERE condition;
INSERT: Inserts data into a table.
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
UPDATE: Modifies existing data in a table.
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
DELETE: Removes data from a table.
DELETE FROM table_name WHERE condition;
DCL commands manage the permissions and rights in the database.
Common DCL commands are:
GRANT: Grants permissions to users.
GRANT permission ON object TO user;
REVOKE: Removes permissions from users.
REVOKE permission ON object FROM user;
TCL commands control the transactions in the database to ensure data integrity.
Common TCL commands are:
COMMIT: Commits and saves all the changes made during the current transaction.
COMMIT;
ROLLBACK: Undoes changes made during the current transaction or since a particular savepoint.
ROLLBACK TO savepoint_name;
SAVEPOINT: Sets a point within a transaction to which you can later roll back.
SAVEPOINT savepoint_name;
SET TRANSACTION: Configures the properties of a transaction.
SET TRANSACTION [ READ WRITE | READ ONLY ];
These command categories serve as the backbone of SQL operations. However, the specific syntax or support for certain commands might differ across various RDBMSs, so it's essential to consult the official documentation for the specific SQL system you are using.
CREATE:
CREATE TABLE employees ( emp_id INT PRIMARY KEY, emp_name VARCHAR(50), emp_salary DECIMAL(10, 2) );
ALTER:
ALTER TABLE employees ADD COLUMN emp_department VARCHAR(50);
DROP:
DROP TABLE employees;
INSERT:
INSERT INTO employees (emp_name, emp_salary) VALUES ('John Doe', 60000);
UPDATE:
UPDATE employees SET emp_salary = 65000 WHERE emp_name = 'John Doe';
DELETE:
COMMIT:
COMMIT;
ROLLBACK:
ROLLBACK;
SAVEPOINT:
SAVEPOINT my_savepoint;
ROLLBACK TO SAVEPOINT:
ROLLBACK TO SAVEPOINT my_savepoint;