Unit 2_Relational Data Model
Unit 2_Relational Data Model
1. What is Table ?
In Relational database, a table is a collection of data elements organised in terms of rows and columns.
A table is also considered as convenient representation of relations. But a table can have duplicate tuples
While a true relation cannot have duplicate tuples. Table is the most simplest form of data storage.
Below is an example of Employee table.
Student_ID S_Name AGE Year
101 Satish Pise 20 2016
102 Sandip Patil 20 2016
2. What is a Record ?
A single entry in a table is called a Record or Row. A Record in a table represents set of related
data. For example, the above Employee table has 4 records. Following is an example of single
record.
3. What is Field ?
A table consists of several records(row), each record can be broken into several smaller entities
known asFields. The above Employee table consist of four fields, ID, Name, Age and Salary.
4. What is a Column ?
In Relational table, a column is a set of value of a particular type. The term Attribute is also used to
represent a column. For example, in Employee table, Name is a column that represent names of
employee.
2.2 Key Concepts
Super Key : - Super Key is defined as a set of attributes within a table that uniquely identifies each
record within a table. Super Key is a superset of Candidate key.
2. Explain candidate key and primary key. OR What is Primary Key? Give example.
OR
Q. Define the term.
(i) Candidate key
(ii) Primary key
i) Candidate Key
Candidate keys are defined as the set of fields from which primary key can be selected. It is an
attribute or set of attribute that can act as a primary key for a table to uniquely identify each record in
that table.A table could contain multiple candidate keys. This key cannot store a NULL. It must
contain Unique values There can be more than one candidate keys for a table.
Primary key is a candidate key that is most appropriate to become main key of the table. It is a key that
uniquely identify each record in a table.
Primary key: Within a relation there is always one attribute which has values that are unique in a
relation and thus can be used to identify tuple of that relation. Such a unique identifier is called the
primary key.
E.g. In the Student(Rollno,name,class,address) relation Rollno is the primary key.
2.3 Constraints
1. Explain not null constraints by suitable example.
Not null constraint: It is a type of Domain integrity constraint. It is used to maintain the value
according to user specification. By default columns allows null values. When not null constraint is
assigned on a column or set of column, then it will not allow null values.
Syntax:
Example:
1. Not Null constraint: This constraint ensures all rows in the table contain a definite value
for the column which is specified as not null. Which means a null value is not allowed.
Syntax: create table <table name>( Column_name Datatype (Size) [CONSTRAINTconstraint name]
NOT NULL );
Example: To create a employee table with Null value, the query would be like
CREATE TABLE employee ( id number(5), name char(20) CONSTRAINT nm_nn NOT NULL,
dept char(10), age number(2), salary number(10), location char(10));
OR
For Example: To create a employee table with Null value, the query would be like
CREATE TABLE employee ( id number(5), name char(20) NOT NULL,dept char(10),age number(2),
2) Check constraint: it defines a condition that each row must satisfy. A singlecolumn can have
column_name n datatype(size)
);
Example:
Example:
3. Primary Key constraint: It is use to avoid redundant/duplicate value entry within the row of
specified column in table. It restricts null values too.
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATA_TYPE, COLUMN_NAME
DATA_TYPE CONSTRAINT CONSTRAINT_NAME PRIMARY KEY);
Example: SQL> CREATE TABLE EMP (ID NUMBER (5) CONSTRAINT ID_PK PRIMARY
KEY, NAME VARCHAR2 (10), SAL NUMBER (10));
4. Unique Constraint: The UNIQUE constraint uniquely identifies each record in a database
table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for
uniquenessfor a column or set of columns. It allows null value.
Syntax:
CREATE TABLE <TABLE_NAME> (COLUMN_NAME DATA_TYPE,
COLUMN_NAME DATA_TYPE CONSTRAINT CONSTRAINT_NAME UNIQUE);
Example:
CREATE TABLE PERSONS (P_ID NUMBER CONSTRAINT P_UK UNIQUE,
FIRSTNAME VARCHAR (20), CITY VARCHAR (20));
5. Referential Integrity Constraint: It is a relational database concept in which multiple tables share a
relationship based on the data stored in the tables, and that relationship must remain consistent.
A value of foreign key is derived from primary key which is defined in parent table.
Syntax:
Ex :
Create table department( empno number (5), foreign key(emp_no) references emp(emp_no));
1. Primary Key constraint: It is use to avoid redundant/duplicate value entry within the row of specified
column in table. It restricts null values too.
Syntax:
SQL> CREATE TABLE EMP (ID NUMBER (5) CONSTRAINT ID_PK PRIMARY KEY,
Unique Constraint: The UNIQUE constraint uniquely identifies each record in a database table.
The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column
or set of columns.
Syntax:
Example:
Primary key is defined in a parent table and foreign key is defined in child table. The child
table contains the values for foreign key column which are present in parent tablesprimary
Create table tablename (column datatype size references parenttablename (primarykey attribute)….)
Example:
Create table product (EmpId number (4) references Emp (EmpId), PNamevarchar2(10));
Strong Entity Set: An entity set that has sufficient attributes to form a primary key is called
asstrong entity set.
Example: Cust_id is a primary key of Customer Entity so customer is strong entity set..
Weak Entity set: An entity set that does not have sufficient attribute to form a primary key is
called as Weak Entity Set.
Example: transaction is not having sufficient attribute to form a primary key so, transaction a
weak entity set
Q. What is the difference between weak entity set and strong entity set?
Attribute
Entities are represented by means of their properties, called attributes. All attributes have
values. For example, a student entity may have name, class, and age as attributes.
Types of Attributes
Simple attribute − Simple attributes are atomic values, which cannot be divided further.
For example, a student's phone number is an atomic value of 10 digits.
Composite attribute − Composite attributes are made of more than one simple attribute.
For example, a student's complete name may have first_name and last_name.
Derived attribute − Derived attributes are the attributes that do not exist in the physical
database, but their values are derived from other attributes present in the database. For
example, average_salary in a department should not be saved directly in the database,
instead it can be derived. For another example, age can be derived from data_of_birth.
Multi-value attribute − Multi-value attributes may contain more than one values. For
example, a person can have more than one phone number, email_address, etc.
ii) Multi valued attributes: A multi-valued attribute can have more than one value at one time.
Example: phone_no of a person is a multi-valued attribute since a person can have more than
one phone_no.
An Attribute describes a property or characterstic of an entity. For example, Name, Age, Address etc can
be attributes of a Student. An attribute is represented using eclipse.
Key Attribute
Key attribute represents the main characterstic of an Entity. It is used to represent Primary key.
Composite Attribute
An attribute can also have their own attributes. These attributes are known as Composite attribute.
Multivalued Attribute
Q. Draw E-R diagram for customer and loan. Assume suitable attribute.
Q. Draw E-R diagram for airline reservation system. Here a passenger can
book ticket from personal for a flight on same date.
Q. Draw an E-R diagram of hospital management system.
Binary Relationship
Recursive Relationship
Ternary Relationship
Binary Relationship
Binary Relationship means relation between two Entities. This is further divided into three types.
1. One to One : This type of relationship is rarely seen in real world.
The above example describes that one student can enroll only for one course and a course will also have
only one Student. This is not what you will usually see in relationship.
2. One to Many : It reflects business rule that one entity is associated with many number of same entity.
The example for this relation might sound a little weird, but this menas that one student can enroll to
many courses, but one course will have one Student.
The arrows in the diagram describes that one student can enroll for only one course.
3. Many to One : It reflects business rule that many entities can be associated with just one entity. For example,
Student enrolls for only one Course but a Course can have many Students.
4. Many to Many :
The above diagram represents that many students can enroll for more than one courses.
Second Normal Form (2NF): A relation is said to be in the second normal form if it is in first
normal form and all the non key attributes are fully functionally dependent on theprimary key.
Example: If in the relation Supp(SNO,SNAME, LOCATION,PNO,QTY),the atrributes
SNAME AND LOCATION depends on SNO and QTY dependence on (SNO, PNO) so the
table can be split up into two tables as Supplier (SNO,SNAME,LOCATION) and
SP(SNO,PNO,QTY) and now both the tables are in second normal form.
Supplier:
SP:
First Normal Form is defined in the definition of relations (tables) itself. This rule
defines that all the attributes in a relation must have atomic domains. The values in
an atomic domain are indivisible units.
Before we learn about the second normal form, we need to understand the following −
If we follow second normal form, then every non-prime attribute should be fully
functionally dependent on prime key attribute. That is, if X → A holds, then there
should not be any proper subset Y of X, for which Y → A also holds true.
We see here in Student_Project relation that the prime key attributes are Stu_ID
and Proj_ID. According to the rule, non-key attributes, i.e. Stu_Name and
Proj_Name must be dependent upon both and not on any of the prime key
attribute individually. But we find that Stu_Name can beidentified by Stu_ID
and Proj_Name can be identified by Proj_ID independently. This is called partial
dependency, which is not allowed in Second Normal Form.
We broke the relation in two as depicted in the above picture. So there exists no partial
dependency.
X is a superkey
Student_Detail Table :
Address Table :