243 lines
8.6 KiB
Markdown
243 lines
8.6 KiB
Markdown
|
---
|
||
|
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
|
||
|
|
||
|
---
|
||
|
<div class="section">
|
||
|
<p>
|
||
|
主キーと外部キーを持つテーブルが複数あるデータベースを作ってみたよ。
|
||
|
</p>
|
||
|
|
||
|
<pre class="syntax-highlight">
|
||
|
<span class="synStatement">create</span> <span class="synSpecial">table</span> Users (
|
||
|
userId <span class="synType">char</span>(<span class="synConstant">20</span>),
|
||
|
userName <span class="synType">char</span>(<span class="synConstant">20</span>),
|
||
|
password <span class="synType">char</span>(<span class="synConstant">20</span>),
|
||
|
corpName <span class="synType">char</span>(<span class="synConstant">100</span>),
|
||
|
corpAddr <span class="synType">char</span>(<span class="synConstant">100</span>),
|
||
|
corpTel <span class="synType">char</span>(<span class="synConstant">20</span>),
|
||
|
corpFax <span class="synType">char</span>(<span class="synConstant">20</span>),
|
||
|
userMail <span class="synType">char</span>(<span class="synConstant">50</span>),
|
||
|
customerFlg <span class="synType">char</span>(<span class="synConstant">5</span>) <span class="synSpecial">default</span> <span class="synConstant">'false'</span>,
|
||
|
PRIMARY KEY(userId)
|
||
|
);
|
||
|
<span class="synStatement">insert</span> <span class="synSpecial">into</span> Users (
|
||
|
userId,
|
||
|
userName,
|
||
|
password,
|
||
|
corpName,
|
||
|
corpAddr,
|
||
|
corpTel,
|
||
|
corpFax,
|
||
|
userMail
|
||
|
) <span class="synSpecial">values</span> (
|
||
|
<span class="synConstant">'admin'</span>,
|
||
|
<span class="synConstant">'Administrator'</span>,
|
||
|
<span class="synConstant">'password'</span>,
|
||
|
<span class="synConstant">'Hechigo Hall'</span>,
|
||
|
<span class="synConstant">'Otani-ward, Tokyo'</span>,
|
||
|
<span class="synConstant">'03-0000-0000'</span>,
|
||
|
<span class="synConstant">'03-0000-0001'</span>,
|
||
|
<span class="synConstant">'admin@hechigohall.co.jp'</span>
|
||
|
);
|
||
|
<span class="synStatement">insert</span> <span class="synSpecial">into</span> Users (
|
||
|
userId,
|
||
|
userName,
|
||
|
password,
|
||
|
corpName,
|
||
|
corpAddr,
|
||
|
corpTel,
|
||
|
corpFax,
|
||
|
userMail
|
||
|
) <span class="synSpecial">values</span> (
|
||
|
<span class="synConstant">'user01'</span>,
|
||
|
<span class="synConstant">'user01'</span>,
|
||
|
<span class="synConstant">'pass01'</span>,
|
||
|
<span class="synConstant">'Comp001'</span>,
|
||
|
<span class="synConstant">'Otani-ward, Tokyo'</span>,
|
||
|
<span class="synConstant">'03-0000-0001'</span>,
|
||
|
<span class="synConstant">'03-0000-0010'</span>,
|
||
|
<span class="synConstant">'u1@corp1.co.jp'</span>
|
||
|
);
|
||
|
<span class="synStatement">insert</span> <span class="synSpecial">into</span> Users (
|
||
|
userId,
|
||
|
userName,
|
||
|
password,
|
||
|
corpName,
|
||
|
corpAddr,
|
||
|
corpTel,
|
||
|
corpFax,
|
||
|
userMail
|
||
|
) <span class="synSpecial">values</span> (
|
||
|
<span class="synConstant">'user02'</span>,
|
||
|
<span class="synConstant">'user02'</span>,
|
||
|
<span class="synConstant">'pass02'</span>,
|
||
|
<span class="synConstant">'Comp02'</span>,
|
||
|
<span class="synConstant">'Otani-ward, Tokyo'</span>,
|
||
|
<span class="synConstant">'03-0000-0002'</span>,
|
||
|
<span class="synConstant">'03-0000-0020'</span>,
|
||
|
<span class="synConstant">'u2@corp2.co.jp'</span>
|
||
|
);
|
||
|
<span class="synStatement">create</span> <span class="synSpecial">table</span> Facilities (
|
||
|
faciCode <span class="synType">char</span>(<span class="synConstant">10</span>),
|
||
|
faciName <span class="synType">char</span>(<span class="synConstant">100</span>),
|
||
|
location <span class="synType">char</span>(<span class="synConstant">100</span>),
|
||
|
capacity <span class="synType">integer</span>,
|
||
|
layout <span class="synType">char</span>(<span class="synConstant">100</span>),
|
||
|
price <span class="synType">integer</span>,
|
||
|
primary key(faciCode)
|
||
|
);
|
||
|
<span class="synStatement">insert</span> <span class="synSpecial">into</span> Facilities (
|
||
|
faciCode,
|
||
|
faciName,
|
||
|
location,
|
||
|
capacity,
|
||
|
layout,
|
||
|
price
|
||
|
) <span class="synSpecial">values</span> (
|
||
|
<span class="synConstant">'01'</span>,
|
||
|
<span class="synConstant">'room01'</span>,
|
||
|
<span class="synConstant">'Hechigo Hall 1st Building 1F'</span>,
|
||
|
<span class="synConstant">20</span>,
|
||
|
<span class="synConstant">'Meeting Room'</span>,
|
||
|
<span class="synConstant">50000</span>
|
||
|
);
|
||
|
<span class="synStatement">insert</span> <span class="synSpecial">into</span> Facilities (
|
||
|
faciCode,
|
||
|
faciName,
|
||
|
location,
|
||
|
capacity,
|
||
|
layout,
|
||
|
price
|
||
|
) <span class="synSpecial">values</span> (
|
||
|
<span class="synConstant">'02'</span>,
|
||
|
<span class="synConstant">'room02'</span>,
|
||
|
<span class="synConstant">'Hechigo Hall 1st Building 1F'</span>,
|
||
|
<span class="synConstant">20</span>,
|
||
|
<span class="synConstant">'Meeting Room'</span>,
|
||
|
<span class="synConstant">50000</span>
|
||
|
);
|
||
|
<span class="synStatement">insert</span> <span class="synSpecial">into</span> Facilities (
|
||
|
faciCode,
|
||
|
faciName,
|
||
|
location,
|
||
|
capacity,
|
||
|
layout,
|
||
|
price
|
||
|
) <span class="synSpecial">values</span> (
|
||
|
<span class="synConstant">'03'</span>,
|
||
|
<span class="synConstant">'room03'</span>,
|
||
|
<span class="synConstant">'Hechigo Hall 1st Building 1F'</span>,
|
||
|
<span class="synConstant">40</span>,
|
||
|
<span class="synConstant">'Lecture Room'</span>,
|
||
|
<span class="synConstant">100000</span>
|
||
|
);
|
||
|
<span class="synStatement">insert</span> <span class="synSpecial">into</span> Facilities (
|
||
|
faciCode,
|
||
|
faciName,
|
||
|
location,
|
||
|
capacity,
|
||
|
layout,
|
||
|
price
|
||
|
) <span class="synSpecial">values</span> (
|
||
|
<span class="synConstant">'04'</span>,
|
||
|
<span class="synConstant">'hall01'</span>,
|
||
|
<span class="synConstant">'Hechigo Hall 2nd Building 1F'</span>,
|
||
|
<span class="synConstant">100</span>,
|
||
|
<span class="synConstant">'Event Hall'</span>,
|
||
|
<span class="synConstant">250000</span>
|
||
|
);
|
||
|
<span class="synStatement">insert</span> <span class="synSpecial">into</span> Facilities (
|
||
|
faciCode,
|
||
|
faciName,
|
||
|
location,
|
||
|
capacity,
|
||
|
layout, price
|
||
|
) <span class="synSpecial">values</span> (
|
||
|
<span class="synConstant">'05'</span>,
|
||
|
<span class="synConstant">'hall02'</span>,
|
||
|
<span class="synConstant">'Hechigo Hall 2nd Building 2F'</span>,
|
||
|
<span class="synConstant">200</span>,
|
||
|
<span class="synConstant">'Event Hall'</span>,
|
||
|
<span class="synConstant">500000</span>
|
||
|
);
|
||
|
<span class="synStatement">create</span> <span class="synSpecial">table</span> Reserves (
|
||
|
reserveNo <span class="synType">integer</span>,
|
||
|
userId <span class="synType">char</span>(<span class="synConstant">20</span>),
|
||
|
faciCode <span class="synType">char</span>(<span class="synConstant">10</span>),
|
||
|
useYear <span class="synType">integer</span>,
|
||
|
useMonth <span class="synType">integer</span>,
|
||
|
useDay <span class="synType">integer</span>,
|
||
|
purpose <span class="synType">char</span>(<span class="synConstant">100</span>),
|
||
|
charge <span class="synType">integer</span>,
|
||
|
paymentFlg <span class="synType">char</span>(<span class="synConstant">5</span>) <span class="synSpecial">default</span> <span class="synConstant">'false'</span>,
|
||
|
cancelFlg <span class="synType">char</span>(<span class="synConstant">5</span>) <span class="synSpecial">default</span> <span class="synConstant">'false'</span>,
|
||
|
primary key(reserveNo),
|
||
|
foreign key(userId) references Users(userId),
|
||
|
foreign key(faciCode) references Facilities(faciCode)
|
||
|
);
|
||
|
<span class="synStatement">insert</span> <span class="synSpecial">into</span> Reserves (
|
||
|
reserveNo,
|
||
|
userId,
|
||
|
faciCode,
|
||
|
useYear,
|
||
|
useMonth,
|
||
|
useDay,
|
||
|
purpose,
|
||
|
charge
|
||
|
) <span class="synSpecial">values</span> (
|
||
|
<span class="synConstant">1</span>,
|
||
|
<span class="synConstant">'user002'</span>,
|
||
|
<span class="synConstant">'01'</span>,
|
||
|
<span class="synConstant">2008</span>,
|
||
|
<span class="synConstant">6</span>,
|
||
|
<span class="synConstant">18</span>,
|
||
|
<span class="synConstant">'Meeting'</span>,
|
||
|
<span class="synConstant">50000</span>
|
||
|
);
|
||
|
<span class="synStatement">insert</span> <span class="synSpecial">into</span> Reserves (
|
||
|
reserveNo,
|
||
|
userId,
|
||
|
faciCode,
|
||
|
useYear,
|
||
|
useMonth,
|
||
|
useDay,
|
||
|
purpose,
|
||
|
charge
|
||
|
) <span class="synSpecial">values</span> (
|
||
|
<span class="synConstant">2</span>,
|
||
|
<span class="synConstant">'user002'</span>,
|
||
|
<span class="synConstant">'02'</span>,
|
||
|
<span class="synConstant">2008</span>,
|
||
|
<span class="synConstant">6</span>,
|
||
|
<span class="synConstant">25</span>,
|
||
|
<span class="synConstant">'Meeting'</span>,
|
||
|
<span class="synConstant">50000</span>
|
||
|
);
|
||
|
<span class="synStatement">insert</span> <span class="synSpecial">into</span> Reserves (
|
||
|
reserveNo,
|
||
|
userId,
|
||
|
faciCode,
|
||
|
useYear,
|
||
|
useMonth,
|
||
|
useDay,
|
||
|
purpose,
|
||
|
charge
|
||
|
) <span class="synSpecial">values</span> (
|
||
|
<span class="synConstant">3</span>,
|
||
|
<span class="synConstant">'user002'</span>,
|
||
|
<span class="synConstant">'03'</span>,
|
||
|
<span class="synConstant">2008</span>,
|
||
|
<span class="synConstant">7</span>,
|
||
|
<span class="synConstant">2</span>,
|
||
|
<span class="synConstant">'Lecture'</span>,
|
||
|
<span class="synConstant">100000</span>
|
||
|
);
|
||
|
</pre>
|
||
|
</div>
|