56
Relational data model Concepts

Relational data model

  • Upload
    kesia

  • View
    66

  • Download
    1

Embed Size (px)

DESCRIPTION

Relational data model. Concepts. Relational data model. Represent database as collection of relations. In relational model row is known as tuple , column header known as attribute, and table known as relation. Relation Schema. Account = (account-number, branch-name, balance) - PowerPoint PPT Presentation

Citation preview

Relational data model

Relational data modelConcepts1Relational data modelRepresent database as collection of relations.In relational model row is known as tuple, column header known as attribute, and table known as relation.2Relation Schema

Account = (account-number, branch-name, balance)Degree of relation=3 Tuplet1= < A-101, Downtown, 500>t2= < A-102, Perryridge, 400>

3Relational DatabaseStudent = (Roll_no, name, age, contact, address)Course = ( course_id, course_name, course_coordinator)Subject =(Subject_id, sub_name, subject_credit)

University = (Student, Course, Subject, ..)4Relational data model

5Relational data modelDomain : Set of all possible values

Domain of account-number : set of all account number

Domain of branch name : set of all branch name

6DomainAtomic domain: Elements of the domain are indivisible units.

domain=(rakesh, mahesh, raj)

Domain=(rakesh kumar, mahesh verma, raj mittal)

7Relational data model

TableRelationRecordTuple8Null valuesCustomer=(cus_id, name, address, contact_no)Sandeep does not have any telephone.

Cus idnameaddresscontactc1230982Sandeepnoidanull9Null valuesCustomer=(cus_id, name, address, contact_no)Address of customer not known at time of record entry.

Cus idnameaddresscontactc1230982Sandeep9987987731Null values signify value is unknown or does not exists.null10Super KeySuper key: set of one and more attributes that, taken collectively allow uniquely identify tuple in the relation.Roll NoStudent NameBranchaddress

Student_name, address>11Candidate key

subset subset < Roll no> < student_name>

= < Address>Candidate key: Super key whose proper subset are not super key or minimal super key.XXXX12keyR=

t=

If K is primary key of relation R then for any t1,t2t1[K] =/= t[K]13ConstraintsDomain constraintsKey constraintsEntity integrity constraintsReferential integrity constraints14Domain ConstraintValue of attribute A must be atomic value from domain.

Domain=(rakesh, mahesh, sanjay)

Domain=(rakesh verma, mahesh singh, sanjay mittal)15Chapter 5-16

5.5Relational database2/11/201316Entity Integrity ConstraintNo primary key value can be null.Having null value implies, some tuples cant be uniquely identified.17Primary key of a relation used in another relation then that key is known as foreign key.Foreign keyEmployee idNameAddressContactQualificationFacultyDept_idHod_idDept_nameDept_total_studentDept18Referential Integrity ConstraintsDomain of FK and PK must be sameValues in FK must be exist in PK or null.19Chapter 5-20

5.72/11/201320Insert operationInsert into facultyEmployee idNameAddressContactQualificationFacultyEntity integrity constraints21Insert operationInsert into facultyInsert into faculty

Employee idNameAddressContactQualificationFacultyKey constraints22Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471B.techDept idHodDept nameD101120122CSInsert into DeptDeptInsert operationReferential integrity constraints23Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471B.techDept idHodDept nameD101120122CSdelete employee with id 120122 from facultyDeptDelete operationFacultyReferential integrity constraints24Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471B.techDept idHodDept nameD101120122CSUpdate id of faculty with id=120122 to id=120111DeptUpdate operationFacultyReferential integrity constraints25Relational AlgebraBasic set of relational model operation.Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471B.techHow to represent operation of relation.

Example : if data of employee Amit is required then how to write this query26Relational Algebra(Select)Basic set of relational model operation.Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471B.techAll data is required

( Faculty )Faculty27Relational Algebra(Select)Basic set of relational model operation.Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471M.techRecord of kapil is required

name=kapil ( Faculty )Faculty28Relational AlgebraBasic set of relational model operation.Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471M.techRecord of faculties are required whose qualification is M.tech

qualification=M.tech ( Faculty )Faculty29Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471M.techRelational AlgebraRecord of faculties are required whose qualification is M.tech and who live in noida

qualification=M.tech ^ address=noida( Faculty )30Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471M.techRelational AlgebraName of all faculties required

name( Faculty )31Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471M.techRelational AlgebraName of all faculties and their address required

name, address( Faculty )32Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471M.techRelational Algebraname of faculties are required whose qualification is M.tech

name( qualification=M.tech ( Faculty ))33Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471M.techRelational Algebra(union)name of all faculties are required

name ( Faculty ) U name ( New_Faculty ) Employee_idNameAddressContactQualification13012Hemhyderabad8733748912M.tech13013Ashishdelhi7788878562M.techNew faculty34Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471M.techRelational Algebra(Set difference)name of all faculties are required

name ( Faculty ) - name ( RFaculty ) Employee_idNameAddressContactQualification120122Amitdelhi9988878782PhdRfaculy ( faculty who left ) 35Relational Algebra(Cross product)Dept idDept name102323Management102324FinancialEmp idDept idnameE121102323davidE122102324kevinDeptEmpDept X EmpDept idDept nameEmp idDept idname102323ManagementE121102323david102324FinancialE121102323david102323ManagementE122102324kevin102324FinancialE122102324kevin Dept.dept_id=emp.dept_id(Dept X Emp)36AssertionsAn assertion is a predicate expressing a condition that we wish the database always to satisfy.An assertion in SQL takes the formcreate assertion check When an assertion is made, the system tests it for validity, and tests it again on every update that may violate the assertionThis testing may introduce a significant amount of overhead; hence assertions should be used with great care.Asserting for all X, P(X) is achieved in a round-about fashion using not exists X such that not P(X)37Assertion ExampleThe amount of every account never less than 1000

Create assertion balance_constraint check(amount>1000) 38TriggersA trigger is a statement that is executed automatically by the system as a side effect of a modification to the database.To design a trigger mechanism, we must:Specify the conditions under which the trigger is to be executed.Specify the actions to be taken when the trigger executes.39Trigger Example Suppose that instead of allowing negative account balances, the bank deals with overdrafts by setting the account balance to zerocreating a loan in the amount of the overdraftgiving this loan a loan number identical to the account number of the overdrawn accountThe condition for executing the trigger is an update to the account relation that results in a negative balance value.40Disadvantage. Simple validations can be done at the application level itself . Triggers executes invisibly from client-application which connects to the database server.So it is difficult to figure out what happens at the database layer. Triggers runs on every update made to the table therefore it adds more load to the database and cause the system to run slow.41SQLBasic Structure Set OperationsAggregate FunctionsNull ValuesNested SubqueriesDerived RelationsViewsModification of the Database Joined RelationsData Definition Language Embedded SQL, ODBC and JDBC1Create tableCreate table faculty( Employee_id number primary key, Name varchar(20), Address varchar(100), Contact number(10), Qualification varchar(5)) Employee idNameAddressContactQualificationDept idHodDept name43Create tableCreate table dept( Dept_id number primary key, Hod number, dept_name varchar(20), foreign key(Hod) references faculty(Employee_id)) Employee idNameAddressContactQualificationDept idHodDept name44Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471B.techDept idHodDept nameD101120122CSdelete employee with id 120122 from facultyDeptDelete operationFaculty45Create tableCreate table dept( Dept_id number primary key, Hod number, dept_name varchar(20), foreign key(Hod) references faculty(Employee_id) on delete cascade) Employee idNameAddressContactQualificationDept idHodDept name46Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471B.techDept idHodDept nameD101120122CSdelete employee with id 120122 from facultyDeptDelete operationFaculty47Insert commandInsert into faculty (Employee_id, Name, Address, Contact, Qualification) values (12012, Kapil, Noida, 8739748931,Mtech)

Employee idNameAddressContactQualification12012KapilNoida8739748931M.tech48Delete commandDelete from faculty where Name=Amit;

Delete from faculty;

Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471B.tech49Truncate Used to delete all rows of tableSyntax:Truncate table faculty;50Update CommandUpdate faculty set Address=Noida;Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471B.techEmployee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122AmitNoida9988878782Phd120123ManojNoida7837483471B.tech51Update CommandUpdate faculty set Address=Noida where Name=Manoj;Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471B.techEmployee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122AmitDelhi9988878782Phd120123ManojNoida7837483471B.tech52Schema of tableDesc

Desc faculty;

53Select Select Employee_id from faculty;

Select Employee_id, Name from faculty

Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471B.tech54SelectSelect emp_id, dept_id,name, dept_name from dept, emp where dept.dept_id=emp.emp_id;Dept idDept name102323Management102324FinancialEmp idDept idnameE121102323davidE122102324kevinDept nameEmp idDept idnameManagementE121102323davidFinancialE122102324kevin55Select(Select name from faculty) union (select name from new_faculty) Employee_idNameAddressContactQualification120121KapilNoida8739748931M.tech120122Amitdelhi9988878782Phd120123ManojGhajiyabad7837483471M.techEmployee_idNameAddressContactQualification13012Hemhyderabad8733748912M.tech13013Ashishdelhi7788878562M.techFacultyNew_Faculty56