Computer Science & Engineering 2111 Introduction to Database Management Systems Relationships and...

6
Computer Science & Engineering 2111 Introduction to Database Management Systems Relationships and Database Creation 1 CSE 2111 Introduction to Database Management Systems

Transcript of Computer Science & Engineering 2111 Introduction to Database Management Systems Relationships and...

Page 1: Computer Science & Engineering 2111 Introduction to Database Management Systems Relationships and Database Creation 1 CSE 2111 Introduction to Database.

1

Computer Science & Engineering 2111

Computer Science & Engineering 2111

Introduction to Database Management SystemsRelationships and Database Creation

CSE 2111 Introduction to Database Management Systems

Page 2: Computer Science & Engineering 2111 Introduction to Database Management Systems Relationships and Database Creation 1 CSE 2111 Introduction to Database.

2

Steps when setting up a Database

1. Decide what information you will store in the database

2. Create the layout of the database

CSE 2111 Introduction to Database Management Systems

Page 3: Computer Science & Engineering 2111 Introduction to Database Management Systems Relationships and Database Creation 1 CSE 2111 Introduction to Database.

3

3. Create a relationship diagram to identify the table relationships, primary keys and foreign keys

• We know the information we want to store, but how do we match a customer’s name to their charges and payments?

• We need a way to relate these two tables to extract useful information.

• We can relate these two tables by matching the Client ID• Foreign key

• A field that defines the relationship between 2 tables

• Relationship Rules• Must be a primary key (unique) in at least one of the tables• The field names on each table do not have to match as long

as the information is the same.• The related fields must be the same data type

– number, text etc.

CSE 2111 Introduction to Database Management Systems

Page 4: Computer Science & Engineering 2111 Introduction to Database Management Systems Relationships and Database Creation 1 CSE 2111 Introduction to Database.

4

Table Name:ClientPrimary Key:ClientID

Table Name:ChargesPrimary Key:None

Foreign Key:ClientIDOn Charges table

Foreign Key:ClientID On Payments table

Table Name:MethodTypesPrimary Key:MethodID

Foreign Key:PaymentType On Payments table

RELATIONSHIP DIAGRAM FOR RESEARCH PAPERS DATABASE

1 1

1

CSE 2111 Introduction to Database Management Systems

Table Name:PaymentsPrimary Key:None

Page 5: Computer Science & Engineering 2111 Introduction to Database Management Systems Relationships and Database Creation 1 CSE 2111 Introduction to Database.

5

4. Create the database using the database software

• Create tables– Fields– Primary keys– Field Types– Field Properties

• Create Relationships– “Join” tables– Enforce Referential Data Integrity

• A set of rules that specifies what records may exist in each table– A record input with a foreign key must always have a matching record in the

primary key table in the relationship

• Cascade Delete Related Records– Removing any entry in a primary key field will automatically remove all entries in

foreign key fields of related tables.

• Cascade Update Related Records– Updating any entry in a primary key field will automatically update all entries in

foreign key fields of related tables.

• Input the informationCSE 2111 Introduction to Database

Management Systems

Page 6: Computer Science & Engineering 2111 Introduction to Database Management Systems Relationships and Database Creation 1 CSE 2111 Introduction to Database.

6

Once Relationships are established you can gather information from one or more tables to answer questions

• Create a list of account numbers and owner names and total transactions

• What are the total deposits made by accounts starting with 5?

• What is the total balance of all accounts held by Jane Doe ?

These requests are known as Queries

CSE 2111 Introduction to Database Management Systems