0% found this document useful (0 votes)
27 views

Unit2 Introduction To Database Systems

Uploaded by

Ayush Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Unit2 Introduction To Database Systems

Uploaded by

Ayush Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Introduction to Database Systems

by Nupur
File system vs DBMS

File System DBMS


File systems are simple and straightforward, but lack data A Database Management System (DBMS) offers a more
abstraction and data sharing. They also lack built-in data structured and organized approach to storing and
integrity and consistency mechanisms. retrieving data, providing better control, security, and
efficiency.
Structure of a DBMS
The structure of DBMS refers to the logical representation of all of its modules
A Database Management System has three major components:
Query Processor- The Query Processor receives the queries (requests) from the user and interprets them in the
form of instructions. It also executes the instructions received from the DML Compiler.
Storage Manager- Storage manager acts as the interface between the data stored in the database and the queries
received from the end-user. This component in the Structure of DBMS is responsible for the constraints applied to
the data so that it remains consistent. It also executes the DCL (Data Control Language)
Disk Storage- The Disk Storage in the Structure of DBMS represents the space where data is stored.
People who deal with database
Database Administrators Data Analysts
Responsible for creating and managing the database . Perform data analysis and extraction.

Data Scientists Software Developers


Contribute to developing applications that interact Develop applications that interact with the database
.

with the database .


Introduction to Data Models
Data Model is the modeling of the data description, data semantics, and consistency constraints of the data. It provides
the conceptual tools for describing the design of a database at each level of data abstraction. Therefore, there are
following four data models used for understanding the structure of the database:

1)Relational Data Model: This type of model designs the data in the form of rows and columns within a table. Thus, a
relational model uses tables for representing data and in-between relationships. Tables are also called relations.
2) Entity-Relationship Data Model: An ER model is the logical representation of data as objects and relationships among
them. These objects are known as entities, and relationship is an association among these entities.
3) Object-based Data Model: An extension of the ER model with notions of functions, encapsulation, and object identity,
as well. This model supports a rich type system that includes structured and collection types.

4) Semistructured Data Model: This type of data model is different from the other three data models (explained above).
The semistructured data model allows the data specifications at places where the individual data items of the same type
may have different attributes sets. The Extensible Markup Language, also known as XML, is widely used for
representing the semistructured data.
Concept of Normalization
What is Normalization?
Normalization is the process of organizing the data in the database.
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.
Normalization divides the larger table into smaller and links them using relationships.
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.
Introduction to ER Model
ER model stands for an Entity-Relationship model. It is a high-level data model. This model is used to define the data
elements and relationship for a specified system.
For example, Suppose we design a school database. In this database, the student will be an entity with attributes like
address, name, id, age, etc. The address can be another entity with attributes like city, street name, pin code, etc and
there will be a relationship between them.
Overview of Database Design, Entities,
Attributes and Relationships
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.
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.

Types of relationship are as follows:


a. One-to-One Relationship

When only one instance of an entity is associated with the relationship, then it is known as one to one relationship.
For example, A female can marry to one male, and a male can marry to one female.

b. One-to-many relationship

When only one instance of the entity on the left, and more than one instance of an entity on the right associates with the
relationship then this is known as a one-to-many relationship.

For example, Scientist can invent many inventions, but the invention is done by the only specific scientist.

c. Many-to-one relationship

When more than one instance of the entity on the left, and only one instance of an entity on the right associates with the
relationship then it is known as a many-to-one relationship.

For example, Student enrolls for only one course, but a course can have many students.

d. Many-to-many relationship

When more than one instance of the entity on the left, and more than one instance of an entity on the right associates
with the relationship then it is known as a many-to-many relationship.
For example, Employee can assign by many projects and project can have many employees.
Introduction to Relational Model

Relational model makes the query much easier than in hierarchical or network database systems.
A relational database is defined as a group of independent tables which are linked to each other using some
common fields of each related table.
This model can be represented as a table with columns and rows.
Each row is known as a tuple.
Each column of the table has a name or attribute.
Some popular relational databases are used nowadays like Oracle, Sybase, DB2, MySQL Server etc.
Integrity Constraints
Integrity constraints are a set of rules. It is used to maintain the quality of information.

Integrity constraints ensure that the data insertion, updating, and other processes have to be performed in such a

way that data integrity is not affected.

Thus, integrity constraint is used to guard against accidental damage to the database.

GeeksforGeeks
DBMS Integrity Constraints - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and
well explained computer science and programming articles, quizzes and…
Querying relational Data using SQL
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.

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;
DML and DDL Commands in SQL
DDL stands for Data Definition Language and refers to SQL commands used to create, modify, and delete database
structures such as tables, indexes, and views. DML stands for Data Manipulation Language and refers to SQL
commands used to insert, update, and delete data within a database. Now, letʼs learn about the DDL and DML
commands in depth.

What are DDL Commands in SQL?


DDL (Data Definition Language) is a type of SQL command used to define data structures and modify data. It creates,
alters, and deletes database objects such as tables, views, indexes, and users. Examples of DDL statements include
CREATE, ALTER, DROP and TRUNCATE.

What are DML Commands in SQL?


DML (Data Manipulation Language) is a type of SQL command used to manipulate data in a database. It inserts, updates,
and deletes data from a database table. Examples of DML statements include INSERT, UPDATE, and DELETE.
Types of DDL Statements
1. CREATE:It is used to create objects in the database, such as tables, views, stored procedures, and more.

2. ALTER:
It is used to modify the structure of an existing database object.

3. DROP:It is used to delete an entire object or part of an object from the database.

4. TRUNCATE: Used to delete all records from a table but does not delete the table structure.

5. RENAME: Used to rename an existing database object.


Types of DML Statements
1. INSERT: Used to add new records to a database table.
2. UPDATE: Used to modify existing records in a database table.
3. DELETE: Used to delete existing records from a database table.
4. MERGE: Used to combine data from two or more tables into one.
5. SELECT: Used to retrieve data from one or more tables in a database.
6. CALL: Used to call a stored procedure or function.
Aggregate Functions
SQL Aggregate functions are functions where the values of multiple rows are grouped as input on certain criteria to
form a single value result of more significant meaning.
It is used to summarize data, by combining multiple values to form a single result.
SQL Aggregate functions are mostly used with the GROUP BY clause of the SELECT statement.
Various Aggregate Functions
1. Count()
2. Sum()
3. Avg()
4. Min()
5. Max()
Aggregate Functions in SQL
Below is the list of SQL aggregate functions, with examples

Count():
Count(*): Returns the total number of records .i.e 6.
Count(salary): Return the number of Non-Null values over the column salary. i.e 5.
Count(Distinct Salary): Return the number of distinct Non-Null values over the column salary .i.e 5.

Sum():
sum(salary): Sum all Non-Null values of Column salary i.e., 3120.
sum(Distinct salary): Sum of all distinct Non-Null values i.e., 3120..

Avg():
Avg(salary) = Sum(salary) / count(salary) = 3120 / 5 = 624
Avg(Distinct salary) = sum(Distinct salary) / Count(Distinct Salary) = 3120 / 5 = 624

Min():
Min(salary): Minimum value in the salary column except NULL i.e., 403.

Max():
Max(salary): Maximum value in the salary i.e., 802.
Demo SQL Database
In this tutorial on aggregate functions, we will use the following table for examples:

Id Name Salary

1 A 802

2 B 403

3 C 604

4 D 705

5 E 606

6 F NULL
You can also create this table on your system, by writing the following queries:
CREATE TABLE Employee ( Id INT PRIMARY KEY, Name CHAR(1), -- Adjust data type and length if names can be longer
than a single character Salary DECIMAL(10,2) -- Adjust precision and scale if needed for salaries );
INSERT INTO Employee (Id, Name, Salary) VALUES (1, 'A', 802), (2, 'B', 403), (3, 'C', 604), (4, 'D', 705), (5, 'E', 606), (6,
'F', NULL);

Aggregate Function Example


In this example, we will use multiple aggregate functions on the data.
Queries --Count the number of employees SELECT COUNT(*) AS TotalEmployees FROM Employee;
-- Calculate the total salary SELECT SUM(Salary) AS TotalSalary FROM Employee;

-- Find the average salary SELECT AVG(Salary) AS AverageSalary FROM Employee;

-- Get the highest salary SELECT MAX(Salary) AS HighestSalary FROM Employee;

-- Determine the lowest salary SELECT MIN(Salary) AS LowestSalary FROM Employee;

Output
TotalEmployees
6

TotalSalary
3120

AverageSalary
624

HighestSalary
802

LowestSalary
403

You might also like