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

DBMS Notes For RPSC Programmer

DBMS Notes for RPSC programmer Exam
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)
60 views

DBMS Notes For RPSC Programmer

DBMS Notes for RPSC programmer Exam
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/ 191

Sunil Yadav Sir - 9694473047

 DBMS (Database Management System)


 What is Data- Row Fact & Figures (Meaningless, unordered data)
 Data Types- Text, Numbers, Image, Audio, Video Etc. यानी की Unorganised Data

Ex. Sunil, Jaipur, 26, PhD

 Information- Processed Data/Organized Data


Data को प्रोसेस करने के बाद हमें Information प्राप्त होती है |

Name Add. Age Qualification


Sunil Jaipur 26 PhD

 Knowledge- After Processed Information we are getting Knowledge.


 Database- Collection of inter-related Data is called database.
Inter-related Data के समू ह को डे टाबेस कहते है |

Ex. Sets of Student Information (Student की Information का समू ह एक डे टाबे स हो सकता है )

S. id Name Age Qualification


S1 Sunil 28 PhD
S2 Harish 27 BSc.
S3 Rahul 26 BSc.

 DBMS- (Database Management System)


Sets of program (Software) used to perform operation on database file in efficient way.

DBMS एक प्रोग्राम का समू ह (सॉफ्टवेयर) होता है , जजसका उपयोग डे टाबेस फाइल पर कुशल (efficient) तरीके से
Operation perform करने के जलए जकया जाता है |

Operation perform जै से- Insert, update, Delete

Database System

Database DBMS
Collection of Inter-related data Software used to manage DB file
(DB File को मै नेज करने का सॉफ्टवेर

Structure Un-structure होता है )

RDBMS (Table) No-SQL Oracle

My SQL

SQL Server

DB 2
DBMS- DBMS is a Application Software.

User (यूजर के पास अपना एप्लीकेशन प्रोग्राम होता है )

Software DBMS Interface (between user and DB file) यह डे टाबेस तथा यूजर के बीच Interface का
काम करता है |

DB File

DBMS is a interface between user and DB file (Database File) which is convert user request in database
query and send to DB File, and from DB it fetch the request and again provide to result to the user.

DBMS यूजर और DB File के बीच का interface होता है, जो की यूजर की request को Database query में बदल करके
DB File तक पहुँ चाता है , और DB File से request fetch करके वापस यूजर को result प्रदान करता है |

 Flat file system/file system


DBMS के पहले File System का उपयोग जकया जाता था|
 File System manage by operating system.
फाइल जसस्टम को ऑपरे जटिं ग जसस्टम मैनेज करता है |
Ex. File System Technology
FAT (File Allocation Table)
NTFS (New Technology File System)
 File System is best for managing small size file (Like- KB, MB)
फाइल जसस्टम छोटी साइज़ की फाइल के जलए अच्छा होता है | (जै से- KB, MB)
 File System Fail to manage large size file (GB, TB)
फाइल जसस्टम द्वारा बड़ी साइज़ (large size) की फाइल को मै नेज करना कजिन होता है | (जै से- GB, TB)

 File System Limitations/Problems (फाइल सिस्टम की प्रोब्लम्स)


 Data redundancy (Data Duplication)
 Data Inconsistency
 Difficult to access required data
 Concurrency problem
 Integrity problem
 Different level access problem
 Data atomicity problem
 Security problem

Note- यह सभी File System की प्रॉब्लम है | इसको DBMS के द्वारा कैसे Solve जकया जाता है | यह हम दे खगें -

Sunil Yadav Sir - 9694473047


 File System v/s DBMS
 Data redundancy

Admission Finance Department Course

Name Father Name Add. Name Father Name Course Fee Name Course Duration

Sunil Dilip Jaipur Sunil Dilip - - Sunil - -

 Data Redundancy/Duplication/Repetition
 File System manage more than one file then some common data (Name, Father Name, and Address)
stored more than one file.

फाइल जसस्टम में data को जब हम एक से ज्यादा यानी Different-Different फाइल में मै नेज करते है , तो कुछ कॉमन
डाटा (Name, Father Name, तथा Address) को सभी फाइल में store करना पड़ता है | जजससे की Same Data
Multiple जगह पर, Duplicate या Redundant हो जाता है | इसे ही हम Data redundancy कहते है |

 Data redundancy के कारण Hard disk Cost बढ़ जाती है |

 Data Inconsistency
 Data redundancy के कारण ही Data Inconsistency की Problem Create होती है |

 Data redundancy के कारण जब Same Data Multiple फाइल में store होता है , तो एक फाइल में डे टा Change
करने से उसका Effect दू सरी फाइल में नही होता है , जजसके कारण Same Data Multiple फाइल में अलग-अलग हो
जाता है | जजसे Data Inconsistencyकहते है |

अथाा त् File System में जब हम जकसी एक फाइल में डे टा को Update करते है , तो उसका Effect दू सरी फाइल पर
नही आता यानी की Update नही होता, जजससे की Data Inconsistency होता है |

 Solution- DBMS Provide minimum redundancy.


DBMS कम से कम redundancy Provide करता है , जजससे Inconsistency की problem नही होती है |

 Difficult to Access Required Data


आवश्यक डे टा को access करना कजिन होता है |

 Physical Information need-


 Storage details need (physical details) (हमें Storage details की जरुरत पड़ती है )
 File format (फाइल का फॉमे ट पता होना चाजहए)
 Required software (सॉफ्टवेयर की जरुरत पड़ती है | जैसे- Excel)
 Location of files (फाइल की लोकेशन की जरुरत पड़ती है )

 DBMS Solution- Easy to access data

डे टाबेस में फाइल को access करना आसान होता है |

Sunil Yadav Sir - 9694473047


यह सभी Internal Task
DBMS Perform करता है |

Conceptual level अथाा त् यूजर को जसफा फाइल का नाम पता होना चाजहए| उसके Column तथा Row का नाम जजसे
access करना है |

Database में user को नही पता की उसका डाटा कहा Store है | जै से- फेसबुक का डाटा हमें नही पता जसफा हम उसमे
User id तथा Password डालकर चलाते है |

DBMS में user जबना Detail को जाने डाटा को Access कर सकता है | यूजर को जसफा यह Information होनी चाजहए,
जजसकी उसे Requirement है | (Table, Column, Row)

DBMS के द्वारा Data को Access करने के जलए यूजर, required Data को fill करता है | उसके बाद बाकी सभी प्रोसेस
(Internal Process) DBMS के द्वारा जकया जाता है |

 File system having more I/O cost (input/output cost)


फाइल जसस्टम में I/O cost ज्यादा होती है |

 Number of secondary memory block transfer secondary memory to main memory in order to access
required data is called I/O cost.

डे टा को access करने के जलए secondary memory के ब्लॉक को main memory (RAM) में ले कर आना I/O cost
कहलाता है |

 DBMS Solution- Indexing

I/O cost का Solution DBMS Indexing के द्वारा प्रदान करता है |

Sunil Yadav Sir - 9694473047


Index Size 30

E. id Name City E1 B1
E1 E2
E2 Block 1
E3
E3
E4 B2
E4 Main memory CPU
E5
E5 Block 2
E6 , B3
, ,
, E1000
, Block 3
,
E1000
Ex. File size 1 GB

Retrive E.id 4 अथाा त् हम block 2 को ही RAM में ले कर जायेंगे|

- Ex. N file= 1000 Page


- I/O cost without index- 1000 page
Index entry- 30 page
- I/O cost with index- 30+1=31 (30 page load index and 1 page load data)
30 page तो index के तथा 1 page File (E.4) ले कर आये तो, I/O cost तो 31 होगी|

 Less degree of concurrency using file system


Concurrent Execution-
Simultaneously (Parallel) Execution two or more transaction (process) is called concurrent execution.

दो या दो से अजधक transaction (process) का Simultaneously (Parallel) execution Concurrent execution


कहलाता है |

Train no. (08081)

Seat no. Name City


S1 - - ROW Lock (DBMS)
S2 - -
S3 - -
S4 - -

User-1 update seat no. S1 (User-1 seat S1को book कर रहे है )


Note- file system not allow this (फाइल जसस्टम यह allow नही करता है )
File system में concurrency Control file level या resource level पर होती है | अथाा त् पूरी फाइल एक resource बन
जाती है | उसको एक टाइम में एक ही यूजर access कर सकता है |

 DBMS Solution- Row/record level locking


File system में concurrency Control, file level या resource level पर होती है | अथाा त् File system जब कोई एक
यूजर जकसी Particular file पर काम करता है , तो उसी टाइम (same time) पर उस फाइल पर दु सरा यूजर काम नही कर
सकता|

DBMS में concurrency Control, Row Level अथवा record level पर होता है , अथाा त् जब कोई यूजर जकसी Particular
record को update करता है , तो DBMS उस Particular Row को ही lock करता है | बाकी सभी Row पर दू सरा यूजर
काम कर सकता है |

नोट:- यजद हमारे फाइल में “N” record है , तो “N” यूजर काम कर सकते है |

 Different level of access control


Difficult to implement different level access.
Different level पर access (किंटर ोल) दे ना कजिन होता है |

 File system different level access need to create physically separate file.

File system में different level पर access दे ने के जलए हमें physically separate file create करनी पड़ती है |

 DBMS Solution- View Table/Virtual Table

DBMS में different level पर access दे ने के जलए हम अलग-अलग (different- different) View create करते है ,
अथाा त् Virtual Table create करते है |

Virtual Table- Virtual Table जदखने में same table जै सी होती है , ले जकन उसका physically Existence नही होता
है |

Virtual Table master table में operation perform करती है अथाा त् Virtual Table में जो Update करते है , वो
master table में भी Update होता है |

जैिे- बैंक में मे नेजर को पूरा access होता है , जबजक clerk को Limited access होते है |

इसमें Admission department को यह तीन ही कॉलम show होिंगे| अथाा त् जजसको जो permission दी जाती है वो ही
show होता है |

Sunil Yadav Sir - 9694473047


 Data Integrity Problem
 Data value stored in database always satisfied some consistency “constraints” (rules)

Data insert करते समय हमें rules (जनयम) का ध्यान रखना पड़ता है |

File system में हम rules को Implement नही कर सकते है | जबजक हम DBMS में Implement कर सकते है |

जैिे-

Salary will never be negative (सैलरी कभी भी negative नही होगी)

Age will be always positive integer (Age हमे शा सकारात्मक पूणाां क होनी चाजहए)

Name always in character string (नाम हमे शा character string में होना चाजहए)

 Atomicity Problem
Transaction

A/C A A/C B
A= 5000 withdrawal from B= 5000 Deposit

Create “failure” problem

Transaction failure हो जाने पर दु बारा शु रू से Transaction perform जकया जाएगा|

Atomicity यह कहती है की या तो Transaction पूरा perform होना चाजहए या कोई भी perform नही होना चाजहए|

Failure की स्थथजत में Transaction revert करना पड़ता है | यह File system में possible नही है |

 Isolation Problem

 Security Problem
File system में हम अलग-अलग यूजर को अलग-अलग Access नही दे सकते| अथाा त् हम एक यूजर को File का Access
नही दे सकते पूरी File का Access दे ना पड़ता है |

Sunil Yadav Sir - 9694473047


 Representation of Data
Bit 0, 1 (Binary/smallest unit)

Nibble 4 bit

Byte 8 bit Use for Memory Use for Networking Communication

KB 1024 byte 210 103

MB 1024 KB 220 106

GB 1024 MB 230 109

TB 1024 GB 240 1012

1. Character (byte)- Collection of bits

Ex. A a b
ASCII- 65 97 98

2. Field- Collection of Character


Single Piece of Information/यानी किसी डे टाबेस टे बल में Fields िो Columns में दर्ाा या जाता है | साधारण र्ब्ोों में
िहे तो आप टे बल िे िॉलम िो फील्ड िह सिते है |

Ex. टे बल में Name, Address और Age 3 Fields है |

Name Address Age


Sunil Jaipur 28

3. Record- Collection of Fields


Complete set of Fields/किसी भी टे बल िे Rows िो हम Records िह सिते है | यानी किसी भी व्यक्ति या वस्तु िी
सारी इनफामेर्न ररिॉडा िहलाती है |

Name Address Mobile


Sunil Jaipur 12345… Record

4. File- Collection of Records (इसिो Table/Relation भी िहते है )

Name Address Mobile


Sunil Jaipur 12345…
Anil Alwar 67890..
Harish Alwar 999999

5. Database- Collection of files/tables

Bit< Character (byte) < Field< Record< File< Database

Smallest to largest unit

Sunil Yadav Sir - 9694473047


 Database Architecture (3-Schema Architecture)
 1 tier architecture (only user interface होता है )
 2 tier architecture
 3 tier architecture (today mostly web application use)

 2 tier (level) Architecture (Client/Server Architecture)


Client िे पास अपना
Client Client Client
application program
होता है |

DB File

Database Server

 Problem 2 tier architecture


 Less Security.

इसमें Security िम होती है | क्योकि Client सीधा Database से Connect रहता है |

 Fail if no. of user is more.

No. of user ज्यादा हो जाने पर यह फ़ैल हो जाता है |

 2 tier architecture में client, Database Server से Directly Connect रहता है |

 2 tier architecture client request Directly Database Server process. Database Server performs all
operation and provides data according to user request.

2 tier architecture िे client िी request िो Directly Database Server िे द्वारा प्रोसेस किया जाता है , कजससे िी
Database Server सभी Operation िो Perform िरता है और यूजर िी request िे अनु सार data provide िरता
है |

 DML Complier, Query िो Low level language में Convert िरता है | क्योकि यूजर िी प्रोसेस High level
language में होती है |

 Client िी request िो Database Server पहले प्रोसेस िरता है , और कफर उसे Low Level Language में Convert
िरिे DB File में Operation िो Perform िरता है | इसकलए वो ज्यादा यूजर बढ़ने पर फ़ैल या हैं ग हो जाता है |
नोट- 2 tier architecture िो Client/Server architecture भी िहते है |

Sunil Yadav Sir - 9694473047


 3 tier Architecture

Client Client Client Client िे पास अपना


application program
होता है |
Application Server
Business layer

DB File

Database Server

 In 3 tier architecture Client request process by application server (business layer) and convert low
level language (machine language) database server perform operation.

3 tier architecture में Client िी request िो application server या business layer प्रोसेस िरता है , और
application server request िो low level language (मर्ीनरी भाषा) Convert िरिे Database Server पर
operation perform किया जाता है |

 3 tier architecture में यूजर (Client) directly Database से Connect नही रहता है |

 3 tier architecture provide more security because Client cannot access directly to database file.
3 tier architecture ज्यादा security प्रदान िरता है , क्योकि Client direct database file िो access नही िर
सिता|

 Mostly web application 3 tier architecture िो support िरती है | यानी िी real life में 3 tier architecture िा
use किया जाता है |

Ex. Facebook, Google, instagram

Sunil Yadav Sir - 9694473047


 3-Schema Architecture/3-Level Abstraction
 Schema- Overall design of database. (definition of table)
Database िा overall logical representation Database Schema िहलाती है |
यानी िी टे बल िी पररभाषा िो या टे बल िे stracture िो स्कीमा िहते है |
Ex. Student (s.id, name, age)

 Instance- The collection of information stored in database at particular point of time is called
instance.

Database में किसी particular point of time पर जो data मौजू द होता है | वह Instance िहलाता है |

 Record sets of database table (snapshot).

 The value of variable in a program at a point of time is called instance.


int a;
a=10
a=20

a=30

इसमें a िी वैल्यू Change हो रही है | यानी िी instance बदलता रहता है |

User User User

External Schema/ External Schema/ External Schema/ What to show user.


View Level View Level View Level इसिा मतलब है िी
यू जर िो क्या कदखाना है |

Conceptual Schema/ What data are stored in database.


Logical Level डे टाबे स में िौनसा डाटा सों गृहीत है | यह
Conceptual Schema बताती है|

Physical Schema/ How data are actual stored in database.


Internal Level डे टाबे स में data वास्तव में िैसे सों गृहीत है | यह
हमें Physical Schema बताती है|

DB File

Database Server

Sunil Yadav Sir - 9694473047


 External Schema/Sub Schema (View Level)
 External Schema से User Connect रहते है |
 External Schema हमें यह बताता है िी हमे यूजर िो क्या कदखाना है |

 External Schema, Conceptual Schema तथा Physical Schema से hidden रहती है | अथाा त् हम यूजर िो यह
नही बताते िी आपिा डाटा िौनसी टे बल में स्टोर है | टे बल िे बीच में Relationship क्या है तथा िौनसी Disk पर
Store है |

 External Schema provides different-different view on different-different user.


External Schema अलग-अलग यूजर िे कलए अलग-अलग view provide िरती है |

जैसे- College में Student िे कलए अलग, Faculty िे कलए अलग तथा Dean/HOD िे कलए अलग View होगा|

 User interface provide to access data.


डाटा िो access िरने िे कलए यूजर interface provide किया जाता है |

 Subset of Conceptual details.


External Schema Conceptual details िी Subset होती है |

 Real data representation to user.


External Schema यूजर िे कलए Real में डाटा िो represent िरती है |

 Conceptual Schema (Logical Level)


 What data are stored in database?
Conceptual Schema हमें यह बताती है िी डे टाबेस में िौनसा डाटा सोंगृहीत (Store) िरना है | यानी table तथा
उनिे बीच िा relationship क्या है | यह define किया जाता है |

Conceptual Schema िे द्वारा हम database design िरते है | यानी ER diagram Conceptual Schema पर िाम
िरता है |

 Relationship among data.


यहााँ पर Structure तैयार (Create) किया जाता है | यानी िी data तथा उनिे बीच relationship define िरती है |
Ex. Student (name, roll no. age)
Faculty (salary, marks)

 Tables Relationship between table

(Entity, attribute or Relationship between entity)

 Constraints on the data.

Data पर रूल (Constraint) लगाया जाता है |


 Physical Schema (Internal level)
 How data are actually stored in database define by physical schema.

Database में वास्तव में DB file (Database file) िैसे store होती है | यह हमें Physical schema बताती है |

 Physical schema िे द्वारा हम actual disk पर िाम िरते है | अथाा त् डे टाबेस में present disk िो manage िरते
है |

 Physical schema िो manage DBA (database administrative) िे द्वारा किया जाता है |


 Physical schema िे द्वारा data dictionary िो maintain किया जाता है |
 Physical schema DBMS िे control में िाम िरती है |
 storage space is allocated file, index
 Record describe for storage.

 Data compression and data encryption technique implement िी जाती है |

Sunil Yadav Sir - 9694473047


 Storage Manager
 Storage manager interface between low level data store and application program.

Storage manager low level data तथा application program िे बीच interface िा िाम िरता है , अथाा त् हम
जो डाटा डे टाबेस में store िरते है | उसे वह low level language में convert िरता है , और actual data file में
जािर store िरता है |

 Storage manager interaction with file manager.

Storage manager फाइल मे नेजर से interaction (पारस्पररि किया) िरता है , क्योकि हमारा डाटा file िे रूप में
store होता है तथा file manager िो Operating system support maintain िरता है |

 Storage manager translate DML statement (SQL query) to low level file statement (File System
Command).

Storage manager DML statement यानी SQL query िो low level file statement (File System Command)
में translate िरता है , अथाा त् जब हम SQL query िो कलखते है तो वह store नही होती है , उसे पहले low level
language में convert किया जाता है |

 Storing, retrieving, updating data in DBMS.


डाटा िो Store िरना, retrieve िरना, update िरना, यह सभी task Storage Manager द्वारा होता है |

 Authorization and Integrity Management


यह authorized तथा integrity manage िरता है | यानी authorise user िो ही access दे ता है तथा सही data ही
insert हो सिे यह check िरता है |

 Transaction Manager (ACID properties)


Concurrent transaction execution performs without data inconsistency.

जब दो या दो से अकधि transaction एि साथ perform होती है , तो यह decide िरता है िी data में किसी भी प्रिार
िी inconsistency नही होनी चाकहए|

 Buffer Management (Temporary Storage)

CPU

RAM

Database

Sunil Yadav Sir - 9694473047


 Data Structure Implementation
1. Data File (Actual File)
2. Data Dictionary (Meta Data) (Meta Data, Data Dictionary में जािर Store होता है )
3. Index (for fast access)

 The Query Processor


 DDL Interpreter- यह data dictionary में data insert िरता है अथाा त् यह structure define िरता है |

 DML Complier- यह DML statement िो queries language में translate िरता है |


यह Queries Optimization perform िरता है |

 Database Users.
There are 4 type of Database users

1. Naive User- Interact database using application program.


यह application program िे द्वारा database से interact िरता है |

यह ज्यादातर DML operation perform िरता है |

जैसे- हम Naive user है | हम application program िे द्वारा interact िरते है |

2. Application Programmer- Write application program.


यह Naive user िे कलए application program write िरिे दे ता है |

3. Sophisticated User (Analyst) - यह analyst होता है | यह application program तथा direct दोनोों िे द्वारा
database से interact िर सिता है |

Interact without application program.

यह कबना application program िे Interact िर सिता है |

यह production team िा कहस्सा होता है |

4. DBA (Database Administrator) - Central control of both data and program to access those data.

किसी यूजर िो क्या कदखाना है | उसिा पूरा control DBA िे पास होता है |

Schema definition (DDL)

Storage structure and access method define.

Physical organization of data and index.

Granting of authorization of data access.

Routing maintains.

Sunil Yadav Sir - 9694473047


 Data Independence
1. Logical Data Independence

2. Physical Data Independence

View Level

Logical data independence

Inconceptual Level

Physical data independence

Physical Level

DB file

Database

1. Logical Data Independence

Whenever conceptual schema change (Add new column, delete old column, change relationship
between tables) then those effect never be on application program. (external schema)

जब भी हम conceptual schema में change िरने , यानी Table में नये column add िरने , किसी पुराने column िो
delete िरने , या table िे बीच में relationship change िरने पर उसिा effect िभी भी application program या
external schema/view level (end user) पर नही होता| कजसे हम Logical data independency िहते है |

जै से- Facebook िभी अपने डे टाबेस में िोई column add िरती है या delete िरती है तो हमारे interface पर िोई
प्रभाव नही पड़ता| इसे ही logical schema िहते है |

2. Physical Data Independence


Physical structure change not effected on conceptual or external schema.

जब हम database में िोई नई disk add िरने या disk िी location change िरने अथाा त् physical structure
change िरने पर उसिा effect िभी भी conceptual schema या external schema/ view level (end user) पर नही
होना चाकहए| कजसे हम physical data independency िहते है |

जै से- Facebook िा data storage fail होने पर वो hard disk या server िो दू सरी जगह कर्फ्ट िरता है , तो हमारे
interface पर िोई effect नही पड़ता है |

Sunil Yadav Sir - 9694473047


 Data Model/Database Model
Data model को database model भी कहते है | यह database की design अर्था त् physical और logical design को
दर्था तथ है |

 Type of Data Model


Hierarchical Data Model
Network Data Model
Relational Data Model (RDBMS)

1. Hierarchical Data Model


Develop by IBM (international business machine) around in 1968.

First commercial database- IMS (Information Management System) इसकथ उपयोग airlines management में
ककयथ गयथ|
Parent/ROOT Node (Level-0)

Child (Level-1)

Child (Level-2)

Explain By Diagram-

 Hierarchical data model को parent-child model भी कहथ जथतथ है |


 Hierarchical data model में data, tree form में represent होतथ है |
 Hierarchical data model में प्रत्येक record को node/segment कहथ जथतथ है |
 Hierarchical data model में एक child दू सरे child की information नही दे ख सकतथ| इसकिए इसे single parent
model भी कहथ जथतथ है |

जै से- E की information D नही दे ख सकतथ यथ उसे नही पतथ होती है | single parent model अर्था त् इसमें child कथ
एक parent होतथ है |

Sunil Yadav Sir - 9694473047


2. Network Data Model
Node/segment
Course

DBMS Java Computer network SAD

Sunil Anil Sanjay Rahul Pawan Ram

 Network model develop by “CODASYL” database task force (group)


इसे CODASYL ग्रुप ने develop ककयथ र्थ|

 IDS (Intergraded Data Stored) यह पहिथ डे टथबेस मॉडि र्थ, जो network model पर आधथररत र्थ, कजसे
“Charles Bach man” ने develop ककयथ र्थ|

 Network Data model भी parent-child relationship को follow करतथ है |


 Network model में एक child के एक से ज्यथदथ parent हो सकते है | इसकिए इसे multiple child-multiple parent
model भी कहथ जथतथ है |

 Network model में child कबनथ parent के भी हो सकतथ है |


 Network model में Data को ग्राफ (Graph) form के द्वथरथ represent ककयथ जथतथ है |

 Relational Data Model (RDBMS)


 Develop by Dr. E.F. Codd in 1970
 इन्होने RDBMS के (0-12) 12 codd rule कदए र्े | (0 इसमें foundation codd rule है )
 इन्हें 1986 में Turing Award कदयथ गयथ र्थ|
Column/Attribute/Field
Student (Table/Relation)

Roll no. Name Age Address


101 Sunil 26 Jaipur Row/Record/Tuple
102 Anil 27 Alwar
103 Rahul 28 Jaipur
104 Pawan 29 Jaipur

 Relational database (RDBMS) is a collection of tables.


Relational database (RDBMS) tables कथ Collection होतथ है |
 Column- को Field, Attribute भी कहते है |
 Row- को Record, Tuple भी कहते है |
 Cardinality- no. of records in table
Table में records की संख्यथ कथकडा कनटी कहिथती है |
 Arity- no. of attribute in table.
Table में attribute/Column की संख्यथ Arity कहिथती है |

Sunil Yadav Sir - 9694473047


Codd Rule 1- data file must be in tabular format.

(Data must be stored as row & column)

डथटथ हमे र्थ table (row/column) की फॉमे ट में store होनथ चथकहए|

Table, row & column से कमिकर बनती है |

Database में Table को relation कहथ जथतथ है |

Column/Attribute/Field तीनों एक ही entity को represent करते है |

Database में present record की entity एक से ज्यथदथ बथर नही होनी चथकहए|

Sunil Yadav Sir - 9694473047


 E-R Model (Entity Relationship Model)
The overall logical represent (structure) of the database expressed in graphically format.
E-R model is a high level data model used to represent database in graphically format.

E-R model develop by “Dr. Peter Chen” in 1976.

 E-R Diagram का उपयोग क्ोों करते है |


 Entity relationship के द्वारा हम database का logical design करते है , जिसमे की database का complete
logical representation होता है |

 E-R Model Schema का सैद्ाां जतक (Theoretical) representation करता है |


 E-R diagram, data model को represent करता है |
 E-R model conceptual design पर आधाररत है |
 E-R diagram Ellipse द्वारा represent जकया िाता है |
 It will be define all entity (table) and their relationship.
यह entity तथा entity के बीच के relationship को define करता है |

 How will be tables are connect to each other (numbers of field in table)
यह बताता है की table एक दु सरे के साथ कैसे connect होती है | table में जकतनी field होगी यह भी बताता है |

 It will describe entity, attribute and relationship.

यह entity, attribute और relationship को describe करता है |

 It is database blue print used to implementing data.


यह डे टाबेस का blue print होता है , जिसका उपयोग data को implement करने के जिए जकया िाता है |

 It is GUI representation database logically structure.


यह GUI (Graphical User Interface) representation database logically structure है |

 E-R Model is based on Three Basic Components.


Entity
Attribute

Relationship

1. Entity
Entity- entity is a real word object or thing (table)

RDBMS- Student (Roll no, Name, Age, Address) (यह पूरा Entity set होता है )

Entity

Table
इसमें Student, Entity है | िो बाद में database में टे बि का नाम बनती है |

Sunil Yadav Sir - 9694473047


2. Attribute
Attribute- Characteristic/properties of entity.

Entity की Characteristic या properties Attribute कहिाती है |

Attribute डे टाबेस में टे बि के कॉिम बनते है |

Student

DOB Mobile

Roll No. Name

3. Relationship
Relationship- Association between entities.

Relationship दो entity के बीच सांबांध (Relation) को बताता है |

Relationship between two entity set called is cardinality.

E-R Model में दो entity set के मध्य सांबांध (Relationship) को काजडि नजिटी कहते है |

Student Study Course

Entity Relationship/ Entity


Association

Sunil Yadav Sir - 9694473047


 Symbol and used to Represent E-R Diagram

S. No. Name Symbol Represent

1 Rectangle Entity

2 Ellipse Attribute

3 Diamond Relationship

Entity to Relationship
4 Lines या
Entity to Attribute

5 Double Ellipse Multivalued Attribute

6 Dashed Ellipse Derived Attribute

7 Double Rectangle Weak Entity

8 Double Diamond Identifying Relationship

9 Ellipse with Line Key Attribute (Primary Key)

Partial Key
10 Ellipse with Dashed Line (Weak entity के जिए बनती है )

11 Double Line Total Participation

12 Ellipse connect with another Ellipse Composite Attribute

 [Derived Attribute] के जिए * + इस bracket (कोष्टक) का प्रयोग जकया िाता है |

 {Multivalued Attribute} के जिए { } इस bracket (कोष्टक) का प्रयोग जकया िाता है |

Sunil Yadav Sir - 9694473047


Roll No. DOB C. id Course Name

Student Study Course

Age Entity Entity


Relationship
Start date

Name
Duration
End date

First Name Mid. Name Last Name

Primary Key Primary Key


Student Course

Roll No. DOB First name Mid. Name Last name C. id Course Start date End date

 Student & Course are entity in database design used for table name.

Student और Course दो Entity है , िो database design के time पर table का नाम बनती है |

 Study relationship is between Student and Course.


Study, relationship है , Student और Course के बीच का|

 Roll no, Name, DOB and Age are attribute of student entity in database design time they will
become table column.

Roll no, Name, DOB और Age Student table के attribute है , िो की database design के time पर table के
कॉिम बनते है |

 First name, mid. Name, & Last name they are Composite Attribute are used to make multiple
columns.

First name, mid. Name, तथा Last name यह सभी Composite Attribute है , िो की टे बि के एक से ज्यादा
कॉिम बनते है |

 Roll no and C. id are Key attributes in database used for primary key in table.
Roll no तथा C. id Key attribute है , िो टे बि में primary key बनते है |

 Age are derived Attribute, and in database design not need to make table column.
Age derived Attribute है , जिसे database design के time पर टे बि के कॉिम बनाने की िरूरत नही होती है |
क्योजक Age at a time derived की िाती है , अथाि त् जकसी Attribute की Help से derive करते है |
 Advanced E-R Diagram Symbols.

Optional One
1

Optional Many
2

Mandatory One
3

Mandatory Many
4

Three type of
One to One Relationship
5

* *
Many to Many
6

Unary Relationship
7

Binary Relationship
8

Ternary Relationship
9

ISA
Specialization/Generalization
10

Sunil Yadav Sir - 9694473047


 Type of Attribute

1. Single v/s Multivalued Attribute

Roll No. Mobile No.

2. Simple v/s Composite Attribute

Street
Name Address

H. No.
Pin Code
District

 Address, टे बि का कॉिम नही बने गा| इसमें sub attribute के Column बनते है |
 Composite attribute further divided into sub attribute.
Composite attribute को आगे sub attribute में divide कर सकते है |
 Need to create multiple tables.
Multiple Table बनाने की िरुरत पड़ सकती है |

3. Stored v/s Derived Attribute

DOB Age

 Derived attribute, table के create के time पर कभी भी table का Column नही बनता है |
 Age will be calculated based on DOB.
Age को DOB के base पर calculate जकया िाएगा|

4. Key attribute v/s Non-Key Attribute

Roll no. Name

 Key attribute, table में record को uniquely identify करने के जिए काम आते है |
 Roll no. always unique for all Students.
 Name can be duplicate.
Name कभी भी duplicate नही होगा|
5. Complex attribute (Composite+ Multivalued)

H. No.
Address

Building No.

Pin code
District

6. Required Attribute (*) या Mandatory

Name*

 Required field value must be compulsory.


Required attribute की field को user application program में compulsory fill करना होता है |

िै से- िब हम form fill करते है , तो star mark (*) वािे Required attribute होते है |

7. Optional Attribute
 Optional field value may or may not field.

Optional field को user application program में fill कर भी सकते है और नही भी कर सकते है |

Sunil Yadav Sir - 9694473047


 Degree of Relationship
Number of entity associated or participated by particular relationship.

एक particular relationship के साथ जितनी entity participate करती है , वह उस relationship का degree of


relationship कहिाता है |

 Degree of Relationship ककतने Type की होती है |


1. Unary or Recursive Relationship (Unary अथाि त् 1 entity)
2. Binary Relationship (Binary अथाि त् 2 entity)
3. Ternary Relationship (Ternary अथाि त् 3 entity)
4. n-ary Relationship (more than 3 entity)

1. Unary या Recursive Relationship

Account

Person Bank

Employee

इसमें यह दर्ाि या है की एक person है , जिसका bank में account है और वह bank का employee भी है |

Same entity participate more than one on single relationship.

2. Binary Relationship

Student Study Course

3. Ternary Relationship

Employee Work Department

Job

Job Timing Job Title


Ex-2

Project

Project id

Name

Instructor Student यह entity का नाम है |

ID Project
Enroll no.
Guide
Name
Name यह सभी Attribute है |
Salary
DOB

4. n-ary Relationship

E5

E4 R E1

E3 E2

When more than three entities participate a particular relationship.


िब जकसी particular relationship के साथ तीन से ज्यादा entity participate करती है , तो उसे n-ary Relationship
कहते है |

Sunil Yadav Sir - 9694473047


 To Enhance the E-R Model we use
E-R Model को Enhance करने के जिए use करें गे|

1. Specialization Based on sub class & super class concept.

2. Generalization Define process of attribute inheritance.

3. Aggregation- (Relationship among Relationship)

 Specialization
 Specialization is a Process used to top-down approach.

Specialization एक प्रोसेस होती है , िो top-down approach का उपयोग करती है |

 Specialization Set of super class and their related sub class.

Specialization Super Class का Set होती है , और उससे related sub class होती है |

Top Super Class


Entity

Sub Class Sub Class


ISA
(Super Class को Sub Class में divide जकया
िाता है )

Sub Entity Sub Entity


Down

 Generalization
 Generalization is a bottom-up approach.

Generalization एक bottom-up approach होती है |

 Sub class are defining first than define super class.

इसमें Sub class को पहिे define जकया िाता है , उसके बाद super class को define जकया िाता है |

 The process to minimise different entity by identified their common characterises.


Common characterises को identify करके Different entity में minimise करता है |
 Specialization & Generalization Common Diagram
Top
Top

Name Mob No. Add.

Person

Generalization
Specialization
Bottom-Top ISA
Salary Top-Down
Approach Course Approach
follow करता है |
follow करता
है |
Employee Student

Student Type
ISA

Part Time Full Time

Time Slot Duration


Bottom
Down

 कुल ककतने Attribute होोंगे|

Part Time- के 5 Attribute है | (Time Slot, Salary, Name, Mobile, Address)

Full Time- के 5 Attribute है | (Duration, Salary, Name, Mobile, Address)

Student- के 5 Attribute है | (Course, Type, Name, Mobile, Address)

Employee- के 4 Attribute है | (Salary, Name, Mobile, Address)

Sunil Yadav Sir - 9694473047


 Aggregation
(Relationship among Relationship)

Job

Employee Work Branch

Manager

(Redundant Relationship among Relationship


Relationship)
Manager

(Process define without aggregation)

We need to Assign Employee, Branch, Job all are a Manager.


हम Employee, Branch, Job सभी को Manager Assign कर रहे है |

जबना aggregation के process define करने से Redundant Relationship की problem create होती है |

जैसे- Employee, Branch, Job सभी की दो-दो Relationship है , Work से तथा Manager से Redundant
Relationship की problem create होती है |

 Solution E-R Diagram with Aggregation


Redundant Relationship का solution हम Aggregation से करते है |

Job

Employee Work Branch

(Relationship among (Abstraction) इसका मतिब इसमें


Relationship) Manage Complexity को Hide जकया िाता है |

Manager
 Weak Entity-

Session
Partial Key Time
Room No.

Student Enroll Course Teach Student

Weak ISA
Total
Entity Offered
Participation

Semester Fee

Course

C. id. C. name

 Weak entities are those entities having not a key (Primary Key) Attribute.

Weak entity वह entity होती है | जिसमे Key Attribute (Primary Key) मौिू द नही होता है |

यानी की वह entity जिसमे हम एक primary key नही बना सकते या नही बन सकती| उस entity को Weak entity
कहते है | इसमें हम partial key बनाते है | िो temporary होती है |

 Weak entity not present in Real database.


Real database में Weak entity मौिू द नही होती है |

 Weak entity always associate relationship with total participation


Weak entity हमे र्ा total participation के साथ relationship स्थाजपत करती है | यानी की िब Weak entity के
साथ िो भी Relationship बनता है , तो वह Total participation के साथ बनाता है |

 Weak entity having partial key attribute.

Weak entity partial key attribute को रख सकती है |

Sunil Yadav Sir - 9694473047


 Keys in DBMS
Key
Student

Roll no. Name Age City


101 Sunil 27 Jaipur
102 Anil 28 Jaipur
103 Sunil 27 Jaipur
104 Rahul 25 Jaipur

Entity set- Student (roll no, name, age, city)

 What is key
 Attribute or set of attribute used to identify table records uniquely.

Key एक Attribute या एक से ज्यादा Attribute का समू ह होती है , जो की table में मौजू द record को uniquely
identify करती है |

 Key एक से ज्यादा Attribute से ममलकर भी बन सकती है |

Ex.
Key
Course

S. id C. id Fee
S1 C1 6000
S1 C2 5000
S1 C3 5000
S2 C1 6000
S3 C2 5000

Entity set- Course (S.id, C. id, fee)

Keys are collection of S. id+C. id Attribute.

 Use of key
 Key is used to uniquely identify table record.
Key table के record को uniquely identify करने के काम आती है |

 It is also use to establish or identified relationship between tables.

इसका उपयोग Table के बीच relationship बनाने के मलए भी मकया जाता है | जै से- Foreign key
1. Candidate Keys
Minimum set of attribute used to differentiate record of the table uniquely.

Candidate key Minimum attribute set के Combination से ममलकर बनती है , जोों की table के record को
uniquely identify करती है |

Student

S. id S. Name Age
S1 Sunil 27
S2 Anil 28
S3 Rahul 29
S4 Sunil 27
S5 Sanu 25

Entity set- Student (S. id, S. Name, Age)

Candidate Key- S. id (क्योमक इसमें अकेला S. id ही uniquely identify कर रहा है )

Employee

E. id E. Name City Passport No. Aadhar No. PAN No.


E1 A Jaipur Pass-1 1010 P-1
E2 B Jaipur Null 1011 P-2
E3 A Jaipur Pass-3 Null P-3
E4 B Kota Null 1013 P-4
E5 C Alwar Pass-5 1014 Null
E6 D Jaipur Pass-6 Null P-5
E7 F Kota Pass-7 1016 Null

इसमें E. Name तथा City Candidate key नही बन सकता है , क्योमक Name तथा City मकसी का same भी हो सकता है |
इसमलए इसमें बामक सभी Candidate key बन सकते है | जै से-

Candidate Key- (E. id, Passport No, Aadhar No, PAN No) यह सभी unique होते है तथा null value भी unique
होती है |

 Constraint of Candidate key (Rules)


 Minimum attribute sets to uniquely identified table record.
Candidate key Minimum attribute का समू ह होती है , जो टे बल के ररकॉर्ड को uniquely identified करती है |

 Single table may have more than one Candidate key.


एक टे बल में एक से ज्यादा Candidate key हो सकती है |

 Candidate key field value can be null.

Candidate key की field value Null हो सकती है |

Sunil Yadav Sir - 9694473047


 Null
 Null is not a zero (0) or blank space.
Null न तो zero (0) होता है , न ही blank space होता है |
 Null is a special value assign by DBMS.
Null र्े टाबेस की एक special value होती है | मजसे DBMS द्वारा assign मकया जाता है |
 Two null value always unique (Different) in a table.
एक Table में दो null value हमे शा unique (अलग-अलग) होती है |
 Null is collection of ASCII character strings automatically assign by DBMS.
Null ASCII character string का collection होती है | मजसे DBMS द्वारा automatically assign मकया जाता है |

2. Primary Key (Unique+Not Null)


 Primary key One of the candidate key, whole field values must be not null.

Primary key एक candidate key होती है , मजसकी field value कभी भी null नही होती है |

Employee

E. id E. Name City Passport No. Aadhar No. PAN No.


E1 A Jaipur Pass-1 1010 P-1
E2 B Jaipur Null 1011 P-2
E3 A Jaipur Pass-3 Null P-3
E4 B Kota Null 1013 P-4
E5 C Alwar Pass-5 1014 Null
E6 D Jaipur Pass-6 Null P-5
E7 F Kota Pass-7 1016 Null
Primary key- E. id

 Constraint of Primary key (Rules)


 All constraint of candidate key.
Candidate key के सभी constraint (rules) primary key पर apply होते है |
 Null value not allowed in primary key.
Primary key में Null value allow नही होती है | अथाड त् field value Not Null होनी चामहए|
 At most one Primary key on single table.
एक single table में ज्यादा से ज्यादा एक Primary key हो सकती है |
 Generally Primary key value assign to user, when table record created.
सामान्यत: Primary key की value यूजर को assign की जाती है | जब टे बल में नया ररकॉर्ड create मकया जाता है |

जैसे- Enrolment No. (University), Account No. (Bank)


 Generally primary key value cannot be change.
ज्यादातर primary key की value change नही कर सकते|
3. Alternate Key (Secondary key)
 Except primary key all candidate key are alternative key or secondary key.

Primary Key को छोड़कर बामक सभी Candidate Key ही Alternate Key या Secondary Key कहलाती है |

Employee

E. id E. Name City Passport No. Aadhar No. PAN No.


E1 A Jaipur Pass-1 1010 P-1
E2 B Jaipur Null 1011 P-2
E3 A Jaipur Pass-3 Null P-3
E4 B Kota Null 1013 P-4
E5 C Alwar Pass-5 1014 Null
E6 D Jaipur Pass-6 Null P-5
E7 F Kota Pass-7 1016 Null
Primary key- E. id

Candidate Key- (E. id, Passport No, Aadhar No, PAN No)

Alternate Key- (Passport No, Aadhar No, PAN No)

 Constraint of Alternate key (Rules)


 All constraint of candidate key.
Candidate key के सभी constraint (rules) primary key पर apply होते है |
 Null value allowed in alternate key.
Alternate key में Null value allow होती है |
 More than one alternate key possible on single keys.
एक single table में एक से ज्यादा alternate key हो सकती है |
Candidate Key- Employee (E. id, Passport No, Aadhar No, PAN No)

Primary Key Alternate key (Secondary Key)

Sunil Yadav Sir - 9694473047


 Prime Attribute/Key Attribute
 All attribute which belong to any candidate key of a table is called prime attribute.
वो सभी attribute जो table के मकसी candidate key से belong करते है | prime attribute कहलाते है |
 Prime attribute also called key attribute.

Prime attribute को key attribute भी कहा जाता है |

Ex. R (A, B, C, D, E)

Candidate Key- A, CD, E

Prime Attribute- (A, C, D, E)

Ex. Employee (E. id, E. Name, City, Passport No, Aadhar No, PAN No)

Candidate Key- (E. id, Passport No, Aadhar No, PAN No)
Prime Attribute- (E. id, Passport No, Aadhar No, PAN No)

 Non-Prime Attribute/Non-Key Attribute


 All attribute not belong to any of the candidate key of a table is called non-prime attribute.

वो सभी attribute जो table के मकसी candidate key से belong नही करते है | non-prime attribute कहलाते है |

Ex. R (A, B, C, D, E)

Candidate Key- A, CD, E

Non-Prime Attribute- (B)

Ex. Employee (E. id, E. Name, City, Passport No, Aadhar No, PAN No)

Candidate Key- (E. id, Passport No, Aadhar No, PAN No)
Non-Prime Attribute- (E. Name, City)

 Simple Candidate Key


वह candidate key जो single attribute से ममलकर बनती है | simple candidate key कहलाती है |

Ex. R (A, B, C, D, E)

Candidate key- A, CD, E

Simple candidate key- (A, E)

Ex. Employee (E. id, E. Name, City, Passport No, Aadhar No, PAN No)

Candidate key- (E. id, Passport No, Aadhar No, PAN No)
Simple candidate key- (E. id, Passport No, Aadhar No, PAN No)
 Compound/Composite Candidate Key
वह candidate key जो दो या दो से ज्यादा attribute से ममलकर बनती है | compound candidate key कहलाती है |

Course

S. id Course id Fee
S1 C1 6000
S2 C2 5000
S3 C3 5000
S4 C4 6000
S5 C5 5000
Course (S.id, C. id, fee)
Candidate key- (S. id+C. id)

Compound candidate key- (S. id+C. id)

Ex. R (A, B, C, D, E, F)

Candidate key- A, B, CD
Prime Attribute- (A, B, C, D) Simple Candidate Key- (A, B)
Non-Prime Attribute- (E, F) Compound candidate key- (C, D)

4. Super Key
Student

S. id S. Name Age Super Key


S1 Sunil 27
S2 Anil 30  S. id
S3 Rahul 29  S. id+S. Name
S4 Sunil 27  S. id+Age
S5 Sanu 27  S. id+ S. Name+Age

 Super key sets of attributes used to differentiate table records uniquely.


Super key attribute का समू ह होता है , जो की table के record को uniquely identify करता है |
 Super key attribute sets having minimum one candidate key.
Super key attribute के set में कम से कम एक candidate key होती है | जै से- S. id

 Super key में Minimum attribute होने की कोई गारों टी नही होती है | अथाड त् मकतने भी attribute ममलकर Super
key बन सकते है |

 Minimum super key, candidate key कहलाती है |

 Primary key एक Candidate Key भी होती है |


 Candidate key एक Super key भी होती है |
Primary Key
 Super key ⊇ Candidate key ⊇ Primary key

Candidate Key अथाड त् सबसे बड़ा super set Super key का, मिर

Candidate key का, मिर Primary key का होता है |


Super Key
 Find out Super Key
Ex.
R (A, B, C, D)
Candidate key- A
Candidate Key= A को छोड़कर बाकी 3
No. of super key- A, AB, AC, AD
और attribute है , तो इसमें 23= 8 super key
ABC, ABD, ACD, ABCD
बने गी

(इसमें A Candidate key है , जो सभी super key का part बने गी)

Formula- 23= 8 Super Key

Ex.
R (A, B, C, D)
Candidate key- A, B

No. of super key- A B


AB BA
AC BC
AD BD
ABC BAC
ABD BCD
ACD BAD
ABCD BACD
A= Candidate key है , तो 3 और attribute है = 23

Common keys- AB BA B= Candidate key है , तो 3 शे ष attribute= 23

ABC BAC AB= Candidate key है , तो Common attribute= 22


ABD BAD
23+23-2= 8+8-4=12 super key बने गी
ABCD BACD
No. of non-prime attributes
Formula no. 2- (No. of super key using prime attribute) ×2

R (A, B, C, D)
Candidate key- A, B
Super key- A, B, AB (3 super key बने गी)
=3×22

=3×4=12 super key


A= Candidate key है , तो 4 और attribute है = 24
Ex.
R (A, BC, D, E) BC= Candidate key है , तो 3 शे ष attribute= 23
Candidate key- A, BC
ABC= Candidate key है , तो Common attribute= 22
No. of super key- 24+23-22
24+23-2= 8+8-4=20 super key बने गी
16+8-4=20

Sunil Yadav Sir - 9694473047


Solve by Formula no. 2-

R (A, BC, D, E)
Candidate Key- A, BC

Prime Attribute से Super Key- 5

A
BC
ABC
AB
AC

Non-Prime Attribute से Super Key- 2

= 5×22= 20 super key

 Formula definition

R (A1, A2, A3, A4……………….An)

 If candidate key A1= No. of super key


=2n-1

 If candidate key A1, A2= No. of super key


=2n-1+2n-1-2n-2

 If candidate key A1A2= No. of super key


=2n-2

 If candidate key A1A2A3……….An= No. of super key


=1
नोट:- यमद सारे के सारे attribute ममलकर 1 Candidate key है , तो No. of super key- 1 होगी|

Ex. R (A, B, C, D)

Candidate key- A

Formula- 2n-1
4-1
=2

=23= 8 super key

Ex. R (A, B, C, D)

Candidate key- A, B

Formula- 2n-1+2n-1-2n-2
4-1 4-1 4-2
2 +2 -2
3 3 2
=2 +2 -2 =8+8-4= 12 super key
5. Foreign key (Referential Key)
 Set of attribute used to reference primary key on same table and another table.
Foreign key, attribute का समू ह होता है , जो एक टे बल या दू सरी टे बल पर primary key को reference करने के
मलए उपयोग में ली जाती है |

 Foreign Key का उपयोग Table के बीच में Relationship बनाने के मलए मकया जाता है | जब मकसी एक टे बल की
Primary Key को मकसी दु सरे टे बल में Primary Key के रूप मे प्रयोग करते है | तो उसे Foreign Key कहते है |

 Foreign key स्वयों की table से भी reference कर सकती है |

 Foreign key में null value हो सकती है , तथा foreign key में value एक से ज्यादा बार repeat हो सकती है , अथाड त्
foreign key में duplicate value allow होती है |

 यह Parent-Child Relationship को follow करती है |


 Null value, foreign key parent table के मकसी भी record को refer नही करती है |
 Foreign key तथा primary key दोनोों table का नाम same होना जरुरी नही है , अलग भी हो सकता है |

 Referential Integrity Constraint


Primary key Foreign key
Student Course

S. id Name Age City C. id C. Name S. id


S1 Sunil 27 Jaipur C1 DBMS S1
S2 Harish 28 Jaipur C2 C.N. S1
S3 Sanu 29 Jaipur C1 DBMS S2
S4 Rahul 30 Alwar C3 OS Null
S5 Hement 27 Alwar C4 DSA S10
Referenced Table (Parent Table) Referencing Table (Child Table)

मजस टे बल में Primary key मौजू द होती है , उसे Referenced table (parent table) कहते है , तथा मजस टे बल में
foreign key होती है | उसे referencing table (child table) कहते है |

 Constraint of Foreign key (Rules)


 Foreign Key used to establishment relationship between tables.
Foreign Key table के बीच relationship स्थामपत करती है |
 Foreign Key can have duplicate (repeated) value.
Foreign Key की field value duplicate (repeated) हो सकती है |
 Foreign Key can have null value.
Foreign Key की field value null हो सकती है |
 Null Foreign Key value cannot reference any primary key records.
Null Foreign Key value primary key के मकसी भी record को reference नही करती है |
 Foreign Key can have field value those available on primary key.

Foreign Key की field value हमे शा primary key से belong करनी चामहए| यानी foreign key में वही value होनी
चामहए, जो primary key में मोजू द हो|

Sunil Yadav Sir - 9694473047


 Referential Integrity Constraints
Primary key Foreign key
Student Course

Roll. No. Name Age C. id C. Name Roll. No.


101 A 20 C1 DBMS 101
102 B 22 C2 C.N. 101
103 C 24 C3 OS 102
104 D 22 C4 SAD 104
Referenced Table (Parent Table) Referencing Table (Child Table)

 Referenced Table (Parent Table)


 Insert- (no violation)
Parent Table में नया record Insert करने पर कोई प्रॉब्लम नही होती है |

 Delete- (may cause violation)


Parent Table में record को delete करने पर प्रॉब्लम हो भी सकती है और नही भी, क्योमक इसमें Parent Table से
child table reference ले रही होती है |

Parent Table में delete जब तक नही कर सकते , जब तक उसके child available है |

 Solution Delete

1. On Delete Cascade- Parent Table में कोई record के delete करने पर साथ में child table का record भी
delete कर दे ते है |

2. On Delete Set Null- Parent Table में record के delete करने पर child table की field value को Null set
कर दे ते है |

नोट:- जब child table में foreign key उस table की primary key होती है , तो “On delete set null” operation
perform नही कर सकते है | क्योमक primary key की field value कभी भी Null नही हो सकती|

3. On Delete No Action- (यह by default operation होता है )


Parent table के record को जब तक delete नही कर सकते, जब तक की उसका child record available होता है |

 Update- (may cause violation)

Parent Table में record को update करने पर प्रॉब्लम हो भी सकती है और नही भी, क्योमक इसमें Parent Table से
child table reference ले रही होती है |

 Solution Update (Same as Delete)


1. On Update Cascade- parent table में कोई record के update करने पर साथ में child table का record भी
update कर दे ते है |

2. On Update Set Null- parent table में record के update करने पर child table की field value को Null set
कर दे ते है |
3. On Update No Action- ( by default solution)
Parent table के record को जब तक update नही कर सकते, जब तक की उसका child record available होता है |

 Referencing Table (Child Table)


 Insert- (may cause violation) प्रॉब्लम हो भी सकती है , और नही भी|

Child table में record तभी Insert होता है , जब वह same field value parent table में exist करती हो| अथाड त्
parent table में available हो|

 delete- (no violation)


Child table में हम record को कभी भी delete कर सकते है | इससे कोई भी प्रॉब्लम (violation) नही होता है |

 Update- (may cause violation)

Child table में record update करने पर प्रॉब्लम हो सकती है , जब तक वह update parent table में available ना
हो|

नोट:- Referenced table (parent table) में record के Insert करने पर तथा Referencing table (child table) में
record के delete करने पर कोई भी प्रॉब्लम (violation) नही होता है | बाकी सभी case में violation होता है |

Operation Referenced Table (Parent Table) Referencing Table (Child Table)


Insert (No Violation) (May Cause Violation)
Delete (May Cause Violation) (No Violation)
Update (May Cause Violation) (May Cause Violation)

 Foreign Key on Same Table


Same Table में Foreign Key हो सकती है |

 Foreign Key Rules-


 Foreign Key can contain Null Value.
Foreign Key में Null value हो सकती है |

 Foreign Key can contain duplicate Value.


Foreign Key duplicate value रख सकते है |

 Foreign Key values always belong to primary key.


Foreign Key value हमे शा primary key से belong करती है | अथाड त् foreign key में वही value हो सकती है , जो
primary key में मौजू द हो|

Sunil Yadav Sir - 9694473047


Primary Key Foreign Key
(Employee)

E. id E. Name Manager id City Salary


E1 Sunil Null Jaipur 20,000
E2 Anil E1 Jaipur 10,000
E3 Sanu E2 Jaipur 8,000
E4 Harish E3 Jaipur 15,000

Q. in given table employee E2 record delete than how many another record need to delete for
maintaining referencing integrity constraint?

E. id E. Name Manager id
E1 Sunil Null
E2 Anil E1
E3 Sanu E2
E4 Harish E3

Ans. Delete E3 employee record

Foreign Key Rules apply करके दे खते है |


Foreign Key में Null value हो सकती है , इसमें है |
Foreign Key में duplicate value हो सकती है , इससे कोई िकड नही पड़ता|

Foreign key में वही value हो सकती है , जो primary key में मौजू द हो| foreign key में E2 value मौजू द है , जो primary
key में मौजू द नही है | यानी Sanu का Manager E2 है , जबमक E2 ने तो company छोड़ दी|

इसमलए हमें E3 record को भी delete करना पड़े गा|


 Mapping Cardinality
(Relationship between entity set)

यह entity set के बीच relationship को बताती है |

Student (S.id, Name, Age)

Course (C.id, C. Name, Duration)

Entity Entity set

 Mapping Cardinality Types


 One to one (1-1)
 One to many (1-M)
 Many to one (M-1)
 Many to many (M-N)

1. One to One Mapping (1-1)

Indian Aadhar
Has
Citizen Number

एक व्यक्ति एक ही आधार कार्ड रख सकता है , अर्ाड त् एक व्यक्ति को एक ही Aadhar No. allot होता है |

In one to one mapping first entity set can be participate at most one entity sets on second entity.
One to one mapping में first entity set दू सरी entity पर अधधकतम एक entity set में participate कर सकती है |

C1 A1
C2 A2
C3 A3
C4 A4
C5 A5

1 1
Employee Work Department
Primary Key F.K F.K Primary Key
Employee Work Department

E. id E. Name Age Add. E. id D. id D. id D. Name Location


E1 A 20 Jaipur E1 D2 D1 CS Jaipur
E2 B 22 Kota E2 D3 D2 Civil Ajmer
E3 C 25 Alwar E3 D1 D3 ME Alwar
E4 D 27 Ajmer E4 D4 D4 EE Kota

Primary Key Foreign Key

E. id E. Name Age Add. E. id D. id D. Name Location


E1 A 20 Jaipur E1 D2 CS Jaipur
E2 B 22 Kota E2 D3 Civil Ajmer
E3 C 25 Alwar E3 D1 ME Alwar
E4 D 27 Ajmer E4 D4 EE Kota

 One to one relationship में एक Employee को एक ही Department धमल सकता है तर्ा एक ही Department में
एक ही Employee काम कर सकता है | इसके धलए हम अलग से एक relationship table बनाते है |

 Relationship table यह बताती है , की E1 को D2, E2 को D3, E3 को D1 तर्ा E4 को D4 Allot धकया गया है | अर्ाड त्
यह table one to one relationship को दर्ाड ता है |

 One to one mapping में Relationship की table में minimum दो attribute होते है , जो table की primary key
होते है |

 Relationship table में हम धकसी भी attribute को (E. id तर्ा D. id) दोनोों में से धकसी को भी Primary key बना
सकते है |

 हम relationship table को धकसी भी entity के सार् जै से- employee या department के सार् merge कर सकते
है | हम department के सार् merge करके दे खते है -

 One to one mapping में minimum दो table बनती है |

2. One to Many Mapping (1-M)

Indian * Mobile
Has
citizen 1 Number
M

नोट:- Many side mapping को (*) symbol से भी represent धकया जाता है |


C1 M1
C2 M2
C3 M3
C4 M4
C5 M5

एक Person एक से ज्यादा mobile no. रख सकता है , ले धकन यधद इसका revert (उल्टा) करके दे खे तो, एक mobile no.
धसर्ड और एक person को ही allotment धकया जा सकता है |

Primary Key F. K P. K F. K P. K

C. id Name City C. id M. id M. id Number Operator


C1 A Jaipur C1 M1 M1 9999 Jio
C2 B Alwar C1 M2 M2 8888 Jio
C3 A Jaipur C2 M3 M3 7777 Jio
C4 C Jaipur C2 M4 M4 6666 Jio
C5 D Jaipur C3 M5 M5 5555 Jio
C6 E Jaipur C4 M6 M6 4444 Jio

हम relationship table में many side (M. id) को primary key बनायेगे| क्योधक C. id repeat हो रही है | अर्ाड त् एक citizen
को एक ही mobile no. धमल सकता है , इसधलए M. id unique है | इसधलए table को many side merge धकया जाता है |

P.K F.K P.K

C. id Name City C. id M. id Number Operator


C1 A Jaipur C1 M1 9999 Jio
C2 B Alwar C1 M2 8888 Jio
C3 A Jaipur C2 M3 7777 Jio
C4 C Jaipur C2 M4 6666 Jio
C5 D Jaipur C3 M5 5555 Jio
C6 E Jaipur C4 M6 4444 Jio
Parent Table Child Table

 One-to-many mapping में relationship table के attribute दोनोों entity की primary key होते है |
 इस mapping में many side entity set relationship table की primary key होती है | यानी हम relationship
table में many side mapping के attribute को primary key बनाते है |

 One-to-many mapping में Relationship table को हमे र्ा many side merge धकया जाता है |
 One-to-many mapping में minimum दो table बनती है |
 One-to-many mapping में Single side (1) entity parent table होती है , तर्ा Many side (M) entity child
table होती है | यानी जहाों primary key होती है , वो parent table होती है , तर्ा जहाों foreign key होती है , वो child
table होती है |

Sunil Yadav Sir - 9694473047


3. Many to one (M-1)

* 1
Order Give Customer

O1 C1 C1 O1
O2 C2 C2 O2
O3 C3 C3 O3
O4 C4 C4 O4
O5 C5 C5 O5

 एक Customer बहुत सारे order दे सकता है , ले धकन एक order एक से ज्यादा Customer को allot नही धकया जा
सकता है |

 एक व्यक्ति के पास दो Mobile no. हो सकते है , ले धकन दो व्यक्ति के पास एक Mobile no नही हो सकता है |

P. K. Order F. K. F. K. P. K Customer

O. id O. Name Price C. id O. id C. id C. Name Add.


O1 Shirt 1000 C1 O1 C1 A Jaipur
O2 T-shirt 500 C1 O2 C2 B Alwar

O3 Jeans 2000 C3 O3 C3 C Jaipur

O4 Shoes 3000 C3 O4 C4 D Jaipur


O5 Shirt 1500 C4 O5 C5 A Jaipur

P. K. Order F. K. P. K. Customer

O. id O. Name Price C. id O. id C. Name Add.


O1 Shirt 1000 C1 O1 A Jaipur
O2 T-shirt 500 C1 O2 B Alwar
O3 Jeans 2000 C3 O3 C Jaipur
O4 Shoes 3000 C3 O4 D Jaipur
O5 Shirt 1500 C4 O5 A Jaipur

 Many-to-one mapping, one-to-many का reverse (उल्टा) होता है | इसमें table भी reverse हो जाएगा|

 Many-to-one mapping में relationship table को one side merge कर धदया जाता है |
4. Many to many (M-N)
M N
Student Study Course

S1 C1
S2 C2
S3 C3
S4 C4
S5 C5

इसमें एक entity दु सरे entity के सार् धकतने भी association में हो सकती है | यानी दोनोों side धकतनी भी mapping हो
सकती है |

एक student धकतने भी course पढ़ सकता है , तर्ा एक course धकतने भी student को धमल सकता है |

Primary Key Student F. K. Study F. K. P. K. Course

S. id Name Age S.id C. id C. id C. Name Fee


S1 A 20 S1 C1 C1 DBMS 5000
S2 B 22 S1 C2 C2 C.N. 1000
S3 C 20 S2 C1 C3 OS 2000
S4 D 22 S2 C3 C4 SAD 3000
S5 E 25 S5 C3 C5 DSA 1500

 Many-to-many mapping में Relationship table के attribute दोनोों entity की primary key होते है |

 Many-to-many mapping में Relationship table की primary key S. id + C. id दोनोों के combination से


धमलकर बने गी| क्योधक S. id + C. id दोनोों में repetition हो रहा है , अर्ाड त् duplicate value available है |

 Many-to-many mapping में relationship table की primary key दोनोों attribute के combination से बनती है |

 Many-to-many mapping में relationship table को धकसी भी entity के सार् merge नही कर सकते है , अर्ाड त्
relationship के धलए separate (अलग) table बनती है |

 इसधलए Many-to-many mapping में minimum 3 table बनती है |

Q. 1- In the given mapping what is minimum number of table.

R1

E1 E2

R2

(a) 2 (b) 3 (c) 4 (d) none of these

Sunil Yadav Sir - 9694473047


T-1
R1

E1 E2 T-2

R2
T-3

सबसे पहले धजतनी भी entity होती है , उन सभी की table बनती है | जै से इसमें दो entity है , तो दो टे बल बने गी|

One-to-many relationship में minimum दो table बनती है | पहली E-1 है , तर्ा दू सरी many side merge करके E-2
बने गी|

Many-to-many relationship में minimum 3 table बनती है | धजसमे से दो table E-1 तर्ा E-2 पहले से बनी हुई है ,
इसधलए many side के धलए R-2 अलग से table बने गी|

One-to-many Many-to-many

E-1 R-1 E-2 E-1 R-2 E-2

T1 T2 T3
पहले से पहले से
बनी हुई बनी हुई
है | है |
Ans. =2+1=3 table

Q. 3

R-1
M M
E-1 R-1 E-2 E-3

R-1

In the above mapping what is the minimum no. of tables?

(a) 4 (b) 5 (c) 6 (d) 7

Ans. - 6

सबसे पहले धजतनी भी entity है | उन सभी की table बनायेगे| इसमें entity की 3 टे बल बने गी|
Many-to-many mapping में relationship की भी separate table बने गी| इसमें relationship की 3 टे बल बने गी|
Q. 3- What is primary key of relationship R1 & R2 respective?

(a) A, B (b) A, A (c) A, AB (d) AB, A

R-1

E-2 E-2

R-2
A B

R-1 की prime key= A (many side)

R-2 की prime key= AB दोनोों धमलकर बने गी|

Q. 4- Three table competitor, event, competes and their relationship given below. (Programmer-2013)

Select the true statement:

1. There is a British competitor in every event.


2. Pierre does not compete in any event.
3. Sven has been entered in two events.
4. Hilary has entered only the running event.

Ans. 4- (Hilary धजसकी C. id- 02 है , उसने केबल running event (01) में ही प्रवेर् धकया है |

Q. 5- When the referential integrity rule is enforced, which one usually not a valid action in response to
the deletion of a row that contains a primary key value referenced elsewhere?

जब रे फ़रें धर्यल इों धटधिटी (Foreign key) rule apply धकया जाता है , तो आमतौर पर एक Row को हटाने के जवाब में कौन
सी एक वैध (valid) कारड वाई नहीों होती है | अर्ाड त् invalid धजसमें प्रार्धमक कुोंजी मान कहीों और सोंदधभड त होता है ?

1. Do not allow the deletion - (अर्ाड त् on delete no action)


2. Accept the deletion without any other action
3. Delete the related rows - (अर्ाड त् on delete cascade)
4. Set the foreign keys of related rows to null - (अर्ाड त् on delete set null)

Ans. 2 (invalid statement)


 Normalization
 Normalization- Normalization is a process reduce/eliminate redundancy/duplication of a database
table

Normalization एक प्रोसेस होती है | जजसके द्वारा हम database table में मौजू द redundant data या duplicate
data को reduce या eliminate जकया जाता है |

अथाा त् Normalization के द्वारा हम database में present redundancy को remove करते है |

T-1
Spliting Table

T-2 T-3

Decomposition

 When two or more relation (table) is combined on single table then data redundancy present.

जब दो या दो से अजधक relation (table) को single relation (table) में combined करते है , तो data redundancy
present हो सकती है |

 When we create directly table using E-R model when data redundancy can be available.
जब हम E-R model की सहायता से directly table को design करते है , तो table में redundancy हो सकती है |

 Decomposition- Decomposition एक प्रोसेस होता है , जजसमे एक table को एक से ज्यादा table में split जकया
जाता है | अथाा त् तोड़ा जाता है |

 Anomalies- anomalies a special type problem occur in database, whenever database tables records
are redundant.

Anomalies एक special प्रकार की प्रॉब्लम होती है| जब database table में data redundant होता है |

 जब table में Data Redundancy होती है , तो उसके कारण table का data inconsistent हो जाता है , जजससे table
में जनम्न Anomalies Create होती है |

 There are three type Anomalies (IDU)


Insert Anomalies

Delete Anomalies

Update Anomalies
Student

S. id S. Name Age C. id C. Name Instructor Fee

S1
Sunil 27 Null Null Null Null
Insert Anomalies
1000
Null Null Null C2 C.N. Sunil
5000

Delete Anomalies S2 Anil 28 C1 DBMS Sunil 3000


Redundancy
S3 1000
Harish 29 C2 C.N. Sunil /Duplication
5000
Update Anomalies
S4 Sanu 25 C3 OS Sharansh 3000

1000
S5 Rahul 26 C2 C.N. Sunil
5000

1. Insert Anomalies
 जब हम student S1 की entry करते है | परन्तु S1 student को course allot नही जकया गया| इसजिए course के
जिए C. id , C. Name, Instructor, Fee में null value set करना पड़े गा|

 C2 की entry की गई है | परन्तु यह जकसी student को allot नही जकया गया| इसजिए student के जिए S. id, S.
Name, Age में null value set करना पड़े गा|

 Student table में जब हम जकसी student के record को insert करने के जिए course के record में null या
dummy value insert करनी पड़ती है | जजससे की डे टाबेस में null की संख्या बढ़ जाती है | जजसे हम Insert
anomalies कहते है |

 Database table more number of null value called Insert anomalies.


Database की table में null की संख्या बढ़ जाना Insert anomalies कहिाता है |

2. Delete Anomalies
 Student table में जब S2 के record को delete जकया जाता है , तो उसके साथ course C1 का record भी delete हो
जाता है | जजसे हम delete anomalies कहते है |

3. Update Anomalies
SQL- update student set fee=5000 where C_id=C2;

 जब हम single course C2 के record को update करते है , तो हमें multiple records को update करना पड़ता है |

 अगर कुछ redundant record तो update हो गए, परन्तु कुछ record update नही हुए, तो इससे inconsistency
की problem हो सकती है | जजसे हम update anomalies कहते है |

Sunil Yadav Sir - 9694473047


 Solution- Decomposition of Relation (Table)
 Decomposition- Spliting to relation into two or more sub relation is called Decomposition.

(To reduce/eliminate redundancy)

Redundancy को Reduce या Eliminate करने के जिए, एक table को एक से ज्यादा table में split जकया जाता है |
अथाा त् तोड़ा जाता है | जजसे Decomposition कहा जाता है |

Student

P. K. F. K. F. K. P. K.

S. id S. name Age S. id C. id C. id C. Name Instructor Fee

S1 Sunil 27 S1 C1 C1 DBMS Sunil 1000


S2 Anil 28 S1 C2 C2 C.N. Sunil 3000
S3 Rahul 29 S2 C1 C3 OS Sharansh 5000
S4 Anil 27 S2 C3

Relation में anomalies और redundancy को remove करने के जिए हम Relation को एक से ज्यादा sub relation में
divide करते है , जजससे की anomalies और redundancy की problem remove हो जाती है |

अथाा त् Relation को एक से ज्यादा table में तोड़कर (split) करके इसका solution करते है | इससे हम data को delete,
update तथा insert करने में कोई problem नही होगी|

इसमें हमें student के record को insert करते समय course के record को insert नही करना पड़ा, तथा course के
record को insert समय student के record को insert नही करना पड़ता है |

इसमें मु झे यजद S1 के record को delete करना पड़ता है , तो मु झे course के record को delete करने की requirement
नही पड़ी है |

 Function Dependency (FD)


 Function dependency defines the relationship between attribute.
Function dependency टे बि में मौजू द attribute के बीच के relationship को बताती है |

Student

S. id S. Name Age S. id S. Name


S1 Sunil 27
S2 Anil 28 Determinant Determiner
S3 Rahul 29 (Dependent)
S4 Sunil 27
S5 Sanu 28

 S. id Determine कर रही है , S. Name को| (अथाा त् S. id बता रही है , की दोनों Sunil अिग-अिग है )

 S. Name Functional dependent है , S. id पर|


S. id S. Name S. id S. Name S. id S. Name S. id S. Name
S1 Sunil S1 Sunil S1 Sunil S1 Sunil

S2 Anil S1 Sunil S2 Sunil S1 Anil


Valid Valid Valid Invalid
S. id ⟶ S. Name

Same S. id Value – Name must be same


Different S. id Value – Name can be anything
 Determinant की same Value पर determiner की Value भी same होनी चाजहए|

 Determinant की Different Value पर determiner (dependent) की Value कुछ भी हो सकती है |

X Y
x1 y1 X Y
Same X value Y
x1 y1
must be same
x1 y1 Determinant Determiner
x2 y1 (Dependent)
Different X value
x3 y2
Y can be anything
x4 y2

 इसमें X की same Value पर Y की Value भी same होनी चाजहए|


 X की Different Value पर Y की की Value कुछ भी हो सकती है |

Ex- Function dependency exists.

X Y Z Valid Invalid X की सभी value unique है | इसजिए


a1 b1 c1 X⟶Y Y⟶Z Y तथा Z की value कुछ भी हो सकती
a2 b1 c1 है | इसजिए यह case valid होगा|
X⟶Z Z⟶X
a3 b1 c1
Z⟶Y
a4 b2 c1
a5 b2 c2 Y⟶X

a6 b2 c2

Q. Which of the following is valid Function dependency?

X Y Z
a1 x1 b2
a1 x2 b2
a2 x2 b2
a2 x3 b3
a3 x2 b2
a4 x4 b4
(a) X⟶Y (b) Y⟶Z (c) Z⟶X (d) Z⟶Y

Ans. (b)

Sunil Yadav Sir - 9694473047


 Type of Function Dependency
1. Trivial FD
2. Non-Trivial FD

3. Semi-Trivial FD

1. Trivial Function Dependency


 If X, Y are two attribute set of relation R in trivial Function dependency when.

(X⊇Y) (यानी की X superset होना चाजहए Y का)

यजद X, Y दो attribute है | जकसी relation R के वो trivial Function dependent जब होंगे, जब X superset होगा Y
का तथा Y subset होगा X का|

 Trivial Function में कोई attribute स्वयं को determine करता है , तो यह always valid case होता है |

Ex.

1. S. id ⟶ S. id (valid case)
X Y

2. S. id+S. Name ⟶ S. name (valid case) (यह X का subset है )


X Y
3. S. id + S. Name + Age ⟶ S. Name + Age (valid)

4. S. id + S. Name ⟶ Age (invalid) (Age subset नही है | इसजिए यह invalid case होगा)

5. S. id + Age + City ⟶ City (valid)

6. S. id + Age + City ⟶ City + mobile (invalid)

7. S. id + Age ⟶ City (invalid)

2. Non-Trivial Function Dependency


 If X, Y are two attribute set of relation R in non-trivial Function dependency when.

(X⋂Y=⌽)

यजद X, Y दो attribute है जकसी relation R के, वो non-trivial Function dependent जब होंगे, जब- X⋂Y=⌽
(फाई) होगा|

यानी X तथा Y के बीच कोई common attribute नही होना चाजहए|

 इसमें X तथा Y में कोई भी common attribute नही होना चाजहए| अथाा त् X जकसी नये attribute को determine
करना चाजहए|
 Non-trivial Function dependency में determinant always नये attribute को determine करता है |
Ex.

1. S. id ⟶ S. Name (valid case)


X Y ⌽
2. S.id + S. Name ⟶ Age (valid)

3. S.id + S. Name ⟶ S. Name + Age (invalid)

4. S. id + Age ⟶ City (valid)

5. S. id + Age + City ⟶ Mobile (valid)

6. S. id + S. Name ⟶ S.id + City (invalid)

3. Semi-Trivial/ Semi Non-Trivial Function Dependency


(Combination of Trivial+non-Trivial)

 If a Function dependency is Trivial+non-Trivial then this called semi non-Trivial Function


dependency.

यजद Function dependency, Trivial और non-Trivial का combination है , तो उसे semi non-Trivial Function
dependency कहते है |

 If X, Y is attribute set of relation R in semi-trivial Function dependency when.


(X⊉Y)
X और Y दो attribute है , जकसी relation R के, वो non-trivial Function dependent जब होंगे|

जब- (X⊉Y) होगा, यानी की X subset नही हो Y का, या Y subset नही हो X का|

Ex.
1. S. id ⟶ S. id + S. Name (valid)
S. id ⟶ S.id (Trivial FD)
S. id ⟶ S. name (Non-Trivial FD)

2. S. id + C. id ⟶ C. id + fee (valid)
S. id + C.id ⟶ C. id (Trivial FD)

S. id + C. id ⟶ Fee (Non-Trivial FD)


Trivial Function Dependency (X⊇Y)
Q.1 Which of the following is trivial Function dependency. Non-Trivial Function Dependency (X⋂Y)

(a) x⊃y (b) x=y (c) x⋂y (d) x⊉y Semi-Trivial Function Dependency (X⊉Y)

Ans. (d)

Q.2 Which of the following is valid semi-trivial Function dependency?

(a) A ⟶ BC (b) AB ⟶ CD (c) AB ⟶ BC (d) None


Ans. (c)

Sunil Yadav Sir - 9694473047


 Amstrong’s DBMS Rules
(Amstrong’s के DBMS Rules)

1. Reflexivity

[X⟶X] (Always Valid)

(कोई भी attribute स्वयं को हमे शा determine करे गा, तो यह हमे शा Valid होगा)

2. Transitivity Rule

*X⟶Y, Y⟶Z+ = X⟶Z

यजद X⟶Y को determine करता है , या Y⟶Z को determine करता है , तब X⟶Z को determine करे गा|

3. Set Augmentation Rule

*X⟶Y+ = XZ⟶YZ

यजद X⟶Y को determine करता है , तब दोनों के साथ कोई common attribute (जै से- Z) िगाने से इस पर कोई फका
नही पड़ता है | इसका मतिब XZ⟶YZ को determine करे गा|

4. Split Rule

*X⟶YZ+ = X⟶Y or X⟶Z

यजद X⟶YZ को determine कर रहा है , तब X⟶Y को तथा X⟶Z को determine करे गा|

5. Merge Rule

*X⟶Y or X⟶Z+ = X⟶YZ

यजद X⟶Y को या X⟶Z को determine कर रहा है , तब X⟶YZ determine करे गा|

(यह Split Rule का reverse होता है )

 Attribute Closer- (×+)


×+- (Set of attribute determine by × recursively)
Attribute Closer के द्वारा हम उस Attribute से जजतनी भी Attribute को बार-बार (recursively) determine कर सकते
है |

Ex.
R (A B C D)
FD- [A⟶B, B⟶C, C⟶D]

A का Closer (A+) = ABCD C का Closer (C+) = CD

B का Closer (B+) = BCD D का Closer (D+) = D


Ex.

R (A B C D E F G)
FD- [A⟶B, BC⟶D, A⟶E, E⟶C, EF⟶G]

A+= [A B E C D]
AF+= [A F B E C D G] ⟶ (Candidate Key)

(AF+ से सभी attributes determine हो रहे है | इसजिए यह AF candidate key होगा)

Ex.

R (A B C D E)
FD [A⟶B, B⟶C, C⟶D, D⟶E, E⟶A]

A+= ABCDE
B+= BCDEA
Candidate Key
C+= CDEAB

D+= DEABC

(यह चारों सभी attributes को determine कर रहे है | इसजिए यह चारों candidate key होंगे)

 Membership Test
Q. FD [AB⟶C, BC⟶D, D⟶E]

AB+= E is member or not

AB+= [ABCDE]

Ans. E is a member of AB+ (AB+ का E Closer है )

Q. R (A B C D E F G)
FD- [AB⟶C, D⟶E, E⟶A, A⟶B, F⟶G]

A+= G is member or not?

ADF+= G is member or not?

Ans. A+= [ABC] (G is not a member)


Ans. ADF+= [ADFBECG] (G is a member)

DF+= [DFEABGE] ⟶ (Candidate Key)

(इसमें DF से ही Candidate Key जनकि रही है | इसजिए ADF Candidate Key नही होगा| क्योजक ADF का Subset है
DF)

Sunil Yadav Sir - 9694473047


 Super key
(X) Is a super key

X attribute value are unique.


X -- --
X की attributes value unique होगी|
𝒳1 - -
[X is S.K of relation R] ⟶ [X+ determine all attribute of relation R]
𝒳2 - -
If X is super key of relation R then X+= determine all attribute of relation R
𝒳3 - -
अगर X+ attributes closer के द्वारा, इस relation R के सभी attribute को determine
𝒳4 - - कर जिया जाएगा, तो यहााँ X super key बन जायेगी|
- - -
- - -
- - -

 Relation R में कोई attribute super key होगा, जब उस attribute का closer उस relation के सारे attribute को
determine करे |

 Super Key, attribute के साथ relation के बाकी attribute add करने पर वो सभी super key ही बनती है |

Ex 1.

R (A B C D E)
FD- [A⟶E, B⟶CD]

A+= AE (यह Super Key नही होगा)

AB+= [ABECD] ⟶ (Super Key)

ABC+= ABCED
यह तीनो भी Super Key होगी, क्योजक जकसी एक super key के Attribute
ABD+= ABDEC (Super Key)
के साथ कोई भी Attribute Add करने पर वह भी super key बनती है |
ABE+= ABECD

Ex 2.

R (A B C D E F G)
FD- [A⟶BC, D⟶E, EF⟶G]

A+= [ABC] ⟶ (Not super key) (1) ADF


AD+= [ADBCE] ⟶ (Not super key) (2) ADFB
ADF+= [ADFBCEG] ⟶ (super key) (3) ADFC यह सभी Super key होगी|
(4) ADFE

(5) ADFG

Sunil Yadav Sir - 9694473047


 Candidate Key
 (Minimum Super Key is Candidate Key)
 X is candidate key of relation R

(जकसी relation R में X candidate key होगी)

 Rule 1- X Must be Super Key


(X super key होना चाजहए)

 Rule 2- No proper subset of X is super key


(X के proper subset अिग से super key नही होने चाजहए)

Ex.

1. R (A B C D)
Rule 1- Candidate key, super key होनी चाजहए|
FD- [A⟶B, C⟶D]
Rule 2- Candidate key के proper subset अिग से super key
AC+= [A C B D]
नही बनने चाजहए|

Super key AC+ के subset (A, C) अिग से super key नही है , इसजिए यह
A+= AB Candidate key बन जायेगी|

C+= CD

 Shortcut Method
जो Attribute right side available नही है | उससे check करें गे की वो Candidate key बन रही है या नही|
2. R (A B C D E F)
FD- [A⟶C, E⟶B, F⟶E]
Candidate Key- ADF+= [A D F C E B]

3. R (A B C D E) (GATE Q.)  इसमें E प्रत्येक Candidate key का Part बने गा|


FD- [AB⟶CD, C⟶A, D⟶B]  EAB में right side में B available है , तो हम इसे D से
EAB+= EABCD replace करे गें|
 EAD में right side में A available है , तो हम इसे C से
EAD+= EADBC
replace करे गें|
+
ECD = ECDAB  ECD में right side में D available है , तो हम इसे B से
ECB+=ECBAD replace करे गें|

Candidate Key- [EAB, EAD, ECD, ECB]
4. R (A B C D E F)
 इसमें A प्रत्येक Candidate key का Part बने गा|
FD- [AB⟶C, C⟶DE, CD⟶F, CE⟶F, EF⟶B]
 AB में right side में B available है , तो हम इसे EF से
+
AB = ABCDEF replace करे गें|
 AEF में right side में F available है , तो हम इसे C से
AEF+= AEFBCD
replace करे गें|
+
AEC = AECDFB (Not a candidate key)  AEC का subset AC+ super key है , इसजिए AEC
AE+= AE (Not a super key) candidate key नही होगा|

AC+= ACDEFB (candidate key)

Candidate Key- [AB, AEF, AC]

5. R (A B C D E F G)
 इसमें A प्रत्येक Candidate key का Part बने गा|
FD- [AB⟶C, C⟶DE, E⟶F, F⟶G, G⟶B]
 B को replace करे गें G से
+
AB = ABCDEFG  G को replace करे गें F से
AG+= AGBCDEF  F को replace करे गें E से
 E को replace करे गें C से (Split Rule िगाकर)
AF+= AFGBCDE
C⟶D
+
AE = AEFGBCD C⟶E
AC+= ACDEFGB
Candidate Key- [AB, AG, AF, AE, AC]

6. R (A B C D E F)
 A को replace करे गें F से
FD- [A⟶BC, C⟶DE, E⟶F, F⟶A]  F को replace करे गें E से

A+= ABCDEF  F को replace करे गें E से


 E को replace करे गें C से (Split Rule िगाकर)
F+= FABCDE
C⟶D
+
E = EFABCD C⟶E
C+= CDEFAB
Candidate Key- [A, F, E, C]

Sunil Yadav Sir - 9694473047


 Rules of Decomposition
Decomposition- If we want to split single relation R into sets of sub-relation.

यजद हम single relation R को sub-relation के sets में split करना चाहते है , तो वह Decomposition कहिाता है |

R1, R2, R3……………Rn

 Decomposition के Rules
1. Lossless join Decomposition

Decomposition होने के बाद में table का data loss नही होना चाजहए|

2. Dependency preserving Decomposition.

Table में मौजू द कोई भी attribute में कोई भी relationship है , तो बाद में भी उनकी relationship होनी चाजहए|

R1 R2 R3 R4
 Relation schema R divided into sub-relation.

R1, R2, R3, R4……………Rn

 In general [R1 ⨝ R2 ⨝ R3…….. Rn]⊇ R

Natural join

General case में हमारे जजतने भी relation है | जब हम sub-relation को Natural join करते है , तो वह या तो original
relation का super set होता है , या वो original relation के equal (बराबर) होता है |

यजद वह original relation से super set होता है , तो उसे Lossy join Decomposition कहते है | यजद वह original
relation के equal (बराबर) होता है | तो उसे lossless join Decomposition कहते है |

1. If [R1 ⨝ R2 ⨝ R3…….. Rn] ⊃R

Lossy join Decomposition

यजद वो original relation से subset होता है | यानी की ज्यादा होता है , तो उसे Lossy join Decomposition कहते है |

2. If [R1 ⨝ R2 ⨝ R3…….. Rn] =R

Lossless join Decomposition

यजद वो original relation के equal (बराबर) होता है , तो यह lossless join Decomposition कहिाता है | जै से-

[R1 ⨝ R2]= R
R
आता है , तो इसे (LLJ) lossless join
Decomposition कहते है |

R1 R2
Ex.1

S. id S. Name C. id
Candidate Key- S. id+C. id
S1 A C1
S1 A C2 FD- [S. id ⟶S. Name]
S2 B C2
S2 B C3
Decomposition of Table

S. K. R1 R2

S. id S. Name S. id C. id
S1 A S1 C1
S1 C2
S2 C2
S2 B
S2 C3

[R1 ⨝ R2]

Condition- R1. S. id = R2. S. id

S. id S. Name S. id C. id

S1 A S1 C1

S1 A S1 C2

S1 A S2 C2

S1 A S2 C3

S2 B S1 C1

S2 B S1 C2

S2 B S2 C2

S2 B S2 C3
[R1 ⨝ R2]= R

S. id S. Name C. id

S1 A C1

S1 A C2

S2 B C2

S2 B C3
Lossless Join Decomposition

Sunil Yadav Sir - 9694473047


Ex.2 (इसमें S. Name को common िे के split करे गें)

S. id S. Name C. id
S1 A C1
S1 A C2
S2 B C2
S3 B C3
Decomposition of Relation R

Not S. K. Not S. K.

S. id S. Name S. Name C. id
S1 A A C1
S2 B A C2
B C2
S3 B
B C3

[R1 ⨝ R2]

Condition- R1 S. Name = R2 S. Name

S. id S. Name C. id
S1 A C1
S1 A C2
S2 B C2
S2 B C3
S3 C C2
S3 C C3
[R1 ⨝ R2]⊃R
Lossy join Decomposition

(S. Name super key नही है | न R1 में न ही R2 में इसजिए यह Lossy join Decomposition होगा)

 If relation R decompose into sub-relation R1, R2 in lossless join decomposition.


When

1. R1 ⋃ R2 = R

2. R1 ⋂ R2 ⟶ R1

Then R1 relation having super key

Or
R1 ⋂ R2 ⟶ R2

Then R2 relation having super key


Q.
D (A B C D E)
FD- [AB⟶C, C⟶D B⟶E]

1. Decomposition (ABC, CD) यह तो R के बराबर ही नहीं है , क्योजक पहिा rule कहता है ,


Lost की R1 ⋃ R2 =R के बराबर होना चाजहए|
R1 (ABC ⨝ R2 CD)
R1 ⨝ R2 = ABCD ≠ R (A B C D E) इसमें तो E ही lost हो गया|

Ans. Lossy join decomposition इसजिए यह Lossy join decomposition होगा|

2. D (ABC, DE)
FD- [AB⟶C, C⟶D B⟶E] यह तो satisfied (√) हो गया| क्योजक यह तो R के बराबर है |

R1= R1 ⋃ R2= ABCDE क्योजक इन दोनों में कोई भी common attribute नही है |

R2= R1 ⋂ R2= ⌽ कोई भी common attribute नही है, तो वह Lossy join


decomposition हो जाएगा|)
Ans. Lossy join decomposition

3. D (ABC, CDE)
FD- [AB⟶C, C⟶D B⟶E] इसमें C ही common है |

R1= R1 ⋃ R2= ABCDE (√) satisfied) अब इसमें check करें गे की C+ super key है , या नही|

R2= R1 ⋂ R2= C+= CD इसमें C+= R1, R2 की super key नही है | इसजिए यह Lossy
join decomposition होगा|
Not the S. K. of R1, R2

4. D (ABCD, BE)
FD- [AB⟶C, C⟶D B⟶E] इसमें B common है |

R1= R1 ⋃ R2= ABCDE (√) B+ से BE attribute determine हो रहा है |

R2= R1 ⋂ R2= B+ = BE B+, R2 की super key बन गयी| इसजिए यह lossless join


decomposition होगा|
B+ is S. K. of R2

5. D (ABC, ABDE)
FD- [AB⟶C, C⟶D B⟶E]

R1 ⋃ R2= ABCDE
R1 ⋂ R2= AB+= ABCDE

AB+ S. K. of both R1, R2

= Relation is lossless join decomposition


6. D (ABC, CD, DE)
R1 R2 R3
FD- [AB⟶C, C⟶D B⟶E]

R1 ⋃ R2= ABCD

R1 ⋂ R2= C+= CD

S. K. of R2

R2 ⋃ R3= ABCDE
R2 ⋂ R3= D+=D

This is not a Super key

(D+ जकसी की भी super key नही बन रहा| इसजिए यह Lossy join decomposition होगा)

7. D (ABC, CD, BE)


R1 R2 R3
FD- [AB⟶C, C⟶D B⟶E]

R1 ⋃ R2= ABCD

R1 ⋂ R2= C+= CD

S. K. of R2 (CD R2 की S. K है , यानी की अभी तक lossless join decomposition है )

R2 ⋃ R3= ABCDE
R2 ⋂ R3= B+ = BE+

S. K. of R3

CD, R2 की super key है तथा BE R3 की super key है | इसजिए यह lossless join decomposition होगा|

 Equivalence of Functional Dependency


 Functional Dependency F & G are equal.
यहां पर दो Functional Dependency F और G equal है |
If F+ ≅ G+

यह equal जब होगा| जब यजद F का attribute closer (F+) और G का attribute closer (G+) दोनों equal होंगे| यानी
F के द्वारा जो Determine जकया जायेगा, यजद same वही G के द्वारा Determine जकया जायेगा, तो वह equal होगा|

 F Cover G- Every FD of G set must implied in F set. [F ⊇ G]


यजद F Cover करता है , G को, तो F super set जायेगा G का|

F
G

F सारे के सारे FD को Cover करती है , जो G के अन्दर मौजूद है |

Sunil Yadav Sir - 9694473047


 G Cover F- Every FD of F set must implied in G set. [G ⊇ F]

G
F
G सारे के सारे FD को Cover करती है , जो F के अन्दर मौजू द है | यहां पर G super set हो जायेगा F का|

Ex.1 R (A B C D)

FD Set-

F- [A⟶B, B⟶C C⟶D]

G- [A⟶BC, C⟶D]

F⊇G (2) G ⊇ F (3) F ≅ G (4) Unknown

F G
A⟶B A⟶BC
B⟶C C⟶D
C⟶D

A+= ABCD A+= ABCD


B+= B C+= CD

C+= CD [F ⊇ G]

[F ⊈ G] (यहां पर G, F का super set नही है | यानी G की power इतनी नही है की


वो F के सभी attribute को determine कर दे )

(यहां पर F super set है G का, यानी F, G के सभी FD को cover करता है |

Ex.2 R (A B C)
FD Set-
F- [A⟶B, B⟶C C⟶A]
F- [A⟶BC, B⟶A C⟶A]
F⊇G (2) G ⊇ F (3) F ≅ G (4) Unknown

F G
A⟶B A⟶BC
B⟶C B⟶A
C⟶A C⟶A

A+= ABC A+= ABC


B+= BAC B+= BCA
C+= CAB C+= CAB

[F ⊆ G] [G ⊆ F ]

Ans. F ≅ G
Ex.3 R (A B C)

FD Set-
F- [A⟶B, B⟶C C⟶A]

F- [A⟶BC, B⟶AC AB⟶C, BC⟶A AC⟶B]

(1) F ⊇ G (2) G ⊇ F (3) F ≅ G (4) Unknown

F G
A⟶B A⟶BC
B⟶C B⟶AC
C⟶A AB⟶C
BC⟶A
AC⟶B

A+= ABC A+= ABC


B+= BAC B+= BCA
C+ = C AB+= ABC

[F ⊈ G] BC+= BCA

AC+= ACB

[F ⊇ G]

Ans. F ⊇ G

 Conclusion/निष्कर्ष

F Cover G G Cover F F&G

1 True False F⊃G

2 False True G⊃F

3 True True F≅G

4 False False Unknown

Sunil Yadav Sir - 9694473047


 Minimal Cover/Canonical Cover
Given FD set minimal cover of F is Fm.

Fm- Minimal set of FD which is logically equal to F

 Given FD sets F= *………+

Fm= F- Remove redundant attribute/redundant FD

1. Extraneous Attribute/Extra Attribute


Ex. 1 FD- [WXYZ⟶U, WX⟶Y]
WXYZ जमिकर U को determine कर रहा है , तथा WX जमिकर Y
को determine कर रहा है |
WX
इसजिए Y को हम Extraneous attribute या Extra attribute कहे गे|
U
Extraneous Y Z इसका मतिब यह है जक यजद हम Y को remove भी कर दें गे, तो भी
Attribute कोई फका नही पड़े गा|

FD- [WXZ⟶U, WX⟶Y] यह FD- [WXYZ⟶U, WX⟶Y] ≅ FD- [WXZ⟶U, WX⟶Y] होगी|

WXZ, U को determine करे गा, तथा WX, Y को determine करे गा|


Ex. 2 FD- [WXYZ⟶U, WX⟶U]
WXYZ चारो जमिकर U को determine कर रहा है |

WX तथा जब अकेिा WX ही U को determine कर रहा है , तो हमें


U चारो attribute िे ने की जरुरत ही नही है |
Y Z
इसजिए Y और Z, Extraneous attribute या Extra attribute है |

FD- [WXYZ⟶U, WX⟶U] ≅ *WX⟶U+

2. Redundant FD
F- [X⟶Y+ (sets of FD’s F except X⟶Y)
If F- [X⟶Y] equal to F

Then X⟶Y is redundant FD

Ex.1 FD sets- [AB⟶C, BC⟶D, AB⟶D]

1. AB⟶C
2. BC⟶D
3. AB⟶D
AB+= ABCD
AB+= ABD
BC+= BCD
BC+= BC
AB+= ABCD
AB+= ABCD

Minimal Cover- [AB⟶C, BC⟶D]


Ex.2 R (A B C D)

FD sets- [A⟶B, C⟶B, D⟶ABC, AC⟶D]

1. A⟶B
2. C⟶B
3. D⟶A
4. D⟶B
5. D⟶C
6. AC⟶D

A+= AB
A+= A
C+= CB
C+ = C
D+= DABC
D+= DBC
D+= DCAB
D+= DAB
AC+= ACDB
AC+= ACB
Minimal Cover-
A⟶B
C⟶B
D⟶A
D⟶C
AC⟶D
FD- [A⟶B, C⟶B, D⟶AC, AC⟶D]

Ex.3 R (W X Y Z)

FD sets- [X⟶W, WZ⟶XY, Y⟶WXZ]

1. X⟶W
2. WZ⟶X
3. WZ⟶Y
4. Y⟶W
5. Y⟶X
6. Y⟶Z

Sunil Yadav Sir - 9694473047


X+= XW
X+= X
WZ+= WZXY
WZ+= WZYX
WZ+= WZ
Y+= YWXZ
Y+= YXZW
Y+= YZ
Y+= YXW
Minimal Cover-
X⟶Y
WZ⟶Y
Y⟶X
Y⟶Z
FD- [X⟶Y, WZ⟶Y, Y⟶XZ]
 (Normal Form)
1 NF

2 NF

3 NF

BCNF

4 NF

5 NF

6 NF

 Reduce/eliminate redundancy of database file.

यह database की redundancy को Reduce या eliminate करते है |

 जब E-R Model से हम database में table बनाते है , तो table में multivalued attribute present होता है , और
table का data redundant या duplicate होता है |

 Normal form के द्वारा हम table में present duplication को remove करते है |

 1 NF
RDBMS default normal form

यह Default normal form होता है | यानी RDBMS की guideline के अनु सार table 1 NF में होनी ही चाजहए|

 3 NF
Generally database design at 3 NF.
Database सामान्यत: 3 NF पर design हो जाता है |

 BCNF
0% redundancy over FD’s but redundancy present on multivalued dependency.
BCNF के द्वारा 0% redundancy हो जाती है | िे जकन यहां पर multivalued dependency के कारण redundancy
हो सकती है |

 4 NF
0% redundancy over multivalued dependency

4 NF multivalued dependency में 0% redundancy रखता है |

 5 NF
Join dependency normal form पर based होता है |

 6 NF
Domain key normal form पर based होता है |

Sunil Yadav Sir - 9694473047


 1 NF (First Normal Form)
RDBMS की guideline के अनु सार टे बि 1 NF में होनी चाजहए|

 Multivalued attribute allowed in E-R model, but not allowed in database table.

Multivalued attribute E-R model में allow होता है , िे जकन database table में allow नही होता है |

 Relation R is in 1NF, if all attribute of table is “Atomic value” (single value)

1NF के अनु सार table में प्रत्येक attribute में atomic value (single value) होनी चाजहए|

इस Student table में C.id multivalued attribute है | इसजिए 1NF में नही है |

 Solution
Solution 1 (Split Record) Solution 2 (Split Column)

S. id S. Name C. id S. id S. Name C. id-1 C. id-2 C. id-3

S1 A C S1 A C C++ Null Not optimize


S1 A C++ S2 B C++ Java Null यानी यह use
नही होता है |
S2 B C++ S3 C C Java Null
S2 B Java S4 D C C++ Java
S3 C C
S3 C Java
S4 D C
S4 D C++
S4 D Java

All attribute are atomic


Q. S. id S. Name Email Phone no. C. id
S1 A E1/E2/E3 P1/P2/P3/P4 C1/C2

In this schema when we convert 1NF then how many record available in table.

1×1×3×4×2=24 (जजतने भी record है उन सभी को multiply करना पड़ता है )

Ans. 24 record

Q. इस Relation में multivalued attribute available है या नही?


R (A B C D E)

FD- [A⟶B, B⟶C, C⟶A]

Candidate Key- ADE+= ADEBC ADE


CDE+= CDEBA CDE Multivalued Attribute
BDE+= BDECA BDE
Ans. Relation R not present in 1NF

 Relation R में “DE” multivalued attribute है , क्योजक DE सभी Candidate key में common है | इसजिए यह
Relation 1NF में present नही है |

 Relation R को 1NF में convert करने के जिए “DE” multivalued attribute की separate table बनायेंगे, तथा सभी
attribute atomic करें गे|

Q. R (A B C D E) A
Candidate key- [A, BC, CDE] BC No multivalued attribute present
Ans. This Relation present in 1NF CDE

Note: - Relation R having one simple/single candidate key then this relation not having multivalued
attribute and relation R is present in 1NF

Relation R में (A) simple/single candidate key है , तो इस relation में multivalued attribute नही हो सकता|
इसजिए यह relation R 1NF में present होगा|

Q. R (A B C D E)
Candidate key- *A, AB, AC+
A
AB

AC

िोट:- यह Candidate Key ही गित है , क्योजक जब A Candidate key है तो AB, AC तो Candidate key हो नही
सकता| AB, AC super key बने गी A की|

िोट:- जब जकसी Relation में एक single candidate key है , तो उस relation में multivalued attribute कभी भी
present नही हो सकता| इसजिए वह relation 1NF में present होगा|

Sunil Yadav Sir - 9694473047


Q. R (A B C D E) AB
Candidate key- *AB, BC, CD+ BC
CD
No multivalued attribute present in relation R
Ans. Relation R present in 1NF

Q. R (A B C D E)
AB
Candidate key- *AB, BC, BD+
BC
“B” is multivalued attribute BD
Ans. Relation R not present in 1NF

 Disadvantage of 1NF
 1NF consist of high degree of redundancy.
1NF high degree redundancy रखता है |

 1NF used to represent table in proper format.


1NF टे बि को proper format में represent करता है |

 2 NF (Second Normal Form)


 Relation (table) must be present in 1NF

Relation (table) 1NF में present होना ही चाजहए|

 Based on partial dependency.


2NF partial dependency पर based होता है |

 Relation (table) not having partial dependency.


Relation (table) में partial dependency नही होंनी चाजहए|

 कोई candidate key का proper subset जकसी non-prime attribute को determine करता है , तो वह partial
dependency कहिाता है |

यानी की जब कोई prime attribute जकसी non-prime attribute को determine करता है , तो वह partial
dependency कहिाता है |

 यजद सभी non-prime attribute, candidate key पर fully functionally dependant है , तो table में partial
dependency present नही होगी, और वह table 2NF में present होगी|
 What is Partial Dependency?
Student

S. id S. Name C. id
S1 A C
S1 A C++
S2 B C++
S2 B Java
S3 C C
S3 C java
Candidate key- S. id+ C. id
Student table में S. id+C. id candidate key है |
Prime attribute- S. id+ C. id
Non-prime attribute- S. Name तथा S. id candidate key का proper subset है , जो की Non-
prime attribute (S. Name) को determine कर रहा है |
FD- [S. id⟶ S. Name]
(यानी की prime attribute जकसी Non-prime attribute को
Prime attribute Non-prime attribute determine करता है , तो उसे partial dependency कहते है )
Proper subset of C.K

Ex.
AB- Candidate key
A- Proper subset of candidate key
AB C- Non-prime attribute
इसमें A,B Candidate key (prime attribute) है , और A इसका Proper
A C subset है |
A determine कर रहा है , C को जो एक Non-prime attribute है | इसे
ही partial dependency कहते है |
(यानी जक Non-prime attribute जकसी Candidate key पर fully
functional depend होना चाजहए, Candidate key के part पर नही)

Q. इस table में partial dependency present है , या नही|

Student
S. id S. Name Age
S1 A 20
S2 B 20
S3 A 20
S4 C 25
S5 D 30
Candidate key- S. id
Prime attribute- S. id
Non-prime attribute- S. Name, Age
FD- [S. id⟶ S. Name]
[S. id⟶ Age]

Candidate Key Non-Prime Attribute

Ans. Partial dependency not present

Sunil Yadav Sir - 9694473047


 Candidate key self-determine non-prime attribute this is not partial dependency.

Candidate key स्वयं non-prime attribute को determine कर सकती है , जजसे partial dependency नही कहते
है |

 यजद Candidate key के proper subset, non-prime attribute को determine करते है , तो partial dependency
present होती है |

 Table में जजतने भी non-prime attribute है , वो सभी Candidate key पर fully functionally dependant है |
इसजिए table में Partial dependency present नही है | इसजिए Table 2NF में present होगी|

िोट- 2NF के अनु सार यजद Table में एक ही candidate key हो और वो भी simple या single हो तो उसमे कभी भी
partial dependency present नही होगी, और वो 2NF में present होगी|

 Remove Partial Dependency (Decomposition)


Student

S. id S. Name C. id Candidate key- S. id+C. id


S1 A C FD- [S. id ⟶ S. Name]
S1 A C++
S2 B C++ Proper subset Non-prime
S2 B Java
of Candidate key attribute
S3 C C
S3 C java
f
R1 R2
S. id S. Name S. id C. id जजस attribute की बजह से partial dependency present
S1 A S1 C हो रही है - S. id, S. Name तो हम उसकी separate table
S2 B S1 C++ बनायंगे|
S3 C S2 C++
S2 Java
S3 C
S3 java

C. K.- S. id C. K.- S. id+C. id


FD- [S. id ⟶ S. Name]

(No partial dependency present in this relation)


 जकसी भी relation में partial dependency को remove करने के जिए जो FD partial dependency का कारण
बनती है | उसके जिए हम separate table बना दे ते है |
Ex. 1 R (A B C D E)
FD- [AB⟶C⟶D⟶E]
 AB Candidate Key का proper subset है |
+
C. K.– ABD = ABDCE  C non-prime attribute है | इसजिए इस table में Partial
Prime attribute- ABD dependency present है |
 “D” candidate key का proper subset है |
Non- Prime attribute- CE
 “E” non-prime attribute है | जफर से Partial
AB ⟶ C
dependency present है |

Proper subset non-prime


of C. K. Attribute

Partial dependency present

Ex. 2 R (A B C D E)
FD- [A⟶B, B⟶E, C⟶D]
Candidate key- AC+= ACBED
Prime attribute- AC
Non-Prime attribute- BED
A⟶B B⟶E
A- Proper subset of candidate B- non-prime attribute
B- non-prime attribute E- non-prime attribute

(Partial dependency present) (Not Partial dependency present)

 Non-Prime attribute जकसी non-prime attribute को determine कर सकता है |


C⟶D
C- Proper subset of candidate
D- Non-prime attribute

(Partial dependency present)

Ex. 3 R (A B C D E F)
FD- [C⟶F, E⟶A, CE⟶D, A⟶B]
Candidate key- CE+= CEFADB
Prime attribute- CE
Non- Prime attribute- FADB
C⟶F
C- Proper subset of candidate
F- Non- Prime attribute

(Partial dependency present)


 3 NF (Third Normal Form)
 Relation (table) must be present in 2NF.

Relation (table) 2NF में present होना ही चाजहए|

 Based on transitive dependency.

3NF transitive dependency पर based होता है |

 No transitive dependency present in relation.

Relation (table) में transitive dependency present नही होंनी चाजहए|

 3NF का उपयोग डे टा duplication को कम करने के जिए जकया जाता है |

 3NF का उपयोग अखण्डता (integrity) को प्राप्त करने के जिए जकया जाता है |

 कोई Non-prime attribute जकसी Non-prime attribute को determine करता है , तो वह transitive


dependency कहिाता है , और वह relation 3NF में present नही होता है |

Ex.

Α Β

Non-prime attribute Non-prime attribute

Transitive Dependency

A B C FD- [A⟶B, B⟶C]

a 1 x Candidate key- A
b 1 x Prime attribute- A
c 1 x Non-Prime attribute- BC
d 1 x
FD- [A ⟶ B, B ⟶ C]
e 2 y
f 2 y इसके कारण transitive dependency present हो रही है |
g 3 z
h 3 z

B ⟶ C को determine कर रहा है , यानी एक Non-prime attribute दु सरे Non-prime attribute को determine


कर रहा है | इसे ही transitive dependency कहते है |

B ⟶ C के कारण transitive dependency present हो रही है | इसजिए इसके जिए separate table बनायेंगे|

Sunil Yadav Sir - 9694473047


Decomposition of Relation
R1 R2
A B B C
a 1 1 x
b 1 2 y
c 1 3 z
d 1 FD- [B⟶C]
e 2
f 2
g 3
h 3
FD- [A⟶B]

Ex. 1 R (A B C D)
FD- [AB⟶C, C⟶D]
Candidate key- AB+= ABCD
Prime attribute- AB
Non-Prime attribute- CD
C⟶D

N. P N. P
Attribute attribute

Transitive dependency present


(This Relation R is not present in 3NF)

 BCNF (Boyce Codd Normal Form)


 Relation/table must be present in 3NF

Relation/table 3NF में present होनी चाजहए|

 Every FD L.H.S. (left hand side) attribute must be super key/candidate key.
प्रत्येक FD में left hand side attribute या तो super key होना चाजहए, या candidate key होना चाजहए|

प्रत्येक FD में left hand side वािा attribute यजद super key या candidate key है , तो वह relation BCNF में
present होगा|

Ex. 2 R (A B C D)
FD- [A⟶B, B⟶C, C⟶D, D⟶A]
Candidate Key-A+= ABCD
D+= DABC
C+= CDAB
B+= BCDA
=Relation R present in BCNF
1NF 2NF 3NF BCNF
Every attribute of table Present in 1NF Present in 2NF Present in 3NF
must be atomic (single
value)
Table में partial Table में transitive FD में प्रत्येक (L.H.S.) left
1NF में table के प्रत्येक dependency नही होनी dependency नही होनी hand side attribute या तो
attribute atomic यानी चाजहए| चाजहए| super key होना चाजहए या तो
single value होनी चाजहए| candidate key होना चाजहए|
Prime attribute जकसी non-prime attribute जकसी
non-prime attribute को दु सरे non-prime attribute
determine नही करना को determine नही करना
चाजहए| चाजहए|

 Relation Between Normal Form

1 NF
यजद कोई Relation R BCNF में है , तो वह relation
2 NF 3NF, 2NF, 1NF में भी present होगा|

यजद कोई Relation R 3NF में है , तो वह relation


3 NF
2NF, 1NF में भी present होगा|

BCNF यजद कोई Relation R, 2NF में है , तो वह relation


1NF में भी present होगा|

Sunil Yadav Sir - 9694473047


 Highest Normal Form Testing
Ex. R (………….)
FD *……….+
Testing highest normal form

सबसे पहिे BCNF, जफर 3NF, जफर 2NF बाद में 1NF में है या नही यह check करें गे|

1. BCNF Testing
α ⟶ β

Every α is S.K/C.K

2. 3NF Testing
α ⟶ β

S.K Prime

 यजद right side attribute कभी Prime attribute होगा, तो उस relation में कभी भी transitive dependency
present नही होगी, क्योजक transitive dependency non-prime attribute के कारण आती है | वह relation 3NF में
present होगा|

3. 2NF Testing
Α ⟶ B

Proper subset Non-Prime


of C.K attribute

यह Present नही होना चाजहए|


(This is not present)

4. 1NF Testing
R (A B C D E)
C. K.- [AB, BC, BD]
“B” is multivalued attribute
“Before 1NF design”
1NF design time “B” is part of every table.

 यजद कोई relation BCNF, 3NF ,2NF में present नही है , तो वह 1NF में present होगा ही होगा|
3NF α⟶ β
2NF
Non-prime attribute
 यजद right side (beta side) Non-prime attribute आ रहा है , तो वह न तो 3NF में न ही 2NF में present होगा|

 यजद α⟶β को determine कर रहा है , और right side (beta side) prime attribute आ रहा है , तो उसमे partial
dependency और transitive dependency present नही होगी|
 Testing Highest Normal Form
Ex. 1 R (A B C D E)
FD- [CE⟶D, D⟶B, C⟶A]
Candidate Key– CE+= CEDBA
Prime attribute- CE
Non-prime attribute- DBA

1. Testing BCNF- CE⟶D D⟶B

Not S.K/C.K.

This relation R not present in BCNF

2. Testing 3 NF- CE⟶D D⟶B


D- Non-prime attribute
B- Non-prime attribute

=This relation R not present in 3NF

3. Testing 2 NF- CE⟶D D⟶B C⟶A


C- Proper subset of C.K
A- Non-prime attribute
=This relation R not present in 2NF

Ans. This relation R present in 1NF


(यानी यह relation R 1NF में present है )

Ex. 2 R (A B C D E F G H)
FD- [AB⟶C, A⟶DE, B⟶F, F⟶GH]
Candidate Key- AB+= ABCDEFGH
Prime attribute- AB
Non-prime attribute- CDEFGH

1. Testing BCNF- AB⟶C A⟶DE

Not S.K/C.K

2. Testing 3NF- AB⟶C, A⟶DE, B⟶F, F⟶GH


F- Non-prime attribute
GH- Non-prime attribute

=Not present in 3NF

Sunil Yadav Sir - 9694473047


3. Testing 2NF- AB⟶C A⟶D
A⟶E

A- Proper subset of C.K


D- Non-prime attribute
=Not present in 2NF

Ans. Relation R present in 1NF


(Relation R 1NF में present है )

Ex. 3 R (A B C D E F)
FD- [AB⟶C, DC⟶AE, E⟶F]
C.K. – ABD+= ABDCAEF (ABD में right side A available है , तो हम A को DC से replace करें गे)
DCB+= DCBAEF

1. Testing BCNF- AB⟶C

Not S.K/C.K

2. Testing 3NF- AB⟶C, DC⟶AE, E⟶F


E- Non-prime attribute
F- Non-prime attribute
=Not present in 3NF

3. Testing 2NF- AB⟶C, DC⟶AE


DC- Proper subset of C.K
AE- Non-prime attribute
=Not present in 2NF
Ans. Highest NF- 1NF

Ex. 4 R (A B C D)
FD- [AB⟶C, BC⟶D]
C.K. – AB+= ABCD

Prime attribute- AB
Non-prime attribute- CD

1. Testing BCNF- AB⟶C, BC⟶D

Not S.K/C.K

2. Testing 3NF- AB⟶C, BC⟶D


BC- Non-prime attribute
D- Non-prime attribute
=Not present in 3NF
3. Testing 2NF- AB⟶C, BC⟶D
Trick for 2NF-

Candidate key- AB
A+= A
B+= B
 यजद candidate key का proper subset अिग से जकसी non-prime attribute को determine नही करता है , तो
वह 2NF में present होता है |

Ans. This relation R present in 2NF

 Rules
1. If every candidate key of relation R having only simple candidate key then are always in.
यजद relation R की प्रत्येक candidate key केबि simple candidate key होती है , तो हमे शा-
(a) 1NF (b) 2NF (c) 3NF (d) BCNF
Ans. (b) 2NF
Simple candidate key जै से- A
(No partial dependency present)
इसमें partial dependency present नही होगी|

2. If every attribute of relation R is prime attribute then R always in-


यजद relation R का प्रत्येक attribute, prime attribute होता है तो R हमे शा-
(a) 1NF (b) 2NF (c) 3NF (d) BCNF
Ans. (c) 3NF

Prime attribute- α ⟶ β no partial or transitive


Dependency present
Prime attribute

3. If relation R is in 3NF and no-trivial FD present in R such that relation is in.


यजद relation R, 3NF में है , तथा trivial FD present नही है -
(a) 1NF (b) 2NF (c) 3NF (d) BCNF
Ans. (d) BCNF
No-trivial FD- No multivalued dependency present
(Relation R is present in BCNF)

4. If relation R is in binary relation only then such relation is in-


यजद relation R केबि binary relation में है ,तो वह जकसमें present होगा|
(a) 1NF (b) 2NF (c) 3NF (d) BCNF
Ans. (d) BCNF
Binary relation में केबि दो attribute होते है - R (A B)

Sunil Yadav Sir - 9694473047


 Decomposition of Highest Normal Form
 Decomposition Rule
1. Lossless Join Decomposition (no data lost)
Table’s का data lose नही होना चाजहए|

2. Dependency Preserving Decomposition (no FD lost)

Table’s जक FD lose नही होनी चाजहए|

Ex. 1 R (A B C D E)
FD- [AB⟶C, C⟶D, B⟶E]
C.K. – AB+= ABCDE
Prime attribute- AB
Non-prime attribute- CDE

1. 2NF Decompose
B⟶E
B- Proper subset of C.K.
E- Non-prime attribute

जजस कारण partial dependency हुई है | उसके जिए separate table बनायेंगे|

R1 (ABCD), R2 (BE)
FD- AB⟶C B⟶E
C⟶D

C⟶D यानी इसमें अभी भी transitive dependency present है इसके जिए हम separate table बनायेंगे|

2. 3NF Decompose
C⟶D
C- Non-prime attribute
D- Non-prime attribute

R1 (ABC), R2 (CD), R3 (BE)


FD- AB⟶C C⟶D B⟶E
3- Table
2- Foreign key
(3NF decompose करने के बाद 3 table तथा 2 foreign key बनी है )
Ex. 2 R (A B C D E F)
FD- [AB⟶C, C⟶D, A⟶E, E⟶F]
C.K. – AB+= ABCDEF

Prime attribute- AB
Non-prime attribute- CDEF

1. 2NF Decompose
A⟶E AB⟶C को तथा C⟶D को determine कर रहा है |
इसजिए ABCD यह R1 table बने गी|
A- Proper subset of C.K.
E- Non-prime attribute Partial dependency A⟶E के कारण आ रही है |
R1 (ABCD), R2 (AEF) इसजिए इसकी separate table बने गी साथ मे E⟶F को
FD- AB⟶C A⟶E determine कर रहा है इसजिए AEF की table बने गी|
C⟶D E⟶F

2. 3NF Decompose
C ⟶ D, E⟶F (इनकी separate table बने गी)
नोट: हमें 2NF Decompose की
R1 (ABC), R2 (CD), R3 (AE) R3 (EF)
table को तोड़ना होता है |
FD- AB⟶C C⟶D A⟶E E⟶F
Table/Relation-4
Foreign key-3

 1NF-
 High Degree of redundancy present.
1NF में High Degree redundancy present होती है |

 Anomalies present.

1NF में Anomalies present होती है |

 Less no. of table.


Table की संख्या कम होती है |

 Query processing time is fast.


Query processing time fast होता है |

 2NF-
 Very less redundancy.

Redundancy कम होती है |

 Some Anomalies can be exist.


कुछ Anomalies हो सकती है |

 Table is more comparing to 1NF.

1NF की तुिना में 2NF में table की संख्या ज्यादा होती है |


 3NF-
 Not redundancy present.

Redundancy ना के बराबर होती है |

 No Anomalies

Anomalies नही होती है |

 Table is more comparing to 2NF.

2NF की तुिना में 3NF में table की संख्या ज्यादा होती है |

 Query processing time is more.

Query processing ज्यादा होती है |

 Database Design Goal


1. 0% redundancy (if possible)

यजद संभव हो तो 0% redundancy होनी चाजहए|

2. Lossless join decomposition (no data lost)


जकसी भी प्रकार का data lost नही होना चाजहए|

3. Dependency preserving decomposition (no FD lost)

जकसी भी प्रकार की FD lost नही होनी चाजहए|

Database Design Goal 1NF 2NF 3NF BCNF

0% redundancy No No No Yes

Lossless Join Decomposition Yes Yes Yes Yes

No
Dependency Preserving Decomposition Yes Yes Yes
(May not possible)

 3NF is more accurate for database design.


Database design के जिए सबसे अच्छा accurate 3NF होता है |
 Indexing and File Organization
 Database Design Steps (Database Life Cycle)
1. Data requirement collection and analysis (planning).
2. Documentation (SRS) software requirement specification.
3. Conceptual database design (logically design).
i. ER model diagram (graphically representation)
ii. Relation model (table)
iii. Normalization (remove redundancy and anomalies problem)
4. Physical database design.
(File Organization) यानी हमारा data फाइल (blocks) के format में store होता है ना की table के|
(File structure and indexing, data dictionary)
(B/B+ tree) for store file structure/ यह फाइल के structure को store करता है |
5. Implementation database and tuning.

 File Organization
 Database (DB) is collection of file (tables)
 File is collection of pages (blocks)
 Block is collection of records

Sunil Yadav Sir - 9694473047


Records of DB File

Fix length record Variable length record


(इसमें record की size Fix होती है ) (इसमें record की size अलग-अलग हो सकती है )
R1 R1

R2 R2

R3 Block R3

‘’ ‘’
‘’ ‘’

Block header Pointer Maintain

 Block Factor= No. of record/block


B-H
R
B= Block size
H= Header size (if no given then 0/ यदद Header size ददया नही गया है , तो हम 0 मानें गे)
R= Record size

Ex. Block size- 1024 byte


Record size- 100 byte
Block Factor=?
B-H =1024-0
R 100 (10.24) =10 Block (हमे शा lower bound रखना होता है )

Block Size-1024 byte B-1

R1 (100 byte) R1
R2 R2
R3 R3
‘’ ‘’
1000 byte ‘’ Block 1 ‘’
‘’ ‘’
‘’ ‘’
R10 R10
R11 24 byte 24 byte Internal Fragmentation problem
Spanned 100×10=1000 byte B-2
Organization
R11 (76 byte) R11 (100 byte) Unspanned Organization

Block 2
Organization of Record

Spanned Organization Unspanned Organization


Record can be spanned in two blocks Record cannot be spanned in two blocks.
Record को हम एक से ज्यादा block में तोड़ सकते है | Record को हम एक से ज्यादा block में तोड़ नही सकते है |

Single record allow to spanned two block Single record on one block.

No internal Fragmentation. May occur internal Fragmentation.


इसमें internal Fragmentation occur नही हो सकता है | इसमें internal Fragmentation occur हो सकता है |

 File Structure
File is a data structure like as array.

Block

0 1 2

3 4 5

6 7 8

Hard Disk (HDD)


नोट:- logically data, tables के रूप में तथा physically data blocks के रूप में store होता है |

 File Structure Generally there are Three Types.


1. Continuous Allocation
2. Linked Allocation
3. Index Allocation

1. Continuous Allocation

Start
Files Size
DBA
abc.db 1 4
0 1 2

3 4 5 (DBA- Data block access)

6 7 8
 File block 1 से Start हो रही है और 4 की Size है |

 Random access possible.


(इसमें Random access possible है )

 File size may not increase every time.


(इसमें File की size हमे शा नही बढ़ा सकते)

Sunil Yadav Sir - 9694473047


2. Linked Allocation (Non-Continuous Allocation)

Start End
Files
DBA DBA
abc.db 1 7
0 1 2

3 4 5

6 7 8

 File size increasing whenever free disk block available.

(हम फाइल की साइज़ को बढ़ा सकते है , जब तक की free disk block available है )

 Only sequential access possible.


(इसमें केबल sequential access possible है )

1. Index Allocation
1
9
Index 11
15 Block Address
7
5
End of file -1

Disk Block for Index

 File Access without index


(हमें index की जरुरत क्ोों पड़ी)

B1
B2 Suppose hear 10,000
blocks
B3
CPU
B4
I/O Need to access only 1
‘’
‘’ Cost record
RAM HDD

 मान लो हमारा डाटा 10Gb का है और हमें केबल 1Mb के डाटा की जरुरत है , तो हमें 10Gb के डाटा के blocks को
RAM में लाना पड़ता है | इसे ही I/O Cost कहते है |
 Introduction to Indexing in Database
 Index used to reduced I/O cost (fast access)

Index का उपयोग I/O cost को reduced करने , यानी fast access के दलए दकया जाता है |

 I/O cost (input/output cost) - No. of secondary memory blocks transfer from secondary memory to
primary memory in order to access data.

दकसी भी particular data को access करने के दलए, दजतने भी secondary memory से blocks को primary
memory में लाया जाता है वह I/O Cost कहलाती है |

S. Name S. No. Search Key Pointer


1
Record 2 B1 1
3
4
5 B2 4
6 RAM CPU
7
A 8 B3 7
9
Access ‘’ ‘’
‘’ ‘’
this Block
File (Student) Index

SQL= Select S_Name from student where S. No. = 8;

 Index File Entry


Field used for index Pointer pointed to disk block

<Search Key, Pointer>


K P
 Index block factor= B–H B= Block size
K+P H= Header size
K= Search key
P= Pointer
नोट:- Block Factor (BF) of index > Block Factor of DB file

(DB File के Block Factor से index का Block Factor हमे शा ज्यादा होता है, यानी index के Block Factor में entry
ज्यादा होती है |

Sunil Yadav Sir - 9694473047


Types of Index
(X- is search key/Candidate Key)

DB records are physically ordered. DB records are physically unordered.


(Based on X) (Based on X)

(जब भी DB record ordered होता है , (जब भी DB record unordered होता है ,


तो Sparse Index बनता है ) तो Dense Index बनता है )
Sparse Index Dense Index

X is Candidate key X is not a key X is Candidate key X is not a key


(Primary index) (Cluster index) (Secondary Index) (Secondary Index)

 यदद Sparse Index में search key एक Candidate key होती है , तो Primary index बनता है और यदद Candidate
key नही है तो Cluster index बनता है |

 Dense Index में हमे शा Secondary Index बनता है |

 जब भी DB record ordered होता है , तो Sparse Index बनता है |

 मान लो यहाों पर DB record ordered है , और X Candidate key है , तो Primary index बनता है | DB record
ordered है , और X Candidate key नही है , तो Cluster index बनता है |

 जब भी DB record unordered होता है , तो Dense Index बनता है |

 मान लो यहाों पर DB record unordered है , और X Candidate key है , तो Secondary index बनता है | DB record
ordered है , और X Candidate key नही है , तो भी Secondary index बनता है , यानी दोनोों case में Secondary
index बनता है |

 Explain by Diagram
4 Type Index in Database

Primary index Clustering index Secondary Index Secondary Index


1 1 9 9
2 1 5 9
3 2 11 5
4 2 17 5
5 3 4 11
6 3 3 11
7 4 13 5
8 4 ‘’ 3
‘’ ‘’ ‘’ 9

X is C.K X not a Key X is C.K X not a Key

Sparse Index Dense Index


 Sparse Index Entry
1
2 B1 1
3
4
5 B2 4
6
7
8 B3 7
9
‘’ ‘’
‘’ ‘’
DB File Index
 Sparse Index Entry- No. of blocks in DB file.

Database file में दजतने blocks होते है , उतनी ही Entry होती है | यानी यहाों पर Entry बहुत कम होती है |

 Dense Index Entry


Index always maintain in ordered

11 2
3 3
2 4
9 5
17 8
8 9
5 11
4 17
DB File Index

 Dense Index Entry- No. of record in DB file.


DB file में दजतने भी record होते है | उन सभी record को dense index entry में रखना पड़ता है |

 Dense index entry always more compare to sparse index entry.


Dense index की entry, sparse index entry से हमे शा ज्यादा होती है |

Ex. DB file= 30,000 records


Record size= 100 byte
Block size= 1024 byte
Search key= 9 byte
Pointer size= 6 byte
Q. 1. No. of entry in sparse index or dense index.
Sparse index और dense index में दकतनी entry होगी|

Q. 2. Block factor of sparse index or dense index.

Sparse index और dense index का Block factor क्ा होगा|


Sparse Index

100 byte B1

B2

30,000 record

DB file
 DB file block factor
B–H 1024-0
R 100 = (10.24) = 10 record /block (प्रत्येक block में 10 record आयेंगे)

 Total No. of block in DB file


30,000
10 = 3000 block

 Sparse index block factor


B–H 1024 1024
K+P 9+6 15 = 68 (यानी की प्रत्येक block में 68 entry है )

 Total block in Sparse index

3000
68 = (44.11) = 45 Block (इसमे Upper bound ले ते है)

Dense Index
100 byte

30,000 record 30,000


record

Dense Index
 Dense Index में दजतने record होते है , उतनी ही Entry होती है | इसदलए 30,000 entry होगी|

 Dense Index Entry= 30,000

 Dense Index block factor= B–H 1024


K+P 9+6 = 68

 Total block in Dense index= 30,000


68 = (441.17) = 442 Blocks (इसमे Upper bound ले ते है)

Sunil Yadav Sir - 9694473047


 Primary Key Index
 Ordered list
 X- search key
Search Key Block Pointer

S. No. S. No.
1 1
2 B1 4 B1
3
7

4 10
5 B2 11 B2
6
‘’

7 ‘’
8 ‘’ B3
B3
9
‘’

10
Primary Key index
11
B4
12
13
‘’
‘’

Index table entry= no. of block in DB file


Data access searching= log2+1
log2= index search को identify कर रहा है |
1= DB file block को यानी Actual data को identify कर रहा है |

Q.

100 byte B1 (2048 byte)


B2

30,000
record

इस DB file में 30,000 record है , तथा प्रत्येक record की size 100 byte है |
Block की size= 2048 byte
Search Key= 9 byte
Pointer= 6 byte
Q. 1. एक record में दकतने Block आयेंगे?

DB Block Factor= Block size


Record size
= 2048/100= (20.48)= 20 Record/Block

Q. 2. Total no. of Block= 30,000


20 = 1500 Block

Q. 3. इस DB file में दकतने index बनें गे?

S. No.
B1 B1
B2 B2
B3 B3
‘’ ‘’
‘’ 30,000 record
B1500 B1500
Index DB File

 Block factor of index= Block size


Search key + Pointer
2048
9+6 = (136.53) = 136 (इसमें lower bound ले ना है)

 Total no. of block in index= 1500


136 = (11.02) = 12 Block (इसमें upper bound ले ना है )

 Record को access करने के दलए दकतने block को access करना पड़े गा?
23=8 (यह 12 से कम है )
Record access (I/O Cost) = log2 N+1
24=16 (यह 12 से ज्यादा है)
Log 12+1
तो 4+1=5
4+1= 5 block को access करना पड़े गा|

 For any DB file (table) at most one primary index can be possible because index file is ordered in
database file.

कोई भी DB file पर एक ही primary index possible है , यानी एक ही primary index बन सकता है , क्ोदक
database file में index file ordered होती है |

 Primary key = Primary index


Primary key के साथ = Primary index बनता है |

 Primary index can be sparse or dense.


Primary index sparse या dense index भी हो सकता है |

 Sparse Primary index is preferred.

Sparse Primary index को ज्यादातर prefer (पसोंद) दकया जाता है |

Sunil Yadav Sir - 9694473047


 Clustering Index
 Index can be build on search key.
Clustering index search key पर build होता है यानी बनता है |
 Search X- is ordered.
 Search X- is ‚Non-key‛
 X- can be duplicate

1 1 B1
2 2
नोट:- यह DB file ordered तो है ,
3 2
परन्तु Key नही है |
4 2 B2
5 3
6 3
7 3 B3
‘’ 3
‘’ 3 B4
‘’ 4
‘’ 5
‘’ 6 B5
6
7

Index DB File
 Cluster index entry = No. of unique record on DB file

 Record access cost (I/O Cost) = log2N+ set of DB block depend on cluster size

 Mostly Cluster index are Sparse, but if cluster size one record then dense can be possible.

ज्यादातर Cluster index Sparse होता है , ले दकन अगर प्रत्येक cluster की size के साथ एक ही record है , तो dense
भी possible हो सकता है |

 At most one cluster index possible on any DB file because record are ordered.
ज्यादा से ज्यादा कोई भी DB file (table) पर एक ही cluster index सोंभव होता है , क्ोदक record ordered होते है |

 For any DB table either primary index or cluster index can possible but not both.

कोई भी DB file (table) के दलए या तो primary index हो सकता है , या cluster index, ले दकन दोनोों index नही बन
सकते|
 Secondary Index (Unordered Records+Key)
Search Key always ordered
S. No. Aadhar No. K P
1 2022 1011
(यहाों Search Key हमे शा
2 1011 2022
ordered बने गी)
3 3031 3031
(इसमें S. No. तो ordered है ,
4 9195 8182
परन्तु Aadhar No. नही है )
5 8182 9195
6 ‘’ ‘’
7 ‘’ ‘’
8 ‘’ ‘’
DB File Secondary Index
 No. of index entry in secondary index = No. of record in DB file.
Secondary index में index entry की सोंख्या = DB file के record की सोंख्या के बराबर होगी|

 Always dense index.


यहााँ पर हमे शा dense index बनता है |

 Secondary Index (Unordered Records+Non-Key)


Ordered + Unique

S. No. S. Name K P
1 Sunil Anil
2 Anil Rahul
3 Sunil Sunil
4 Sunil Surbhi
5 Surbhi ‘’
6 Rahul ‘’
7 Sunil ‘’
Sunil Sunil
8 Surbhi
‘’ ‘’ Sunil Sunil
‘’ ‘’
DB file Intermediate index index
(Block of pointer)

 Multilevel Indexing
K P
B1
B1
B2
B3 B2
B4 B3
B5
B4
‘’
Index (level 3) index (level 2) index (level 1)

Sunil Yadav Sir - 9694473047


 B tree/B+tree
E.id Name City Block 1
1 A Jaipur 128 byte एक record की size= 128 byte है |
(512 byte)
2 B Jaipur Block 2 Disk के एक Block की size= 512
3 C Alwar
byte है |
4 D Kota
एक block में दकतने record आयेंगे?
5 E Jaipur
6 F Jaipur Block 3 Block factor= 512
7 G Jaipur 128 = 4 Record
8 H Jaipur
Total block= 100
9 ‘’ ‘’
4 = 25 Block
10 ‘’ ‘’
11 ‘’ ‘’ Without index block access=
12 ‘’ ‘’ 25 Block
Disk Index
‘’ ‘’ ‘’
‘’ ‘’ ‘’ नोट:- हमारा data Disk (HDD) में store होता है |
100 ‘’ ‘’ index भी HDD में store होता है |

 With Index Block Access

E.id Name City


Key Pointer 1 A Jaipur
1 B1 2 B Jaipur B1
5 B2 3 C Alwar
9 B3 4 D Kota
13 B4 5 E Jaipur
‘’ ‘’ 6 F Jaipur B2
‘’ ‘’ 7 G Jaipur
97 B25 8 H Jaipur
Index 9 ‘’ ‘’
Key size = 2 byte 10 ‘’ ‘’ B3
Pointer size = 6 byte 11 ‘’ ‘’
12 ‘’ ‘’
नोट:- index भी HDD में store होता है |
‘’ ‘’ ‘’
‘’ ‘’ ‘’ B25
100 ‘’ ‘’

Index को store करने के दलए दकतने block की जरुरत है ?


Index block factor = Block size 512
Key+pointer = 6+2 = 64 (यानी एक block में 64 entry store हो जायेंगी)

 हमारी total entry 25 है , इसदलए हमारा index एक ही block में store हो जायेंगा|

 मान लो एक file में करोड़ोों record है , तो record की size बढ़ने पर Index की size भी बढ़ जायेंगी|

 Index को store करने के दलए बहुत सारे blocks की जरुरत पड़े गी, इसदलए हम multilevel index create करते है |
 Multilevel Indexing

B1 B1
B1
B1
B2
B2 B2
RAM B2
CPU
B3
B3 B3
‘’ ‘’ ‘’
B4 ‘’
‘’ ‘’
‘’
B40 ‘’
Index level 1 B400 ‘’
Index level 2
Index level 3 B4000

Index level 4

 Rotate Multilevel Indexing

Block Pointer Block Pointer Block Pointer

Key, Pointer Key, Pointer


Children (Order of B-tree)

 Key= order–1 (Key हमे शा order से 1 कम होती है )

 BST (Binary Search Tree)


(Binary यानी इसमें 2 Children/Order दमलें गे)

10 Root Node

8 20 Internal Node

6 9 15 30 Leaf Node

 Key = 1 (प्रत्येक Node पर 1 Key है , जै से- 10, 8, 20, 6, 9, 15, 30)

 Order/ Children= 2
 M-Way Tree

10, 20

Children

5, 8 15, 18 25, 30

Key= 2 (जै से- 10, 20)


Order/ Children= 3
M-way tree- More than one key

नोट:- Binary search tree में 1 key होती है , जबदक M-way tree में एक से ज्यादा दकतनी भी key हो सकती है |

Ex. 10, 20, 30, 40, 50


M-way tree implement

10
20

30

40

 B-tree (Balanced tree)

Sunil Yadav Sir - 9694473047


 B-tree (Balanced tree/Dynamic Multilevel Indexing)

Block Pointer Block Pointer Block Pointer Block Pointer

Key, Data Pointer Key, Data Pointer Key, Data Pointer

C1 C2 C3 C4

Block Pointer
(Maximum No. of Children)
(Order of B-tree)

Order ‚P‛ = 4 (यानी P Order है )

E. id Name --- --
1 ‘’
2 ‘’
3 ‘’
4 ‘’
5 ‘’
‘’ ‘’

Key Pointer (Data Pointer) = हमे शा order से 1 कम होता है |


Order (P) = 4
Key = 3

 B-tree with order=P

Children Root Node Internal Node


Maximum P P
P
Minimum 2
2

Ex- Order = 10

Min. Internal Node= 10


2 =5
Ex. Insert the following key using B-tree with order=4
10, 20, 30, 40, 50, 60, 70, 80, 90, 100
Order- 4 (यानी Children/ Block Pointer maximum 4 होोंगे)
Key- 4-1 = 3 होगी|

B. P. B. P. B. P. B. P.
10, 20, 30, 40
1.
10 20 30 Medium

2. 20 30, 40, 50, 60

Medium
10 30 40 50

3. 50, 60, 70, 80


20 40
Medium

10 30 50 60 70

4.
20 40 60

10 30 50 70 80 90

20, 40, 60, 80


5.
20 40 60
Medium

40

20 60 80

10 30 50 70 90 100
0

नोट:- Maximum item in B-tree with order M and Height h


= Mh-1-1

Sunil Yadav Sir - 9694473047


 B+tree
 Data pointer store in only leaf node.

केबल leaf node में ही Data store होना चादहए|

 Internal node or Root node having block pointer (B.P.), key

Internal node या Root node में block pointer, key होगी|

 Leaf node link together each other using block pointer.


Block pointer का उपयोग करते हुए Leaf node एक दु सरे के साथ link होोंगे|

 Searching operation perform fast, because only need to access leaf node.
Searching fast होती है , क्ोदक केबल leaf node को ही access करना होता है |

 Internal or root node key copy also present at leaf node.

Internal node या root node की key copy भी leaf node में मौजू द होती है |

 Redundant key can be present.

Redundant key भी मौजू द हो सकती है |

 B+tree Structure

Key Key (Root Node)

Key Key Key Key Key Key (Internal Node)

Key Key Key Key (Leaf


Key Key Key Key Key
Key Node)
D. P. D. P. D. P. D. P. D. P.

नोट:- Leaf node- (Key+Data Pointer)

Leaf node में Key+Data Pointer मौजू द होता है |

 Easy Understand (Short Diagram)

B. P. Key B. P. Key B. P. Key B. P. (Internal Node)

Key Key Key B. P. (Leaf Node Structure)


Q.1. AB+ tree index is to be build on the ‚Name‛ attribute of the relation STUDENT. Assume that all
student names are of length 8 byte, disk block are of size 512 byte. Index pointer is of size 4 byte.
Given this scenario, what would be the best choice of the degree (number of pointer per node) of
the B+ tree?

BP Name

Key

B. P.

Key

B. P.

Key

B. P.

Key

B. P.

Block (512 Byte) Student

मान ले ते है block pointer= N है


N×BP + (N-1) × Key ≤ 512 byte
N×4 (N-1) 8 ≤ 512
4N + 8N-8 ≤ 512
12N ≤ 512+8
N = 520
12 = 43.33
=43 Ans.

Q. 2. A B-tree of order 4 is build from scratch by 10 successive insertions. What is the maximum number
of node Spliting operation that may take place?

Order= 4 है , तो Key 4-1=3 होगी|

B.P. Key B.P. Key B.P. Key B.P.

Elements- 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

1. 1, 2, 3

1 3, 4, 5

Sunil Yadav Sir - 9694473047


2.

2, 4

1 3 5, 6, 7

3.

2, 4, 6

1 3 5 7, 8, 9

4.

2 6, 8

5. 1 5 7 9, 10

Ans. = Total No. of Spliting = 5

Q. 3. What is the order ‚P‛ of B+ tree given that the search key field value ‚V‛ =9 byte long, the block size
is B= 512 byte record pointer is Pr= 7byte and block pointer P= 6 byte?

Search Key= 9 byte


Block size= 512 byte
Block pointer= 6 byte
Order/Child =?
मान ले ते है Block pointer= N है |
= N×B.P.+ (N-1) × key ≤ block size
= N×6 + (N-1) 9 ≤ 512
= 6N + 9N - 9 ≤ 512
= 15N ≤ 512 +9

= N ≤ 521
15 = 34.73
=Ans. 34
 SQL (Structured Query Language) 1970
User
Structured- Tables/Relations

Query- Inquiry ‚SQL‛ ‚Communication Language‛


(Interface)

Tables/Relation (RDBMS)
DB File

Database

 IBM develop original version of SQL 1970 named as ‚SEQUEL‛ (Structured English Query Language).
IBM ने SQL का original version 1970 में विकवित वकया था, वजिका नाम ‚SEQUEL‛ था|

 In 1970 father of DBMS Dr. E.F. Codd published a research paper based on relational algebra to
provide how we can use mathematical operation to communication database.

1970 में Dr. E.F. Codd ने एक research paper publish वकया, जो relational algebra पर आधाररत था, उिके
base पर उन्ोोंने बताया था की हम mathematical operation का उपयोग करके database िे communication
कर िकते है |

 It was theoretical paper


यह theoretical (िैद्ाों वतक) पेपर था

 Later on based on Dr. E.F. Codd relational algebra paper IBM developed ‚SEQUEL‛ in 1970.
बाद में Dr. E.F. Codd के relational algebra paper के based पर IBM ने 1970 में ‚SEQUEL‛ को विकवित वकया|

 In 1986 ANSI (American National Standard Institute) ISO standardized SQL first version as SQL-86.
1986 में ANSI ISO standardized SQL का पहला version SQL-86 विकवित वकया|

 SQL is Non-Procedural language.

SQL गैर-प्रवियात्मक भाषा है |

 Non-Procedural language means ‚What to do‛

Non-procedural language का मतलब- इिको नही बताना होता है वक हमें क्या करना है , कैिे करना है |

SQL is Domain Specific language, Means its work only RDBMS.

SQL Domain Specific भाषा है , मतलब यह केबल RDBMS (Relational Database) पर काम करती है और वकिी
Domain पर काम नही करती है |
 SQL is Case Insensitive language.

SQL Case Insensitive भाषा है , इिमें small या capital latter में कोई difference नही होता है , िब Common होता
है |

SELECT
Select All are same.
SElect
नोट- My-SQL में टे बल का नाम Case-sensitive होता है , जबवक ORACLE में टे बल का नाम Case Insensitive होता है |
 My-SQL में Table name size 64 character होती है |

 Table name में हम Alphabets, Number or Special Character में केबल Under Score (_), Dollar ($), को use
कर िकते है |

 SQL Language Part (Based on Command)


DDL DML TCL DCL
(Data Definition (Data Manipulation (Transaction Control (Data Control
Language) Language) Language) language)
CREATE INSERT COMMIT GRANT
ALTER UPDATE ROLLBACK REVOKE
DROP DELETE SAVE POINT
TRUNCATE SELECT
RENAME

नोट- Application program के िाथ DML (Data Manipulation Language) को Embedded वकया जाता है |

 Integrity Constraints
 Rules apply using DDL language
Integrity constraints rules होते है , जो DDL language के िाथ apply वकये जाते है , यानी Table create करते
िमय apply करते है |

1. Primary key (Unique+Not Null)


2. Foreign key (Referential Key)
3. Check
4. Default
5. Unique
6. Not-Null
 View Definition (Create View)
 Embedded SQL/ Dynamic SQL
(SQL Embedded in Application program like Java)
 Authorization (Permission Allocation)

Sunil Yadav Sir - 9694473047Type your text


1. DDL (Data Definition Language)
 Is used for creating and modifying the database objects such as tables, views, users, indices.

यह database objects जै िे की- table, views, user, indices को create करने और modify करने के वलए उपयोग
वकया जाता है |

 DDL Command is used to define the structure/schema of database.

DDL Command database के structure/schema को पररभावषत (define) करती है |

 All DDL Command are Auto-Committed.


DDL की िभी Command Auto-Committed होती है , मतलब Automatic Commit हो जाती है |

 DDL Command
i. Create- To create new database, tables, views, users.

Create Command के द्वारा हम नया database, table, view, user को create करते है |

ii. Alter- To alter modifies the structure of tables.

Alter Command के द्वारा हम table के structure को modify (बदलाब) कर िकते है |

iii. Drop- To delete database objects (Tables, Views) permanently.


Drop Command के द्वारा हम database के object को हमे शा के वलए delete कर दे ते है |

iv. Truncate- To delete all records from tables.

Truncate Command table िे िभी records को delete कर िकते है |

v. Rename- To change name of table.

Rename Command के द्वारा हम टे बल का नाम change कर िकते है |

 Create Command
 Database Create
Stmt >create database database_name;
SQL >create database collage; (इििे Collage name का database create होगा)

 Show Statement (To see existing database or tables)


>show databases; (इििे िभी database show हो जायेंगे)
>show tables; (इििे िभी tables show हो जायेंगी)

 Use Statement- (To use or select any existing database)


(मौजू द database को select करने या database पर काम करने के वलए)

>use database name;


>use collage;

1. Table Create

Stmt >create table table name;


(Column_name1 data type;
Column_name2 data type;
‘’
‘’
Column_name_n data type;
Integrity constraints
);

SQL >create table student;


(Roll_no int,
Name char(30),
Age int,
Address varchar(50),
Primary Key (Roll_no)
);

Student

Roll_no. Name Age Address (इििे टे बल का Structure/ Schema create होगा)

नोट- Table Create करते िमय Table या Column के name में हम space नही दे िकते| इिमें हम under score
(_) use करते है |

 Desc/Describe Statement-
(To show table structure)

यह टे बल के Structure को दे खने के वलए Desc Command का उपयोग वकया जाता है |

>Desc table_name;

>Desc Student;

Sunil Yadav Sir - 9694473047


 SQL Data Type
 Character String
i. Char (n)- A fix length string, with user specific size n Char में fixed length होती है , जै िे-
मान लो हमने 20 byte size दी है , और
Ex. Name char(20) Sunil नाम वलखा है , वजिमे 5 byte use
हुआ है | बाकी 15 byte खराब जायेगा
Store- Sunil

ii. Varchar (n)- A variable length string (Can contain letter, number or special character) with user
specify size n
Varchar में जो हम size दे ते है | जै िे-
Ex. Name char(20) Sunil 5 byte store करता है , तो 5
character ही use होोंगे, 15 byte
Store- Sunil
खराब नही जायेगा|
 Varchar (n)/ varchar 2 (n) both are equal.

Varchar (n) और varchar 2 (n) में कोई फकक भी होता है | दोनोों equal होते है |

 Varchar 2 is used by ORACLE.


Varchar 2 ORACLE द्वारा उपयोग वकया जाता है |

 Numeric
iii. Int/integer- For store integer value

यह integer type की value को store करता है |

iv. Small int- Same as integer but less size compare to int.
यह integer के िमान होता है , ले वकन यह int िे कम size का होता है |

v. Big int- Same as integer but large size compare to int.


यह integer के िामान होता है , ले वकन यह int िे बड़ा size का होता है |

Numeric (size, d) Size identified total length of digits.

Decimal (size, d) d is digits other precision.

Float (size, d)

Ex. Numeric (5, 2) - 100.20 (Total 5 digits और decimal के बाद 2 digits हो िकते है )

vi. Float (n)- Floating point number with user specific size n

Ex. Float (3)

Store- 10125.123 (n after precision digit/ n precision बाद का digit है )

vii. Double (size, n):- Large floating point number stored with total digits and n is precision digits.
 Big String
viii. Bit (n)- Store fixed length bits n
Bit यानी 0 और 1 को store करता है |
Ex. Bit (n)- ‚101011‛
Bit (5)- ‚10_ _ _‛ (इिमें 3 digit wastage होोंगे)

ix. Bit Varing (n)- Variable length bit string.


Ex. Bit Varing (5)- ‚10‛ (इिमें केबल 2 Digit ही use होोंगे)

 Date
Date Format- YYYY-MM-DD

Ex. 2022-08-22

 Time
Time Format- HH:MM:SS

Ex. 19:52:55

 Time Stamp (Both Store)


Date+Time- YYYY-MM-DD HH:MM:SS

Ex. 2022-08-22 19:52:55

 SQL Constraints (Rules)


 Constraints are used to specify rules for data in a table.

वकिी table में data के rules को वनवदक ष्ट (specify) करने के वलए Constraint का उपयोग वकया जाता है |

 Constraints ensure data accuracy or reliability.


Constraint डाटा िटीकता या विश्विनीयता िुवनवित करता है |

 If any Constraints rules violation then data cannot insert into table.
अगर कोई Constraint rule violate होता है , तो table में data insert नही होगा|

(Integrity Constraint violation) हमें ऐिा Error मै िेज वदखाई दे गा|

 Constraints can be Specified/Create


Constraint कब Specified/Create वकया जा िकता है |
1. When table create.
जब table create करें गे|
2. We can also apply using Alter Statement.
Alter statement के िाथ भी हम apply कर िकते है |
 Type of Integrity Constraint
 Entity Integrity Constraints
Primary key (Unique +Not-Null)
Unique
 Domain Integrity Constraints
Not Null
Check
 Referential Integrity Constraints
Foreign Key

 Primary key (Unique +Not-Null)


Primary key Column की value unique और Not-Null होती है , जो की हम table में data insert करते िमय
Check करते है की Primary key Column की value जरुर (Compulsory) insert करनी है और इि Column में
duplicate value insert नही कर िकते|

 Unique
Column all value is different.
Unique Constraint Column में duplicate entry नही कर िकते| Column की िभी value अलग-अलग होती है |

 Not Null
Column cannot contain null value.

Column में null value नही हो िकती, यानी Column को खाली नही छोड़ िकते| उिमे value fill करना जरुरी
(Compulsory) होता है |

 Check
For specify any condition on table column.
Check Constraint Column में condition लगाने के काम आता है , जो की data insert के िमय condition check
होती है | जै िे- कोई age 18 िे ज्यादा िाला होना चावहए|

>age int check (age>18);

 Default-
For set column default value

Default Constraint के द्वारा table के column में default value set करते है |

 Foreign key
For referencing (Relationship) on same or another table
Foreign key Constraint same या अन्य टे बल िे reference ले ती है |
 Applying All Constraint Rule
िभी Constraint का उपयोग करते हुए हम Student Table Create करें गे|

Create table student

(Roll_no int primary key,

Name char(20) not null,

Mobile numeric(10) unique,

City varchar(30) default Jaipur,

Age int,

Check (age>18)
);

 Applying Foreign Key Constraint Rule


Foreign Key Constraint का उपयोग करते हुए हम Course Table Create करें गे|

Create table course

(C_id varchar(5),

C_Name char(20),

Instructor char(20),

Fee float(2),

Roll_no int,

Foreign key (Roll_no) references

Students (Roll_no));

Roll_no Name Mobile City Age C_id C_Name Instructor Fee Roll_no

Primary Key Foreign Key

नोट- Table name or Column name can contain character, number or special character (_) under score,
($) dollar, (#) hash.

Table name या Column name में character, number या special character जै िे- (_) under score, ($) dollar, (#)
hash शावमल हो िकते है |
 Applying Referenced Constraint Rule
Create table course

(C_id varchar(5),

C_Name char(30),

Roll int,

Foreign key (Roll) references student (Roll_no)

On delete set null

On update cascade

);

 Copy Table Creating

Stmt. >create table student_copy AS select * from student;

This statement creates student_copy new table and all column and data also available on copy table.

इि statement िे student copy नाम िे एक duplicate table बनती है , वजिमे student table के िभी कॉलम और
डाटा copy हो जाता है |

Stmt. >create table student_copy1 AS select Roll_no, name, city from student;

This statement only specify column copy on copy-table

इि statement िे student table के केबल specify column को copy-table में लेके आते है |

 When we create copy table then primary key constraints will never copy.
जब हम copy-table बनाते है , तो primary key constraints कभी भी copy नही होता है |

 Copy-table always occupied separate memory space and work independent on main table.
Copy-table हमे शा separate memory space contain करती है और मु ख्य टे बल पर स्वतोंत्र रूप िे कायक करती है |

 Create View (View Create करना)


 View is a virtual table.

Create एक virtual table होता है |

 View always performs operation on master table.

View हमे शा master table पर operation perform करता है |

 View never contains separate memory space.


View कभी भी separate memory space contains नही करती है | यानी View में कभी भी अलग िे memory
space नही होता है |

 View basically create for gives specific access on given tables (security maintain)

View मू ल रूप िे वदए गए table पर specific access प्रदान करता है , जै िे- security maintain करना|
Stmt. >create view view_name
AS
Select column_name1, column_name2
‘’
From table_name

[Where condition] (यह optional होता है )

SQL >create view Std


AS
Select Roll_no, name, city Std view के द्वारा Master Table के Roll_no,
From student; name, city के column पर access वदया गया है |

नोट- When we create view two or more table help view that some case DML operation may not work
proper.

This ½ (half) codd rule ORECLE not follows.

जब दो या दो िे अवधक table की िहायता िे view create करते है , तो कुछ case में database में DML operation
proper काम नही करते है |

यह आधा (half) codd rule ORACLE भी follow नही करता है |

यानी view के द्वारा कई बार जो हम view table में update करते है , िो master table में update नही हो पाता है |

 Alter Command
1. Add New Column
Alter command के द्वारा हम table में नया कॉलम Add कर िकते है |

Stmt >Alter table table_name add column column_name (data type);


(Add के िाथ column
SQL >Alter table student add column city char(20);
को लगाना optional है )

S_id Name S_id Name City

Old Table New Table

 Add more than one column


हम एक िे ज्यादा column भी add कर िकते है |

SQL >Alter table student add column (city char(20), mobile numeric(10), age int);
2. Drop Existing Column
Alter command के द्वारा हम टे बल में मौजू द कॉलम को drop कर िकते है , यानी मौजू द कॉलम को हटा िकते है |

Stmt >Alter table table_name drop column column_name;

SQL >Alter table student drop column age;

Student Drop Age Column

S_id Name City Age

3. Change Table Column Data Type


Alter command के द्वारा हम टे बल में मौजू द कॉलम का data type change कर िकते है |

Stmt >Alter table table_name modify column_name new_data type;

SQL > Alter table student modify S_id varchar(20);

Student

int S_id Name City


Varchar

4. Change Column Size/Length


Alter command के द्वारा हम टे बल में मौजू द कॉलम की size/length change कर िकते है |

Stmt >Alter table table_name modify column_name data type (new_size)

SQL > Alter table student modify name char(30);

Char (20) Student


Char (30)
S_id Name City

5. Change Column Name


Alter command के द्वारा हम कॉलम का नाम change कर िकते है |

Stmt >Alter table table_name rename column old column_name to new_column name;

SQL > Alter table student rename column S_id to Roll_no;

Student

S_id Name City


Roll No.

Sunil Yadav Sir - 9694473047


6. Change Table Name
Alter command के द्वारा हम टे बल का नाम change कर िकते है |

Stmt >Alter table table_name rename to new_table name;

SQL > Alter table student rename to student_info;


या

Stmt >Rename table old_name to New_name; (इिको ऐिे भी वलख िकते है)

SQL >Rename table student to student_info;

Student
Student_info

Roll No. Name City

7. Add Constraint on Table


Alter command के द्वारा हम टे बल के कॉलम पर Constraint Add कर िकते है |

Stmt >Alter table table_name add Constraint_name (column_name);

SQL > Alter table student add primary key (Roll_no);

Primary Key Student

Roll No. Name City

8. Constraints Drop on Column


Alter command के द्वारा हम कॉलम िे Constraint को हटा िकते है |

Stmt >Alter table table_name drop_name (column_name);

SQL > Alter table student drop primary key (Roll_no);

 इिको ऐिे भी वलख िकते है क्योवक Table में एक ही Primary Key होती है |

SQL >Alter table student primary key;


 Drop Command (Delete Data+Structure)
Drop Command टे बल का Structure हमे शा के वलए हटा दे ती है , वजििे की टे बल में मौजू द Data और Structure
दोनोों हमे शा के वलए Remove हो जाते है |

Student Meta Data (Data about data)

Roll No. Name City


101 Sunil Jaipur
102 Sanu Jaipur
Actual Data
103 Rahul Jaipur
104 Harish Jaipur

Data के बारे में जो information रखता है , उिे Meta Data कहते है | (Data about data)

यानी टे बल में मौजू द Actual data को छोड़कर टे बल का बाकी िभी data को Meta Data कहलाता है | जै िे की-
Table name, Table के Column Name आवद|

Meta data, data dictionary में जाकर store होता है |

Stmt >Drop table table_name;

SQL >Drop table student;

नोट- We cannot recover drop command table structure.

Drop command के द्वारा हटाई गई टे बल के Data और Structure को िापि िे recover नही वकया जा िकता है |

SQL >Desc student;

Result ‚Table Don’t Existing‛ (Table मौजू द नही है , यह result Show होगा)

नोट- Describe Command के द्वारा हम टे बल का structure दे ख िकते है | Describe को हम Desc भी वलख िकते है |

 Truncate Command (Remove Table Data Permanently)


Truncate Command Actual Data को हमे शा के वलए हटा दे ती है | यानी टे बल के िभी record को एक िाथ हमे शा
के वलए delete कर दे ती है , परन्तु टे बल का structure delete नही होता है |

Student

Roll No. Name City Table Structure (Remain same)

101 Sunil Jaipur टे बल का Structure ऐिे ही रहे गा|

102 Sanu Jaipur


(Delete Only Actual Data)
103 Rahul Jaipur
104 Harish Jaipur

Stmt >Truncate table table_name


SQL >Truncate student;

 Truncate Command टे बल में मौजू द Data को हमें शा के वलए हटा दे ती है , वजिे हम िापि िे Recover नही कर
िकते है |

Sunil Yadav Sir - 9694473047


2. DML (Data Manipulation Language)
 DML Command used for accessing and modifying data in database.
DML Command database में मौजू द data को access करने तथा उिको modify करने के काम आती है |
Accessing- Select
Modifying- Insert, Update, Delete.

 DML में हम टे बल के Actual data पर Operation Perform करते है , अथाक त् टे बल में नया record insert करते है ,
Old record को update करते है और Old record को Delete करते है | यानी की data में manipulate (बदलाब)
करने के वलए DML Command का उपयोग करते है |

 DML is not auto committed.

DML auto committed नही होता है | यानी DML के िभी Statement को अलग िे Commit करने पड़ते है |

Student

Roll No. Name City


101 A Jaipur Insert a new Row

102 BD Jaipur Update Row


103 C Jaipur Delete Row

नोट- Insert, Update, Delete यह िभी Task RAM में Perform होते है , जब तक हम Commit Operation
perform नही कर दे ते है | Commit Operation के बाद िभी Task database में हमे शा के वलए save हो जाती है |

1. Insert Command
New record/records insert in table.

Insert Command िे हम टे बल में नया record/records Insert करते है |

Stmt-1>insert into table_name (column1, column2…….. column_n)

Values (‘column1_value’, ‘column2_value’…….. ‘column_n_value’);

Stmt-2>insert into table_name


Values (‘column1_value’, ‘column2_value’…….. ‘column_n_value’);

 Need to insert all column value on same table column sequence.

इि statement में एक ही table column sequence में ही िभी column value insert करनी पड़े गी|

SQL >insert into student (Roll_no, Name)

Values (101, ‘Sunil’);

 Insert more than one record.


हम एक िे ज्यादा record भी Insert कर िकते है |

SQL >insert into student (Roll_no, Name)

Values (102, ‘Sonu’), (103, ‘Anil’), (104, ‘Rahul’);


Student

Roll No. Name City


101 Sunil Null
102 Sonu Null
103 Anil Null
104 Rahul Null

नोट- Table Name के िाथ जब हम column को select करते है , तो हमें primary key column और Not Null
column को हमे शा select करना चावहए, नही तो table में record insert नही होगा|

जैसे- मान लो Name- Not Null है और हम Roll No. तथा Age को select करते है , तो िह insert नही होगा, Error
आयेगा|

SQL >insert into student

Values (105, ‘Harish’, ‘Jaipur’);

SQL >insert into student


Value (106, ‘Surbhi’, Null);

नोट- If we don’t select table column then all column values insert compulsory in same table
sequence.

यवद हम table में column को select नही करते है , तो table के िभी column की value को same sequence में
insert करना (compulsory) जरुरी होता है |

2. Update Command
Modify the existing Record/Records in a table.

Update Command के द्वारा हम टे बल में मौजू द Record या Records को Modify कर िकते है | यानी एक या एक
िे अवधक records को Update कर िकते है |

 Always use the Where Clause with update statement to update selected records, else table all record
are affected.

Update Statement के िाथ हमे शा Where Clause का use करता होता है , वजििे की selected record ही
update हो िके, Where Clause का use नही करने पर टे बल में मौजू द िभी record update हो जाते है |

Stmt >Update table_name set Column1=value,

Column2=value,

Column3=value,

[Where Condition];

SQL-1 >Update student set name=’Ravindra’

Where Roll_no=103; (इििे Roll No 103 िाले का नाम Ravinder िे ’Ravindra’ हो जायेगा)

Sunil Yadav Sir - 9694473047


Student

Roll No. Name City


101 Sunil Jaipur
102 Anil Jaipur
103 Ravinder Jaipur
Ravindra
104 Rahul Jaipur

SQL-2 >Update student set city=’Alwar’; (इि Statement िे टे बल में मौजू द City
Student के िभी कॉलम में Alwar set हो जायेगा,
क्योवक Where Condition नही लगाई है)
Roll No. Name City
101 Sunil Alwar
102 Anil Alwar
103 Ravindra Alwar
104 Rahul Alwar

 Statement 1- Update Only Single Record Because Where Condition Given.


 Statement 2- Update All Records Because Where Condition Missing.

3. Delete Command
Delete/Remove Existing Record/Records in a Table.
Delete Command के द्वारा हम टे बल में मौजू द Record या Records को Delete या Remove कर िकते है |

 Always use a Where Clause with a Delete Statement to delete selected record, otherwise all records
are deleted.

Delete Statement के िाथ हमे शा Where Clause का use वकया जाता है , वजििे की selected record ही Delete
हो िके, Where का use नही करने पर टे बल में मौजू द िभी Records Delete हो जाते है |

Stmt >Delete from table_name


[Where Condition];

SQL-1 >Delete from student


Where Roll_no=103;

 वजिका Roll No.103 है | िह Record Delete हो जायेगा|

SQL-2 >Delete from student


Where city=’Alwar’;

 वजिकी City ‚Alwar‛ है | िह Record Delete हो जायेगा|

SQL-3 > Delete from student

 इििे टे बल में मौजू द िभी Records अस्थाई रूप िे Delete हो जाएों गे, क्योवक Where Condition नही लगाई है |
Student Student Student

Roll No. Name City Roll No. Name City Roll No. Name City
101 Sunil Alwar 101 Sunil Alwar 101 Sunil Alwar
102 Anil Jaipur 102 Anil Jaipur 102 Anil Jaipur
103 Ravindra Jaipur 103 Ravindra Jaipur 103 Ravindra Jaipur
104 Rahul Alwar 104 Rahul Alwar 104 Rahul Alwar
Statement 1 Statement 2 Statement 3

 Difference Between Drop, Truncate and Delete

Drop Truncate Delete


Command- DDL Command- DDL Command- DML

Drop Command टे बल में मौजू द िभी Truncate Command टे बल में मौजू द Delete Command टे बल में मौजू द
Record को या टे बल के Structure को िभी Data/Record को एक िाथ record को one by one (एक-एक
हमे शा के वलए Remove कर दे ती है | Remove कर दे ती है , जबवक टे बल का करके) Remove कर दे ती है |
Structure same exist रहता है |
Drop Command मे मोरी िे Table Truncate Command टे बल Space Truncate Command टे बल Space
Space को Free कर दे ती है | को Free नही करती है , यानी Table को Free नही करती है |
क्योवक Drop Command टे बल के Space same रहता है |
Structure को Remove कर दे ती है | क्योवक Truncate records को delete
करती है , Structure Same रहता है |
ROLLBACK Possible नही होता है | ROLLBACK Possible नही होता है | ROLLBACK Possible होता है |

Where Clause का उपयोग नही कर Where Clause का उपयोग नही कर Where Clause का उपयोग कर िकते
िकते है | िकते है | है |
Truncate, Delete िे Fast होती है | Delete, Truncate िे Slow होती है |
Truncate ‚Log‛ Maintain नही करता Delete ‚Log‛ Maintain करता है , जो
है | की एक Delete वकए हुए Data को
Recovery में Help करता है |
जै िे- Recycle bin की तरह, वजिे हम
ROLLBACK करके record को िापि
ला िकते है |
Truncate Command Table िे data Delete Command table के record
को remove करने िे पहले Data Page को Lock करती है |
को Lock करती है |
Truncate Data Page को Delete एक िाथ एक record को
Deallocation करती है | Remove करती है , तथा Transaction
Log में Entry करती है |
Truncate Command Trigger को Delete Command Trigger को
Active नही करता है | Active करता है |
4. Select Command
 Select statement used to select a set of data from a database tables.

Select statement का use database table िे data के िमू ह को select करने के वलए करते है |

 Select used to retrive data from a database table.

Select database table िे data को retrive यानी Fetch करता है |

 Select is also called DQL (Data Query language) database it’s used to ‚Queries Information‛ from a
database table.

Select को DQL (Data Query language) भी कहा जाता है , क्योवक यह database table िे data को Fetch करता
है |

नोट- Select Command िे हम data को केबल Display ही कर िकते है , उिमे कोई changes नही कर िकते|
यानी data में कोई Update नही कर िकते|

1. To retrive data from selected field.


Select Command िे हम selected field िे data Fetch करते है |

Stmt >Select column1, column2…… column_n


From table_name

[Where Condition]; (यह Optional होती है | Where Condition नही लगायेंगे तो भी data Fetch हो जायेंगा)

2. To retrive all the field from the tables.


Select Command टे बल िे िभी field को retrive कर िकते है |

Stmt >Select * from table_name;

Student

S_id Name Age City


S1 A 20 Jaipur
S2 B 22 Jaipur
S3 C 25 Jaipur
S4 A 27 Alwar
S5 D 29 Alwar
S6 E 30 Kota
S7 F 22 Kota

SQL >Select S_id, Name from student; (इििे Student टे बल के S_id और Name Show हो जायोंगे)

SQL >Select * from student; (इििे Student टे बल का िारा Data Show हो जायेगा)

SQL >Select City from student; (इििे िभी City नाम के Column Show हो जायोंगे, Duplicate
Value भी Display हो जायेगी)

SQL >Select Distinct City form student; (इििे िभी Duplicate Value Remove हो जायेगी)
City

Jaipur
Alwar
Kota

नोट- SQL में Duplicate Value को Remove करने के वलए Distinct का Use वकया जाता है |

 SQL Operator

1. Arithmetic Operator 2. Comparison Operator


+,- (Unary Operator) = (Equality)

+ != , <> (Inequality)
- (Binary Operator) < , > , <= , >=
* IN, LIKE
/ IS NULL, IS NOT NULL, BETWEEN

3. Logical Operator
AND- Return true if both components condition is true.

OR- Return true if either components condition is true.

NOT- Return true if condition is false.

4. Set Operator
UNION- Return all distinct Row from both queries.

UINION ALL- Return all row from both queries.

INTERSECT- Return all row selected by both queries.

MINUS- Return all distinct Row that is in queries but not in second one.

5. Aggregate Operator
AVG (Average)

MIN (Minimum)

MAX (Maximum)

SUM (Total)

COUNT (Count)
 Aggregate Operator
 Aggregate function performs calculation on set of value and returns a single value.

Aggregate function value के िमू ह पर calculation perform करते है , और single value return करते है |

1. AVG (Average)

Employee

E_id Name D_id Salary


E1 A CS 20,000
E2 B CS 25,000
E3 C EC 30,000
E4 A EC 35,000
E5 D ME 15,000

 िभी Employee की Average Salary

SQL >Select Avg (salary) from employee;

Employee

Avg (salary)
25,000

 हमें उन िभी Employee का नाम Find करना है , वजनकी Salary Average Salary िे ज्यादा है |

SQL >Select Name form employee

Where salary>(select Avg (salary) from employee);

Employee

Name
C
A

2. MIN (Minimum)
 Find out Minimum value of given sets of value.

Value के िमू ह िे Minimum value Find out करनी है |

SQL >Select Min (salary) from employee;

Employee

Min (salary)
15,000

Sunil Yadav Sir - 9694473047


 Minimum Salary िाले employee का नाम Find करना है |

SQL >Select Name from employee

Where salary=(select Min (salary) from employee);

Employee

Name
D

3. MAX (Maximum)
 Find out Maximum value of given sets of value.

Value के िमू ह िे Maximum value Find out करनी है |

SQL >Select Max (salary) from employee;

Employee

Max (salary)
35,000

 Maximum salary िाले employee का नाम Find करना है |

SQL >Select Name from employee


Where salary=(select Max (salary) from employee);

Employee

Name
A

 Find name of employee who are second highest salary.

Second highest salary के employee का नाम Find करना है |

Salary

20,000
25,000
30,000 Find this salary
35,000 Highest salary of this table
15,000

हम इि टे बल की highest salary (35,000) को हटा दें गे|


वफर इि टे बल िे highest salary find करें गे, िही हमारी Second highest salary होगी|
SQL >Select Max (salary) from employee;

 इि Statement िे इि टे बल की highest salary (35,000) display होगी|

SQL >Select Max (salary) form employee


Where salary<>(select Max (salary) from employee);

 इि Statement िे इि टे बल की highest salary (35,000) को छोड़कर बावक िभी salary display होगी|

Salary

20,000
25,000
30,000
15,000

SQL >Select Name from employee


Where salary=(select Max (salary) from employee);

 इि Statement िे इि टे बल की highest salary (30,000) के employee का नाम display होगी|

Employee

Name
C

4. SUM (Total)
 Calculate Sum

Sum को Calculate करता है |

SQL >Select Sum (salary) from employee;

Sum (salary)
1,25,000

5. COUNT (Count)
 No. of Rows count
यह Row की िोंख्या को count करता है |

Employee

E_id E_Name D_id Salary City


E1 A CS 2000 Jaipur
E2 B CS 3000 Jaipur
E3 A CS 4000 Alwar
E4 C ME 1500 Alwar
E5 D ME NULL Kota

Sunil Yadav Sir - 9694473047


SQL >Select Count (*) from employee;

Count (*)
5

 यह Statement Total Row को Count करके Display करता है |

SQL >Select Count (salary) from employee;

Count (*)
4

 Null never Count.

इिमें हमने particular Column का Name mention वकया है , इिवलए यह Null को कभी भी Count नही करें गा|

SQL >Select Distinct (count (city) from employee);

Distinct का मतलब यह Duplicate को remove करके Result Display करें गा|

Count (City)
3

 Group by Clause
 Group by clause used for grouping similar data.

Group by clause similar data का group बनाकर result show करता है |

Employee

E_id E_Name D_id Salary


E1 A CS 10,000
E2 B CS 15,000
E3 C CS 20,000
E4 D ME 18,000
E5 E ME 25,000
E6 F ME 35,000
E7 G EC 30,000

 Department by average salary display करना है |

SQL >Select Avg(salary) from employee group by D_id;


या
SQL >Select D_id, Avg(salary) from employee group by D_id;

Employee

D_id Avg (Salary)


CS 15,000
ME 26,000
EC 30,000
नोट- Group by Clause के िाथ वजि भी कॉलम को use वकया जाता है , उिी कॉलम को हम select statement के
िाथ include कर िकते है | नही तो syntax error आयेंगा|

 Having Clause
 Having Clause is used to Group by Clause for filtering group value.
Having Clause group value को filter करने के काम आता है |

 Aggregate function condition statement use having, cannot be used with where clause.

Aggregate function condition statement में having का use वकया जाता है | where clause का use नही कर
िकते|

 The where clause apply condition on selected column and having clause apply condition on group
value.

Where clause को selected column पर apply करते है , और having clause को Aggregate function में
group value पर apply करते है |

 Find Avg (salary) where minimum no. of employee is ≥ 2

हमें टे बल में उन employee की Average salary count करनी वजनमे दो या दो िे ज्यादा employee है | यानी single
employee की salary नही, क्योवक उिकी salary ही उिकी Average salary होगी|

SQL >Select D_id Avg (salary) from Employee


Group by D_id
Having count (*)>=2;
या
Having count (D-id)>=2; (यह भी वलख िकते है )

Employee

D_id Employee
CS 15,000
ME 26,000

 इि Statement िे उि ही की Department की salary Display हुई है , वजनमें दो या दो िे अवधक employee है |

 Order by Clause
 Arrange display result in ascending or descending order.

Result को ascending या descending order में display करने के वलए order by clause का use वकया जाता है |

 Ascending के वलए ASC तथा ascending के वलए DESC का use करते है |

 By default order by result is ascending order.

Order by clause का by default order ascending order होता है |

Type your text


SQL >Select D_id Avg (salary) from employee
Group by D_id
Having count (D_id)>=2
Order by D_id Desc;

D_id Salary
ME 26,000
CS 15,000

 All Clauses Order Sequence


िभी Clause को वकि order Sequence में use वकया जाता है |

1. Select
2. From
3. Where
4. Group by
5. Having
6. Order by

 Set Operator
1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS (-)

Employee Student

E_id E_Name City S_id Name Age


E1 A Jaipur E1 A 20
E2 B Jaipur E2 B 25
E3 C Jaipur E6 F 26
E4 D Jaipur E7 G 28
E5 A Jaipur E8 H 30

SQL >Select E_id, Name from employee


UNION (हम यहाों पर इन चारो में िे कोई-िा भी Operator Use कर िकते है )
>Select E_id, Name from student;

नोट- Set operator को use करने के वलए दोनोों टे बल में Domain same होने चावहए| जै िे की- E_id Varchar है , तो
S_id भी Varchar होनी चावहए|
 Find the employee name who don’t have student.

ऐिे employee का नाम पता करना है , जो student नही है |

SQL >Select E_id, Name from employee


MINUS/EXCEPT (इिमें हम MINUS या EXCEPT का use कर िकते है )

Select S_id, Name from student;

 इि Statement िे employee table के उन employee का नाम display होगा, जो student नही है |

 Logical Operator
1. AND

2. OR

SQL-1 >Select Name, City from employee


Where salary>25000 AND Age>21;

 AND- Both conditions true then display result.


दोनोों condition true होने पर result display होता है |

SQL-2 >Select Name, City from employee


Where salary>25000 OR Age>21;

 OR- Either one condition true then display result.


दोनोों में िे कोई एक condition true होने पर result display होता है |

 Comparison Operator
1. IS NULL/IS NOT NULL
2. IN
3. BETWEEN
4. LIKE

1. IS NULL/ IS NOT NULL


>Select Name, City from employee

Where salary IS NULL/ IS NOT NULL; (इिमें हम NOT NULL या IS NOT NULL का use कर िकते है )

Employee

Salary Name
20,000 A
NULL B
IS NOT NULL 30,000 C IS NULL
NULL D
35,000 E

Sunil Yadav Sir - 9694473047


 IS NULL में ऐिा Data आ जायेगा वजिकी Salary NULL है |

 IS NOT NULL में ऐिा Data आ जायेगा वजिकी Salary NULL नही है , यानी NOT NULL है |

नोट- Database में NULL को compare करने के वलए special operator IS NULL तथा IS NOT NULL का use वकया
जाता है |

2. IN
 Use to compare multiple values.

यह multiple value को compare करने के वलए use वकया जाता है |

 Shorthand OR Operator.
इिे OR Operator का Shorthand भी कह िकते है |

SQL >Select Name from employee


Where salary=10,000 OR salary=20,000 OR salary=30,000
या
SQL >Select Name from employee

Where salary IN (10,000, 20,000, 30,000);

 यह OR Operator का Shorthand होता है | इिमें multiple value को एक िाथ include कर िकते है |

SQL >Select Name from employee


Where City IN (Jaipur, Alwar, Kota);

 Table में वजनकी City Jaipur, Alwar, Kota है , उन िभी employee का नाम display होगा|

SQL >Select Name from employee


Where IN (Select City from employee where salary>50,000);

3. BETWEEN
 To compare rang including both boundary value.
दोनोों boundary value को include करते हुए rang को compare करता है |

SQL >Select Name from employee

Where salary BETWEEN 10,000 AND 30,000;

 वजनकी salary 10,000 िे 30,000 के बीच है , उन िभी employee का नाम display होगा|

नोट- Both boundary value are included.

BETWEEN Operator हमे शा दोनोों boundary value (10,000 और 30,000) को include करके result को display
करता है |
4. LIKE
 Compare string with specific condition.
Specific condition के base पर string को Compare करने के वलए LIKE Operator का use वकया जाता है |

SQL >Select Name, City from employee


Where Name LIKE ‘Sunil %’
‘S %’
‘% Yadav’
‘% Yadav %’
‘S_ _ _%’
‘S % V’

‘S_ _ _’

 ‘Sunil %’– Name start with Sunil (Name Sunil िे start होना चावहए, last में कुछ भी हो िकता है )
 ‘S %’- Name start with ‘S’ (Name ‘S’ िे start होना चावहए)
 ‘% Yadav’- Name End with Yadav (Name Yadav िे End होना चावहए)
 ‘% Yadav %’– Name sub string Yadav anywhere (Yadav नाम कही पर भी होना चावहए)
 ‘S_ _ _%’– Name start with ‘S’ having only four character (‘S’ िे start होना चावहए और Minimum 4
character होने चावहए, Maximum वकतने भी हो िकते है )
 ‘S % V’- Name start with ‘S’ or End with ‘V’ (start ‘S’ िे तथा End ‘V’ िे होना चावहए, mid में कुछ भी हो
िकता है )
 ‘S_ _ _’- Name start with ‘S’ and exactly four character (‘S’ िे start होना चावहए और exact 4 character ही
होने चावहए)

 %- Zero or more character.

% का मतलब यहााँ पर 0 या वकतने भी character में कुछ भी value हो िकते है |

 Under score (_)– Compare single character.


Single character को Compare करने के वलए under score (_) का use वकया जाता है |

 Arithmetic Operator
 Display 10% increment on salary.

SQL >Select Name, salary * 1.1 from employee;

 ऐिे िभी employee वजनकी age 50 िे ज्यादा है , उनकी salary 5000 जु ड़कर (+) होकर display होगी|

SQL >Select Name, salary+5000 from employee

Where age>50;

Sunil Yadav Sir - 9694473047


3. TCL (Transaction Control Language)
1. COMMIT
2. ROLLBACK
3. SAVE POINT

 Transaction का मतलब DML operation जै िे- Insert, Update, Delete यह िभी Transaction की category में
आते है |

 Insert, Update, Delete पर Control TCL (Transaction Control Language) द्वारा वकया जाता है |

1. COMMIT
 Use to save data permanently (Secondary Storage).
Commit का use data को हमे शा के वलए Secondary Storage में save करने के वलए होता है |

 Commit िे पहले िभी Task Local buffer (RAM) में perform होते है , परन्तु commit के बाद Data Hard Disk में
save हो जाते है |

 Commit- RAM में जो task perform होते है , उनको Hard Disk में store करने के काम आता है |

 जब तक Data RAM में होता है , तो हम उिे ROLLBACK कर िकते है | यानी ROLLBACK तभी possible है , जब तक
हम Commit नही करते| Commit करने के बाद ROLLBACK possible नही है |

SQL-1 >Insert into student values (101, ‘Sunil’, 28);

SQL-2 >Update student set age = 30 where S_id=105; यह िभी Task Permanent Save हो जायेगी|

SQL-3 >Delete from student where S_id = 103;


>Commit;

 Commit के द्वारा operation 1, 2, 3 को database file में हमे शा के वलए save वकया जाता है |

2. ROLLBACK
 Discard all operation until last commit.

Rollback last committed transaction के बाद की िभी transaction को वनष्क्रिय कर दे ता है |

SQL-1 >Insert into student values (105, ‘Surbhi’, 25)


>Commit

SQL-2 >Update student set age= 30 where S_id=109;

SQL-3 >Delete from student where S_id = 104; (This operation was performed by mistake)
>ROLLBACK

 Rollback Discard operation 2, 3 even after only mistake 3 operation.

Rollback Last Commit के बाद के यानी operation 2, 3 को वनष्क्रिय (Discard) कर दे गा| चाहे mistake 3
operation के बाद में है |
3. SAVE POINT
 Use to mark transaction so that particular transaction can be rollback.

Save point, transaction को mark करने के काम आता है , वजििे की हम selected transaction को ही Rollback
कर िके|

>Commit
SQL-1 >Insert into student values (106, ‘Sonu’, 28);
>Save Point abc;

SQL-2 >Update student set age=30 where S_id=105; (यह operation गलती िे perform हो गया)

>Rollback abc;

 Only transaction 2 discard

केबल transaction 2 ही वनष्क्रिय होगी|

4. DCL (Data Control Language)


1. GRANT
2. REVOKE
 Create new database user.
Database में new user कैिे create होता है |
Stmt >Create user user_name identified by password;

SQL >Create user ABC identified by ‘1234’;

 इि Statement के द्वारा ABC नाम का यूजर create होता है , वजिके password 1234 है |

1. GRANT
 To allocate database user permission.
Database के user को permission दे ने के वलए हम Grant का use करते है |

Stmt >Grant <Privilege list> ON <Relation, view> TO <user, role, public>

नोट- Privilege list में हम Select, Execute, All की permission दे िकते है |

Select- इिमें हम read करने यानी की display करने की permission दे िकते है |


Execute- इिमें हम Insert, Update, Delete की permission दे िकते है , और कोई एक permission भी दे िकते
है |

All- इिमें हम िभी permission दे िकते है |

SQL-1 >Grant select ON student TO ABC;

 To allocate permission to ABC user on student table to display data.

इि Statement िे ABC नाम के यूजर को student table पर data read करने यानी की data display करने की
permission दी गई है |

Sunil Yadav Sir - 9694473047


SQL-2 >Grant All ON student TO ABC;

 To allocate permission to ABC user on student table for perform all operation.

इि Statement िे ABC नाम के यूजर को student table पर िारे operation perform करने की permission दी
गई है |

SQL-3 >Grant select ON student TO PUBLIC;

 All Database user getting permission to display data on student table.

इि Statement िे Database के िभी यूजर को Student table का data display करने की Permission grant की
गई है |

नोट- जब हमें Database के िभी User को Common Permission दे नी होती है , तो हम PUBLIC का use करते है |

 Role
 Collection of permission
Role Permission का Collection होता है |

 Role कैिे Create करते है |

Stmt >Create role role_name

SQL >create role HOD;

 इििे HOD नाम का role create होता है |

 To allocate permission on role


Role को permission allocate कैिे करते है |

SQL >Grant All ON student TO HOD;


>Grant Select ON Finance TO HOD;
>Grant Execute ON exam TO HOD;

 इि िभी statement िे HOD नाम के Role को तीन प्रकार की permission allocate की गई है |


 इिके बाद हमें जो भी HOD की Post पर आयेगा, उिको हम HOD Role Allocate कर दें गे|

 To allocate role on user.

वकिी यूजर को role कैिे allocate करते है |

Stmt >Grant role_name to user_name;

SQL >Grant HOD to Sunil; (Sunil- User name, HOD- Role name)

 Sunil user getting all permission that is allocated to HOD Role.


इि statement िे HOD Role को वजतनी भी permission दी गई है , िो िभी permission Sunil नाम के यूजर को
वमल जायेगी|

 हमने यहााँ पर Sunil नाम के यूजर को Role allocate कर वदया, वजििे हमें तीन प्रकार की permission नही दे नी पड़ी|
Role permission का collection होता है |
2. REVOKE
 To deallocate permission on database user.

Database में यूजर िे permission िापि ले ने के वलए Revoke का use वकया जाता है |

Stmt >Revoke <Privilege list> ON <relation, view> FROM <user, role, public>

SQL-1 >Revoke Select ON student FROM ABC;

 इि Statement िे ABC नाम के यूजर िे student table पर data display करने की permission िापि ली गई है |

SQL-2 >Revoke All ON student FROM ABC; िापि

 इि Statement िे ABC यूजर िे student table के िभी operation perform करने की permission िापि ली गई
है |

SQL-3 >Revoke Select ON student FROM PUBLIC;

 इि Statement िे database के िभी यूजर िे student table पर data display करने की यानी data read करने
की permission िापि ली गई है |

Sunil Yadav Sir - 9694473047


DDL (Data Definition Language)
DDL िे हम table के structure पर काम करते है , जै िे- database create करना, Table create करना|

CREATE Create Command के द्वारा हम Database तथा Table Create करते है |

ALTER ALTER Command के द्वारा हम टे बल में नया कॉलम Add कर िकते है |


टे बल में मौजू द (Existing) कॉलम हटा (Drop) कर िकते है |
टे बल में मौजू द कॉलम का Data Type Change कर िकते है |
टे बल में मौजू द कॉलम का Size Change कर िकते है |
टे बल के कॉलम का Name Change कर िकते है |
टे बल का Name Change कर िकते है |
टे बल के कॉलम पर Constraint लगा िकते है | (यानी कौनिा कॉलम Primary Key होगा)
DROP DROP Command टे बल में मौजू द िभी Record को तथा Table के Structure को हमे शा के वलए
Delete कर दे ती है |
DROP Command के द्वारा Delete की गई Table के Data और Structure को हम बाद में
Recover नही कर िकते है |
TRUNCATE TRUNCATE Command टे बल में मौजू द िभी Record को एक िाथ Delete करती है जबवक
Table का Structure Delete नही होता है |
DML (Data Manipulation Language)
DML में हम Table के Actual Data पर Operation Perform करते है यानी की डाटा में Changment (बदलाि) करने के वलए
उपयोग में ले ते है |

INSERT INSERT Command के द्वारा हम Table में नया Record Insert करते है |

UPDATE UPDATE Command के द्वारा हम टे बल में मौजू द Record/ Records को Modify कर िकते है |
यानी एक या एक िे ज्यादा Records को Modify कर िकते है |
DELETE DELETE Command के द्वारा हम टे बल में मौजू द Record/ Records को Delete करते है |
DELETE Command के द्वारा हम टे बल में मौजू द Record को One-by-One Remove करती है
यावन Row-by-Row Remove करती है |
SELECT SELECT Command के द्वारा हम Database में Table िे Data को retrive (Fetch) करते है , यानी
read करते है |
SELECT Command के द्वारा हम Data को केबल Display ही कर िकते है , उिमे कोई बदलाि
नही होगा| यावन Data में कोई Update नही कर िकते है |

TCL (Transaction Control Language)


Insert, Update, Delete करना Transaction की Category में आते है |

COMMIT COMMIT के द्वारा हम Transaction को Database की Table में हमे शा (Permanent) के वलए
Store करते है या स्थाई रूप िे save करने के वलए करते है |
ROLLBACK ROLLBACK के द्वारा हम Last Committed Transaction के बाद की िभी Transaction को
Revert कर दे ते है यानी की Undo का काम करता है |
SAVE POINT SAVE POINT िे हम Transaction को Mark करते है , वजििे की हम Select Transaction को ही
ROLLBACK कर िके|

DCL (Data Control Language)


GRANT GRANT िे हम Database के User को Permission Allocate करते है |

REVOKE REVOKE िे हम Database में User की Permission को बापि ले ते है |

Aggregate Function Set Operator Logical Operator Comparison Operator


Avg Union AND NOT
Min Intersection OR IN
Max Minus BETWEEN
Sum LIKE
Count

Sunil Yadav Sir - 9694473047


 Transaction and Concurrency Control
 What is transaction?
 Transaction is a sets of operation (instruction) which perform a single logical unit of work.

Transaction बहुत सारे operation का समू ह होती है , जो single logical unit की तरह perform करता है | जैसे-
लोगोों के द्वारा अपने बैंक अकाउों ट से पैसा जमा करना और पैसा ननकालना|

 Transactions generally perform database change.

Transaction सामान्यत: database को change करती है |

 All DML operation (insert, update, delete) are categories of Transaction.


सभी DML operation (insert, update, delete) Transaction की category में आते है | अथाा त् सभी Transactions
को DML operation द्वारा perform नकया जाता है |

Account A B

2000 3000 (कोई A/c A से A/c B में money transfer करना है )

I1 R (A) (First Instruction में हम A/c A को read कर रहे है )

I2 A= A-1000 (I2 में हम A/c A से 1000 withdrawal कर रहे है )

(Sets of I3 W (A) (I3 में हम A/c A को write कर रहे है | 2000-1000


Instruction)
=1000)
यानी एक
Transaction I4 R (B) (I4 में हम A/c B को read कर रहे है )
में बहुत सारे
I5 B= B+1000 (I5 में हम A/c B में 1000 deposit कर रहे है )
Instruction
काम करते है | I6 W (B) (I6 में हम B को write कर रहे है , नजससे A/c B में
3000+1000=4000 हो जाए)
I7 Commit

 ACID Properties
एक Transaction को कैसे सफलतापूर्ाक Handle करना है | यह ACID Properties ननधाा ररत करता है |

 Transaction की 4 Properties होती है|

A- Atomicity

C- Consistency

I- Isolation

D- Durability
1. Atomicity
 Either all instruction executes successfully or none of instruction execute.

(Either all or none)

या तो transaction की सभी instruction proper execute होनी चानहए, या transaction की कोई भी instruction
execute नही होनी चानहए|

(या तो सभी या कुछ नही)

 Atomicity यह ननधाा ररत करता है | नक अगर कोई transaction हो रहा है , तो र्ह transaction होगा, या तो नही होगा|
अथाा त् transaction एक ही step में हो जाना चानहए| यनद transaction अधूरा रह गया तो Rollback हो जाना चानहए|

Account A B

3000 2000

I1 R (A) (A/c A को Read)

I2 A= A-1000 (A/c A में से 1000 withdrawal)


Rollback
I3 W (A) (A/c A को write, 3000-1000=2000)

I4 R (B) (A/c B को read)

I5 B= B+1000 (A/c B में 1000 deposit)

I6 W (B) (A/c B को write, 2000+1000=3000)

Failure occur

 यहाों Commit से पहले ही Failure occur हो गया है , तो सारे Task को र्ापस से Rollback करना पड़े गा| क्योनक
Atomicity का यही Rule है | नक या तो सभी Transaction Execute होने चानहए, या तो इनमे से कोई भी Transaction
execute नही होना चानहए|

नोट- Transaction management component performs atomicity.


Atomicity को Transaction management component, perform करता है |

2. Consistency
 Before transaction start and after transaction complete database must be consistent state.
Transaction start होने से पहले और transaction complete होने के बाद database consistent state में होना
चानहए| यानी Transaction start से पहले और transaction complete होने के बाद amount का sum, Equal होना
चानहए|

 Consistency यह ननधाा ररत करता है | नक जब कोई Transaction होता है , तो Database एक State से दु सरे State में
Consistence होना चानहए| जै से- अगर एक खाते से दु सरे खाते में Transaction होता है , तो Transaction Start होने से
पहले दोनोों खातो की रानि तथा Transaction Complete होने के बाद दोनोों खातो की रानि Total के बराबर होनी
चानहए|
Account A B

3000 5000

SUM=8000

I1 R (A) (A/c A को Read)

I2 A= A-1000 (A/c A में से 1000 withdrawal)

I3 W (A) (A/c A को write यानी इसे save कर नदया, 3000-1000=2000)

I4 R (B) (A/c B को read)

I5 B= B+1000 (A/c B में 1000 deposit)

I6 W (B) (A/c B को write नजससे B में 5000+1000=6000 हो गए)

I7 Commit

A=2000 B=6000

SUM=8000

 इसमें Transaction start होने से पहले SUM 8,000 था, और Transaction complete होने के बाद भी 8,000 है | यानी
data Consistent state में है |

3. Isolation
T1 T2 T3 Transaction T1 complete होने के बाद Transaction T2 start होती है |

T1 Transaction T2 complete होने के बाद Transaction T3 start होती है ,

T2 तो यह Serial Transaction कहलाती है |

T3

Serial Transaction

T1 T2 T3

Parallel Execution

 All parallel transaction need to convert serial form for execution.

सभी parallel transaction को serial form में convert करके execute करना चानहए| नजससे की database
inconsistence नही हो|

T1 के execution से T2 पर कोई effect नही पड़ना चानहए| यानी की parallel transaction एक दु सरे पर effect नही
करनी चानहए| इसनलए इसको serial form में convert करके execute करना चानहए| नजससे database
inconsistence नही होगा|
 Isolation यह ननधाा ररत करता है | नक अगर दो या दो से अनधक transaction एक साथ हो रहा है | तो उन सभी
transaction का प्रभार् एक दु सरे transaction पर नही पड़ना चानहए| अथाा त् सभी transaction अलग-अलग होना
चानहए|

 Concurrency Control Component Maintain Isolation.


Isolation को Concurrency Control Component, Maintain करता है |

4. Durability
 Database all change are permanent.
Database के सारे change हमे िा के नलए होने चानहए| यानी नक database के नकसी भी प्रकार के failure होने पर
data lost नही होना चानहए|

 Successful transaction data persist all time.


Successful transaction data हर समय बना रहना चानहए|

 All successful transaction persist their data after any type of failure occur (h/w and s/w).
नकसी भी प्रकार का failure (h/w and s/w) occur होने पर सभी successful transaction नमलनी चानहए|

 Durability यह ननधाा ररत करता है | नक एक बार database में कोई data हमे िा के नलए save हो जाता है , तो बाद में
नकसी भी प्रकार का failure (h/w and s/w) occur हो जाता है , तो र्ह data हमें successful नमलना चानहए|

 Recovery Management Component Maintain Durability.


Durability को Recovery Management Component, Maintain करता है |

Sunil Yadav Sir - 9694473047


 Transaction State/Transaction Life Cycle

RAM CPU
HDD

Active State- Active State प्रत्येक transaction का प्रारों नभक State होता है | इसमें transaction एक Active
State में प्रर्ेि करता है | यानी की HDD का data RAM में enter करता है , और डाटा पर read/write operation
perform होता है |

Partially Committed State- Active State में पूरा read/write operation ख़त्म हो जाता है , तब transaction
इस Partially Committed State में प्रर्ेि करता है | इस state में डाटा buffer memory (RAM) में store होता है |

Committed State- Transaction जब Partially Committed State में successfully commit हो जाता है | अथाा त्
जब कोई नसस्टम fault या software error नही आता है , तो transaction इस Committed State में आ जाता है , और
database में जो भी change होता है | र्ह database (HDD) में हमे िा के नलए store हो जाता है |

Failed State- अगर Active State में या Partially Committed State में कोई नसस्टम fault या software error आ
जाता है , तो transaction इस Failed State में आ जाता है |

Aborted State- जब Transaction faild (नर्फल) हो जाता है , तब Failed State से Aborted State में चला जाता है ,
और buffer memory में stored डाटा delete हो जाता है , या Rollback हो जाता है | यानी Transaction को Kill या
Restart करना है |

Terminated State- जब Transaction seccessfully commit हो जाता है , या Abort हो जाता है | तब Terminated


State में चला जाता है , और Transaction ख़त्म (Kill) हो जाता है |

Sunil Yadav Sir - 9694473047


 Scheduling
Multiple Transaction का execution sequence scheduling कहलाता है |

Serial Schedule Parallel Schedule


(Concurrent Transaction)
Serial Schedule एक प्रकार की Schedule है , जहाों एक
ले न-दे न दु सरे ले नदे न को िुरू करने से पहले पूरी तरह से
T1 T2 T3
execute होता है |
Serial Schedule में जब पहला ले न-दे न अपना cycle पूरा
करता है , तभी अगले ले नदे न को execute नकया जाता है | Parallel Schedule/ Concurrent Transaction में
जैसे- transaction individual operations को execute कर
T1 T2 T3 सकता है | जै से-
Transaction T1 complete होने के बाद Transaction Transaction T1 से Transaction T2 पर switch हो गए,
T start होगी| Transaction T complete होने के बाद नफर T2 से T3 पर नफर T1 पर switch हो गए|
2 2

Transaction T3 start होगी| इससे कभी भी इससे multiple transaction parallel execution हो रहे
inconsistency नही होती है | है | इससे perform काफी हद तक बढ़ जाती है |

Advantage Advantage
Consistency Maintain करता है | Waiting time- Low (कम हो जायेगा)
Response time- Low
Resourse Utilization- High (बढ़ जायेगा)
Throughput- High

Disadvantage Disadvantage
Waiting time- Increase (बढ़ जाता है ) Database may be inconsistent.
Response time- Increase Database inconsistent हो भी सकता है |
Resourse Utilization- decrease (कम हो जाता है )
Throughput- decrease

 Throughput- No. of Transaction per unit time.


एक Unit Time में नजतनी No. of Transaction Complete होती है | र्ह उसका Throughput कहलाता है |

 Problem Because of Concurrent Execution.


Concurrent execution के कारण कौन-कौन सी problem आ सकती है |

1. Wr (Write/Read) Problem Direct Read Problem


2. RW (Read/Write) Problem Unrepeatable Read Problem
3. WW (Write/Write) Problem Phantom Read Problem
4. Irrecoverable Problem
5. Cascading Rollback Problem
6. Lost Update Problem
 Shared Resourse जब दो या दो से ज्यादा Transaction एक साथ Read
A=10 करती है , तो कोई Problem नही आती है |

T1 T2 जब एक Transaction Read करे गी और दू सरी


Transaction Write करे गी, तो Problem Create होगी|
R (A) R (A) यानी Dirty (गलत) data Read करे गी|
R (A) W (A)
दोनोों Transaction Write करे गी तो दोनोों Transaction
Perform Using W (A) R (A) Overwrite करे गी और data को lose कर दे गी|
Problem occur
Serialzability
W (A) W (A) इसनलए हमें इन सभी Task को Serialzability के द्वारा
Perform करना चानहए|

1. Direct Read Problem


A=10

T1 T2
R (A)
W (A)
R (A)
W (A)
Commit
‘’
‘’
Failure

2. Unrepeatable Read Problem


A=1

T1 T2
R (A)

R (A)

W (A)

R (A)
3. Write/Write Problem
A=10

T1 T2
R (A)

R (A)

W (A)

‘’ W (A)

‘’ Commit

Commit

4. Phantom Read Problem


A=10

T1 T2
R (A)

R (A)

Delete (A)

Commit

R (A) Value not exists

5. Cascade Rollback Problem/Multiple Rollback Problem


A=100

T1 T2 T3 T4 T5

R (A)

W (A)=50

R (A)=50

Rollback R (A)=50

R (A)=50

R (A)=50

Failure

Sunil Yadav Sir - 9694473047


 Disadvantage
 Wastage of CPU Execution Time

 Wastage of Resourse Utilization

Note- To Avoid Cascade Rollback Uncommitted Transaction cannot Permission to read any other
transaction.

 Schedule
 Exexution sequence of two or more transaction.

दो या दो से ज्यादा Transaction का Exexution Sequence ‚Schedule‛ कहलाता है |

S S’

T1 T2 T3 T1 T2 T3

R (A) R (A)

W (A) W (A)

R (A) ≅ R (A)

R (B) W (A)

W (A) R (B)

W (B) W (B)

Sunil Yadav Sir - 9694473047


 Serialzability
 नकसी भी Transaction को Serializable Proof करने के नलए हम Parallel Transaction के Equivalent Serial
Transaction Drow करते है | नजसे हम Serialzability कहते है |

 Serialzability दो प्रकार की होती है |


1. Conflict Serialzability
2. View Serialzability

T1 T2 T1 T2
R (A) R (A)

W (A) W (A)

R (A) R (A)

W (A) W (A)

T1 T2 – Serial Execution T2 T1 – Serial Execution

 Two Transaction के नलए Serial Execution Sequence

2!= 2×1=2 होोंगे|

1. T1 T2

2. T2 T1

Concurrent Execution Serial Execution Sequence

T1 T2 T3 T1 T2 T3
R (A)
T1 T3 T2
R (A)
T2 T3 T1
W (A) 3! = 3×2= 6
T2 T1 T3
R (A)

W (A) T3 T1 T2

W (A) T3 T2 T1

Execution Sequence

T1 R (A)

T2 R (A), W (A)

T3 R (A)

T1 W (A)

T3 W (A)
1. Conflict Serialzability
 A schedule is called conflict serialzability if we can transform equivlaent of serial schedule by
exchange non-conflict operation.

यनद हम Non-Conflict Operation का आदान-प्रदान (exchange) करके Serial Schedule के Equivlaent बदल
सकते है , तो एक Schedule को Conflict Serialzability कहा जा सकता है |

 Non-Conflict Pair

T1 T2 T1 T2

R (A) R (A) R (B)

R (A) R (A) W (B)

W (A) R (B)

W (A) W (B)

 Conflict Pair

T1 T2

R (A) W (A)

W (A) R (A)

W (A) W (A)

 हम Conflict Pair को Exchange नही कर सकते है , परन्तु Non-conflict Pair को Exchange कर सकते है |
 Read का Conflict Pair Write होता है | जबनक Write के Conflict Pair Read और Write दो होते है |

Ex-1

S S’

T1 T2 ≅ T1 T2
R (A) R (A)
Conflict Pair
W (A) W (A)

R (A) R (B)

W (A) R (A)

R (B) W (A)
Non-Conflict Pair

Sunil Yadav Sir - 9694473047


T1 T2 T1 T2
R (A) R (A)

W (A) W (A)

R (A) R (B)

R (B) R (A)

W (A) W (A)

Proof Serial Execution- T2 T1

 Solve by method-2
 Check all conflict pair between transaction and draw edge.

 यह Method Precedence Graph पर Based है |

Ex-2

T1 T2
R (A)
Precedence Graph
Conflict Pair
W (A)
T1 T2
R (A)
No cycle or loop exists.
W (A) Then this Schedule is Conflict serialzability

R (B)

Ex-3

T1 T2 T1 T2
R (x) R (x)

W (x) W (x) Conflict Pair

R (y) R (y)

W (y) W (y) Conflict Pair


W (z) W (z)

W (z) W (z)

Precedence Graph

T1 T2

Loop exists

Then this Schedule is Non-Conflic Serialzability


Ex-4

T1 T2
R (A)

W (A)

R (A)

W (A)

R (B)

W (B)

R (B)

W (B)

T1 T2
R (A)
Precedence Graph
W (A)
T1 T2
R (A)

No loop or cycle exists


W (A)
This Schedule is Conflict serialzability.
R (B)
Consistent Schedule
W (B)

R (B)

W (B)

 If any schedule having Conflict serialzability then that is always in consistent.


यनद कोई भी schedule Conflict serialzability है , तो र्ह हमे िा consistent होगा|

 If schedule is Non-Conflict serialzability then that schedule may or may not consistent.

यनद schedule Non-Conflict serialzability है , तब र्ह schedule consistent हो भी सकता है , और नही भी| यानी
हम कुछ भी नही कह सकते|

Sunil Yadav Sir - 9694473047


Ex-5

T1 T2 T3 Precedence Graph
R (A)
T1 T2
R (B)
T3
R (A)
No loop or cycle exists
R (B)
This schedule is Conflict serialzability.
R (C)
Consistent schedule
W (B)

W (C)

R (C)

W (A)

W (C)

 इसमें 6 Serial Execution Sequence हो सकते है |

T1 T2 T3

T1 T3 T2

T2 T1 T3

T2 T3 T1

T3 T1 T2

T3 T2 T1

 Serial Execution Sequence ननकालने के नलए In-degree check करनी होती है |

T1 T2 In-degree 0 vertex= T2

T3 In-degree 0 vertex यानी नजस Transaction की तरफ कोई भी Edge नही आ


रही होती है |

जै से- T2 में कोई edge नही आ रही, यानी T2 में In-degree vertex है | इसनलए हम
T2 को graph से हटा दें गे|

T1 Next In-degree 0 vertex= T3

T2 T3 T1 (यह इसका Execution Serial Sequence है )


T3
Ex-6

T1 T2 T3 T4 T1 T2
R (A)
T3 T4
R (A)
No loop or cycle exists.
R (A) यह Schedule Conflict serialzability है ,

W (B) तो यह Consistent है |

W (A)

R (B)

W (B)

Ex-7

T1 T2 T3 T1 T2
R (A)
T3
W (A)
Loop Exists.
W (A) Non-Conflict serialzability है |

W (A)

 This schedule checks using view serialzability for consistency.

यह schedule view serialzability का उपयोग करके consistency check करता है |


2. View Serialzability

S S’

T1 T2 T3 ≅ T1 T2 T3
R (A) R (A)

W (A) W (A)

W (A) W (A)

W (A) W (A)

Parallel/Concurrent Schedule Serial Schedule

 Intial Read
S A S’ Schema S में A एक ही variable है | Schema S में A को सबसे पहले T1
T1 T1 ने Read नकया| और S’ में भी सबसे पहले Read T1 ने नकया|
दोनोों Intial Read Equal है | यानी यह Satisfied है |

 Final Write
S A S’ Schema S में Final Write T3 कर रहा है | और S’ में भी Final Write T3
T3 T3 कर रहा है | यानी यह भी Satisfied है |

 Intermediate Read Intermediate Read अथाा त् Inbetween (बीच में ) कोई Read हो रहा हो|

इसमें कोई Intermediate Read नही हो रहा है | यानी यह भी Satisfied है |

 Schema S is view serialzability= Schema S is Consistent.


 हमने यह Proof कर नदया की Schema S और Schema S’ view serialzability के equivalent है |

 यनद कोई Schedule Conflict serialzability है , तो र्ह


view serialzability भी होगा, और र्ह Consistent
Conflict Serialzability होगा|

View Serialzability  यनद कोई Schedule view serialzability है , तो र्ह


Conflict serialzability होगा या नही, यह कह नही
Consistent
सकते| परन्तु र्ह Consistent होगा|
Ex.

T1 T2 Conflict Serialzability
R (a)
T1 T2
W (a)
No Loop Exists
R (a) यह Conflict Serialzability है |

W (a)

R (b) v

W (b) T

R (b) J

W (b)

T1 T2 View Serialzability
R (a)
Intial Read
W (a)
T1 a T1
R (b)
T1 b T1
W (b) Final Write

R (a) T2 a T2

W (a) T2 b T2

R (b) Intermediate Read

W (b) यह भी Satisfied है |

 This schedule is Conflict Serialzability and View Serialzability= Consistent


 Recoverable Schedule
Ex. S
Database- A=200
T1 T2
R (A) Local Buffer (RAM)- A= 200
A= A+10 T1= A=210
W (A) T2= A=210-50= 160

‘’ R (A) Database= 160

Rollback ‘’ A= A-50 इसके बाद T2 ने Commit कर नदया, तो database की Value


160 हो जायेगी|
‘’ W (A)
T1 के Commit से पहले T1 fail हो गयी|
‘’ Commit
अब हमें Rollback करना पड़े गा| Rollback का मतलब है , की
Failure Database में जो Original data था उसको update कर दो|

Commit T1 के नहसाब से Database में A= 200 था|


Database= A=200
T2 के Task को हम Recover नही कर सकते, क्योनक इसने
Commit कर नदया, और Commited Transaction को र्ापस
से Recover नही नकया जा सकता|

नोट- This Schedule S is irrecoverable Schedule because transaction T2 data not possible to recover.

यह Schedule S irrecoverable Schedule है , क्योनक Transaction T2 के data को हम recover नही कर सकते है |

 Example of Recoverable schedule.


Ex-1 S Ex-2 S

T1 T2 T1 T2
R (A) R (A)
A= A+10 A= A+10
W (A) W (A)
Commit
‘’ R (A)
R (A) ‘’ A= A-50
A= A-50
‘’ W (A)
W (A)
Commit
Commit
Commit
 Both Example are Recoverable Schedule
दोनोों Example Recoverable है |

 इस Transaction में Dirty Read Available नही है , इसनलए यह Recoverable Schedule है |


No Dirty Read present= Recoverable Schedule

Sunil Yadav Sir - 9694473047


Ex. S

T1 T2 T3
R (A)

R (A)

W (A)

R (A)

Commit

Commit

Commit

 Schedule S is Recoverable.

 If Dirty Read not present then Schedule is Recoverable.


यनद Transaction में Dirty Read present नही होता है , तो र्ह Recoverable Schedule होता है |

 If Dirty Read present but commit sequence are same as transaction initiate sequence then
recoverable. Else Irrecoverable

यनद Transaction में Dirty Read present होता है , तो दो case बनते है | यनद commit का sequence same र्ही है ,
जो Transaction का sequence है , तो र्ह Recoverable Schedule है | नही तो र्ह Irrecoverable Schedule है |

Ex.
T1 T2 T3 T4
R (A)
R (A)
R (A)
W (A)
R (A)
Commit
Commit
Commit

 Commit Sequence is different

 Schedule S is irrecoverable.
इसमें Commit का Sequence different है | इसनलए यह Schedule irrecoverable होगा|

Sunil Yadav Sir - 9694473047


 Cascadeless Schedule
S

T1 T2 T3 T4
R (A)
W (A)
‘’ R (A)
‘’ R (A)
‘’ R (A)
Failure

 Schedule S are Cascade Rollback

 Example of Cascadeless Schedule


Ex-1 Ex-2
T1 T2 T3 T4 T1 T2 T3 T4
R (A) Commit
W (A) Commit
Commit R (A) Commit
R (A) Commit
R (A)

 Both Schedule are Cascadeless

 Same commit sequence as transactionread dirty data.

 नकसी भी Schedule को Cascadeless बनाने के नलए dirty read की problem को solve करना होता है , यानी
Transaction में dirty read present ना हो|

यनद Schedule में dirty read problem present है , तो commit का sequence हमे िा transaction के initiate
sequence के अनु सार होना चानहए|

 If Schedule are Cascadeless then that Schedule is Recoverable.

यनद Schedule Cascadeless है , तो र्ह Recoverable होगा|


 Strict Schedule
Database A= 10

T1 T2 (नबना Read नकए नकसी Data को Write करते है ,


R (A) तो इसे Blind Write कहते है )
W (A)

W (A) Blind Write

Commit
N
R (A)
N
Commit

Not Strict Schedule

T1 T2
R (A)
Strict Schedule यह कहती है , की दू सरी Transaction को जब तक
W (A) Write मत करने दो, जब तक पहली Transaction Commit नही हो जाये|
Commit W (A) अथाा त् Strict Schedule यह कहती है , की नबना Commit नकये दू सरी
R (A) Transaction को Read और Write की permission नही दे नी है |

Commit

Strict Schedule

 Diagram

Strict

Cascadeles
s
Recoverable

Conflict Seriabzable

View Seriabzable
 Lock Based Protocol
 To achieve consistency- isolation is most important

Consistency को achieve करने के लिए isolation most important है |

 To achieve isolation we are implementing locking protocol.

Isolation को achieve करने के लिए हम locking protocol implement करते है |

 Locks are Two Types


1. Shared Lock
2. Exclusive Lock

1. Shared Lock
Denote- Lock S(Q)

 If transaction T𝑖, obtained shared-mode-lock on data Q


Then T𝑖 can read only data Q.

यलि लकसी Transaction ने shared- lock कर रखा है , तो वह केबि Data Read ही कर सकता है |

2. Exclusive Lock
Denote- Lock X(Q)

 If transaction T𝑖, obtained exclusive-mode-lock on data item Q


Then T𝑖 can Read or Write data item Q.

यलि लकसी Transaction ने exclusive mode में lock कर रखा है , तो वह Data Read और Write िोनोों कर सकता है |

Shared (S) Exclusive (X)

Shared (S) True False

Exclusive (X) False False

एक या एक से ज्यािा Transaction Shared mode में है , यानी एक या एक से ज्यािा transaction Read कर रही है ,
तो इसे हम Allow करे गे| यानी यह True होगी|

अर्ाा त् Shared mode में लकतनी भी transaction एक सार् Read कर सकती है |

जब एक transaction Shared mode में है और िू सरी Exclusive mode में है , तो इसे हम Allow नही करे गे| यह
False होगी|

जब एक transaction Exclusive mode में है और िू सरी भी Exclusive mode में है , तो इसे हम Allow नही करे गे| यह
False होगी|

Sunil Yadav Sir - 9694473047


Ex-1
T1 T2
Lock S(A)
R (A)
Lock S(A)

 Allow both T1 and T2 to read A

A को Read करने के लिए T1 और T2 िोनोों Allow है |

 Both transactions are Shared mode (Reading Mode).


यहाों पर िोनोों Transaction Shared Mode में है , यानी िोनोों transaction Read कर रही है , और यह Shared mode
में Allow होता है |

Ex-2

T1 T2
Lock X(A)
R (A)
W (A)
Lock S(A)
R (A)

 T2 not allowed to Read A


A को Read करने के लिए T2 Allow नही है , क्योलक यहाों पर Exclusive Lock है |

 Disadvantage of Lock Based Protocol


 Inconsistency can be occur.
Inconsistency हो सकती है |

 May not possible for serializable.

यह serializable भी नही हो सकता है |

 May not free from irrecoverable.


यह irrecoverable से free नही है |

 May not free from Cascadeless.


यह Cascadeless से free नही है |

 May not free from deadlock or starvation.


यह deadlock और starvation से free नही है | यानी deadlock और starvation occur हो सकता है |
 Two Phase Locking (2PL)
 This Protocol Ensure Serialzability.

यह Protocol Serialzability को Ensure करता है |

 Growing Phase (Lock)


 Transaction may obtain locks, but cannot release lock.
Growing Phase में Transaction, lock को obtain कर सकती है | lock को Release (Unlock) नही कर सकती है |

 Shrinking Phase (Unlock)


 Transaction may release lock but cannot obtained lock.

Shrinking Phase में Transaction, lock को Release (Unlock) कर सकती है | lock को obtain नही कर सकती है |

Ex.

T1 T2
Lock X(A)
R (A)

Growing W (A)
Phase Lock X(B)
R (B)
W (B)
Shrinking Unlock (A)
Phase Unlock (B)
Lock X(A)
R (A)
W (A)
Unlock (A)

 Two Phase Locking ensure “Conflict Serialzability”

Two Phase Locking “Conflict Serialzability” को archive करता है

 Disadvantage|
 May not free from irrecoverable, Cascadeless or deadlock
Two Phase Locking irrecoverable, Cascadeless और deadlock से free नहीों है |

Sunil Yadav Sir - 9694473047


 Deadlock

T1 T2
Lock X(A) T1 T2
R (A)
W (A)
A B
Lock S(B)
R (B)
Lock X(B)
R (B)
W (B)
Lock S(A)
R (A)

 Conservative 2 Phase Locking

T1 T2 T1 के पास जब A acquired है , तर्ा T2 के पास B acquired है |


जब T1 B को access करना चाहता है तो, T1 को allow नही करें गे,
A B जब तक B फ्री नही होता|

(Not allow T1 if B is not free)


T1 को allow ही मत करो, जब तक B free नही है |

 No hold until all lock acquired by any transaction.

Conservative 2 Phase Locking कहता है की, जब तक सभी lock acquired नही हो जाते, उसे hold मत करने िो|
इससे यह deadlock free हो जायेगा|

 Conflict Serialzable + Deadlcok free

यह Conflict Serialzable और Deadlcok free provide करता है | यानी यह Conflict Serialzable और Deadlcok
free है |

 May not free from irrecoverable or Cascadeless.

यह irrecoverable और Cascadeless से free नही है | यानी इसमें irrecoverable और Cascadeless possible हो


सकता है |

 Strict 2 Phase Locking


 Strict 2 Phase Locking कहता है , लक जब तक Conservative Lock Mode commit नही कर िे ता, तब तक िू सरी
Transaction को allow ही मत करो|

 2 Phase Lock + All Exclusive lock should hold until commit/abort.

Strict 2 Phase Locking मतिब 2 Phase Locking के सार् सभी Exclusive lock को जब तक hold रखो तब तक
वह commit नही कर िे ता|

Sunil Yadav Sir - 9694473047


T1 T2
Lock X (A)

R (A)
W (A)

Unlock (A) Lock S (A)

 Not allowed until T1 is commited.

जब तक T1 Commit नही करती, तब तक T2 को allow मत करो|

 Strict 2 Phase Locking Achieves Conflict Serializable, View Serializable, Recoverable, Cascadeless.

Strict 2 Phase Locking Conflict Serializable, View Serializable, Recoverable, Cascadeless Achieve करती
है |

 But deadlock can be possible.


िे लकन अभी भी deadlock possible है |

 Rigorous 2 PL
 2 Phase Lock + All shared/exclusive lock should hold until commit/rollback.
यहाों पर read/write िोनोों को hold रखना है, जब तक commit या rollback ना हो जाये|

T1 T2
Lock S(A)

R (A)
Unlock(A)

Lock S(A) Not allow T2 until T1 is commit/abord

 Rigorous 2 Phase Locking Achieves Conflict Serializable, View Serializable, Recoverable, Cascadeless.
Rigorous 2 Phase Locking Conflict Serializable, View Serializable, Recoverable, Cascadeless Achieve

करती है |

 But deadlock can be possible.


िे लकन अभी भी deadlock possible है |
 SQL Join Operation
P.K. Student Course F.K.

S. id Name Age C. id C. Name S. id


S1 Sunil 27 C1 DBMS S1
S2 Sanu 28 C2 C.N. S2
S3 Monu 29 C3 OS S2

1. Find the name of student who’s age=29


ऐसे student का नाम Find करना है , जजसकी age 29 हो|

SQL- >select name from student Where age=29;

2. Find the name of student who’s study course “DBMS”

ऐसे student का नाम Find करना है , जो “DBMS” Course Study कर रहा है |

Need data more than one table.


इसके जिए एक से ज्यादा table से data चाजहए| एक से ज्यादा table से data जनकािने के जिए Cross Product करना
पड़ता है |

Student × Course

S. id Name Age C. id C. Name S. id


S1 Sunil 27 C1 DBMS S1
S1 Sunil 27 C2 C.N. S2
S1 Sunil 27 C3 OS S2
S2 Sanu 28 C1 DBMS S1
S2 Sanu 28 C2 C.N. S2
S2 Sanu 28 C3 OS S2
S3 Monu 29 C1 DBMS S1
S3 Monu 29 C2 C.N. S2
S3 Monu 29 C3 OS S2

No. of Column= M+N (3+3=6)


No. of Record= M×N (3*3=9)

 SQL Join
 Cross Product + Some Condition
 Join is a combination of Cross Product follow by selection process (Some Condition).

यजद Cross Product के साथ हम कोई Condition apply करते है , तो वह Join बन जाता है |

 For join two tables minimum one column must be one common on both table.
Join के जिए दोनोों टे बि में कम से कम एक कॉिम common होना चाजहए|

Syntax- A ⨝c B
 Types of Join Operation
1. Inner Join

Table A Table B

 Inner Join Types


 Theta Join/Conditional Join
 Natural Join
 EQUI Join
 Self-Join

2. Outer Join

Table A Table B

 Outer Join Types


 Left Outer Join
 Right Outer Join
 Full Outer Join

 Inner Join
 Inner join contain tuple those satisfied the specific condition.
Inner join में वह tuple होते है , जो specific condition को पूरा (Satisfied) करते है |

 Theta Join/Conditional Join


 Combined tuples from two or more table satisfied the specific condition.
दो या से अजधक टे बि के Combined Tuple, specific condition को satisfied करते है |

Condition- <, >, <=, >=, =, !=

Student Employee

S. id Name Age E. id E. Name Age


S1 A 20 S1 A 22
S2 B 22 S2 D 24
S3 B 24 S3 F 26
SQL >Select name from student, employee
Where Student.S_id=Employee.E_id

AND Student.Age<Employee.age;

Student × Employee

S. id Name Age E. id E. Name Age

S1 A 20 S1 A 22

S1 A 20 S2 D 24

S1 A 20 S3 F 26

S2 B 22 S1 A 22
Student.S_id=employee.E_id
S2 B 22 S2 D 24
Student.Age<employee.age;
S2 B 22 S3 F 26

S3 B 24 S1 A 22

S3 B 24 S2 D 24

S3 B 24 S3 F 26

 Natural Join
 Natural Join performs if both tables having at least one attribute (Column) common on both table.
Natural Join को apply करने के जिए दोनोों टे बि में कम से कम एक कॉिम का नाम same होना चाजहए|

 Attribute (Column) name & Domain must be same


Column का name और Domain same होना चाजहए| तभी Natural Join को apply कर सकते है |

Student Course

S. id Name Age C. id C. Name S. id


S1 A 20 C1 DBMS S1
S2 B 22 C2 C.N. S2
S3 C 25 C3 OS S2

1. Find the name of student whose study any course.


ऐसे student का नाम Find करना है , जो जकतने भी course पढ़ता हो|

SQL-1 >Select name from student, course


Where Student.S_id=Course.S_id;
या
SQL-2 Select * from student Natural Join course;

Sunil Yadav Sir - 9694473047


Natural Join

S. id Name Age C. id C. Name S. id

S1 A 20 C1 DBMS S1

S2 B 22 C2 C.N. S2

S2 B 22 C3 OS S2

 EQUI Join
 EQUI Join में दोनोों टे बि के कॉिम का नाम different होने पर भी condition िगा सकते है |

अथाा त् EQUI Join में दोनोों टे बि में कॉिम का नाम (Domain) same होना जरुरी नही है |

 When Theta Join use only equivalent (=) condition.

 Equi Join is a special case of Theta Join where use only equivalent (=) condition.
Equi Join, Theta Join का special case होता है , जो equivalent (=) condition पर काम करता है |

Ex. Student ⨝ Course


Student.S_id=Course.S_id

Employee Department

E. id Name City D. id Location E. id


E1 A Jaipur D1 Jaipur E1
E2 B Kota D2 Delhi E2
E3 C Alwar D3 Alwar E2

1. Find the name of Employee whose city or department location is same.

ऐसे Employee का नाम Find करना है , जजसकी city और department location same है |

SQL-1 >Select name from Employee, Department


Where Employee.E_id=Department.E_id

AND Employee.City=Department.Location;

EQUI Join

E. id Name City D. id Location E. id

E1 A Jaipur D1 Jaipur E1

E2 B Kota D2 Delhi E2

E2 B Kota D3 Alwar E2

नोट- EQUI Join can be work different column name.


EQUI Join different column name पर भी काम करता है |
 Self Join
 Special case to join table on itself.

Self Join जकसी टे बि को स्वयों के साथ जोड़ने का एक Special case होता है |

Employee

E. id Name M. id

E1 Sunil E1
E2 Rahul E1
E3 Harish E2
E4 Ravi E1

1. Find the name of Employee who’s manager also.


ऐसे Employee का नाम Find करना है , जो manager भी है |

SQL-1 >Select T1 name from Employee, As T1, Employee As T2


Where T1.E_id=T2.M_id;

Employee- T1 Employee- T2

E. id Name M. id E. id Name M. id

E1 Sunil E1 E1 Sunil E1

E2 Rahul E1 E2 Rahul E1

E3 Harish E2 E3 Harish E2

E4 Ravi E1 E4 Ravi E1
 Outer Join
 Outer join is extension of the join operation.

Outer join, join operation का extension होता है |

 Natural join + Extra information


Natural join + Extra information Outer join होता है |

Outer Join containt matching tuples and some extra information on Left table, Right table or both
table.

Outer Join में Left table, Right table या both table पर matching tuples और कुछ extra information
जमिती है |

 Left Outer Join


 All records from left table + matching records on right table.
Left Outer Join left table के सभी record तथा साथ में right table के matching records रखता है |

Notation- R1 ⟕ R2

Left Right

Student Course

S. id Name Age C. id C. Name S. id


S1 A 20 C1 DBMS S1
S2 B 22 C2 C.N. S2
S3 C 25 C3 OS S3
S4 D 22
S5 E 24

SQL >Select * from Student


Left Outer Join Course

ON Student.S_id=Course.S_id;

Result of Left Outer Join


S. id Name Age C. id C. Name S. id
S1 A 20 C1 DBMS S1
S2 B 22 C2 C.N. S2
S2 C 25 C3 OS S3
S4 D 22 Null Null Null
S5 E 24 Null Null Null
 Left Outer Join में Left Table का सभी data, और Right Table का केबि Matching data आता है |
 Right Outer Join
 All records from right table + matching records on left table.

Right Outer Join right table के सभी record तथा साथ में left table के matching records रखता है |

Notation- R1 ⟖ R2

Left Right

Student Course

S. id Name Age C. id C. Name S. id

S1 A 20 C1 DBMS S1
S2 B 22 C2 C.N. S2
S3 C 24 C3 OS S3
S4 D 20 C4 SAD Null
S5 E 24

SQL >Select * from Student


Right Outer Join Course
ON Student.S_id=Course.S_id;

Result of Right Outer Join

S. id Name Age C. id C. Name S. id

S1 A 20 C1 DBMS S1

S2 B 22 C2 C.N. S2

S3 C 24 C3 OS S3

Null Null Null C4 SAD Null

 Right Outer Join में Right Table का सभी data, और Left Table का केबि Matching data आता है |

Sunil Yadav Sir - 9694473047


 Full Outer Join
 All the tuples from both Left table and Right table.

Full Outer Join दोनोों Left table और Right table के सभी record को रखता है |

Notation- R1 ⟗ R2

Left Right

Student Course

S. id Name Age C. id C. Name S. id


S1 A 20 C1 DBMS S1
S2 B 22 C2 C.N. S2
S3 C 24 C3 OS S3
S4 D 22 C4 SAD Null
S5 E 20

SQL >Select * from Student


Full Outer Join Course

ON Student.S_id=Course.S_id;

Result of Full Outer Join

S. id Name Age C. id C. Name S. id

S1 A 20 C1 DBMS S1
 S2 B 22 C2 C.N. S2 Matching Record

 S3 C 24 C3 OS S3

S4 D 22 Null Null Null Left Table Record



S5 E 20 Null Null Null
 Right Table Record
Null Null Null C4 SAD Null

 Full Outer Join में दोनोों Left Table का और Right Table का सभी data आता है |

नोट- Full Outer Join को हम Full Join भी जिख सकते है |

Sunil Yadav Sir - 9694473047


 Relation Algebra
 Relation Algebra developed by Dr. E. F. Codd in 1970.

Relation Algebra को 1970 में Dr. E. F. Codd ने विकवित वकया था|

 Relation Algebra is a Procedural Language.

Relation Algebra एक procedural language होती है |

 Procedural Language means (What to do + How to do)


Procedural Language का मतलब, हमें इिे बताना होता है की- क्या करना है , और कैिे करना है |

 Relation Algebra also called Formal Query Language.


Relation Algebra को Formal Query Language भी कहा जाता है |

 All SQL (Structure Query Language) commands are based on Relation Algebra.

SQL की िभी command Relation Algebra पर based होती है |

 Relation Algebra is a Theoretical concept and SQL is practical approach based on Relation Algebra
to communication on database.

Relation Algebra एक Theoretical concept है , तथा SQL practical approach है , जो Relation Algebra पर
based है , जो डे टाबेि िे communication करती है |

 Relation Algebra Operation


1. (Projection)
2. 𝜎 (Selection)

3. ×(Cross Product)

4. ρ (Rename)
5. ⋃(Union)
6. ∩ (Intersect)
7.  (Set Different)

8. / , % (Division)

Projection Selection Cross Product

Table में Particular column Table में Particular Record


को Select करना Projection को Select करना Selection
कहलाता है | कहलाता है |

Sunil Yadav Sir - 9694473047


1. Projection ()
 Select column on table.

Table में Column को Select करना Projection कहलाता है |

Student

Roll No. Name City Age


101 Sunil Jaipur 27
102 Sonu Jaipur 29
103 Sunil Jaipur 28
104 Rahul Alwar 29
105 Sonu Jaipur 29

1. Retrive Roll No, Name of all students.

 Roll.No, Name (Student)


SQL- Select Roll.No, Name from student;

Result-

Roll No. Name


101 Sunil
102 Sonu
103 Sunil
104 Rahul
105 Sonu

2. Retrive City on student table.

 City (Student)
SQL- Select distinct city from student;

Result-

City
Jaipur
Show Distinct Result
Alwar

 Relation Algebra by default gives distinct result.

Relation Algebra by default Distinct result दे ता है | यानी result में Duplicate value show नही करता है |

 For unique result in SQL we use Distinct Keyword.

SQL में unique result के वलए Distinct Keyword का use वकया जाता है | अथाा त् यवद SQL में हमें unique result
चावहए, तो हम Distinct Keyword का use करते है |
2. Selection (𝜎)
 When we need to apply condition on table to select Records/Tuples.

Selection के द्वारा हम टे बल पर Condition apply करते है , वजििे की हम Record या Tuple को select कर िके|

Student

Roll No. Name Age


101 Sunil 27
102 Sonu 28
103 Anil 29
104 Rahul 22
105 Harish 25

1. Retrive Name who’s age>28


ऐिे Student का नाम चावहए, वजिकी age 28 िे ज्यादा है |

σ Age>28 (Student)

Result-

Roll No. Name Age


103 Anil 29

 Name (σ Age>28 (Student))


Result-

Name
Anil

SQL- Select Name from Student


Where age>28;

 Selection और Projection Operation को apply करने के वलए हमें िबिे पहले Selection apply करना होता है |
उिके बाद Projection operation को apply वकया जाता है |

 SQL में Selection Operation Where Close के द्वारा implement वकया जाता है |

3. Cross Product (×)


R1 R2

A B C C D
1 2 3 3 9
4 5 6 4 8
3 5 8

Sunil Yadav Sir - 9694473047


R1 × R2
A B C C D

1 2 3 3 9

1 2 3 4 8

4 5 6 3 9

4 5 6 4 8

3 5 8 3 9

3 5 8 4 8

 इिमें R1 Table का प्रत्येक Record R2 Table के दोनोों प्रत्येक िे Multiply होगा|


R1 × R2 Table Columns- R1 + R2 (3+2=5)
R1 × R2 Table Records/Tuples- R1 × R2 (3×2=6)

4. Rename (ρ)
 Change column name temporary on display table.
Rename operation display table में अस्थाई रूप िे कॉलम का नाम Change करने के वलए काम आता है |
अथाा त् इििे original table के कॉलम का नाम Change नही होता है , केबल display होता है |

Student

Roll No. Name City

1. ρ Registration.no, Full Name, Address (Student);

Result-
Student

Registration.no Full Name Address

2. ρ Roll.no, Name, Address (Student);


City को Address में Rename करने के वलए|

3. ρ Registration.no, Name, City (Student);


इििे केबल first कॉलम का नाम rename होगा| यानी Roll.no िे Registration.no rename होगा|
SQL- Select Roll.no As Registration.no,
Name As Full Name,
City As Address,
From student;
 SQL में Rename operation perform करने के वलए As keyword का उपयोग वकया जाता है |

4. Union (⋃)
 The union of two relations is a relation that includes all tuples from first table or second table or
both tables.

Union operation दोनोों table के data को include करके data display करता है , जो की first table में हो या
second table या दोनोों table में |

 Duplicate records are eliminate.


Union operation Duplicate records को eliminate करके data display करता है , यानी distinct result दे ता है |

Ex-1 X Y
x, y, z z, a, b
X⋃Y

x, y, z, a, b

Ex-2 R S

1 A 1 A
2 B 2 E
3 C 3 C
4 D 4 F

R⋃S

1 A
2 B
3 C
4 D
2 E
4 F
Ex-3
Student Employee

Roll No. Name E. id Name


101 Sunil 101 Sunil
102 Anil 105 Ravi
103 Sonu 103 Sonu
104 Rahul 106 Ram

Sunil Yadav Sir - 9694473047


Student ⋃ Employee

Roll No. Name


101 Sunil
102 Anil
103 Sonu
104 Rahul
105 Ravi
106 Ram

SQL-1 Select Roll_no, name, from Student,


Union
Select E_id, Name from Employee;

SQL-2 Select * from Student,


Union
Select * from Employee;

5. Intersection (∩)
 Intersect of two relation is a relation that include only common records on both table.
Intersect operation दोनोों table के common record को display करता है |

Table-1 Table-2

Ex-1 X Y

a, b, c c, d

X⋃Y
c

Ex-2 R S

1 a 1 a
2 b 5 e
3 c 6 f
4 d 4 d

R∩S

1 a
4 d

Sunil Yadav Sir - 9694473047


Ex-3
Student Employee

Roll No. Name E. id Name


101 Sunil 101 Sunil
102 Anil 106 Anil
103 Sonu 107 Ravi
104 Rahul 103 Sonu

Student ∩ Employee

101 Sunil
103 Sonu

SQL-1 Select Roll_no, name, from Student,


Intersect
Select E_id, Name from Employee;

SQL-2 Select * from Student,


Intersect
Select * from Employee;

6. Set Different ()


 Different of two relations is a relation that having those records that are part of first relation but not
a part of second relation.

Set Different में हम उि record को ले ना होता है , जो first table का part होता है , ले वकन second table का part
नही होता है | यानी की दोनोों table के common record को हटाना होता है |

R1 R2

Ex-1 X Y

x, y, z z, a, b

XY
x, y

YX XY≠YX
a, b X Y तथा Y X कभी भी equal नही होता| अथाा त्
दोनोों का result हमे शा अलग-अलग होगा|
Ex-2
Student Employee

Roll No. Name E. id Name

101 Sunil 101 Sunil


102 Anil 103 Sonu
103 Sonu 105 Ram
104 Ravi 106 Ravi

Student  Employee

Roll No. Name


102 Anil
104 Ravi

SQL- Select Roll_no, name, from Student,


Except
Select E_id, Name from Employee;

Employee  Student

Roll No. Name


105 Ram
106 Ravi

7. Division (/, %)
 Division operation display first relation records those fully participate on second relation.
Division operation first table के उन िभी records को display करता है , जो second table में fully participate
हो|

Student Course

S. id C. id C. id

S1 C1 C1
S2 C1 C2
S1 C2 C3
S3 C3
S1 C3

ऐिे Student की S_id चावहए, जो िारे Course पढ़ता है |

Student (S_id, C_id) / Course (C_id)

Student / Employee

S. id
S1
 Relational Calculus
 Relational Calculus is a Non-Procedural language.

Relational Calculus एक Non-Procedural language होती है | इिका मतलब, हमें क्या करना है , इिे यह बताना
होता है | कैिे करना है , यह process नही बताना होता है |

 Relational Calculus is a declarative language.

Relational Calculus एक declarative language होती है |

 Relational Calculus use mathematical predicate calculus.

Relational Calculus mathematical predicate calculus का उपयोग करता है |

 Predicate statement used to either true or false.


Predicate statement true या false का use करते है |

अथाा त् Predicate Statement दो प्रकार के होते है , या तो true statement होता है , या false statement होता है |

 Relational Calculus Types


1. Tuples Relational Calculus (TRC)

2. Domain Relational Calculus (DRC)

Tuples Relational Calculus (TRC) Domain Relational Calculus (DRS)

Work on Tuple/Row Work on Column/Attribute


यह Tuple/Row पर Work करता है | यह Column/Attribute पर Work करता है |
SQL Part
यह SQL का part होता है | या SQL drive वकया जाता है |
{T | R ∈ P (Q)} {(C1, C2, C3) | <a1, a2, a3>}

Tuple Condition Attribute Condition


T- Tuple होता है , जो हमें result के रूप में वमले गा|
P(Q)- Condition होती है |

 Predicates Statements
1. P ∨ Q = P OR Q (दोनोों में िे कोई एक condition true होने पर result true आयेगा)
2. P ∧ Q = P AND Q (दोनोों condition true होने पर result true आयेगा)
3. ⅂ P = Negative P
4. P ⟶ Q
≅⅂P∨Q if P is true the Q must be true. (यवद P true है , तो Q भी true होना चावहए)
If P is false the Q anything. (यवद P false है , तो Q कुछ भी हो िकता है )

Sunil Yadav Sir - 9694473047


⅂P ∨ Q
P Q P⟶Q

True True True

True False False

False True True

False False True

 Quantifiers
1. Existential Quantifier (∃) Some Value (कुछ value true होने पर भी result true हो िकता है )
2. Universal Quantifier (∀) All Value (िभी value true होने पर भी result true हो िकता है )

∃ ℓ ∈ †(Q(ℓ)) “There Exist” a tuple in Relation ℓ such that the predicate Q(ℓ)
यहाों Q(ℓ) condition में कुछ condition true होती है , तो tuple true हो जायेगा|

∀ ℓ ∈ †(Q(ℓ)) “For All” tuple T of relation ℓ such that Q(ℓ)


यहाों Q(ℓ) condition में िभी condition true होती है , तो tuple true हो जायेगा|

∀x P(x) True for every x value then P(x) is true.


यहाों पर x की िभी value true है , तो P(x) Statement true हो जायेगा|

False for some value x then P(x) is false.


यहाों पर x की कुछ value false है , तो P(x) Statement false हो जायेगा|

∃x P(x) True for some value x then P(x) is true.


यहाों पर x की कुछ value true है , तो P(x) Statement true हो जायेगा|

False for every value x then P(x) is false.


यहाों पर x की िभी value false है , तो P(x) Statement false हो जायेगा|

Ex-1 ∀x P(x) ≅ ⅂ ∃x ⅂ P(x)

For all x true then P(x) true No some value x is false then P(x) not be false.
यहाों पर x की िभी value true होने यहाों पर x की कुछ false नही है , तो
पर P(x) Statement true होगा| P(x) Statement भी false नही होगा|

Ex-2 ∃x ⅂P(x) ≅ ⅂ ∀x P(x)

For some value x Not every x


P(x) is false. P(x) is true

Ex-3 ∃x P(x) ≅ ⅂ ∀x ⅂P(x)

For some value x Not every x


P(x) is true. P(x) is false.
Ex-4 ∀x ⅂P(x) ≅ ⅂ ∃x P(x)

For every x Not some x


P(x) is false. P(x) is true.

Ex-5 ∀x (P(x) AND Q(x)) ≅ Not ∃x (Not P(x) OR Not Q(x))

Ex-6 ∀x (P(x) OR Q(x)) ≅ Not ∃x (Not P(x) AND Not Q(x))

Ex-7 ∃x (P(x) AND Q(x)) ≅ Not ∀x (Not P(x) OR Not Q(x))

Ex-8 ∃x (P(x) OR Q(x)) ≅ Not ∀x (Not P(x) AND Not Q(x))

Q-1 Every Student in class study DBMS.


Class में प्रत्येक Student DBMS को study करते है |

If x is student in DBMS
Then x must be study DBMS.

C (x)- Student in Class


S (x)- Study DBMS

1. C(x) ⟶ S(x) If Class(x) is true Then Study(x) must be true.


या
2. ∀x (⅂ C(x) ∨ S(x))

3. ⅂∃x (C(x) ∧ ⅂ S(x))

Q-2 Some student in class study DBMS.


Class में कुछ Student DBMS को study करते है |

C (x)- Student in class


S (x)- Study DBMS

Stmt- ∃x (C(x) ∧ S(x))

Q-3 At least two student in class study DBMS.


Class में कम िे कम दो student DBMS को study करते है |

Stmt- ∃x (C(x) ∧ S(x) ∧ ∃y (C(y) ∧ S(y) x ≠ y))

Some x student in Some y student in


Class study DBMS. Class study DBMS.

Sunil Yadav Sir - 9694473047


1. Tuples Relational Calculus (TRC)
{T | P(T)} T- Tuple Variable
P(T)- Formula (Condition) Where T tuple satisfied.
Retrive Tuple T based on condition P(T)

Ex. {T | T ∈ student ∧ T.age>20}


Retrive all tuple in student table whose satisfied condition age>20

SQL- Select * from student where age>20;

 Free Tuple Variable


 Variable not bounded by quantifier. (∃, ∀)
कोई ऐिा Variable जो quantifier द्वारा bounded नही है | उिे free variable कहते है |
अथाा त् कोई ऐिा Variable जो quantifier के िाथ नही लगता है | िह free variable कहलाता है |
T1 ∈ Student
T2 ∈ Course
Here T2, T1 are free variable.

 Bounded Tuple Variable


 Variable not bounded by quantifier. (∃, ∀)
कोई ऐिा Variable जो quantifier द्वारा bounded हो| उिे bounded tuple variable कहते है |
अथाा त् कोई ऐिा Variable जो quantifier के िाथ लगता है | िह bounded tuple variable कहलाता है |
∀ T1 ∈ Student
∃ T2 ∈ Course

Here T2, T1 are bounded tuple variable.

Q-1 Retrive S_id from Enroll table that some course instructor is Sunil.
Enroll Table िे उि S_id को Retrive करना है | वजिमे कुछ course Sunil द्वारा पढ़ाये जा रहे है |

Enroll Course

S. id C. id C. id Instructor
S1 C1 C1 Sunil
S2 C2 C2 Ravi
S3 C1 C3 Ram
S4 C1
S2 C3
S3 C3

Stmt- {T1. S_id | ∃.T1 ∈ Enroll ∧ ∃.T2 ∈ Course

(T1. S_id=T2. C_id ∧ T2. Instructor=Sunil)}


Q-2 Retrive S_id Enroll at least two course.

उि S_id को Retrive करना है | जो कम िे कम दो course में Enroll हो|

Enroll Course

S. id C. id C. id Instructor
S1 C1 C1 Sunil
S2 C2 C2 Ravi
S3 C1 C3 Ram
S4 C1
S2 C3
S3 C3

Stmt- {T1. S_id | ∃ T1 ∈ Enroll, ∃ T2 Enroll


(T1. S_id=T2. S_id ∧
T1. C_id ≠ C_id)}

Q-3 Retrive S_id Enroll every course.

उि S_id को Retrive करना है | जो प्रत्येक course में Enroll हो|

Stmt- ⊼ S_id, C_id (Enroll) / ⊼ C_id (Course)

Q-4 Consider the following Relation P (X, Y, Z), Q (X, Y, T), R (Y, V)
P Q R

X Y Z X Y T Y V
x1 y1 z1 x2 y1 2 y1 v1
x1 y1 z2 x1 y2 5 y3 v2
x2 y2 z2 x1 y1 6 y2 v3
x2 y4 z4 x3 y3 1 y2 v2

How many tuples will be return on following Relation algebra queries?

𝑥 (P.y=R.y ∧ R.V=V2) (P×R) 𝑥 (Q.y=R.y ∧ Q.T>2) (Q×R)


(a) 0 (b) 1 (c) 2 (d) 3

X Y Z Y V
x2 y2 z2 y2 v2

X Y T Y V X X
x1 y2 5 y2 v3 x2 x1
x1 y1 6 y1 v1 x1
x1 y2 5 y2 v2 x1

Ans. (b)

Sunil Yadav Sir - 9694473047


Q-4 Select operation in SQL equivalent to-

1. The selection operation in Relational algebra.


2. The selection operation in Relational algebra except that select in SQL retains duplicates.
3. The projection operation in Relational algebra.

4. The projection operation in Relational algebra except that select in SQL retains duplicates.

Ans. 4

Q-5 Consider the following Relation-

Student Performance

Roll No. Student Name Roll No. Course Marks


1 Raj 1 Math 80
2 Rohit 1 English 70
3 Raj 2 Math 75
3 English 80
2 Physical 65
3 Math 80

The No. of Row return by SQL queries?

Select S.student_Name, Sum (P.marks)


From student S, performance P
Where S.Roll.No.=P.Roll.No.
Group by S.student_Name

(a) 2 (b) 3 (c) 4 (d) 5

Ans. (a)

Student Name Sum (Marks)

Raj
Rohit

Sunil Yadav Sir - 9694473047

You might also like