This document provides an introduction to relational databases and database management systems (DBMS). It discusses the purpose of databases and DBMS, including storing data efficiently and securely. It also describes key concepts like the three-schema architecture, database languages including SQL, database structures like tables and relations, and how programming languages can access SQL.
3. UNIT I - RELATIONAL DATABASES
Purpose of Database System - Views of data –
Database Languages - Database Architecture -
Introduction to Relational Model - Structure of
Relational Databases - Database Schemas – Keys -
Schema Diagrams - Relational Query Languages -
Relational Operations - Instruction to SQL - Accessing
SQL from a programming language
4. What is Data?
In simple words, data can be facts related to any object
in consideration. For example, your name, age, height, weight,
etc. are some data related to you.
A picture, image, file, pdf, etc. can also be considered
data.
5. Database
A database is a collection of related data organized in a way
that data can be easily accessed, managed and updated.
Database can be a software or hardware based , with sole
purpose of storing data.
Example:
An online telephone directory uses a database to store data
of people, phone numbers, and other contact details.
Facebook. It needs to store, manipulate, and present data
related to members, their friends, member activities,
messages, advertisements, and a lot more.
6. Database Management Systems
Definition:
“A Collection of interrelated data and a set of programs to
access those data”
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.
Example: MySQL, PostgreSQL, Microsoft SQL Server, Oracle
Database, and Microsoft Access
7. DBMS –Manage the Data
Who Access the Data
When Access the Data
How Access the Data
Features :
Security of Data
Durability of Data( ability to withstand)
Isolation of Data(privacy)
Consistency of Data(stable)
Reliability of Data(performs consistently without
causing problems)
Scalability of Data(ability to expand the capacity of a
database system)
9. Purpose of Database System
Disadvantages of File System:
Data Redundancy
Data Inconsistency
Difficulty in Accessing Data
Limited Data Sharing
Integrity Problems
Concurrent Access Anomalies
Security Problems
10. Purpose:
Storage Store the data in Efficient way so that
retrieval can be made easy
Security Securing the data or privacy
Flexibility Small or Huge data
Reliability Data stored should not be changed
11. View of data in DBMS narrate how the data is visualized at each level
of data abstraction
Data Abstraction
Data abstraction is hiding the complex data structure in order
to simplify the user’s interface of the system.
Hiding irrelevant details from user and providing abstract view of
data to users, helps in easy and efficient user-database interaction
Views of Data
12. To achieve data abstraction, use Three-Schema architecture which
abstracts the database at three levels.
Three-Schema Architecture:
The main objective -> separation between the user interface and
the physical database.
The three-schema architecture defines the view of data at three
levels:
Physical level (internal level)
Logical level (conceptual level)
View level (external level)
13.
14. Physical level:
how data is actually stored in database
Logical level:
what data is stored in database and what relationships exist
among those data
View level:
the user interaction with database system.
15. 1. Physical Level/ Internal Level
how the data is stored in the hardware. It also
describes how the data can be accessed.
The physical level shows the data abstraction at the
lowest level and it has complex data structures.
Only the database administrator operates at this
level.
16. 2. Logical Level/ Conceptual Level
It is a level above the physical level.
data is stored in the form of the entity set, entities, their data types,
the relationship among the entity sets, user operations performed to
retrieve or modify the data.
As users are restricted to access some particular parts of the database.
It is the developer and database administrator who operates
at the logical or the conceptual level.
17. 3. View Level/ User level/ External level
It is the highest level of data abstraction and exhibits only a
part of the whole database.
It exhibits the data in which the user is interested.
The view level can describe many views of the same data.
Here, the user retrieves the information using different
application from the database.
18. Schema and Instances
Schema:
The overall design of the database is called the
database schema.
Design of a database
only a structural view(design) of a database.
The schema for student table
Reg. No Name Marks
19. Types of Schema:
Physical Schema:
how the data stored in blocks of storage
Logical schema:
data can be described as certain types of data records
gets stored in data structures.
View schema or Subschema:
end user interaction with database systems
A database may also have several schemas at the view
level, sometimes called subschemas, that describe
different views of the database.
20. Instance:
The data stored in database may gets changed while inserting
or deleting.
The collection of information at particular moment is called
instance of data.
The schema for student table
Reg. No Name Marks
1 AAA 410
2 BBB 446
3 CCC 480
22. Database Languages
used to read, update and store data in a database.
Types:
Data Definition Language (DDL)
Data Manipulation Language (DML)
Data Control language (DCL)
Transaction Control Language(TCL)
24. Data Definition Language (DDL):
Specifying the database schema by a set of definitions.
operations:
To create the database instance – CREATE
To alter the structure of database – ALTER
To drop database instances – DROP
To delete tables in a database instance – TRUNCATE
To rename database instances – RENAME
To drop objects from database such as tables – DROP
To Comment – Comment
25. Data Manipulation Language (DML)
accessing and manipulating data in a database
operations:
To read records from table(s) – SELECT
To insert record(s) into the table(s) – INSERT
Update the data in table(s) – UPDATE
Delete all the records from the table – DELETE
26. Procedural DML – Require a user to specify what data are needed
and how to get those data.
The procedural DMLs are also called one-at-a-time DMLs as it
retrieves and processes each record separately.
27. Non Procedural /Declarative DML - Require a user to specify what
data are needed and without specifying how to get those data.
The non-procedural DMLs are also called set-a-time DMLs; this
is because a non-procedural DMLs can retrieve several records
using a single DML command.
Non-procedural DMLs are also called declarative languages.
28. Data Control language (DCL)
Granting and revoking user access on a database
operations:
To grant access to user – GRANT
To revoke access from user – REVOKE
29. Transaction Control Language(TCL)
The changes in the database that we made using DML
commands are either performed or rollbacked using TCL
operations:
To persist the changes made by DML commands in database –
COMMIT
To rollback the changes made to the database – ROLLBACK
34. Table or relation:
a collection of data represented in rows and columns
cannot have duplicate data or rows.
Roll No Name Marks Phone
001 AAA 88 111
002 BBB 83 222
003 CCC 98 333
004 DDD 67 444
35. Tuple or record or row
Each row of a table is known as record
a set of related data
004 DDD 67 444
36. Attribute or columns:
Each record can be broken down into several small
parts of data.
Ex: RollNo, Sname, Marks, Phone
Relation Schema and Instance:
Schema: structure of the relation
Instance: a specific set of rows
37. Domain:
a set of permitted values for an attribute in table.
Example: a set of all possible marks of the students.
Domain of marks (88,83,98)
38. Degree:
Total number of columns present in the relational
database
Cardinality:
Total number of rows present in the relational
database
In the above table, degree is 3 and cardinality is 3.
Emp# Job Name Salary
E10 Sales 12500
E12 Null 25000
39.
40. Foreign key
it is an attribute which establish relationship
between another table.
to point to the primary key of another table.
Parent Table Child Table
43. Selection (σ)
to fetch rows or tuple from the table.
syntax:
σpredicate(relation)
predicate – logic using which the data from the relation is selected.
Ex: Student – Table
Reg. No Sname Age
1 AAA 15
2 BBB 18
3 CCC 16
σage>15(Student)
45. to project only a certain set of attributes of a relation.
remove duplicate data from the columns.
Syntax: ΠA1,A2,…(r)
Projection:
46.
47. Cartesian product:
to combine data from two different relations into one and fetch
data from the combined relation
Syntax: A x B
Table A1
A B
1 1
2 2
Table B1
C D E
1 2 7
2 6 8
5 7 9
A1 x B1
A B C D E
1 1 1 2 7
1 1 2 6 8
1 1 5 7 9
2 2 1 2 7
2 2 2 6 8
2 2 5 7 9
49. Union:
to fetch data from two relations
the relations specified should have same number
of attributes and same attribute domain.
duplicated tuples are automatically eliminated
from the result.
Syntax: A ᴜ B
50. Table A1
A B
1 1
1 2
2 1
Table B1
A B
1 2
2 3
A1 ᴜ B1
A B
1 1
1 2
2 1
2 3
51. Intersection:
to fetch data from both tables which is common in both
the tables.
Syntax: A ∩ B
Table A1
Name id
aaa 1
bbb 2
ccc 1
Table B1
Name course
aaa C
ccc C++
A1 ∩ B1
Name
aaa
ccc
52. Set-Difference:
the result of set difference is tuples, which are present in
one relation but are not in the second relation.
Syntax: A - B
Table A1
A B
1 1
1 2
2 1
Table B1
A B
1 2
2 3
A1 - B1
A B
1 1
2 1
53. Rename Operation:
to rename the output relation for any query relation which returns the
result.
Syntax: ρ (RelationNew, RelationOld)
Table A1
A B
1 1
2 2
ρ (A1,B1)
Table B1
A B
1 1
2 2
55. a database programmer must have access to a general-
purpose programming language for at least two reasons:
SQL does not provide the full expressive power of a
general-purpose language
(C, Java, or Python that cannot be expressed in SQL. To
write such queries, we can embed SQL within a more
powerful language.)
Non declarative actions—such as printing a report,
interacting with a user, or sending the results of a query to
a graphical user interface—cannot be done from within
SQL
Accessing SQL From a Programming Language
56. Accessing SQL From a Programming Language
To access SQL from other programming languages, we can use:
Dynamic SQL: JDBC and ODBC
Embedded SQL
PHP
57. ODBC (Open Database Connectivity) and JDBC (Java
Database Connectivity) serve as APIs for a program to interact
with a database server.
In general, the application must make calls to:
1.connect with the database server
2.send SQL commands to the database server
3.fetch tuples of result one-by-one into program variables
Dynamic SQL: JDBC and ODBC
58. To access SQL from other programming languages, we can use:
59.
60. ODBC
ODBC works with C, C++, C# and Visual Basic (other APIs such as
ADO.NET sit on top of ODBC).
ODBC is the standard for application programs communicating
with a database server.
The API will:
1.open a connection with a database
2.send queries and updates
3.get back results
ODBC can be used with applications such as GUIs, spreadsheets etc.
61. JDBC
JDBC works with Java.
Along with supporting various features for querying and updating
data, and for retrieving query results, JDBC also supports metadata
retrieval i.e. retrieving information about the database such as
relations present in the database and the names and types of relation
attributes.
JDBC connects with the database as follows:
1.open a connection
2.create a “Statement” object
3.execute queries using the Statement object to send queries and
fetch results
4.exception mechanism to handle errors
62. Embedded SQL
Embedded SQL refers to embedding SQL queries in another
language.
SQL can be embedded in various languages including C, Java
and Cobol.
A language into which SQL queries are embedded is referred
to as a host language, and the SQL structures permitted in the
host language comprise embedded SQL.
The EXEC SQL statement is used to identify embedded SQL
request to the preprocessor:
EXEC SQL <embedded SQL statement> END_EXEC
63. PHP
PHP is a server-side scripting language.
It was mainly developed for the web but can also be used as
a general-purpose programming language.
PHP works well with MySQL and can be used in combination
with HTML to create a webapp that connects to a database.
64. PHP
Working:
1.web browser sends HTTP requests and receives HTTP
responses
2.PHP script (on the server-side) connects to DBMS and uses
query results to produce its output
3.web server calls the PHP script and incorporates its output
into the response
4.web browser renders the HTML document from the response
65. PHP
Executing SQL from PHP:
1.connect to server (mysql_connect)
2.select the database (mysql_select_db)
3.run query
4.retrieve row of results (mysql_fetch_array)
5.retrieve attributes (foreach)