Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.

Slides:



Advertisements
Similar presentations
DB glossary (focus on typical SQL RDBMS, not XQuery or SPARQL)
Advertisements

Keys, Referential Integrity and PHP One to Many on the Web.
The Relational Database Model
Maintenance Modifying the data –Add records –Delete records –Update records Modifying the design –Add fields into tables –Remove fields from a table –Change.
Fundamentals of Relational Database Yong Choi School of Business CSUB, Bakersfield.
Relational Model Stores data as tables –Each column contains values about the same attribute –Each column has a distinct name –Each row contains values.
The Relational Database Model:
Concepts of Database Management Sixth Edition
The Relational Database Model. 2 Objectives How relational database model takes a logical view of data Understand how the relational model’s basic components.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
The Relational Model Codd (1970): based on set theory Relational model: represents the database as a collection of relations (a table of values --> file)
Database – Part 2a Dr. V.T. Raja Oregon State University.
Computer Science & Engineering 2111 Introduction to Database Management Systems Relationships and Database Creation 1 CSE 2111 Introduction to Database.
Define Table Relationships—1 of 3 One of the most powerful features of a relational database management system, such as Access, is its ability to define.
3 The Relational Model MIS 304 Winter Class Objectives That the relational database model takes a logical view of data That the relational model’s.
1 Microsoft Access 2002 Tutorial 3 – Querying a Database.
© Pearson Education Limited, Chapter 2 The Relational Model Transparencies.
Primary & Foreign Keys. PK & FK 1.Primary key is required 2.The PK must be unique 3.If the primary key from one table is related to a field in another.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
DAY 14: ACCESS CHAPTER 1 Tazin Afrin October 03,
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
RDB/1 An introduction to RDBMS Objectives –To learn about the history and future direction of the SQL standard –To get an overall appreciation of a modern.
Database Technical Session By: Prof. Adarsh Patel.
Microsoft Access 2003 Define some key Access terminology: Field – A single characteristic or attribute of a person, place, object, event, or idea. Record.
Fundamentals of Relational Database Yong Choi School of Business CSUB, Bakersfield.
IE 423 – Design of Decision Support Systems Database development – Relationships and Queries.
MS Access: Creating Relational Databases Instructor: Vicki Weidler Assistant: Joaquin Obieta.
Relationships and Advanced Query Concepts Using Multiple Tables Please use speaker notes for additional information!
DAY 12: DATABASE CONCEPT Tazin Afrin September 26,
Chapter 2 The Relational Data Model. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Relational model basics Integrity.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
1 Outline  What is a Primary Key?  AutoNumber primary keys  Single-field primary keys  Composite-field primary key  About Foreign Keys  Database.
Relational Database. Database Management System (DBMS)
Microsoft Access Intro Class 6 Relationships.
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
Introduction to Database using Microsoft Access 2013 Part 7 November 19, 2014.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Relational Theory and Design
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 3 The Relational Database Model.
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
Session 1 Module 1: Introduction to Data Integrity
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 3 The Relational Database Model.
Chapter 3: Relational Databases
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Week 2 Lecture The Relational Database Model Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
Quiz Which of the following is not a mandatory characteristic of a relation? Rows are not ordered (Not required) Each row is a unique There is a.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
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.
In this session, you will learn to: Manage databases Manage tables Objectives.
XP Chapter 1 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Level 2 Objectives: Understanding and Creating Table.
Tables & Relationships
Relational Model.
CIS 155 Table Relationship
Database Systems: Design, Implementation, and Management Tenth Edition
Chapter 4 Relational Model Characteristics
Lecture 2 The Relational Model
Module 5: Implementing Data Integrity by Using Constraints
Chapter 3 The Relational Database Model
Relational Queries (query 12) Display vendor contact info (contact person and phone number) for inventory products (relationship query) Query: Inventory.
Database Systems: Design, Implementation, and Management
Presentation transcript:

Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database

Some Terms to get us going Entity relationship diagram (ERD) shows the db structure An Entity or Table is any object that we store information on An Attribute or a field is a specific value in a Table like SSN in an Employee table A Row or Record is one instance on an object in a Table A Primary Key (PK) is a field that can server as a unique identifier in a table A Foreign Key (FK) is a field in a child table that stores related info from a parent table e.g. Customer and Orders Relationship is an association between tables

Customer and SalesOrderHeader (AdventureWorks2008)

Types of Database Relationship One to One One to Many (Most common) Many to Many

One - One Relationship In this relationship, a row in table A can have no more than one matching row in table B, and vice versa Not very common Divide a table with many columns into two for performance Isolate part of a table for security reasons Example Account and Account Ext table

One - Many Relationship This is the most common type of relationship. In this type of relationship, a row in table A can have one to many matching rows in table B, but a row in table B can have only one matching row in table A Typically the PK of the primary (parent) table matches the same data (FK) in the secondary (child) table. Example is Customer and SalesOrderHeader table shown earlier

Many to Many Relationship In this type, a row in table A can have many matching rows in table B, and vice versa You create such a relationship by defining a third table, called a junction table, whose primary key consists of the foreign keys from both table A and table B Example would be Students and Teachers tables

College database I have created a new database called College First I will create and populate student data using TSQL Second I will create and populate teacher data using TSQL Now remember this is a many – many relationship so what do we need? We need a junction table I will create the junction table called student_teacher which will contain a field for studentid and one for teacherid Demo

What in the world is a Join? In order to understand relationships, you have to know Joins Joins are an integral component of relational database design and usage Joins let you match data from multiple tables; based on significant key information A typical join condition specifies a foreign key from one table and its associated primary key in the other table Types of Joins are INNER and OUTER

Inner Join A join that displays only the rows that have a match in both joined tables Most common type of join in practice For example, you can join the Customers and SalesOrderHeader tables to create a result set that shows the Orders for each Customer Null values do not match other null values so they are ignored You can have any number of joins in one statement as long as there is a valid relationship between the tables

Syntax for Inner Join Old Syntax is SQL-87: SELECT * FROM SALES.CUSTOMER C, SALES.SALESORDERHEADER S WHERE C.CUSTOMERID=S.CUSTOMERID New Syntax (ANSI SQL or SQL-92): SELECT * FROM SALES.CUSTOMER C INNER JOIN SALES.SALESORDERHEADER S ON C.CUSTOMERID=S.CUSTOMERID Demo

Outer Join A join that includes rows even if they do not have related rows in the joined table Outer joins are typically used to find corrupt data or unmatched data After the results, you can run a delete query to remove the problem records Three variations of an outer join to specify the unmatched rows to be included

Types of Outer Joins LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN

Left Outer Join All rows from the first-named table (the "left" table, which appears leftmost in the JOIN clause) are included. Unmatched rows in the right table do not appear. We are going to use Customer and SalesOrderHeader tables Basically we are interested in finding out Customers that have yet to place an order Demo

Right Outer Join All rows in the second-named table (the "right" table, which appears rightmost in the JOIN clause) are included. Unmatched rows in the left table are not included. Similar in concept to Left Outer Join except it is reversed

Full Outer Join All rows in all joined tables are included, whether they are matched or not. For example, a full outer join between students and teachers shows all students and all teachers, even those that have no match in the other table. This type of join is not very common

Referential integrity Referential integrity is a system of rules that ensure relationships between related tables are valid You cannot enter a value in the foreign key column of the child table (Orders) if that value does not exist in the primary key of the parent table (Customers) You cannot delete a row from a primary table (Customers) if rows matching it exist in a related table (Orders) In SQL Server you can enforce this via Cascade Updates and Delete or through the use of Triggers