重要
翻訳は あなたが参加できる コミュニティの取り組みです。このページは現在 73.08% 翻訳されています。
15.3. レッスン: モデルにデータを追加する
作成したモデルには、今、含まれることを意図されるデータが投入される必要があります。
このレッスンの目標: データベースモデルに新しいデータを挿入する方法を学習します。
15.3.1. insert文
How do you add data to a table? The sql INSERT statement provides the
functionality for this:
insert into streets (name) values ('High street');
注意すべきいくつかの事:
After the table name (
streets), you list the column names that you will be populating (in this case only thenamecolumn).After the
valueskeyword, place the list of field values.文字列は単一引用符で囲む必要があります。
Note that we did not insert a value for the
idcolumn; this is because it is a sequence and will be auto-generated.If you do manually set the
id, you may cause serious problems with the integrity of your database.
You should see INSERT 0 1 if it is successful.
テーブル内のすべてのデータを選択して、挿入アクションの結果を見ることができます:
select * from streets;
結果:
select * from streets;
id | name
----+-------------
1 | High street
(1 row)
やってみよう: ★☆☆ (初級レベル)
Use the INSERT command to add a new street to the streets table.
答え
使うべきSQLコマンドはこのようなものです(街路の名称はあなたが選んだもので置き換えることができます):
insert into streets (name) values ('Low Road');
15.3.2. 制約に従ってデータの追加を順序付けする
15.3.3. やってみよう: ★★☆ (中級レベル)
Try to add a person object to the people table with the following details:
Name: Joe Smith
House Number: 55
Street: Main Street
Phone: 072 882 33 21
注釈
この例では、文字列ではなく整数として電話番号を定義したことを思い出してください。
At this point, you should have an error report if you try to do this without
first creating a record for Main Street in the streets table.
以下のことに気づいたはずです:
その名前を使用して街路を追加できません
You can't add a street using a street
idbefore first creating the street record on thestreetstable
2つのテーブルが主キー/外部キーのペアを介して結合していることに注意してください。これは、有効な人は有効な対応する街路レコードも存在していなければ作成できないことを意味します。
上記の知識を使用して、データベースに新しい人を追加します。
答え
正しいSQL文は次のとおりです:
insert into streets (name) values('Main Road');
insert into people (name,house_no, street_id, phone_no)
values ('Joe Smith',55,2,'072 882 33 21');
(先ほどと同じようにselect文を使って)もう一度街路テーブルを見てみると、Main Road のエントリの id が 2 であることがわかるでしょう。
そのため、上記のように単に数字の 2 を入力することができます。上のエントリで Main Road が完全に書き込まれていなくても、データベースはそれを street_id の値 2 と関連付けることができるのです。
すでに新しい街路オブジェクトを追加している場合、その新しい Main Road は 2 ではなく *3*の id を持っていることに気づくでしょう。
15.3.4. データを選択
レコードを選択するための構文はすでに示しました。さらにいくつかの例を見てみましょう:
select name from streets;
select * from streets;
select * from streets where name='Main Road';
後のセッションでは、データを選択してフィルタリングする方法について詳細に見て行きます。
15.3.5. データを更新
何か既存のデータに変更を加えたい場合は?例えば、街路名が変更されます:
update streets set name='New Main Road' where name='Main Road';
Be very careful using such update statements - if more than one record matches
your WHERE clause, they will all be updated!
よりよい解決策は、テーブルの主キーを使用して変更するレコードを参照することです:
update streets set name='New Main Road' where id=2;
It should return UPDATE 1.
注釈
The WHERE statement criteria are case sensitive, Main
Road is not the same as Main road.
15.3.6. データを削除
In order to delete an object from a table, use the DELETE command:
delete from people where name = 'Joe Smith';
今度は人々のテーブルを見てみましょう:
address=# select * from people;
id | name | house_no | street_id | phone_no
----+------+----------+-----------+----------
(0 rows)
15.3.7. やってみよう: ★★★ (上級レベル)
学んだスキルを使用して、データベースに新しい友達を何人か追加してください:
name | house_no | street_id | phone_no
-----------------+----------+-----------+--------------
Joe Bloggs | 3 | 2 | 072 887 23 45
Jane Smith | 55 | 3 | 072 837 33 35
Roger Jones | 33 | 1 | 072 832 31 38
Sally Norman | 83 | 1 | 072 932 31 32
15.3.8. 結論
以前に作成した既存のモデルに新しいデータを追加する方法がわかりましたね。データの新しい種類を追加したい場合は、そのデータを格納する新しいモデルを変更かつ/または作成したいこともあることを忘れないでください。
15.3.9. 次は?
データを追加してしまったので、クエリを使用してさまざまな方法でこのデータにアクセスする方法を学びましょう。