Unit 1

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 65

INTRODUCTION

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
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.
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.
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
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)
Applications
 Banking

 Universities

 Reservation Systems

 Telecommunication

 Library Management System

 Social Media Sites

 Online Shopping
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
Purpose: Store the data in Efficient way so that
 Storage retrieval can be made easy

 Security Securing the data or privacy

 Flexibility Small or Huge data

 Reliability Data stored should not be changed


Views of Data
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
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)
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.


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.
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.
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.
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
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.
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
Database Languages
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)
Insert the Sub Title of Your Presentation
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
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
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.
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.


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
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
Database Architecture
Relational DBMS

 Collection of tables having unique names.

RollNo Name Phone


001 AAA 111
002 BBB 222
003 CCC 333
Terms:

• Relation / tables • Cardinality


• Tuple / record / row • Relational operations
• Field / column / • Normalization
attributes
• Domain
• key
• Degree
 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
 Tuple or record or row
 Each row of a table is known as record
 a set of related data

004 DDD 67 444


 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
 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)
 Degree:
Total number of columns present in the relational
database
 Cardinality:
Total number of rows present in the relational
database
Emp# Job Name Salary
E10 Sales 12500
E12 Null 25000

In the above table, degree is 3 and cardinality is 3.


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
Relational Algebra

Operations:
 Selection
 projection
 Cartesian product
 Set Operations
 Join
 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
σage>15(Student)
2 BBB 18
3 CCC 16
Instructor
Projection:
 to project only a certain set of attributes of a relation.
 remove duplicate data from the columns.
 Syntax: ΠA1,A2,…(r)
Cartesian product:
 to combine data from two different relations into one and fetch data
from the combined relation
Syntax: A x B
Table A1
A1 x B1
A B
A B C D E
1 1
1 1 1 2 7
2 2
1 1 2 6 8
Table B1 1 1 5 7 9
C D E 2 2 1 2 7
1 2 7 2 2 2 6 8
2 6 8 2 2 5 7 9
5 7 9
Set Operations

Union

Intersection

Set difference
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
Table A1

A B
1 1 A1 ᴜ B1
A B
1 2
1 1
2 1
1 2
Table B1 2 1
2 3
A B

1 2

2 3
Intersection:
 to fetch data from both tables which is common in both
the tables.
Syntax: A ∩ B
Table A1
Name id A1 ∩ B1
aaa 1 Name
bbb 2 aaa
ccc 1 ccc
Table B1
Name course
aaa C
ccc C++
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 A1 - B1
A B A B
1 1 1 1
1 2 2 1
2 1

Table B1
A B
1 2
2 3
Rename Operation:
 to rename the output relation for any query relation which returns the
result.

 Syntax: ρ (RelationNew, RelationOld)

Table A1 Table B1
A B A B
1 1 ρ (A1,B1) 1 1
2 2 2 2
Accessing SQL From a Programming Language
Accessing SQL From a Programming Language

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

To access SQL from other programming languages, we can use:

 Dynamic SQL: JDBC and ODBC

 Embedded SQL

 PHP
Dynamic SQL: JDBC and ODBC

 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


To access SQL from other programming languages, we can use:
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.


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


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


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)

You might also like