L_rdbms

510 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
510
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

L_rdbms

  1. 1. Unit L: Relational Data Bases <ul><li>Areas to be covered: </li></ul><ul><li>Databases </li></ul><ul><li>The Relational data model </li></ul><ul><li>Database management software </li></ul><ul><li>SQL </li></ul>
  2. 2. 1. Databases <ul><li>Organizations and individuals keep data describing their assets and activities </li></ul><ul><li>This data is used in </li></ul><ul><ul><li>The conduct of day to day operations </li></ul></ul><ul><ul><li>Planning </li></ul></ul><ul><ul><li>Decision making </li></ul></ul><ul><li>To be accessible, the data must be stored in an organized manner </li></ul>
  3. 3. Data <ul><li>Examples of organized manual storage </li></ul><ul><ul><li>A telephone book : stores names, addresses and phone numbers for individuals and organizations in name sequence so that the telephone number for an individual or organization can be accessed by name </li></ul></ul><ul><ul><li>Employee cards : each card stores information about one employee; the cards are filed in name sequence so that the card for a specific employee can be accessed by name </li></ul></ul><ul><ul><li>Parts cards : each card stores information about one part; the cards are filed in part number sequence so that the card for a specific part can be accessed bu part number </li></ul></ul>
  4. 4. Parts Data
  5. 5. Data Files <ul><li>Each entry on the card is a field </li></ul><ul><li>Each card is a record </li></ul><ul><li>The collection of cards is a data file </li></ul><ul><li>For computer storage and access, we could store the parts cards as a series of records in a data file </li></ul>
  6. 6. Data Files <ul><li>Observe that each record has a field (shown in gray) whose value is unique </li></ul><ul><li>This field is called the primary key field </li></ul><ul><li>In our example, the primary key field is the part number field </li></ul><ul><li>We can access information about a specific part by using its primary key (part number) to find its record in the file </li></ul>
  7. 7. Data Files <ul><li>We could have </li></ul><ul><ul><li>A file of records for library books </li></ul></ul><ul><ul><ul><li>With fields for book number, title & author </li></ul></ul></ul><ul><ul><li>A file of records for parts </li></ul></ul><ul><ul><ul><li>With fields for part number, description, cost & location </li></ul></ul></ul><ul><ul><li>A file of records for customers </li></ul></ul><ul><ul><ul><li>With fields for customer number, name & address </li></ul></ul></ul><ul><ul><li>A file of records for students </li></ul></ul><ul><ul><ul><li>With fields for student number, name, address & phone number </li></ul></ul></ul><ul><ul><li>A file of records for courses </li></ul></ul><ul><ul><ul><li>With fields for course code, name & hours </li></ul></ul></ul>
  8. 8. Data Files <ul><li>We store data files on disk. </li></ul><ul><ul><li>The data file is then available for use by different programs </li></ul></ul><ul><ul><ul><li>The parts file can be used by order entry, purchasing and other programs </li></ul></ul></ul><ul><li>In business applications, we usually store our data files as files of records </li></ul>
  9. 9. Data Files <ul><li>Typically, a data file has relationships with other data files </li></ul><ul><ul><li>When a sales order is placed by a customer to purchase parts </li></ul></ul><ul><ul><ul><li>Sales order, customer and parts files are involved in recording information about the transaction </li></ul></ul></ul><ul><ul><li>When a student registers for a course </li></ul></ul><ul><ul><ul><li>Registration, student, course and professor files are involved in recording information about the transaction </li></ul></ul></ul>
  10. 10. Database <ul><li>A database is simply a collection of related data files </li></ul><ul><ul><li>A sales order database, a student registration database, a human resources database, an accounting database </li></ul></ul><ul><li>But how do we record the relationships among related data files? </li></ul>
  11. 11. 2. The Relational Data Model <ul><li>When students register for courses, we could combine information from the student and course files to produce a registration file, as shown on the following slide </li></ul>
  12. 12. Student Registration Database <ul><li>The registration file, with a primary key composed of student and course numbers, would appear as follows: </li></ul>
  13. 13. Relationships <ul><li>Although each record has a unique primary key, there is a large amount of redundant data (the same information appears in more than one record and file). This means that </li></ul><ul><ul><li>A large amount of data must be stored </li></ul></ul><ul><ul><li>Many files and records must be updated if the value in a single field is changed (say a student address) </li></ul></ul><ul><li>We will examine how a relational database represents the relationships among files to minimize this redundancy </li></ul>
  14. 14. Relationships <ul><li>Every database contains 2 types of information </li></ul><ul><ul><li>Entities , which are things of interest to us and about which we collect data, such as products, employees, suppliers, customers, purchases and sales </li></ul></ul><ul><ul><li>Relationships , which express the real-world associations between entities, for example between products and customers when customers purchase products or between students and courses when students register for courses </li></ul></ul><ul><li>We use entity relationship diagrams to show the relationships among entities </li></ul>
  15. 15. Student Registration Database <ul><li>Let’s look at the relationship between the course and professor files </li></ul><ul><li>Graphically, it can be shown as </li></ul>Courses Professors
  16. 16. Student Registration Database <ul><li>Courses and professors are entities: things about which we collect information </li></ul><ul><li>The lines between entities show their relationships </li></ul><ul><ul><li>Each course is taught by one professor </li></ul></ul><ul><ul><li>Each professor can teach many courses </li></ul></ul><ul><li>This is a one-to-many relationship </li></ul>
  17. 17. Student Registration Database <ul><li>In a relational database, we store this relationship by adding the key field from the one-file (professor file) as a foreign key to the many-file (course file) </li></ul><ul><li>A foreign key is a field in a file that is a primary key in another file </li></ul>
  18. 18. Student Registration Database <ul><li>In a relational database, the relationships among tables are represented by common fields </li></ul><ul><li>In this instance, the common field is professor number </li></ul><ul><li>For example, to find the name of the professor who teaches a specific course, follow the steps on the next slide </li></ul>
  19. 19. Student Registration Database <ul><li>Steps </li></ul><ul><ul><ul><li>Find the record in the course file with that course number </li></ul></ul></ul><ul><ul><ul><li>Get the professor number from that record </li></ul></ul></ul><ul><ul><ul><li>Find the record in the professor file with that professor number </li></ul></ul></ul><ul><ul><ul><li>Get the professor name from that record </li></ul></ul></ul><ul><li>Observe that we have minimized the redundancy of data </li></ul><ul><ul><ul><li>The course and professor descriptive information are each stored in only one file </li></ul></ul></ul>
  20. 20. Student Registration Database <ul><li>Now, let’s include the student file with the course and professor files </li></ul><ul><li>The entity relationship diagram is as follows </li></ul>Students Courses Professors
  21. 21. Student Registration Database <ul><li>We now have three entities: students, courses and professors </li></ul><ul><li>The relationship between the students and courses files is </li></ul><ul><ul><li>Each student can register in many courses </li></ul></ul><ul><ul><li>Each course can contain many students </li></ul></ul><ul><li>This is a many-to-many relationship </li></ul>
  22. 22. Student Registration Database <ul><li>In a relational database, we store this relationship by adding another file with a compound key composed of the key fields of both files </li></ul>
  23. 23. Student Registration Database <ul><li>Remember that in a relational database, the relationships among files are represented by common fields </li></ul><ul><li>To find the names of the students enrolled in a particular course </li></ul><ul><ul><li>Access the registration table and select the records with that course number </li></ul></ul><ul><ul><li>Get the student number from each of these records </li></ul></ul><ul><ul><li>Use each student number to access a record in the student file and get the student name </li></ul></ul>
  24. 24. Student Registration Database <ul><li>Now compare the registration data file with the one developed previously </li></ul><ul><li>We have replaced a complex structure (with lots of redundant data) with a small, stable file (with a minimum of redundant data), thereby </li></ul><ul><ul><li>Reducing the amount of data to be stored </li></ul></ul><ul><ul><li>Simplifying the process of updating data (a student address change requires only that one record in the student file be changed) </li></ul></ul>
  25. 25. Student Registration Database <ul><li>The database then appears as follows </li></ul>
  26. 26. Sales Order Database <ul><li>Let’s look at the relationship between the customer and order files </li></ul><ul><li>The entity relationship diagram is as follows </li></ul>Customers Orders
  27. 27. Sales Orders Database <ul><li>Each customer can place many orders </li></ul><ul><li>Each order is from one customer </li></ul><ul><li>A one-to-many relationship </li></ul>
  28. 28. Sales Order Database <ul><li>Now, let’s look at the relationship between the orders and parts files </li></ul><ul><li>The entity relationship diagram is as follows </li></ul>Customers Orders Parts
  29. 29. Sales Orders Database <ul><li>Each order can contain many parts </li></ul><ul><li>Each part can be in many orders </li></ul><ul><li>A many-to-many relationship </li></ul>
  30. 30. Sales Orders Database <ul><li>The sales order database then appears as follows </li></ul>
  31. 31. Sales Orders Database <ul><li>When a customer orders parts, we could have recorded the information as follows </li></ul>
  32. 32. Sales Orders Database <ul><li>Instead, we replaced this complex structure that is full of redundant data with a number of small stable files, where relationships are implemented with common fields </li></ul>
  33. 33. Databases <ul><li>We store information in databases so that we can </li></ul><ul><ul><li>Use it to record transactions and perform additional processing functions </li></ul></ul><ul><ul><ul><li>Register students and produce transcripts </li></ul></ul></ul><ul><ul><ul><li>Record sales orders and prepare shipping documents </li></ul></ul></ul><ul><ul><li>Analyze it (ask questions), for example to learn more about our customers or competitors or inventory policy. These questions are called queries </li></ul></ul>
  34. 34. Databases <ul><li>For example, some queries of the student registration database would be </li></ul><ul><ul><li>Which courses does a specific professor teach? </li></ul></ul><ul><ul><li>What courses are taught by two specific professors? </li></ul></ul><ul><ul><li>Who teaches a specific course and where is his/her office? </li></ul></ul><ul><ul><li>For a specific student number, in which courses is the student registered and what is his/her name? </li></ul></ul><ul><ul><li>Who are the professors for a specific student? </li></ul></ul><ul><ul><li>Who are the students registered in a specific course? </li></ul></ul>
  35. 35. Relational Data Model <ul><li>For the relational model, data is organized into </li></ul><ul><ul><li>2 dimensional tables called relations (files) </li></ul></ul><ul><ul><li>rows are tuples (records) </li></ul></ul><ul><ul><li>columns are attributes (fields) </li></ul></ul><ul><li>Each table is a small, stable structure that is designed to minimize the redundancy of data in the database </li></ul><ul><li>A primary key is one or more attributes that uniquely identify a record in a data file </li></ul>
  36. 36. Relational Data Model <ul><li>A foreign key is a primary key from a different data file </li></ul><ul><li>Record relationships are stored in the data and records are processed in relation to one another using matches of data values. </li></ul><ul><li>Any piece of information in one table can be related to any piece of information in another table as long as the two tables share a common attribute </li></ul>
  37. 37. Relational Data Model <ul><li>Any data can be retrieved by attribute and table name and any attribute (not just predefined key fields) can be used for retrieval </li></ul><ul><li>Every attribute can be used as a key for ad hoc queries; relationships do not have to be predefined when the database is created </li></ul>
  38. 38. Databases <ul><li>Databases are used in most business applications </li></ul><ul><ul><li>Sales, accounts receivable, inventory </li></ul></ul><ul><ul><li>Purchasing, accounts payable </li></ul></ul><ul><ul><li>Budgeting, payroll, general ledger </li></ul></ul><ul><ul><li>On-line banking </li></ul></ul><ul><ul><li>Mutual fund administration </li></ul></ul><ul><ul><li>Library catalogues </li></ul></ul>
  39. 39. 3. Database Management Software <ul><li>There are 3 types of data models </li></ul><ul><ul><li>Hierarchical </li></ul></ul><ul><ul><li>Network </li></ul></ul><ul><ul><li>Relational </li></ul></ul><ul><li>Each physically structures and records the relationships among records in the database in a different manner </li></ul><ul><li>Whereas, relational uses common fields, hierarchical and network use pointers (fields containing addresses of related records) </li></ul>
  40. 40. Database Management Software <ul><li>However, all maintain data independence </li></ul><ul><ul><ul><li>the format of the database is recorded within the database itself, on disk with the data (and not in the application programs) </li></ul></ul></ul><ul><li>Since many application may process data in the same database, this is important because, </li></ul><ul><ul><ul><li>Each application program does not have to define the format of the database </li></ul></ul></ul><ul><ul><ul><li>When the format of the database is changed, application programs do not have to be changed, for example to accommodate the addition of a new field to a record or a new data file to the database </li></ul></ul></ul>
  41. 41. Databases <ul><li>Building a database is accomplished in several steps using the capabilities of the database management system. </li></ul><ul><ul><li>Define the information to be stored and the relationships between the information </li></ul></ul><ul><ul><li>Build a database schema </li></ul></ul><ul><ul><li>Create an empty database </li></ul></ul><ul><ul><li>Fill (or populate) the database with data </li></ul></ul>
  42. 42. Database Management Software <ul><li>There are 4 classes of users </li></ul><ul><ul><ul><li>A database administrator uses the data definition language supplied by the dbms to define the data types and definitions for the data files in the database He develops a data schema to describe how the information is related and how to interpret such relations </li></ul></ul></ul><ul><ul><ul><li>An application programmer uses the data manipulation language supplied by the dbms to access, update and modify the data in the database </li></ul></ul></ul><ul><ul><ul><li>An end user uses an application program that uses the dbms software to access data for processing </li></ul></ul></ul><ul><ul><ul><li>An advanced user uses a query language to writes queries which are processed by the dbms software to access the database and provide answers to the queries </li></ul></ul></ul>
  43. 43. General organization of a DBMS
  44. 44. Other Capabilities of Database Management Software <ul><li>Efficient access to data </li></ul><ul><ul><li>Databases typically contain millions of records </li></ul></ul><ul><li>Controlled access to data </li></ul><ul><ul><li>Who can see what </li></ul></ul><ul><ul><li>Read and/or write access </li></ul></ul><ul><li>Concurrent access to data </li></ul><ul><ul><li>Simultaneous access by multiple users / applications </li></ul></ul><ul><ul><li>Locking a record during update </li></ul></ul>
  45. 45. Other Capabilities of Database Management Software <ul><li>Support for different user views </li></ul><ul><ul><li>Cross section through a database, focusing on a specific task </li></ul></ul><ul><ul><li>End users will see the data according their individual needs </li></ul></ul><ul><li>Support for database recovery </li></ul><ul><ul><li>Techniques for reconstruction of a database after a failure (disk crash, program error, power outage, fire) </li></ul></ul>
  46. 46. Database Management Software <ul><li>Relational is the most recent model and the one most widely used in the business world </li></ul><ul><ul><li>It is also the most intuitive </li></ul></ul><ul><ul><li>Examples of relational database software include </li></ul></ul><ul><ul><ul><li>Access, Oracle, Progress, Sybase, DB2, Ingres </li></ul></ul></ul>
  47. 47. 4. SQL <ul><li>There are several relational database languages, and one of them, SQL (Structured Query Language) seems the most popular and is reasonably standard </li></ul><ul><li>It is a multi-purpose database language </li></ul><ul><li>Its commands </li></ul><ul><ul><li>define a schema (data definition commands) </li></ul></ul><ul><ul><li>create and maintain a database instance (data manipulation commands) </li></ul></ul><ul><ul><li>retrieve data in organized forms (query commands) </li></ul></ul>
  48. 48. SQL <ul><li>Most systems (Oracle, Ingres, DB2, Sybase) allow you to write applications in an imperative language (often it is C), in which SQL commands are embedded, that is, can be called as procedures </li></ul>
  49. 49. SQL <ul><li>Data Definition Language </li></ul><ul><ul><li>( create table, drop table, alter table ) </li></ul></ul><ul><li>Data Manipulation Language </li></ul><ul><ul><li>( insert , delete , update ) </li></ul></ul><ul><li>Query Language. </li></ul><ul><ul><li>(select) </li></ul></ul>
  50. 50. Data Definition Language <ul><li>Create Table </li></ul><ul><ul><li>To create an empty database </li></ul></ul><ul><ul><li>Create Table Name ( Field Type, Field Type… ) </li></ul></ul><ul><li>Drop Table </li></ul><ul><ul><li>To delete a table </li></ul></ul><ul><ul><li>Drop Table Name </li></ul></ul><ul><li>Alter Table </li></ul><ul><ul><li>To add/remove a field from the database </li></ul></ul><ul><ul><li>Alter table Name add/drop Field Type </li></ul></ul>
  51. 51. Data Manipulation Language <ul><li>Insert </li></ul><ul><ul><li>To add records to the end of a table (append) </li></ul></ul><ul><ul><li>Insert Into Name values ( Field1, Field2 … ) </li></ul></ul><ul><ul><ul><li>Strings have single quotes ‘’ </li></ul></ul></ul><ul><ul><ul><li>Works with the Select statement to add records from another table </li></ul></ul></ul><ul><li>Update </li></ul><ul><ul><li>To modify records in a database </li></ul></ul><ul><ul><li>Update Name set Field = Expression </li></ul></ul><ul><ul><ul><li>Also works with the Where clause in the Select statement </li></ul></ul></ul>
  52. 52. Data Manipulation Language <ul><li>Delete </li></ul><ul><ul><li>To remove records from a table </li></ul></ul><ul><ul><li>Delete * from Name </li></ul></ul><ul><ul><ul><li>Deletes all records </li></ul></ul></ul><ul><ul><ul><li>Might help if someone made a mistake </li></ul></ul></ul><ul><ul><ul><li>Note the Where clause in the Select statement works with Delete </li></ul></ul></ul>
  53. 53. Lab Student Mark Class Course Assignment Room Employee E-R Diagram for Examples
  54. 54. Database for Examples
  55. 55. Database for Examples <ul><li>( Primary keys are shown in grey) </li></ul>
  56. 56. Select <ul><li>Purpose </li></ul><ul><ul><li>To create a subset of records that meet stated criteria </li></ul></ul><ul><ul><li>Includes </li></ul></ul><ul><ul><ul><li>Join – combines tables into a new table </li></ul></ul></ul><ul><ul><ul><li>Project – creates a subset of columns in a table (creates new tables that contain only the fields required) </li></ul></ul></ul><ul><li>Form </li></ul><ul><ul><li>Too much for one slide, we shall build it up </li></ul></ul><ul><ul><li>General Layout is </li></ul></ul><ul><ul><ul><li>Select ???? </li></ul></ul></ul><ul><ul><ul><li>From ???? </li></ul></ul></ul><ul><ul><ul><li>Where ???? </li></ul></ul></ul><ul><ul><ul><li>Order ???? </li></ul></ul></ul>
  57. 57. Multiple Tables <ul><li>Select Field Names from Tables </li></ul><ul><ul><li>Note if there is duplication in a field name, then the more precise name must be given </li></ul></ul><ul><ul><ul><li>Table.Field </li></ul></ul></ul><ul><ul><li>Select Surname, Student.Initials, Empname, Employee.Initials </li></ul></ul><ul><ul><li>From Student, Employee </li></ul></ul><ul><ul><ul><li>Lots of records are created. Each record from the Student and Employee tables is selected </li></ul></ul></ul>
  58. 58. Where <ul><li>Where allows for filtering of records </li></ul><ul><ul><li>Where Surname = ‘Smith’ </li></ul></ul><ul><ul><li>Where Mark > 50 and Course = ‘CSI 1301’ </li></ul></ul><ul><li>Using keys to link tables </li></ul><ul><ul><li>Select Surname, Course, Section </li></ul></ul><ul><ul><li>From Student, Register </li></ul></ul><ul><ul><li>Where Student.Studnum = Register.Studnum </li></ul></ul><ul><li>Note if there is duplication in a field name in two tables, then the more precise name must be given </li></ul><ul><ul><li>Table.Field </li></ul></ul>
  59. 59. Query 2 <ul><li>Symbols and descriptions of 3 hour courses </li></ul>
  60. 60. Query 8 <ul><li>Names of professors who have offices on the 4th floor or above </li></ul><ul><ul><li>room numbers 4000 and higher </li></ul></ul><ul><ul><li>show the name and the room number. </li></ul></ul>
  61. 61. Order By <ul><li>Order By will sort the data </li></ul><ul><ul><li>It will use numbers 1 - first field selected, 2 second, 3 third … </li></ul></ul><ul><ul><ul><li>It will use the field names as well </li></ul></ul></ul><ul><ul><ul><li>Ascending order is the default </li></ul></ul></ul><ul><ul><ul><ul><li>term Asc for Ascending, Desc for Descending </li></ul></ul></ul></ul><ul><ul><li>Select Studnum, Mark </li></ul></ul><ul><ul><ul><li>From Mark </li></ul></ul></ul><ul><ul><ul><li>Order By Studnum </li></ul></ul></ul><ul><ul><li>Select Studnum, Mark </li></ul></ul><ul><ul><ul><li>From Mark </li></ul></ul></ul><ul><ul><ul><li>Order By 1, Desc </li></ul></ul></ul>
  62. 62. Query 9 <ul><li>Redo Query 8, sorted by room numbers </li></ul><ul><ul><li>attribute at position 2 in the final result </li></ul></ul><ul><ul><li>and then by names attribute at position 1 </li></ul></ul>
  63. 63. Query 11 <ul><li>Who tutors the student with number 86030? </li></ul><ul><ul><li>it is S.D. HAMILTON </li></ul></ul><ul><ul><li>note that aliases are used here only as abbreviations </li></ul></ul>
  64. 64. Query 22 <ul><li>Average mark in the CS course </li></ul>
  65. 65. Query 27 <ul><li>Is there a faster query than Query 11? </li></ul><ul><ul><li>This very slow query accesses three tables with 1371648 rows! </li></ul></ul><ul><ul><li>It's better to help the unoptimized SQL by using a temporary table. </li></ul></ul>
  66. 66. Query 27 <ul><li>Create a new table with the two attributes needed for further selection </li></ul>
  67. 67. Query 27 <ul><li>Insert only data about 86030 into the new table </li></ul>
  68. 68. Query 27 <ul><li>Since there are 9 rows in tmp, </li></ul><ul><ul><li>the overall size of the product drops from 1371648 to 13824 </li></ul></ul>
  69. 69. Query 27 <ul><li>Clean up: remove the table </li></ul>
  70. 70. Query 31 <ul><li>Modify the ROOM table by adding a Building description </li></ul>19 More records
  71. 71. Query 32 <ul><li>Create a new course ECON with Lab </li></ul><ul><li>Add a record to the LAB table </li></ul>
  72. 72. Query 32 <ul><li>Add a record to the COURSE table </li></ul>
  73. 73. Query 35 <ul><li>Add 1% to every mark in the table of marks </li></ul><ul><ul><li>the attribute name denotes the old value; the expression after “=” defines the new value </li></ul></ul>

×