Relational Data Model
Relational Data Model
MODEL
SYLLABUS
RELATIONAL MODEL
Relational Model (RM) represents the database as a collection of relations. A
relation is nothing but a table of values. Every row in the table represents a
collection of related data values. These rows in the table denote a real-world
entity or relationship.
The table name and column names are helpful to interpret the meaning of values
in each row. The data are represented as a set of relations. In the relational model,
data are stored as tables. However, the physical storage of the data is independent
of the way the data are logically organized.
Some popular Relational Database management systems are:
•DB2 and Informix Dynamic Server – IBM
•Oracle and RDB – Oracle
•SQL Server and Access – Microsoft
RELATIONAL MODEL CONCEPTS IN DBMS
1.Attribute: Each column in a Table. Attributes are the properties which define a relation. e.g., Student_Rollno,
NAME,etc.
2.Tables – In the Relational model the, relations are saved in the table format. It is stored along with its entities. A
table has two properties rows and columns. Rows represent records and columns represent attributes.
3.Tuple – It is nothing but a single row of a table, which contains a single record.
4.Relation Schema: A relation schema represents the name of the relation with its attributes.
5.Degree: The total number of attributes which in the relation is called the degree of the relation.
6.Cardinality: Total number of rows present in the Table.
7.Column: The column represents the set of values for a specific attribute.
8.Relation instance – Relation instance is a finite set of tuples in the RDBMS system. Relation instances never have
duplicate tuples.
9.Relation key – Every row has one, two or multiple attributes, which is called relation key.
10.Attribute domain – Every attribute has some pre-defined value and scope which is known as attribute domain
RELATIONAL INTEGRITY CONSTRAINTS
3. Referential Integrity Constraints
•A referential integrity constraint is specified between two tables.
•In the Referential integrity constraints, if a foreign key in Table 1 refers to the
Primary Key of Table 2, then every value of the Foreign Key in Table 1 must
be null or be available in Table 2.
Example:
4. 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:
39.8M
654
Hello Java Program for Beginners
The following are commonly used constraints available in PostgreSQL.
•NOT NULL Constraint − Ensures that a column cannot have NULL value.
•UNIQUE Constraint − Ensures that all values in a column are different.
•PRIMARY Key − Uniquely identifies each row/record in a database table.
•FOREIGN Key − Constrains data based on columns in other tables.
•CHECK Constraint − The CHECK constraint ensures that all values in a
column satisfy certain conditions.
1 . N O T N U L L C o n s t r a i n t
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 NOT NULL constraint is always written as a column constraint.
A NULL is not the same as no data; rather, it represents unknown data.
Example
For example, the following PostgreSQL statement creates a new table called COMPANY1 and adds
five columns, three of which, ID and NAME and AGE, specify not to accept NULL values −
CREATE TABLE COMPANY1( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT
NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL );
2. UNIQUE Constraint
The UNIQUE Constraint prevents two records from having identical values in a particular
column. In the COMPANY table, for example, you might want to prevent two or more people
from having identical age.
Example
For example, the following PostgreSQL statement creates a new table called COMPANY3
and adds five columns. Here, AGE column is set to UNIQUE, so that you cannot have two
records with same age −
The PRIMARY KEY constraint uniquely identifies each record in a database table. There can be
more UNIQUE columns, but only one primary key in a table. Primary keys are important when
designing the database tables. Primary keys are unique ids.
We use them to refer to table rows. Primary keys become foreign keys in other tables, when
creating relations among tablesA 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 cannot have two records having the
same value of that field(s).
Example
CREATE TABLE COMPANY4( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT
NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL );
4.FOREIGN KEY Constraint
A foreign key constraint specifies that the values in a column (or a group of columns) must
match the values appearing in some row of another table. We say this maintains the referential
integrity between two related tables. They are called foreign keys because the constraints are
foreign; that is, outside the table. Foreign keys are sometimes called a referencing key.
Example
For example, the following PostgreSQL statement creates a new table called COMPANY5 and
adds five columns.
CREATE TABLE COMPANY6( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT
NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL );
For example, the following PostgreSQL statement creates a new table called DEPARTMENT1,
which adds three columns. The column EMP_ID is the foreign key and references the ID field of
the table COMPANY6.
CREATE TABLE DEPARTMENT1( ID INT PRIMARY KEY NOT NULL, DEPT CHAR(50)
NOT NULL, EMP_ID INT references COMPANY6(ID) );
5.CHECK Constraint
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 is not entered into the
table.
Example
For example, the following PostgreSQL statement creates a new table called COMPANY5 and
adds five columns. Here, we add a CHECK with SALARY column, so that you cannot have any
SALARY as Zero.
CREATE TABLE COMPANY5( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT
NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL CHECK(SALARY >
0) );
Aggregate functions
Aggregate functions in DBMS take multiple rows from the table and return a value according to the
query.
All the aggregate functions are used in Select statement.
Syntax −
SELECT <FUNCTION NAME> (<PARAMETER>) FROM <TABLE NAME>
1.AVG Function
This function returns the average value of the numeric column that is supplied as a parameter.
Example: Write a query to select average salary from employee table.
Select AVG(salary) from Employee;
2.COUNT Function
The count function returns the number of rows in the result. It does not count the null values.
Example: Write a query to return number of rows where salary > 20000.
Select COUNT(*) from Employee where Salary > 20000;
Types −
•COUNT(*): Counts all the number of rows of the table including null.
•COUNT( COLUMN_NAME): count number of non-null values in column.
•COUNT( DISTINCT COLUMN_NAME): count number of distinct values in a column.
3.MAX Function
The MAX function is used to find maximum value in the column that is supplied as a parameter. It can
be used on any type of data.
Example − Write a query to find the maximum salary in employee table.
Select MAX(salary) from Employee;
4.MIN Function
The MIN function is used to find minimum value in the column that is
supplied as a parameter. It can be used on any type of data.
Example − Write a query to find the minimum salary in employee table.
Select MIN(salary) from Employee;
5.SUM Function
This function sums up the values in the column supplied as a parameter.
Example: Write a query to get the total salary of employees.
Select SUM(salary) from Employee ;
QUERY LANGUAGE
The Query language is the language in which user request information from the
database. Query language is primarily created for creating, accessing and modifying
data in and out from a database management system (DBMS). Typically, QL requires
users to input a structured command that is similar and close to the English language
querying construct.
For example, the SQL query: SELECT * FROM
The customer will retrieve all data from the customer records/table.
Query Language
In simple words, a Language which is used to store and retrieve data from database is known as query language. For example – SQL
There are two types of query language:
1. Procedural Query language:
In procedural query language, user instructs the system to perform a series
of operations to produce the desired results. Here users tells what data to be
retrieved from database and how to retrieve it.
2. Non-procedural query language:
In Non-procedural query language, user instructs the system to produce the
desired result without telling the step by step process. Here users tells what
data to be retrieved from database but doesn’t tell how to retrieve it.
Relational Algebra:
Relational algebra is a conceptual procedural query language used on
relational model.
Relational Calculus:
Set Difference is denoted by – symbol. Lets say we have two relations R1 and R2 and we want to select
all those tuples(rows) that are present in Relation R1 but not present in Relation R2, this can be done
using Set difference R1 – R2.
Syntax of Set Difference (-)
table_name1 - table_name2
Set Difference (-) Example
Lets take the same tables COURSE and STUDENT that we have seen above.
Query:
Lets write a query to select those student names that are present in STUDENT table but not present in
COURSE table.
Query to display the last name of those students where age is greater than 30
Last_Name
---------
Singh
Query to display all the details of students where Last name is ‘Singh’
{ t | Student(t) AND t.Last_Name = 'Singh' }
Output:
First_Name Last_Name Age
---------- --------- ----
Ajeet Singh 30
Chaitanya Singh 31
2. Domain Relational Calculus (DRC)
In domain relational calculus the records are filtered based on the domains.
Again we take the same table to understand how DRC works.
Table: Student
First_Name Last_Name Age
---------- --------- ----
Ajeet Singh 30
Chaitanya Singh 31
Rajeev Bhatia 27
Carl Pratap 28
Query to find the first name and age of students where student age is greater than 27
Table name:Student_Detail
Query:
CREATE VIEW DetailsView AS SELECT NAME, ADDRESS FROM Student_Details
WHERE STU_ID < 4;
SELECT * FROM DetailsView;
Output:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad