Introduction to Relational Databases Some Preliminaries What Is a Database? “Organised Collection of Symbols” “To Be Inter
Total Page:16
File Type:pdf, Size:1020Kb
HACD.1: Introduction to Relational Databases 04/10/2010 Introduction to Relational Some Preliminaries Databases The theory taught in this part of the course was originally Hugh Darwen devised by Edgar F. Codd in 1969. His seminal paper (1970) was entitled A Relational Model of Data for Large Shared [email protected] Data Banks. www.dcs.warwick.ac.uk/~hugh We will use a language called Tutorial D for examples and exercises. CS252.HACD: Fundamentals of Relational Databases Section 1: Introduction We will use Rel, an implementation of Tutorial D, for our on- line work. 1 2 “Organised Collection of What Is a Database? Symbols” For example: An organised, machine-readable collection of symbols, to be interpreted as a true StudentIdName CourseId account of some enterprise. S1Anne C1 Machine-updatable, too … S1Anne C2 … so a database is also a collection of variables. S2Boris C1 S3Cindy C3 A database is typically available to a community of users, with possibly varying requirements. The symbols are organised into rows and columns, thus forming a table. One of the rows is different in kind from the others. 3 4 “To Be Interpreted as a True “Collection of Variables” Account” ENROLMENT StudentIdName CourseId For example (from the table just shown): S1Anne C1 S1Anne C2 StudentIdName CourseId S1Anne C1 S2Boris C1 S3 Cindy C3 Perhaps those green symbols, organised as they are with S4Devinder C1 respect to the blue ones, are to be understood to mean: ENROLMENT is a variable. Perhaps the table we saw earlier “Student S1, named Anne, is enrolled on course C1.” was once its value. If so, it (the variable) has been updated since then (the row for S4 has been added). 5 6 CS252: Fundamentals of Relational Databases 1 HACD.1: Introduction to Relational Databases 04/10/2010 What Is a Relational Database? “Relation” not equal to “Table” A database whose symbols are organised into a This table is different from the one we have just seen, collection of relations. Here is a relation, shown in but it represents the same relation: tabular form: Name StudentId CourseId Name CourseIdStudentId C1S4Devinder Anne C1S1 C3S3Cindy Anne C2S1 Anne S1 C1 Boris C1S2 Boris S2 C1 C3CindyS3 C2S1Anne Devinder C1S4 In other words, the relation represented does not Might be the value currently assigned to ENROLMENT, a depend on the order in which we place the rows or the relation variable (“relvar”). columns in the table. 7 8 Anatomy of a Relation What Is a DBMS? Name CourseIdStudentId A piece of software for managing databases Anne C1S1 and providing access to them. A DBMS responds to imperatives (“statements”) attribute name attribute values n-tuple, or tuple. given by application programs, custom-written This is a 3-tuple. or general-purpose, executing on behalf of users. The tuples constitute the body Imperatives are written in the database language of the relation. of the DBMS (e.g., SQL). Heading (a set of attributes) The number of The degree of this heading is 3, tuples in the body Responses include completion codes, messages and which is also the degree of the relation. is the cardinality of results of queries. the relation. 9 10 What Does a DBMS Do? Create and Destroy Variables In response to requests given by application programs: Creation (in Tutorial D): • creates and destroys variables VAR ENROLMENT BASE RELATION • takes note of integrity rules (constraints) { StudentId SID , • takes note of authorisations (who is allowed to do Name CHAR, what, to what) CourseId CID } • updates variables (honouring constraints and KEY { StudentId, CourseId } ; authorisations) • provides results of queries Destruction: • and more DROP VAR ENROLMENT ; Now, how does a relational DBMS do these things? … 11 12 CS252: Fundamentals of Relational Databases 2 HACD.1: Introduction to Relational Databases 04/10/2010 Take Note of Integrity Rules Take Note of Authorisations E.g., can’t have more than 20,000 enrolments altogether. In E.g. (perhaps – but not in Tutorial D): Tutorial D: PERMISSION U9_ENROLMENT FOR User9 CONSTRAINT MAX_ENROLMENTS TO READ ENROLMENT ; COUNT ( ENROLMENT ) <= 20000 ; PERMISSION U8_ENROLMENT FOR User8 And if a constraint ceases to be applicable: TO UPDATE ENROLMENT ; DROP CONSTRAINT MAX_ENROLMENTS ; Permissions sometimes need to be withdrawn: DROP PERMISSION U9_ENROLMENT ; 13 14 Updates Variables Provides Results of Queries E.g.: E.g.: Who is enrolled on course C1? DELETE ENROLMENT WHERE StudentId = (ENROLMENT WHERE CourseId = CID(‘C1’)) SID ( ‘S4’ ) ; { StudentId, Name } UPDATE ENROLMENT WHERE StudentId = The result is another relation! In tabular form: SID ( ‘S1’ ) Name := ‘Ann’ ; StudentId Name INSERT ENROLMENT S1 Anne RELATION { TUPLE { StudentId SID ( ‘S4’ ) , S2 Boris Name ‘Devinder’ , S4 Devinder CourseId CID ( ‘C1’ ) } } ; 15 16 EXERCISE IMPORTANT BIT OF ADMIN Consider this table: Are you: (a) an overseas student visiting for just one year, or A B A (b) doing a degree that is completely outside the CS 1 2 3 department? (In which case you should complete an 4 5 Unusual Option Form, obtainable from your dept secretary) 6 7 8 If so, on exit from this lecture: 9 9 ? 1 2 3 Write your name, ITS userid, and course code on the sheet of paper provided. Give three reasons why it cannot be representing a relation. Otherwise you won’t get access to the software needed for the Worksheets! 17 18 CS252: Fundamentals of Relational Databases 3.