--- title: MySQLの勉強がてら author: kazu634 date: 2008-06-15 url: /2008/06/15/_1019/ wordtwit_post_info: - 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4075;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}' categories: - mysql ---
主キーと外部キーを持つテーブルが複数あるデータベースを作ってみたよ。
create table Users ( userId char(20), userName char(20), password char(20), corpName char(100), corpAddr char(100), corpTel char(20), corpFax char(20), userMail char(50), customerFlg char(5) default 'false', PRIMARY KEY(userId) ); insert into Users ( userId, userName, password, corpName, corpAddr, corpTel, corpFax, userMail ) values ( 'admin', 'Administrator', 'password', 'Hechigo Hall', 'Otani-ward, Tokyo', '03-0000-0000', '03-0000-0001', 'admin@hechigohall.co.jp' ); insert into Users ( userId, userName, password, corpName, corpAddr, corpTel, corpFax, userMail ) values ( 'user01', 'user01', 'pass01', 'Comp001', 'Otani-ward, Tokyo', '03-0000-0001', '03-0000-0010', 'u1@corp1.co.jp' ); insert into Users ( userId, userName, password, corpName, corpAddr, corpTel, corpFax, userMail ) values ( 'user02', 'user02', 'pass02', 'Comp02', 'Otani-ward, Tokyo', '03-0000-0002', '03-0000-0020', 'u2@corp2.co.jp' ); create table Facilities ( faciCode char(10), faciName char(100), location char(100), capacity integer, layout char(100), price integer, primary key(faciCode) ); insert into Facilities ( faciCode, faciName, location, capacity, layout, price ) values ( '01', 'room01', 'Hechigo Hall 1st Building 1F', 20, 'Meeting Room', 50000 ); insert into Facilities ( faciCode, faciName, location, capacity, layout, price ) values ( '02', 'room02', 'Hechigo Hall 1st Building 1F', 20, 'Meeting Room', 50000 ); insert into Facilities ( faciCode, faciName, location, capacity, layout, price ) values ( '03', 'room03', 'Hechigo Hall 1st Building 1F', 40, 'Lecture Room', 100000 ); insert into Facilities ( faciCode, faciName, location, capacity, layout, price ) values ( '04', 'hall01', 'Hechigo Hall 2nd Building 1F', 100, 'Event Hall', 250000 ); insert into Facilities ( faciCode, faciName, location, capacity, layout, price ) values ( '05', 'hall02', 'Hechigo Hall 2nd Building 2F', 200, 'Event Hall', 500000 ); create table Reserves ( reserveNo integer, userId char(20), faciCode char(10), useYear integer, useMonth integer, useDay integer, purpose char(100), charge integer, paymentFlg char(5) default 'false', cancelFlg char(5) default 'false', primary key(reserveNo), foreign key(userId) references Users(userId), foreign key(faciCode) references Facilities(faciCode) ); insert into Reserves ( reserveNo, userId, faciCode, useYear, useMonth, useDay, purpose, charge ) values ( 1, 'user002', '01', 2008, 6, 18, 'Meeting', 50000 ); insert into Reserves ( reserveNo, userId, faciCode, useYear, useMonth, useDay, purpose, charge ) values ( 2, 'user002', '02', 2008, 6, 25, 'Meeting', 50000 ); insert into Reserves ( reserveNo, userId, faciCode, useYear, useMonth, useDay, purpose, charge ) values ( 3, 'user002', '03', 2008, 7, 2, 'Lecture', 100000 );