DB Short Note All in One
DB Short Note All in One
In database systems, the ACID properties are a set of rules that ensure database transactions are
processed reliably and securely. ACID is an acronym that stands for:
A - Atomicity C - Consistency I - Isolation D - Durability
These properties guarantee that database transactions are executed in a way that maintains data integrity,
even in the presence of errors, crashes, or concurrent access.
Let's dive into each property:
A - Atomicity
Atomicity ensures that database transactions are treated as a single, indivisible unit of work. This means
that either all changes are committed, or none are. If any part of the transaction fails, the entire transaction
is rolled back, and the database is returned to its previous state.
Example: When transferring money from one account to another, the transaction should either succeed
entirely or fail entirely. If the transfer fails, the accounts should be restored to their original state.
C - Consistency
Consistency ensures that the database remains in a valid state, even after multiple transactions have been
executed. This means that the database must always satisfy its integrity constraints, such as primary key
uniqueness and referential integrity.
Example: In a banking system, the total amount of money in all accounts should always be consistent
with the total amount of money in the system.
I - Isolation
Isolation ensures that concurrent transactions do not interfere with each other's execution. This means that
each transaction should operate as if it were the only transaction accessing the database.
Example: When multiple users are updating their accounts simultaneously, each transaction should be
isolated from the others, ensuring that the updates are applied correctly and without interference.
D - Durability
Durability ensures that once a transaction has been committed, its effects are permanent and cannot be
rolled back. This means that the database guarantees that the changes made by a transaction will survive
even in the event of a system failure or crash.
Example: Once a payment has been processed and committed, the database should ensure that the
payment is recorded permanently, even if the system crashes or is shut down.
By enforcing these ACID properties, database systems can ensure that transactions are executed reliably,
securely, and with data integrity. This is crucial in many applications, such as financial systems, e-
commerce platforms, and social media networks, where data consistency and reliability are paramount.
Join mechanisms in DB
Inner join: This type of join returns only the rows that have matching values in both tables based on the
join condition.
Outer join: This includes several variations:
Left outer join: Returns all rows from the left table, and the matched rows from the right table. If
there is no match, NULL values are returned from the right side.
Right outer join: Returns all rows from the right table, and the matched rows from the left table.
If there is no match, NULL values are returned from the left side.
Full outer join: Returns all rows when there is a match in either the left or right table.
Cross join: This join produces a Cartesian product of the two tables involved, meaning each row from the
first table is combined with each row from the second table.
DISTINCT keyword?
DISTINCT removes duplicates based on all the columns in the SELECT clause
DISTINCT keyword can be used in SELECT but not used in WHERE clauses
Usage of DISTINCT should be avoided as far as possible due to performance issues
//SELECT DISTINCT column1, column2, column3 FROM table name;
Here's a list of some important SQL keywords and their functions in the context of databases:
1. SELECT: Retrieves data from a database.
o Example: SELECT column1, column2 FROM table_name;
2. DISTINCT: Returns unique values in the result set by eliminating duplicates.
o Example: SELECT DISTINCT column1 FROM table_name;
3. FROM: Specifies the table(s) from which to retrieve data.
o Example: SELECT column1 FROM table_name;
4. WHERE: Filters rows based on a specified condition.
o Example: SELECT column1 FROM table_name WHERE condition;
5. GROUP BY: Groups rows that have the same values into summary rows.
o Example: SELECT column1, COUNT(*) FROM table_name GROUP BY column1;
6. HAVING: Filters groups based on a specified condition when using GROUP BY.
o Example: SELECT column1, COUNT(*) FROM table_name GROUP BY column1
HAVING COUNT(*) > 1;
7. ORDER BY: Sorts the result set in ascending or descending order.
o Example: SELECT column1 FROM table_name ORDER BY column1 ASC;
8. JOIN: Combines rows from two or more tables based on a related column.
o Types: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN.
o Example: SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;
9. UNION: Combines the result sets of two or more SELECT statements.
o Example: SELECT column1 FROM table1 UNION SELECT column1 FROM table2;
10. INSERT INTO: Inserts new rows into a table.
o Example: INSERT INTO table_name (column1, column2) VALUES (value1, value2);
11. UPDATE: Modifies existing data in a table.
o Example: UPDATE table_name SET column1 = value1 WHERE condition;
12. DELETE: Deletes rows from a table.
o Example: DELETE FROM table_name WHERE condition;
13. CREATE TABLE: Creates a new table in the database.
o Example: CREATE TABLE table_name (column1 datatype, column2 datatype);
14. ALTER TABLE: Modifies an existing table (e.g., adding columns, modifying constraints).
o Example: ALTER TABLE table_name ADD column3 datatype;
15. DROP TABLE: Deletes a table and its data from the database.
o Example: DROP TABLE table_name;
16. CREATE INDEX: Creates an index on a table column(s) to improve query performance.
o Example: CREATE INDEX idx_column1 ON table_name (column1);
17. DROP INDEX: Deletes an index from a table.
o Example: DROP INDEX idx_column1 ON table_name;
18. TRUNCATE TABLE: Deletes all rows from a table without logging individual row deletions.
o Example: TRUNCATE TABLE table_name;
19. ROLLBACK: Rolls back a transaction to its starting point.
o Example: ROLLBACK;
20. COMMIT: Saves changes made during the current transaction.
o Example: COMMIT;
These keywords and their associated functionalities form the backbone of SQL queries and database
operations, enabling developers and database administrators to manage and manipulate data efficiently
within a relational database management system (RDBMS).
Aggregate functions in SQL are functions that operate on multiple rows of a table to compute a single
result. These functions summarize data from a set of rows according to a specific criterion. Here are the
common aggregate functions used in SQL:
1. COUNT():
o Counts the number of rows that match a specified condition.
o Example: SELECT COUNT(*) FROM employees;
2. SUM():
o Calculates the sum of values in a numeric column.
o Example: SELECT SUM(salary) FROM employees;
3. AVG():
o Calculates the average of values in a numeric column.
o Example: SELECT AVG(age) FROM employees;
4. MIN():
o Finds the minimum value in a column.
o Example: SELECT MIN(salary) FROM employees;
5. MAX():
o Finds the maximum value in a column.
o Example: SELECT MAX(salary) FROM employees;
Usage Notes:
Aggregate functions are typically used with the SELECT statement to compute summary
information.
They can be used with GROUP BY to calculate summary values for groups of rows.
NULL values in columns are generally ignored by aggregate functions, except for COUNT(*),
which counts all rows regardless of NULL values
Example
//-- Count the number of employees
SELECT COUNT(*) FROM employees;