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
12. ER To Relational Mapping
› Step 1 Mapping of Regular Entity type
- สร้างตารางสาหรับแต่ละ Entity
- แปลงแต่ละ tuple เป็น instance
Entity : RLY STATION
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))