PHP-DB - 1. サンプルデータの準備
ここからは簡単なEラーニングシステムの開発を想定して、以下の2つのテーブルを作成して学習を進めます。
categoriesテーブルcoursesテーブル
categories テーブル
categories テーブルには以下の3件のレコードを作成します。
| id | title |
|---|---|
| 1 | Programming |
| 2 | Design |
| 3 | Marketing |
categories テーブルの作成に必要なSQLは以下のとおりです。
drop table if exists categories;
create table categories(
id integer primary key,
title varchar(255)
);
insert into categories (id, title) values (1, 'Programming');
insert into categories (id, title) values (2, 'Design');
insert into categories (id, title) values (3, 'Marketing');
courses テーブル
courses テーブルには以下の5件のレコードを作成します。
| id | title | learning_time | category_id |
|---|---|---|---|
| 1 | PHP Basic | 30 | 1 |
| 2 | PHP Database | 50 | 1 |
| 3 | Python Basic | 40 | 1 |
| 4 | Web Design | 50 | 2 |
| 5 | History of Japan | 100 |
courses テーブルの作成に必要なSQLは以下のとおりです。
drop table if exists courses;
create table courses(
id integer primary key,
title varchar(255),
learning_time integer,
category_id integer
);
insert into courses (id, title, learning_time, category_id) values (1, 'PHP Basic', 30, 1);
insert into courses (id, title, learning_time, category_id) values (2, 'PHP Database', 50, 1);
insert into courses (id, title, learning_time, category_id) values (3, 'Python Basic', 40, 1);
insert into courses (id, title, learning_time, category_id) values (4, 'Web Design', 50, 2);
insert into courses (id, title, learning_time, category_id) values (5, 'History of Japan', 100, null);
idが5のレコードのcategory_idはnullとしています。nullとは空のデータを意味します。