1 / 47

Database Management System

Database Management System. Adisak Intana Lecturer. Chapter 2: Relational Database System. Table Contents. Database Model. Relational Database System. Relationship. Relation characteristics. Kinds of key. Table Contents. Database Model. Relational Database System. Relationship.

Télécharger la présentation

Database Management System

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Database Management System Adisak Intana Lecturer Chapter 2: Relational Database System

  2. Table Contents Database Model Relational Database System Relationship Relation characteristics Kinds of key 2

  3. Table Contents Database Model Relational Database System Relationship Relation characteristics Kinds of key 3

  4. Database Model • Database Modelใช้สำหรับอธิบายโครงการสร้างและความสัมพันธ์ระหว่างฐานข้อมูล เพื่อช่วยให้สามารถสร้างฐานข้อมูลได้ง่ายขึ้น • Database Modelแบ่งเป็น 3 ประเภท ดังนี้ • Hierarchical Database Model • Network Database Model • Relational Database Model 4

  5. Hierarchical Database Model • โครงสร้าง มีลักษณะเป็นแบบ ต้นไม้ (Tree) • ส่วนที่อยู่ระดับบนสุดเรียกว่า root ซึ่งเป็น parent • ส่วนที่อยู่ระดับที่ต่ำกว่า เรียกว่า child 5

  6. Hierarchical Database Model 6

  7. Hierarchical Database Model 7

  8. Hierarchical Database Model Root: Painter Level 1: Painter Name Level 2: Picture Name 8

  9. Hierarchical Database Model • ข้อดี • เห็นภาพชัดเจน เข้าใจง่าย • โครงสร้างเห็นได้ชัดเจน ทำให้ข้อมูลเป็นอิสระจากตัวโปรแกรมที่ดึงข้อมูลขึ้นมาใช้งาน • สามารถจัดการกับข้อมูลที่มีความสัมพันธ์แบบ 1:Mได้อย่างมีประสิทธิภาพ 9

  10. Hierarchical Database Model • ข้อเสีย • ความซับซ้อนของข้อมูล จะต้องมีความเข้าใจในลักษณะโครงสร้างทางกายภาพ (Physical data storage characteristics) ของการจัดเก็บข้อมูล • ยากต่อการจัดการโครงสร้าง • ไม่สามารถจัดการกับข้อมูลที่มีความสัมพันธ์แบบ M:M 10

  11. Network Database Model • ความสัมพันธ์ของข้อมูลจะอยุ่ในรูปแบบ Set • ในแต่ละ set จะประกอบไปด้วย 2 ส่วน คือ • Owner (Parent) • Member (child) • ในแต่ละ set จะแทนด้วยความสัมพันธ์ 1:M ระหว่าง owner กับ member 11

  12. Network Database Model 12 **INVOICE (ใบสั่งซื้อสินค้า) **INVLINE (รายละเอียดใบสั่งซื้อสินค้า)

  13. Network Database Model 13

  14. Network Database Model 14

  15. Network Database Model • ข้อดี • สามารถจัดการกับข้อมูลที่มีความสัมพันธ์แบบ M:N • โครงสร้างเห็นได้ชัดเจน ทำให้ข้อมูลเป็นอิสระจากตัวโปรแกรมที่ดึงข้อมูลขึ้นมาใช้งาน 15

  16. Network Database Model • ข้อเสีย • ความซับซ้อนของข้อมูล จะต้องมีความเข้าใจในลักษณะโครงสร้างทางกายภาพ (Physical data storage characteristics) ของการจัดเก็บข้อมูลมากกว่าแบบ Hierarchy Model • ยากต่อการจัดการโครงสร้าง 16

  17. Table Contents Database Model Relational Database Management System Relationship Relation characteristics Kinds of key 17

  18. Relational DBMS (RDBMS) • A type of DBMS that stores data in the form of related two dimensional tables. • The structure of the database is based on the “ Relational data model “ which was introduced by Dr. E.F. Codd in 1970. 18

  19. Relational DBMS (RDBMS) • Logical database is called relational database. • Data in the logical database is represented as a collection of relations( the same terminology used in mathematics ) or tables. 19

  20. Stud-id Name Sex Age Stud_id Subj_id Grade 4702777 Math102 B+ 4702777 Amporn F 18 4702777 Phy101 C 4703666 Narin M 19 4703666 Math102 A 4702888 Sasin M 19 4702888 Phy101 B Subj-id Sjame Credit 4702888 Math101 C Phy101 Physics 3 Math102 Math 4 A Sample of relational database Student Enroll Subject 20

  21. Stud-id Name Sex Age 4702777 Amporn F 18 4703666 Narin M 19 4702888 Sasin M 19 Relational database • Domain of an attribute X is the set of all possible values of X. Student Domain of Sex = {F, M} Age= {x I|0 < x < 60} 21

  22. Attributes Relation name EmpNo Ename Job Sal Comm DeptNo Emp E1 USA SALESMAN 5000 0 D1 E2 PRANEE SALESMAN 5000 1000 D1 E3 SUTAT MANAGER 2000 2000 D2 E4 WATTANA ANALYST 8000 D1 E5 BOON CLERK 4000 D2 tuple atomic value null value A relation in a relational database 22

  23. Relational DBMS (RDBMS) • Logical database is called relational database. • Data in the logical database is represented as a collection of relations( the same terminology used in mathematics ) or tables. 23

  24. Relationship • ในระบบฐานข้อมูล ข้อมูลแต่ละส่วนจะมีความสัมพันธ์เกี่ยวข้องกัน • ทฤษฎีระบบฐานข้อมูลได้แบ่งความสัมพันธ์ของข้อมูลออกเป็น 3 รูปแบบคือ • one-to-one relationship • one-to-many relationship • many-to-many relationship 24

  25. One-to-One Relationship • ความสัมพันธ์แบบ 1:1 เป็นความสัมพันธ์ที่ข้อมูลชุด A แต่ละรายการ มีความสัมพันธ์กับข้อมูลชุด Bเพียงรายการเดียว • ตัวอย่างเช่น อาจารย์กับห้องพักอาจารย์ เป็นความสัมพันธ์แบบ 1:1 เพราะ • อาจารย์แต่ละคนมีห้องพัก 1 ห้อง • ห้องพักแต่ละห้องเป็นของอาจารย์เพียงคนเดียว 25

  26. One-to-One Relationship Teacher 1 1 Room 26

  27. One-to-Many Relationship • ความสัมพันธ์แบบ 1:M เป็นความสัมพันธ์ที่ข้อมูลชุด A แต่ละรายการ มีความสัมพันธ์กับข้อมูลชุด Bมากกว่า 1 รายการ • ตัวอย่างเช่น จิตรกรกับภาพวาด เป็นความสัมพันธ์แบบ 1:M เพราะ • จิตกรแต่ละคนสามารถวาดภาพได้มากกว่า 1 ภาพ • ภาพแต่ละภาพถูกจิตรกรเพียง 1 คนเท่านั้น 27

  28. One-to-Many Relationship Painter 1 M Painting 28

  29. Many-to-Many Relationship • ความสัมพันธ์แบบ M:M เป็นความสัมพันธ์ที่ข้อมูลชุด A แต่ละรายการ มีความสัมพันธ์กับข้อมูลชุด Bมากกว่า 1 รายการ • และในขณะเดียวกัน ข้อมูลชุด Bแต่ละรายการ มีความสัมพันธ์กับข้อมูลชุด Aมากกว่า 1 รายการ • ตัวอย่างเช่น นักศึกษากับวิชาที่ลงทะเบียนเรียน เป็นความสัมพันธ์แบบ M:M เพราะ • นักศึกษาแต่ละคนสามารถลงทะเบียนเรียนได้หลายรายวิชา • รายวิชาแต่ละรายวิชาถูกนักศึกษาลงทะเบียนเรียนได้หลายคน 29

  30. One-to-Many Relationship Student M N Subject 30

  31. Table Contents Database Model Relational Database Management System Relationship Relation characteristics Kinds of key 31

  32. คุณสมบัติของ Relation • ข้อมูลในแต่ละ tuple ต้องไม่ซ้ำกัน 32

  33. คุณสมบัติของ Relation • การเรียงลำดับของ tuple ไม่มีความสำคัญ • การเรียงลำดับของ attribute ไม่มีความสำคัญ • ค่าข้อมูลของแต่ละ attribute ต้องอยู่ใน Domain เดียว • ค่าของข้อมูลในแต่ละ attribute มีเพียงค่าเดียว (Single Value) 33

  34. Table Contents Database Model Relational Database Management System Relationship Relation characteristics Kinds of key 34

  35. Candidate key and primary key • An attribute (or set of attributes) of a relation whose values can be used to identified each tuple in the relation is called a candidate key. • If a relation has only one candidate key, it will be the primary key of that relation. 35

  36. Primary key and secondary key • If there is more than one candidate key in a relation, one candidate key must be selected to be the primary key of that relation and the rest will become the secondary key(s)or alternate key(s). 36

  37. DeptNo Dname Dept D1 MARKETING D2 PERSONNEL D3 COMPUTER Primary key and secondary key • BothDeptNo and Dnameare candidate keys • If DeptNo is chosen to be the primary key, thus Dname must be the secondary key 37

  38. Primary key and secondary key Dname DeptNo Dept D1 MARKETING D2 PERSONNEL D3 COMPUTER • Relation “Dept” structure (intension part) is represented as : Dept(DeptNo, Dname) Secondary key Primary key 38

  39. Primary key • Each relation, there is always an attribute (or a set of attributes) has been set to be theprimary key. • student(std-id, name, sex, age) • subject(sj-id, sjname, credit) • enroll(std-id, sj-id, grade) 39

  40. Primary key • Each tuple of a relation, theattribute value of the primary key (primary key value) can be used to identify that tuple. • Primary key value of each tuple of a relation mustsatisfy the following two time-independent properties • Not null • Unique 40

  41. Stud_id Subj_id Grade 4702777 Math102 B+ 4702777 Phy101 C 4703666 Math102 A 4702888 Phy101 B 4702888 Math101 C Compound primary key • A key which consists of more than one attribute is called a compound key. Enroll Compound key 41

  42. Stud_id Subj_id Grade 4702777 Math102 B+ 4702777 Phy101 C 4703666 Math102 A 4702888 Phy101 B 4702888 Math101 C Compound primary key • In each tuple, the compound value of attributes of the compound keys must be unique and can be used to be the representation of that tuple. Enroll 42

  43. Foreign keys • An attribute (or set of attributes) X of a relation R2 is a foreign keyif it is the primary key of some other relation R1 in the same database. • For all time, each current value of X in the relation R2 must be a current value of X in the relation R1. • R2 is called referencing relation • R1 is called referenced relation 43

  44. Foreign key Emp Dept DeptNois a foreign key of relation Emp 44

  45. Foreign key Emp Dept Each DeptNo value in relation Emp must refer to an existed DeptNo value in relationEmp 45

  46. กฎเกี่ยวกับ Key • The Entity integrity rule – PK จะต้อง unique และ not NULL • The Referential integrity rule – foreign key จะต้องอ้างถึง primary key ของอีก relation ได้ 46

  47. กฎเกี่ยวกับ Key • ข้อกำหนดเกี่ยวกับ foreign key ดังนี้ • FK สามารถเป็น NULL ได้หรือไม่ • ข้อบังคับในการแก้ไข PK ซึ่งเป็น FK ของ relation อื่น • Restrict - ถ้าจะลบ DeptId 01 จะต้องไม่มี StdId อ้างถึง • Cascade - ถ้าจะลบ DeptId 02 จะต้องลบ StdId 4001 ด้วย หรือถ้าจะแก้ไข DeptId 02 เป็น 200 จะ • ต้องแก้ไข StdId 4001 ให้ DeptId เป็น 200 ด้วย • Nullify - ถ้าจะแก้ไขหรือลบ DeptId 02 จะต้องแก้ไข StdId 4001 ให้ DeptId เป็น NULL ก่อน 47

More Related