24 golden coins, 1 is fake ( weighs less). database concepts ahmad, mohammad j. cs 101
TRANSCRIPT
24 GOLDEN COINS, 1 IS FAKE ( WEIGHS LESS)
DATABASE CONCEPTS
Ahmad, Mohammad J.CS 101
3
Graphicsheat.com
4
WHY DO DATABASES MATTER TO YOU?
• Among non-CS people, database skills are rare• A healthy understanding of databases makes you
marketable• Almost every business has a database of some sort – and
someone has to manage it!• You could be selected to be trained by your company to
manage a database someday!
5
IN THE BEGINNING
• Pre-1970, databases were flat
• We have essentially worked with “flat” databases:– Tab Delimited– CSV
• These databases were just text files (.txt)
THE PROBLEM?
Lname, FName, Age, Salary|Smith, John, 35, $280|Doe, Jane, 28, $325|Brown, Scott, 41, $265|Howard, Shemp, 48, $359|Taylor, Tom, 22, $250
• A text file database, like above, is difficult to search though and find specific information
6
AND THEN THERE WAS LIGHT
• Relational databases were created in 1970
• IBM researcher, E.F. Codd, wrote a paper describing them
• Relational databases store information in tables– For visual purposes, think similar to Excel tables
8
RELATIONAL DATABASES ARE EVERYWHERE!
Company Inventory Tracking
Schools
Online Stores
Social Networking
Offline Stores
Healthcare
Record Keeping
Internet
9
SAMPLE RDB
10
LET’S BACK UP…
11
FIELD
• The smallest data element contained in a table• Field types:
– Text– Numeric– Date
• Field examples:– Birthdates– Last Name– Age– Social Security #
12
RECORD
• Record – a complete set of all of the data elements (fields) about one person, place, event, or concept
• Example record in a course gradebook:
– First Name, Last Name, Student ID, Age, E-mail
13
TABLE
• The foundation of every database
• Contains a primary key (unique field)
• Is a collection of related records
– Example Table:• All Student records in my CS 101 sections would be added to my Section
Roster Table during registration
14
KEY
• A field that contains a unique identifier
• Foreign key– A field in one table, that is a primary key in another
15
DATABASE
• Consists of:– One or more tables to store data– One or more forms to enter data– One or more reports to output the table data as organized
information
16
LET’S DEFINE A TABLE
• Let’s make a table for student information
– What fields?– What data types?– How do we identify records?
17
RELATIONSHIPS
“When two tables notice each other…”
• Access is a Relational Database Management System
• That being said it’s important that, over time, we learn to create relationships effectively.
18
RELATIONSHIP BASICS
• For now, let’s ignore how we actually make a relationship (it’s just drag and drop)
• Let’s examine some already created relationships and talk through them
SAMPLE RELATIONSHIPS
19
WHAT CHANGED?
WHY IS THIS NECESSARY?
RELATIONSHIP TYPES IDENTIFIED
• One-to-Many
• Many-to-Many***
• One-to-One
MANY-TO-MANY?
23
A BRIEF NOTE ON MANY TO MANY
“Think about a simple relationship like the one between Authors and Books. An author can write many books. A book could have many authors. Now, without a bridge table to resolve the many-to-many relationship, what would the alternative be? You'd have to add multiple Author_ID columns to the Books table, one for each author. But how many do you add? 2? 3? 10? However many you choose, you'll probably end up with a lot of sparse rows where many of the Author_ID values are NULL and there's a good chance that you'll run across a case where you need "just one more." So then you're either constantly modifying the schema to try to accommodate or you're imposing some artificial restriction ("no book can have more than 3 authors") to force things to fit.”
-Joe Stefanelli on StackOverflow.com
25
REFERENTIAL INTEGRITY
• For relationships, you want to be able to be absolutely sure that they are valid.
• What if you had a business, and didn’t check that the people you wrote checks to were employees?
26
WHAT COULD HAPPEN?
27
CASCADE UPDATES / DELETES
• Cascading Updates (Good)– Typically, we will always cascade updates. Let’s assume we
change the stock number of an item, all orders that referenced that stock number would automatically update (for record keeping information).
• Cascades Deletes (Bad.. Well, generally)– Let’s assume an employee is working on some big projects.
The HR department accidentally removes the employee… every project in the database, that refers to the deleted employee, will be deleted.
SPREADSHEET VS DATABASES
MS Excel
• Only need one worksheet to manage data
• Have mostly numeric data (i.e. expense reports)
• Require subtotals and totals in your worksheet
• Want to run a series of “What-If” Scenarios
• Need to create complex charts or graphs
MS Access
• Require multiple related tables to store data
• Have a large amount of data• Need to connect to and send/receive
data to/from external databases• Need to group, sort, and total data
based on various parameters• Have an application that requires
multiple users to connect to one data source at once
28