Unit -2.2
Unit -2.2
Domain:
Relational schema: The relation’s name and columns names are present in a
relational schema.
o Relational Schema: used for describing (the structure of) a relation. E.g.,
R(A1, A2, ..., An) says that R is a relation with attributes A1, A2, ..., An. The
degree of a relation is the number of attributes it has, here n.
o Each attribute Ai is the name of a role played by some domain D in the
relation schema R. D is domain of Ai.
o Example:
Relation instance:
o The relation instance constitutes of a limited set of tuples in the relational
database system. There are no duplicate tuples in relation instances.
o Instance is alsoused to refer to a single tuple or row.
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.
For example, ID is used as a key in the Student table because it is unique for each
student. In the PERSON table, passport_number, license_number, SSN are keys
since they are unique for each person.
Types of keys:
1. Primary key
o It is the first key used to identify one and only one instance of an entity
uniquely. An entity can contain multiple keys, as we saw in the PERSON
table. The key which is most suitable from those lists becomes a primary key.
o In the EMPLOYEE table, ID can be the primary key since it is unique for each
employee. In the EMPLOYEE table, we can even select License_Number and
Passport_Number as primary keys since they are also unique.
o For each entity, the primary key selection is based on requirements and
developers.
2. Candidate key
o A candidate key is an attribute or set of attributes that can uniquely identify a
tuple.
o Except for the primary key, the remaining attributes are considered a candidate
key. The candidate keys are as strong as the primary key.
o For example: In the EMPLOYEE table, id is best suited for the primary key.
The rest of the attributes, like SSN, Passport_Number, License_Number, etc.,
are considered a candidate key.
3. Super Key
o We can define a super key as a set of those keys that identify a row or a tuple
uniquely. The word super denotes the superiority of a key. Thus, a super key is
the superset of a key known as a Candidate key (discussed in the next section).
It means a candidate key is obtained from a super key only.
o So, from the above table, we conclude the following set of the super keys:
o These all are the set of super keys which, together or combining with other
prime attributes, can identify a table uniquely.
o Just like, if we set Super key on Emp_SSN, it will be able to identify all other
tuples of the table very easily. Similarly, if we set the Super key on (Emp_Id,
Emp_name}, we can easily get the value or details of the other remaining
attributes of the employee. So, in this way, we can create and search out the
super keys from a table.
o Now, the question is how to identify the Primary key from a super key
because the super key is the superset. So, the answer is that a Primary key is
picked up from the super key set only by selecting a single attribute that can
provide a unique identification to the whole table. Hence, it is the
responsibility of the DBA to choose the most appropriate and suitable primary
key out of the candidate keys or super key.
4. Foreign key
o Foreign keys are the column of the table used to point to the primary key of
another table.
o 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.
o We add the primary key of the DEPARTMENT table, Department_Id, as a
new attribute in the EMPLOYEE table.
o In the EMPLOYEE table, Department_Id is the foreign key, and both the
tables are related.
5. Alternate key
o There may be one or more attributes or a combination of attributes that
uniquely identify each tuple in a relation. These attributes or combinations of
the attributes are called the candidate keys. One key is chosen as the primary
key from these candidate keys, and the remaining candidate key, if it exists, is
termed the alternate key. In other words, the total number of the alternate keys
is the total number of candidate keys minus the primary key. The alternate key
may or may not exist. If there is only one candidate key in a relation, it does
not have an alternate key.
o For example, employee relation has two attributes, Employee_Id and
PAN_No, that act as candidate keys. In this relation, Employee_Id is chosen
as the primary key, so the other candidate key, PAN_No, acts as the Alternate
key.
6. Composite key
o Whenever a primary key consists of more than one attribute, it is known as a
composite key. This key is also known as Concatenated Key.
Domain constraints specify that within each tuple, the value of each
attribute A must bean atomic value from the domain dom(A).
The data types associated with domains typicallyinclude standard
numeric data types for integers (such as short integer, integer, andlong
integer) and real numbers (float and double-precision float).
Characters, booleans,fixed-length strings, and variable-length strings
are also available, as are date, time, timestamp,and, in some cases,
money data types.
Other possible domains may be described bya subrange of values from
a data type or as an enumerated data type in which all possiblevalues
are explicitly listed.
Example:
o 2. Key Constraints:
Keys are the entity set that is used to identify an entity within its entity
set uniquely.
An entity set can have multiple keys, but out of which one key will be
the primary key. A primary key can contain a unique and null value in
the relational table.
Example 1:
Example 2:
o 3. Constraints on nulls:
By default, a column can hold NULL values.
The NOT NULL constraint enforces a column to NOT accept NULL
values.
This enforces a field to always contain a value, which means that you
cannot insert a new record, or update a record without adding a value
to this field.
Example:
SQL NOT NULL on CREATE TABLE: The following SQL ensures
that the "ID", "LastName", and "FirstName" columns will NOT accept
NULL values when the "Persons" table is created:
In the above example, we can see, that in the S_No column and last
tuple, the attribute is null, so it cannot be assigned as the primary key.
Now in the example above, we can see that in table 1, Code 5758 is not
valid, as this attribute is not defined in table 2 and also the attribute is
assigned as the primary key, and also Code in table 1 is assigned the
foreign key.
Relational Database Schemas
A relational database schema S is a set of relation schemas S
= (R 1, R2, ... , Rm} and a set of integrity constraints. (or)A
relational database schema is a set of relation schemas and a
set of integrity constraints.
Update Operations:
o The Update (or modify) operation is used to change the values of one or more
attributes in a tuple (or tuples) of some relation R. It is necessary to specify a
condition on the attributes of the relation to select the tuple (or tuples) to be
modified.
o Here are someexamples:
---------------------------------