DBMS Notes For RPSC Programmer
DBMS Notes For RPSC Programmer
DBMS एक प्रोग्राम का समू ह (सॉफ्टवेयर) होता है , जजसका उपयोग डे टाबेस फाइल पर कुशल (efficient) तरीके से
Operation perform करने के जलए जकया जाता है |
Database System
Database DBMS
Collection of Inter-related data Software used to manage DB file
(DB File को मै नेज करने का सॉफ्टवेर
My SQL
SQL Server
DB 2
DBMS- DBMS is a Application Software.
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 प्रदान करता है |
Note- यह सभी File System की प्रॉब्लम है | इसको DBMS के द्वारा कैसे Solve जकया जाता है | यह हम दे खगें -
Name Father Name Add. Name Father Name Course Fee Name Course Duration
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 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 होता है |
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 के द्वारा जकया जाता है |
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
कहलाता है |
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
DBMS में concurrency Control, Row Level अथवा record level पर होता है , अथाा त् जब कोई यूजर जकसी Particular
record को update करता है , तो DBMS उस Particular Row को ही lock करता है | बाकी सभी Row पर दू सरा यूजर
काम कर सकता है |
नोट:- यजद हमारे फाइल में “N” record है , तो “N” यूजर काम कर सकते है |
File system different level access need to create physically separate file.
File system में different level पर access दे ने के जलए हमें physically separate file create करनी पड़ती है |
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 होता है |
Data insert करते समय हमें rules (जनयम) का ध्यान रखना पड़ता है |
File system में हम rules को Implement नही कर सकते है | जबजक हम DBMS में Implement कर सकते है |
जैिे-
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
Atomicity यह कहती है की या तो Transaction पूरा perform होना चाजहए या कोई भी perform नही होना चाजहए|
Failure की स्थथजत में Transaction revert करना पड़ता है | यह File system में possible नही है |
Isolation Problem
Security Problem
File system में हम अलग-अलग यूजर को अलग-अलग Access नही दे सकते| अथाा त् हम एक यूजर को File का Access
नही दे सकते पूरी File का Access दे ना पड़ता है |
Nibble 4 bit
Ex. A a b
ASCII- 65 97 98
DB File
Database Server
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 भी िहते है |
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 किया जाता है |
Instance- The collection of information stored in database at particular point of time is called
instance.
Database में किसी particular point of time पर जो data मौजू द होता है | वह Instance िहलाता है |
a=30
DB File
Database Server
External Schema, Conceptual Schema तथा Physical Schema से hidden रहती है | अथाा त् हम यूजर िो यह
नही बताते िी आपिा डाटा िौनसी टे बल में स्टोर है | टे बल िे बीच में Relationship क्या है तथा िौनसी Disk पर
Store है |
जैसे- College में Student िे कलए अलग, Faculty िे कलए अलग तथा Dean/HOD िे कलए अलग View होगा|
Conceptual Schema िे द्वारा हम database design िरते है | यानी ER diagram Conceptual Schema पर िाम
िरता है |
Database में वास्तव में DB file (Database file) िैसे store होती है | यह हमें Physical schema बताती है |
Physical schema िे द्वारा हम actual disk पर िाम िरते है | अथाा त् डे टाबेस में present disk िो manage िरते
है |
Storage manager low level data तथा application program िे बीच interface िा िाम िरता है , अथाा त् हम
जो डाटा डे टाबेस में store िरते है | उसे वह low level language में convert िरता है , और actual data file में
जािर store िरता है |
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 किया जाता है |
जब दो या दो से अकधि transaction एि साथ perform होती है , तो यह decide िरता है िी data में किसी भी प्रिार
िी inconsistency नही होनी चाकहए|
CPU
RAM
Database
Database Users.
There are 4 type of Database users
3. Sophisticated User (Analyst) - यह analyst होता है | यह application program तथा direct दोनोों िे द्वारा
database से interact िर सिता है |
4. DBA (Database Administrator) - Central control of both data and program to access those data.
किसी यूजर िो क्या कदखाना है | उसिा पूरा control DBA िे पास होता है |
Routing maintains.
View Level
Inconceptual Level
Physical Level
DB file
Database
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 िहते है |
जब हम 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 नही पड़ता है |
First commercial database- IMS (Information Management System) इसकथ उपयोग airlines management में
ककयथ गयथ|
Parent/ROOT Node (Level-0)
Child (Level-1)
Child (Level-2)
Explain By Diagram-
जै से- E की information D नही दे ख सकतथ यथ उसे नही पतथ होती है | single parent model अर्था त् इसमें child कथ
एक parent होतथ है |
IDS (Intergraded Data Stored) यह पहिथ डे टथबेस मॉडि र्थ, जो network model पर आधथररत र्थ, कजसे
“Charles Bach man” ने develop ककयथ र्थ|
डथटथ हमे र्थ table (row/column) की फॉमे ट में store होनथ चथकहए|
Database में present record की entity एक से ज्यथदथ बथर नही होनी चथकहए|
How will be tables are connect to each other (numbers of field in table)
यह बताता है की table एक दु सरे के साथ कैसे connect होती है | table में जकतनी field होगी यह भी बताता है |
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 में टे बि का नाम बनती है |
Student
DOB Mobile
3. Relationship
Relationship- Association between entities.
E-R Model में दो entity set के मध्य सांबांध (Relationship) को काजडि नजिटी कहते है |
1 Rectangle Entity
2 Ellipse Attribute
3 Diamond Relationship
Entity to Relationship
4 Lines या
Entity to Attribute
Partial Key
10 Ellipse with Dashed Line (Weak entity के जिए बनती है )
Name
Duration
End date
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.
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
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 बनाने की िरुरत पड़ सकती है |
DOB Age
Derived attribute, table के create के time पर कभी भी table का Column नही बनता है |
Age will be calculated based on DOB.
Age को DOB के base पर calculate जकया िाएगा|
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
Name*
िै से- िब हम form fill करते है , तो star mark (*) वािे Required attribute होते है |
7. Optional Attribute
Optional field value may or may not field.
Optional field को user application program में fill कर भी सकते है और नही भी कर सकते है |
Account
Person Bank
Employee
2. Binary Relationship
3. Ternary Relationship
Job
Project
Project id
Name
ID Project
Enroll no.
Guide
Name
Name यह सभी Attribute है |
Salary
DOB
4. n-ary Relationship
E5
E4 R E1
E3 E2
Specialization
Specialization is a Process used to top-down approach.
Specialization Super Class का Set होती है , और उससे related sub class होती है |
Generalization
Generalization is a bottom-up approach.
इसमें Sub class को पहिे define जकया िाता है , उसके बाद super class को define जकया िाता है |
Person
Generalization
Specialization
Bottom-Top ISA
Salary Top-Down
Approach Course Approach
follow करता है |
follow करता
है |
Employee Student
Student Type
ISA
Job
Manager
जबना aggregation के process define करने से Redundant Relationship की problem create होती है |
जैसे- Employee, Branch, Job सभी की दो-दो Relationship है , Work से तथा Manager से Redundant
Relationship की problem create होती है |
Job
Manager
Weak Entity-
Session
Partial Key Time
Room No.
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 होती है |
What is key
Attribute or set of attribute used to identify table records uniquely.
Key एक Attribute या एक से ज्यादा Attribute का समू ह होती है , जो की table में मौजू द record को uniquely
identify करती है |
Ex.
Key
Course
S. id C. id Fee
S1 C1 6000
S1 C2 5000
S1 C3 5000
S2 C1 6000
S3 C2 5000
Use of key
Key is used to uniquely identify table record.
Key table के record को uniquely identify करने के काम आती है |
इसका उपयोग 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
Employee
इसमें E. Name तथा City Candidate key नही बन सकता है , क्योमक Name तथा City मकसी का same भी हो सकता है |
इसमलए इसमें बामक सभी Candidate key बन सकते है | जै से-
Candidate Key- (E. id, Passport No, Aadhar No, PAN No) यह सभी unique होते है तथा null value भी unique
होती है |
Primary key एक candidate key होती है , मजसकी field value कभी भी null नही होती है |
Employee
Primary Key को छोड़कर बामक सभी Candidate Key ही Alternate Key या Secondary Key कहलाती है |
Employee
Candidate Key- (E. id, Passport No, Aadhar No, PAN No)
Ex. R (A, B, 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)
वो सभी attribute जो table के मकसी candidate key से belong नही करते है | non-prime attribute कहलाते है |
Ex. R (A, B, 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)
Non-Prime Attribute- (E. Name, City)
Ex. R (A, B, 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)
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)
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
Super key में Minimum attribute होने की कोई गारों टी नही होती है | अथाड त् मकतने भी attribute ममलकर Super
key बन सकते है |
Candidate Key अथाड त् सबसे बड़ा super set Super key का, मिर
Ex.
R (A, B, C, D)
Candidate key- A, B
R (A, B, C, D)
Candidate key- A, B
Super key- A, B, AB (3 super key बने गी)
=3×22
R (A, BC, D, E)
Candidate Key- A, BC
A
BC
ABC
AB
AC
Formula definition
Ex. R (A, B, C, D)
Candidate key- A
Formula- 2n-1
4-1
=2
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 में null value हो सकती है , तथा foreign key में value एक से ज्यादा बार repeat हो सकती है , अथाड त्
foreign key में duplicate value allow होती है |
मजस टे बल में Primary key मौजू द होती है , उसे Referenced table (parent table) कहते है , तथा मजस टे बल में
foreign key होती है | उसे referencing table (child table) कहते है |
Foreign Key की field value हमे शा primary key से belong करनी चामहए| यानी foreign key में वही value होनी
चामहए, जो primary key में मोजू द हो|
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 नही हो सकती|
Parent Table में record को update करने पर प्रॉब्लम हो भी सकती है और नही भी, क्योमक इसमें Parent Table से
child table reference ले रही होती है |
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 होता है |
Child table में record तभी Insert होता है , जब वह same field value parent table में exist करती हो| अथाड त्
parent table में available हो|
Child table में record update करने पर प्रॉब्लम हो सकती है , जब तक वह update parent table में available ना
हो|
नोट:- Referenced table (parent table) में record के Insert करने पर तथा Referencing table (child table) में
record के delete करने पर कोई भी प्रॉब्लम (violation) नही होता है | बाकी सभी case में violation होता है |
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
Foreign key में वही value हो सकती है , जो primary key में मौजू द हो| foreign key में E2 value मौजू द है , जो primary
key में मौजू द नही है | यानी Sanu का Manager E2 है , जबमक E2 ने तो company छोड़ दी|
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
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 करके दे खते है -
Indian * Mobile
Has
citizen 1 Number
M
एक Person एक से ज्यादा mobile no. रख सकता है , ले धकन यधद इसका revert (उल्टा) करके दे खे तो, एक mobile no.
धसर्ड और एक person को ही allotment धकया जा सकता है |
Primary Key F. K P. K F. K P. K
हम relationship table में many side (M. id) को primary key बनायेगे| क्योधक C. id repeat हो रही है | अर्ाड त् एक citizen
को एक ही mobile no. धमल सकता है , इसधलए M. id unique है | इसधलए table को many side merge धकया जाता है |
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 होती है |
* 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
P. K. Order F. K. P. K. Customer
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 को धमल सकता है |
Many-to-many mapping में Relationship table के attribute दोनोों entity की primary key होते है |
Many-to-many mapping में relationship table की primary key दोनोों attribute के combination से बनती है |
Many-to-many mapping में relationship table को धकसी भी entity के सार् merge नही कर सकते है , अर्ाड त्
relationship के धलए separate (अलग) table बनती है |
R1
E1 E2
R2
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
T1 T2 T3
पहले से पहले से
बनी हुई बनी हुई
है | है |
Ans. =2+1=3 table
Q. 3
R-1
M M
E-1 R-1 E-2 E-3
R-1
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?
R-1
E-2 E-2
R-2
A B
Q. 4- Three table competitor, event, competes and their relationship given below. (Programmer-2013)
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 धजसमें प्रार्धमक कुोंजी मान कहीों और सोंदधभड त होता है ?
Normalization एक प्रोसेस होती है | जजसके द्वारा हम database table में मौजू द redundant data या duplicate
data को reduce या eliminate जकया जाता है |
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 होती है |
Delete Anomalies
Update Anomalies
Student
S1
Sunil 27 Null Null Null Null
Insert Anomalies
1000
Null Null Null C2 C.N. Sunil
5000
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 कहते है |
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 कहते है |
Redundancy को Reduce या Eliminate करने के जिए, एक table को एक से ज्यादा table में split जकया जाता है |
अथाा त् तोड़ा जाता है | जजसे Decomposition कहा जाता है |
Student
P. K. F. K. F. K. P. K.
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
नही पड़ी है |
Student
S. id Determine कर रही है , S. Name को| (अथाा त् S. id बता रही है , की दोनों Sunil अिग-अिग है )
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
a6 b2 c2
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)
3. Semi-Trivial FD
यजद 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
4. S. id + S. Name ⟶ Age (invalid) (Age subset नही है | इसजिए यह invalid case होगा)
(X⋂Y=⌽)
यजद X, Y दो attribute है जकसी relation R के, वो non-trivial Function dependent जब होंगे, जब- X⋂Y=⌽
(फाई) होगा|
इसमें X तथा Y में कोई भी common attribute नही होना चाजहए| अथाा त् X जकसी नये attribute को determine
करना चाजहए|
Non-trivial Function dependency में determinant always नये attribute को determine करता है |
Ex.
यजद Function dependency, Trivial और non-Trivial का combination है , तो उसे semi non-Trivial Function
dependency कहते है |
जब- (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)
(a) x⊃y (b) x=y (c) x⋂y (d) x⊉y Semi-Trivial Function Dependency (X⊉Y)
Ans. (d)
1. Reflexivity
(कोई भी attribute स्वयं को हमे शा determine करे गा, तो यह हमे शा Valid होगा)
2. Transitivity Rule
यजद X⟶Y को determine करता है , या Y⟶Z को determine करता है , तब X⟶Z को determine करे गा|
*X⟶Y+ = XZ⟶YZ
यजद X⟶Y को determine करता है , तब दोनों के साथ कोई common attribute (जै से- Z) िगाने से इस पर कोई फका
नही पड़ता है | इसका मतिब XZ⟶YZ को determine करे गा|
4. Split Rule
यजद X⟶YZ को determine कर रहा है , तब X⟶Y को तथा X⟶Z को determine करे गा|
5. Merge Rule
Ex.
R (A B C D)
FD- [A⟶B, B⟶C, C⟶D]
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)
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+= [ABCDE]
Q. R (A B C D E F G)
FD- [AB⟶C, D⟶E, E⟶A, A⟶B, F⟶G]
(इसमें DF से ही Candidate Key जनकि रही है | इसजिए ADF Candidate Key नही होगा| क्योजक ADF का Subset है
DF)
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]
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]
(5) ADFG
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]
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 से
यजद हम single relation R को sub-relation के sets में split करना चाहते है , तो वह Decomposition कहिाता है |
Decomposition के Rules
1. Lossless join Decomposition
Decomposition होने के बाद में table का data loss नही होना चाजहए|
Table में मौजू द कोई भी attribute में कोई भी relationship है , तो बाद में भी उनकी relationship होनी चाजहए|
R1 R2 R3 R4
Relation schema R divided into sub-relation.
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 कहते है |
यजद वो original relation से subset होता है | यानी की ज्यादा होता है , तो उसे Lossy 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]
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
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]
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 होगा)
1. R1 ⋃ R2 = R
2. R1 ⋂ R2 ⟶ R1
Or
R1 ⋂ R2 ⟶ R2
2. D (ABC, DE)
FD- [AB⟶C, C⟶D B⟶E] यह तो satisfied (√) हो गया| क्योजक यह तो R के बराबर है |
R1= R1 ⋃ R2= ABCDE क्योजक इन दोनों में कोई भी common attribute नही है |
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 है |
5. D (ABC, ABDE)
FD- [AB⟶C, C⟶D B⟶E]
R1 ⋃ R2= ABCDE
R1 ⋂ R2= AB+= ABCDE
R1 ⋃ R2= ABCD
R1 ⋂ R2= C+= CD
S. K. of R2
R2 ⋃ R3= ABCDE
R2 ⋂ R3= D+=D
(D+ जकसी की भी super key नही बन रहा| इसजिए यह Lossy join decomposition होगा)
R1 ⋃ R2= ABCD
R1 ⋂ R2= C+= CD
R2 ⋃ R3= ABCDE
R2 ⋂ R3= B+ = BE+
S. K. of R3
CD, R2 की super key है तथा BE R3 की super key है | इसजिए यह lossless join decomposition होगा|
यह equal जब होगा| जब यजद F का attribute closer (F+) और G का attribute closer (G+) दोनों equal होंगे| यानी
F के द्वारा जो Determine जकया जायेगा, यजद same वही G के द्वारा Determine जकया जायेगा, तो वह equal होगा|
F
G
G
F
G सारे के सारे FD को Cover करती है , जो F के अन्दर मौजू द है | यहां पर G super set हो जायेगा F का|
Ex.1 R (A B C D)
FD Set-
G- [A⟶BC, C⟶D]
F G
A⟶B A⟶BC
B⟶C C⟶D
C⟶D
C+= CD [F ⊇ G]
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
[F ⊆ G] [G ⊆ F ]
Ans. F ≅ G
Ex.3 R (A B C)
FD Set-
F- [A⟶B, B⟶C C⟶A]
F G
A⟶B A⟶BC
B⟶C B⟶AC
C⟶A AB⟶C
BC⟶A
AC⟶B
[F ⊈ G] BC+= BCA
AC+= ACB
[F ⊇ G]
Ans. F ⊇ G
Conclusion/निष्कर्ष
FD- [WXZ⟶U, WX⟶Y] यह FD- [WXYZ⟶U, WX⟶Y] ≅ FD- [WXZ⟶U, WX⟶Y] होगी|
2. Redundant FD
F- [X⟶Y+ (sets of FD’s F except X⟶Y)
If F- [X⟶Y] equal to F
1. AB⟶C
2. BC⟶D
3. AB⟶D
AB+= ABCD
AB+= ABD
BC+= BCD
BC+= BC
AB+= ABCD
AB+= ABCD
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)
1. X⟶W
2. WZ⟶X
3. WZ⟶Y
4. Y⟶W
5. Y⟶X
6. Y⟶Z
2 NF
3 NF
BCNF
4 NF
5 NF
6 NF
जब E-R Model से हम database में table बनाते है , तो table में multivalued attribute present होता है , और
table का data redundant या duplicate होता है |
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
5 NF
Join dependency normal form पर based होता है |
6 NF
Domain key normal form पर based होता है |
Multivalued attribute allowed in E-R model, but not allowed in database table.
Multivalued attribute E-R model में allow होता है , िे जकन database table में allow नही होता है |
1NF के अनु सार table में प्रत्येक attribute में atomic value (single value) होनी चाजहए|
इस Student table में C.id multivalued attribute है | इसजिए 1NF में नही है |
Solution
Solution 1 (Split Record) Solution 2 (Split Column)
In this schema when we convert 1NF then how many record available in table.
Ans. 24 record
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 होगा|
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 रखता है |
कोई 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 पर नही)
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 को 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 होगी|
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
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
Ex.
Α Β
Transitive Dependency
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 के कारण transitive dependency present हो रही है | इसजिए इसके जिए separate table बनायेंगे|
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
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 नही करना
चाजहए| चाजहए|
1 NF
यजद कोई Relation R BCNF में है , तो वह relation
2 NF 3NF, 2NF, 1NF में भी present होगा|
सबसे पहिे 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
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
Not S.K/C.K.
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
Not S.K/C.K
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
Not S.K/C.K
Ex. 4 R (A B C D)
FD- [AB⟶C, BC⟶D]
C.K. – AB+= ABCD
Prime attribute- AB
Non-prime attribute- CD
Not S.K/C.K
Candidate key- AB
A+= A
B+= B
यजद candidate key का proper subset अिग से जकसी non-prime attribute को determine नही करता है , तो
वह 2NF में present होता है |
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 नही होगी|
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
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.
2NF-
Very less redundancy.
Redundancy कम होती है |
No Anomalies
0% redundancy No No No Yes
No
Dependency Preserving Decomposition Yes Yes Yes
(May not possible)
File Organization
Database (DB) is collection of file (tables)
File is collection of pages (blocks)
Block is collection of records
R2 R2
R3 Block R3
‘’ ‘’
‘’ ‘’
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
Single record allow to spanned two block Single record on one block.
File Structure
File is a data structure like as array.
Block
0 1 2
3 4 5
6 7 8
1. Continuous Allocation
Start
Files Size
DBA
abc.db 1 4
0 1 2
6 7 8
File block 1 से Start हो रही है और 4 की Size है |
Start End
Files
DBA DBA
abc.db 1 7
0 1 2
3 4 5
6 7 8
1. Index Allocation
1
9
Index 11
15 Block Address
7
5
End of file -1
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 कहलाती है |
(DB File के Block Factor से index का Block Factor हमे शा ज्यादा होता है, यानी index के Block Factor में entry
ज्यादा होती है |
यदद Sparse Index में search key एक Candidate key होती है , तो Primary index बनता है और यदद Candidate
key नही है तो Cluster index बनता है |
मान लो यहाों पर DB record ordered है , और X Candidate key है , तो Primary index बनता है | DB record
ordered है , और X Candidate key नही है , तो Cluster 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
Database file में दजतने blocks होते है , उतनी ही Entry होती है | यानी यहाों पर Entry बहुत कम होती है |
11 2
3 3
2 4
9 5
17 8
8 9
5 11
4 17
DB File 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 आयेंगे)
3000
68 = (44.11) = 45 Block (इसमे Upper bound ले ते है)
Dense Index
100 byte
Dense Index
Dense Index में दजतने record होते है , उतनी ही Entry होती है | इसदलए 30,000 entry होगी|
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
‘’
‘’
Q.
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 आयेंगे?
S. No.
B1 B1
B2 B2
B3 B3
‘’ ‘’
‘’ 30,000 record
B1500 B1500
Index DB File
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 होती है |
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 की सोंख्या के बराबर होगी|
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)
हमारी 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
10 Root Node
8 20 Internal Node
6 9 15 30 Leaf Node
Order/ Children= 2
M-Way Tree
10, 20
Children
5, 8 15, 18 25, 30
नोट:- Binary search tree में 1 key होती है , जबदक M-way tree में एक से ज्यादा दकतनी भी key हो सकती है |
10
20
30
40
C1 C2 C3 C4
Block Pointer
(Maximum No. of Children)
(Order of B-tree)
E. id Name --- --
1 ‘’
2 ‘’
3 ‘’
4 ‘’
5 ‘’
‘’ ‘’
Ex- Order = 10
B. P. B. P. B. P. B. P.
10, 20, 30, 40
1.
10 20 30 Medium
Medium
10 30 40 50
10 30 50 60 70
4.
20 40 60
10 30 50 70 80 90
40
20 60 80
10 30 50 70 90 100
0
Searching operation perform fast, because only need to access leaf node.
Searching fast होती है , क्ोदक केबल leaf node को ही access करना होता है |
Internal node या root node की key copy भी leaf node में मौजू द होती है |
B+tree Structure
BP Name
Key
B. P.
Key
B. P.
Key
B. P.
Key
B. P.
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?
Elements- 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
1. 1, 2, 3
1 3, 4, 5
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
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?
= N ≤ 521
15 = 34.73
=Ans. 34
SQL (Structured Query Language) 1970
User
Structured- Tables/Relations
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
कर िकते है |
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 विकवित वकया|
Non-procedural language का मतलब- इिको नही बताना होता है वक हमें क्या करना है , कैिे करना है |
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
कर िकते है |
नोट- Application program के िाथ DML (Data Manipulation Language) को Embedded वकया जाता है |
Integrity Constraints
Rules apply using DDL language
Integrity constraints rules होते है , जो DDL language के िाथ apply वकये जाते है , यानी Table create करते
िमय apply करते है |
यह database objects जै िे की- table, views, user, indices को create करने और modify करने के वलए उपयोग
वकया जाता है |
DDL Command
i. Create- To create new database, tables, views, users.
Create Command के द्वारा हम नया database, table, view, user को create करते है |
Create Command
Database Create
Stmt >create database database_name;
SQL >create database collage; (इििे Collage name का database create होगा)
1. Table Create
Student
नोट- Table Create करते िमय Table या Column के name में हम space नही दे िकते| इिमें हम under score
(_) use करते है |
Desc/Describe Statement-
(To show table structure)
>Desc table_name;
>Desc Student;
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 होते है |
Numeric
iii. Int/integer- For store integer value
iv. Small int- Same as integer but less size compare to int.
यह integer के िमान होता है , ले वकन यह int िे कम size का होता है |
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
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 होोंगे)
Date
Date Format- YYYY-MM-DD
Ex. 2022-08-22
Time
Time Format- HH:MM:SS
Ex. 19:52:55
वकिी table में data के rules को वनवदक ष्ट (specify) करने के वलए Constraint का उपयोग वकया जाता है |
If any Constraints rules violation then data cannot insert into table.
अगर कोई Constraint rule violate होता है , तो table में data 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 िे ज्यादा िाला होना चावहए|
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 करें गे|
Age int,
Check (age>18)
);
(C_id varchar(5),
C_Name char(20),
Instructor char(20),
Fee float(2),
Roll_no int,
Students (Roll_no));
Roll_no Name Mobile City Age C_id C_Name Instructor Fee Roll_no
नोट- 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,
On update cascade
);
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;
इि 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 करती है और मु ख्य टे बल पर स्वतोंत्र रूप िे कायक करती है |
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
नोट- When we create view two or more table help view that some case DML operation may not work
proper.
जब दो या दो िे अवधक table की िहायता िे view create करते है , तो कुछ case में database में DML operation
proper काम नही करते है |
यानी view के द्वारा कई बार जो हम view table में update करते है , िो master table में update नही हो पाता है |
Alter Command
1. Add New Column
Alter command के द्वारा हम table में नया कॉलम Add कर िकते है |
SQL >Alter table student add column (city char(20), mobile numeric(10), age int);
2. Drop Existing Column
Alter command के द्वारा हम टे बल में मौजू द कॉलम को drop कर िकते है , यानी मौजू द कॉलम को हटा िकते है |
Student
Stmt >Alter table table_name rename column old column_name to new_column name;
Student
Stmt >Rename table old_name to New_name; (इिको ऐिे भी वलख िकते है)
Student
Student_info
इिको ऐिे भी वलख िकते है क्योवक Table में एक ही Primary Key होती है |
Data के बारे में जो information रखता है , उिे Meta Data कहते है | (Data about data)
यानी टे बल में मौजू द Actual data को छोड़कर टे बल का बाकी िभी data को Meta Data कहलाता है | जै िे की-
Table name, Table के Column Name आवद|
Drop command के द्वारा हटाई गई टे बल के Data और Structure को िापि िे recover नही वकया जा िकता है |
Result ‚Table Don’t Existing‛ (Table मौजू द नही है , यह result Show होगा)
नोट- Describe Command के द्वारा हम टे बल का structure दे ख िकते है | Describe को हम Desc भी वलख िकते है |
Student
Truncate Command टे बल में मौजू द Data को हमें शा के वलए हटा दे ती है , वजिे हम िापि िे Recover नही कर
िकते है |
DML में हम टे बल के Actual data पर Operation Perform करते है , अथाक त् टे बल में नया record insert करते है ,
Old record को update करते है और Old record को Delete करते है | यानी की data में manipulate (बदलाब)
करने के वलए DML Command का उपयोग करते है |
DML auto committed नही होता है | यानी DML के िभी Statement को अलग िे Commit करने पड़ते है |
Student
नोट- Insert, Update, Delete यह िभी Task RAM में Perform होते है , जब तक हम Commit Operation
perform नही कर दे ते है | Commit Operation के बाद िभी Task database में हमे शा के वलए save हो जाती है |
1. Insert Command
New record/records insert in table.
इि statement में एक ही table column sequence में ही िभी column value insert करनी पड़े गी|
नोट- Table Name के िाथ जब हम column को select करते है , तो हमें primary key column और Not Null
column को हमे शा select करना चावहए, नही तो table में record insert नही होगा|
जैसे- मान लो Name- Not Null है और हम Roll No. तथा Age को select करते है , तो िह insert नही होगा, Error
आयेगा|
नोट- 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 हो जाते है |
Column2=value,
Column3=value,
[Where Condition];
Where Roll_no=103; (इििे Roll No 103 िाले का नाम Ravinder िे ’Ravindra’ हो जायेगा)
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
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 हो जाते है |
इििे टे बल में मौजू द िभी 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
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 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 नही कर िकते|
[Where Condition]; (यह Optional होती है | Where Condition नही लगायेंगे तो भी data Fetch हो जायेंगा)
Student
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
+ != , <> (Inequality)
- (Binary Operator) < , > , <= , >=
* IN, LIKE
/ IS NULL, IS NOT NULL, BETWEEN
3. Logical Operator
AND- Return true if both components condition is true.
4. Set Operator
UNION- Return all distinct Row from 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
Employee
Avg (salary)
25,000
हमें उन िभी Employee का नाम Find करना है , वजनकी Salary Average Salary िे ज्यादा है |
Employee
Name
C
A
2. MIN (Minimum)
Find out Minimum value of given sets of value.
Employee
Min (salary)
15,000
Employee
Name
D
3. MAX (Maximum)
Find out Maximum value of given sets of value.
Employee
Max (salary)
35,000
Employee
Name
A
Salary
20,000
25,000
30,000 Find this salary
35,000 Highest salary of this table
15,000
इि Statement िे इि टे बल की highest salary (35,000) को छोड़कर बावक िभी salary display होगी|
Salary
20,000
25,000
30,000
15,000
Employee
Name
C
4. SUM (Total)
Calculate Sum
Sum (salary)
1,25,000
5. COUNT (Count)
No. of Rows count
यह Row की िोंख्या को count करता है |
Employee
Count (*)
5
Count (*)
4
इिमें हमने particular Column का Name mention वकया है , इिवलए यह Null को कभी भी Count नही करें गा|
Count (City)
3
Group by Clause
Group by clause used for grouping similar data.
Employee
Employee
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 करते है |
हमें टे बल में उन employee की Average salary count करनी वजनमे दो या दो िे ज्यादा employee है | यानी single
employee की salary नही, क्योवक उिकी salary ही उिकी Average salary होगी|
Employee
D_id Employee
CS 15,000
ME 26,000
Order by Clause
Arrange display result in ascending or descending order.
Result को ascending या descending order में display करने के वलए order by clause का use वकया जाता है |
D_id Salary
ME 26,000
CS 15,000
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
नोट- Set operator को use करने के वलए दोनोों टे बल में Domain same होने चावहए| जै िे की- E_id Varchar है , तो
S_id भी Varchar होनी चावहए|
Find the employee name who don’t have student.
Logical Operator
1. AND
2. OR
Comparison Operator
1. IS NULL/IS NOT NULL
2. IN
3. BETWEEN
4. LIKE
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
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.
Shorthand OR Operator.
इिे OR Operator का Shorthand भी कह िकते है |
Table में वजनकी City Jaipur, Alwar, Kota है , उन िभी employee का नाम display होगा|
3. BETWEEN
To compare rang including both boundary value.
दोनोों boundary value को include करते हुए rang को compare करता है |
वजनकी salary 10,000 िे 30,000 के बीच है , उन िभी employee का नाम display होगा|
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 वकया जाता है |
‘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 ही
होने चावहए)
Arithmetic Operator
Display 10% increment on salary.
ऐिे िभी employee वजनकी age 50 िे ज्यादा है , उनकी salary 5000 जु ड़कर (+) होकर display होगी|
Where age>50;
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-2 >Update student set age = 30 where S_id=105; यह िभी Task Permanent Save हो जायेगी|
Commit के द्वारा operation 1, 2, 3 को database file में हमे शा के वलए save वकया जाता है |
2. ROLLBACK
Discard all operation until last commit.
SQL-3 >Delete from student where S_id = 104; (This operation was performed by mistake)
>ROLLBACK
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;
इि Statement के द्वारा ABC नाम का यूजर create होता है , वजिके password 1234 है |
1. GRANT
To allocate database user permission.
Database के user को permission दे ने के वलए हम Grant का use करते है |
इि Statement िे ABC नाम के यूजर को student table पर data read करने यानी की data display करने की
permission दी गई है |
To allocate permission to ABC user on student table for perform all operation.
इि Statement िे ABC नाम के यूजर को student table पर िारे operation perform करने की permission दी
गई है |
इि Statement िे Database के िभी यूजर को Student table का data display करने की Permission grant की
गई है |
नोट- जब हमें Database के िभी User को Common Permission दे नी होती है , तो हम PUBLIC का use करते है |
Role
Collection of permission
Role Permission का Collection होता है |
SQL >Grant HOD to Sunil; (Sunil- User name, HOD- Role name)
हमने यहााँ पर 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>
इि Statement िे ABC नाम के यूजर िे student table पर data display करने की permission िापि ली गई है |
इि Statement िे ABC यूजर िे student table के िभी operation perform करने की permission िापि ली गई
है |
इि Statement िे database के िभी यूजर िे student table पर data display करने की यानी data read करने
की permission िापि ली गई है |
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 नही कर िकते है |
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 कर िके|
Transaction बहुत सारे operation का समू ह होती है , जो single logical unit की तरह perform करता है | जैसे-
लोगोों के द्वारा अपने बैंक अकाउों ट से पैसा जमा करना और पैसा ननकालना|
Account A B
ACID Properties
एक Transaction को कैसे सफलतापूर्ाक Handle करना है | यह ACID Properties ननधाा ररत करता है |
A- Atomicity
C- Consistency
I- Isolation
D- Durability
1. Atomicity
Either all instruction executes successfully or none of instruction execute.
या तो transaction की सभी instruction proper execute होनी चानहए, या transaction की कोई भी instruction
execute नही होनी चानहए|
Atomicity यह ननधाा ररत करता है | नक अगर कोई transaction हो रहा है , तो र्ह transaction होगा, या तो नही होगा|
अथाा त् transaction एक ही step में हो जाना चानहए| यनद transaction अधूरा रह गया तो Rollback हो जाना चानहए|
Account A B
3000 2000
Failure occur
यहाों Commit से पहले ही Failure occur हो गया है , तो सारे Task को र्ापस से Rollback करना पड़े गा| क्योनक
Atomicity का यही Rule है | नक या तो सभी Transaction Execute होने चानहए, या तो इनमे से कोई भी Transaction
execute नही होना चानहए|
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
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 होती है |
T3
Serial Transaction
T1 T2 T3
Parallel 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 अलग-अलग होना
चानहए|
4. Durability
Database all change are permanent.
Database के सारे change हमे िा के नलए होने चानहए| यानी नक database के नकसी भी प्रकार के failure होने पर
data lost नही होना चानहए|
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 नमलना चानहए|
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 करना है |
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
T1 T2
R (A)
W (A)
R (A)
W (A)
Commit
‘’
‘’
Failure
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
T1 T2
R (A)
R (A)
Delete (A)
Commit
T1 T2 T3 T4 T5
R (A)
W (A)=50
R (A)=50
Rollback R (A)=50
R (A)=50
R (A)=50
Failure
Note- To Avoid Cascade Rollback Uncommitted Transaction cannot Permission to read any other
transaction.
Schedule
Exexution sequence of two or more transaction.
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)
T1 T2 T1 T2
R (A) R (A)
W (A) W (A)
R (A) R (A)
W (A) W (A)
1. T1 T2
2. T2 T1
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
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
W (A) W (A)
R (A) R (B)
R (B) R (A)
W (A) W (A)
Solve by method-2
Check all conflict pair between transaction and draw edge.
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)
R (y) R (y)
W (z) W (z)
Precedence Graph
T1 T2
Loop exists
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)
R (B)
W (B)
If schedule is Non-Conflict serialzability then that schedule may or may not consistent.
यनद schedule Non-Conflict serialzability है , तब र्ह schedule consistent हो भी सकता है , और नही भी| यानी
हम कुछ भी नही कह सकते|
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)
T1 T2 T3
T1 T3 T2
T2 T1 T3
T2 T3 T1
T3 T1 T2
T3 T2 T1
T1 T2 In-degree 0 vertex= T2
जै से- T2 में कोई edge नही आ रही, यानी T2 में In-degree vertex है | इसनलए हम
T2 को graph से हटा दें गे|
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)
S S’
T1 T2 T3 ≅ T1 T2 T3
R (A) R (A)
W (A) W (A)
W (A) W (A)
W (A) W (A)
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 हो रहा हो|
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
W (b) यह भी Satisfied है |
नोट- This Schedule S is irrecoverable Schedule because transaction T2 data not possible to recover.
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 है |
T1 T2 T3
R (A)
R (A)
W (A)
R (A)
Commit
Commit
Commit
Schedule S is Recoverable.
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
Schedule S is irrecoverable.
इसमें Commit का Sequence different है | इसनलए यह Schedule irrecoverable होगा|
T1 T2 T3 T4
R (A)
W (A)
‘’ R (A)
‘’ R (A)
‘’ R (A)
Failure
नकसी भी Schedule को Cascadeless बनाने के नलए dirty read की problem को solve करना होता है , यानी
Transaction में dirty read present ना हो|
यनद Schedule में dirty read problem present है , तो commit का sequence हमे िा transaction के initiate
sequence के अनु सार होना चानहए|
Commit
N
R (A)
N
Commit
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
1. Shared Lock
Denote- Lock S(Q)
यलि लकसी Transaction ने shared- lock कर रखा है , तो वह केबि Data Read ही कर सकता है |
2. Exclusive Lock
Denote- Lock X(Q)
यलि लकसी Transaction ने exclusive mode में lock कर रखा है , तो वह Data Read और Write िोनोों कर सकता है |
एक या एक से ज्यािा Transaction Shared mode में है , यानी एक या एक से ज्यािा transaction Read कर रही है ,
तो इसे हम Allow करे गे| यानी यह True होगी|
जब एक transaction Shared mode में है और िू सरी Exclusive mode में है , तो इसे हम Allow नही करे गे| यह
False होगी|
जब एक transaction Exclusive mode में है और िू सरी भी Exclusive mode में है , तो इसे हम Allow नही करे गे| यह
False होगी|
Ex-2
T1 T2
Lock X(A)
R (A)
W (A)
Lock S(A)
R (A)
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)
Disadvantage|
May not free from irrecoverable, Cascadeless or deadlock
Two Phase Locking irrecoverable, Cascadeless और deadlock से free नहीों है |
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 कहता है की, जब तक सभी lock acquired नही हो जाते, उसे hold मत करने िो|
इससे यह deadlock free हो जायेगा|
यह Conflict Serialzable और Deadlcok free provide करता है | यानी यह Conflict Serialzable और Deadlcok
free है |
Strict 2 Phase Locking मतिब 2 Phase Locking के सार् सभी Exclusive lock को जब तक hold रखो तब तक
वह commit नही कर िे ता|
R (A)
W (A)
Strict 2 Phase Locking Achieves Conflict Serializable, View Serializable, Recoverable, Cascadeless.
Strict 2 Phase Locking Conflict Serializable, View Serializable, Recoverable, Cascadeless Achieve करती
है |
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)
Rigorous 2 Phase Locking Achieves Conflict Serializable, View Serializable, Recoverable, Cascadeless.
Rigorous 2 Phase Locking Conflict Serializable, View Serializable, Recoverable, Cascadeless Achieve
करती है |
Student × Course
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
2. Outer Join
Table A Table B
Inner Join
Inner join contain tuple those satisfied the specific condition.
Inner join में वह tuple होते है , जो specific condition को पूरा (Satisfied) करते है |
Student Employee
AND Student.Age<Employee.age;
Student × Employee
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 होना चाजहए|
Student Course
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 होना जरुरी नही है |
Equi Join is a special case of Theta Join where use only equivalent (=) condition.
Equi Join, Theta Join का special case होता है , जो equivalent (=) condition पर काम करता है |
Employee Department
ऐसे Employee का नाम Find करना है , जजसकी city और department location same है |
AND Employee.City=Department.Location;
EQUI Join
E1 A Jaipur D1 Jaipur E1
E2 B Kota D2 Delhi E2
E2 B Kota D3 Alwar E2
Employee
E. id Name M. id
E1 Sunil E1
E2 Rahul E1
E3 Harish E2
E4 Ravi E1
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 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
जमिती है |
Notation- R1 ⟕ R2
Left Right
Student Course
ON Student.S_id=Course.S_id;
Right Outer Join right table के सभी record तथा साथ में left table के matching records रखता है |
Notation- R1 ⟖ R2
Left Right
Student Course
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
S1 A 20 C1 DBMS S1
S2 B 22 C2 C.N. S2
S3 C 24 C3 OS S3
Right Outer Join में Right Table का सभी data, और Left Table का केबि Matching data आता है |
Full Outer Join दोनोों Left table और Right table के सभी record को रखता है |
Notation- R1 ⟗ R2
Left Right
Student Course
ON Student.S_id=Course.S_id;
S1 A 20 C1 DBMS S1
S2 B 22 C2 C.N. S2 Matching Record
S3 C 24 C3 OS S3
Full Outer Join में दोनोों Left Table का और Right Table का सभी data आता है |
All SQL (Structure Query Language) commands are based on Relation Algebra.
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 करती है |
3. ×(Cross Product)
4. ρ (Rename)
5. ⋃(Union)
6. ∩ (Intersect)
7. (Set Different)
8. / , % (Division)
Student
Result-
City (Student)
SQL- Select distinct city from student;
Result-
City
Jaipur
Show Distinct Result
Alwar
Relation Algebra by default Distinct result दे ता है | यानी result में Duplicate value show नही करता है |
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
σ Age>28 (Student)
Result-
Name
Anil
Selection और Projection Operation को apply करने के वलए हमें िबिे पहले Selection apply करना होता है |
उिके बाद Projection operation को apply वकया जाता है |
SQL में Selection Operation Where Close के द्वारा implement वकया जाता है |
A B C C D
1 2 3 3 9
4 5 6 4 8
3 5 8
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
4. Rename (ρ)
Change column name temporary on display table.
Rename operation display table में अस्थाई रूप िे कॉलम का नाम Change करने के वलए काम आता है |
अथाा त् इििे original table के कॉलम का नाम Change नही होता है , केबल display होता है |
Student
Result-
Student
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 में |
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
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
Student ∩ Employee
101 Sunil
103 Sonu
Set Different में हम उि record को ले ना होता है , जो first table का part होता है , ले वकन second table का part
नही होता है | यानी की दोनोों table के common record को हटाना होता है |
R1 R2
Ex-1 X Y
x, y, z z, a, b
XY
x, y
YX XY≠YX
a, b X Y तथा Y X कभी भी equal नही होता| अथाा त्
दोनोों का result हमे शा अलग-अलग होगा|
Ex-2
Student Employee
Student Employee
Employee Student
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 / Employee
S. id
S1
Relational Calculus
Relational Calculus is a Non-Procedural language.
Relational Calculus एक Non-Procedural language होती है | इिका मतलब, हमें क्या करना है , इिे यह बताना
होता है | कैिे करना है , यह process नही बताना होता है |
अथाा त् Predicate Statement दो प्रकार के होते है , या तो true statement होता है , या false statement होता है |
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 कुछ भी हो िकता है )
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 हो जायेगा|
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 नही होगा|
If x is student in DBMS
Then x must be study DBMS.
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
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
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
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)
4. The projection operation in Relational algebra except that select in SQL retains duplicates.
Ans. 4
Student Performance
Ans. (a)
Raj
Rohit