Docsity

DBMS Complete Handwritten Notes | ER Model, SQL, Normalization, Keys, Joins & Transactions, Study notes of Database Management Systems (DBMS)

Partial preview of the text

Download DBMS Complete Handwritten Notes | ER Model, SQL, Normalization, Keys, Joins & Transactions and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

DBMS NOTES 4. ER MODEL (Entity Relationship Model) ER model is a high level conceptual data model used to design the structure of a database. Basic Components : A veal world object with independent existence. (e.g. Student, Teacher) ¢ Entity : * Attribute : Relationshi pie Cardinality Q Key Attribute : Property or characteristic of an entity. (e-9. RollNo, Name) Association between two (e.g. Enrolls) Specifies the number of instances of one or more entities. entity that can be associated with another. (e.g. Roll No of Student) ER Diagram Symbols : Attribute that uniquely identifies an entity. Symbol Name Meaning Entity Represents an entity set Attribute Represents an attribute Relationship Represents relationship between entities Weak Entity Represents weak entity, set Key Attribute | Attribute that uniquely identifies an entity Name ah N Student Enrolls Course COM OOO This shows a 1-N_ relationship. One Student can Enroll in many Courses. One Course can have many Students. 2. Normalization @® Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity . Normal Forms : @ 41NF (First Normal Form) ; ¢ All attributes must have atomic (indivisible) values. e No repeating groups or arrays. Example : StudentID | Name | Courses SAUHERSIDY Name [RE fi Ravi | DBMS 1 Ravi | DBMS, 0S J —> Not in INF 4 Rai | 0S | —> In 4NF a Neha | DBMS, CN 5 tie I maae 2 Neha | CN @ 2NF (Second Normal Form) : © Must be in INF. ° Every non-key attribute must be fully functionally dependent on the whole of the primary key. Example : ProductNc i OrderED | PreductID | ProductName | Qty | Price ee Ce er oe ay = ProductID (partial dependency ). fot PL Pen. 2) 40 LoL lie Notebook 5) 50 ae key Order Product OrderID | ProductID | Qty ProductID | ProductName | Price dot PL 2 PL Pen 410 fo4. P2 3 P2 Notebook 50 PK —> (OrderID, ProductID) @ 3NF (Third Normal Form) : in 2NF. * No transitive dependency. Non-Key attributes must not depend on other non Key attributes . ° Must be PK —> ProductID After 3NF : ee Y Employee EmpID | EmpName | DeptID | DeptName | Deptlocation EmpID | EmpName | DeptID 4 Amit D4 HR Delhi 4 Amit Dt pi Neho. Da oi Pune 4 Neha D2 f a } PK > EmpID Transitive Dependency Department DeptID | DeptName | DeptLocation Benefits of Normalization : oe ale DH Removes redundancy De ‘a Pane * Improves data integrity PK —» DeptID * Saves storage space + Avoids anomalies (insert, update, delete) o. KEYS Keys are attributes (or set of attributes) that uniquely identify a record in a relation (table). Types Keys : Key Description. Example Super Key A set of one or more attributes that {RollNo}, {Email}, uniquely identifies a record. fRollNo, Name } Candidate Key A minimal super key (no proper subset {RollNo}, {Email} is a super key). Primary Key The candidate key chosen to uniquely {RollNo} identify vecords. Alternate Key Candidate keys not chosen as primary key. | {Email} Composite Key A key that consists of two or more {StudentID, CourseID} attributes. Foreign Key An attribute (or set) that refers to DeptID in Student table the primary key of another table. refers to DeptID in. Dept table, Unique Key Ensures all values in a column (or set) Email are different. Example : Consider a relation STUDENT RollNo Name Email Dept ID Phone. 104 Ravi [email protected] D1 9876543210 102 Neha. [email protected] D2 9123456780 103 Amit amit@gmail com DL 9988776655 i t i Candidate Key Candidate Key Foreign Key Unique Key (Primary Key) (Alternate Key) (references Dept) oe Referential Link : DEPT 4 has >—N—| STUDENT (Dept ID) Notes : (Dept ID) | hy Keys are important ? | * Uniquely identify records. | ¢ Establish relationships between tables. | * Maintain data integrity. | © Avoid duplicate and inconsistent data. © Primary Key values must be unique and not null. * Candidate keys are the possible choices for primary hey. © A table can have only one primary key but can hove multiple candidate keys. ° Foreign key values must match the primary key values in the referenced. table or be null. 5. TRANSACTIONS A transaction is a sequence cd operations performed as a single unit of work. It is the basic unit of execution in DBMS. Properties of Transaction : . Atomicity : ° Consistenc yt All operations are executed completely or not at all. Transaction brings the database from one consistent state to another consistent state. e Isolation Concurrent execution of transactions leaves the database in a consistent state. . Durability Once a transaction is committed, its changes are permanent. States a Transaction : Active Last facto Commit Committed eee (E xecuting ) operation Coramitted. (Suecess) Error or hin: Rollback Failed — Aborted 6. ACID PROPERTIES ACID properties guarantee reliable processing of transactions. Property Description. Explanation Atomicity All operations in a transaction Either all operations succeed (commit) are treated as a single unit. | or none are applied (rollback). Consistency Transaction moves the database from) All integrity constraints are preserved one valid state to another valid state.) before and after transaction. Isolation Concurrent transactions do not Intermediate results of a transaction interfere with each other. are hidden from others. Durability Once committed , es are Committed data is saved to stable permanent even Y system fails. | storage. 7. INDEXING Indexing is a technique used to speed up data retrieval operations on a table. . rimery Index Example : Types of Index : e a Index File Data File = me Index on the ser Ld (primary key),| Ri nae Aa - Bet rimary x x On ore ous ey 4)-| RollNo (so Fe Ae Clustering Index Index on a non-key field that orders Dept the data physically. ce 204 Block 3 Secondary Index | Index on a non-ordering field. Name oot Blacks Dense Index Index entry for every search key value. | Entry for every record, Sparse Index Index entry for some search key values. | Entry for some blocks, Index entries point to blocks of the data file. 8. JOINS (Contd.) Join is used to combine rows from two or more tables based on a related column between them. Example Tables : Student Department: RollNo Name | DeptID DeptID | DeptName We: will ge these, tablee) for Ravi D4 D4 CSE all jon examples, on this B RES be pa IT and next page. gj Amit D4 D3 ECE 4 Pooja. D3 7) SELB EN Example : Consider table. Employee A table is joined with itself. Result : EmpID | Name | ManagerID za Fi SELECT A.Name AS Employee, B.Name AS Manager i Ramesh NULL ge hae a FROM Employee A, Employee B 2 | Suresh t Suresh Phamesh WHERE A.ManagerID = B.EmpID; 3 Mie t Mahesh | Ramesh a» | NikniL 5 Nikhil | Suresh 8) NATURAL JOIN Result : ' Venn Diagram Automatically joins tables on columns RollNo | Name | DeptID | DeptName with the same name and data type. 4 Ravi D4 CSE ZB 2 Neha D2 IT EZ) Drain SELECT * FROM Student F hi i ae Gi NATURAL JOIN Department ; - 4 Pooja D3 ECE (Only aoe) 9) EQUI JOIN Result : ees Venn Diagram Join condition uses equality (=) RollNo | Name | DeptID | DeptName between columas. 4 Ravi D4 CSE Z 2 | Neha | D2 TT Z SELECT # FROM Student s Z JOIN Department a 3 Amit Da CSE J ON s.DeptID = d.DeptID; 4 Pooja. D3 ECE (Only eee 410) NON-EQUI JOIN Example Tables : Join condition uses operators Salary Grade Result : other than equality (>, <,>=, <=, l=). EmpID | Salary Grade | MinSal | MaxSal EmpID | Salary | Grade SELECT * FROM Salary s 4 25000 A 0 30000 1 25000 A JOIN Grade 9 2 45000 B 30004 60000 2 45000 B ON s.Salary BETWEEN g. MinSal 3 70000 € 60004 400000 3 0000 Cc AND. 9. MaxSal ee aa SG =e r= OE tn, eee Sn a en S or Key Points about Joins : * INNER JOIN returns only matching rows © OUTER JOINS (LEFT, RIGHT, FULL) return unmatched rows also © CROSS JOIN returns Cartesian product (every row of first table with every row of second table). | |