sql server 2008 – entity relationships in a database

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

Upload: saeran

Post on 05-Jan-2016

35 views

Category:

Documents


0 download

DESCRIPTION

SQL Server 2008 – Entity Relationships in a Database. Learningcomputer.com. 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: SQL Server 2008 – Entity Relationships in a Database

Learningcomputer.com

SQL Server 2008 – Entity Relationships in a Database

Page 2: SQL Server 2008 – Entity Relationships in a Database

Some Terms to get us goingEntity relationship diagram (ERD) shows the db

structureAn Entity or Table is any object that we store

information onAn Attribute or a field is a specific value in a Table like

SSN in an Employee tableA Row or Record is one instance on an object in a TableA Primary Key (PK) is a field that can server as a

unique identifier in a tableA 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

Page 3: SQL Server 2008 – Entity Relationships in a Database

Customer and SalesOrderHeader (AdventureWorks2008)

Page 4: SQL Server 2008 – Entity Relationships in a Database

Types of Database RelationshipOne to OneOne to Many (Most common)Many to Many

Page 5: SQL Server 2008 – Entity Relationships in a Database

One - One RelationshipIn this relationship, a row in table A can

have no more than one matching row in table B, and vice versa

Not very commonDivide a table with many columns into two

for performanceIsolate part of a table for security reasons Example Account and Account Ext table

Page 6: SQL Server 2008 – Entity Relationships in a Database

One - Many RelationshipThis 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

Page 7: SQL Server 2008 – Entity Relationships in a Database

Many to Many RelationshipIn 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

Page 8: SQL Server 2008 – Entity Relationships in a Database

College databaseI have created a new database called CollegeFirst I will create and populate student data

using TSQLSecond I will create and populate teacher data

using TSQLNow 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

Page 9: SQL Server 2008 – Entity Relationships in a Database

What in the world is a Join?In order to understand relationships, you

have to know JoinsJoins are an integral component of

relational database design and usageJoins 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

Page 10: SQL Server 2008 – Entity Relationships in a Database

Inner JoinA join that displays only the rows that have

a match in both joined tablesMost common type of join in practiceFor 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

Page 11: SQL Server 2008 – Entity Relationships in a Database

Syntax for Inner JoinOld 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 SON C.CUSTOMERID=S.CUSTOMERID

Demo

Page 12: SQL Server 2008 – Entity Relationships in a Database

Outer JoinA join that includes rows even if they do not

have related rows in the joined tableOuter joins are typically used to find

corrupt data or unmatched dataAfter the results, you can run a delete

query to remove the problem recordsThree variations of an outer join to specify

the unmatched rows to be included

Page 13: SQL Server 2008 – Entity Relationships in a Database

Types of Outer JoinsLEFT OUTER JOINRIGHT OUTER JOINFULL OUTER JOIN

Page 14: SQL Server 2008 – Entity Relationships in a Database

Left Outer JoinAll 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

Page 15: SQL Server 2008 – Entity Relationships in a Database

Right Outer JoinAll 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

Page 16: SQL Server 2008 – Entity Relationships in a Database

Full Outer JoinAll 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

Page 17: SQL Server 2008 – Entity Relationships in a Database

Referential integrityReferential 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