Relational D Bs3

661 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
661
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
30
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Relational D Bs3

  1. 1. RDBMS Basics Chapter 2: The Relational Database Model (With Considerable Modifications)
  2. 2. RDBMS Basics We already know that a R elational D ata B ase M anagement S ystem ( RDBMS ) is: <ul><li>A DBMS Approach which manages data (logically) as a collection of tables where data, and data relationships, are represented by common values in related tables </li></ul><ul><li>A Table (file) consists of records , which consist of attributes ( fields ), which relies on the storage of various data types </li></ul>
  3. 3. RDBMS Basics  Consider some information the University maintains:  Name  Address  SSN  Major  Courses Taken  Grades Received  Tuition Paid  Tuition Owed  Grants/Scholarships  HOW is this information stored? You are an entity with attributes which vary . Within the University, different areas have different interests in you (i.e., the Registrar, the Bursar, etc.). Nonetheless, you are still part of the University as a whole.  HOW does this relate to a database?
  4. 4. RDBMS Basics You are an entity Record with attributes Fields which vary Fields can contain characters, numbers, symbols, etc. Within the University, different areas , have different interests in you Files (i.e,. The Registrar, Bursar, etc.) Nonetheless, you are still part of the University Database Database Components:
  5. 5. RDBMS Basics Database Components: A Database consists of Files , which contain Records , which contain Fields , which may consist of a variety of data types Jones, Mary 234567890 102 3.87 Notice that there should always be a Key (Unique) Field Hernandez, Juan 123456789 72 2.42 • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
  6. 6. RDBMS Basics RDBMS Restrictions/Conventions:  Each Relation MUST have a unique name NOT Allowed StudentID Name Address Major 123456789 Saenz, Lupe 123 Mesa Finance 234567890 Chung, Mei 37 5 th St. INFOSYS 345678901 Adams, John 54B Hague Accounting 456789012 Elam, Mary 123-22 E St. Accounting •••••• •••••• •••••• •••••• Table Student Student Owed Department 103456678 1,502.36 Marketing 123456789 COBA219 Finance 456789012 COBA232 Accounting •••••• •••••• •••••• Table Balance SID •••••• Depart 987654321 •••••• Finance 876543210 •••••• INFOSYS 765432109 •••••• Accounting •••••• •••••• •••••• Table Student
  7. 7. RDBMS Basics RDBMS Restrictions /Conventions :  Each Relation MUST have a unique name Table Student  All Columns ( Tuples ) MUST have Unique names * NOTE : The same field names CAN be used in different Relations StudentID Name Address Major Address 123456789 Saenz, Lupe 123 Mesa Arlington Finance 234567890 Chung, Mei 37 5 th St. INFOSYS New York 345678901 Adams, John 54B Hague Dallas Accounting 456789012 Elam, Mary 123-22 E St. Ft. Worth INFOSYS One of the names MUST be changed
  8. 8. RDBMS Basics RDBMS Restrictions /Conventions :  Each Relation MUST have a unique name Table Student  All Columns (Tuples) MUST have Unique names Unless this is stored as Character String “24.34” (and NOT as the real number 24.34 ), it MUST be changed  All Column Elements MUST be of the same data type StudentID Name Address Major 123456789 Saenz, Lupe 123 Mesa 24.34 234567890 Chung, Mei 37 5 th St. INFOSYS 345678901 Adams, John 54B Hague Accounting 456789012 Elam, Mary 123-22 E St. INFOSYS
  9. 9. RDBMS Basics RDBMS Restrictions /Conventions :  Each Relation MUST have a unique name Table Student  All Columns (Tuples) MUST have Unique names StudentID Name Address Major 123456789 Saenz, Lupe 123 Mesa Finance  All Column Elements MUST be of the same data type <ul><li>Notice that this means each record requires the SAME number of Bytes of Storage </li></ul>A Character String Requiring 9 Bytes of Storage A Character String Requiring 20 Bytes of Storage A Character String Requiring 25 Bytes of Storage A Character String Requiring 10 Bytes of Storage A Total 64 Bytes of Storage
  10. 10. RDBMS Basics RDBMS Restrictions /Conventions :  Each Relation MUST have a unique name  All Columns (Tuples) MUST have Unique names  All Column Elements MUST be of the same data type  The order of Rows is NOT important Is the Same as StudentID Name Address Majo r 123456789 Saenz, Lupe 123 Mesa Finance 234567890 Chung, Mei 37 5 th St. INFOSYS 345678901 Adams, John 54B Hague Accounting StudentID N ame Address Major 123456789 Saenz, Lupe 123 Mesa Finace 345678901 Adams, John 54B Hague Accounting 234567890 Chung, Mei 37 5 th St. INFOSYS
  11. 11. RDBMS Basics RDBMS Restrictions /Conventions :  Each Relation MUST have a unique name  All Columns (Tuples) MUST have Unique names  All Column Elements MUST be of the same data type  The order of Rows is NOT important Each contains 64 Bytes  The Number of Bytes/Record Must be the same StudentID Name Address Majo r 123456789 Saenz, Lupe 123 Mesa Finance 234567890 Chung, Mei 37 5 th St. INFOSYS 345678901 Adams, John 54B Hague Accounting
  12. 12. RDBMS Basics RDBMS Keys:  Purpose  Define entity relationships  Determination  Knowing the value of a key field means you also know ( Determine ) the values of the other fields  E.g., knowing StudentID means you know StudentName , StudentAddress , etc. StudentID StudentName , StudentAddress (StudentID Determines StudentName and StudentAddress)
  13. 13. RDBMS Basics RDBMS Keys:  Purpose  Define entity relationships  Determination  An attribute is functionally dependent on another if can be determined by that attribute  Functional Dependence StudentID StudentAddress (StudentID Determines Student Address)  NOTE: StudentID StudentAddress (Two Students MAY live at the same address)
  14. 14. RDBMS Basics RDBMS Keys:  Each Relation MUST have a unique identifier or PRIMARY KEY Table Student No two students can have the same StudentID StudentID Name Address Major 123456789 Saenz, Lupe 123 Mesa Finance 234567890 Chung, Mei 37 5 th St. INFOSYS 345678901 Adams, John 54B Hague Accounting 456789012 Elam, Mary 123-22 E St. INFOSYS •••••• •••••• •••••• ••••••
  15. 15. RDBMS Basics RDBMS Keys:  Each Relation MUST have a unique identifier or PRIMARY KEY  A COMPOSITE KEY is a combination of keys (Multi-key attributes) used to produce uniqueness StudentName,StudentAddress StudentMajor (StudentName AND StudentAddress Determines StudentMajor)  NOTE: If Student Major is functionally dependent upon StudentName AND StudentAddress , BUT not on either StudentName or StudentName it is FULLY FUNCTIONAL DEPENDENT on the Concatenated key (Attributes are fully functionally dependent on PRIMARY KEYS )
  16. 16. RDBMS Basics RDBMS Keys:  Each Relation MUST have a unique identifier or PRIMARY KEY  A is COMPOSITE KEY is a combination of keys (Multi-key attributes) used to produce uniqueness  A is SUPER KEY is either a PRIMARY or COMPOSITE KEY that uniquely identifies an entity StudentID StudentAddress AND StudentName,StudentAddress StudentMajor Are BOTH Superkeys
  17. 17. RDBMS Basics RDBMS Keys:  Each Relation MUST have a unique identifier or PRIMARY KEY  A is COMPOSITE KEY is a combination of keys (Multi-key attributes) used to produce uniqueness  A is SUPER KEY is either a PRIMARY or COMPOSITE KEY that uniquely identifies an entity  A is CANDIDATE KEY is any key or group of keys that could become a SUPER KEY <ul><li>StudentID is a candidate key </li></ul><ul><li>StudentName,StudentAddress is a candidate key </li></ul><ul><li>StudentID,StudentAddress is NOT a candidate key </li></ul>(StudentID by itself is a CANDIDATE KEY )
  18. 18. RDBMS Basics RDBMS Keys:  Each Relation MUST have a unique identifier or PRIMARY KEY  A is COMPOSITE KEY is a combination of keys (Multi-key attributes) used to produce uniqueness  A is SUPER KEY is either a PRIMARY or COMPOSITE KEY that uniquely identifies an entity  A is CANDIDATE KEY is any key or group of keys that could become a SUPER KEY  A is SECONDARY KEY is any field, or combination of fields, which does NOT yield a unique value <ul><li>Used for retrieval/Narrowing purposes only </li></ul><ul><li>StudentName,StudentZip may yield several records </li></ul>
  19. 19. RDBMS Basics RDBMS Keys:  Each Relation MUST have a unique identifier or PRIMARY KEY  A is COMPOSITE KEY is a combination of keys (Multi-key attributes) used to produce uniqueness  A is SUPER KEY is either a PRIMARY or COMPOSITE KEY that uniquely identifies an entity  A is CANDIDATE KEY is any key or group of keys that could become a SUPER KEY  A is SECONDARY KEY is any field, or combination of fields, which does NOT yield a unique value <ul><li>Used for retrieval/Narrowing purposes only </li></ul><ul><li>StudentName,StudentZip may yield several records </li></ul>
  20. 20. RDBMS Basics RDBMS Keys:  In Order to relate two (or more) tables FOREIGN KEYS must be used Table Student Table Department A FOREIGN KEY in one table is a PRIMARY/SUPER KEY In another StudentID Name Address Major 123456789 Saenz, Lupe 123 Mesa Finance 234567890 Chung, Mei 37 5 th St. INFOSYS 345678901 Adams, John 54B Hague Accounting DeptName Chairman Telephone INFOSYS Sircar, Sumit 555-1234 Marketing McDaniel, C. 555-2345 Accounting Courtney, H. 555-3456
  21. 21. RDBMS Basics Database Integrity:  Maintaining wholeness and Unity  Entity Integrity:  All Entries MUST be Unique  No NULL values in primary key fields Table Student Illegal Entries StudentID Name Address Major 123456789 Saenz, Lupe 123 Mesa Finance 234567890 Chung, Mei 37 5 th St. INFOSYS 123456789 Adams, John 54B Hague Accounting 456789012 Elam, Mary 123-22 E St. INFOSYS Bush, G.W. 555 Austin Marketing
  22. 22. RDBMS Basics Database Integrity:  Maintaining wholeness and Unity  Entity Integrity  Relational Integrity:  Foreign Key MUST have a valid entry in the corresponding table (or be NULL) Table Student Table Department NOT Allowed StudentID Major 123456789 Scamming 234567890 INFOSYS 345678901 DeptName Chairman Telephone INFOSYS Sircar, Sumit 555-1234 Marketing McDaniel, C. 555-2345 Accounting Courtney, H. 555-3456 ???
  23. 23. RDBMS Basics Database Integrity:  Maintaining wholeness and Unity  Entity Integrity  Relational Integrity:  Foreign Key MUST have a valid entry in the corresponding table (or be NULL) Table Student Table Department  Primary Key entry CAN NOT be deleted if a foreign key refers to it INFOSYS can NOT be deleted DeptName Chairman Telephone INFOSYS Sircar, Sumit 555-1234 Marketing McDaniel, C. 555-2345 StudentID Major 123456789 Accounting 234567890 INFOSYS 345678901 Finance
  24. 24. RDBMS Basics Basic RDBMS Operators:  SELECT  Choose All or Specific values from a table SELECT ALL Yields Given: Table Student StudentID Name Address 123456789 Saenz, Lupe 123 Mesa 234567890 Chung, Mei 37 5 th St. 345678901 Adams, John 54B Hague 456789012 Elam, Mary 123-22 E St. 567890123 Bush, G.W 555 Austin 678901234 Clinton, B. 89 Congress 789012345 Bush, J. 555 Austin StudentID Name Address 123456789 Saenz, Lupe 123 Mesa 234567890 Chung, Mei 37 5 th St. 345678901 Adams, John 54B Hague 456789012 Elam, Mary 123-22 E St. 567890123 Bush, G.W 555 Austin 678901234 Clinton, B. 89 Congress 789012345 Bush, J. 555 Austin
  25. 25. RDBMS Basics Basic RDBMS Operators:  SELECT  Choose All or Specific values from a table SELECT WHERE ADDRESS = “555 Austin” Yields Given: Table Student StudentID Name Address 123456789 Saenz, Lupe 123 Mesa 234567890 Chung, Mei 37 5 th St. 345678901 Adams, John 54B Hague 456789012 Elam, Mary 123-22 E St. 567890123 Bush, G.W 555 Austin 678901234 Clinton, B. 89 Congress 789012345 Bush, J. 555 Austin StudentID Name Address 567890123 Bush, G.W 555 Austin 789012345 Bush, J. Austin
  26. 26. RDBMS Basics Basic RDBMS Operators:  PROJECT  Produce list of all values for a selected attribute PROJECT STUDENTID Yields Given: Table Student StudentID Name Address 123456789 Saenz, Lupe 123 Mesa 234567890 Chung, Mei 37 5 th St. 345678901 Adams, John 54B Hague 456789012 Elam, Mary 123-22 E St. 567890123 Bush, G.W 555 Austin 678901234 Clinton, B. 89 Congress 789012345 Bush, J. 555 Austin StudentID 123456789 234567890 345678901 456789012 567890123 678901234 789012345
  27. 27. RDBMS Basics Basic RDBMS Operators:  PROJECT  Produce list of of all values for a selected attribute PROJECT STUDENTID, NAME Yields Given: Table Student StudentID Name Address 123456789 Saenz, Lupe 123 Mesa 234567890 Chung, Mei 37 5 th St. 345678901 Adams, John 54B Hague 456789012 Elam, Mary 123-22 E St. 567890123 Bush, G.W 555 Austin 678901234 Clinton, B. 89 Congress 789012345 Bush, J. 555 Austin StudentID Name 123456789 Saenz, Lupe 234567890 Chung, Mei 345678901 Adams, John 456789012 Elam, Mary 567890123 Bush, G.W 678901234 Clinton, B. 789012345 Bush, J.
  28. 28. RDBMS Basics Basic RDBMS Operators:  JOIN  Links 2 different tables together (Producing a different table)  Each Table MUST have attributes that are common to both tables 123 CID 124 125 126 127 CN AA BB CC BB DD CA 6 20 12 9 18 CR AA BB CC CAdd 123 A 234 B 345 C TableA TableB
  29. 29. RDBMS Basics Basic RDBMS Operators:  JOIN  Consider the relationship between a Physician and his/her Patients 123456789 PhysID Physician Patient 234567890 345678901 Physname Smith, Mary Brown, John Jones, Jerry PatID Patname PhysID 987654321 Washington, G. 234567890 876543210 765432109 654321098 543210987 Adams, J.Q. Jefferson, T. XXX YYY ZZZ AAA 432109876 321098765 345678901 123456789 123456789 234567890 345678901 123456789
  30. 30. RDBMS Basics Basic RDBMS Operators:  JOIN  The Product of the tables is: Physician Patient = * The product of a 3 (row) x 2 (column) Table and a 6 (row) x 3 (column) Table would be a 18 (row) by 5 (column) Table 123456789 Smith, Mary 234567890 Brown, John 345678901 Jones, Jerry Patname PatID Phys Washington, G. 987654321 234567890 Jefferson, T. 765432109 123456789 XXX 654321098 123456789 YYY 543210987 234567890 ZZZ 432109876 345678901 PhysID Physname PhysID Physname PatID Patname Phys Adams, J.Q. 876543210 345678901 123456789 Smith, Mary 123456789 Smith, Mary Washington, G. 987654321 234567890 Adams, J.Q. 876543210 345678901 123456789 Smith, Mary Jefferson, T. 765432109 123456789 123456789 Smith, Mary XXX 654321098 123456789 123456789 Smith, Mary YYY 543210987 234567890 123456789 Smith, Mary ZZZ 432109876 345678901 234567890 234567890 234567890 234567890 234567890 234567890 Washington, G. 987654321 234567890 Adams, J.Q. 876543210 345678901 Jefferson, T. 765432109 123456789 XXX 654321098 123456789 YYY 543210987 234567890 ZZZ 432109876 345678901 Brown, John Brown, John Brown, John Brown, John Brown, John Brown, John Washington, G. 987654321 234567890 Jefferson, T. 765432109 123456789 XXX 654321098 123456789 YYY 543210987 234567890 ZZZ 432109876 345678901 Adams, J.Q. 876543210 345678901 345678901 345678901 345678901 345678901 345678901 345678901 Washington, G. 987654321 234567890 Adams, J.Q. 876543210 345678901 Jefferson, T. 765432109 123456789 XXX 654321098 123456789 YYY 543210987 234567890 ZZZ 432109876 345678901 Jones, Jerry Jones, Jerry Jones, Jerry Jones, Jerry Jones, Jerry Jones, Jerry Washington, G. 987654321 234567890 Jefferson, T. 765432109 123456789 XXX 654321098 123456789 YYY 543210987 234567890 ZZZ 432109876 345678901 Adams, J.Q. 876543210 345678901
  31. 31. RDBMS Basics Basic RDBMS Operators:  JOIN <ul><li>Now, entering the command: </li></ul><ul><li>Select Patname … Where PhysID = Phys would yield: </li></ul>Smith, Mary Washington, G. 987654321 PhysID Physname PatID Patname Phys Adams, J.Q. 876543210 345678901 123456789 Smith, Mary 123456789 Smith, Mary Washington, G. 987654321 234567890 123456789 Smith, Mary Jefferson, T. 765432109 123456789 123456789 Smith, Mary XXX 654321098 123456789 123456789 Smith, Mary YYY 543210987 234567890 123456789 Smith, Mary ZZZ 432109876 345678901 234567890 234567890 234567890 234567890 234567890 234567890 Washington, G. 987654321 234567890 Adams, J.Q. 876543210 345678901 Jefferson, T. 765432109 123456789 XXX 654321098 123456789 YYY 543210987 234567890 ZZZ 432109876 345678901 Brown, John Brown, John Brown, John Brown, John Brown, John Brown, John 345678901 345678901 345678901 345678901 345678901 345678901 Washington, G. 987654321 234567890 Adams, J.Q. 876543210 345678901 Jefferson, T. 765432109 123456789 XXX 654321098 123456789 YYY 543210987 234567890 ZZZ 432109876 345678901 Jones, Jerry Jones, Jerry Jones, Jerry Jones, Jerry Jones, Jerry Jones, Jerry PhysID Physname PatID Patname Phys Adams, J.Q. 876543210 345678901 123456789 Smith, Mary 123456789 234567890 123456789 Smith, Mary Jefferson, T. 765432109 123456789 123456789 Smith, Mary XXX 654321098 123456789 123456789 Smith, Mary YYY 543210987 234567890 123456789 Smith, Mary ZZZ 432109876 345678901 234567890 234567890 234567890 234567890 234567890 234567890 Washington, G. 987654321 234567890 Adams, J.Q. 876543210 345678901 Jefferson, T. 765432109 123456789 XXX 654321098 123456789 YYY 543210987 234567890 ZZZ 432109876 345678901 Brown, John Brown, John Brown, John Brown, John Brown, John Brown, John 345678901 345678901 345678901 345678901 345678901 345678901 Washington, G. 987654321 234567890 Adams, J.Q. 876543210 345678901 Jefferson, T. 765432109 123456789 XXX 654321098 123456789 YYY 543210987 234567890 ZZZ 432109876 345678901 Jones, Jerry Jones, Jerry Jones, Jerry Jones, Jerry Jones, Jerry Jones, Jerry
  32. 32. RDBMS Basics Basic RDBMS Operators:  JOIN <ul><li>Natural Joins (Also called Inner Joins ) </li></ul><ul><li>ONLY the rows with common values in their join columns are retained. </li></ul>123 CID 124 125 126 127 CN AA BB CC BB DD CA 6 20 12 9 18 TableA CR AA BB CC CAdd 123 A 234 B 345 C TableB TableC CID CN CA CAdd 123 AA 6 123 A 124 BB 20 234 B 125 CC 12 345 C 126 BB 9 234 B 123 AA 6 123 A
  33. 33. RDBMS Basics How???  A Simple PRODUCT of the Tables is First created 123 CID 124 125 126 127 CN AA BB CC BB AA CA 6 20 12 9 18 TableA CR AA BB CC CAdd 123 A 234 B 345 C TableB CN CR CAdd CID CA 123 6 AA 123 A AA 124 20 AA 123 A BB 125 12 AA 123 A CC 126 9 AA 123 A BB 127 18 AA 123 A AA 123 6 BB 234 B AA 124 20 BB 234 B BB 125 12 BB 234 B CC 126 9 BB 234 B BB 127 18 BB 234 B AA 123 6 CC 345 C AA 124 20 CC 345 C BB 125 12 CC 345 C CC 126 9 CC 345 C BB 127 18 CC 345 C AA CR AA Joins CR BB Joins CR CC Joins
  34. 34. RDBMS Basics How???  A Simple PRODUCT of the Tables is First created  A SELECT command where CN = CR is issued CN CR CAdd CID CA 123 6 AA 123 A AA 124 20 AA 123 A BB 125 12 AA 123 A CC 126 9 AA 123 A BB 127 18 AA 123 A AA 123 6 BB 234 B AA 124 20 BB 234 B BB 125 12 BB 234 B CC 126 9 BB 234 B BB 127 18 BB 234 B AA 123 6 CC 345 C AA 124 20 CC 345 C BB 125 12 CC 345 C CC 126 9 CC 345 C BB 127 18 CC 345 C AA TableC CID CN CA CAdd 123 AA 6 123 A 124 BB 20 234 B 125 CC 12 345 C 126 BB 9 234 B
  35. 35. RDBMS Basics ADDITIONAL RDBMS Operators:  UNION (NOT always available)  May use single single table  E.G., Creating a phone directory from 2 separate tables  There are no common values so a join can not be performed  Separate queries will produce separate tables  Duplicate records NOT displayed  Combines the results of two unrelated queries into a single output
  36. 36. RDBMS Basics ADDITIONAL RDBMS Operators:  INTERSECT (NOT always available)  Similar to UNION (Combines the results of two unrelated queries into a single output)  There MUST be matching rows in the tables  E.G., Creating a phone directory of Faculty with offices in BUS who also teach in the Business Bldg.  Some Faculty Have offices in BUS  Some Faculty Teach in the Business Building  Merge those who do both into one listing
  37. 37. RDBMS Basics ADDITIONAL RDBMS Operators:  DIFFERENCE (NOT always available)  Yields all rows in one table NOT found in the other  Produces list of all possible pairings  PRODUCT (Seen Earlier)  DIVIDE
  38. 38. RDBMS Basics RDBMS Software Classifications  Fully Relational  Supports all relational algebra functions  Enforces Entity and Referential Integrity Rules  Relationally Complete  Supports all relational algebra functions  Does NOT support Integrity Rules  Minimally Relational  Supports only SELECT, PROJECT, and JOIN  Tabular  Supports only SELECT, PROJECT, and JOIN  User MUST define all access paths
  39. 39. RDBMS Basics

×