我瞭解SQL的第一次,而我是從這個網站上沿以下:http://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561爲什麼要使用外鍵?
筆者去宣告一個外鍵的麻煩,但我不認爲它曾經做任何事情爲了我們。下面是從頁的例子:
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);
一些表的創建,並在訂單表的CUSTOMER_ID由引用CUSTOMER_ID Customers表。
這裏是混淆了我:文章的作者執行與各種結果下面的語句:
SELECT * FROM customers JOIN orders;
SELECT * FROM customers NATURAL JOIN orders;
SELECT * FROM customers JOIN orders WHERE customers.customer_id = orders.customer_id;
有涉及除其他事項外LEFT
和RIGHT OUTER JOIN
S比語句,但在任何時候做外鍵曾影響過任何事物
我錯過了什麼,或者這些例子太簡單了,不值得使用外鍵?
感謝