Understand Primary, Foreign, and Composite Keys 98-364 Database Administration Fundamentals LESSON 4.2.

Slides:



Advertisements
Similar presentations
Chapter 10: Designing Databases
Advertisements

The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
Relational Databases Chapter 4.
THE RELATIONAL DATABASE MODEL & THE DATABASE DEVELOPMENT PROCESS Fact of the Week: According to a Gartner study in ‘06, Microsoft SQL server had the highest.
Understand Normalization
Accounting Databases Chapter 2 The Crossroads of Accounting & IT
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
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.
The Relational Model Codd (1970): based on set theory Relational model: represents the database as a collection of relations (a table of values --> file)
July 14, 2015ICS 424: recap1 Relational Database Design: Recap of ICS 324.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
Michael F. Price College of Business Chapter 6: Logical database design and the relational model.
Oracle Data Definition Language (DDL)
1 Relational model concepts Key constraints Referential integrity constraint Steen Jensen, autumn 2013.
© Pearson Education Limited, Chapter 2 The Relational Model Transparencies.
Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems.
RAJIKA TANDON DATABASES CSE 781 – Database Management Systems Instructor: Dr. A. Goel.
A Guide to SQL, Eighth Edition Chapter Two Database Design Fundamentals.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Databases in Visual Studio. Database in VisualStudio An MS SQL database are built in Visual studio The Name can be something like ”(localdb)\Projects”
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
1 A Guide to MySQL 2 Database Design Fundamentals.
Normalization (Codd, 1972) Practical Information For Real World Database Design.
Instructor: Dema Alorini Database Fundamentals IS 422 Section: 7|1.
Relational Database. Database Management System (DBMS)
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
Databases Shortfalls of file management systems Structure of a database Database administration Database Management system Hierarchical Databases Network.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Chapter 10 Designing the Files and Databases. SAD/CHAPTER 102 Learning Objectives Discuss the conversion from a logical data model to a physical database.
Relational Theory and Design
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Database Terms Hernandez, Chapter 3. Data/Information The values you store in the database are data. Pieces of Data in and of themselves is not particularly.
Database Management Supplement 1. 2 I. The Hierarchy of Data Database File (Entity, Table) Record (info for a specific entity, Row) Field (Attribute,
The relational model A data model (in general) : Integrated collection of concepts for describing data (data requirements). Relational model was introduced.
Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions.
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,
Understand Tables and How to Create Them Database Administration Fundamentals LESSON 2.2.
Chapter 13.3: Databases Invitation to Computer Science, Java Version, Second Edition.
* Database is a group of related objects * Objects can be Tables, Forms, Queries or Reports * All data reside in Tables * A Row in a Table is a record.
Exploring Microsoft Access Chapter 5 One-to-Many Relationships: Subforms and Multiple Table Queries.
Chapter 3: Relational Databases
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
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.
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
Decision Analysis Fall Term 2015 Marymount University School of Business Administration Professor Suydam Week 10 Access Basics – Tutorial B; Introduction.
Getting started with Accurately Storing Data
CompSci 280 S Introduction to Software Development
Fundamentals of DBMS Notes-1.
Fundamental of Database Systems
Understanding Data Storage
Database Constraints Ashima Wadhwa.
The Relational Model and Database Normalization
Relational Databases.
Chapter 4 Relational Databases
Lecture # 13 (After 1st Exam)
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Data Modelling Introduction
Normalization Referential Integrity
PHPMyAdmin.
SQL DATA CONSTRAINTS.
CMPE/SE 131 Software Engineering March 9 Class Meeting
A Very Brief Introduction to Relational Databases
Chapter 17 Designing Databases
INSTRUCTOR: MRS T.G. ZHOU
SQL NOT NULL Constraint
Presentation transcript:

Understand Primary, Foreign, and Composite Keys Database Administration Fundamentals LESSON 4.2

Database Administration Fundamentals LESSON 4.2 Lesson Overview In this lesson, you will learn:  How to choose appropriate keys  What a primary key is  What a foreign key is  Considerations for maintaining data integrity

Database Administration Fundamentals LESSON 4.2 Choosing Appropriate Keys  Keys must be unique.  Not relatively unique, but truly unique for each record in the database.  If you are named Bob, this may be unique in your home, but it’s not unique in the city of New York.  Keys only need to have value for the database.  Student_ID has the ability to be unique among the 500,000 current students in the New York school system.  Creating a key just for one student makes it unique and valuable only to the database. If the key gets compromised, it has no value outside the database.  On the other hand, if your Social Security number becomes public knowledge, it can cause problems for you because it has value outside the database.

Database Administration Fundamentals LESSON 4.2 Choosing Appropriate Keys (continued)  To be most useful, keys need to be of consistent type and style.  A key needs to be made up of only one data type.  This is why most keys are made of the same type of data and have the same length.  When keys are consistent, this helps keep the database in order and helps with retrieving records.

Database Administration Fundamentals LESSON 4.2 Primary Keys General Rules:  — The primary key uniquely identifies each record in a database table.  — Primary keys must contain unique values.  — A primary key column cannot contain NULL values.  — Each table should have a primary key, and each table can have only one primary key. Working Definition:  A primary key uniquely defines a relationship within a database. For an attribute to be a good primary key, it must not repeat.  Sometimes we try to use a person’s name as the primary key. In the classroom, a teacher will call on a student by name, but it may not uniquely define that student. The class could have two Mary Smiths, for example.  Databases solve this problem by creating unique primary keys, such as the “Student_ID” of a specific data type for data integrity. There will never be two students with the same ID.

Database Administration Fundamentals LESSON 4.2 Primary Keys (continued) Example of a primary key within a table in normalized form Table Teacher_classes contains Teacher_ID and Class_ID: Teacher_IDClass_ID 78v v22Lit200  In the above table, we have a unique primary key for the table Teacher_classes. Each teacher has a unique ID (primary key in bold) tied to many class IDs.  In First Normal Form (1NF), we create tables for each group of related data and identify each row with a unique column.  In this case, the primary key is Teacher_ID.  Note: Additional review materials that cover the topic of 1NF are in Lesson 4.1.

Database Administration Fundamentals LESSON 4.2 Primary Keys (continued) Syntax Example SQL CREATE PRIMARY KEY CREATE TABLE Students_Nameof School ( Student_ID int NOT NULL PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) )  In this example (which is written to no particular version of SQL), a primary key is created and tied to Student_ID.  It has the data type of integer and defined as NOT NULL.  When one enters data into the field, it must have a value.  NOT NULL is an added constraint on what can go into the field.  Integer is the first constraint or restriction to what can be placed into the primary key field Student_ID.

Database Administration Fundamentals LESSON 4.2 Foreign Keys General Rules  A foreign key in one table points to a primary key in another table.  A foreign key column should not contain NULL values.  A NULL value in a foreign key means it has no relations to other tables. Working Definition  A foreign key is a reference to the primary key in another table.  — Using this method, we relate or reference all the tables together.  — Foreign keys do not need to have unique values.  — We use foreign keys as lookup items for another table.

Database Administration Fundamentals LESSON 4.2 Foreign Keys (continued) Example of a foreign key Table Teacher_classes with Teacher_ID and Class_ID:  There is a unique primary key for the table Teacher_classes.  Each teacher will have a unique ID tied to many class IDs. The Class_ID is the foreign key.  The table above shows that one teacher can have different classes and multiple sections of the same class.  To find the details of the classes, one would use the foreign key to find the description of the class in the class_description table.  The table above is a virtual table or a view of a query on Teacher_ID “78v22”. Note: See Lessons 2.3 and 3.1 for more on this subject. Teacher_IDClass_ID 1Class_ID 2Class_ID 3 78v Lit

Database Administration Fundamentals LESSON 4.2 Foreign Key (continued) Syntax Example to create a foreign key at the same time as a primary key CREATE TABLE Students_Name of School ( Student_ID int NOT NULL PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), ZipCode int(5) NOT NULL FOREIGN KEY)  In this example (which is not written to any specific version of SQL), a foreign key is created and tied to ZipCode.  It has the data type of integer and cannot be empty ( NULL ).  When one enters data into the field, it must have a value. The foreign key is the lookup key to find city and state information for this student.

Database Administration Fundamentals LESSON 4.2 Data Integrity  Data integrity is enforced by a series of constraints or rules.  Three types of integrity constraints are an inherent part of the relational database model. 1. Entity integrity—Focus is on the primary key.  The rule is that every table must have a primary key, and that the column or columns chosen to be the primary key should be unique and not NULL.

Database Administration Fundamentals LESSON 4.2 Data Integrity (continued) 2. Referential integrity—Focus is on the foreign key.  The rule is any foreign key value can only be in one of two states. First state: a foreign key value refers to a primary key value of a table in the database. Second state: a foreign key value can be NULL. Either there is no relationship between the objects in the database, or this relationship is undefined.

Database Administration Fundamentals LESSON 4.2 Data Integrity (continued) 3. Domain integrity—Specifies that all allowable values in a relational database must be declared.  A domain is a set of values of the same type (data type).  Domains, therefore, are groups of values from which actual values are drawn.  A domain describes the set of possible values for a given attribute.  Because a domain constrains the attribute's values, it can be considered a constraint. Example: The character value “John” is not in the integer domain. The integer value “42” satisfies the domain constraint.

Database Administration Fundamentals LESSON 4.2 Lesson Review 1. Why are unique keys important? 2. What is the relationship between keys and data types? 3. What are the differences between a foreign key and a primary key? 4. Why can a foreign key have a NULL value but a primary key cannot? 5. List and describe the three rules of data integrity.