Question

This file shows a customer database and a CD database.
The customers are foreign keyed as buyerIDs. Some of the customers/buyers have purchased several copies of one particular CD.
I created the file in Notepad++, imported it into phpMyAdmin, I played around with the Query section to try to get the example statement:

Customer Gerald Bostick bought 3 copies of Thick as a Brick 

The query I came up with is:

SELECT `customer`.`CustName`, `customer`.`CDPurchases`, `cd`.`Title`
FROM `customer`, `cd`

What I got was every customer in the table purchasing each CD:

Joe Doe 12  Ascension
Suzy Creamcheese    3   Ascension
Jane Doe    1   Ascension
Gerald Bostick  3   Ascension
Lisa Simpson    NULL    Ascension
Joe Doe 12  The Velvet Rope
Suzy Creamcheese    3   The Velvet Rope
Jane Doe    1   The Velvet Rope
Gerald Bostick  3   The Velvet Rope
Lisa Simpson    NULL    The Velvet Rope
Joe Doe 12  The Pecan Tree
Suzy Creamcheese    3   The Pecan Tree
Jane Doe    1   The Pecan Tree
Gerald Bostick  3   The Pecan Tree
Lisa Simpson    NULL    The Pecan Tree

Is the foreign key set up correctly? I changed it from

CONSTRAINT FK_Buyer FOREIGN KEY FK_Buyer (BuyerId)
REFERENCES Customer (CustID)

to

CONSTRAINT FK_Buyer FOREIGN KEY (BuyerId)
REFERENCES Customer (CustID)

Or is the SET statement or my query incorrect?

My coding is:

DROP DATABASE IF EXISTS Library;
CREATE DATABASE Library;
USE Library;
DROP TABLE IF EXISTS Customer;
DROP TABLE IF EXISTS CD;


CREATE TABLE Customer (
  CustID INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  CustName VARCHAR(20) NOT NULL,
  CDPurchases INTEGER,
  PRIMARY KEY (CustID)
);

CREATE TABLE CD (
  CDID INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  Title VARCHAR(45) NOT NULL,
  BuyerId INTEGER UNSIGNED,
  Price FLOAT(6,2) UNSIGNED NOT NULL,
  PRIMARY KEY (CDID),
  CONSTRAINT FK_Buyer FOREIGN KEY (BuyerId)
    REFERENCES Customer (CustID)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT
);

INSERT INTO Customer VALUES (null, "Joe Doe", 12);
SET @joedoe := LAST_INSERT_ID();
INSERT INTO Customer VALUES (null, "Suzy Creamcheese", 3);
INSERT INTO Customer VALUES (null, "Jane Doe", 1);
SET @janedoe := LAST_INSERT_ID();
INSERT INTO Customer VALUES (null, "Gerald Bostick",3);
SET @geraldbostick := LAST_INSERT_ID();
INSERT INTO Customer VALUES (null, "Lisa Simpson", null);

INSERT INTO CD VALUES (null, "Thriller", @janedoe, 12.99);
INSERT INTO CD VALUES (null, "Grown and Sexy", null, 16.95);
INSERT INTO CD VALUES (null, "Ascension", null, 14.50);
INSERT INTO CD VALUES (null, "The Velvet Rope", null, 13.85);
INSERT INTO CD VALUES (null, "The Pecan Tree", null, 9.99);
INSERT INTO CD VALUES (null, "Condensate", null, 11.85);
INSERT INTO CD VALUES (null, "The Dana Owens Album", null, 9.95);
INSERT INTO CD VALUES (null, "Dustbowl", @joedoe, 17.95);
INSERT INTO CD VALUES (null, "Thick as a Brick", @geraldbostick, 10.95);

Thank you for any help.

Was it helpful?

Solution

This is because your query created a cross-join or Cartesian product (x * x)

SELECT customer.CustName, customer.CDPurchases, cd.Title 
FROM customer, cd
WHERE customer.custid = cd.buyerid;

When you are joining two tables together, the database engine does not assume your join criteria - you have to explicitly state it. The fact that there is foreign key constraints only makes available better referential integrity - it does not force the tables to join.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top