Download presentation
Presentation is loading. Please wait.
Published byLuke Flynn Modified over 9 years ago
1
1 Lecture 4 Data Integrity: Primary and Foreign Keys
2
2 But first - table aliases zTable name prefixes prevent ambiguity in a query remember emp.deptno=dept.deptno zBut they can be tedious to enter zSo we can define temporary labels in the FROM clause and use them elsewhere in the query. These temporary labels are called table aliases
3
3 Example without aliases zTo list information about all the employees in Chicago, enter: SELECTDEPT.DEPTNO, DNAME, LOC, ENAME, JOB FROM EMP, DEPT WHEREEMP.DEPTNO = DEPT.DEPTNO ANDEMP.DEPTNO = 30;
4
4 Abbreviating a Table Name using aliases ( Again to list information about all the employees in Chicago) SELECT D.*, ENAME, JOB FROM EMP E, DEPT D WHERE E.DEPTNO = D.DEPTNO AND E.DEPTNO = 30; Alias D.* retrieves all of the columns of the DEPT table This produces the same results as the last query Aliases E and D defined in the FROM clause The ‘join- condition’ defines the relationship between the two tables
5
5
6
6 Joining a table to itself zAliasing allows you to join a table to itself as though it was two separate tables. zUseful when we want to join one row in a table to another row in the same table. EMP MANAGER WORKER
7
7 List employees whose salary exceeds their manager’s SELECT WORKER.ENAME, WORKER.SAL, MANAGER.ENAME, MANAGER.SAL FROM EMP WORKER, EMP MANAGER WHEREWORKER.MGR = MANAGER.EMPNO ANDWORKER.SAL > MANAGER.SAL;
8
8 Manager Worker
9
9 Result is ENAMESAL SCOTT3000 JONES2975 FORD3000 JONES2975 zSCOTT’s row is joined to JONES’s because SCOTT’s MGR is 7566 and JONES’s EMPNO is 7566
10
10 Topics zReferential Integrity yTable constraints: xPrimary Keys xForeign Keys xOn delete cascade xCheck xDefault
11
11 Referential Integrity zOracle 7 onwards allows the use of table constraints to enforce referential integrity. That is, the data in tables must be consistent. Imagine inserting an employee with department 60! The table constraints allow us to enforce that this cannot happen. z(reading chapter 5 Dowling)
12
12 Definition zReferential Integrity; rules in a database that ensure that data is valid and consistent. zWhereas individual constraints can be used to ensure the validity of data in a single column or a table, referential integrity constraints ensure the validity of data between tables. – N.Dowling Database Design and Management (1998)
13
13 Primary Key zColumn(s) can have the primary key constraint placed on them, this has the following affect: yNo duplicate values yUnique index created for column(s) yCannot have NULL values
14
14 Primary Key zExamples from “our” tables are empno in myemp and deptno in mydept. zOnly one Primary key is allowed per table but the key can consist of several columns
15
15 Foreign Key zReferences a primary key in another table, example deptno in myemp (foreign key) references deptno in mydept (primary key) and is thus used to join the tables. zForeign Keys can also be specified in table constraints.
16
16 deptnodname loc empnoenamejobdeptno. MYDEPT TABLE primary key foreign key MYEMP TABLE mgrhiredate
17
17 Referential Integrity zTo examine this let us look at an example zCurrently (as there are no table constraints) we can insert an employee with department number 60. INSERT INTO myemp (empno, deptno) VALUES (1234, 60);
18
18
19
19 Table Constraints zWe can use alter table to add constraints: ALTER TABLE mydept ADD CONSTRAINT tab_mydept_pk PRIMARY KEY (deptno); zThis defines deptno as a primary key in table mydept. This means NULLs are no longer allowed and every value must be unique.
20
20 Table Constraints zIn the previous example, we named the constraint tab_mydept_pk. The name is arbitrary but using a name like tab_mydept_pk tells us a lot about the constraint. zOracle will use this name in any error messages it generates (thus helping us to identify the cause of the problem).
21
21 Table Constraints zNow we add the foreign key constraint to the myemp table ALTER TABLE myemp ADD CONSTRAINT tab_myemp_fk FOREIGN KEY (deptno) REFERENCES mydept (deptno);
22
22 Table Constraints zNow try to insert an employee with department number 60. zINSERT INTO myemp (empno, deptno) VALUES (1234, 60); zERROR at line 1: ORA-02291: integrity constraint (SPB001.TAB_MYEMP_FK) violated - parent key not found
23
23 Table Constraints zWe can also ensure that if a master or primary key row is deleted all the foreign or detail records get deleted with the ON DELETE CASADE constraint
24
24 Table Constraints zALTER TABLE myemp ADD CONSTRAINT tab_emp_fk FOREIGN KEY (deptno) REFERENCES mydept (deptno) ON DELETE CASCADE;
25
25 Table Constraints zChecks can also be applied to data or defaults supplied with table constraints. These are used to define conditions that every row must fulfil. zCONSTRAINT salary_check CHECK (sal > 2000 and deptno =10) zThis condition can ONLY refer to columns within the same table
26
26 Table Constraints zThe default constraint defines a value for a column that will be assigned whenever a row is inserted that does not have an explicit value for the column zCREATE TABLE little_example (city VARCHAR2(20) DEFAULT (‘Stirling’), text VARCHAR2(5) DEFAULT (‘#####’));
27
27 Table Constraints zThe unique constraint ensure that one or more columns are unique within that table. Unlike a primary key, NULLs are allowed. zCREATE TABLE another_example (cityVARCHAR(20) UNIQUE);
28
28 Table Constraints zCan dropped, disabled and enabled ALTER TABLE DROP CONSTRAINT ; ALTER TABLE DISABLE CONSTRAINT ; ALTER TABLE ENABLE CONSTRAINT ;
29
29 Table Constraints zThese can be placed on a table when it is created. CREATE TABLE new_emp (a_empno NUMBER(4), …, CONSTRAINT tab_new_emp_pk PRIMARY KEY(a_empno),…);
30
30 Summary zReferential Integrity yTable constraints: xPrimary Keys xForeign Keys xOn delete cascade xCheck xDefault
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.