Professional Documents
Culture Documents
Lecture 16
Relation Attributes/Fields/Columns
SSN
1
2
Tuples/rows/records
3
4
5
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.
4/14/2023 11
Candidate Key
• Is “STU_NUM, STU_LNAME” a Candidate Key ?
• No, because STU_NUM by itself is a candidate key
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
The ideal primary key is short, numeric(alpha), fixed length and never changes
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
PK: Employee ID
Unique Key: GovernmentNumber
• To enforce additional unique conditions on the columns
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.
• 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)
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.
• 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.