# Essential Commands Mastering foundational SQL is prerequisite for building effective data-driven Java applications. This document outlines the core SQL Data Manipulation Language (DML) statements—`SELECT`, `INSERT`, `UPDATE`, `DELETE`. Precision in these commands is paramount for application performance, scalability, and data integrity. These statements are the direct instructions executed by the database engine. ## SELECT Employ the `SELECT` statement to retrieve data subsets from database tables. **Purpose:** Data retrieval based on specified criteria. ```sql SELECT column1, column2 FROM table_name; ``` * `SELECT`: Initiates data retrieval operations. * `column1, column2`: Designates the specific columns required. List multiple columns separated by commas. * `FROM table_name`: Identifies the source table for the operation. Retrieve all columns using the `*` wildcard: ```sql SELECT * FROM users; ``` **Example:** Fetch the `username` and `email` for all entries within the `users` table. ```sql SELECT username, email FROM users; ``` Execution against an empty table yields no result rows. For a populated table with columns `id`, `username`, `about`, `birthday `, `active`, `email`, a `SELECT *` might produce: | id | username | about | birthday | active | email | |----|----------------|-------|----------|--------|----------------------| | 1 | software shinobi | NULL | NULL | 1 | software shinobi@email.com | *(Note: Database systems commonly represent boolean `true` as `1`)*. Selecting only `username` and `email`: ```sql SELECT username, email FROM users; ``` | username | email | |----------------|----------------------| | software shinobi | software shinobi@email.com | ## INSERT Utilize the `INSERT` statement to append new records (rows) to a database table. **Purpose:** Introduction of new data rows. ```sql INSERT INTO table_name (column1, column2) VALUES (value1, value 2); ``` * `INSERT INTO`: The command directive for adding data. * `table_name (column1, column2)`: Specifies the target table and the explicit columns receiving data. * `VALUES (value1, value2)`: Provides the data values corresponding to the specified column list. Value order must align with column order. **Example:** Add a single user entry to the `users` table. ```sql INSERT INTO users (username, email, active) VALUES (' software shinobi', 'software shinobi@email.com', true); ``` Omitting the column list requires supplying values for *all* columns in the table, strictly adhering to the table's column definition order. ## UPDATE Employ the `UPDATE` statement to modify existing data entries within a database table. **Purpose:** Modification of existing data records. ```sql UPDATE table_name SET column1 = new_value WHERE condition; ``` * `UPDATE table_ name`: Declares the table targeted for modifications. * `SET column1 = new_value`: Assigns `new_value` to `column1`. Multiple `column = value` assignments are permitted, separated by commas. * `WHERE condition`: The essential filter clause. Only rows satisfying this condition are processed. **Example:** Adjust the username for the user associated with `id = 1`. ```sql UPDATE users SET username = 'Software Shinobi' WHERE id = 1; ``` **Critical:** Excluding the `WHERE` clause will apply the changes to *every* row in the table. Exercise extreme caution and validate constraints. ## DELETE Use the `DELETE` statement to eliminate records (rows) from a database table. **Purpose:** Removal of data records. ```sql DELETE FROM table_name WHERE condition; ``` * `DELETE FROM table_name`: Identifies the table from which rows will be removed. * `WHERE condition`: Specifies the criteria determining which rows are deleted. **Example:** Remove the user record where `id` is equal to 1. ```sql DELETE FROM users WHERE id = 1 ; ``` **Critical:** Omitting the `WHERE` clause triggers the deletion of *all* records in the table. Such data removal is typically irreversible without a prior backup. Proceed with absolute certainty. ## Comments Incorporate comments into SQL scripts to bolster readability and clarify operational intent. Comments are explicitly ignored by the database engine during execution. **Purpose:** Script comprehension and internal documentation. **Single-line comments:** Prefixed by `--`. All text subsequent to `--` on the same line is treated as a comment. ```sql SELECT * FROM users; -- Retrieve all user records ``` **Multi-line comments:** Enclosed between `/*` and `*/`. Ideal for extended explanations or temporary exclusion of SQL blocks. ```sql /* This complex query block fetches data for quarterly reports. Needs optimization review. */ -- SELECT revenue_data FROM finance_table WHERE quarter = 'Q2'; ``` Apply comments strategically to explain nuanced logic or articulate the strategic objective of specific queries. Maintain consistent commenting practices within your team to ensure maximum collaborative clarity.