more sql data management 2005. topics zintegrity constraints zouter join zunion

21
More SQL Data Management 2005

Upload: preston-caldwell

Post on 14-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

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

More SQL

Data Management 2005

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

Topics

Integrity ConstraintsOuter JoinUnion

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

Types of ‘Integrity’ Constraints

Static True for all states State invariant True at all times e.g. for all e in emp,

e.dept in (dept.deptno)

Transition Between two states of

the system S,S’ Conditions that must

apply before a change, and then apply after

pre- and post-conditions e.g. e’.sal > e.sal i.e.

any wage change must be upwards!

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

Types of Database Function

QueryExtracts existing

facts and derives new facts

Leaves Factbase unchanged

SELECT …

UpdateReflects some

change in the UoDChanges state of

factbaseMust check

integrity constraints

UPDATE .., INSERT.., DELETE..

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

DDL

CREATE TABLE define the table name define the fields of the table define constraints on the table

MODIFY TABLE add a field or constraint

DROP TABLE delete the table

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

DDL commands

CREATE TABLE EMP (Empno char(8) not null,…Constraint emp_pk primary key(empno));

ALTER TABLE EMP ADD SPOUSE VARCHAR(40) );

DROP TABLE EMP;

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

Integrity Constraints and Business Rules

Database integrity Rules which ensure that relationships are

well-definedBusiness Rules

Additional constraints on values and interrelationships

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

Data integrity

Primary key CONSTRAINT emp_pk PRIMARY KEY (empno)

Not Null Ename NOT NULL

Field Validation CONSTRAINT emp_sal CHECK (sal <6000)CONSTRAINT emp_job CHECK (job IN

(‘analyst,’salesman’, ‘manager’, ‘president’, ‘clerk’))

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

Foreign Key

Relationship implentation ‘You take the primary key of the one side and

add it to the many side” But there is no reference to which table the

key came from!Foreign key constraint

CONSTRAINT 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

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

dept

deptnodnamelocation

emp

empnoename not nulljob not nullhiredatesalcomm

manager

The EMP DEPT database

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

Coding constraints Exercise

Code up all the constraints for the emp-dept database

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

Outer Join

Inner join returns only matching rowsWe can get non-matching rows too

A LEFT JOIN Bincludes all the non matching rows in A as well

A RIGHT JOIN Bincludes all the non matching rows in B as well

a FULL JOIN Bincludes non matching rows from both tables

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

M F23 fred 23 sue20 joe 30 alice34 bill 34 julie

Select * from M ,F 23 fred 23 sue 23 fred 30 alice 23 fred 34 julie 20 joe 23 sue 20 joe 30 alice 20 joe 34 julie 34 bill 23 sue 34 bill 30 alice 34 bill 34 julie

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

M F23 fred 23 sue20 joe 30 alice34 bill 34 julie

Select * from M inner join F on M.age = F.age 23 fred 23 sue 23 fred 30 alice 23 fred 34 julie 20 joe 23 sue 20 joe 30 alice 20 joe 34 julie 34 bill 23 sue 34 bill 30 alice 34 bill 34 julie

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

M F23 fred 23 sue20 joe 30 alice34 bill 34 julie

Select * from M left join on M.age = F.age 23 fred 23 sue 34 bill 34 julie

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

M F23 fred 23 sue20 joe 30 alice34 bill 34 julie

Select * from M right join on M.age = F.age 23 fred 23 sue 34 bill 34 julie

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

M F23 fred 23 sue20 joe 30 alice34 bill 34 julie

Select * from M full join on M.age = F.age 23 fred 23 sue 34 bill 34 julie

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

M F23 fred 23 sue20 joe 30 alice34 bill 34 julie

Select * from M, F on M.age > F.age 23 fred 23 sue 23 fred 30 alice 23 fred 34 julie 20 joe 23 sue 20 joe 30 alice 20 joe 34 julie 34 bill 23 sue 34 bill 30 alice 34 bill 34 julie

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

Inner Join (natural Join)Select * from M inner join F on M.age=F.age;

Left (Outer) JoinSelect * from M left join F on M.age=F.age;

Right (Outer) JoinSelect * from M right join F on M.age=F.age;

Full (outer) JoinSelect * from M full join F on M.age=F.age;

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

Show all departments without employees Select dname from dept left join emp on dept.deptno = emp.deptno

shows all the department names

where ename IS NULL;shows only the departments without

employees

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

Union

We can Union two or more tables in the one statement

Handy if the separate select statements use different tables

what does this query do?Select empno, ename, job from emp where

job='Manager'unionSelect m.empno, m.ename, m.job from emp e,

emp m where m.empno = e.mgr;