Presentation is loading. Please wait.

Presentation is loading. Please wait.

More SQL Data Management 2005. Topics zIntegrity Constraints zOuter Join zUnion.

Similar presentations


Presentation on theme: "More SQL Data Management 2005. Topics zIntegrity Constraints zOuter Join zUnion."— Presentation transcript:

1 More SQL Data Management 2005

2 Topics zIntegrity Constraints zOuter Join zUnion

3 Types of ‘Integrity’ Constraints zStatic zTrue for all states zState invariant zTrue at all times ze.g. for all e in emp, e.dept in (dept.deptno) z Transition z Between two states of the system S,S’ z Conditions that must apply before a change, and then apply after z pre- and post-conditions z e.g. e’.sal > e.sal i.e. any wage change must be upwards!

4 Types of Database Function zQuery zExtracts existing facts and derives new facts zLeaves Factbase unchanged zSELECT … z Update z Reflects some change in the UoD z Changes state of factbase z Must check integrity constraints z UPDATE.., INSERT.., DELETE..

5 DDL zCREATE TABLE ydefine the table name ydefine the fields of the table ydefine constraints on the table zMODIFY TABLE yadd a field or constraint zDROP TABLE ydelete the table

6 DDL commands zCREATE TABLE EMP ( Empno char(8) not null, … Constraint emp_pk primary key(empno) ); zALTER TABLE EMP yADD SPOUSE VARCHAR(40) ); zDROP TABLE EMP;

7 Integrity Constraints and Business Rules zDatabase integrity yRules which ensure that relationships are well- defined zBusiness Rules yAdditional constraints on values and interrelationships

8 Data integrity zPrimary key xCONSTRAINT emp_pk PRIMARY KEY (empno) zNot Null xEname NOT NULL zField Validation xCONSTRAINT emp_sal CHECK (sal <6000) xCONSTRAINT emp_job CHECK (job IN (‘analyst,’salesman’, ‘manager’, ‘president’, ‘clerk’))

9 Foreign Key zRelationship implentation y‘You take the primary key of the one side and add it to the many side” yBut there is no reference to which table the key came from! zForeign key constraint xCONSTRAINT emp-deptfk FOREIGN KEY (deptno) REFERENCES dept NO ACTION – reject attempt to delete parent if children exist CASCADE – delete all the child records if the parent is deleted SET NULL – set foreign key field to null SET DEFAULT – set foreign key field to its default

10 dept deptno dname location emp empno ename not null job not null hiredate sal comm manager The EMP DEPT database

11 Coding constraints Exercise zCode up all the constraints for the emp- dept database

12 Outer Join zInner join returns only matching rows zWe can get non-matching rows too yA LEFT JOIN B xincludes all the non matching rows in A as well yA RIGHT JOIN B xincludes all the non matching rows in B as well ya FULL JOIN B xincludes non matching rows from both tables

13 yMF x23 fred23 sue x20 joe30 alice x34 bill34 julie ySelect * from M,F y23 fred23 sue y23 fred30 alice y23 fred34 julie y20 joe23 sue y20 joe30 alice y20 joe34 julie y34 bill23 sue y34 bill30 alice y34 bill 34 julie y

14 yMF x23 fred23 sue x20 joe30 alice x34 bill34 julie ySelect * from M inner join F on M.age = F.age y23 fred23 sue y23 fred30 alice y23 fred34 julie y20 joe23 sue y20 joe30 alice y20 joe34 julie y34 bill23 sue y34 bill30 alice y34 bill 34 julie y

15 yMF x23 fred23 sue x20 joe30 alice x34 bill34 julie ySelect * from M left join on M.age = F.age y23 fred23 sue y34 bill 34 julie y

16 yMF x23 fred23 sue x20 joe30 alice x34 bill34 julie ySelect * from M right join on M.age = F.age y23 fred23 sue y34 bill 34 julie y

17 yMF x23 fred23 sue x20 joe30 alice x34 bill34 julie ySelect * from M full join on M.age = F.age y23 fred23 sue y34 bill 34 julie y

18 yMF x23 fred23 sue x20 joe30 alice x34 bill34 julie ySelect * from M, F on M.age > F.age y23 fred23 sue y23 fred30 alice y23 fred34 julie y20 joe23 sue y20 joe30 alice y20 joe34 julie y34 bill23 sue y34 bill30 alice y34 bill 34 julie y

19 zInner Join (natural Join) xSelect * xfrom M inner join F on M.age=F.age; zLeft (Outer) Join xSelect * xfrom M left join F on M.age=F.age; zRight (Outer) Join xSelect * xfrom M right join F on M.age=F.age; zFull (outer) Join xSelect * xfrom M full join F on M.age=F.age;

20 zShow all departments without employees ySelect dname yfrom dept left join emp yon dept.deptno = emp.deptno xshows all the department names ywhere ename IS NULL; xshows only the departments without employees

21 Union zWe can Union two or more tables in the one statement zHandy if the separate select statements use different tables zwhat does this query do? Select empno, ename, job from emp where job='Manager' union Select m.empno, m.ename, m.job from emp e, emp m where m.empno = e.mgr;


Download ppt "More SQL Data Management 2005. Topics zIntegrity Constraints zOuter Join zUnion."

Similar presentations


Ads by Google