0% found this document useful (0 votes)
59 views45 pages

Relational Database

The document discusses the relational data model and some key concepts used in relational databases. It defines tables, tuples, relations, attributes, and other terminology. It explains that the relational model uses tables to store data with rows representing tuples and columns representing attributes. Constraints are defined to maintain data integrity and ensure valid data types. Common constraints include NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK and DEFAULT. Primary keys uniquely identify each row while foreign keys link rows between tables by matching to a primary key column.

Uploaded by

abhiroopnigote
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views45 pages

Relational Database

The document discusses the relational data model and some key concepts used in relational databases. It defines tables, tuples, relations, attributes, and other terminology. It explains that the relational model uses tables to store data with rows representing tuples and columns representing attributes. Constraints are defined to maintain data integrity and ensure valid data types. Common constraints include NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK and DEFAULT. Primary keys uniquely identify each row while foreign keys link rows between tables by matching to a primary key column.

Uploaded by

abhiroopnigote
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 45

 Relational data model is the primary data model,

which is used widely around the world for data


storage and processing.
 It stores data in the form of tables.
 This concept was proposed by E.F. Codd, a researcher
of IBM in year 1960s.
 It uses a collection of tables to represent both data and
relationship among the data.
 Each table has multiple columns and each column has
unique name.
Basic Terminology used in Relational Model

 Tables − In relational data model, relations


are saved in the format of Tables. This format
stores the relation among entities. A table has
rows and columns, where rows represents
records and columns represent the attributes.
 Tuple − A single row of a table, which
contains a single record for that relation is
called a tuple.
 Relation instance − A finite set of tuples in the
relational database system represents relation
instance. Relation instances do not have
duplicate tuples.
 Relation schema − A relation schema
describes the relation name (table name),
attributes, and their names.
 Relation key − Each row has one or more

attributes, known as relation key, which can


identify the row in the relation (table)
uniquely.
 Attribute domain − Every attribute has some

pre-defined value scope, known as attribute


domain.
 Tuple of a relation (Each row is called as tuple)
 Cardinality of a relation (No. of tuples in a

relation)
 Degree of a relation (No. of attributes in a

relation)
 Domain (Set of all possible values that an attribute

my validly contain)
 Constraints are the rules enforced on data
columns on table. These are used to limit the
type of data that can go into a table. This ensures
the accuracy and reliability of the data in the
database.
 The whole purpose of constraints is to maintain
the data integrity during an update/delete/insert
into a table.
 Constraints could be column level or table level.
 Column level constraints are applied only to one
column, whereas table level constraints are
applied to the whole table.
 the values of individual columns are valid.
 In a table, rows have a valid primary key or unique key

values.
 in a dependent table, rows have valid foreign key values

that reference rows in a parent table.


 Domain Constraints
 Tuple Uniqueness Constraints
 Key Constraints
 Entity Integrity Constraint)
 Referential Integrity Constraint)
Domain Constraint
• Domain constraint defines the domain or set of values for an attribute.
• It specifies that the value taken by the attribute must be the atomic value
from its domain.

Example-
Consider the following Student table-

Here, value ‘A’ is not allowed since only integer values can be taken by the age attribute.
Tuple Uniqueness Constraint
Tuple Uniqueness constraint specifies that all the tuples must be
necessarily unique in any relation.

Example-01:

Consider the following Student table-

This relation satisfies the tuple uniqueness constraint since here all the tuples are
unique.
Example-02:
Consider the following Student table-

This relation does not satisfy the tuple uniqueness constraint since here all the tuples
are not unique.
Key Constraint
Key constraint specifies that in any relation-
•All the values of primary key must be unique.
•The value of primary key must not be null.

Example-

Consider the following Student table-

This relation does not satisfy the key constraint as here all the values of primary key are
not unique.
Entity Integrity Constraint
Entity integrity constraint specifies that no attribute of primary key must
contain a null value in any relation.
This is because the presence of null value in the primary key violates the
uniqueness property.

Example-

Consider the following Student table-

This relation does not satisfy the entity integrity constraint as here the primary key
contains a NULL value.
Referential Integrity Constraint
This constraint is enforced when a foreign key references the primary key of a relation.
It specifies that all the values taken by the foreign key must either be available in the
relation of the primary key or be null.

Important Results-

•The following two important results emerges out due to referential integrity
constraint-

•We can not insert a record into a referencing relation if the corresponding record
does not exist in the referenced relation.

•We can not delete or update a record of the referenced relation if the
corresponding record exists in the referencing relation.

•We can not insert a record into a referencing relation if the corresponding record
does not exist in the referenced relation.

•We can not delete or update a record of the referenced relation if the
corresponding record exists in the referencing relation.
Example-

Consider the following two relations- ‘Student’ and ‘Department’.


Here, relation ‘Student’ references the relation ‘Department’.
Here,

The relation ‘Student’ does not satisfy the referential integrity constraint.
This is because in relation ‘Department’, no value of primary key specifies
department no. 14.

Thus, referential integrity constraint is violated.


Referential Integrity Constraint Violation-
There are following three possible causes of violation of referential integrity constraint-

Cause-01: Insertion in a referencing relation

Cause-02: Deletion from a referenced relation

Cause-03: Updation in a referenced relation


Cause-01: Insertion in a Referencing Relation-

•It is allowed to insert only those values in the


referencing attribute which are already present
in the value of the referenced attribute.

•Inserting a value in the referencing attribute


which is not present in the value of the
referenced attribute violates the referential
integrity constraint.
Cause-02: Deletion from a Referenced Relation-

•It is not allowed to delete a row from the referenced


relation if the referencing attribute uses the value of
the referenced attribute of that row.

•Such a deletion violates the referential integrity


constraint.
Cause-03: Updation in a Referenced Relation-

•It is not allowed to update a row of the referenced


relation if the referencing attribute uses the value of
the referenced attribute of that row.

•Such an Updation violates the referential integrity


constraint.
Handling the Violation-

Restrict-- Here we reject the deletion/Updation.

Cascade –
Here if a record in the parent table(referencing relation) is
deleted/Updated, then the corresponding records in the
child table(referenced relation) will automatically be
deleted/Updated.

Set null or set default –


Here we modify the referencing attribute values that cause
violation and we either set NULL or change to another valid
value
SQL Constraints are rules used to limit the type of data that can go
into a table, to maintain the accuracy and integrity of the data inside
table.
 Following are commonly used constraints
available in SQL:
NOT NULL Constraint: Ensures that a column cannot
have NULL value.
DEFAULT Constraint: Provides a default value for a
column when none is specified.
UNIQUE Constraint: Ensures that all values in a
column are different. It can be a NULLL.
PRIMARY Key: Uniquely identified each rows/records
in a database table.
FOREIGN Key: Uniquely identified a rows/records in
any another database table.
CHECK Constraint: The CHECK constraint ensures
that all values in a column satisfy certain conditions.
 By default, a column can hold NULL values. If you
do not want a column to have a NULL value, then
you need to define such constraint on this column
specifying that NULL is now not allowed for that
column.
 A NULL is not the same as no data, rather, it
represents unknown data.
The UNIQUE Constraint prevents two records from
having identical values in a particular column. In the
CUSTOMERS table, for example, you might want to
prevent two or more people from having identical
age.
 UNIQUE constraint ensures that a field or column will
only have unique values. A UNIQUE constraint field
will not have duplicate data.
 UNIQUE constraint can be applied at column level or

table level.
If CUSTOMERS table has already been created, then to add a UNIQUE
constraint to AGE column, you would write a statement similar to the
following:
ALTER table customer add UNIQUE ( AGE);
 A primary key is a field in a table which uniquely identifies
each row/record in a database table. Primary keys must
contain unique values. A primary key column cannot have
NULL values.
 A table can have only one primary key, which may consist of
single or multiple fields. When multiple fields are used as a
primary key, they are called a composite key.
 If a table has a primary key defined on any field(s), then you
can not have two records having the same value of that
field(s).
 A foreign key is a key used to link two tables
together. This is sometimes called a referencing
key.
 Foreign Key is a column or a combination of
columns whose values match a Primary Key in a
different table.
 The relationship between 2 tables matches the
Primary Key in one of the tables with a Foreign Key
in the second table.
 The CHECK Constraint enables a condition to check
the value being entered into a record. If the
condition evaluates to false, the record violates the
constraint and isn't entered into the table.
DEFAULT Constraint
The DEFAULT constraint is used to set a default value for
a column.

The default value will be added to all new records, if no


other value is specified.
Keys in DBMS

A key is a set of attributes that can identify each tuple uniquely


in the given relation.

Super Key

Candidate Key

Primary Key

Alternate Key

Composite Key

Foreign Key
Super Key-
•A super key is a set of attributes that can identify each tuple
uniquely in the given relation.
•A super key is not restricted to have any specific number of
attributes.
•Thus, a super key may consist of any number of attributes

NOTE-

All the attributes in a super key are definitely sufficient to identify each
tuple uniquely in the given relation but all of them may not be necessary.
Candidate Key-

A minimal super key is called as a candidate key.


OR
A set of minimal attribute(s) that can identify each tuple
uniquely in the given relation is called as a candidate key.

NOTE-

•All the attributes in a candidate key are sufficient as well as


necessary to identify each tuple uniquely.
•Removing any attribute from the candidate key fails in
identifying each tuple uniquely.
•The value of candidate key must always be unique.
•The value of candidate key can never be NULL.
Primary Key-
A primary key is a candidate key that the database designer
selects while designing the database.
Alternate Key-

Candidate keys that are left unimplemented or unused after


implementing the primary key are called as alternate keys.
OR
Unimplemented candidate keys are called as alternate keys.
Foreign Key-

An attribute ‘X’ is called as a foreign key to some other


attribute ‘Y’ when its values are dependent on the values of
attribute ‘Y’.
NOTES-

•Foreign key references the primary key of the table.


•Foreign key can take only those values which are present in the
primary key of the referenced relation.
•Foreign key may have a name other than that of a primary key.
•Foreign key can take the NULL value.
•There is no restriction on a foreign key to be unique.
•In fact, foreign key is not unique most of the time.
•Referenced relation may also be called as the master table or
primary table.
•Referencing relation may also be called as the foreign table.
Composite Key-

A primary key comprising of multiple attributes and not


just a single attribute is called as a composite key.

You might also like