You are on page 1of 26

BACHELOR OF COMPUTER APPLICATION

PRACTICAL
[SEMESTER-3]
Enrollment number-2121270167

BY-
ANKIT
BCA 301 (DATA BASE MANAGEMENT SYSTEMS)

- Relational Modal-
In the concept of relational database management system, data is organized into
tables. Tables are similar to folders in a file system, where each table stores a
collection of information. Tables are further divided into columns and rows. Columns
represent the attributes of an entity, while rows represent the entities themselves.

Let's take a relational database example: In a table that stores information about
students, the columns would represent attributes such as name, date of birth, and
major. In contrast, the rows would represent individual students.

A relational model is a powerful tool for organizing and manipulating data. However,
it is also relatively easy to understand and use.   
Features of Relational Model in DBMS
The relational model in DBMS has several features that make it well suited for storing
data: atomicity, consistency, isolation, durability, data consistency, data working
together, data flexibility, and lookup relationships.

Atomicity keeps data accurate by ensuring that all changes to the data are either
made completely or not made at all and that there are no partial changes to the
data. 

Consistency ensures that the state of the database remains consistent throughout
the transaction. 

Isolation ensures that changes made by one transaction are not visible to other
transactions until the changes are committed.

Durability ensures that once changes are committed, they will remain even if there is
a system failure. 
Types of Relational Models
There are generally three types of relational models in DBMS - 

1. The One-to-Many Model


In this type of relationship, one entity is related to multiple other entities. For
example, a customer may have multiple orders, a teacher may have multiple
students, and a product may be sold in multiple stores. The key feature of the one-to-
many model is that the relationship between two entities is not reciprocal. That is,
the fact that a customer has multiple orders does not mean that an order has
multiple customers.

2. The One-to-One Model


The one-to-one model of relational models states that there is a direct and unique
relationship between two specific objects. In other words, each object can only be
related to one other object and vice versa. This type of relationship is often used to
represent relationships between physical things, such as people and their driver's
license numbers. While the one-to-one model is relatively simple, it can be useful for
representing complex real-world relationships.

3. The Many-to-Many Model


In a many-to-many model, each record in the primary table can be linked to multiple
records in the foreign table and vice versa. This type of relationship is less common.
For example, a many-to-many relationship takes place between students and
courses. A student can take multiple courses, and a course can have multiple
students enrolled.

To create a many-to-many relationship, we need to create a third table, called a


junction table, that contains the primary keys of the other tables. The junction table
establishes the relationships between the rows in the two other tables.

Relational Model concepts in DBMS


Relational database concepts to understand in DBMS include :  
 Attribute: An attribute is a characteristic or quality of an entity. In the context of the
relational model, an attribute is a column in a table.  
 Tables: A table is a data collection organized into rows and columns. In the relational
model for database management, tables are used to store information about entities.  
 Tuple: A tuple is a row in a table. Tuples are composed of attributes.  
 Relation Schema: A relation schema is a blueprint for a table. It defines the attributes
that are contained in a table as well as the relationships between those attributes.  
 Degree: The degree is the number of attributes that it contains.  
 Cardinality: Cardinality defines the relationship between two attributes in a relation
schema. There are three possible relationships: one-to-one, one-to-many, and many-
to-many.  
 Column: A column is an attribute that is contained in a table. Columns are also
sometimes referred to as fields.  
 Relation instance: A relation instance is a set of tuples that conforms to a given
relation schema. In other words, it is populated data that has been organized into
rows and columns according to the blueprint set forth by a relation schema.  
 Relation key: A relation key is an attribute or combination of attributes that
uniquely identifies a tuple in a table.  
 Domain: The domain of an attribute is the set of values that can be stored in that
attribute. For example, if an attribute represents ages, its domain would be any age
from 0 onward. 
Advantages of using the Relational Model
The relational database model in DBMS is the most widely used model for managing
databases. It offers a number of advantages over other models, including accuracy,
ease of use, collaboration, security, and categorization.

Perhaps the most important advantage of the relational model is its accuracy.
Because data is organized into tables, it is easy to keep track of information and
ensure that it is up-to-date. This accuracy is essential for businesses that rely on
database information to make decisions.

The relational model is also easy to use. Users can easily access data and run queries
without needing to understand complex concepts. This ease of use makes the
relational model ideal for businesses needing to quickly retrieve information from
their databases.

The relational model also supports collaboration by allowing multiple users to access
and update the same data. This feature is essential for businesses that need to share
information between departments or across locations. 
Finally, the relational model offers security features that protect data from
unauthorized access and modification. These features are critical for businesses that
must ensure their data's confidentiality. This is why it is recommended to go for a  
designed exclusively for database administrators and enthusiasts. This is helpful to
those who want to deploy and administer cutting-edge applications at scale.

Disadvantages of using the Relational Model


While the relational model in DBMS  has many advantages, some disadvantages are
worth considering. 

One downside is the maintenance problem. As data is added, deleted, and updated,
the relational model can become increasingly complex and difficult to manage. This
can lead to costly errors and reduced efficiency.  

Another issue is cost. Relational databases can be expensive to set up and maintain.
They also require significant storage space, which can add to the overall cost.  

Another potential downside is the lack of scalability. As data grows, it can become
increasingly difficult to query and update a relational database in a timely manner.  
Finally, the structure of the relational model in DBMS is complex and can also be
challenging. The need for multiple tables and relationships can make it difficult to
understand and work with a relational database.  

For these reasons, it is important to weigh the pros and cons of implementing the
concept of a relational database management system before making a decision.

Conclusion
A relational model is a powerful tool for managing data in a DBMS. The relational
model provides a flexible way to store and retrieve information by structuring data
into tables and enforcing relationships between them. Additionally, using primary
and foreign keys ensures that data is consistent and can be easily linked together.

Despite these advantages, the relational model has some limitations, such as its
reliance on flat files and the difficulty of representing hierarchical data. However,
these limitations can be overcome with careful design and implementation.
If you are looking for more information on this topic, we suggest checking out our 
With this course, you will learn everything there is to know about using MongoDB,
such as writing code, building real-world projects and learning Cloud Labs.

Example: STUDENT Relation

NAME ROLL_NO PHONE_NO ADDRESS AGE

Ram 14795 7305758992 Noida 24

Shyam 12839 9026288936 Delhi 35

Laxman 33289 8583287182 Gurugram 20

Mahesh 27857 7086819134 Ghaziabad 27

Ganesh 17282 9028 9i3988 Delhi 40

o In the given table, NAME, ROLL_NO, PHONE_NO, ADDRESS, and AGE are the attributes.
o The instance of schema STUDENT has 5 tuples.
o t3 = <Laxman, 33289, 8583287182, Gurugram, 20>
Structure and Query Language
Structured Query Language (SQL) is a specialized programming language designed for
interacting with a database. SQL allows us to perform three main tasks:

1. To define the scope of the database


2. To add, delete or modify the data in the database
3. To read data from the database

Building Blocks of SQL


Modern SQL consists of three major types of query languages. Each of the languages
corresponds to a function or task described above. All three combine to form a fully
functioning language that allows a user to perform all possible functions on a
relational database.
Schema: Schema Query Language (Schema QL) is used to create tables and define
relationships between them. The most common example of Schema QL is the ‘CREATE
TABLE’ statement.

 Transact: Transact SQL provides the ability to insert, update, and delete data
stored in a relational database.
 Data: The data query language is the part used to interact with stored data. This
serves to enable users to query the data and conduct the task of data analysis.
Commonly Used SQL Statements
The following is a list of commonly used SQL commands that can be used to create
tables, insert data, change the structure of the tables, and query the data.

Defining and Creating Tables

CREATE

The CREATE statement is used to create tables in a database. The statement can define
the field names and field data types within a table. The CREATE statement is also used
to define the unique identities of the table using primary key constraints. It is also
used to describe the relationships between tables by defining a foreign key.

Template:

CREATE TABLE [table Name] (

Column_1 Datatype1,

Column_2 Datatype2..

);

ALTER

The ALTER statement is used to change the structure of a table in the database. The
statement can be used to create a new column or change the data type of an existing
column.

Template:

ALTER TABLE [table Name]

ADD Column_1, Datatype_1


DROP

The DROP statement is used to delete a table from a database. It must be used with
caution as deletion is irreversible.

Template:

DROP TABLE [table Name]

Adding, Modifying, and Deleting Data


INSERT

The INSERT statement is used to add records or rows to a table. The name of the table
to insert records into is defined first. Then, the column names within the table are
defined. Finally, the values of the records are defined to insert into these columns.

Template:

INSERT INTO [table Name] (Field_1,…,Field _N)

VALUES (Value_1,…,Value_ N)
UPDATE

The UPDATE statement is used to modify records in a table. The statement changes the
values of a specified subset of records held in the defined columns of the table. It is a
good practice to filter rows using a WHERE clause when updating records. Otherwise,
all records will be altered by the UPDATE statement.

Template:

UPDATE [table Name]

SET Column_1 = Value_1, …, Column_ N = Value_ N

WHERE [filter citeria ]

DELETE

The DELETE statement is used to delete rows from a table based on criteria defined
using a WHERE clause. The statement should be used carefully, as all deletion in a
database is permanent. If a mistake is made using a DELETE statement, the database
will need to be restored from a backup.

Template:
DELETE FROM [table Name]

WHERE [filter criteria]

Extracting and Analysing Data


SELECT

The SELECT statement is one of the most used statements in SQL. It is used to select
rows from one or more tables in a database. A SELECT statement is usually used with a
WHERE clause to return a subset of records based on a user-defined criterion. The
SELECT statement is used to conduct most data analysis tasks as it allows the user to
extract and transform the desired records from a database.

Template:

For specific columns:

SELECT Column_1, …, Column_ K FROM [table Name]

WHERE [filter criteria]

For all columns:


SELECT * FROM [table Name]

WHERE [filter criteria]

Building a Financial Database


In the following example, we will use some of the SQL statements discussed above to
create a financial database with pricing and fundamental data. Microsoft Access is an
accessible tool that can be used to build relational databases within the Microsoft
Office ecosystem.

First, we use the CREATE statement to define the structure of the table. Our table will
have the following columns and data types:

 ID: A unique identifier for every record entered in the table (INTEGER)
 Company Name: The name of the company (TEXT)
 Ticker: Company ticker used to identify the company stock on an exchange (TEXT)
 Price: The latest closing price of the stock (FLOAT)
 PE Ratio: The price to earnings ratio of the stock (FLOAT)

We can follow the template described above to write the CREATE statement. The
following statement creates the table:
CREATE TABLE price Table (

ID INTEGER,

Company Name TEXT (100),

ticker TEXT (20),

price FLOAT,

pe Ratio FLOAT

);

Next, we insert some records into the table using the INSERT statement. Following are
two examples of inserting rows in our table.

INSERT INTO price Table ( ID, [company Name], [ticker], [price], [pe Ratio])

VALUES (5, “Walmart”, “WMT”, 138, 26);

INSERT INTO price Table ( ID, [company Name], [ticker], [price], [pe Ratio])

VALUES (6, “General Motors Co.”, “GM”, 60 11);


Suppose we want to analyse the stocks in the database. We want to analyse the best
value stocks that are available. We can define value stocks as those with a price-to-
earnings ratio of less than twenty. It is accomplished using a SELECT statement with a
WHERE clause as shown below:

SELECT * FROM price Table WHERE pe Ratio < 20;

The instruction returns the three stocks: Intel, Ford, and General Motors. The above is
a simple example, but a larger database with more securities and fundamental data
can be a powerful tool in identifying potential investments.

A Short History of SQL


Structured Query Language (SQL) was first introduced in a paper from 1970, “A
Relational Model of Data for Large Shared Data Banks,” by Edgar F. Codd. Codd
introduced relational algebra, which is used to define relationships between data
tables. It is the theoretical foundation of SQL. The first implementation of SQL was
developed by two researchers at IBM: Donald Chamberlin and Raymond Boyce.

Additional Resources
Learn more about Structured Query Language through CFI’s SQL
Fundamentals course. To keep learning and developing your knowledge, we highly
recommend the additional resources .

Rules:
SQL follows the following rules:
o Structure query language is not case sensitive. Generally, keywords of SQL are written
in uppercase.
o Statements of SQL are dependent on text lines. We can use a single SQL statement on
one or multiple text line.
o Using the SQL statements, you can perform most of the actions in a database.
o SQL depends on tuple relational calculus and relational algebra.
SQL process:
o When an SQL command is executing for any RDBMS, then the system figure out the
best way to carry out the request and the SQL engine determines that how to interpret
the task.
o In the process, various components are included. These components can be
optimization Engine, Query engine, Query dispatcher, classic, etc.
o All the non-SQL queries are handled by the classic query engine, but SQL query engine
won't handle logical files.

ADVANTAGES OF SQL
There are the following advantages of SQL:

High speed
Using the SQL queries, the user can quickly and efficiently retrieve a large amount
of records from a database.

No coding needed
In the standard SQL, it is very easy to manage the database system. It doesn't
require a substantial amount of code to manage the database system.

Well defined standards


Long established are used by the SQL databases that are being used by ISO and
ANSI.

Portability
SQL can be used in laptop, PCs, server and even some mobile phones.

Interactive language - SQL is a domain language used to communicate with the


database. It is also used to receive answers to the complex questions in seconds.
Multiple data view-
Using the SQL language, the users can make different views of the database
structure.
Concurrency control Techniques
Concurrency control is provided in a database to:
 (i) enforce isolation among transactions.
 (ii) preserve database consistency through consistency preserving execution of transactions.
 (iii) resolve read-write and write-read conflicts.
Various concurrency control techniques are:
1. Two-phase locking Protocol
2. Time stamp ordering Protocol
3. Multi version concurrency control
4. Validation concurrency control
These are briefly explained below

. Two-Phase Locking Protocol:


 Locking is an operation which secures: permission to read, OR permission to write a data
item. Two phase locking is a process used to gain ownership of shared resources without
creating the possibility of deadlock. The 3 activities taking place in the two phase update
algorithm are:
(i). Lock Acquisition
(ii). Modification of Data
(iii). Release Lock
Two phase locking prevents deadlock from occurring in distributed systems by releasing
all the resources it has acquired, if it is not possible to acquire all the resources required
without waiting for another process to finish using a lock. This means that no process is
ever in a state where it is holding some shared resources, and waiting for another process
to release a shared resource which it requires. This means that deadlock cannot occur
due to resource contention. A transaction in the Two Phase Locking Protocol can assume
one of the 2 phases:
 (i) Growing Phase: In this phase a transaction can only acquire locks but cannot
release any lock. The point when a transaction acquires all the locks it needs is
called the Lock Point.
 (ii) Shrinking Phase: In this phase a transaction can only release locks but cannot
acquire any.
2. Time Stamp Ordering Protocol: A timestamp is a tag that can be attached to any
transaction or any data item, which denotes a specific time on which the transaction or
the data item had been used in any way. A timestamp can be implemented in 2 ways. One
is to directly assign the current value of the clock to the transaction or data item. The
other is to attach the value of a logical counter that keeps increment as new timestamps
are required. The timestamp of a data item can be of 2 types:
 (i) W-timestamp(X): This means the latest time when the data item X has been
written into.
 (ii) R-timestamp(X): This means the latest time when the data item X has been read
from. These 2 timestamps are updated each time a successful read/write operation
is performed on the data item X.
3. Multi version Concurrency Control: Multi version schemes keep old versions of data
item to increase concurrency. Multi version 2 phase locking: Each successful write results
in the creation of a new version of the data item written. Timestamps are used to label the
versions. When a read(X) operation is issued, select an appropriate version of X based on
the timestamp of the transaction. 
4. Validation Concurrency Control: The optimistic approach is based on the assumption
that the majority of the database operations do not conflict. The optimistic approach
requires neither locking nor time stamping techniques. Instead, a transaction is executed
without restrictions until it is committed. Using an optimistic approach, each transaction
moves through 2 or 3 phases, referred to as read, validation and write.
 (i) During read phase, the transaction reads the database, executes the needed
computations and makes the updates to a private copy of  the database values. All
update operations of the transactions are recorded in a temporary update file,
which is not accessed by the remaining transactions.
 (ii) During the validation phase, the transaction is validated to ensure that the
changes made will not affect the integrity and consistency of the database. If the
validation test is positive, the transaction goes to a write phase. If the validation
test is negative, he transaction is restarted and the changes are discarded.
 (iii) During the write phase, the changes are permanently applied to the database.
Database recovery Techniques

You might also like