You are on page 1of 42

CS-2005 Database Systems

Lecture 16
Relation Attributes/Fields/Columns

SSN
1
2
Tuples/rows/records
3
4
5

Columns are named but tuples are not


DBMS Keys
• Keys play an important role in the relational database.
• It is used to uniquely identify any record or row of data from the table.
• It is also used to establish and identify relationships between tables.
Key
{Dept, Course}
Key {CS, 101}
{SSN} {SE, 102}
{1}
{2} Not a Key
{Name, Marks}
{Ali, 78}
{Ali, 78}

Key
{Name, Marks, Dept, Course}
{Ali, 78,CS,101}
{Ali, 78,CS,103}
Why do we need DMBS keys?
• For identifying any row of data in a table uniquely
• We can force identity of data and ensure integrity of data is maintained.
• A key’s role is based on a concept known as determination.
• Concept of Determination is very important for Functional Dependencies
which we will see later in detail
Types of Keys
• Super Key
• Candidate Key
• Primary Key
• Alternate Key
• Foreign Key
• Surrogate Key

7
Super Key
• Super key is an attribute or set of attributes that uniquely
identify each record.

• It’s a theoretical concept.


SSN -> SK
{Dept, Course} -> SK

{Name, Marks, Dept} = ??


{Course, Marks, Dept} = ??

Maximum Super keys = 2n – 1.


Candidate key
• A candidate key is an attribute or set of
attributes that can uniquely identify a tuple.
• Except for the primary key, the remaining
attributes are considered a candidate key.
• The candidate keys are as strong as the
primary key.
• Candidate key is a super key whose proper
subset is not super key.
• It is also called minimum super key.
Candidate Key - Example

• Example Super Keys


• STU_NUM
• STU_NUM, STU_LNAME
• STU_NUM, STU_LNAME, STU_INIT

4/14/2023 11
Candidate Key
• Is “STU_NUM, STU_LNAME” a Candidate Key ?
• No, because STU_NUM by itself is a candidate key

• Recall from previous slide (Added Information):


1. Candidate Key is a Superkey (K) such that its no proper subset is a superkey within
the relation.
• But STU_NUM is its subset and is a SuperKey
2. No proper subset of Superkey has the uniqueness property (irreducibility).
• But STU_NUM is its subset and posses uniqueness property

4/14/2023 12
• SId is a candidate key
• Sid+SEmail is a candidate key
Benefit of Candidate Key
• Searching Student’s Record
• Your Parents may not know your Roll # but using your first-name and last-name (a
candidate key), fewer search records will be retrieved from the system
• But Roll # is one of the candidate key, it gives exact record if known
• Search account details in a Bank
• Account holder goes to a bank without taking his/ her account number
• Account information will be retrieved with the help candidate keys

4/14/2023 14
Candidate Key during search

DBA can create Index on searching attributes (candidate Keys) for faster retrieval
4/14/2023 15
Candidate Key during search …

16
Primary Key
• It is the first key used to identify one and only
one instance of an entity uniquely.
• For each entity, the primary key selection is
based on requirements and developers.
SSN
1
2
3
4
5

Here, SSN is primary key


Primary Key
• A candidate key selected as the primary means of identifying rows in a
relation:

There is one and only one primary key per relation

The primary key is NOT NULL and UNIQUE

The ideal primary key is short, numeric(alpha), fixed length and never changes

Key that has its ownership of an Enterprise

The primary key may be a composite key


Primary Key
• Is Primary Key a Super Key ?
• Yes !!!
• a primary key is a superkey as well as a candidate key.
Primary Keys
• PK is a property of an organization.
• Driving license# is a property of Ministry of Motors/ transportation,
• It must not be used PK in university or other organizations. It can be a
candidate key for other organization for search purposes.
Primary Key - Samples

512235665 Social Security Number


610112654 (By Birth Asian, Europian, HomeLand)

200338900 Student ID (With Registration year)

DMM051 Product ID (with Manufacturing Loc, branches)


JUB004

312668852369 Bank Account# (followed by Branch Code)

100211 SKU# (By Category) or a barcode


300411

Serial# Serial# starts with 1 and incremented by 1

005384 Company has many branches with code 005, 007 etc.,
007004 invoices issued from branches are recognized with branch codes

22
IBAN - Fixed Length another example

The International Bank Account Number (IBAN) is an international standard (ISO 13616-1: 2007)
for identifying bank accounts across national borders. An IBAN is not a new account number, but
simply a new format for an existing bank account number that will be recognized internationally.

4/14/2023 23
Cont..
• Recall from the Definition of Primary Key
• The primary key is NOT NULL and UNIQUE

• What is Difference between Primary Key and Unique Key ?


Primary Key vs Unique Key
Primary Key vs Unique Key - Example

 PK: Employee ID
 Unique Key: GovernmentNumber
• To enforce additional unique conditions on the columns

 Why not just use GovernmentNumber as the primary key?


• GovernmentNumber is generated by another organization outside your
database

 Observation
• the primary key is a synthetic value such as an identity value (sequence),
therefore, an naturally occurring unique values, such as Account Numbers
become candidates for unique keys.
Foreign key

• Foreign keys are the column of the table used to point to the primary key
of another table.
• Every employee works in a specific department in a company, and
employee and department are two different entities.
• So we can't store the department's information in the employee table.
• That's why we link these two tables through the primary key of one table.
Foreign Key
• An attribute whose values match the primary key values in the
related table.

• Showing Keys in Relational Schema


VENDOR (VEND_CODE, VEND_CONTACT, VEND_AREACODE, VEND_PHONE)
PRODUCT (PROD_CODE, PROD_DESCRIPT, PROD_PRICE, PROD_ON_HAND, VEND_CODE)

4/14/2023 Data type, Length/ Size of FK and referring PK must be same 28


Foreign Key (FK)
• An attribute (or combination of attributes) in one table whose
values must either match the primary key in another table or
be null.

• When an FK is null ?
• Either the value in parent table is not defined yet
• Or the value in parent table is deleted
Foreign Key
• Can a foreign key is an attribute that refers to primary key of
same relation ?
• Yes, for recursive relationship
• Example of Recursive Relationship
Surrogate Key
• A surrogate key as an artificial column added to a relation to
serve as a primary key:
• DBMS supplied
• Short, numeric and never changes – an ideal primary key!
• Has artificial values that are meaningless to users
• Normally hidden in forms and reports
Surrogate Keys
• RENTAL_PROPERTY without surrogate key:
RENTAL_PROPERTY (Street, City, State/Province, Zip/PostalCode, Country, Rental_Rate)

• RENTAL_PROPERTY with surrogate key:


RENTAL_PROPERTY (PropertyID , Street, City, State/Province, Zip/PostalCode, Country, Rental_Rate)

4/14/2023 33
Integrity Constraints/Rules
• Entity Integrity
• Referential Integrity
• Null
• Domain Constraint
• Additional Constraint
Entity Integrity
• All primary key entries are unique
• No two rows with the same primary key value
• No part of a primary key maybe null
• No null values in a primary key

• Example
• No invoice can have a duplicate number, nor can it be null. In short, all
invoices are uniquely identified by their invoice number.
Referential Integrity
• Data is linked between two or
more tables
• by having the foreign key (in the associated table)
reference a primary key value (in the primary
table).
• Because of this, we need to ensure that data on
both sides of the relationship remain intact.
• Referential integrity refers to the
accuracy and consistency of data within
a relationship.
• whenever a foreign key value is used it must
reference a valid, existing primary key in the
parent table.
Benefits of Referential Integrity Constraint
• It will prevent users from
• Adding records to a related table if there is no associated record in the
primary table.
• Changing values in a primary table that result in orphaned records in a
related table.
• Deleting records from a primary table if there are matching related
records.
On Delete Clause in Foreign Key Constraint
• CASCADE
• if the parent record is deleted, any child records are also deleted
• NO ACTION
• rejects the delete or update operation for the parent table if there is a related foreign key value
in the referenced table.
• SET NULL
• Delete or update the row from the parent table, and set the foreign key column or columns in
the child table to NULL
• SET DEFAULT
• Allows the developer to specify a value to which to set the foreign key column(s) on an
UPDATE or a DELETE
Domain Constraint
• To allow a valid set of values for an attribute
• Example
• an hourly-wage domain allows only values greater than a specified value.
• The check clause in SQL permits domains to be restricted:
• Use check clause to ensure that an hourly-wage domain allows only values greater
than a specified value.
Attribute Constraint
• An attribute constraint is a declaration to the effect that a
specified attribute is of a specified type.

• Any attempt to introduce an attribute value into the database


that is not a type of the relevant type will simply rejected.

• Example
• Storing name in Attribute of Roll #
Table Constraint
• A table constraint is a constraint on an individual table.
• Example:
• PRIMARY KEY Specifies the column or columns that uniquely identify
a row in the table. NULL values are not allowed.
• UNIQUE Specifies that values in the columns must be unique.
• FOREIGN KEY Specifies that the values in the columns must
correspond to values in referenced primary key or unique columns or
that they are NULL.
• CHECK Specifies a wide range of rules for values in the table.
Null
• A null is no value at all.
• It does not mean a zero or a space
• a null (that is, no data entry at all) is not permitted in the primary key
• Nulls, if used improperly, can create problems because they have many
different meanings
• An unknown attribute value.
• A known, but missing, attribute value.
• A “not applicable” condition.

You might also like