0% found this document useful (0 votes)
15 views5 pages

DBMS Unit-2

The relational model represents how data is stored in relational databases using tables with rows and columns. Each table has a unique name and schema defining its structure. Data is stored in rows called tuples and can be queried using SQL. Constraints like keys ensure data integrity when performing operations on the database.

Uploaded by

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

DBMS Unit-2

The relational model represents how data is stored in relational databases using tables with rows and columns. Each table has a unique name and schema defining its structure. Data is stored in rows called tuples and can be queried using SQL. Constraints like keys ensure data integrity when performing operations on the database.

Uploaded by

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

Relational Model:

The relational model represents how data is stored in Relational Databases. A relational database
consists of a collection of tables, each of which is assigned a unique name. Consider a relation STUDENT
with attributes ROLL_NO, NAME, ADDRESS, PHONE, and AGE shown in the table.
Table Student
ROLL_NO NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18
2 RAMESH GURGAON 9652431543 18
3 SUJIT ROHTAK 9156253131 20
4 SURESH DELHI 18

Important Terminologies
 Attribute: Attributes are the properties that define an entity. e.g.; ROLL_NO, NAME,
ADDRESS
 Relation Schema: A relation schema defines the structure of the relation and represents the
name of the relation with its attributes. e.g.; STUDENT (ROLL_NO, NAME, ADDRESS,
PHONE, and AGE) is the relation schema for STUDENT. If a schema has more than 1 relation,
it is called Relational Schema.
 Tuple: Each row in the relation is known as a tuple. The above relation contains 4 tuples, one
of which is shown as:

1 RAM DELHI 9455123451 18

 Relation Instance: The set of tuples of a relation at a particular instance of time is called a
relation instance. Table 1 shows the relation instance of STUDENT at a particular time. It can
change whenever there is an insertion, deletion, or update in the database.
 Degree: The number of attributes in the relation is known as the degree of the relation.
The STUDENT relation defined above has degree 5.
 Cardinality: The number of tuples in a relation is known as cardinality. The STUDENT relation
defined above has cardinality 4.
 Column: The column represents the set of values for a particular attribute. The
column ROLL_NO is extracted from the relation STUDENT.

ROLL_NO
1
2
3
4

 NULL Values: The value which is not known or unavailable is called a NULL value. It is
represented by blank space. e.g.; PHONE of STUDENT having ROLL_NO 4 is NULL.
 Relation Key: These are basically the keys that are used to identify the rows uniquely or also
help in identifying tables. These are of the following types.
 Primary Key
 Candidate Key
 Super Key
 Foreign Key
 Alternate Key
 Composite Key
Constraints in Relational Model
While designing the Relational Model, we define some conditions which must hold for data present
in the database are called Constraints. These constraints are checked before performing any
operation (insertion, deletion, and updation) in the database. If there is a violation of any of the
constraints, the operation will fail.
Domain Constraints
These are attribute-level constraints. An attribute can only take values that lie inside the domain
range. e.g.; If a constraint AGE>0 is applied to STUDENT relation, inserting a negative value of
AGE will result in failure.
Key Integrity
Every relation in the database should have at least one set of attributes that defines a tuple
uniquely. Those set of attributes is called keys. e.g.; ROLL_NO in STUDENT is key. No two
students can have the same roll number. So a key has two properties:
 It should be unique for all tuples.
 It can’t have NULL values.

Referential Integrity
When one attribute of a relation can only take values from another attribute of the same relation or
any other relation, it is called referential integrity. Let us suppose we have 2 relations

Table Student
ROLL_NO NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18
2 RAMESH GURGAON 9652431543 18
3 SUJIT ROHTAK 9156253131 20
4 SURESH DELHI 18

Table Branch
BRANCH_CODE BRANCH_NAME
CS COMPUTER SCIENCE
IT INFORMATION TECHNOLOGY
ECE ELECTRONICS AND COMMUNICATION ENGINEERING
CV CIVIL ENGINEERING

BRANCH_CODE of STUDENT can only take the values which are present in BRANCH_CODE of
BRANCH which is called referential integrity constraint. The relation which is referencing another
relation is called REFERENCING RELATION (STUDENT in this case) and the relation to which
other relations refer is called REFERENCED RELATION (BRANCH in this case).

Anomalies in the Relational Model


An anomaly is an irregularity or something which deviates from the expected or normal state.
When designing databases, we identify three types of anomalies: Insert, Update, and Delete.
Insertion Anomaly in Referencing Relation
We can’t insert a row in REFERENCING RELATION if referencing attribute’s value is not present
in the referenced attribute value. e.g.; Insertion of a student with BRANCH_CODE ‘ME’ in
STUDENT relation will result in an error because ‘ME’ is not present in BRANCH_CODE of
BRANCH.
Deletion/ Updation Anomaly in Referenced Relation:
We can’t delete or update a row from REFERENCED RELATION if the value of REFERENCED
ATTRIBUTE is used in the value of REFERENCING ATTRIBUTE. e.g; if we try to delete a tuple
from BRANCH having BRANCH_CODE ‘CS’, it will result in an error because ‘CS’ is referenced by
BRANCH_CODE of STUDENT, but if we try to delete the row from BRANCH with
BRANCH_CODE CV, it will be deleted as the value is not been used by referencing relation. It can
be handled by the following method:
On Delete Cascade
It will delete the tuples from REFERENCING RELATION if the value used by REFERENCING
ATTRIBUTE is deleted from REFERENCED RELATION. e.g.; For, if we delete a row from
BRANCH with BRANCH_CODE ‘CS’, the rows in STUDENT relation with BRANCH_CODE CS
(ROLL_NO 1 and 2 in this case) will be deleted.
On Update Cascade
It will update the REFERENCING ATTRIBUTE in REFERENCING RELATION if the attribute value
used by REFERENCING ATTRIBUTE is updated in REFERENCED RELATION. e.g;, if we update
a row from BRANCH with BRANCH_CODE ‘CS’ to ‘CSE’, the rows in STUDENT relation with
BRANCH_CODE CS (ROLL_NO 1 and 2 in this case) will be updated with BRANCH_CODE
‘CSE’.
Super Keys
Any set of attributes that allows us to identify unique rows (tuples) in a given relationship is known
as super keys. Out of these super keys, we can always choose a proper subset among these that
can be used as a primary key. Such keys are known as Candidate keys. If there is a combination
of two or more attributes that are being used as the primary key then we call it a Composite key.

Codd Rules in Relational Model


Edgar F Codd proposed the relational database model where he stated rules. Now these are
known as Codd’s Rules. For any database to be the perfect one, it has to follow the rules.

Advantages of the Relational Model


 Simple model: Relational Model is simple and easy to use in comparison to other languages.
 Flexible: Relational Model is more flexible than any other relational model present.
 Secure: Relational Model is more secure than any other relational model.
 Data Accuracy: Data is more accurate in the relational data model.
 Data Integrity: The integrity of the data is maintained in the relational model.
 Operations can be Applied Easily: It is better to perform operations in the relational model.
Disadvantages of the Relational Model
 Relational Database Model is not very good for large databases.
 Sometimes, it becomes difficult to find the relation between tables.
 Because of the complex structure, the response time for queries is high.
Characteristics of the Relational Model
 Data is represented in rows and columns called relations.
 Data is stored in tables having relationships between them called the Relational model.
 The relational model supports the operations like Data definition, Data manipulation, and
Transaction management.
 Each column has a distinct name and they are representing attributes.
 Each row represents a single entity.

Querying Relational Data:


A relational database query is a question about the data, and the answer consists of a new
relation containing the result. For example, we might want to find all students AGE less than 18
or all students enrolled in particular course.
The SELECT statement is used to fetch the data from a database table which returns this data in
the form of a result table. These result tables are called result-sets.
Syntax in Mysql

SELECT column1, column2, ...


FROM table_name;

If you want to select all the fields available in the table, use the following syntax:
Syntax in Mysql

SELECT * FROM table_name;

The symbol ´*´ means that we retain all fields of selected tuples in the result.
We can retrieve rows corresponding to students who are younger than 18 with the following SQL
query:
Example:

SELECT * FROM Students WHERE age < 18;

The condition age < 18 in the WHERE clause specifies that we want to select only tuples in
which the age field has a value less than 18.

In addition to selecting a subset of tuples, a query can extract a subset of the fields of each
selected tuple. we can compute the student_id and First_name of students who are younger than
18 with the following query:
Example:

SELECT ID, FirstName FROM Students WHERE age < 18;

SQL Aliases
Aliases are the temporary names given to tables or columns. An alias is created with
the AS keyword.
Alias Column Syntax in Mysql

SELECT column_name AS alias_name


FROM table_name;

Alias Table Syntax in Mysql

SELECT column_name(s)
FROM table_name AS alias_name;

Example:

SELECT studentID AS ID,


FROM students AS S;

Aliases can be useful when:

 There are more than one table involved in a query


 Functions are used in the query
 Column names are big or not very readable
 Two or more columns are combined togeth
SELECT data from Multiple Tables
We can also combine information from multiple tables.
Syntax in Mysql

SELECT table1.column1, table2.column2


FROM table1, table2
WHERE table1.column1 = table2.column1;

Example:

SELECT S.name, E.cid


FROM Students AS S, Enrolled AS E
WHERE S.sid = E.sid;

You might also like