This repository is dedicated to providing comprehensive examples and explanations of SQL commands, organized by their types: DDL (Data Definition Language), DQL (Data Query Language), DML (Data Manipulation Language), DCL (Data Control Language), and TCL (Transaction Control Language). This resource is designed to help both beginners and experienced developers improve their understanding and skills in SQL.
Before you begin, ensure you have met the following requirements:
- You have a SQL database management system installed (e.g., MySQL, PostgreSQL, SQLite).
- You have a basic understanding of SQL and database concepts.
- You have a text editor or an IDE (e.g., VS Code, PyCharm).
-
Open your SQL client (e.g., MySQL Workbench, pgAdmin) and connect to your database.
-
Load the SQL scripts from the repository to create and populate your database.
-
Follow the tutorials and examples provided in the repository to practice and improve your SQL skills.
DDL commands are used to define the structure of the database schema. Examples include:
-
CREATE: Used to create a new table, view, or other database objects.
-- Example: Creating a new table CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(100), department VARCHAR(50) );
-
ALTER: Used to modify an existing database object.
-- Example: Adding a new column to a table ALTER TABLE employees ADD salary DECIMAL(10, 2);
-
DROP: Used to delete an existing database object.
-- Example: Dropping a table DROP TABLE employees;
-
TRUNCATE: Used to remove all records from a table, but the structure remains.
-- Example: Truncating a table TRUNCATE TABLE employees;
DQL commands are used to query the database and retrieve data. The primary DQL command is:
-
SELECT: Used to retrieve data from the database.
-- Example: Selecting data from a table SELECT * FROM employees WHERE department = 'IT';
DML commands are used to manipulate the data within the database. Examples include:
-
INSERT: Used to add new records to a table.
-- Example: Inserting new records into a table INSERT INTO employees (id, name, department) VALUES (1, 'John Doe', 'IT'), (2, 'Jane Smith', 'HR');
-
UPDATE: Used to modify existing records in a table.
-- Example: Updating records in a table UPDATE employees SET department = 'Finance' WHERE id = 2;
-
DELETE: Used to remove records from a table.
-- Example: Deleting records from a table DELETE FROM employees WHERE id = 1;
DCL commands are used to control access to data within the database. Examples include:
-
GRANT: Used to give privileges to users.
-- Example: Granting privileges to a user GRANT SELECT, INSERT ON employees TO user1;
-
REVOKE: Used to remove privileges from users.
-- Example: Revoking privileges from a user REVOKE INSERT ON employees FROM user1;
TCL commands are used to manage transactions in the database. Examples include:
-
COMMIT: Used to save all changes made during the current transaction.
-- Example: Committing a transaction COMMIT;
-
ROLLBACK: Used to undo changes made during the current transaction.
-- Example: Rolling back a transaction ROLLBACK;
-
SAVEPOINT: Used to set a point within a transaction to which you can later roll back.
-- Example: Creating a savepoint SAVEPOINT sp1;
Contributions are always welcome! If you have any suggestions, bug reports, or improvements, please:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes.
- Commit your changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature-branch
). - Open a pull request.
Please ensure your contributions adhere to the coding standards and best practices.
- SQLBolt for SQL tutorials and exercises.
- W3Schools for comprehensive SQL documentation.
- Stack Overflow for community support and solutions.