• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
My slide  understanding keys
 

My slide understanding keys

on

  • 120 views

 

Statistics

Views

Total Views
120
Views on SlideShare
120
Embed Views
0

Actions

Likes
0
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    My slide  understanding keys My slide understanding keys Presentation Transcript

    • Rushdi Shams, Dept of CSE, KUET 1 Database SystemsDatabase Systems Understanding KeysUnderstanding Keys Entity IntegrityEntity Integrity Referential IntegrityReferential Integrity Propagation ConstraintPropagation Constraint Version 1.0Version 1.0
    • 2Rushdi Shams, Dept of CSE, KUET What is keyWhat is key  A key is a CONSTRAINT in field/ column in aA key is a CONSTRAINT in field/ column in a tabletable  Generally, keys are essential in database systemsGenerally, keys are essential in database systems to invoke some integrityto invoke some integrity  By a key, you can tag a field in a table so that theBy a key, you can tag a field in a table so that the field can be used to relate tablesfield can be used to relate tables
    • 3Rushdi Shams, Dept of CSE, KUET Types of keyTypes of key There are 3 types of keys in general. They are-There are 3 types of keys in general. They are-  Primary keyPrimary key  Unique keyUnique key  Foreign keyForeign key
    • 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 calledAlso called Surrogate keySurrogate key  Say, you have a table of names of the students ofSay, you have a table of names of the students of your class and their ages. How can you differyour class and their ages. How can you differ between them? Because many of you have thebetween them? Because many of you have the same name! don’t you? So, what happens if yousame name! don’t you? So, what happens if you try to find out a name “Rezwana” and you findtry to find out a name “Rezwana” and you find there are 5 Rezwanas in the class?there are 5 Rezwanas in the class?  That invokes the idea of primary keyThat invokes the idea of primary key
    • 5Rushdi Shams, Dept of CSE, KUET Primary key (continued)Primary key (continued)  This problem won’t be serious if you had anyThis problem won’t be serious if you had any identifying criteria to differ between 5 Rezwanas.identifying criteria to differ between 5 Rezwanas.  You still can identify 5 Rezwanas by adding anYou still can identify 5 Rezwanas by adding an extra column in that table. Can you say whatextra column in that table. Can you say what field that might be?field that might be?  That is your roll number! So, 5 Rezwanas can beThat is your roll number! So, 5 Rezwanas can be easily identifed by their roll numbers.easily identifed by their roll numbers.  So, roll number is the primary key here.So, roll number is the primary key here.
    • 6Rushdi Shams, Dept of CSE, KUET Primary key (continued)Primary key (continued)
    • 7Rushdi Shams, Dept of CSE, KUET Primary key (continued)Primary key (continued)
    • 8Rushdi Shams, Dept of CSE, KUET Unique KeyUnique Key  Unique keys are applied on the column of aUnique keys are applied on the column of a tabletable  Any field that is UNIQUE key, you will have toAny field that is UNIQUE key, you will have to remember that no 2 entries in that column inremember that no 2 entries in that column in that table will be the samethat table will be the same  So, are you thinking “why there are primary keysSo, are you thinking “why there are primary keys and unique keys? They are same, no?”and unique keys? They are same, no?”
    • 9Rushdi Shams, Dept of CSE, KUET Unique key (continued)Unique key (continued)  No, they are not the same. In real life, when you areNo, they are not the same. In real life, when you are creating tables, you sometimes will not want to let thecreating tables, you sometimes will not want to let the table have the same entry twice (even though that is nottable have the same entry twice (even though that is not the primary key)the primary key)  An example can be, if you have a course table withAn example can be, if you have a course table with primary key COURSE ID and another field COURSEprimary key COURSE ID and another field COURSE NAME- take a look! You will not allow the data entryNAME- take a look! You will not allow the data entry operator to entry the same subject name twice, do you?operator to entry the same subject name twice, do you?  So, making COURSE NAME as UNIQUE will giveSo, making COURSE NAME as UNIQUE will give you that opportunityyou that opportunity
    • 10Rushdi Shams, Dept of CSE, KUET Foreign KeyForeign Key  Foreign keys are copies of primary key in theForeign keys are copies of primary key in the child tablechild table  When two tables are in relation, then child tableWhen two tables are in relation, then child table is that table which depends on the other.is that table which depends on the other.
    • 11Rushdi Shams, Dept of CSE, KUET Foreign key (continued)Foreign key (continued)
    • 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 NULLNOT NULL,, student_namestudent_name varchar(20),varchar(20), PRIMARY KEY(student_id)PRIMARY KEY(student_id) ););
    • 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 NULL,NOT NULL, course_namecourse_name varchar(20)varchar(20) UNIQUEUNIQUE,, PRIMARY KEY(course_id)PRIMARY KEY(course_id) ););
    • 14Rushdi Shams, Dept of CSE, KUET Creating Foreign keysCreating Foreign keys CREATE TABLE address(CREATE TABLE address( student_idstudent_idnumber(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 student );); BackBack
    • 15Rushdi Shams, Dept of CSE, KUET Case StudyCase Study
    • 16Rushdi Shams, Dept of CSE, KUET Case study (continued)Case study (continued)
    • 17Rushdi Shams, Dept of CSE, KUET Case study (continued)Case study (continued) CREATE TABLE Band(CREATE TABLE Band( band_idband_id varchar(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_id varchar(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 ););
    • 18Rushdi Shams, Dept of CSE, KUET Important Fact!Important Fact!  A child table may not have any primary key ofA child table may not have any primary key of its own. It can make its foreign keys as itsits own. It can make its foreign keys as its primary keys as well!primary keys as well!
    • 19Rushdi Shams, Dept of CSE, KUET Entity IntegrityEntity Integrity  Entity integrity concerns with primary keyEntity integrity concerns with primary key  It states that “Every table must have a primaryIt states that “Every table must have a primary key” and “the column(s) that make up thekey” and “the column(s) that make up the primary key must be unique to identify a rowprimary key must be unique to identify a row and NOT NULL”and NOT NULL”
    • 20Rushdi Shams, Dept of CSE, KUET Referential Integrity & PropagationReferential Integrity & Propagation ConstraintConstraint  It ensures the integrity of referential relationshipsIt ensures the integrity of referential relationships between tablesbetween tables  In a relation between 2 tables, one table has primary keyIn a relation between 2 tables, one table has primary key and the other a foreign keyand the other a foreign key  Referential integrity ensures the integrity of the valuesReferential integrity ensures the integrity of the values of primary and foreign key of parent and child tableof primary and foreign key of parent and child table  Most database engines call these criteriaMost database engines call these criteria CONSTRAINTS. Primary and foreign keys are alsoCONSTRAINTS. Primary and foreign keys are also constraintsconstraints
    • 21Rushdi Shams, Dept of CSE, KUET Referential Integrity & PropagationReferential Integrity & Propagation Constraint (continued)Constraint (continued)  A primary key table is assumed as parent tableA primary key table is assumed as parent table and a foreign key table is assumed as child tableand a foreign key table is assumed as child table  If a value in foreign key column in child table isIf a value in foreign key column in child table is entered, it MUST be present in primary keyentered, it MUST be present in primary key column in parent tablecolumn in parent table  Foreign key can have NULL values, but primaryForeign key can have NULL values, but primary key can NEVER have NULL valueskey can NEVER have NULL values
    • 22Rushdi Shams, Dept of CSE, KUET Referential Integrity & PropagationReferential Integrity & Propagation Constraint (continued)Constraint (continued)  If the primary key value is changed it should beIf the primary key value is changed it should be CASCADED to force to change foreign key values asCASCADED to force to change foreign key values as wellwell  Changing a value in foreign key must check firstly theChanging a value in foreign key must check firstly the existence of its primary key. If the foreign key isexistence of its primary key. If the foreign key is changed to NULL, no primary key is required. If it ischanged to NULL, no primary key is required. If it is changed to NON NULL, the check for primary key ischanged to NON NULL, the check for primary key is requiredrequired  If deletion of primary key is required, it is required toIf deletion of primary key is required, it is required to delete foreign key along with it (CASCADE) or foreigndelete foreign key along with it (CASCADE) or foreign key firstly then primary keykey firstly then primary key
    • 23Rushdi Shams, Dept of CSE, KUET Referencial Integrity (continued)Referencial Integrity (continued)  Creating referential integrity is easy! You haveCreating referential integrity is easy! You have already done that. Look at the REFERENCESalready done that. Look at the REFERENCES keyword here. That is ensuring your referentialkeyword here. That is ensuring your referential integrity.integrity.  Creating Foreign keysCreating Foreign keys
    • 24Rushdi Shams, Dept of CSE, KUET Propagation Constraint (continued)Propagation Constraint (continued)  A propagation constraint is a rule about what to do if a row of a table that isA propagation constraint is a rule about what to do if a row of a table that is referenced is modified or even deleted.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 student ON DELETE CASCADEON DELETE CASCADE );); This simply means that if parent table’s student_id is removed, all the childThis simply means that if parent table’s student_id is removed, all the child table’s student_id s are removed!table’s student_id s are removed!
    • 25Rushdi Shams, Dept of CSE, KUET Propagation Action/ ReferentialPropagation Action/ Referential ActionAction  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
    • 26Rushdi Shams, Dept of CSE, KUET Propagation Action/ ReferentialPropagation Action/ Referential Action (continued)Action (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.
    • 27Rushdi Shams, Dept of CSE, KUET Super keySuper key  A super key is a set of columns within a tableA super key is a set of columns within a table whose values can be used to identify a rowwhose values can be used to identify a row uniquely.uniquely.  In real world scenario, it is sometimes difficultIn real world scenario, it is sometimes difficult to identify primary keys. Then super key is usedto identify primary keys. Then super key is used to unique identification of rows.to unique identification of rows.
    • 28Rushdi Shams, Dept of CSE, KUET Super key (continued)Super key (continued)  In this example, what if the roll is just like 1-60.In this example, what if the roll is just like 1-60. you have this table for all 4 years’ students! Howyou have this table for all 4 years’ students! How will you identify them by the roll?will you identify them by the roll?  So, you can have combinations like {roll, name},So, you can have combinations like {roll, name}, {roll, name, dept}, {roll, name, year, semester},{roll, name, dept}, {roll, name, year, semester}, etc. to uniquely identify them! These areetc. to uniquely identify them! These are superkeys.superkeys. RollRoll NameName DeptDept YearYear SemesterSemester
    • 29Rushdi Shams, Dept of CSE, KUET Candidate keyCandidate key  It is theIt is the minimal setminimal set of super key.of super key.  Say, from the previous example, you only canSay, from the previous example, you only can have super keys as followings (though you mayhave super keys as followings (though you may have other combinations, but just shorten thathave other combinations, but just shorten that this time)-this time)- {roll, name}, {roll, name, dept}, {roll, name,{roll, name}, {roll, name, dept}, {roll, name, year, semester}year, semester} In this case, the candidate key is {roll, name}!In this case, the candidate key is {roll, name}!
    • 30Rushdi Shams, Dept of CSE, KUET Compound keyCompound key  If you make a key with 2 or more columns, thenIf you make a key with 2 or more columns, then that is called a compound keythat is called a compound key  Also calledAlso called Composite keyComposite key oror concatenated keyconcatenated key  Any one, none, or all, of the multiple attributesAny one, none, or all, of the multiple attributes within the compound key can be foreign keyswithin the compound key can be foreign keys
    • 31Rushdi Shams, Dept of CSE, KUET Alternate keyAlternate key  An alternate key is any of the candidate keys thatAn alternate key is any of the candidate keys that was not chosen as primary keywas not chosen as primary key  Say, you have 5 columns A, B, C, D, E and 3Say, you have 5 columns A, B, C, D, E and 3 candidate keys {A, B}, {A, C}, {A, D}, {B, C}candidate keys {A, B}, {A, C}, {A, D}, {B, C} and {D, E}. You have chosen {A, B} as theand {D, E}. You have chosen {A, B} as the primary key. So, {A, C}, {A, D}, {B, C} andprimary key. So, {A, C}, {A, D}, {B, C} and {D, E} will be alternate keys{D, E} will be alternate keys  Also known asAlso known as Secondary keySecondary key  Strictly used in data retrieval purposeStrictly used in data retrieval purpose
    • 32Rushdi Shams, Dept of CSE, KUET Relational Database KeysRelational Database Keys
    • 33Rushdi Shams, Dept of CSE, KUET ReferencesReferences  Database Systems:Database Systems: Design, Implementation, and Management,Design, Implementation, and Management, Sixth Edition, Rob and CoronelSixth Edition, Rob and Coronel  www.wikipedia.orgwww.wikipedia.org  Beginning Database Design by Gavin Powell,Beginning Database Design by Gavin Powell, Wrox Publications, 2005Wrox Publications, 2005  Database Systems by Paul Beynon-Devies,Database Systems by Paul Beynon-Devies, Palgrave Macmillan, 2004Palgrave Macmillan, 2004