วันเสาร์ที่ 31 มีนาคม พ.ศ. 2561

example of normalization


- 1NF ต้องไม่เป็น repeating group หรือมี multiple value attribute
    โดยจากตัวอย่างนี้ Student_Grade_Report ยังมี multiple value attribute อยู่ นั่นคือ Course เพราะ Student 1 คน สามารถมีได้หลาย course ทำให้เป็น repeating group
    วิธีการ - ลบ attribute ที่เป็น multiple value ออก
               - นำไปสร้างตารางใหม่ คือ StudentCourse
               - โดยใน StudentCourse มี primary key เป็น {StudentNo,CourseNo}

- 2NF attribute ควรขึ้นอยู่กับ primary key แบบ fully dependent ทุกตัว
    จากการทำ 1NF มาแล้ว ทำให้ได้ตาราง StudentCourse มา ซึ่งในตารางนี้มีแค่ attribute Grade ที่เป็น fully dependent กับ primary key คือขื้นกับทั้ง StudentNo และ CourseNo แต่ CourseName, InstructorNo, InstructorName, InstructorLocation เป็น partial dependent คือขึ้นอยู่กับ CourseNo อย่างเดียว
    วิธีการ - ลบ attribute ที่เป็น partial dependent ออก
               - นำไปสร้างเป็นตารางใหม่คือ CourseInstructor
               - โดยใน CourseInstructor มี primary key คือ CourseNo

 - 3NF ต้องไม่มี transitive dependency คือ ในแต่ละ attribute ไม่มีการขึ้นต่อกันเป็นทอดๆ
    จากการทำ 2NF มาแล้ว ทำให้ได้ตาราง CourseInstructor ซึ่ง InstructorName, InstructorLocation  ขึ้นอยู่กับ InstructorNo ที่ขึ้นอยู่กับ CourseNo อีกทอดหนึ่ง
    วิธีการ - ลบ attribute ที่เป็น transitive dependency
               - นำไปสร้างเป็นตารางใหม่คือ Instructor
               - โดยตาราง Instructor มี primary key คือ InstructorNo
               - ตรวจสอบว่ายังมี transitive dependency อีกหรือไม่ สรุปคือไม่มีแล้วจึงไม่ต้องทำต่อ


วันอังคารที่ 27 มีนาคม พ.ศ. 2561

Guidline4

- ออกแบบ relation schema ให้มีความสัมพันธ์ที่เหมาะสม ระหว่าง attribute ที่เชื่อมกันด้วย primary key และ foreign key โดยต้องรับประกันได้ว่าจะไม่สร้าง tuple เทียม หรือก็คือหลีกเลี่ยงการจับคู่ attribute ที่ไม่ได้เชื่อมระหว่าง primary key กับ foreign key เพราะอาจจะทำให้เกิด tuple เทียมได้

วันอังคารที่ 20 มีนาคม พ.ศ. 2561

Code database (Postgres)

import psycopg2
import csv
import random

class Grade():

def __init__(self, file_data):
self.file_data = file_data
self.connection = psycopg2.connect("dbname='grade_db' user='postgres' host='localhost' password='fern36612'")
self.cursor = self.connection.cursor()

def create_table(self):
self.cursor.execute("CREATE TABLE IF NOT EXISTS student(number_id TEXT Primary Key, name TEXT, surename TEXT)")
self.connection.commit()
self.cursor.execute("CREATE TABLE IF NOT EXISTS transcript(subject TEXT, credit INTEGER, section INTEGER, grade TEXT, term INTEGER, number_id TEXT, FOREIGN KEY (number_id) REFERENCES student(number_id))")
self.connection.commit()
self.connection.close

def get_data(self):
n = 6900000000001
list_subject = ["010123101   INTRODUCTION TO COMPUTER", "010123102   PROGRAMMING FUNDAMENTALS", "040203111   ENGINEERING MATHEMATICS", "040313005   PHYSICS"]
list_grade = ["A", "B", "C", "D"]
for i in range(1000000):
self.insert_data(n, "Jessada", "Weeradetkumpon")
for j in range(40):
k = random.randint(0,3)
self.insert_grade(list_subject[k],"1", "1", list_grade[k], 1, n)
print(i,": success")
n+=1
self.connection.commit()
self.connection.close

def insert_data(self, number_id, name, surename):
    sql = "INSERT INTO student (number_id, name, surename) VALUES (%s, %s, %s)"
    self.cursor.execute(sql, (number_id, name, surename))

def insert_grade(self, subject, credit, section, grade, term, number_id):
sql = "INSERT INTO transcript (subject, credit, section, grade, term, number_id) VALUES(%s, %s, %s, %s, %s, %s)"
self.cursor.execute(sql, (subject, credit, section, grade, term, number_id))

def show_grade(self):
    self.cursor.execute("SELECT * FROM grade Where number_id = '5801012620097'")
    records = self.cursor.fetchall()
    self.connection.close
    for i in records:
    print(i)

a = Grade('grade.csv')
#a.create_table()
#a.get_data()
#a.show_grade()