0% found this document useful (0 votes)
19 views23 pages

Database by Muhammad Owais

Uploaded by

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

Database by Muhammad Owais

Uploaded by

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

[Date] DATA BASE

FOR ADA/ADS BS IT & CS

Muhammad Owais
IT 2ND SEMESTER
DATABASE BY MUHAMMAD OWAIS

What is a Database?
A database is a collection of organized data stored in a way that allows for efficient retrieval and
manipulation.

Database Management System (DBMS):


A DBMS is software that manages, stores, and retrieves data in a database.

File-based System
Think of a file-based system in DBMS as a basic way to organize information using files and folders on your
computer. Imagine you have a physical filing cabinet for your documents:

• Files: These are like papers inside the cabinet. Each paper holds specific information.
• Folders: These help organize your papers into different categories, like "Invoices," "Reports," and "Letters."

In a file-based system:
• Storage: Data is stored in separate files.
• Manual Management: You have to manually create, organize, and manage these files.
• Redundancy: Sometimes, you might accidentally duplicate data, like having the same invoice in two folders.
• Inconsistency: Since there's no central control, the same information might be updated differently in two
files.

Limitations of File Based System


 SEPARATION & ISOLATION OF DATA
It is difficult to access the data which is isolated and stored in separate files. Imagine we have to generate a
single report of student, who is studying in particular class, his study report, his library book details, and
hostel information. All these informations are stored in different files. How do we get all these details in one
report?

 DUPLICATION OF DATA (REDUNDANCY)


There is uncontrolled duplication of data in the file-based approach. Duplication is waste of space and it
costs time and money to enter the data more than once.Additional space requirements leads to
additional costs.Duplication of data means that the data is no longer consistent which leads to Loss of
Data Integrity.

 LOSS OF DATA INTEGRITY


Imagine Student Details and Student_ReportCard files have student’s address in it, and there was a
change request for one particular student’s address.
The program searched only Student Details file for the address and it updated it correctly.

1
DATABASE BY MUHAMMAD OWAIS
There is another program which prints the student’s report and mails it to the address mentioned
in the Student_ReportCard file.
What happens to the report of a student whose address is being changed? There is a mismatch in the
actual address and his report is sent to his old address. This mismatch in different copies of same data
is called data inconsistency (loss of data integrity).

 DATA DEPENDENCE
The data stored in file depends upon the application program through which the file was created. It means
that the structure of data files is coupled with application program.

 INCOMPATIBLE FILE FORMATS


The structure of files are dependent on the application programming languages. The structure of a file
generated by a PYTHON program may be different from the structure of a file generated by a ‘C’ program.
The incompatibility of such files makes them difficult to process jointly.

 FIXED QUERIES OF PROGRAMS


File-based systems are very dependent upon the application developer, who has to write any queries or
reports that are required. There was no facility for asking unplanned queries about the data or about the
type of data available

DATABASE
A database is an organized collection of structured information, or data, typically stored electronically in a computer
system. A database is usually controlled by a database management system (DBMS).

What is DBMS?
Database Management Systems (DBMS) are software systems used to store, retrieve, and run queries
on data. A DBMS serves as an interface between an end-user and a database, allowing users to create,
read, update, and delete data in the database.
 Data abstractions
in DBMS refer to hiding unnecessary data from the end-user. Example: If we want to retrieve any email
from Gmail, we don't know where that data is physically kept, such as in India or the United States, or
what data model was utilized to store it. These things are not essential to us
 Attribute
In a database management system (DBMS), an attribute is a piece of data that describes an entity. For
example, in a customer database, the attributes might be name, address, and phone number. In a
product database, the attributes might be name, price, and date of manufactur.
 ENTRY
In a database management system (DBMS), an entity is a piece of data that is stored in the database.
An entity can be a person, place, thing, or even an event. \

DBMS(DATABASE MANAGEMENT SYSTEM)


Database Management Systems (DBMS) are software systems used to store, retrieve, and run queries
on data. A DBMS serves as an interface between an end-user and a database, allowing users to create,
read, update, and delete data in the database.

2
DATABASE BY MUHAMMAD OWAIS

DDL AND DML

DDL (Data Definition Language) provides the ability to define, create and modify database objects such
as tables, views, indexes, and users.
DML (Data Manipulation Language) allows for manipulating data in a database, such as inserting,
updating, and deleting records.

 Query language and S-Q-L


in DBMS is primarily created for creating, accessing and modifying data in and out from the database
management system (DBMS). Typically, QL requires users to input a structured command that is similar
and close to the English language querying construct
 Application programs,
which are written using a combination of the data manipulation language of the DBMS and a conventional
programming language, request data elements from the database. Data elements called for by the
application programs are found and delivered by the DBMS.

Database architecture refers to the design and structure of a database system, defining how data is
stored, managed, and utilized. There are typically three levels of database architecture

Roles in the Database Environment


In this section, we examine what we listed in the previous section as the fifth component of the DBMS
environment:
the people. We can identify four distinct types of people who participate in the DBMS environment: data and
database administrators, database designers, application developers, and end-users.
•Data Administrator (DA)
Data administrator is a person who is responsible for processing data into a convenient data model. The
person is in charge of figuring out which data is relevant to be stored in the database. Data Administrator is
less of a technical role and more of a business role with some technical knowledge.
•A database administrator, or DBA,
is responsible for maintaining, securing, and operating databases and also ensures that data is correctly
stored
and retrieved. In addition, DBAs often work with developers to design and implement new features and
troubleshoot any issues.
•Database designers
Database designers are responsible for defining application requirements that include outlining the
database entities, defining relationships between those entities, and modeling the actual database
tables, views, and other objects.
• LOGICAL DATABASE DESIGNER
The logical database designer is concerned with identifying the data (that is, the entities and
attributes), the relationships between the data, and the constraints on the data that is to be stored
in the database. The logical database designer must have a thorough and complete understanding
of the organization’s data and any constraints on this data (the constraints are sometimes called

3
DATABASE BY MUHAMMAD OWAIS

business rules). These constraints describe the main characteristics of the data as viewed by the
organization

• PHYSICAL DATABASE DESIGNER

The physical database designer decides how the logical database design is to be physically realized. This
involves:

• mapping the logical database design into a set of tables and integrity constraints;
• selecting specific storage structures and access methods for the data to achieve good
performance;
• designing any security measures required on the data
Sure thing! Let's break it down even further.

Database Architecture: An In-Depth Look


• Internal Level (Physical Level)
• What It Is: This is the closest to the hardware and deals with how data is physically stored
on disk.
• Components:
• Data Files: These are actual files that store your data.
• Indexes: These help speed up data retrieval.
• Storage Structures: B-trees, hashing, etc., are methods used to organize data for efficient
access.
• Example: Think of this as the physical arrangement of books on the shelves in a library.
How they are stacked, the type of shelves, and how books are indexed for easy retrieval.

• Conceptual Level (Logical Level)


• What It Is: This level provides a unified view of the entire database, focusing on entities,
relationships, and constraints without worrying about physical details.
• Components:
• Entities: These are objects of interest, like "Student," "Course," "Instructor" in a school
database.
• Relationships: These define how entities are related, like a student enrolling in a course.
• Data Integrity Constraints: Rules like "a student ID must be unique."
• Example: This is like the catalog system of a library that helps you know the categories of
books available and how they are related (e.g., all science fiction books).

• External Level (View Level)


• What It Is: This level defines how individual users interact with the database.
• Components:
• User Views: Different users or user groups have different perspectives on the data.
• Access Control: Ensures users only see and interact with data they are authorized to
access.
• Example: This is like different sections of the library or different online catalogs tailored to
user preferences, such as a children’s section, a fiction section, or a research section.

Data independence

4
DATABASE BY MUHAMMAD OWAIS
Data independence refers to the ability to modify the schema at one level of a database system without having to
alter the schema at the next higher level. This concept is crucial in database management systems (DBMS) as it helps
in simplifying database management and enhances its flexibility.
There are two types of data independence:

1. Physical Data Independence


• What It Is: The ability to change the physical schema without altering the conceptual schema.
• Example: Changing how data is stored on disk (e.g., switching from one storage format to another) without
modifying the logical structure of the database.
• Benefit: Improves performance and storage efficiency without affecting application programs.

2. Logical Data Independence
• What It Is: The ability to change the conceptual schema without altering the external schema or application
programs.
• Example: Adding a new attribute to a table or changing relationships between tables without affecting how
users interact with the data.
• Benefit: Enhances the ability to expand and evolve the database as requirements change without impacting
user applications.

What is the 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.

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,

• 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.

5
DATABASE BY MUHAMMAD OWAIS
• Column: The column represents the set of values for a particular attribute. The column ROLL_NO is
extracted from the relation STUDENT.

• 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.

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:

6
DATABASE BY MUHAMMAD OWAIS
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.

What is the Relational Model?


The relational model for database management is an approach to logically represent and manage the data stored in a
database. In this model, the data is organized into a collection of two-dimensional inter-related tables, also
known as relations.
Each relation is a collection of columns and rows, where the column represents the attributes of an entity and the
rows (or tuples) represent the records.
The use of tables to store the data provided a straightforward, efficient, and flexible way to store and access
structured information. Because of this simplicity, this data model provides easy data sorting and data access.
Hence, it is used widely around the world for data storage and processing.

Let's look at a scenario to understand the relational model:


Consider a case where you wish to store the name, the CGPA attained, and the roll number of all the students of a
particular class. This structured data can be easily stored in a table as described below:

As we can notice from the above relation:


• Any given row of the relation indicates a student i.e., the row of the table describes a real-
world entity.
• The columns of the table indicate the attributes related to the entity. In this case, the roll
number, CGPA, and the name of the student.

Relational Model Concepts

7
DATABASE BY MUHAMMAD OWAIS

As discussed earlier, a relational database is based on the relational model. This database consists of various
components based on the relational model. These include:
• Relation: Two-dimensional table used to store a collection of data elements.
• Tuple: Row of the relation, depicting a real-world entity.
• Attribute/Field: Column of the relation, depicting properties that define the relation.
• Attribute Domain: Set of pre-defined atomic values that an attribute can take i.e., it describes the
legal values that an attribute can take.
• Degree: It is the total number of attributes present in the relation.
• Cardinality: It specifies the number of entities involved in the relation i.e., it is the total number
of rows present in the relation.
• Relational Schema: It is the logical blueprint of the relation i.e., it describes the design and
the structure of the relation. It contains the table name, its attributes, and their types:

TABLE_NAME(ATTRIBUTE_1 TYPE_1, ATTRIBUTE_2 TYPE_2, ...)

For our Student relation example, the relational schema will be:

STUDENT(ROLL_NUMBER INTEGER, NAME VARCHAR(20), CGPA FLOAT)

• Relational Instance: It is the collection of records present in the relation at a given time

Introduction of Relational Algebra in DBMS


Relational Algebra is a procedural query language. Relational algebra mainly provides a theoretical foundation for
relational databases and SQL. The main purpose of using Relational Algebra is to define operators that transform one
or more input relations into an output relation. Given that these operators accept relations as input and produce
relations as output, they can be combined and used to express potentially complex queries that transform potentially
many input relations (whose data are stored in the database) into a single output relation (the query results). As it is
pure mathematics, there is no use of English Keywords in Relational Algebra and operators are represented using
symbols.

Fundamental Operators

These are the basic/fundamental operators used in Relational Algebra.

• Selection(σ)
• Projection(π)
• Union(U)
• Set Difference(-)
• Set Intersection(∩)
• Rename(ρ)
• Cartesian Product(X)

What is Projection?
• This operation selects certain required attributes, while discarding other attributes. Projection in DBMS is a
process of selecting some specific attribute/columns from a table while excluding the other attributes/columns
from a selected table. It is useful for selecting a set of required attributes of the data.
• Notation –
• πA (R)
• Projection Query
• The following query retrieves only the Name and Salary columns from the Employees table:

8
DATABASE BY MUHAMMAD OWAIS
• Relation Algebra: πName Salary (Employees)
• SQL Query : SELECT Name, Salary FROM Employees;

Select operation
chooses the subset of tuples from the relation that satisfies the given condition mentioned in the syntax of selection.
The selection operation is also known as horizontal partitioning since it partitions the table or relation horizontally.

Notation:

σ c(R)

where ‘c’ is the selection condition which is a boolean expression(condition), we can have a single condition like
Roll= 3 or a combination of conditions like X>2 AND Y<1, and symbol ‘σ (sigma)’ is used to denote select(choose)
operator,

R is a relational algebra expression, whose result is a relation. The boolean expression specified in condition ‘c’ can be
written in the following form:

<attribute name> <comparison operator> <constant value> or <attribute name>

where <attribute name> is the name of an attribute of relation, <comparison operator> is any of the operator {<, >, =,
<=, >=, !=} and, <constant value> is a constant value taken from the domain of the relation.

Example-1:

σ Place = 'Mumbai' or Salary >= 1000000 (Citizen)


σ Department = 'Analytics'(σLocation = 'NewYork'(Manager))

The query above(immediate) is called nested expression, here, as usual, we evaluate the inner expression first (which
results in relation say Manager1), then we calculate the outer expression on Manager1(the relation we obtained from
evaluating the inner expression), which results in relation again, which is an instance of a relation we input.

Example-2:

Given a relation Student(Roll, Name, Department, Fees, Team) with the following tuples:

Select all the students of Team A :

σ Team = 'A' (Student) RESULT

Cartesian Product Operation in Relational Algebra


We already are aware of the fact that relations are nothing but a set of tuples, and here we will have 2 sets of
tuples.

On applying CARTESIAN PRODUCT on two relations that is on two sets of tuples, it will take every tuple one by
one from the left set(relation) and will pair it up with all the tuples in the right set(relation).

9
DATABASE BY MUHAMMAD OWAIS
So, the CROSS PRODUCT of two relation A(R1, R2, R3, …, Rp) with degree p, and B(S1, S2, S3, …, Sn) with
degree n, is a relation C(R1, R2, R3, …, Rp, S1, S2, S3, …, Sn) with degree p + n attributes.

CROSS PRODUCT is a binary set operation means, at a time we can apply the operation on two relations. But the two
relations on which we are performing the operations do not have the same type of tuples, which means Union
compatibility (or Type compatibility) of the two relations is not necessary.

Notation:

A✕S

where A and S are the relations,


the symbol ‘✕’ is used to denote the CROSS PRODUCT operator.

What are Joins in DBMS?


A join is a way to combine data from two or more tables based on a common column. For example, let’s say we have
two tables: Customers and Orders. The Customers table contains information about each customer, including their
name, address, and email address. The Orders table contains information about each order, including the order date,
product name, and quantity.

To combine data from these two tables, we can join them on the customer ID column, which is common to both tables.
By doing so, we can retrieve information about each customer’s orders in a single query.

TYPES OF JOINTS
INNER JOIN

• Description: Returns records that have matching values in both tables.

SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

LEFT JOIN (LEFT OUTER JOIN)

• Description: Returns all records from the left table, and the matched records from the right table. The result is
NULL from the right side if there is no match.

SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
RIGHT JOIN (RIGHT OUTER JOIN)

• Description: Returns all records from the right table, and the matched records from the left table. The result is
NULL from the left side if there is no match.

SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
FULL JOIN (FULL OUTER JOIN)

10
DATABASE BY MUHAMMAD OWAIS
• Description: Returns all records when there is a match in either left or right table. If there is no match, the
result is NULL from the left or right side.

SELECT columns
FROM table1
FULL JOIN table2
ON table1.column = table2.column;
CROSS JOIN

• Description: Returns the Cartesian product of both tables. This means it returns all records from the left table,
and each record is paired with all records from the right table.

SELECT columns
FROM table1
CROSS JOIN table2;
SELF JOIN
• Description: A regular join, but the table is joined with itself.
SELECT A.columns, B.columns
FROM table AS A, table AS B
WHERE condition;

Introduction to Database Keys


Keys are very important part of Relational database model. They are used to establish and identify relationships
between tables and also to uniquely identify any record or row of data inside a table.

A Key can be a single attribute or a group of attributes, where the combination may act as a key.

Super Key
Super Key is defined as a set of attributes within a table that can uniquely identify each record within a table. Super
Key is a superset of Candidate key.

In the table defined above super key would include student_id, (student_id, name), phone etc.

Confused? The first one is pretty simple as student_id is unique for every row of data, hence it can be used to identity
each row uniquely.

Next comes, (student_id, name), now name of two students can be same, but their student_id can't be same hence this
combination can also be a key.

Similarly, phone number for every student will be unique, hence again, phone can also be a key.

So they all are super keys.

Candidate Key
Candidate keys are defined as the minimal set of fields which can uniquely identify each record in a table. It is an
attribute or a set of attributes that can act as a Primary Key for a table to uniquely identify each record in that table.
There can be more than one candidate key.

In our example, student_id and phone both are candidate keys for table Student.

• A candiate key can never be NULL or empty. And its value should be unique.

• There can be more than one candidate keys for a table.

• A candidate key can be a combination of more than one columns(attributes).

11
DATABASE BY MUHAMMAD OWAIS

Primary Key
Primary key is a candidate key that is most appropriate to become the main key for any table. It is a key that can
uniquely identify each record in a table.

For the table Student we can make the student_id column as the primary key.

Composite Key
Key that consists of two or more attributes that uniquely identify any record in a table is called Composite key. But
the attributes which together form the Composite key are not a key independentely or individually.

Secondary or Alternative key


The candidate key which are not selected as primary key are known as secondary keys or alternative keys.

Non-key Attributes
Non-key attributes are the attributes or fields of a table, other than candidate key attributes/fields in a table.

Non-prime Attributes
Non-prime Attributes are attributes other than Primary Key attribute(s)..

What is Normalization?
o Normalization is the process of organizing the data in the database.

o Normalization is used to minimize the redundancy from a relation or set of relations. It is also used to
eliminate undesirable characteristics like Insertion, Update, and Deletion Anomalies.

o Normalization divides the larger table into smaller and links them using relationships.

o The normal form is used to reduce redundancy from the database table.

Why do we need Normalization?


The main reason for normalizing the relations is removing these anomalies. Failure to eliminate anomalies leads to
data redundancy and can cause data integrity and other problems as the database grows. Normalization consists of a
series of guidelines that helps to guide you in creating a good database structure.\

Types Of Normalization?
The types of normalization in a database management system (DBMS) are:
• First normal form (1NF)
The foundation of normalization, 1NF requires that a table's attribute can only hold a single value. This is done by
removing duplicate data and minimizing attributes and relations.

12
DATABASE BY MUHAMMAD OWAIS

• Second normal form (2NF)


2NF ensures that non-key attributes depend only on the primary key. This means that each column should have a
direct relationship with the primary key.

• Third normal form (3NF)


The most common standard for normalization in commercial databases, 3NF eliminates most database anomalies.

• Boyce-Codd normal form (BCNF)


BCNF can eliminate any remaining anomalies after 3NF. It helps to generate database schemas that store information
without unnecessary redundancy.

• Fourth normal form (4NF)


4NF aims to eliminate multi-valued dependencies from a relational database.

• Fifth normal form (5NF)


Also known as Project-Join Normal Form (PJNF), 5NF is the highest level of normalization. It's used to handle
complex many-to-many relationships in a database.

All types of database normalization are cumulative, meaning each one builds on top of the previous one.

What Are Functional Dependencies?


A functional dependency is a relationship between two sets of attributes in a database, where one set (the
determinant) determines the values of the other set (the dependent). For example, in a database of
employees, the employee ID number (determinant) would determine the employee’s name, address, and
other personal information (dependent). This means that, given an employee ID number, we can determine
the corresponding employee’s name and other personal information, but not vice versa.

Functional dependencies can also be represented using mathematical notation. For example, the functional
dependency above can be represented as:
Employee ID → Employee Name, Address, etc.

It’s important to note that functional dependencies only apply to the individual tuples in the table, and not to
the table as a whole.

13
DATABASE BY MUHAMMAD OWAIS

What are the different types of functional dependencies?


There are several types of functional dependencies, including full functional dependencies, partial functional
dependencies, and transitive functional dependencies.

• Full functional dependency


A full functional dependency is a type of functional dependency where the dependent attributes are determined by
the determinant attributes. For example, in the database of employees, the employee ID number fully determines the
employee’s name, address, and other personal information.

• Partial functional dependency


A partial dependency is a functional dependency where the dependent attributes are partially determined by the
determinant attributes. For example, in a database of employees, the employee ID number may partially determine the
employee’s address, but not the employee’s name or other personal information.

• Transitive functional dependency


A transitive functional dependency is a functional dependency where the dependent attributes are determined by a
set of attributes that are not included in the determinant attributes. For example, in a database of employees, the
employee ID number may determine the employee’s department, which in turn determines the employee’s salary.

An entity relationship model (ER model)


is a conceptual data model that describes the structure of a database by representing the relationships between entities:

• Purpose
The ER model is a blueprint for a database that helps developers and stakeholders understand the data organization.

• Visual representation
The ER model is visually represented as an Entity Relationship Diagram (ER Diagram) that uses boxes, symbols, and
connectors.

• Components
The ER model is based on three main components: entities, attributes, and relationships.

• Cardinality
Database management system (DBMS), cardinality refers to the relationship between data in one table and another
table. The relationship is specified by how many times an entity occurs in the relationship. Cardinality can be used to
define data models and analyze entities within datasets.
Types Of Cardinality
The three types of cardinality relationships are:
• One-to-one
One row in one table relates to one row in another table. For example, a school might use a one-to-one relationship to
assign a single student ID to each student.
• One-to-many
One row in one table relates to many rows in another table. For example, a doctor might have many patients.
• Many-to-many
Many rows in one table relate to many rows in another table. For example, a student might sign up for many classes,
and a class might have several students in it.

Cardinality can also be divided into high and low cardinality. A column or row has high cardinality if it consists of
many distinct values, and low cardinality if it consists of many repeated values

14
DATABASE BY MUHAMMAD OWAIS
• Example
An ER diagram could show the relationship between a student and a course, which might be many-to-many. A student
might be able to take more than one course, and a course might be taken by more than one student.

1. Entity:
An entity may be any object, class, person or place. In the ER diagram, an entity can be represented as rectangles.

Consider an organization as an example- manager, product, employee, department etc. can be taken as an entity.

a. Weak Entity

An entity that depends on another entity called a weak entity. The weak entity doesn't contain any key attribute of its
own. The weak entity is represented by a double rectangle.

2. Attribute

The attribute is used to describe the property of an entity. Eclipse is used to represent an attribute.

For example, id, age, contact number, name, etc. can be attributes of a student.

a. Key Attribute

The key attribute is used to represent the main characteristics of an entity. It represents a primary key. The key
attribute is represented by an ellipse with the text underlined.

15
DATABASE BY MUHAMMAD OWAIS
b. Composite Attribute

An attribute that composed of many other attributes is known as a composite attribute. The composite attribute is
represented by an ellipse, and those ellipses are connected with an ellipse.

c. Multivalued Attribute

An attribute can have more than one value. These attributes are known as a multivalued attribute. The double oval is
used to represent multivalued attribute.

For example, a student can have more than one phone number.

d. Derived Attribute

An attribute that can be derived from other attribute is known as a derived attribute. It can be represented by a dashed
ellipse.

For example, A person's age changes over time and can be derived from another attribute like Date of birth.

3. Relationship

A relationship is used to describe the relation between entities. Diamond or rhombus is used to represent the
relationship.

SQL
o SQL stands for Structured Query Language. It is used for storing and managing data in relational database
management system (RDMS). In RDBMS data stored in the form of the tables.

o It is a standard language for Relational Database System. It enables a user to create, read, update and delete
relational databases and tables.

o All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use SQL as their standard
database language.

o SQL allows users to query the database in a number of ways, using English-like statements.

o SQL is mostly used by engineers in software development for data storage. Nowadays, it is also used by data
analyst for following reason:

16
DATABASE BY MUHAMMAD OWAIS
QUERIES IN SQL

Creating a Table

This command creates a table named Students


with columns ID, FirstName, LastName, Age,
and Grade.

Inserting Data
INSERT
INSERT INTO
INTO Students
Students (ID,(ID, FirstName,
FirstName, LastName,
LastName, Age,Age, Grade)
Grade) VALUES
VALUES (1, (1, 'John'
'John', , 'Doe'
'Doe', 20,, 20,
'A');'A');
INSERT
INSERT INTOStudents
INTO Students (ID,FirstName,
(ID, FirstName, LastName,
LastName, Age,
Age, Grade)
Grade) VALUES
VALUES (2,(2, 'Jane'
'Jane', , 'Smith'
'Smith', , 22,
22, 'B');
'B');

Output: This command inserts two records into the Students table.

Selecting Data

17
DATABASE BY MUHAMMAD OWAIS

What is SQL Aggregation?


SQL aggregation is the task of collecting a set of values to return a single value. It is done with the help of aggregate
functions, such as SUM, COUNT, and AVG. For example, in a database of products, you might want to calculate the
average price of the whole inventory.
How is it used?
Aggregation in SQL is, typically, used in conjunction with grouping. The Group By clause is used to arrange rows into
groups in SQL. Aggregation, together with grouping, is key to generating quick reports and insights from a database.
For example, an ecommerce company might want to see its highest spending customers over a given time period.
Syntax for Aggregation and Grouping
The syntax for GROUP BY clause is:
GROUP BY ColumnName1, ColumnName2;
Here, ColumnName is the name(s) of the column(s) you want to apply the Group By Clause to.
The syntax for aggregation in SQL is:
AggregateFunctionName(DISTINCT or ALL GroupName)
Here, AggregateFunctionName is the name of the aggregate function you want to apply, such as SUM, AVG etc.
Within parentheses, you specify if you want to apply the function to a select group of values, or to all of them. If you
don't specify anything, SQL considers ALL as default. GroupName is the name of the group you want to apply the
aggregate function to.
Example of GROUP BY and Aggregate Functions
Typically, the two functions are used together to summarise a database. Let's consider a simple example of database of
voters. The database is called Voter_List. For each voter, Voter_List has the following columns:
• Voter_ID
• City_Name
• State_Name
• Language_Spoken
For our example, let us count the number of voters in each city. To do so, we will run the following SQL command:
SELECT City_Name
COUNT (Voter_ID) Voter_Count
FROM Voter_List
GROUP BY City_Name
ORDER BY City_Name;
The command will first group unique voter IDs for each city, and then count the IDs to give us the number of voters in
each city.
Commonly Used SQL Aggregate Functions
The following are some of the most commonly used SQL aggregate functions:
• AVG: This calculates the average of all values in a group.
• MIN: It returns the lowest value in a group.
• MAX: MAX aggregate function in SQL returns the largest value in a group
• COUNT: It is used to count the number of rows in a set. the COUNT function includes rows with
NULL values.
• SUM: This is used to calculate the sum of all non-NULL values in a group
Concurrency control in databases ensures that database transactions are performed concurrently without
violating the integrity of the database. Here are the key concepts:

Concurrency Control
1. Transactions: A sequence of operations performed as a single logical unit of work.
2. Concurrency Control Mechanisms: Techniques to manage simultaneous operations without
conflicts.

18
DATABASE BY MUHAMMAD OWAIS
o Locking: Prevents access to data by multiple transactions simultaneously. Common types of
locks:
▪ Shared Lock: Allows multiple transactions to read a resource.
▪ Exclusive Lock: Allows only one transaction to modify a resource.
o Timestamp Ordering: Ensures transactions are executed in the order of their timestamps.
o Optimistic Concurrency Control: Transactions execute without restrictions, and validation
is done at the end.
o Multiversion Concurrency Control (MVCC): Maintains multiple versions of data for
reading and writing.
Consistency Control
1. Consistency: Ensures the database remains in a valid state before and after a transaction.
2. ACID Properties: A set of properties to ensure reliable processing of transactions:
o Atomicity: Ensures that all operations of a transaction are completed, or none of them are.
o Consistency: Ensures that a transaction brings the database from one valid state to another.
o Isolation: Ensures that intermediate transaction results are invisible to other transactions.
o Durability: Ensures that once a transaction is committed, it remains so.

Database backup
Database backup is crucial for protecting data from loss, corruption, and other disasters. Here are some key
concepts and methods for database backup:
Types of Backups
1. Full Backup: Captures the entire database, including all data and objects. It’s the most comprehensive
but also the most time-consuming and storage-intensive.
BACKUP DATABASE [YourDatabase] TO DISK = 'C:\Backups\YourDatabase_Full.bak'
2. Differential Backup: Captures only the changes made since the last full backup. It’s faster and smaller
than full backups but depends on the last full backup.
sql
BACKUP DATABASE [YourDatabase] TO DISK = 'C:\Backups\YourDatabase_Differential.bak' WITH
DIFFERENTIAL
3. Incremental Backup: Captures only the changes made since the last backup of any type (full or
incremental). It’s the fastest and smallest but can be complex to restore.
sql
BACKUP LOG [YourDatabase] TO DISK = 'C:\Backups\YourDatabase_Incremental.bak'
Backup Strategies
1. Full Backup Strategy: Suitable for small databases where full backups can be taken regularly without
impacting performance.
2. Full + Differential Backup Strategy: A full backup is taken periodically (e.g., weekly), and
differential backups are taken more frequently (e.g., daily).
3. Full + Incremental Backup Strategy: A full backup is taken periodically (e.g., weekly), and
incremental backups are taken frequently (e.g., hourly or daily).
Best Practices
1. Automate Backups: Use scripts and tools to schedule regular backups.
2. Store Backups Securely: Store backups in a secure location, preferably offsite, to protect against
physical disasters.
3. Test Restore Procedures: Regularly test restoring backups to ensure that they can be successfully
recovered in case of an emergency.

19
DATABASE BY MUHAMMAD OWAIS
4. Use Encryption: Encrypt backups to protect sensitive data from unauthorized access.

Indexes in databases
Indexes in databases are special data structures that improve the speed of data retrieval operations. Here's a
detailed overview:
What Are Indexes?
Indexes are used to quickly locate and access the data in a database table. They work similarly to an index in
a book, allowing you to find information without scanning every page.
Types of Indexes
1. Primary Index: Created on the primary key of a table. Ensures that the key values are unique and
sorted.
sql
CREATE INDEX idx_primary ON Students (ID);
2. Secondary Index: Created on non-primary key columns to improve the performance of queries
involving these columns.
sql
CREATE INDEX idx_lastname ON Students (LastName);
3. Unique Index: Ensures that all the values in the indexed column are unique.
sql
CREATE UNIQUE INDEX idx_unique_email ON Students (Email);
4. Composite Index: Created on multiple columns of a table. Useful for queries involving multiple
columns in the WHERE clause.
sql
CREATE INDEX idx_composite ON Students (LastName, FirstName);
5. Full-Text Index: Used for full-text searches in large text fields.
sql
CREATE FULLTEXT INDEX idx_fulltext ON Students (Essay);
Benefits of Using Indexes
• Speed: Significantly improves the performance of read operations (SELECT queries).
• Efficiency: Reduces the amount of data scanned to find the desired rows.
• Sorting: Helps with sorting data (ORDER BY clause) without additional sorting steps.
Trade-offs and Considerations
• Storage: Indexes require additional storage space.
• Write Performance: Can slow down write operations (INSERT, UPDATE, DELETE) because the
indexes need to be updated as well.
• Maintenance: Requires periodic maintenance and optimization to ensure they remain efficient.
Examples of Using Indexes
sql
-- Creating an index on the LastName column
CREATE INDEX idx_lastname ON Students (LastName);

-- Creating a composite index on LastName and FirstName


CREATE INDEX idx_composite ON Students (LastName, FirstName);

-- Dropping an index
DROP INDEX idx_lastname ON Students;

20
DATABASE BY MUHAMMAD OWAIS

Indexes are a crucial part of database design and optimization. They need to be carefully planned and
maintained to balance the benefits of faster query performance against the costs of additional storage and
maintenance.

What is NoSQL?
NoSQL systems are a type of database designed to handle a wide variety of data models, especially useful
for large-scale data storage and for big data applications. Here’s an overview:
NoSQL stands for "Not Only SQL." It represents a broad category of databases that are designed to handle
data more flexibly than traditional relational databases (SQL databases). These databases are schema-less,
meaning they don't require a fixed table structure.
Types of NoSQL Databases
1. Document Stores: Store data as documents, usually in JSON or BSON format. Great for hierarchical
data.
o Example: MongoDB
o Usage: Content management systems, e-commerce.
2. Key-Value Stores: Simple databases where each item is a key-value pair. Optimized for fast
lookups.
o Example: Redis
o Usage: Caching, session management.
3. Column Family Stores: Store data in columns rather than rows, allowing for very high scalability.
o Example: Apache Cassandra
o Usage: Real-time analytics, event logging.
4. Graph Databases: Focus on the relationships between data points, using nodes, edges, and
properties.
o Example: Neo4j
o Usage: Social networks, recommendation engines.
Advantages of NoSQL
• Scalability: Designed to scale out horizontally, handling large volumes of data and high traffic.
• Flexibility: Schema-less design allows for rapid changes and adaptability to different data types.
• Performance: Optimized for read and write operations, often outperforming traditional SQL
databases for certain workloads.
• Big Data: Particularly suited for handling big data applications due to their scalability and
performance.
When to Use NoSQL
• When your application needs to handle large volumes of unstructured or semi-structured data.
• When you require high performance and scalability.
• When the data model is dynamic or evolving.
• When you need to store and query hierarchical or graph-based data efficiently.
Example of NoSQL (MongoDB)
json
// Document example in MongoDB
{
"_id": "12345",
"FirstName": "John",
"LastName": "Doe",

21
DATABASE BY MUHAMMAD OWAIS
"Age": 30,
"Skills": ["JavaScript", "Node.js", "MongoDB"]
}
This JSON document could be part of a larger collection representing multiple users.
Conclusion
NoSQL databases provide a powerful, flexible way to handle modern data needs. They complement
traditional SQL databases and offer solutions to specific challenges in data storage and management.

What is the Hierarchical Model in DBMS?


The Hierarchical Model was the first database management system model. This concept uses a hierarchical
tree structure to organise the data. The hierarchy begins at the root, which contains root data, and then grows
into a tree as child nodes are added to the parent node. This model accurately depicts several real-world
relationships such as food recipes, website sitemaps, and so on.
Example
The following diagram depicts the relationship between the shoes available on a shopping website:

THANK YOU

MUHAMMAD OWAIS MUNIR


GOVT GRADUATE COLLEGE OF SCIENCE MULTAN
CR OF BS-INFORMATIONAL TECHNOLOGY

22

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy