CSE 2111 Lecture 10-Introduction to Database Management

advertisement
Computer Science & Engineering 2111
Introduction to Database Management Systems
Relationships and Database Creation
CSE 2111 Introduction to Database
Management Systems
1
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
2
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
3
Foreign Key:
ClientID
Table Name:
On Charges table 1 Client
Primary Key:
ClientID
∞
Foreign Key:
ClientID On Payments table
1
∞
Table Name:
Payments
Primary Key:
None
Table Name:
Charges
Primary Key:
None
∞
Foreign Key:
PaymentType On Payments table
RELATIONSHIP DIAGRAM FOR
RESEARCH PAPERS DATABASE
1
Table Name:
MethodTypes
Primary Key:
MethodID
CSE 2111 Introduction to Database
Management Systems
4
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 information
CSE 2111 Introduction to Database
Management Systems
5
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
6
Download