SQL Statements
Explore the fundamental SQL statements used to interact with and manage relational databases. This section covers Data Manipulation Language (DML) and Data Definition Language (DDL) commands.
Data Manipulation Language (DML)
DML statements are used to manage data within schema objects. They include commands for retrieving, inserting, updating, and deleting records.
SELECT
Retrieves data from one or more tables.
SELECT column1, column2
FROM table_name
WHERE condition;
INSERT
Inserts new records into a table.
INSERT INTO table_name (column1, column2)
VALUES ('value1', value2);
UPDATE
Modifies existing records in a table.
UPDATE table_name
SET column1 = 'new_value'
WHERE condition;
DELETE
Removes records from a table.
DELETE FROM table_name
WHERE condition;
Data Definition Language (DDL)
DDL statements are used to define and modify the structure of database objects such as tables, indexes, and views.
CREATE
Creates new database objects.
CREATE TABLE table_name (
column1 VARCHAR(255),
column2 INT
);
ALTER
Modifies existing database objects.
ALTER TABLE table_name
ADD column3 DATE;
DROP
Deletes database objects.
DROP TABLE table_name;
TRUNCATE
Removes all records from a table quickly.
TRUNCATE TABLE table_name;
Data Control Language (DCL)
DCL statements are used to grant and revoke access privileges to database users.
GRANT
Grants permissions to users.
GRANT SELECT, INSERT ON table_name TO user_name;
REVOKE
Revokes permissions from users.
REVOKE UPDATE ON table_name FROM user_name;
Transaction Control Language (TCL)
TCL statements manage transactions within the database, ensuring data integrity.
COMMIT
Saves all transactions to the database.
COMMIT;
ROLLBACK
Undoes transactions that have not been saved.
ROLLBACK;
SAVEPOINT
Sets a savepoint within a transaction.
SAVEPOINT savepoint_name;