Database Management Systems
Database Management Systems
VIJAYA COLLEGE
Page 1
CHAPTER 1: INTRODUCTION
DATABASE MANAGEMENT SYSTEM Data may be used to represent thing like name, telephone number, address, people name. The data can be stored using Microsoft ACCESS, EXCEL, etc. The data base is collections of data are group together to make large information. The database system is collection of programs that enables the users to create and manipulate a dbase. For example, to creates a new account in a bank and creates a new account in yahoo. The database management system is a general purpose software system, in which specifies defining, constructing and manipulating of the database. DATABASE(Dbase) MANAGEMENT SYSTEM 1. It is collection of programs that enables users to create and manipulate a dbase a. Ex: creates a new account in yahoo.com b. Ex: creates a new account in a bank 2. It is a general purpose software system 3. It specifies defining, constructing and manipulating An Example 1. In University database system, it has four files namely STUDENT, COURSE, SECTION, GRADE_REPORT schema 2. Ex: Student dbase 3. STUDENT file has four attributes Table 1. STUDENT file Student name Register no Semester Major Varaprasad 001 1 CSE Prasad 004 2 ECE 4. Ex: COURSE file has four attributes Table 2. COURSE file Course name Course no Credit Dept OS 08SCS11 3 CSE DS&A 08SCS12 4 CSE DBMS 08SCS13 4 CSE CSPA 08SCS14 4 CSE TFCS 08SCS151 3 CSE 5. Ex: SECTION file has five attributes Table 3. SECTION file Section no Course no Semester Year Instructor 1 08SCS11 1 2008 Dr.G.V.P 2 08SCS12 1 2008 Dr.G.V.P 3 08SCS13 1 2008 KNS 4 08SCS14 1 2008 Dr.S.R.K 5 08SCS151 1 2008 GRP 6 08SCS27 3 2008 Dr.G.V.P 6. Ex: GRADE_REPORT file has three attributes Table 4. GRADE_REPORT Register no Section no Grade 001 1 A 004 2 B 001 2 C CHARACTERISTICS OF DATABASE APPROACH 1. Single Repository of Data(SRD) a. A place where the data is stored b. A place where multiple databases or files are located for distribution
VIJAYA COLLEGE
Page 2
VIJAYA COLLEGE
Page 3
VIJAYA COLLEGE
Page 4
VIJAYA COLLEGE
Page 5
Attributes types Single attribute or atomic attribute 1. Cant be subdivided further 2. Ex: in employee relation, SSN, Age, sex, sal Composite attribute(C attribute) 1. Can be subdivided further 2. Ex: in Employee relation, address Figure 2. Address attribute Single value attribute 1. Has single value for a particular entity set 2. Ex: In Employees relation, Age, Dept no Multi-value attribute 1. Can be divisible 2. Ex: Degree(B.E, B.Tech, M.E, M.Tech, PhD, DSc) Address H.NO Cros Main Street Area
VIJAYA COLLEGE
Page 6
STUDENT
Name Sem Major RegN
Employee Dependents
Attribute 1. Ex: RegN 2. 4. Multi-valued attribute 1. Ex: Degree 2. 5. Derived attribute 1. Ex: Age 2. Primary key 1. Ex: SSN 2. Relationship 1. Ex: Deposit 2. Participation 1. Partial Participation 2. _________ 3. Total portion 4. Relationship 1. It is an association among several entities 2. A relationship set is a mathematical relational among entities n=2 1. WorksFor 2. Ex: WORKSFOR
Age RegN SSN EMPLOYEE SSN Name Add DEPARTM ENT Dno DNam DAdd Dadd
3. Manages
VIJAYA COLLEGE
Page 7
PROJECTS PNam EMPLOYEE SSN Name Add Date Name DEPARTMENT Dno DAdd Dadd PROJECT PN PNam EMPLOYEE SSN Name Add MACHINE S Mno MName Mcost SUPPLIER SSN Name Add PARTS PN PNam Pno Plocat EMPLOYEE DEPARTMENT EMPLOYEE DEPARTMENT
11. ER-Diagram for employees database Supervision WorkFor Supervisor Supervisee Manages Control Dependence of Works_on RELATIONSHIP 1:1 Relation Ex: one person has one passport 1:N Relation Ex: one student registers many subjects N:1 Relation
VIJAYA COLLEGE
Page 8
EMPLOYE E Dept FN MM LN SSN Name Add Project Pno PNam Ploct Hours Startdate Dno DNam DAdd Dadd Name Sex Relationship DOB Dependent EMPLOYEE DEPARTMENT
RECURSIVE RELATIONSHIP SET (RR SET) 1. Where there is a relationship set which connects the same entity set is called RR set supervisor Supervision supervisee HoD supervisees many employees WEAK ENTITY SET 1. Entity set 2. Doesnt have a primary 3. Ex:
LOAN PAYMENT
REFINING THE ER-DRAM FOR COMPANY DBASE 1. Identify the entities 2. Identify the strong entities and weak entities a. Employees Strong entity b. Department Weak entity c. Project Strong entity d. Dependent Weak entity 3. Identify the relevant attributes a. Employee, SSN Name, add, sex, sal b. Department, Dno, dname, Dlocation c. Project , Pno, Pname, Plocation d. Dependent, Dename, DoB, Sex, Relationship 4. Identify the relationship sets a. Employee , Department, WorkFor b. Employee , Department, manages c. Department, Project, Controls d. Project, Employee, WorkOn e. Employee , Department, Dependent_Off f. Employee, Employee, supervision 5. Identify the cardinality ratio & participation constraints a. Works_For N 1
VIJAYA COLLEGE
Page 9
EMPLOYEE FN MM LN DEPARTMENT Dno DNam SSN Name Add PROJECT Pno PNam Ploct Name Sex Hours Startdate DAdd Dadd Relationship DOB DEPENDENT SSN Name Add EMPLOYEE DNam DEPARTMENT Dno DAdd Dadd PN Model Add PHONE
VIJAYA COLLEGE
Page 10
SSN Name Add INSTRUCTOR CN Name COURSE BN Name Author TEXT BOOK Edition CHAPER 3. RELATIONAL MODEL AND RELATIONAL ALGEBRA
RELATIONAL MODEL 1. Relational model represents the dbase as collection of records 2. Ex: University dbase 3. Student table Student name Register no addr Phone DoB Sem Perc Varaprasad 001 N.S.Halli 5567895 22/9/79 1 70 Prasad 004 DS. Halli 5567897 10/6/65 2 60 4. Student table is called Student relation 5. In student relation, row is called a tuple 6. In student relation, column header is called an attribute DOMAIN, ATTRIBUTES, TUPLES, AND RELATIONS 1. A domain D is a set of atomic values a. Ex: in student relation b. Reg no: 10 alphanumeric characters i. Ex: 1BM99CS015 c. Name : characters i. Ex: Varaprasad d. Address i. alphanumeric characters ii. Ex: 456, N.S. Halli, Bangalore-19 e. Phone i. 7 digits ii. 5567895 f. DOB i. Date ii. Ex: 22/9/79 g. Sem i. 1 digit ii. Ex: 4 h. Perc i. Float{0-100} ii. Ex: 70 2. A Relation schema R, denoted by R(A1, A2,.An) is made up of a relation name R and a list of attributes (A1, A2,.An). 3. Degree of relation is number of attributes in a R a. Ex: in student relation(R), degree is 7 CHARACTERISTIC OF REMATISTICS OF RELATIONS 1. Ordering of tuples in a Relation(R)
VIJAYA COLLEGE
Page 11
VIJAYA COLLEGE
Page 12
VIJAYA COLLEGE
Page 13
VIJAYA COLLEGE
Page 14
VIJAYA COLLEGE
Page 15
(EMPLOYEE)
e. Ex: Select the tuples who either work in dept no 4 or make over Rs.25000 P.Mor, those whos salary greater than 30,000 P.M, or work in dept no 5.
(EMPLOYEE)
(EMPLOYEE)
(EMPLOYEE)
(EMPLOYEE)
VIJAYA COLLEGE
Page 16
(dno=5(EMPLOYEE)
(EMPLOYEE)
ii. RESULTS Lname, Fname, sal(DEP5-EMP) g. We can write this query in another way i. TEMP
dno=5
ii. F(FIRSTNAME, LASTNAME, SAL) Lname, Fname, sal(TEMP) 7. Set Theoretical Operations a. Consider two relations R and S. b. UNION of R and S i. The union of two relations is a relation that includes all the tuples that are either in R or in S or in both R and S. ii. Duplicate tuples are eliminated. iii. Ex: Retrieve the SSNs of all employees who either work in dept 5 or directly supervise an employee who works in dept 5 iv. In this section, we can query using UNION operation v. DEP5-EMP vi. RESULT1
(dno=5(EMPLOYEE)
(DEP5-EMP) (DEP5-EMP)
viii. RESULT RESULT1 U RESULT2 c. Student relation Fist name Last name GV P RV L MV S HT L Instructor Fist name Last name LD R RV L
VIJAYA COLLEGE
Page 17
sex=F
(EMPLOYEE)
(FEMALE_EMP )
VIJAYA COLLEGE
Page 18
SSN=ESSN
(EMP_DEPENDENTS)
v. RESULT Lname, Fname, Dependent_name(ACTUAL_DEPENDENTS) 9. Join Operation a. Is used to combine the related tuples from two relations into single tuples b. Do cross product of the two relations( improved version of CP). c. Becomes more complex, tuples are removed within the cross product to make the result of the join more meaningful. d. Syntax is X e. Join operations are i. Natural Join ii. Theta join iii. Equijoin join iv. Outer Join f. Natural Join i. When we omit the condition during joining we call it is natural joining ii. Let S and R are two relation, they have attribute like S(A1, A2, .Am, C1, C2.Ck) and R(C1, C2.Cl, B1, B2, Bn) iii. C1, C2.Cl are common attributes iv. We will use simple of * v. Ex: Department DNo Dame 1 CSE 2 ISE 3 MCA PROJECT Relation PNo PName DNo 10 DS 2 20 DBMS 2 30 TFCS 1 40 OS 3 vi. Natural join of Department and PROJECT is PNo PName DNo Dame 10 DS 2 ISE 20 DBMS 2 ISE 30 TFCS 1 CSE 40 OS 3 MCA vii. Here, joining is done over the attribute of DNo of Department and DNo of PROJECT viii. In PROJECT Relation, DNo is FK g. Theta join i. Used joining of two relations ii. is similar like Cartesian Product iii. Here, we will use condition iv. Let R and S are relations v. If x is attribute of R and y is attribute of S, then RXS where xy vi. Where, is .,=,>,=, etc.. h. Equijoin join i. Let R and S are relations ii. Syntax is R X <joint_condtion> S iii. We want to retrieve the name of manager of each department. iv. DEPT_MGR DEPARTMENT X MGRSSN=SSN(EMPLOYEE) v. RESULT Dname, Fname, Lname(DEPT_MGR) i. Outer Join i. Is similar like CP and Equijoin join ii. Tuples of a relation dont participate in join is called dangling tuples iii. Outer join operations are
VIJAYA COLLEGE
Page 19
(EMPLOYEE) SMITH)
g. RESULT Fname, Lname(SSNS*EMPLOYEE) 11. Grouping and Aggregation function a. All relational algebra operations are used to represent as relation consisting of rows and columns b. Aggregation functions are i. Sum, Age, Count, Maximum, Minimum ii. Group_attributes l Aggregate_functions(Relation) c. Retrieve dept no, no.of employees and their average salary i. RESULT(Dno, N, Avgsal) Dno lcount(SSN), Avg(sal)(EMPLOYEE) d. Participation the relation into groups e. Apply aggregate function to each group f. Retrieve dept no, no.of employees and their average age i. RESULT(Dno, N, Avgsal) Dno lcount(SSN), Avg(age)(EMPLOYEE) Examples of queries in Relational Algebra
VIJAYA COLLEGE
Page 20
(PROJECT)
(Works_On)
c. RESULT_EMP_SSNS EMP_PROJ DEPT5_PROJ d. RESULT Fname, Lname(RESULT_EMP_SSNS* EMPLOYEE) 14. Make list of project numbers that involves an employee who last name is Smith, either as a worker or a manager of dept that controls the department.
(EMPLOYEE)
(WORKS_ON * SMITH)
15. List the name of managers who have atleat one dependent.
MGRSSN(Department)
//Find MGRSSNs
EMP_With_Deps(ssn), ESSN(Dependent) MGRS_With_Deps (MGRS EMP_With_Deps) Result Fname, Lname(MGRS_With_Deps *Employee) ER To Relational Mapping Rules (ER to Table) 1. ER diagram it doesnt mean that logical dbase design is over all(constraints and proper set of relations). 2. Strong entity to table Rule 1: a. For every strong entity E, creates a table R that includes all attributes like primary key, etc. b. Employee table SSN Name Sal Add DoB 3. Rule 2: Weak entity to table a. How to identify weak entity i. Doesnt have primary key ii. Has partial key iii. Participates as one-to-many relations iv. If the participating owner entity is deleted, then all its references must be deleted. b. Convert weak entity to table EMPLOYEE SSN DOB Name Sal Add c. Rule 3: Mapping rule for ER relationship sets. ii. Entity sets are related with binary relationship iii. Relationship sets are WorksFor, Manages, Dependents, WorksOn, Control, Supervision. 1. Employee Workfor Department 2. Employee Manages Department 3. Employee Dependentsof Dependents 4. Employee WorksOn Project
VIJAYA COLLEGE
Page 21
CHAPER 4. STRUCTURE QUERY LANGUAGE (SQL) Introduction about SQL 1. It is used to represent a relational database, 2. Table is used to represent for relation. a. Rows for tuples. i. Ex: Records
VIJAYA COLLEGE
Page 22
VIJAYA COLLEGE
Page 23
Question 9: Retrieve all employees in Dep 5 whose salary is between $ 30,000 and $40,000. Select * From EMPLOYEE Where(Salary between 30000 and 40000) and DNO=5. Question 10: Retrieve all the employees whose address is in HOUSTON, TEXAS Select FN, LN From EMPLOYEE Where address like 1% HOUSTON, TEXAS% Question 11: Find all the employees who were born during the 1950s Select FN, LN From EMPLOYEE Where BOD like --5-,--,-Question 12: Retrieve the name of each employee who works in all projects Controlled by Dept 5
VIJAYA COLLEGE
Page 24
VIJAYA COLLEGE
Page 25
VIJAYA COLLEGE
Page 26
VIJAYA COLLEGE
Page 27
VIJAYA COLLEGE
Page 28
VIJAYA COLLEGE
Page 29
T i m e T i m e
VIJAYA COLLEGE
Page 30
T i m e T i m
VIJAYA COLLEGE
Page 31
Read, write Begin Commit End transaction Abort Abort Desirable properties of transactions 1. Transaction posses several properties 2. Atomicity: A transaction is an atomic unit of processing. It is either performed a task or not. 3. Consistency Preservation: A transaction is consistency preserving if its complete of dbase from one state to another. 4. Isolation: A transaction is being executed in isolation from other transactions. T1 should not interface with T2 while execution of T1. Active Partial committed Committed Failed Terminated 5. Durability: The changes applied to the dbase by using committed operation must persist in a dbase. Schedules of transactions: 1. Schedule has n transactions T1, T2, T3..Tn. 2. Schedule has ordering operations subject to constraint.
T1 T2
VIJAYA COLLEGE
Page 32
4. Sb : r1(x), w1(x), r2(x), w2(x), r1(y, a1 ; Serial Schedule : 1. Execute all operations of transaction T1 followed by all operations of transaction T2.
T1 T2 Read_Item(X); X:=X-N Write_Item(X); Read_Item(Y); Y:=Y+N; Write_Item(Y); Read_item(X) X:=X+M; Write_Item(X);
T i m e T i m e T i m e T
VIJAYA COLLEGE
Page 33
Serializable A Schedule S of n transactions is serializable, if it is equivalent to some serial schedule of the same transaction. X=100 S1 S2
Read_Item(X); Read_Item(X); X:=X+10 X:=X*1.1 Write_Item(X); Write_Item(X);
X=110; X=110;
VIJAYA COLLEGE
Page 34