Update docs/tech_docs/sql_notes.md

This commit is contained in:
2024-03-11 05:29:53 +00:00
parent 9116c2275f
commit 1a08f3307b

View File

@@ -1,3 +1,75 @@
To create a reference guide that provides context and a complete picture of SQL terms, particularly focusing on Data Manipulation Language (DML), Data Definition Language (DDL), and Data Control Language (DCL), it's important to understand what each of these terms means and how they are used in the context of managing and interacting with databases. This guide aims to flesh out these concepts with definitions and examples, providing a quick yet comprehensive refresher.
# SQL Reference Guide: DML, DDL, and DCL
## Data Manipulation Language (DML)
DML is a subset of SQL used for adding (inserting), deleting, and modifying (updating) data in a database. DML commands do not alter the structure of the table itself, but rather, work with the data within tables.
### SELECT
- **Purpose**: Retrieves data from one or more tables in a database.
- **Use Case**: Fetching user information from a `users` table.
- **Example**: `SELECT username, email FROM users WHERE isActive = 1;`
### INSERT
- **Purpose**: Adds new rows (records) to a table.
- **Use Case**: Adding a new user to the `users` table.
- **Example**: `INSERT INTO users (username, email, isActive) VALUES ('john_doe', 'john@example.com', 1);`
### UPDATE
- **Purpose**: Modifies existing data within a table.
- **Use Case**: Updating a user's email address in the `users` table.
- **Example**: `UPDATE users SET email = 'new_email@example.com' WHERE username = 'john_doe';`
### DELETE
- **Purpose**: Removes rows from a table.
- **Use Case**: Removing a user from the `users` table.
- **Example**: `DELETE FROM users WHERE username = 'john_doe';`
## Data Definition Language (DDL)
DDL encompasses SQL commands used to define or modify the structure of the database schema. It deals with descriptions of the database schema and is used to create and modify the structure of database objects in the database.
### CREATE
- **Purpose**: Creates new tables, views, or other database objects.
- **Use Case**: Creating a new table called `users`.
- **Example**: `CREATE TABLE users (id INT PRIMARY KEY, username TEXT, email TEXT, isActive INT);`
### ALTER
- **Purpose**: Modifies the structure of an existing database object, like adding or deleting columns in a table.
- **Use Case**: Adding a new column `birthdate` to the `users` table.
- **Example**: `ALTER TABLE users ADD birthdate DATE;`
### DROP
- **Purpose**: Deletes tables, views, or other database objects.
- **Use Case**: Removing the `users` table from the database.
- **Example**: `DROP TABLE users;`
### TRUNCATE
- **Purpose**: Removes all records from a table, including all spaces allocated for the records but does not delete the table itself.
- **Use Case**: Deleting all records from the `users` table while keeping the table structure.
- **Example**: `TRUNCATE TABLE users;`
## Data Control Language (DCL)
DCL includes commands that control access to data in the database. It's used to manage permissions through roles and rights within the database environment.
### GRANT
- **Purpose**: Gives user's access privileges to the database.
- **Use Case**: Granting a user read-only access to the `users` table.
- **Example**: `GRANT SELECT ON users TO 'read_only_user';`
### REVOKE
- **Purpose**: Removes access privileges from a user.
- **Use Case**: Revoking all access from a user to the `users` table.
- **Example**: `REVOKE ALL PRIVILEGES ON users FROM 'former_employee';`
## Conclusion
Understanding DML, DDL, and DCL is crucial for anyone working with SQL databases, as they cover the spectrum of operations from manipulating data to defining the structure of database objects, and controlling access to data. This guide provides a clear overview of these key SQL language components, offering a solid foundation for refreshing knowledge or learning about SQL command categories.
---
Creating a more complete SQL reference guide involves encompassing a broad range of SQL syntax, functions, best practices, and advanced concepts. This guide is designed to serve as a comprehensive overview for users at various levels of expertise, offering both a refresher for experienced users and a solid foundation for newcomers.
# Comprehensive SQL Reference Guide