Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Relational Database Introducing PRIMARY KEY

Similar presentations


Presentation on theme: "Introduction to Relational Database Introducing PRIMARY KEY"— Presentation transcript:

1 Introduction to Relational Database Introducing PRIMARY KEY
Thaddeus Konar LCC CIT Instructor T. Konar

2 Introduction to Relational Database
Summary of what we already know: Database allows storage of related data in the tables as a collection of relations. Each relationship is represented in a table. Database stores data in multiple two-dimensional (rows and columns) tables. Each table defines a relationship. Access uses some non-standard Database nomenclature: Each column represents a field and the real technical term for it is ATTRIBUTE. Each row represents a record and the real technical term for it is ENTITY. There is also a relationship between tables (single value in one table can point to a row of data in another table). T. Konar

3 Introduction to Relational Database
Here is my library card: Classification Name Primary Key College Library Thaddeus Konar Instructor CIT DOB: 07/04/1776 ISSUED: 09/01/2003 1295 Department My library card has many fields to hold the relevant information about me. Day of Birth Issue Date ID Number T. Konar

4 Introduction to Relational Database
My library card contains Related Data (relates to me – a library patron) Primary Key College Library Thaddeus Konar Instructor CIT DOB: 07/04/1776 ISSUED: 09/01/2003 1295 Every field contains the information describing me. There is no guarantee that there is no another Thaddeus Konar, or another 200+ old person, but there should only be one library patron with ID # 1295 T. Konar

5 Introduction to Relational Database
Primary Key College Library John Vanko Student DOB: 11/12/1976 ISSUED: 09/01/2003 7645 Primary Key College Library Stephen Packo Instructor ENG DOB: 05/14/1973 ISSUED: 02/15/2003 2365 Primary Key College Library Stephen Packo Instructor ENG DOB: 05/14/1973 ISSUED: 02/15/2003 2365 Primary Key College Library Debbie Smith Student DOB: 09/23/1982 ISSUED: 09/01/2003 2345 Primary Key College Library Stephen Packo Instructor ENG DOB: 05/14/1973 ISSUED: 02/15/2003 2365 Primary Key College Library Stephen Packo Instructor ENG DOB: 05/14/1973 ISSUED: 02/15/2003 2365 Primary Key College Library Thaddeus Konar Instructor CIT DOB: 07/04/1776 ISSUED: 09/01/2003 1295 Primary Key College Library Stephen Packo Instructor ENG DOB: 05/14/1973 ISSUED: 02/15/2003 2365 Primary Key College Library Mary Zaba Staff Adm DOB: 02/03/1973 ISSUED: 02/01/2003 1256 Primary Key College Library Karen Pstrag Staff Adm DOB: 02/21/1964 ISSUED: 02/01/2003 2945 Other folks have their cards with information related to them. T. Konar

6 Related data could be presented in a table as a record.
Introduction to Relational Database Related data could be presented in a table as a record. Each row represents a single RECORD (entity) ID Number Name Classification Dept. Day of Birth Issue Date ... We have our ID cards, but the library would have the same information in one place. Hopefully use database and store this information in a table, one row per patron. 1295 Thaddeus Konar 1295 CIT T. Konar

7 Introduction to Relational Database
Columns are table’s FIELDS (attributes) and they define the table format. ID Number Name Classification Dept. Day of Birth Issue Date 1295 Thaddeus Konar Instructor CIT ... As you noticed, all cards have the same format and the same fields. ID number, Name, Classification, Dept, etc T. Konar

8 Introduction to Relational Database
Every table has one FIELD (attribute/column) that uniquely identifies each RECORD (entity/row). ID Number Name Classification Dept. Day of Birth Issue Date 1295 Thaddeus Konar Instructor CIT ... The attributes as my name, birth date, or job...do not identify me uniquely. The only attribute which does it is the ID Number T. Konar

9 Introduction to Relational Database
The FIELD (attribute/column) that is unique for each and every RECORD (entity/row) in the table is designated as a PRIMARY KEY. ID Number Name Classification Dept. Day of Birth Issue Date 1295 Thaddeus Konar Instructor CIT ... The same is for other library patrons. Only ID Number guarantees the unique identification of a single record. T. Konar

10 Introduction to Relational Database
PRIMARY KEY identifies a single and unique RECORD (row) of the table. For this table there could be a duplicate Name, Classification, DOB, etc. but not ID Number. ID Number Name Classification Dept. Day of Birth Issue Date 1295 Thaddeus Konar Instructor CIT ... Pretty much repeats what was said before. T. Konar

11 Introduction to Relational Database
ID Number Name Classification Dept. Day of Birth Issue Date 1295 Thaddeus Konar Instructor CIT 2345 Debie Smith Student 7645 John Vanko 2945 Karen Pstrag Staff ADM 1256 Mary Zaba 2365 Stephen Packo ENG ID Number Name Classification Dept. Day of Birth Issue Date 1295 Thaddeus Konar Instructor CIT 2345 Debie Smith Student 7645 John Vanko 2945 Karen Pstrag Staff ADM 1256 Mary Zaba 2365 Stephen Packo ENG So we have our library patrons table! From the database point of view, this table has problems. The classification field values are repeated. This is not an efficient use of database. Also the ID number 1295 identifies me, my name, birthday, etc. But the department is really not part of me. Data is duplicated Data not ‘really’ pertaining to me (I am part of CIT, but CIT is not part of me) T. Konar

12 Introduction to Relational Database
Patrons ID_Number Name CID DID Day of Birth Issue Date 1295 Thaddeus Konar 1 2345 Debie Smith 2 7645 John Vanko 2945 Karen Pstrag 3 1256 Mary Zaba 2365 Stephen Packo Classification Foreign Key Points to the Primary Key in Classification Table Foreign Key Points to the Primary Key in Department Table Department ID_num Description 1 Instructor 2 Student 3 Staff 4 The proper approach would be to split the data into three tables. We should add the Classification and the Department table. We designate the field CID in Patrons table as a pointer field to the Classification table, and DID in Patrons as a pointer field to the Department table. This pointer is called ‘Foreign Key’. ID_num Description 1 CIT 2 ENG 3 ADM 4 T. Konar

13 Introduction to Relational Database
Patrons ID Number Name CID DID Day of Birth Issue Date 1295 Thaddeus Konar 1 2345 Debie Smith 2 7645 John Vanko 2945 Karen Pstrag 3 1256 Mary Zaba 2365 Stephen Packo Classification Department ID_num Description 1 Instructor 2 Student 3 Staff 4 ID_num Description 1 CIT 2 ENG 3 ADM 4 Each table must have a primary key, and our new tables will also have them. Primary Key for Library Patrons Table Primary Key for Classification Table Primary Key for Department Table T. Konar

14 Introduction to Relational Database
Patrons CREATE TABLE classification ( id_num INT PRIMARY KEY, description CHAR(12), ); CREATE TABLE department ( id_num INT PRIMARY KEY, description CHAR(10), ); ID Number Name CID DID Day of Birth Issue Date 1295 Thaddeus Konar 1 2345 Debie Smith 2 7645 John Vanko 2945 Karen Pstrag 3 1256 Mary Zaba 2365 Stephen Packo Classification Department ID_num Description 1 Instructor 2 Student 3 Staff 4 ID_num Description 1 Instructor 2 Student 3 Staff 4 CREATE TABLE patrons ( id_number INT PRIMARY KEY, name CHAR(50), FOREIGN KEY cid REFERENCES classification (id_num), FOREIGN KEY did REFERENCES department (id_num), dob INT, issue_d INT ); You might ask, “How do you do that? How do you make one field point to another row in another table?” This is done by a programmer (database designer) and the code to create table like Patrons would look like this{} You see that ID_Number field is declared to be Primary Key, and CID and DID are Foreign Keys pointing at other tables.{} Also you will notice that two new tables are also declared with primary keys. ID_num Description 1 CIT 2 ENG 3 ADM 4 ID_num Description 1 CIT 2 ENG 3 ADM 4 T. Konar

15 Introduction to Relational Database
Summary: PRIMARY KEY is a unique field that identifies a single RECORD (row/entity) of the table. PRIMARY KEY is a ‘target’ (lookup identifier) for a Foreign Key in other tables. PRIMARY KEY of the relation is a unique identifier for that relation Please remember that each record describes a single instance of a relation, and the table describes the structure of all relations in that table. Without Primary Keys we would not be able to identify a single record and it would not be possible to have the relations between different tables. PRIMARY KEY can be one or more attributes, must be unique, must have a ‘fixed’ value, and can be created automatically. T. Konar

16 Introduction to Relational Database
What is wrong with this table? Cust. ID Name Phone 42 Bubba Smith 45 Debie Smith John Smith 48 Karen Smith 56 Mary Smith 65 John and Karen have same phone number. That is OK. There are two Mary Smith. That is OK There are two people sharing the same ID. That is NOT OK! T. Konar

17 Introduction to Relational Database
What is wrong with this table? CID Classification 1 Instructor 2 Student 3 Staff Student and Instructor classification share the same ID. That is NOT OK! T. Konar

18 Introduction to Relational Database
PRIMARY KEY Explained The End T. Konar


Download ppt "Introduction to Relational Database Introducing PRIMARY KEY"

Similar presentations


Ads by Google