Structured Query Language (SQL)
Structured Query Language (SQL) is a powerful domain-specific language used for managing and manipulating relational databases. SQL consists of various types of statements that allow users to interact with a database by performing operations such as querying, updating, inserting, and deleting data. In this discussion, we’ll explore the main types of SQL statements: Data Query Language (DQL), Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language (DCL).
Data Query Language (DQL)
DQL statements are primarily focused on retrieving data from the database. The most common DQL statement is the SELECT
statement, which is used to retrieve data from one or more tables. It allows users to specify the columns they want, the conditions for selecting rows, and the order in which the results should be presented. The basic syntax of a SELECT
statement is as follows:
SELECT column1, column2, ... FROM table WHERE condition;
SELECT employee_id, first_name, last_name FROM employees WHERE department_id = 10;
Data Definition Language (DDL)
DDL statements are used for defining and managing the structure of the database, such as creating, altering, or deleting tables and indexes. Key DDL statements include:
CREATE
: Used to create objects in the database, such as tables, indexes, and views.
CREATE TABLE employees ( employee_id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), hire_date DATE );
ALTER
: Modifies the structure of an existing database object.
ALTER TABLE employees ADD COLUMN email VARCHAR(100);
DROP
: Deletes a database object, such as a table or index.
DROP TABLE employees;
Data Manipulation Language (DML):
DML statements are used for managing data stored in the database. The primary DML statement is INSERT
, UPDATE
, and DELETE
.
INSERT
: Adds new rows of data into a table.
INSERT INTO employees (employee_id, first_name, last_name, hire_date) VALUES (1, 'John', 'Doe', '2023-01-01');
UPDATE
: Modifies existing data in a table.
UPDATE employees SET last_name = 'Smith' WHERE employee_id = 1;
DELETE
: Removes rows from a table based on specified conditions.
DELETE FROM employees WHERE employee_id = 1;
Data Control Language (DCL):
DCL statements are concerned with the control and management of access to the data within the database. The two main DCL statements are GRANT
and REVOKE
.
GRANT
: Provides specific privileges to users or roles.
GRANT SELECT, INSERT ON employees TO user1;
REVOKE
: Removes previously granted privileges.
REVOKE INSERT ON employees FROM user1;
Transaction Control Language (TCL):
TCL statements are used to manage transactions within a database. Transactions are sequences of one or more SQL statements that are executed as a single unit of work.
COMMIT
: Saves all changes made during the current transaction.
COMMIT;
ROLLBACK
: Undoes the changes made during the current transaction.
ROLLBACK;
SAVEPOINT
: Sets a point within a transaction to which you can later roll back.
SAVEPOINT point_name;
In conclusion, SQL is a versatile language that provides various types of statements catering to different aspects of database management. DQL statements focus on data retrieval, DDL statements deal with the structure of the database, DML statements manage the data within the database, and DCL statements control access to the data. Understanding and effectively using these statements is essential for anyone working with relational databases.