Search

Sunday, December 14, 2008

Relational Database Design 5/7

    4. ขั้นตอนการเขียน ER model

      4.1 กำหนด Entity type โดยกำหนดมาจากความต้องการของผู้ใช้ระบบ ว่าจะให้มี Entity สำหรับเก็บข้อมูลอะไรบ้าง

        เอนติตี้(Entity) อาจเรียกว่า file หรือ table
        4.1.1 Strong entity คือเกิดขึ้นด้วยตนเองไม่ขึ้นกับ entity ใด เช่น นักศึกษา หรืออาจารย์ หรือสินค้า เป็นต้น
        4.1.2 Weak entity คือขึ้นโดยอาศัย entity อื่น เช่น เกรดเฉลี่ย ที่มาจากแฟ้มผลการเรียน หรือ แฟ้มลงทะเบียน หรือ แฟ้มสั่งซื้อ เป็นต้น
        :: สิ่งต่าง ๆ ที่ผู้ใช้งานฐานข้อมูลจะต้องยุ่งเกี่ยวด้วย เช่น คน แผนก ประเภท การสั่งซื้อ เป็นต้น
        Entities are the principal data object about which information is to be collected. Entities are usually recognizable concepts, either concrete or abstract, such as person, places, things, or events which have relevance to the database. Some specific examples of entities are EMPLOYEES, PROJECTS, INVOICES. An entity is analogous to a table in the relational model.

      4.2 กำหนดความสัมพันธ์ (Relationship type) ที่เกิดขึ้นระหว่าง entity ในลักษณะของกริยา
        ดีกรีของความสัมพันธ์(Degree of relation) มี 4 แบบ
        4.2.1 Unary relationship คือความสัมพันธ์ภายใน entity เดียวกัน เช่นแต่งงานของพนักงาน แต่ถ้ามีระดับแบบลูกน้อง หัวหน้าจะเรียก Recursive relationship(Unary)
        4.2.2 Binary relationship คือความสัมพันธ์แบบสอง entity
        4.2.3 Ternary relationship คือความสัมพันธ์แบบสาม entity
        4.2.4 Quaternary relationship คือความสัมพันธ์แบบสี่ entity

      4.3 กำหนดแอททริบิวท์ (Attribute) ของแต่ละเอนติตี้
        :: แอททริบิวท์(Attibute) อาจเรียก field หรือ column คือ สิ่งที่ใช้อธิบายคุณสมบัติของเอนติตี้ เช่นคุณสมบัติของคน ก็มี รหัส ชื่อ อายุ เพศ เป็นต้น
        Attributes describe the entity of which they are associated. A particular instance of an attribute is a value. For example, "Jane R. Hathaway" is one value of the attribute Name. The domainof an attribute is the collection of all possible values an attribute can have. The domain of Name is a character string.
        A key is the attribute or set of attributes which allow to identify each unique instance of an entity. So no two instances of an entity have or ever can have the same key value. These keys are called candidate keys. (รหัสนักศึกษา กับรหัสบัตรประชาชน)
        Often we have different attributes or combinations of attributes which can serve as key. One key will be choosen and indicated as main key. It is called the primary key. The other keys are the alternate keys. The primary key is often the shortest possible and is the most unlikely to change.
        The primary key is indicated by underlining the name of attributes which form the primary key. In this case the attribute 'Code' has beed underlined.
        The roof, body and trim of a same car can have different colors. The attribute 'color' is called a multivalued attribute. This type of attribute is represented with a double line. (นักศึกษา 1 คนลงได้หลายวิชา)
        The 'age' attribute, indicated with the slashed line, is a derived attribute. It can be computed from the year and the present date. (เกรดเฉลี่ยของนักศึกษา)

        คำต่าง ๆ ที่ควรทราบ
        - คีย์หลัก (Primary key) :: คีย์หลักประจำแฟ้ม
            คุณสมบัติของคีย์หลัก
          1. ข้อมูลของแอททริบิวท์มีความเป็นหนึ่งเดียว(Uniqueness) กล่าวคือทุก ๆ แถวของตารางจะต้องไม่มีข้อมูลของแอททริบิวท์ที่เป็นคีย์หลักซ้ำกันเลย
          2. ต้องประกอบด้วยจำนวนแอททริบิวท์ที่น้อยที่สุด(Minimality) ที่จะสามารถใช้เจาะจง หรืออ้างอิงถึงแถวใดแถวหนึ่งในรีเลชันได้
        - คีย์คู่แข่ง (Candidate key) :: คีย์ที่สามารถเป็น Primary key ได้ เช่น รหัสบัตรประชาชน หรือรหัสผู้เสียภาษี
        - คีย์สำรอง (Alternate key) :: คีย์ตัวอื่น ๆ ในตารางหลังจากเลือก primary key แล้ว
        - คีย์นอก (Foreign key) :: คีย์ตัวอื่น ๆ ในตารางหลังจากเลือก primary key แล้ว
        - คีย์ร่วม (Composite key) :: สามารถแยกออกไปได้อีก เช่น ที่อยู่
        - Composite attibute :: สามารถแยกออกไปได้อีก เช่น ที่อยู่
        - Atomic attibute :: ไม่สามารถแยกออกไปได้อีก เช่น นามสกุล
        - Multivalued attibute :: อาจมีหลายค่าได้ เช่น สีรถ
        - Derived attibute :: ไม่มีค่าแน่นอนของตน แต่ขึ้นกับค่าอื่น เช่นอายุ ขึ้นกับปีเกิด และปีปัจจุบัน
        - Entity type :: ชื่อของ entity เช่น course (courseno,coursename)
        - Entity instance :: ค่าภายในของ entity เช่น bcom101,Introduction to computer

      4.4 คาร์ดินัลลิตี้ และปาร์ติซิเปชั่นของความสัมพันธ์ (Cardinality and participation of relationship)
        4.4.1 คาร์ดินัลลิตี้ของความสัมพันธ์(Cardinality of Relationship)
          :: แต่ละเอนติตี้มีความสัมพันธ์ต่อกัน โดยมีคำกริยามาเชื่อมระหว่างแต่ละเอนติตี้
        1. One-to-one relationship ความสัมพันธ์แบบ 1 ต่อ 1
          เช่น คนขับกับรถ หรือครูใหญ่กับโรงเรียน เป็นต้น
        2. One-to-many relationship ความสัมพันธ์แบบ 1 ต่อหลายข้อมูล
          เช่น ลูกค้ากับหมายเลขโทรศัพท์มือถือ หรือ อาจารย์ที่ปรึกษากับนักเรียน
        3. Many-to-many relationship ความสัมพันธ์แบบหลายข้อมูล ต่อหลายข้อมูล
          เช่น นักเรียนกับวิชาที่ลงทะเบียน หรือ คนงานกับโครงการสร้างบ้าน

        4.4.2 ปาร์ติซิเปชั่นของความสัมพันธ์ (Participation of relationship)
        :: ความสัมพันธ์ระหว่าง entity
        1. Total หรือ Mandatory participation (ต้องมี จะใช้เส้นคู่)
        2. Partial หรือ Optional participation (เลือกได้ จะใช้เส้นเดียว)

No comments: