Tables, keys and relationships · テーブル、キー、関係性
More than one table
Real databases split data across several tables. This lesson uses a small shop with two of them:
customer— one row per customer (id,name,city)orders— one row per order (id,customer_id,order_date,total)
Storing the customer's name on every order would repeat it again and again. Keeping customers in their own table stores each one once.
複数のテーブル
実際のデータベースではデータを複数のテーブルに分割して保持します。このレッスンは、2つのテーブルを持つ小さなショップを例にしています:
customer— 每位客户一行(id,name,city)orders— 每笔订单一行(id,customer_id,order_date,total)
注文ごとに顧客の名前を記録すると、同じ情報が繰り返されてしまいます。顧客を独自のテーブルに保持することで、各顧客を一度だけ保存できます。
Primary keys
A primary key is the column that uniquely identifies each row. It is never blank and never repeated.
customer.idis the primary key ofcustomer.orders.idis the primary key oforders.
Given a primary key value, you can always find exactly one row.
プライマリキー
プライマリキーとは、各行を一意に識別するカラムです。空白になることはなく、重複もしません。
customer.idはcustomerのプライマリキーです。orders.idはordersのプライマリキーです。
プライマリキーの値が与えられれば、常に正確に1行を見つけることができます。
Foreign keys
A foreign key is a column that holds the primary key of another table, linking the two.
orders.customer_id holds a value from customer.id — it says which customer each order belongs to.
Referential integrity means every foreign-key value must match a real primary key: an order can't belong to a customer who doesn't exist.
フォリンキー
フォリンキーとは、別のテーブルのプライマリキーを保持し、2つのテーブルを結びつけるカラムです。
orders.customer_id は customer.id の値を保持しており、どの顧客の注文であるかを示しています。
参照整合性とは、すべてのフォリンキー値が実在するプライマリキーと一致している必要があることを意味します。存在しない顧客に属する注文は許容されません。
Common mistakes
- A PRIMARY KEY is unique and cannot be NULL.
- A foreign key points to another table's primary key.
よくあるミス
- プライマリキーは一意であり、NULLにはできません。
- フォリンキーは別のテーブルのプライマリキーを指します。
Keys link two tables · キーは2つのテーブルを結びつける
A shared key lets a row in one table point to a row in another. · 共通のキーにより、あるテーブルの行が別のテーブルの行を指すことができる。
Show the whole customer table so you can see the rows and their primary key id. · 全customer テーブルを表示して、行とその主キーidを確認する。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Show each order's id, its customer_id (the foreign key), and its total, ordered by id. · 各注文のid、その外鍵customer_id、およびtotalを表示し、id で並べ替える。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。