my slide understanding keys

33
Rushdi Shams, Dept of CSE, KU ET 1 Database Systems Database Systems Understanding Keys Understanding Keys Entity Integrity Entity Integrity Referential Integrity Referential Integrity Propagation Constraint Propagation Constraint Version 1.0 Version 1.0

Upload: rushdi-shams

Post on 19-Jan-2015

61 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: My slide  understanding keys

Rushdi Shams, Dept of CSE, KUET 1

Database Database SystemsSystems

Understanding KeysUnderstanding Keys

Entity IntegrityEntity Integrity

Referential IntegrityReferential Integrity

Propagation ConstraintPropagation Constraint

Version 1.0Version 1.0

Page 2: My slide  understanding keys

2Rushdi Shams, Dept of CSE, KUET

What is keyWhat is key

A key is a CONSTRAINT in field/ A key is a CONSTRAINT in field/ column in a tablecolumn in a table

Generally, keys are essential in Generally, keys are essential in database systems to invoke some database systems to invoke some integrityintegrity

By a key, you can tag a field in a By a key, you can tag a field in a table so that the field can be used to table so that the field can be used to relate tablesrelate tables

Page 3: My slide  understanding keys

3Rushdi Shams, Dept of CSE, KUET

Types of keyTypes of key

There are 3 types of keys in general. There are 3 types of keys in general. They are-They are-

Primary keyPrimary key Unique keyUnique key Foreign keyForeign key

Page 4: My slide  understanding keys

4Rushdi Shams, Dept of CSE, KUET

Primary keyPrimary key Used to uniquely identify a row in a tableUsed to uniquely identify a row in a table Also called Also called Surrogate keySurrogate key Say, you have a table of names of the Say, you have a table of names of the

students of your class and their ages. students of your class and their ages. How can you differ between them? How can you differ between them? Because many of you have the same Because many of you have the same name! don’t you? So, what happens if name! don’t you? So, what happens if you try to find out a name “Rezwana” you try to find out a name “Rezwana” and you find there are 5 Rezwanas in the and you find there are 5 Rezwanas in the class?class?

That invokes the idea of primary keyThat invokes the idea of primary key

Page 5: My slide  understanding keys

5Rushdi Shams, Dept of CSE, KUET

Primary key (continued)Primary key (continued)

This problem won’t be serious if you This problem won’t be serious if you had any identifying criteria to differ had any identifying criteria to differ between 5 Rezwanas.between 5 Rezwanas.

You still can identify 5 Rezwanas by You still can identify 5 Rezwanas by adding an extra column in that table. adding an extra column in that table. Can you say what field that might be?Can you say what field that might be?

That is your roll number! So, 5 That is your roll number! So, 5 Rezwanas can be easily identifed by Rezwanas can be easily identifed by their roll numbers.their roll numbers.

So, roll number is the primary key here.So, roll number is the primary key here.

Page 6: My slide  understanding keys

6Rushdi Shams, Dept of CSE, KUET

Primary key (continued)Primary key (continued)

Page 7: My slide  understanding keys

7Rushdi Shams, Dept of CSE, KUET

Primary key (continued)Primary key (continued)

Page 8: My slide  understanding keys

8Rushdi Shams, Dept of CSE, KUET

Unique KeyUnique Key

Unique keys are applied on the Unique keys are applied on the column of a tablecolumn of a table

Any field that is UNIQUE key, you Any field that is UNIQUE key, you will have to remember that no 2 will have to remember that no 2 entries in that column in that table entries in that column in that table will be the samewill be the same

So, are you thinking “why there are So, are you thinking “why there are primary keys and unique keys? They primary keys and unique keys? They are same, no?”are same, no?”

Page 9: My slide  understanding keys

9Rushdi Shams, Dept of CSE, KUET

Unique key (continued)Unique key (continued) No, they are not the same. In real life, when No, they are not the same. In real life, when

you are creating tables, you sometimes will you are creating tables, you sometimes will not want to let the table have the same entry not want to let the table have the same entry twice (even though that is not the primary twice (even though that is not the primary key)key)

An example can be, if you have a course table An example can be, if you have a course table with primary key COURSE ID and another with primary key COURSE ID and another field COURSE NAME- take a look! You will field COURSE NAME- take a look! You will not allow the data entry operator to entry the not allow the data entry operator to entry the same subject name twice, do you?same subject name twice, do you?

So, making COURSE NAME as UNIQUE will So, making COURSE NAME as UNIQUE will give you that opportunitygive you that opportunity

Page 10: My slide  understanding keys

10Rushdi Shams, Dept of CSE, KUET

Foreign KeyForeign Key

Foreign keys are copies of primary Foreign keys are copies of primary key in the child tablekey in the child table

When two tables are in relation, then When two tables are in relation, then child table is that table which child table is that table which depends on the other.depends on the other.

Page 11: My slide  understanding keys

11Rushdi Shams, Dept of CSE, KUET

Foreign key (continued)Foreign key (continued)

Page 12: My slide  understanding keys

12Rushdi Shams, Dept of CSE, KUET

Creating Primary keysCreating Primary keys

CREATE TABLE student(CREATE TABLE student(

student_idstudent_id number(10)number(10) NOT NOT NULLNULL,,

student_namestudent_name varchar(20),varchar(20),

PRIMARY KEY(student_id)PRIMARY KEY(student_id)

););

Page 13: My slide  understanding keys

13Rushdi Shams, Dept of CSE, KUET

Creating Unique keysCreating Unique keys

CREATE TABLE course(CREATE TABLE course(

course_idcourse_id varchar(10)varchar(10) NOT NOT NULL,NULL,

course_namecourse_namevarchar(20)varchar(20)UNIQUEUNIQUE,,

PRIMARY KEY(course_id)PRIMARY KEY(course_id)

););

Page 14: My slide  understanding keys

14Rushdi Shams, Dept of CSE, KUET

Creating Foreign keysCreating Foreign keys

CREATE TABLE address(CREATE TABLE address(student_idstudent_id number(10)number(10) NOT NULL,NOT NULL,student_addressstudent_address varchar(30),varchar(30),postcodepostcode varchar(10),varchar(10),PRIMARY KEY (student_id),PRIMARY KEY (student_id),FOREIGN KEY(student_id) REFERENCES FOREIGN KEY(student_id) REFERENCES studentstudent

););

BackBack

Page 15: My slide  understanding keys

15Rushdi Shams, Dept of CSE, KUET

Case StudyCase Study

Page 16: My slide  understanding keys

16Rushdi Shams, Dept of CSE, KUET

Case study (continued)Case study (continued)

Page 17: My slide  understanding keys

17Rushdi Shams, Dept of CSE, KUET

Case study (continued)Case study (continued)CREATE TABLE Band(CREATE TABLE Band(

band_idband_idvarchar(10)varchar(10)NOT NULL,NOT NULL,

band_nameband_name char(20),char(20),PRIMARY KEY(band_id)PRIMARY KEY(band_id)

););CREATE TABLE Track(CREATE TABLE Track(

track_idtrack_id varchar(10)varchar(10)NOT NULL,NOT NULL,

band_idband_idvarchar(10),varchar(10),track_nametrack_name char(20),char(20),descriptiondescription varchar(50),varchar(50),PRIMARY KEY(track_id),PRIMARY KEY(track_id),FOREIGN KEY (band_id) FOREIGN KEY (band_id) REFERENCES BandREFERENCES Band

););

Page 18: My slide  understanding keys

18Rushdi Shams, Dept of CSE, KUET

Important Fact!Important Fact!

A child table may not have any A child table may not have any primary key of its own. It can make primary key of its own. It can make its foreign keys as its primary keys its foreign keys as its primary keys as well!as well!

Page 19: My slide  understanding keys

19Rushdi Shams, Dept of CSE, KUET

Entity IntegrityEntity Integrity

Entity integrity concerns with Entity integrity concerns with primary keyprimary key

It states that “Every table must have It states that “Every table must have a primary key” and “the column(s) a primary key” and “the column(s) that make up the primary key must that make up the primary key must be unique to identify a row and NOT be unique to identify a row and NOT NULL”NULL”

Page 20: My slide  understanding keys

20Rushdi Shams, Dept of CSE, KUET

Referential Integrity & Referential Integrity & Propagation ConstraintPropagation Constraint

It ensures the integrity of referential It ensures the integrity of referential relationships between tablesrelationships between tables

In a relation between 2 tables, one table In a relation between 2 tables, one table has primary key and the other a foreign keyhas primary key and the other a foreign key

Referential integrity ensures the integrity Referential integrity ensures the integrity of the values of primary and foreign key of of the values of primary and foreign key of parent and child tableparent and child table

Most database engines call these criteria Most database engines call these criteria CONSTRAINTS. Primary and foreign keys CONSTRAINTS. Primary and foreign keys are also constraintsare also constraints

Page 21: My slide  understanding keys

21Rushdi Shams, Dept of CSE, KUET

Referential Integrity & Referential Integrity & Propagation Constraint Propagation Constraint

(continued)(continued) A primary key table is assumed as A primary key table is assumed as

parent table and a foreign key table is parent table and a foreign key table is assumed as child tableassumed as child table

If a value in foreign key column in child If a value in foreign key column in child table is entered, it MUST be present in table is entered, it MUST be present in primary key column in parent tableprimary key column in parent table

Foreign key can have NULL values, but Foreign key can have NULL values, but primary key can NEVER have NULL primary key can NEVER have NULL valuesvalues

Page 22: My slide  understanding keys

22Rushdi Shams, Dept of CSE, KUET

Referential Integrity & Referential Integrity & Propagation Constraint Propagation Constraint

(continued)(continued) If the primary key value is changed it should If the primary key value is changed it should

be CASCADED to force to change foreign key be CASCADED to force to change foreign key values as wellvalues as well

Changing a value in foreign key must check Changing a value in foreign key must check firstly the existence of its primary key. If the firstly the existence of its primary key. If the foreign key is changed to NULL, no primary foreign key is changed to NULL, no primary key is required. If it is changed to NON key is required. If it is changed to NON NULL, the check for primary key is requiredNULL, the check for primary key is required

If deletion of primary key is required, it is If deletion of primary key is required, it is required to delete foreign key along with it required to delete foreign key along with it (CASCADE) or foreign key firstly then (CASCADE) or foreign key firstly then primary keyprimary key

Page 23: My slide  understanding keys

23Rushdi Shams, Dept of CSE, KUET

Referencial Integrity Referencial Integrity (continued)(continued)

Creating referential integrity is Creating referential integrity is easy! You have already done that. easy! You have already done that. Look at the REFERENCES keyword Look at the REFERENCES keyword here. That is ensuring your here. That is ensuring your referential integrity.referential integrity.

Creating Foreign keysCreating Foreign keys

Page 24: My slide  understanding keys

24Rushdi Shams, Dept of CSE, KUET

Propagation Constraint Propagation Constraint (continued)(continued)

A propagation constraint is a rule about what to do if a row A propagation constraint is a rule about what to do if a row of a table that is referenced is modified or even deleted.of a table that is referenced is modified or even deleted.

CREATE TABLE address(CREATE TABLE address(student_idstudent_id number(10)number(10) NOT NULL,NOT NULL,student_addressstudent_address varchar(30),varchar(30),postcodepostcode varchar(10),varchar(10),PRIMARY KEY (student_id),PRIMARY KEY (student_id),FOREIGN KEY(student_id) REFERENCES studentFOREIGN KEY(student_id) REFERENCES studentON DELETE CASCADEON DELETE CASCADE

););This simply means that if parent table’s student_id is This simply means that if parent table’s student_id is

removed, all the child table’s student_id s are removed!removed, all the child table’s student_id s are removed!

Page 25: My slide  understanding keys

25Rushdi Shams, Dept of CSE, KUET

Propagation Action/ Propagation Action/ Referential ActionReferential Action

NO ACTIONNO ACTION: : This corresponds to the restricted option. It rejects the delete action on the table containing the primary key reference by the foreign key. This is the default setting in the absence of a specified ON DELETE constraint

CASCADE: This corresponds to the cascades option. It causes automatic deletion/ modification of associated rows in the foreign key table following deletion/ modification of the row or rows in the primary key table

Page 26: My slide  understanding keys

26Rushdi Shams, Dept of CSE, KUET

Propagation Action/ Propagation Action/ Referential Action Referential Action

(continued)(continued) SET DEFAULT:SET DEFAULT: This causes deletion of

the row in the primary key table followed by setting the foreign key values to the default specified on the foreign key

SET NULL:SET NULL: This corresponds to the nullifies option. Following deletion of rows in the primary key table it sets the associated foreign key values to NULL.

Page 27: My slide  understanding keys

27Rushdi Shams, Dept of CSE, KUET

Super keySuper key

A super key is a set of columns A super key is a set of columns within a table whose values can be within a table whose values can be used to identify a row uniquely.used to identify a row uniquely.

In real world scenario, it is In real world scenario, it is sometimes difficult to identify sometimes difficult to identify primary keys. Then super key is used primary keys. Then super key is used to unique identification of rows.to unique identification of rows.

Page 28: My slide  understanding keys

28Rushdi Shams, Dept of CSE, KUET

Super key (continued)Super key (continued)

In this example, what if the roll is In this example, what if the roll is just like 1-60. you have this table for just like 1-60. you have this table for all 4 years’ students! How will you all 4 years’ students! How will you identify them by the roll?identify them by the roll?

So, you can have combinations like So, you can have combinations like {roll, name}, {roll, name, dept}, {roll, name}, {roll, name, dept}, {roll, name, year, semester}, etc. to {roll, name, year, semester}, etc. to uniquely identify them! These are uniquely identify them! These are superkeys.superkeys.RollRoll NameName DeptDept YearYear SemesSemes

terter

Page 29: My slide  understanding keys

29Rushdi Shams, Dept of CSE, KUET

Candidate keyCandidate key

It is the It is the minimal setminimal set of super key. of super key. Say, from the previous example, you only Say, from the previous example, you only

can have super keys as followings can have super keys as followings (though you may have other (though you may have other combinations, but just shorten that this combinations, but just shorten that this time)-time)-

{roll, name}, {roll, name, dept}, {roll, {roll, name}, {roll, name, dept}, {roll, name, year, semester}name, year, semester}

In this case, the candidate key is {roll, In this case, the candidate key is {roll, name}!name}!

Page 30: My slide  understanding keys

30Rushdi Shams, Dept of CSE, KUET

Compound keyCompound key

If you make a key with 2 or more If you make a key with 2 or more columns, then that is called a columns, then that is called a compound keycompound key

Also called Also called Composite keyComposite key or or concatenated keyconcatenated key

Any one, none, or all, of the multiple Any one, none, or all, of the multiple attributes within the compound key attributes within the compound key can be foreign keyscan be foreign keys

Page 31: My slide  understanding keys

31Rushdi Shams, Dept of CSE, KUET

Alternate keyAlternate key

An alternate key is any of the candidate An alternate key is any of the candidate keys that was not chosen as primary keykeys that was not chosen as primary key

Say, you have 5 columns A, B, C, D, E Say, you have 5 columns A, B, C, D, E and 3 candidate keys {A, B}, {A, C}, and 3 candidate keys {A, B}, {A, C}, {A, D}, {B, C} and {D, E}. You have {A, D}, {B, C} and {D, E}. You have chosen {A, B} as the primary key. So, chosen {A, B} as the primary key. So, {A, C}, {A, D}, {B, C} and {D, E} will {A, C}, {A, D}, {B, C} and {D, E} will be alternate keysbe alternate keys

Also known as Also known as Secondary keySecondary key Strictly used in data retrieval purposeStrictly used in data retrieval purpose

Page 32: My slide  understanding keys

32Rushdi Shams, Dept of CSE, KUET

Relational Database KeysRelational Database Keys

Page 33: My slide  understanding keys

33Rushdi Shams, Dept of CSE, KUET

ReferencesReferences

Database Systems: Database Systems: Design, Implementation, and Design, Implementation, and Management, Sixth Edition, Rob and Management, Sixth Edition, Rob and CoronelCoronel

www.wikipedia.orgwww.wikipedia.org Beginning Database Design by Gavin Beginning Database Design by Gavin

Powell, Wrox Publications, 2005Powell, Wrox Publications, 2005 Database Systems by Paul Beynon-Database Systems by Paul Beynon-

Devies, Palgrave Macmillan, 2004Devies, Palgrave Macmillan, 2004