Chapter 28 - Relational Databases


Chapter Goals



Chapter 28 - Relational Databases


28.1 Organising Database Information

28.1.1 Database Tables

Product_Code Description Unit_Price
116-064 Toaster 24.95
257-535 Hair dryer 29.95
643-119 Car vacuum 19.99

28.1.1 Database Tables (cont.)


28.1.1 Database Tables (cont.)

SQL Data Type C++ Data Type
INTEGER or INT int
REAL float
DOUBLE double
DECIMAL(n, x) Fixed-point decimal numbers with n total digits and x digits after the decimal point
BOOLEAN bool
CHARACTER(x) or CHAR(x) Fixed-length string of length x. Similar to string

28.1.1 Database Tables (cont.)


28.1.1 Database Tables (cont.)


28.1.1 Database Tables (cont.)


28.1.2 Linking Tables


28.1.2 Linking Tables (cont.)


28.1.2 Linking Tables (cont.)


28.1.2 Linking Tables (cont.)

Invoice_Number Customer_
Name
Customer_
Address
Customer_
City
Customer_
State
Customer_
Zip
...
INTEGER CHAR(40) CHAR(40) CHAR(30) CHAR(2) CHAR(10) ...
11731 Sam's Small Appliances 100 Main St. Anytown CA 98765 ...
11732 Electronics Unlimited 1175 Liberty Ave. Pleasantville MI 45066 ...
11733 Sam's Small Appliances 100 Main St. Anytown CA 98765 ...

28.1.2 Linking Tables (cont.)


28.1.2 Linking Tables (cont.)

  • In C++, many objects can refer to a single object
  • To accomplish this in SQL:
    • Break data into 2 tables
    • Solves replication problem
    • Cust# primary key for Customer table
      • A foreign key in the Invoice table
      • Links tables
Invoice
Invoice_
Number
Customer_
Number
Customer_
Payment
INTEGER INTEGER DECIMAL(10,2)
11731 3175 0
11732 3176 249.95
11733 3175 0

28.1.2 Linking Tables (cont.)

Customer
Customer_
Number
Name Address City State Zip
INTEGER CHAR(40) CHAR(40) CHAR(30) CHAR(2) CHAR(10)
3175 Sam's Small Appliances 100 Main St. Anytown CA 98765
3176 Electronics Unlimited 1175 Liberty Ave. Pleasantville MI 45066

28.1.2 Linking Tables (cont.)


28.1.3 Implementing One-to-Many Relationships


28.1.3 Implementing One-to-Many Relationships (cont.)

Poor design for Invoice Table
Invoice_
Number
Customer_
Number
Product_
Code1
Quantity1 Product_
Code2
Quantity2 Product_
Code3
Quantity3 Payment
INTEGER INTEGER CHAR(10) INTEGER CHAR(10) INTEGER CHAR(10) INTEGER DECIMAL(10,2)
11731 3175 116-064 3 257-535 1 643-119 2 0

28.1.3 Implementing One-to-Many Relationships (cont.)

  • Break into 2 tables
  • Link items to an invoice w/Invoice_Number
Item
Invoice_
Number
Product_
Code
Quantity
INTEGER CHAR(10) INTEGER
11731 116-064 3
11731 257-535 1
11731 643-119 2
11732 116-064 10
11733 116-064 2
11733 643-119 1
Invoice
Invoice_
Number
Customer_
Number
Payment
INTEGER INTEGER DECIMAL(10,2)
11731 3175 0
11732 3176 249.5
11733 3175 0

28.1.3 Implementing One-to-Many Relationships (cont.)

  • Our DB now has 4 tables:
    • Invoice
    • Customer
    • Item
    • Product

28.2 Queries


28.2 Queries (cont.)

Product
Product_Code Description Unit_Price
116-064 Toaster 24.95
257-535 Hair dryer 29.95
643-119 Car vacuum 19.99
Item
Invoice_Number Product_Code Quantity
INTEGER CHAR(10) INTEGER
11731 116-064 3
11731 257-535 1
11731 643-119 2
11732 116-064 10
11733 116-064 2
11733 643-119 1

28.2 Queries (cont.)

Invoice
Invoice_Number Customer_Number Payment
INTEGER INTEGER DECIMAL(10,2)
11731 3175 0
11732 3176 249.5
11733 3175 0
Customer
Customer_Number Name Address City State Zip
INTEGER CHAR(40) CHAR(40) CHAR(30) CHAR(2) CHAR(10)
3175 Sam's Small Appliances 100 Main St. Anytown CA 98765
3176 Electronics Unlimited 1175 Liberty Ave. Pleasantville MI 45066

28.2.1 Simple Queries


28.2.2 Selecting Columns


28.2.3 Selecting Subsets


28.2.3 Selecting Subsets (cont.)


28.2.3 Selecting Subsets (cont.)


28.2.3 Selecting Subsets


28.2.4 Calculations


28.2.5 Joins


28.2.5 Joins (cont.)


28.2.5 Joins (cont.)


28.2.5 Joins (cont.)


28.2.5 Joins (cont.)


28.2.5 Joins (cont.)


28.2.6 Updating and Deleting Data


28.2.6 Updating and Deleting Data


28.3 Installing a Database


28.3 Installing a Database (cont.)

General sequence to install and test installation:


28.3 Installing a Database (cont.)

To connect from C++ (specific to MySQL):


28.4 Database Programming in C++

28.4.1 Connecting to the MySQL Database


28.4.1 Connecting to the MySQL Database


28.4.1 Connecting to the MySQL Database


28.4.2 Executing SQL Statements


28.4.2 Executing SQL Statements (cont.)


28.4.2 Executing SQL Statements (cont.)


28.4.3 Analyzing Query Results


28.4.3 Analyzing Query Results (cont.)


28.4.3 Analyzing Query Results (cont.)


28.4.3 Analyzing Query Results (cont.)


28.4.3 Analyzing Query Results (cont.)


28.4.3 Analyzing Query Results (cont.)


28.4.3 Analyzing Query Results (execsql.cpp)


28.4.3 Analyzing Query Results (product.sql)


28.5 Case Study: Accessing an Invoice Database


28.5 Case Study: Accessing an Invoice Database (cont.)

printinv.cpp


28.5 Case Study: Accessing an Invoice Database (cont.)


28.5 Case Study: Accessing an Invoice Database (cont.)

printinv.cpp


28.5 Case Study: Accessing an Invoice Database (cont.)


28.5 Case Study: Accessing an Invoice Database (cont.)

addinv.cpp


28.5 Case Study (printinv.cpp)


28.5 Case Study (addinv.cpp)


Advanced Topic 23.1


Transactions


Chapter Summary


  1. RDB stores information in tables. Each column has a datatype and unique name
  2. SQL - command language for interacting w/the DB
  3. CREATE TABLE and INSERT commands to add to a DB. SELECT to query. UPDATE and DELETE to modify the data
  4. Do not replicate data among rows. Distribute over multiple tables
  5. Primary key - column(s) that uniquely identify a record
    Foreign key - reference to primary key in linked table
  6. A join is a query that involves multiple tables
  7. Use an API to access a DB from a C++ program
  8. Transaction - set of updates that should succeed entirely, or not at all