SQL 1 Lecture Notes 1
SQL 1 Lecture Notes 1
Concept of Keys
Sample Table:
Super Key
● An attribute or set of attributes which can uniquely identify a tuple
● It may be redundant or reducible which means that a proper subset
of it might also uniquely identify a record in the table
● Example
○ Assuming customer_id can uniquely identify a record, it can be
the super key. (customer_id, name) can also be the super key
though the attribute “name” is redundant.
○ Similarly, the phone number for every customer will be unique,
hence again, the phone can also be the super key.
Candidate Key
● The minimal set of fields (non-reducible) that can uniquely identify
each record in a table
● Every table must have at least a single candidate key (obviously can
have multiple candidate keys) which may have single or multiple
attributes
● Attributes which are part of candidate keys must not contain NULL
values
● In our example, customer_id and phone both are candidate keys for
table customer.
● Prime Attributes - An attribute that is present in any of the
candidate keys is a prime attribute
Primary Key
Ans - The most appropriate candidate key that should be the primary key is
customer_id
Example -
● Similarly, category_id can take only those values which are present
in id in the ProductCategory table since only those products actually
exist.
● Foreign key references the primary key of the other table.
● A foreign key may have a name other than that of a primary key. It
can take only those values which are present in the primary key of
the referenced relation.
● There is no restriction on a foreign key to be unique. It can have
NULL values as well.
Composite Key
● A candidate key consisting of multiple attributes and not just a single
attribute is called a composite key. It holds all the properties of
candidate keys.
● Example - In the customer relation (customer_id, phone) could be a
composite key
Unique Key
How?
● The Adhaar Card Number is unique for all the citizens (tuples) of
India (table).
● If it gets lost and another duplicate copy is issued, then the duplicate
copy always has the same number as before. Thus, it is
non-updateable.
● Few citizens may not have got their Aadhaar cards, so for them, its
value is NULL.
ACID Property
Case
● In our ecommerce example, let's say there are 2 buyers (A,B). They made
some transactions.
● Do you think the sum of account balances of the 3 accounts (A, B and you)
of your accounts will change before and after the payment?
● If there are other customers who are also paying to your account, how will
it affect your transaction?
What is a transaction?
→ A transaction is a single logical unit of work that accesses and possibly
modifies the contents of a database. Transactions access data using read and
write operations.
1. Atomicity
a. All changes to data are performed as if they are a single operation.
That is, all the changes are performed, or none of them is.
2. Consistency
a. Data is in a consistent state when a transaction starts and when it
ends.
b. For example, the consistency property ensures that the total value of
funds in both the accounts is the same at the start and end of each
transaction.
3. Isolation
a. The intermediate state of a transaction is invisible to other
transactions. As a result, transactions that run concurrently appear
to be serialized.
b. One of the goals of isolation is to allow multiple transactions to
occur at the same time without adversely affecting the execution of
each.
4. Durability
a. After a transaction is successfully completed, changes to a database
should persist and should not get lost, even in the event of a system
failure.
b. For example, the durability property ensures that the changes made
to each account will not be reversed.
a. Aggregate function
Aggregate Description
function
Function Description
NOW Returns the current date and time at which the statement is
executed.
STR_TO_DATE Converts a string into a date and time value based on a specified
format.
C. String Functions
Name Description
E.Math Functions
Name Description
FLOOR() Returns the largest integer value not greater than the
argument
F. Comparison Functions