620 likes | 874 Views
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)
E N D
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) Degree of relation=3 <no. of attribute> Tuple t1= < A-101, Downtown, 500> t2= < A-102, Perryridge, 400> …………… ……………
Relational Database Student = (Roll_no, name, age, contact, address) Course = ( course_id, course_name, course_coordinator) Subject =(Subject_id, sub_name, subject_credit) University = (Student, Course, Subject, …..)
Relational data model • Domain : Set of all possible values • Domain of account-number : set of all account number • Domain of branch name : set of all branch name
Domain • Atomic domain: Elements of the domain are indivisible units. • domain=(rakesh, mahesh, raj) • Domain=(rakeshkumar, maheshverma, raj mittal)
Relational data model Table Relation Record Tuple
Null values • Customer=(cus_id, name, address, contact_no) • Sandeep does not have any telephone. null
Null values • Customer=(cus_id, name, address, contact_no) • Address of customer not known at time of record entry. null Null values signify value is unknown or does not exists.
Super Key • Super key: set of one and more attributes that, taken collectively allow uniquely identify tuple in the relation. <Roll No> <Roll No, Student_name> <Roll no, Branch> <Roll no, student_name, branch> <Roll no, Student_name, Branch, address> Student_name, address>
Candidate key • Candidate key: Super key whose proper subset are not super key or minimal super key. <Roll No> <Roll No, Student_name> subset <Roll no> <Student name> <Roll no, Branch> subset < Roll no> <Branch> <Roll no, student_name, branch> <Roll no> < student_name> <branch> <Roll no, Student_name, Branch, address> <Student_name, address> = <Student_name> < Address> X X X X
key • R=<A1,A2,A3,…> • t=<v1,v2,v3,….> • If K is primary key of relation R then for any t1,t2 • t1[K] =/= t[K]
Constraints • Domain constraints • Key constraints • Entity integrity constraints • Referential integrity constraints
Domain Constraint • Value of attribute A must be atomic value from domain. • Domain=(rakesh, mahesh, sanjay) • Domain=(rakeshverma, maheshsingh, sanjaymittal)
Entity Integrity Constraint • No primary key value can be null. • Having null value implies, some tuples can’t be uniquely identified.
Foreign key • Primary key of a relation used in another relation then that key is known as foreign key. Faculty Dept
Referential Integrity Constraints • Domain of FK and PK must be same • Values in FK must be exist in PK or null.
Insert operation • Insert <null, manoj, noida, 8099889809, M.tech> into faculty Faculty Entity integrity constraints
Insert operation • Insert <4321, manoj, noida, 8099889809, M.tech> into faculty • Insert <4321, rakesh, delhi, 999889809, M.tech> into faculty Faculty Key constraints
Insert operation Insert <D102, 120124, EE> into Dept Dept Referential integrity constraints
Delete operation delete employee with id 120122 from faculty Faculty Dept Referential integrity constraints
Update operation Update id of faculty with id=120122 to id=120111 Faculty Dept Referential integrity constraints
Relational Algebra • Basic set of relational model operation. How to represent operation of relation. Example : if data of employee Amit is required then how to write this query
Relational Algebra(Select) • Basic set of relational model operation. Faculty All data is required σ ( Faculty )
Relational Algebra(Select) • Basic set of relational model operation. Faculty Record of kapil is required σname=kapil( Faculty )
Relational Algebra • Basic set of relational model operation. Faculty Record of faculties are required whose qualification is M.tech σqualification=M.tech( Faculty )
Relational Algebra Record of faculties are required whose qualification is M.tech and who live in noida σqualification=M.tech ^ address=noida( Faculty )
Relational Algebra Name of all faculties required π name( Faculty )
Relational Algebra Name of all faculties and their address required π name, address( Faculty )
Relational Algebra name of faculties are required whose qualification is M.tech π name(σqualification=M.tech( Faculty ))
Relational Algebra(union) New faculty name of all faculties are required π name ( Faculty ) Uπ name ( New_Faculty )
Relational Algebra(Set difference) Rfaculy ( faculty who left ) name of all faculties are required π name ( Faculty ) -π name ( RFaculty )
Relational Algebra(Cross product) Dept Emp Dept X Emp σDept.dept_id=emp.dept_id(Dept X Emp)
Assertions • An assertion is a predicate expressing a condition that we wish the database always to satisfy. • An assertion in SQL takes the form create assertion <assertion-name> check <predicate> • When an assertion is made, the system tests it for validity, and tests it again on every update that may violate the assertion • This 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)
Assertion Example • The amount of every account never less than 1000 Create assertion balance_constraint check(amount>1000)
Triggers • A 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.
Trigger Example • Suppose that instead of allowing negative account balances, the bank deals with overdrafts by • setting the account balance to zero • creating a loan in the amount of the overdraft • giving this loan a loan number identical to the account number of the overdrawn account • The condition for executing the trigger is an update to the account relation that results in a negative balance value.
Disadvantage • . 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.
SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Joined Relations Data Definition Language Embedded SQL, ODBC and JDBC
Create table • Create table faculty ( Employee_id number primary key, Name varchar(20), Address varchar(100), Contact number(10), Qualification varchar(5) )
Create table • Create table dept ( Dept_id number primary key, Hod number, dept_namevarchar(20), foreign key(Hod) references faculty(Employee_id) )
Delete operation delete employee with id 120122 from faculty Faculty Dept
Create table • Create table dept ( Dept_id number primary key, Hod number, dept_namevarchar(20), foreign key(Hod) references faculty(Employee_id) on delete cascade )
Delete operation delete employee with id 120122 from faculty Faculty Dept
Insert command • Insert into faculty (Employee_id, Name, Address, Contact, Qualification) values (12012, ‘Kapil’, ‘Noida’, 8739748931,’Mtech’)
Delete command • Delete from faculty where Name=‘Amit’; • Delete from faculty;
Truncate • Used to delete all rows of table • Syntax: • Truncate table faculty;