SlideShare a Scribd company logo
1 of 38
นาย ภูวพงศ์ ศรีวิจารณ์ 56-010126-2011-1
นาย ธีรวัฒน์ ผ่องสกุล 56-010126-3015-9
นางสาว ธีระนันท์ เย็นไธสง 56-010126-3016-7
Database Systems: Concepts,Design
and Applications, 2nd Edition
by Shio Kumar Singh
ISBN: 9788131760925
Requirements of Railway Reservation System
› เก็บข้อมูลสถานีรถไฟ โดยที่สถานีรถไฟสามารถมีรถไฟได้หลายๆชนิด และ รถไฟแต่ละชนิด
สามารถจอดได้หลายๆสถานี
› เก็บเส้นทางรถไฟ จะขึ้นอยู่กับเส้นทางปลายทางของรถไฟ เช่น เก็บหมายเลขของเส้นทาง
› เก็บชนิดของรถไฟ สามารถเก็บข้อมูลของรถไฟได้หลายชนิด
› เก็บข้อมูลเส้นทางปลายทาง เช่นเก็บหมายเลขของรถไฟ เก็บวันที่รถไฟมาถึง
› เก็บข้อมูลของที่นั่งบนรถไฟ เช่นเก็บหมายเลขของที่นั่ง
› เก็บข้อมูลค่าโดยสาร โดยค่าโดยสารจะมีราคาไม่เท่ากันขึ้นอยู่กับเส้นทางปลายทาง
Notation for ER Diagrams
ER Diagrams of Railway Reservation System
› Entity
1. TRAIN
2. TRAIN TYPE
3. RLY STATION
4. DEST_ROUTE
5. TRAIN ROUTE (Weak Entity)
6. FARE (Weak Entity)
7. SEAT (Weak Entity)
ER Diagrams of Railway Reservation System
› Entity : TRAIN
Attribute
- TRAIN_NO
- TOTAL_NO_OF_SEAT
› Entity : RLY STATION
Attribute
- STATION_ID
- NAME
- CITY
- STATE
ER Diagrams of Railway Reservation System
› Entity : TRAIN TYPE
Attribute
- TRAIN_NAME
- MAX_SEATS
› Entity : SEAT (Weak Entity)
Attribute
- SEAT_NO
ER Diagrams of Railway Reservation System
› Entity : DEST_ROUTE
Attribute
- TRAIN_NO
- WEEK_DAYS
› Entity : FARE (Weak Entity)
Attribute
- FARE_CODE
- AMOUNT
- RESTRICTIONS
ER Diagrams of Railway Reservation System
› Entity : TRAIN ROUTE (Weak Entity)
Attribute
- ROUTE_NO
› Entity : ROUTE INSTANCE
Attribute
- DATE
- NO_OF_AVAL_SEATS
ER To Relational Mapping
ER To Relational Mapping
› Step 1 Mapping of Regular Entity type
- สร้างตารางสาหรับแต่ละ Entity
- แปลงแต่ละ tuple เป็น instance
Entity : RLY STATION
› Entity : TRAIN TYPE
› Entity : TRAIN
› Entity : DEST_ROUTE
› Entity : ROUTE INSTANCE
ER to Relational Mapping
› Step 2: Mapping of Weak Entity Types
-สร้างตารางขึ้นมาใหม่
-ใส่ partial key ของ weak entity
-นา primary key ของ owner มาใส่เป็น foreign key
Entity : TRAIN ROUTE
TRAIN_NO เป็น Primary Key ของ owner(DEST_ROUTE)
ใส่เป็น foreign key
› Entity : FARE
TRAIN_NO เป็น Primary Key ของ owner(DEST_ROUTE)
ใส่เป็น foreign key
› Entity : SEAT
DATE เป็น Primary Key ของ owner(ROUTE INSTANCE) ใส่เป็น foreign key
› Step 3: Mapping of Binary 1:1 Relationship Types
› SKIP To Step 4
› Step 4: Mapping of Binary 1:N Relationship Types
› - เลือกด้านที่เป็น N เอา Primary Key ของด้านที่เป็น 1 มาใส่เป็น foreign key
- แล้วเอา Attribute ของ Entity มาใส่
- หากมี Attribute ของ Relation ก็ใส่ลงไปด้วย
› ENTITY : RLY STATION 1:N TRAIN ROUTE
RLY STATION
TRAIN ROUTE
› ENTITY : TRAIN TYPE 1:N TRAIN
TRAIN TYPE
TRAIN
› Step 5: Mapping of Binary M:N Relationship Types
- สร้างตารางขึ้นมาใหม่
- เอา Primary key ของทั้งสอง Entity มาเป็น foreign key
- นา Attribute ของ Relation มาใส่
ENTITY : RLY STATION M:N TRAIN TYPE
RLY STATION
CAN_STOP
TRAIN TYPE
› Step 5: Mapping of Multivalued attribute
› SKIP
Railway Reservation System
การทดลองและผลการทดลอง
Data type ของข้อมูลแต่ละ Column ใน Table
RLY STATION
- STATION_ID integer
- NAME char(14)
- CITY char(5)
- STATE char(7)
CAN_STOP
- STATION_ID integer
- TRAIN_NAME char(6)
TRAIN_TYPE
-TRAIN_NAME char(6)
- MAX_SEATS integer
TRAIN
- TRAIN_NAME char(6)
- TRAIN_NO integer
- TOTAL_NO_OF_SEAT int(0-50)
TRAIN_ROUTE
- ROUTE_NO integer
- TRAIN_NO integer
- STATION_ID integer
DEST_ROUTE
- TRAIN_NO integer
- WEEK_DAYS date
FARE
- FARE_CODE integer
- TRAIN_NO integer
- AMOUNT int(50,500000)
- RESTRICTIONS char(30)
SEAT
-SEAT_NO integer
-DATE date
-CUST_NAME char(15)
-CUST_PHONE int(10)
ROUTE INSTANCE
-DATE date
-NO_OF_AVAL_SEATS int(0-50)
-TRAIN_NO integer
-ROUTE_NO integer
-STATION_ID integer
-ARR_TIME timestamp
-DEP_TIME timestamp
SQL Statement ที่ใช้สร้างตาราง
› sqlite>create table RLY_STATON(
Station_ID int ,
City varchar(100),
Name varchar(100),
State varchar(100),
primary key(Station_ID) );
› sqlite>create table TRAIN_TYPE (
Train_name varchar(100),
Max_seate int,
primary key(Train_name) );
› sqlite>create table TRAIN(
Train_NO int ,
Total_no_of_seats int,
Train_name varchar(100),
primary key(Train_NO),
foreign key(Train_name)
references
TRAIN_TYPE(Train_name) );
› sqlite>create table CAN_STOP(
Station_ID int,
Train_name varchar(100),
primary key(Station_ID, Train_name),
foreign key(Station_ID)
references RLY_STATON(Station_ID),
foreign key(Train_name)
references TRAIN_TYPE(Train_name) );
› sqlite>create table TRAIN_ROUTE(
Route_NO int,
Station_ID int,
Scheduleder_time varchar(100),
Schedularr_time varchar(100),
Train_NO int,
primary key(Route_NO, Train_NO),
foreign key(Train_NO),
references TRAIN(Train_name),
foreign key(Station_ID),
references RLY_STATON(Station_ID) );
› sqlite>create table DEST_ROUTE(
Train_NO int,
Week_day varchar(100),
primary key(Train_NO),
foreign key(Train_NO)
references TRAIN key(Train_NO) );
› sqlite>create table FARE(
Train_NO int,
Fare_code int,
Restrictions varchar(100),
Amount int,
primary key(Train_NO, Fare_code),
foreign key(Train_NO),
references TRAIN(Train_NO) );
› sqlite>create table SEAT(
Seat_NO int,
Date varchar(100),
Cust_phone int,
Cust_name varchar(100),
primary key(Seat_NO, Date),
foreign key(Date),
references ROUTE_INSTANCE(Date) );
› sqlite>create table ROUTE_INSTANCE (
Date varchar(100),
No_of_aval_seate int,
Train_NO int,
Route_NO int,
Station_ID int,
Arr_time varchar(100),
Dep_time varchar(100),
primary key(Date),
foreign key(Train_NO),
references TRAIN(Train_NO) ,
foreign key(Route_NO),
references TRAIN_ROUTE(Route_NO),
foreign key(Station_ID),
references RLY_STATON(Station_ID)
);
Code ที่ใช้Insert ข้อมูลลงใน Database
import random, string, sqlite3.connector,time
from string import ascii_lowercaser
from random import randint
start_time = time.time()
conn = mysql.connector.connect(user='root',
password='123456',
host='127.0.0.1',
database='train')
c = conn.cursor()
size=[100,150,200,250,300,350,400,450,500]
mm=["01","02","03","04","05","06","07","08","09","10","11","12
"]
re1=["Yes","No"]
re2=["Unlimit","15kg","30kg","45kg"]
i=1
day=1
month=1
year=0
def twochar(num):
if num<10:
cha="0"+str(num)
else:
cha=str(num)
return cha
def fourchar(no):
if(no<10):
out="000"+str(no)
elif(no<100):
out="00"+str(no)
elif(no<1000):
out='0'+str(no)
else:
out=str(no)
return out
def twochar(num):
if num<10:
cha="0"+str(num)
else:
cha=str(num)
return cha
def fourchar(no):
if(no<10):
out="000"+str(no)
elif(no<100):
out="00"+str(no)
elif(no<1000):
out='0'+str(no)
else:
out=str(no)
return out
while i<=1000000:
amount = randint(50,50000)
city = ''.join(random.choice(ascii_lowercase) for i in
range(10))
tname = ''.join(random.choice(ascii_lowercase) for i in
range(15))
rands = randint(0,8)
y = randint(0,9999)
ye = fourchar(y)
ran1 = randint(0,1)
ran2 = randint(0,3)
res = "Smoke:"+re1[ran1]+"|WeightLimit:"+re2[ran2]
m = randint(0,11)
d = randint(1,30)
y1 = randint(0,9999)
m1 = randint(0,11)
d1 = randint(1,30)
h1 = randint(0,23)
i1 = randint(0,59)
s1 = randint(0,59)
y2 = randint(0,9999)
m2 = randint(0,11)
d2 = randint(1,30)
h2 = randint(0,23)
i2 = randint(0,59)
s2 = randint(0,59)
if m==1 and d >= 29 :
d = 28
if m1==1 and d1 >= 29 :
d1 = 28
if m2==1 and d2 >= 29 :
d2 = 28
if day>=29 and month==2:
day=1
month+=1
elif day>30:
day=1
month+=1
if month>12:
month=1
year+=1
arr = fourchar(y1)+"-"+mm[m1]+"-"+twochar(d1)+"
"+twochar(h1)+":"+twochar(i1)+":"+twochar(s1)
dep = fourchar(y2)+"-"+mm[m2]+"-"+twochar(d2)+"
"+twochar(h2)+":"+twochar(i2)+":"+twochar(s2)
weekday = ye+"-"+mm[m]+"-"+twochar(d)
date = fourchar(year)+"-"+twochar(month)+"-"+twochar(day)
fname = ''.join(random.choice(ascii_lowercase) for i in range(8))
lname = ''.join(random.choice(ascii_lowercase) for i in range(8))
aname = fname+" "+lname
oname = ''.join(random.choice(ascii_lowercase) for i in range(8))
pname = ''.join(random.choice(ascii_lowercase) for i in range(8))
state = ''.join(random.choice(ascii_lowercase) for i in range(7))
rand_name = oname + ' ' + pname
phone = ''.join(str(randint(0,9)) for i in range(10))
noaval = randint(0,50)
total = size[rands]-noaval
exec_str1 = 'insert into RLY_STATION values
('+str(i)+',"'+city+'","'+rand_name+'","'+state+'")'
exec_str2 = 'insert into TRAIN_TYPE values
("'+tname+'",'+str(size[rands])+')'
exec_str3 = 'insert into CAN_STOP values
('+str(i)+',"'+tname+'")'
exec_str4 = 'insert into TRAIN values
('+str(i)+','+str(total)+',"'+tname+'")'
exec_str5 = 'insert into TRAIN_ROUTE values
('+str(i)+','+str(i)+',"'+arr+'","'+dep+'",'+str(i)+')'
exec_str6 = 'insert into DEST_ROUTE values
('+str(i)+',"'+weekday+'")'
exec_str7 = 'insert into FARE values
('+str(i)+','+str(i)+',"'+res+'",'+str(amount)+')'
exec_str8 = 'insert into SEAT values
('+str(i)+',"'+date+'","'+phone+'","'+aname+'")'
exec_str9 = 'insert into ROUTE_INSTANCE values
("'+date+'",'+str(noaval)+','+str(i)+','+str(i)+','+str(i)+',"
'+arr+'","'+dep+'")'
c.execute(exec_str1)
c.execute(exec_str2)
c.execute(exec_str3)
c.execute(exec_str4)
c.execute(exec_str5)
c.execute(exec_str6)
c.execute(exec_str7)
c.execute(exec_str8)
c.execute(exec_str9)
conn.commit()
i+=1
day+=1
conn.close()
print('---%s seconds---'%(time.time()-start_time))
File Size
การเปรียบเทียบประสิทธิภาพ
Processor : I3-4030U
CPU 1.90 GHz
RAM 4 GB
Processor : I7-4710HQ
CPU 2.5 GHz
RAM 8 GB
MySQL
select * from fare
where Amount =
2000;
125.019 sec 51.118 sec 2.906 sec
select * from
TRAIN_TYPE
where Max_seat =
200;
738.846 sec 540.465 sec 12.688 sec
› Processor: i3-4030U CPU 1.90 GHz
RAM : 4.00 GB
Query : select * from fare where Amount = 2000;
ครั้งที่ 1
ครั้งที่ 2
› Processor: i7-4710HQ CPU 2.5 GHz
RAM : 8.00 GB
Query : select * from fare where Amount = 2000;
MySQL
› Query : select * from fare where Amount = 2000;
› select * from TRAIN_TYPE where Max_seat = 200;
สรุป
› Database หรือระบบฐานข้อมูลเป็นระบบสาหรับจัดเก็บ
ข้อมูล กลุ่มของข้อมูลที่มีความสัมพันธ์กัน โดยอาศัยการบริหาร
จัดการฐานข้อมูลด้วยระบบ DBMS
› เราใช้ระบบฐานข้อมูลเพื่อแทนระบบเก่าที่ใช้กันอยู่ในยุคก่อน
เช่น ระบบแฟ้มข้อมูลซึ่งการจัดเก็บข้อมูลในลักษณะแฟ้มข้อมูล
นั่นอาจ จะทาให้ข้อมูลชนิดเดียวกัน หรือเรื่องเดียวกันถูกเก็บไว้
หลาย ๆ ทาให้เกิดปัญหาต่าง ๆมากมาย
› ข้อดีของฐานข้อมูล
- ลดความซ้าซ้อนของข้อมูล
- ไม่เกิดความความขัดแย้งกันของข้อมูล
- สามารถใช้ข้อมูลร่วมกันอย่างมีประสิทธิภาพ
- ใช้ระบบรักษาความปลอดภัยเพื่อเพิ่มความปลอดภัยให้กับ
ฐานข้อมูล
- ควบคุมความเป็นมาตรฐานเดียวกันได้
- ข้อมูลเป็นอันหนึ่งอันเดียวกัน ข้อมูลเรื่องเดียวกันต้องมีค่า
เดียวกัน
- ข้อมูลมีความเป็นอิสระ ไม่ขึ้นอยู่กับโปรแกรมที่พัฒนาขึ้น
› ข้อเสียของฐานข้อมูล
- ระบบฐานข้อมูลก่อให้เกิดต้นทุนสูง เช่น ซอฟท์แวร์ที่
ใช้ในการจัดการระบบฐานข้อมูล บุคลากร ต้นทุนในการ
ปฏิบัติงาน และ ฮาร์ดแวร์ เป็นต้น
- การเริ่มใช้ระบบฐานข้อมูล อาจก่อให้เกิดความซับซ้อน
ได้ เช่น การจัดเก็บข้อมูล การออกแบบ การเขียน
โปรแกรม เป็นต้น
- เสี่ยงต่อการหยุดชะงักของระบบ เนื่องจากข้อมูลถูก
จัดเก็บไว้ในลักษณะเป็นศูนย์รวม ความล้มเหลวของการ
ทางานบางส่วนในระบบอาจทาให้ระบบฐานข้อมูลทั้งระบบ
หยุดชะงักได้
THANKS YOU !

More Related Content

Featured

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Presentation Database Group 4

  • 1. นาย ภูวพงศ์ ศรีวิจารณ์ 56-010126-2011-1 นาย ธีรวัฒน์ ผ่องสกุล 56-010126-3015-9 นางสาว ธีระนันท์ เย็นไธสง 56-010126-3016-7
  • 2. Database Systems: Concepts,Design and Applications, 2nd Edition by Shio Kumar Singh ISBN: 9788131760925
  • 3. Requirements of Railway Reservation System › เก็บข้อมูลสถานีรถไฟ โดยที่สถานีรถไฟสามารถมีรถไฟได้หลายๆชนิด และ รถไฟแต่ละชนิด สามารถจอดได้หลายๆสถานี › เก็บเส้นทางรถไฟ จะขึ้นอยู่กับเส้นทางปลายทางของรถไฟ เช่น เก็บหมายเลขของเส้นทาง › เก็บชนิดของรถไฟ สามารถเก็บข้อมูลของรถไฟได้หลายชนิด › เก็บข้อมูลเส้นทางปลายทาง เช่นเก็บหมายเลขของรถไฟ เก็บวันที่รถไฟมาถึง › เก็บข้อมูลของที่นั่งบนรถไฟ เช่นเก็บหมายเลขของที่นั่ง › เก็บข้อมูลค่าโดยสาร โดยค่าโดยสารจะมีราคาไม่เท่ากันขึ้นอยู่กับเส้นทางปลายทาง
  • 4.
  • 5. Notation for ER Diagrams
  • 6. ER Diagrams of Railway Reservation System › Entity 1. TRAIN 2. TRAIN TYPE 3. RLY STATION 4. DEST_ROUTE 5. TRAIN ROUTE (Weak Entity) 6. FARE (Weak Entity) 7. SEAT (Weak Entity)
  • 7. ER Diagrams of Railway Reservation System › Entity : TRAIN Attribute - TRAIN_NO - TOTAL_NO_OF_SEAT › Entity : RLY STATION Attribute - STATION_ID - NAME - CITY - STATE
  • 8. ER Diagrams of Railway Reservation System › Entity : TRAIN TYPE Attribute - TRAIN_NAME - MAX_SEATS › Entity : SEAT (Weak Entity) Attribute - SEAT_NO
  • 9. ER Diagrams of Railway Reservation System › Entity : DEST_ROUTE Attribute - TRAIN_NO - WEEK_DAYS › Entity : FARE (Weak Entity) Attribute - FARE_CODE - AMOUNT - RESTRICTIONS
  • 10. ER Diagrams of Railway Reservation System › Entity : TRAIN ROUTE (Weak Entity) Attribute - ROUTE_NO › Entity : ROUTE INSTANCE Attribute - DATE - NO_OF_AVAL_SEATS
  • 11. ER To Relational Mapping
  • 12. ER To Relational Mapping › Step 1 Mapping of Regular Entity type - สร้างตารางสาหรับแต่ละ Entity - แปลงแต่ละ tuple เป็น instance Entity : RLY STATION
  • 13. › Entity : TRAIN TYPE › Entity : TRAIN
  • 14. › Entity : DEST_ROUTE › Entity : ROUTE INSTANCE
  • 15. ER to Relational Mapping › Step 2: Mapping of Weak Entity Types -สร้างตารางขึ้นมาใหม่ -ใส่ partial key ของ weak entity -นา primary key ของ owner มาใส่เป็น foreign key Entity : TRAIN ROUTE TRAIN_NO เป็น Primary Key ของ owner(DEST_ROUTE) ใส่เป็น foreign key
  • 16. › Entity : FARE TRAIN_NO เป็น Primary Key ของ owner(DEST_ROUTE) ใส่เป็น foreign key › Entity : SEAT DATE เป็น Primary Key ของ owner(ROUTE INSTANCE) ใส่เป็น foreign key
  • 17. › Step 3: Mapping of Binary 1:1 Relationship Types › SKIP To Step 4
  • 18. › Step 4: Mapping of Binary 1:N Relationship Types › - เลือกด้านที่เป็น N เอา Primary Key ของด้านที่เป็น 1 มาใส่เป็น foreign key - แล้วเอา Attribute ของ Entity มาใส่ - หากมี Attribute ของ Relation ก็ใส่ลงไปด้วย › ENTITY : RLY STATION 1:N TRAIN ROUTE RLY STATION TRAIN ROUTE
  • 19. › ENTITY : TRAIN TYPE 1:N TRAIN TRAIN TYPE TRAIN
  • 20. › Step 5: Mapping of Binary M:N Relationship Types - สร้างตารางขึ้นมาใหม่ - เอา Primary key ของทั้งสอง Entity มาเป็น foreign key - นา Attribute ของ Relation มาใส่ ENTITY : RLY STATION M:N TRAIN TYPE RLY STATION CAN_STOP TRAIN TYPE
  • 21. › Step 5: Mapping of Multivalued attribute › SKIP
  • 24. Data type ของข้อมูลแต่ละ Column ใน Table RLY STATION - STATION_ID integer - NAME char(14) - CITY char(5) - STATE char(7) CAN_STOP - STATION_ID integer - TRAIN_NAME char(6) TRAIN_TYPE -TRAIN_NAME char(6) - MAX_SEATS integer TRAIN - TRAIN_NAME char(6) - TRAIN_NO integer - TOTAL_NO_OF_SEAT int(0-50) TRAIN_ROUTE - ROUTE_NO integer - TRAIN_NO integer - STATION_ID integer DEST_ROUTE - TRAIN_NO integer - WEEK_DAYS date FARE - FARE_CODE integer - TRAIN_NO integer - AMOUNT int(50,500000) - RESTRICTIONS char(30) SEAT -SEAT_NO integer -DATE date -CUST_NAME char(15) -CUST_PHONE int(10) ROUTE INSTANCE -DATE date -NO_OF_AVAL_SEATS int(0-50) -TRAIN_NO integer -ROUTE_NO integer -STATION_ID integer -ARR_TIME timestamp -DEP_TIME timestamp
  • 25. SQL Statement ที่ใช้สร้างตาราง › sqlite>create table RLY_STATON( Station_ID int , City varchar(100), Name varchar(100), State varchar(100), primary key(Station_ID) ); › sqlite>create table TRAIN_TYPE ( Train_name varchar(100), Max_seate int, primary key(Train_name) ); › sqlite>create table TRAIN( Train_NO int , Total_no_of_seats int, Train_name varchar(100), primary key(Train_NO), foreign key(Train_name) references TRAIN_TYPE(Train_name) ); › sqlite>create table CAN_STOP( Station_ID int, Train_name varchar(100), primary key(Station_ID, Train_name), foreign key(Station_ID) references RLY_STATON(Station_ID), foreign key(Train_name) references TRAIN_TYPE(Train_name) ); › sqlite>create table TRAIN_ROUTE( Route_NO int, Station_ID int, Scheduleder_time varchar(100), Schedularr_time varchar(100), Train_NO int, primary key(Route_NO, Train_NO), foreign key(Train_NO), references TRAIN(Train_name), foreign key(Station_ID), references RLY_STATON(Station_ID) );
  • 26. › sqlite>create table DEST_ROUTE( Train_NO int, Week_day varchar(100), primary key(Train_NO), foreign key(Train_NO) references TRAIN key(Train_NO) ); › sqlite>create table FARE( Train_NO int, Fare_code int, Restrictions varchar(100), Amount int, primary key(Train_NO, Fare_code), foreign key(Train_NO), references TRAIN(Train_NO) ); › sqlite>create table SEAT( Seat_NO int, Date varchar(100), Cust_phone int, Cust_name varchar(100), primary key(Seat_NO, Date), foreign key(Date), references ROUTE_INSTANCE(Date) ); › sqlite>create table ROUTE_INSTANCE ( Date varchar(100), No_of_aval_seate int, Train_NO int, Route_NO int, Station_ID int, Arr_time varchar(100), Dep_time varchar(100), primary key(Date), foreign key(Train_NO), references TRAIN(Train_NO) , foreign key(Route_NO), references TRAIN_ROUTE(Route_NO), foreign key(Station_ID), references RLY_STATON(Station_ID) );
  • 27. Code ที่ใช้Insert ข้อมูลลงใน Database import random, string, sqlite3.connector,time from string import ascii_lowercaser from random import randint start_time = time.time() conn = mysql.connector.connect(user='root', password='123456', host='127.0.0.1', database='train') c = conn.cursor() size=[100,150,200,250,300,350,400,450,500] mm=["01","02","03","04","05","06","07","08","09","10","11","12 "] re1=["Yes","No"] re2=["Unlimit","15kg","30kg","45kg"] i=1 day=1 month=1 year=0 def twochar(num): if num<10: cha="0"+str(num) else: cha=str(num) return cha def fourchar(no): if(no<10): out="000"+str(no) elif(no<100): out="00"+str(no) elif(no<1000): out='0'+str(no) else: out=str(no) return out
  • 28. def twochar(num): if num<10: cha="0"+str(num) else: cha=str(num) return cha def fourchar(no): if(no<10): out="000"+str(no) elif(no<100): out="00"+str(no) elif(no<1000): out='0'+str(no) else: out=str(no) return out while i<=1000000: amount = randint(50,50000) city = ''.join(random.choice(ascii_lowercase) for i in range(10)) tname = ''.join(random.choice(ascii_lowercase) for i in range(15)) rands = randint(0,8) y = randint(0,9999) ye = fourchar(y) ran1 = randint(0,1) ran2 = randint(0,3) res = "Smoke:"+re1[ran1]+"|WeightLimit:"+re2[ran2] m = randint(0,11) d = randint(1,30) y1 = randint(0,9999) m1 = randint(0,11) d1 = randint(1,30) h1 = randint(0,23) i1 = randint(0,59) s1 = randint(0,59)
  • 29. y2 = randint(0,9999) m2 = randint(0,11) d2 = randint(1,30) h2 = randint(0,23) i2 = randint(0,59) s2 = randint(0,59) if m==1 and d >= 29 : d = 28 if m1==1 and d1 >= 29 : d1 = 28 if m2==1 and d2 >= 29 : d2 = 28 if day>=29 and month==2: day=1 month+=1 elif day>30: day=1 month+=1 if month>12: month=1 year+=1 arr = fourchar(y1)+"-"+mm[m1]+"-"+twochar(d1)+" "+twochar(h1)+":"+twochar(i1)+":"+twochar(s1) dep = fourchar(y2)+"-"+mm[m2]+"-"+twochar(d2)+" "+twochar(h2)+":"+twochar(i2)+":"+twochar(s2) weekday = ye+"-"+mm[m]+"-"+twochar(d) date = fourchar(year)+"-"+twochar(month)+"-"+twochar(day) fname = ''.join(random.choice(ascii_lowercase) for i in range(8)) lname = ''.join(random.choice(ascii_lowercase) for i in range(8)) aname = fname+" "+lname oname = ''.join(random.choice(ascii_lowercase) for i in range(8)) pname = ''.join(random.choice(ascii_lowercase) for i in range(8)) state = ''.join(random.choice(ascii_lowercase) for i in range(7)) rand_name = oname + ' ' + pname phone = ''.join(str(randint(0,9)) for i in range(10)) noaval = randint(0,50) total = size[rands]-noaval
  • 30. exec_str1 = 'insert into RLY_STATION values ('+str(i)+',"'+city+'","'+rand_name+'","'+state+'")' exec_str2 = 'insert into TRAIN_TYPE values ("'+tname+'",'+str(size[rands])+')' exec_str3 = 'insert into CAN_STOP values ('+str(i)+',"'+tname+'")' exec_str4 = 'insert into TRAIN values ('+str(i)+','+str(total)+',"'+tname+'")' exec_str5 = 'insert into TRAIN_ROUTE values ('+str(i)+','+str(i)+',"'+arr+'","'+dep+'",'+str(i)+')' exec_str6 = 'insert into DEST_ROUTE values ('+str(i)+',"'+weekday+'")' exec_str7 = 'insert into FARE values ('+str(i)+','+str(i)+',"'+res+'",'+str(amount)+')' exec_str8 = 'insert into SEAT values ('+str(i)+',"'+date+'","'+phone+'","'+aname+'")' exec_str9 = 'insert into ROUTE_INSTANCE values ("'+date+'",'+str(noaval)+','+str(i)+','+str(i)+','+str(i)+'," '+arr+'","'+dep+'")' c.execute(exec_str1) c.execute(exec_str2) c.execute(exec_str3) c.execute(exec_str4) c.execute(exec_str5) c.execute(exec_str6) c.execute(exec_str7) c.execute(exec_str8) c.execute(exec_str9) conn.commit() i+=1 day+=1 conn.close() print('---%s seconds---'%(time.time()-start_time))
  • 32. การเปรียบเทียบประสิทธิภาพ Processor : I3-4030U CPU 1.90 GHz RAM 4 GB Processor : I7-4710HQ CPU 2.5 GHz RAM 8 GB MySQL select * from fare where Amount = 2000; 125.019 sec 51.118 sec 2.906 sec select * from TRAIN_TYPE where Max_seat = 200; 738.846 sec 540.465 sec 12.688 sec
  • 33. › Processor: i3-4030U CPU 1.90 GHz RAM : 4.00 GB Query : select * from fare where Amount = 2000; ครั้งที่ 1 ครั้งที่ 2
  • 34. › Processor: i7-4710HQ CPU 2.5 GHz RAM : 8.00 GB Query : select * from fare where Amount = 2000;
  • 35. MySQL › Query : select * from fare where Amount = 2000;
  • 36. › select * from TRAIN_TYPE where Max_seat = 200;
  • 37. สรุป › Database หรือระบบฐานข้อมูลเป็นระบบสาหรับจัดเก็บ ข้อมูล กลุ่มของข้อมูลที่มีความสัมพันธ์กัน โดยอาศัยการบริหาร จัดการฐานข้อมูลด้วยระบบ DBMS › เราใช้ระบบฐานข้อมูลเพื่อแทนระบบเก่าที่ใช้กันอยู่ในยุคก่อน เช่น ระบบแฟ้มข้อมูลซึ่งการจัดเก็บข้อมูลในลักษณะแฟ้มข้อมูล นั่นอาจ จะทาให้ข้อมูลชนิดเดียวกัน หรือเรื่องเดียวกันถูกเก็บไว้ หลาย ๆ ทาให้เกิดปัญหาต่าง ๆมากมาย › ข้อดีของฐานข้อมูล - ลดความซ้าซ้อนของข้อมูล - ไม่เกิดความความขัดแย้งกันของข้อมูล - สามารถใช้ข้อมูลร่วมกันอย่างมีประสิทธิภาพ - ใช้ระบบรักษาความปลอดภัยเพื่อเพิ่มความปลอดภัยให้กับ ฐานข้อมูล - ควบคุมความเป็นมาตรฐานเดียวกันได้ - ข้อมูลเป็นอันหนึ่งอันเดียวกัน ข้อมูลเรื่องเดียวกันต้องมีค่า เดียวกัน - ข้อมูลมีความเป็นอิสระ ไม่ขึ้นอยู่กับโปรแกรมที่พัฒนาขึ้น › ข้อเสียของฐานข้อมูล - ระบบฐานข้อมูลก่อให้เกิดต้นทุนสูง เช่น ซอฟท์แวร์ที่ ใช้ในการจัดการระบบฐานข้อมูล บุคลากร ต้นทุนในการ ปฏิบัติงาน และ ฮาร์ดแวร์ เป็นต้น - การเริ่มใช้ระบบฐานข้อมูล อาจก่อให้เกิดความซับซ้อน ได้ เช่น การจัดเก็บข้อมูล การออกแบบ การเขียน โปรแกรม เป็นต้น - เสี่ยงต่อการหยุดชะงักของระบบ เนื่องจากข้อมูลถูก จัดเก็บไว้ในลักษณะเป็นศูนย์รวม ความล้มเหลวของการ ทางานบางส่วนในระบบอาจทาให้ระบบฐานข้อมูลทั้งระบบ หยุดชะงักได้