Natural vs. Generated Keys. Definitions Natural key—a key that occurs in the data, that uniquely identifies rows. AKA candidate key. Generated key—a key.

Slides:



Advertisements
Similar presentations
Normal forms - 1NF, 2NF and 3NF
Advertisements

Normalisation.
FEN Introduction to the database field: Quality checking table design: Design Guidelines Normalisation Seminar: Introduction to relational.
Relational Terminology. Normalization A method where data items are grouped together to better accommodate business changes Provides a method for representing.
The Relational Model and Relational Algebra Nothing is so practical as a good theory Kurt Lewin, 1945.
Shantanu Narang.  Background  Why and What of Normalization  Quick Overview of Lower Normal Forms  Higher Order Normal Forms.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.
Chapter 3 The Relational Model Transparencies © Pearson Education Limited 1995, 2005.
Relations The Relational Data Model John Sieg, UMass Lowell.
Design Guidelines Normalisation Table Design. Informal Design Guidelines Table Semantics A table should hold information about one and only one entity/concept.
Chapter 3. 2 Chapter 3 - Objectives Terminology of relational model. Terminology of relational model. How tables are used to represent data. How tables.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
Database Design & ER Diagrams
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Chapter 14 & 15 Conceptual & Logical Database Design Methodology
Database Systems Lecture 5 Natasha Alechina
Database Design.  Define a table for each entity  Give the table the same name as the entity  Make the primary key the same as the identifier of the.
N. J. Taylor Database Management Systems (DBMS) 1.
Database Principles Relational Database Design I.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Chapter 4 The Relational Model Pearson Education © 2014.
© Pearson Education Limited, Chapter 2 The Relational Model Transparencies.
Data at the Core of the Enterprise. Objectives  Define of database systems.  Introduce data modeling and SQL.  Discuss emerging requirements of database.
INTEGRITY. Integrity constraint Integrity constraints are specified on a database schema and are expected to hold on every valid database state of the.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
MIS 301 Information Systems in Organizations Dave Salisbury ( )
Fundamentals of Relational Database Yong Choi School of Business CSUB, Bakersfield.
NormalizationNormalization Chapter 4. Purpose of Normalization Normalization  A technique for producing a set of relations with desirable properties,
RDBMSSection Relational DBMS DATABASE DEVELOPMENT And Franchise Colleges By MANSHA NAWAZ.
FEN Quality checking table design: Design Guidelines Normalisation Table Design Is this OK?
Introduction to Databases Trisha Cummings. What is a database? A database is a tool for collecting and organizing information. Databases can store information.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
DataBase Management System What is DBMS Purpose of DBMS Data Abstraction Data Definition Language Data Manipulation Language Data Models Data Keys Relationships.
Component 4: Introduction to Information and Computer Science Unit 6a Databases and SQL.
1 © Prentice Hall, 2002 Chapter 5: Logical Database Design and the Relational Model Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B.
Lecture 8: Database Concepts May 4, Outline From last lecture: creating views Normalization.
In this session, you will learn to: Describe data redundancy Describe the first, second, and third normal forms Describe the Boyce-Codd Normal Form Appreciate.
Database Management Supplement 1. 2 I. The Hierarchy of Data Database File (Entity, Table) Record (info for a specific entity, Row) Field (Attribute,
INTRODUCTION lecture1 1. Data base concept Data is a meaningless static value. What does 3421 means? Information is the data you process in a manner that.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
The Relational Model. 2 Relational Model Terminology u A relation is a table with columns and rows. –Only applies to logical structure of the database,
Copyright © 2009 Pearson Education, Inc. Publishing as Prentice Hall Chapter 9 Designing Databases 9.1.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
MIS 3053 Database Design And Applications The University Of Tulsa Professor: Akhilesh Bajaj Normal Forms Lecture 1 © Akhilesh Bajaj, 2000, 2002, 2003.
CSCI 6442 Entity-Relation Data Modeling
The Relational Model © Pearson Education Limited 1995, 2005 Bayu Adhi Tama, M.T.I.
The relational model1 The relational model Mathematical basis for relational databases.
Mapping ER to Relational Model Each strong entity set becomes a table. Each weak entity set also becomes a table by adding primary key of owner entity.
Chapter 3 The Relational Model. Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between mathematical.
FEN Introduction to the database field: The development process Seminar: Introduction to relational databases Development process: Analyse.
Normalization ACSC 425 Database Management Systems.
Databases Introduction - concepts. Concepts of Relational Databases.
Chapter 4 The Relational Model Pearson Education © 2009.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Agenda TMA02 M876 Block 4. 2 Model of database development data requirements conceptual data model logical schema schema and database establishing requirements.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
Normalization.
Relational Database Design
Database SQL.
Presentation transcript:

Natural vs. Generated Keys

Definitions Natural key—a key that occurs in the data, that uniquely identifies rows. AKA candidate key. Generated key—a key that is generated at the time data is inserted into the database, that did not occur naturally 2

Postulate: In a normalized data model, every row has a natural key Proof: 3NF requires that the value of every attribute be ffd by the primary key. By construction, primary keys uniquely identify instances and are unique. Therefore, each row has a different value for the primary key and is hence unique. Result: There is no reason to add a primary key to normalized data in order to make rows unique. They already are. 3 Question: does 3NF require that there be a single- attribute primary key?

Generated Keys In some organizations, programmers have the idea that a generated key should be included in every relation, a misunderstanding of the relational approach. Often they will say “it’s to make rows unique” or “to provide a unique identifier.” This is terribly wrong! That’s a fine idea for an Excel spreadsheet but it shows a misunderstanding of what a relation is and what a tuple is. Tuples are naturally unique because each one corresponds to a distinct instance of an entity type in the real world. You don’t have to add anything to make them unique. If your database design is correct, you have unique rows even if you haven’t added a key to each row. 4

Making the Point Again, Again Each row corresponds to an occurrence of a entity instance Each entity instance is unique; that’s why it is tracked as an instance Therefore, every row is different from every other row without adding anything to it 5 Question: what if entity instances are not unique? How do we track them in a database?

Claim That’s Made for Generated Keys Some think that generated keys allow more flexibility For example, in our EMP table, can we allow a person’s name to change? If we use a generated key, then the name can change and all other associated data will still be associated with the correct person What do you think of this statement? Can you use telephone number as the primary key? Why or why not? Can primary keys ever change in value? And what if the primary key is a foreign key in another table? 6

SQL and Primary Keys SQL allows primary keys to change; they are just a value that changes What about foreign keys? Can they change? In the CREATE TABLE statement, you state ON UPDATE or ON DELETE CASCADE, RESTRICT, NO ACTION, SET NULL or SET DEFAULT. For ON DELETE, DELETE can also be an action 7

Benefits of Natural Keys: Assures proper attention to the data model, because unique values must be identified Tables have fewer columns Avoid storage of meaningless data The key value itself may have value to an application, hence will avoid retrieval of the row itself 8

When to Use Generated Keys Sometimes you have a compound key that is very long Lots of queries will be very complicated and tables with foreign keys will copy most of another table As a guideline, the only time to use a generated key is when the natural key has at least three columns A special case of this rule is the case where the whole row would have to be a composite key without a generated key Example: internal person 9

Another Case for Generated Keys Suppose we don’t have any kind of single or composite identifier that is always present Database systems generally don’t allow for part of a designated composite key to be null Even though it’s legitimate, because of database system limitations, you may have to use generated keys if you don’t have a common identifier without any null parts, ever 10

The Bottom Line Don’t use generated keys. Ever. Unless you absolutely have to. 11