Sqlite外部キー

最新バージョンのSqliteは、外部キーを貼れる。

※参考サイトより抜粋:http://www.db.is.kyushu-u.ac.jp/rinkou/sqlite/sqlitedbviewer.html

begin transaction;

create table 科目 (
       科目番号   integer not null,
       科目名   varchar(100) not null,
       単位数   integer not null,
       担当教員名 varchar(100)  not null,
       primary key (科目番号)
);

create table 学生 (
       学生番号   integer not null,
       氏名   varchar(100) not null,
       primary key (学生番号)
);

create table 履修登録 (
       科目番号   integer not null,
       学生番号   integer not null,
       primary key (科目番号,学生番号),
       foreign key (科目番号) references 科目(科目番号),
       foreign key (学生番号) references 学生(学生番号)
);
insert into 科目 values ( 1001, ‘データベース’,    2, ‘X’ );
insert into 科目 values ( 1002, ‘プログラミング’,  2, ‘Y’ );
insert into 学生 values ( 2008001, ‘AA AAA’ );
insert into 学生 values ( 2008002, ‘BB BBBB’ );
insert into 履修登録 values ( 1001, 2008001 );
insert into 履修登録 values ( 1001, 2008002 );
insert into 履修登録 values ( 1002, 2008002 );

commit;

カテゴリー: 未分類   パーマリンク

コメントをどうぞ

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>