sqlnotes
sqlnotes
• A primary key is a unique key, meaning it can uniquely identify each record (tuple) in a
table.
• It must have unique values and cannot contain any duplicate values.
• A primary key cannot be NULL, as it needs to provide a valid, unique identifier for every
record.
• A primary key does not have to consist of a single column. In some cases, a composite
primary key (made of multiple columns) can be used to uniquely identify records in a
table.
• Databases typically store rows ordered in memory according to primary key for fast
access of records using primary key.
Foreign Key
A foreign key is an attribute in one table that refers to the primary key in another table. The
table that contains the foreign key is called the referencing table, and the table that is
referenced is called the referenced table.
• A foreign key in one table points to the primary key in another table, establishing a
relationship between them.
• It helps connect two or more tables, enabling you to create relationships between
them. This is essential for maintaining data integrity and preventing data redundancy.
• For example, DNO is a primary key in the DEPT table and a non-key in EMP
SQL Join
• An SQL JOIN clause is used to query and access data from multiple
tables by establishing logical relationships between them. It can
access data from multiple tables simultaneously using common key
values shared across different tables.
• INNER JOIN: The INNER JOIN keyword selects all rows from both the
tables as long as the condition is satisfied.
Stntax: SELECT table1.column1,table1.column2,table2.column1,….
FROM table1 INNER JOIN table2 ON table1.matching_column =
table2.matching_column;
Example: SELECT StudentCourse.COURSE_ID, Student.NAME,
Student.AGE FROM Student
INNER JOIN StudentCourse
ON Student.ROLL_NO = StudentCourse.ROLL_NO;
• LEFT JOIN (LEFT OUTER JOIN): Returns all rows from the left table,
and the matched rows from the right table. If there’s no match, NULL
values are returned for columns from the right table.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,….
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Example:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
• RIGHT JOIN (RIGHT OUTER JOIN): Returns all rows from the right
table, and the matched rows from the left table. If there’s no match,
NULL values are returned for columns from the left table.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Example: SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
• FULL JOIN (FULL OUTER JOIN): Returns all rows when there is a
match in one of the tables. If there’s no match, NULL values are
returned for columns from the table without a match.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
Exmaple:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
FULL JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
GROUP BY
The GROUP BY statement groups rows that have the same values into summary rows, like "find
the number of customers in each country".
Syntx:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Example
HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
ACID Properties
Atomicity ensures that a transaction is atomic, it means that either the entire transaction
completes fully or doesn’t execute at all. There is no in-between state i.e. transactions do not
occur partially. If a transaction has multiple operations, and one of them fails, the whole
transaction is rolled back, leaving the database unchanged. This avoids partial updates that can
lead to inconsistency.
Consistency ensures that a database remains in a valid state before and after a transaction. It
guarantees that any transaction will take the database from one consistent state to another,
maintaining the rules and constraints defined for the data. In simple terms, a transaction should
only take the database from one valid state to another. If a transaction violates any database
rules or constraints, it should be rejected, ensuring that only consistent data exists after the
transaction.
This property ensures that multiple transactions can occur concurrently without leading to
the inconsistency of the database state. Transactions occur independently without interference.
Changes occurring in a particular transaction will not be visible to any other transaction until
that particular change in that transaction is written to memory or has been committed.
Durability: Persisting Changes
This property ensures that once the transaction has completed execution, the updates and
modifications to the database are stored in and written to disk and they persist even if a system
failure occurs. These updates now become permanent and are stored in non-volatile memory.
In the event of a failure, the DBMS can recover the database to the state it was in after the last
committed transaction, ensuring that no data is lost.