SQL Commands and Constraints Overview
SQL Commands and Constraints Overview
INNER JOIN returns only the rows that have matching values in both tables. LEFT OUTER JOIN returns all rows from the left table and the matched rows from the right table, or NULL if there is no match. RIGHT OUTER JOIN returns all rows from the right table and the matched rows from the left table, or NULL if there's no match. FULL OUTER JOIN returns all rows when there is a match in either left or right table records, filling in NULLs for missing matches from each side .
DELETE is used to remove rows from a table based on a condition and can be rolled back. TRUNCATE removes all rows in a table without logging individual row deletions and cannot be rolled back, essentially resetting it but retaining the structure. DROP TABLE deletes the entire table structure and contents, and cannot be rolled back even with transaction control .
Set operations like UNION and UNION ALL are used to combine the results of two or more SELECT queries. UNION removes duplicate rows from the combined result set, whereas UNION ALL includes all rows, including duplicates, making it faster. UNION is preferable when a unique list is needed, while UNION ALL is used for performance boosting when duplicates are acceptable and need to be retained, such as combining logs from different sources where log duplicates exist naturally .
INSERT INTO statement variants in SQL include inserting single records or multiple records at once, offering flexibility. The standard form (INSERT INTO … VALUES) is typically used for adding single entries, beneficial in applications where user-driven data input happens frequently. For bulk record additions, batch INSERTS reduce the transaction overhead compared to individual entries. However, batch inserts can complicate error handling, requiring detailed logging or transactions to ensure data integrity in case of failure .
NOT NULL ensures that a column cannot have a NULL value, which prevents missing data. UNIQUE ensures that all values in a column are different, preventing duplicate entries. PRIMARY KEY uniquely identifies each record in a table, enforcing entity integrity by combining the properties of NOT NULL and UNIQUE for the designated field .
A SQL VIEW simplifies database complexity by providing a virtual table representing the result of a database query. It encapsulates SQL logic and presents it as a single table to the end-user, enhancing security by restricting access to underlying tables, and simplifying query recoding. Potential drawbacks include performance overhead and the limitation of some updates on non-updatable views. A view can be updated if it has been defined with the WITH CHECK OPTION clause ensuring any update reflects valid data, like CREATE OR REPLACE VIEW customers_view AS SELECT name, age FROM customers WHERE age >= 25 .
Nested SQL queries, or subqueries, allow a query to be used as a condition in the main query. They can solve complex data retrieval tasks by enabling calculations to occur at multiple levels, such as calculating aggregates or filtering results based on dynamically computed values. Benefits include breaking down complex tasks into manageable parts and improving code readability and maintenance .
SQL logical operators, such as AND, OR, and NOT, are used in conditional statements to filter records based on multiple criteria. AND operator combines two or more conditions and returns records that meet all conditions. NOT negates a condition, returning records that do not meet the specified condition. For example, SELECT * FROM EMPLOYEE WHERE emp_id = 2 AND Address = 'twenth street' retrieves employees only where both conditions are true; SELECT * FROM EMPLOYEE WHERE NOT emp_id = 1 excludes records where emp_id is 1 .
Aggregation functions such as COUNT and MIN are used to perform calculations on multiple rows of a table’s column and return a single value. COUNT returns the number of rows that match the query criteria, while MIN returns the smallest value in a selected column. GROUP BY groups rows that have the same values in specified columns into summary rows, and HAVING is used to filter groups based on aggregate properties, providing precise control over how data is summarized and reported .
SQL triggers are special procedures that automatically execute on events like INSERT, UPDATE, or DELETE. They are used to maintain integrity and consistency, enforce security measures, and provide automated auditing capabilities. In a salary management system, a trigger could automatically log the old and new salary of an employee whenever a salary update action occurs, ensuring transparency and maintaining a record of salary changes .