0

I'm learning about SQL for the first time, and I'm following along from this website: http://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561

The author goes to the trouble of declaring a foreign key, but I don't think it ever does anything for us. Here's the example from the page:

CREATE TABLE customers (
    customer_id INT AUTO_INCREMENT PRIMARY KEY,
    customer_name VARCHAR(100)
);

CREATE TABLE orders (
    order_id INT AUTO_INCREMENT PRIMARY KEY,
    customer_id INT,
    amount DOUBLE,
    FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

INSERT INTO `customers` (`customer_id`, `customer_name`) VALUES
(1, 'Adam'),
(2, 'Andy'),
(3, 'Joe'),
(4, 'Sandy');

INSERT INTO `orders` (`order_id`, `customer_id`, `amount`) VALUES
(1, 1, 19.99),
(2, 1, 35.15),
(3, 3, 17.56),
(4, 4, 12.34);

Some tables are created, and the customer_id in the orders table is made to reference the customer_id in the customers table.

Here's what confuses me: the author of the article executes the following statements with various results:

SELECT * FROM customers JOIN orders;
SELECT * FROM customers NATURAL JOIN orders;
SELECT * FROM customers JOIN orders WHERE customers.customer_id = orders.customer_id;

There are more statements involving LEFT and RIGHT OUTER JOINs among other things, but at no point does the foreign key ever influence anything.

Am I missing something, or are these examples too simple to merit the use of a foreign key?

Thanks

2 Answers 2

0

The foreign key definition -- alas -- is not used in SELECT statements. This is a shame, because NATURAL JOIN should use the FOREIGN KEY definition. "Should" here is a moral imperative. The ANSI standard specifies that it uses columns with the same name, rather than the explicit key definitions. In my opinion, there is no reason to use NATURAL JOIN. Avoid it in favor of the USING clause instead.

They are used in other circumstances. Notably, the foreign key definition validates that data inserted into or updated in a table has correct values. This is very important and the foundation of relational integrity.

In addition, they allow cascading constraints. These "update" related tables when a primary key value is changed or deleted.

Note that a foreign key definition can be a hint used by the optimizer. You may not see the effect, but it is information that can be used for performance reasons.

0

The foreign key is being used in the statement

SELECT * FROM customers JOIN orders WHERE customers.customer_id = orders.customer_id;

in the WHERE clause - the part about customers.customer_id = orders.customer_id is exactly the application of the foreign key relationship.

Foreign key constraints do not result in implied foreign key relations during a SELECT. The purpose of a constraint is to restrict ("constrain") the values of data being inserted into the constrained columns. Thus they only come into play during INSERT and UPDATE operations.

In this case specifically, when a value is inserted or updated in orders.customer_id, the database first verifies that that value already exists in the column customers.customer_id; if the value does not exist the database will cause the statement to fail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.