of 39

Database – Info Storage and Retrieval

Feb 26, 2016

ReportDownload

Documents

brooklyn

Database – Info Storage and Retrieval. Aim: Understand basics of Info storage and Retrieval; Database Organization; DBMS, Query and Query Processing; Work some simple exercises; Concurrency Issues (in Database) Readings: [SG] --- Ch 13.3 Optional: - PowerPoint PPT Presentation

Lecture Title

Database Info Storage and RetrievalAim: Understand basics of Info storage and Retrieval; Database Organization; DBMS, Query and Query Processing; Work some simple exercises; Concurrency Issues (in Database)Readings: [SG] --- Ch 13.3Optional: Some experiences with MySQL, AccessLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiOutlineWhat is a Database and EvolutionOrganization of DatabasesFoundations of Relational DatabaseDBMS and Query ProcessingConcurrency Issue in Database

LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiWhat is a DatabaseFirst attempt A collection of dataExamples: Employee database Jobs Database LINC Database Inventory Database Recipe Database Database of Hotels Database of Restaurants MP3 DatabaseLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiWhat is a Database (2)Combination of Databases Can do more eg: Employee Database + CIA Database eg: Inventory Database + Recipe Database

Database is A combination of a variety of data collections into a single integrated collection

LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiEvolution of Databases From separate, independent database One Course-DB per NUS dept/faculty (in the 90s) Inherent Problem: incompatability, inconvenience, slow, error proneTo Integrated Database One integrated DB or DB schemaServing the needs of all depts/facultyBetter data compatability, fasters,CF: NUS CORS Online RegistrationCF: IRAS e-filing (Online Tax Submission)LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiDBMS and DBAWith Integrated Database, we need To ensure data consistency Provide services to all deptsDifferent services to diff dept, Different interface To provide different views of the same dataEg: CEO, CFO, Proj Mgr, ProgrammerEg: Dean, Heads, Professors, AOs, Students to decide how to Organize data (schemas)Usually organized into tablesDBMS = DB Management SystemDBA = Database AdministratorLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiOutlineWhat is a Database and EvolutionOrganization of DatabasesFoundations of Relational DatabaseDBMS and Query ProcessingConcurrency Issue in Database

LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiDatabase (with 3 Tables (Relations))SCHEDULE-DBCourseDayHourUIT2201Tue1000UIT2201Tue1100CS1101Wed1300CS1101Wed1400GRADES-DBCourseStud-IDGradeUIT2201U071024AUIT2201U081337CUIT2201U072007BCS1101U072007ASTUDENTS-DBStud-IDNameAddressPhoneU071024Albert Zan23 Sheares Hall4358U081337Betty Yeo89 PGP6177U072007Cathy Xin37 Raffles Hall1388LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiFigure 13.3: Data Organization Hierarchy

Database Organization (Overview)LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiData Organization (A Bottom-Up View)BitA binary digit, (0 or 1)ByteA group of eight (8) bits Stores the binary rep. of a character / small integerA single unit of addressable memoryFieldA group of bytes used to represent a string

LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiData Organization (continued)RecordA collection of related fields

Data FileRelated records are kept in a data file

DatabaseRelated files make up a databaseLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiFigure 13.4: Records and Fields in a Single File

Database Files or Database TableEg: SCHEDULE-DB Table and RecordSCHEDULE-DBCourseDayHourUIT2201Tue1000LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiOutlineWhat is a Database and EvolutionOrganization of DatabasesFoundations of Relational DatabaseDBMS and Query ProcessingConcurrency Issue in Database

LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiDatabase (with 3 Tables (Relations))SCHEDULE-DBCourseDayHourUIT2201Tue1000UIT2201Tue1100CS1101Wed1300CS1101Wed1400GRADES-DBCourseStud-IDGradeUIT2201U071024AUIT2201U081337CUIT2201U072007BCS1101U072007ASTUDENTS-DBStud-IDNameAddressPhoneU071024Albert Zan23 Sheares Hall4358U081337Betty Yeo89 PGP6177U072007Cathy Xin37 Raffles Hall1388LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiFoundations of Relational DBTable (Relation) : information about an entityA set of records (eg: Schedule-DB Table)Record (Tuple): data about an instance of the entityA row in the table; A tuple; Eg: (UIT2201, Tue, 10 AM) Attribute (Fields): category of information/dataColumns in the table (eg: Course, Day, Stud-ID, Grades)Schema: A set of Attributes {Course, Day, Time} SCHEDULE-DB Database: A set of tables (relations) { SCHEDULE-DB, GRADES-DB, STUDENTS-DB }LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiRelational-DB OperationsInsert (SCHEDULE-DB, (CS1102, Thu, 1100))Delete (SCHEDULE-DB, (UIT2201, Tue, 1100))Delete (SCHEDULE-DB, (UIT2201, * , * ))Delete (SCHEDULE-DB, ( *, Tue, * ))Lookup (SCHEDULE-DB, ( * , Wed, * ))SCHEDULE-DBCourseDayHourUIT2201Tue1000UIT2201Tue1100CS1101Wed1300CS1101Wed1400LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiTypical OperationsInsert a new RecordDeleting Records Delete a specific record Delete all records that match the specification XSearching Records Look up all records that match the given specification XDisplay some attributes (projection)Join Operation LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiRelational-DB and Abstract AlgebraFoundation of Relational DB is Relational Algebra (in abstract mathematics)Tables are modelled as Relations (algebra)Specified by schema (conceptual model)Operations on a Tables aremodelled by Relational OperationsTypical OperationsInsert, Delete, Lookup, Project, etc(If interested, read article from course web-site) LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiOutlineWhat is a Database and EvolutionOrganization of DatabasesFoundations of Relational DatabaseDBMS and Query ProcessingConcurrency Issue in Database

LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiDatabase Management SystemsDBMS (Database Mgmt Systems)Software system, maintains the files and dataRelational Database Model (and Design)Database specified via schema (conceptual models)Database Query ProcessingTo query the database (to get information)SQL (Structured Query Language) Specialized query languageRelationships between tablesEstablished via primary keys and foreign keysLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon Wai

Database for Rugs-for-You

LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiQuery Processing with SQLSQL is a DB Query Language Supported by many of the common DBMS Provides easier means to insert/delete records Quite simple to use/learn on your ownSQL Queries (format) SELECT FROM WHERE ;

LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiQuery Processing (simple, using SQL)SELECT ID, LastName, FirstName, PayRateFROM EMPLOYEESWHERE (LastName = KAY);

Output of SQL Query ID LASTNAME FIRSTNAME PAYRATE 116 Kay Janet $16.60 171 Kay John $17.80SQL QueryLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiQuery Processing (simple, using SQL)SELECT ID, LastName, FirstName, HoursWorkedFROM EMPLOYEESWHERE (HOURSWORKED > 200);

SELECT *FROM EMPLOYEESWHERE (PAYRATE > 15.00);LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiIn SQL (a Query Language).Simple SQL Queries

SELECT * FROM SCHEDULE-DB WHERE (DAY=Wed)

SELECT Day, Hour FROM SCHEDULE-DB WHERE (COURSE=UIT2201)

SELECT Course, Hour FROM SCHEDULE-DBSCHEDULE-DBCourseDayHourUIT2201Tue1000UIT2201Tue1100CS1101Wed1300CS1101Wed1400LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiFigure 13.8: Three Tables in the Rugs-For-You Database

Primary Keys and Foreign Keys(Readings: Primary & Foreign Keys, [SG3] Section 13.3)LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiSQL with Multiple RelationsIn SQL, combining two or more tablesthat share common data (via keys)SQL uses a Join operation.SELECT ID, LastName, FirstName, PlanType, DateIssuedFROM EMPLOYEES, INSURANCEPOLICIESWHERE (LastName = Takasano) AND (ID = EmployeeID);

keykeyLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiJoins Operation (of Two Relations)SCHEDULE-DBCourseDayHourUIT2201Tue10 AMUIT2201Tue11 AMCS1101Wed1 PMCS1101Wed2 PMVENUE-DBCourseRoomUIT2201SR5CS1101LT15CourseDayHourRoomUIT2201Tue10 AMSR5UIT2201Tue11 AMSR5CS1101Wed1 PMLT15CS1101Wed2 PMLT15JOIN Operation(SCHEDULE-DB.course = VENUE-DB.course)LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiMore about JOIN operationCheck out animation of Join OpRunning time: O(mn) row operationsJoin is an expensive operation!May produce huge resultant tables;Exercise great care with JOINs(See examples in Tutorial) LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiQP: Declarative vs ProceduralSQL is a declarative languageSQL query declare what you wantDBMS+SQL auto-magically processes query to get the results in an efficient mannerHow does SQL do the job? [not given in query]Procedural Query ProcessingThe how of query processingBased on three basic primitives (from relational-alg)Primitives: e-project, e-select, e-joinSpecified like an algorithm[This is not covered in [SG3]. Read my notesLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiThree basic primitivesT1 e-select from SCHED