* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Chs. 1 + 2
Encyclopedia of World Problems and Human Potential wikipedia , lookup
Serializability wikipedia , lookup
Microsoft Access wikipedia , lookup
Registry of World Record Size Shells wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Oracle Database wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Relational algebra wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Ingres (database) wikipedia , lookup
Functional Database Model wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Concurrency control wikipedia , lookup
Versant Object Database wikipedia , lookup
Clusterpoint wikipedia , lookup
ContactPoint wikipedia , lookup
Chapter 1: Introduction Chapter 2: Intro. To the Relational Model Database System Concepts, 6th Ed. ©Silberschatz, Korth and Sudarshan See www.db-book.com for conditions on re-use Database Management System (DBMS) DBMS is • Collection of interrelated data, a.k.a. database (DB for short) • Set of programs to access the data: • Find individual data items or sets • • Modify • • Data mining: Find patterns! (Ch.20) “Cleaning” … examples Insert + Delete Database System Concepts - 6th Edition 1.2 ©Silberschatz, Korth and Sudarshan 1.1 Examples of DB Applications • Banking: transactions • Airlines: reservations, schedules • Universities: registration, grades • Sales: customers, products, purchases • Online retailers: order tracking, customized recommendations • Manufacturing: production, inventory, orders, supply chain • Human resources: employee records, salaries, tax deductions Database System Concepts - 6th Edition 1.3 ©Silberschatz, Korth and Sudarshan Today’s databases can be very large There are dozens of companies today with DBs in the 100 PB (petabyte) range. The LHC at CERN produces about 25 PB of data every year, for an estimated 300 PB total so far. Hurray for the Higgs boson! Facebook had about 300 PB in 2014, and its users are producing about 84 PB a year. Database System Concepts - 6th Edition 1.4 ©Silberschatz, Korth and Sudarshan Robotic data tape storage library at one of the CERN-LHC Tier 1 centers Source: http://www.quantumdiaries.org/2012/09/18/higgs-hunting-software/ Database System Concepts - 6th Edition 1.5 ©Silberschatz, Korth and Sudarshan Database System Concepts - 6th Edition 1.6 ©Silberschatz, Korth and Sudarshan Source: http://www.digitaltrends.com/computing/amazon-snowmobile-data-migration-cloud-storage-truck/ The University DB example used in our text Stores info about: • Instructors • Students • Departments • Course offerings Application program examples: • Add new students, instructors, and courses • Register students for courses, generate class rosters • Assign grades to students, compute GPAs • Generate transcripts Database System Concepts - 6th Edition 1.7 ©Silberschatz, Korth and Sudarshan Before DBMSs existed, DB applications were built directly on top of file systems The system stores permanent records in various files If a new dept. is created, then a new file is needed, to store info about all the instructors in that dept. Application programs extract records from, and add records to, the appropriate files. Application programs are also needed to enforce the various rules (constraints), e.g. a student cannot take more than 6 courses at the same time. Database System Concepts - 6th Edition 1.8 ©Silberschatz, Korth and Sudarshan Drawbacks of using file systems • Data redundancy and inconsistency Multiple file formats, duplication of information in different files • Difficulty in accessing data Need to write a new program to carry out each new task • Data isolation — multiple files and formats • Integrity problems Integrity constraints (e.g., account balance > 0) become “buried” in program code rather than being stated explicitly Hard to add new constraints or change existing ones Database System Concepts - 6th Edition 1.9 ©Silberschatz, Korth and Sudarshan Drawbacks of using file systems (cont.) • Atomicity of updates • Failures may leave database in an inconsistent state with partial updates carried out • Example: Transfer of funds from one account to another should either complete or not happen at all • Concurrent access by multiple users • Concurrent access needed for performance • Uncontrolled concurrent accesses can lead to inconsistencies Example: Two people reading a balance (say $100) and updating it by withdrawing money (say $50 each) “at the same time”. • Security problems • Hard to provide user access to some, but not all, data Database System Concepts - 6th Edition 1.10 ©Silberschatz, Korth and Sudarshan Drawbacks of using file systems Conclusion • Redundancy • Inconsistency • Difficulty of access • Isolation • Integrity • Atomicity • Concurrency • Security Try to remember 3 of these! DBMSs offer solutions to all the above problems! Database System Concepts - 6th Edition 1.11 ©Silberschatz, Korth and Sudarshan Not in text 2.0 Mathematical Concept of Relation Example relations between the same two sets of objects Source:System http://www.math-only-math.com/worksheet-on-functions-or-mapping.html Database Concepts - 6th Edition 1.12 ©Silberschatz, Korth and Sudarshan Not in text Mathematical Concept of Relation Extreme cases: The empty relation and the total relation a d a d b e b e c f c f Source:System http://www.math-only-math.com/worksheet-on-functions-or-mapping.html Database Concepts - 6th Edition 1.13 ©Silberschatz, Korth and Sudarshan Not in text Mathematical Concept of Relation Food for thought: How many different relations exist between these two sets of elements? a d b e c f Source:System http://www.math-only-math.com/worksheet-on-functions-or-mapping.html Database Concepts - 6th Edition 1.14 ©Silberschatz, Korth and Sudarshan Not in text Mathematical Concept of Relation In a Relational DB, the relation is represented by a table Source:System http://www.math-only-math.com/worksheet-on-functions-or-mapping.html Database Concepts - 6th Edition 1.15 ©Silberschatz, Korth and Sudarshan QUIZ: Draw the tables corresponding to the relations b, c, and d Source:System http://www.math-only-math.com/worksheet-on-functions-or-mapping.html Database Concepts - 6th Edition 1.16 ©Silberschatz, Korth and Sudarshan 2.1 Relations and Tables attributes (or columns) tuples (or rows) These are called 4-tuples Database System Concepts - 6th Edition 1.17 ©Silberschatz, Korth and Sudarshan QUIZ The IDs have 5 digits, the names are strings of 20 characters, the dept. names are chosen from a set of 50, and the salaries are integers with 6 digits. How many different tuples can there be? Database System Concepts - 6th Edition 1.18 ©Silberschatz, Korth and Sudarshan Attribute Types The set of allowed values for each attribute is called the domain of the attribute. Attribute values are (normally) required to be atomic; that is, indivisible. The special value null is a member of every domain. The null value causes complications in the definition of many operations. Database System Concepts - 6th Edition 1.19 ©Silberschatz, Korth and Sudarshan 2.2 Relation Schema and Instance A1, A2, …, An are attributes R = (A1, A2, …, An ) is a relation schema Example: instructor = (ID, name, dept_name, salary) Formally, given sets D1, D2, …. Dn a relation r is a subset of the Cartesian product D1 x D2 x … x Dn Thus, a relation is a set of n-tuples (a1, a2, …, an) where each ai Di The current values (relation instance) of a relation are specified by a table An element t of r is a tuple, represented by a row in a table Database System Concepts - 6th Edition 1.20 ©Silberschatz, Korth and Sudarshan Relations are Unordered! The order of tuples is irrelevant (so tuples may be stored in arbitrary order!) Example: instructor relation with unordered tuples Database System Concepts - 6th Edition 1.21 ©Silberschatz, Korth and Sudarshan We have covered the following textbook sections: 1.1, 1.2, 2.1, 2.2 See you at 2:40 in the lab! SCIENCE 206 EOL 1 Database System Concepts - 6th Edition 1.22 ©Silberschatz, Korth and Sudarshan QUIZ What is the difference between a Database (DB) and a Database Management System (DBMS)? How was data stored and managed before the introduction of DB technology? Briefly outline three problems with the method above. Database System Concepts - 6th Edition 1.23 ©Silberschatz, Korth and Sudarshan QUIZ What is the difference between a relation and a table? Explain the following DB concepts: Domain Atomic Null Database System Concepts - 6th Edition 1.24 ©Silberschatz, Korth and Sudarshan QUIZ Are these different relations/tables? No, b/c relations are by definition unordered! Database System Concepts - 6th Edition 1.25 ©Silberschatz, Korth and Sudarshan 1.3 View of Data We use abstraction to manage any complex system. Catalog, tables, functions, … Files, hard-disk blocks … Database System Concepts - 6th Edition 1.26 ©Silberschatz, Korth and Sudarshan Who uses these levels? DB user (e.g. Registrar’s office clerk) DB programmer (e.g. the person who loads data into the DB) DB Admin straddles the levels DBMS developer (e.g. Oracle, Teradata) Database System Concepts - 6th Edition 1.27 ©Silberschatz, Korth and Sudarshan Instances and Schemas Schema – the logical structure of the database Example: The database consists of information about a set of customers and accounts and the relationship between them Analogous to the type of a variable in programming (e.g. array of characters) Physical schema: database design at the physical level Logical schema: database design at the logical level Instance – the actual content of the database at a particular point in time Analogous to the value of a variable (e.g. “Hello, world!”) Database System Concepts - 6th Edition 1.28 ©Silberschatz, Korth and Sudarshan Physical Data Independence = the ability to modify the physical schema without changing the logical schema • This is an example of modularity! • Applications depend on the logical schema • In general, the interfaces between the various levels and components should be well defined so that changes in some parts do not seriously influence others. Database System Concepts - 6th Edition 1.29 ©Silberschatz, Korth and Sudarshan Data model = collection of conceptual tools for describing • • • Data Data relationships Data semantics (e.g. constraints) • Relational • Entity-Relationship (E-R) model (mainly for DB design) • Object-based: encapsulation, methods, inheritance (Object- oriented and Object-relational) • Semistructured: a “type” does not necessarily mean the same set of attributes! (XML) • Other older models: • • Network Hierarchical Database System Concepts - 6th Edition 1.30 ©Silberschatz, Korth and Sudarshan 1.4 and 1.5 DB Languages Data Manipulation Language (DML) • For accessing and manipulating the data • A.k.a. query language • Two classes of languages • Procedural – user specifies what data is required and how to get those data • Declarative (nonprocedural!) – user specifies what is required without specifying how to do it • SQL is the most widely used query language … and declarative. Database System Concepts - 6th Edition 1.31 ©Silberschatz, Korth and Sudarshan SQL example: Find the name of the instructor with ID 22222 select from where Database System Concepts - 6th Edition name instructor instructor.ID = ‘22222’ 1.32 ©Silberschatz, Korth and Sudarshan SQL example: Find the ID and building of instructors in the Physics dept. select instructor.ID, department.building from instructor, department where instructor.dept_name = department.dept_name and department.dept_name = ‘Physics’ Database System Concepts - 6th Edition 1.33 ©Silberschatz, Korth and Sudarshan SQL Queries in a nutshell: select … from … Database System Concepts - 6th Edition 1.34 where… ©Silberschatz, Korth and Sudarshan QUIZ: SQL Write a query to find the building where the instructor with ID 10101 works Database System Concepts - 6th Edition 1.35 ©Silberschatz, Korth and Sudarshan QUIZ: SQL Write a query to find the building where the instructor with ID 10101 works select department.building from instructor, department where instructor.dept_name = department.dept_name and instructor.ID = 10101 Database System Concepts - 6th Edition 1.36 ©Silberschatz, Korth and Sudarshan QUIZ: SQL Find the name of all instructors in the Comp. Sci. dept. Database System Concepts - 6th Edition 1.37 ©Silberschatz, Korth and Sudarshan QUIZ: SQL Find the name of all instructors in the Comp. Sci. dept. who make more than $80,000 Database System Concepts - 6th Edition 1.38 ©Silberschatz, Korth and Sudarshan QUIZ: SQL Find the name of instructors who work in a dept. with a budget over $ 100,000 Database System Concepts - 6th Edition 1.39 ©Silberschatz, Korth and Sudarshan SQL Application programs generally access databases through one of Language extensions to allow embedded SQL Application program interface (e.g., ODBC/JDBC) which allow SQL queries to be sent to a database (Chapters 3, 4 and 5) Database System Concepts - 6th Edition 1.40 ©Silberschatz, Korth and Sudarshan Data Definition Language (DDL) Specification notation for defining the database schema Example: create table instructor ( ID char(5), name varchar(20), dept_name varchar(20), salary numeric(8,2)) DDL compiler generates a set of table templates stored in a data dictionary Data dictionary contains metadata (i.e., data about data) Database schema Integrity constraints Primary key (ID uniquely identifies instructors) Referential integrity (references constraint in SQL) – e.g. dept_name value in any instructor tuple must appear in department relation Authorization Database System Concepts - 6th Edition EoL 1.41 ©Silberschatz, Korth and Sudarshan QUIZ: SQL Write SQL DDL code to create a table named account, with two columns: Account number, which is a string of 10 characters Balance, which is an integer Database System Concepts - 6th Edition 1.42 ©Silberschatz, Korth and Sudarshan QUIZ: SQL Write SQL DDL code to create a table named account, with two columns: Account number, which is a string of 10 characters Balance, which is an integer create table account (account_number char(10), balance integer) Database System Concepts - 6th Edition 1.43 ©Silberschatz, Korth and Sudarshan 1.6.4 Database design • Database = multiple relations • Information about an enterprise is broken up into parts (relations) instructor, department, student, advisor, etc. • Bad design: place all information in one huge table, e.g. Database System Concepts - 6th Edition 1.44 ©Silberschatz, Korth and Sudarshan Database design • Bad design: all information in one huge table results in • repetition of information (e.g., two students have the same instructor) • the need for null values (e.g., represent an student with no advisor) • problems when updating the table • Normalization theory (Chapter 7) deals with how to design “good” relations. Database System Concepts - 6th Edition 1.45 ©Silberschatz, Korth and Sudarshan Lets’ break the big table, then! Do you see a problem with this DB? Database System Concepts - 6th Edition 1.46 ©Silberschatz, Korth and Sudarshan The correct solution preserves the connections in the data! Database System Concepts - 6th Edition 1.47 ©Silberschatz, Korth and Sudarshan 2.3 Keys Remember: • Relations are sets of tuples (tables are sets of rows). • Mathematical sets cannot, by definition, have duplicates. • Therefore, no two tuples in a relation can have exactly the same attributes. Database System Concepts - 6th Edition 1.48 ©Silberschatz, Korth and Sudarshan QUIZ Keys Is this a valid table? Database System Concepts - 6th Edition 1.49 ©Silberschatz, Korth and Sudarshan 2.3 Keys No two tuples in a relation can have exactly the same attributes. If we look at all columns of the table, the combinations of values in them are unique, but … Question: Can we find a smaller set of attributes whose combination is unique for the given relation/table? Database System Concepts - 6th Edition 1.50 ©Silberschatz, Korth and Sudarshan QUIZ Keys What is the smallest key for this table? Database System Concepts - 6th Edition 1.51 ©Silberschatz, Korth and Sudarshan QUIZ Keys What is the smallest key for this table? Database System Concepts - 6th Edition A B C A1 A2 A2 A3 A4 A5 B1 B1 B1 B2 B2 B3 C1 C2 C3 C2 C1 C4 1.52 ©Silberschatz, Korth and Sudarshan Mathematical Notations for Keys K is a subset of the attributes of R: KR K is a superkey of R if values for K are sufficient to identify a unique tuple of each possible relation r(R) Example: {ID} and {ID,name} are both superkeys of instructor. Superkey K is a candidate key if K is minimal Example: {ID} is a candidate key for Instructor One of the candidate keys is selected to be the primary key. Which one? Depends on application. Database System Concepts - 6th Edition 1.53 ©Silberschatz, Korth and Sudarshan Schema diagram for University DB: Look at the keys in each table! Database System Concepts - 6th Edition 1.54 ©Silberschatz, Korth and Sudarshan Individual work (try to solve before the next class) End-of-chapter exercises • 1.1, 1.4, 1.8, 1.9 • 2.1, 2.3, 2.4 Database System Concepts - 6th Edition 1.55 ©Silberschatz, Korth and Sudarshan We have covered the following textbook sections: 1.3, 1.4, 1.5, 1.6.4, 2.3 See you at 2:40 in the lab! SCIENCE 206 EOL 2 Database System Concepts - 6th Edition 1.56 ©Silberschatz, Korth and Sudarshan QUIZ What are the data models we mentioned? • Relational • Entity-Relationship (E-R) • Object-based: Object-oriented and Object-relational • Semistructured: a “type” does not necessarily mean the same set of attributes! (e.g. XML, JSON) • Older models: • Network • Hierarchical Database System Concepts - 6th Edition 1.57 ©Silberschatz, Korth and Sudarshan QUIZ What are the two extreme cases of structuring of a DB into tables/relations? Why is either one bad? Name two major players in the DBMS market. Database System Concepts - 6th Edition 1.58 ©Silberschatz, Korth and Sudarshan QUIZ: SQL • Is SQL a procedural language? Explain. • Is SQL a DML or a DDL? Explain. Database System Concepts - 6th Edition 1.59 ©Silberschatz, Korth and Sudarshan QUIZ SQL Write (Postgre)SQL commands to do the following: Create a new database electronics Create a table circuits in electronics, with two columns: Name of chip (e.g. 74LS04) Type of gates (e.g. NOT) Nr. of gates on a chip (e.g. 6) Delete the whole table Delete the whole database Database System Concepts - 6th Edition 1.60 ©Silberschatz, Korth and Sudarshan CREATE DATABASE electronics; \connect electronics Must connect to DB to create table! CREATE TABLE circuits ( name varchar(15), type varchar(10), nr_gates integer ); DROP TABLE circuits; \connect postgres DROP DATABASE electronics; Database System Concepts - 6th Edition 1.61 Cannot drop the DB we are connected to! ©Silberschatz, Korth and Sudarshan QUIZ: Keys Database System Concepts - 6th Edition 1.62 ©Silberschatz, Korth and Sudarshan QUIZ: Keys Practice Exercise 2.1 Consider the following relational DB: Employee(person_name, street, city) Works(person_name, company_name, salary) Company(company_name, city) What are the appropriate primary keys? Database System Concepts - 6th Edition 1.63 ©Silberschatz, Korth and Sudarshan 1.6.1 The DB design process 1. A high-level data model (e.g. the E-R model, see below) is chosen as a conceptual framework to specify the requirements • DB designer interacts with users (a.k.a. domain experts) • Outcome is a (complete) specification of user requirements 2. A data model (e.g. the relational model) is chosen 3. Conceptual design: DB designer translates user requirements into conceptual schema for the model chosen • What attributes to represent in the DB • How to group attributes into tables Database System Concepts - 6th Edition 1.64 In the language of section 1.3.2, conceptual schema comes before logical schema! ©Silberschatz, Korth and Sudarshan 1.6.1 The DB design process 3. Conceptual design: DB designer translates user requirements into conceptual schema for the model chosen • Functional requirements = what kind of operations must be performed on the data 4. Logical design • The result is the logical schema of the DB (e.g. SQL DDL) 5. Physical design • The result is the physical schema of the DB (e.g. files, disk arrays, SAN) Database System Concepts - 6th Edition 1.65 ©Silberschatz, Korth and Sudarshan 1.6.2 Example: DB design for the University DB Read and understand! See also Appendix A! Database System Concepts - 6th Edition 1.66 ©Silberschatz, Korth and Sudarshan 1.6.3 The E-R model Detailed coverage to follow in Ch.7. Database System Concepts - 6th Edition 1.67 ©Silberschatz, Korth and Sudarshan SKIP Sections 1.7 through 1.12 READ and TAKE NOTES: 1.13 History of DB Systems Database System Concepts - 6th Edition 1.68 ©Silberschatz, Korth and Sudarshan 2.3 (contd.) Using Keys to Connect Different Relations/Tables Foreign key constraint: Value in one table must appear in another Referencing table (source) Referenced table (destination) Example: instructor.dept_name is a foreign key in instructor. It references department.dept_name Important: foreign keys are not keys! (not unique) Database System Concepts - 6th Edition 1.69 ©Silberschatz, Korth and Sudarshan QUIZ: Foreign Keys Practice Exercise 2.2 Give an example of tuple insert and an example of tuple delete that would cause violations of the FK constraint. Database System Concepts - 6th Edition 1.70 ©Silberschatz, Korth and Sudarshan 2.4 Schema diagrams Database System Concepts - 6th Edition 1.71 ©Silberschatz, Korth and Sudarshan 2.4 Schema diagrams An almost equivalent schema is this (What is missing?) Database System Concepts - 6th Edition 1.72 ©Silberschatz, Korth and Sudarshan 2.5 Relational Query Languages Very little new info. beyond Ch.1 – Read! Database System Concepts - 6th Edition 1.73 ©Silberschatz, Korth and Sudarshan 2.5 Relational Operations Database System Concepts - 6th Edition 1.74 ©Silberschatz, Korth and Sudarshan Relational Algebra: Selection of tuples (rows) Relation r Select tuples with A=B and D > 5 σ A=B and D > 5 (r) Database System Concepts - 6th Edition 1.75 ©Silberschatz, Korth and Sudarshan Relational Algebra: Selection of Columns (Attributes) Relation r : Select A and C A.k.a. projection Π A, C (r) Database System Concepts - 6th Edition 1.76 ©Silberschatz, Korth and Sudarshan QUIZ: Relational Algebra Write a relational algebra expression that returns those values of A that are associated with values of C in between 5 and 15 (inclusive). Database System Concepts - 6th Edition 1.77 ©Silberschatz, Korth and Sudarshan Relational Algebra and SQL The selection operator σ is analogous to the SQL clause … The projection operator P is analogous to the SQL clause … Database System Concepts - 6th Edition 1.78 ©Silberschatz, Korth and Sudarshan More Relational Algebra Database System Concepts - 6th Edition 1.79 ©Silberschatz, Korth and Sudarshan Joining two relations Cartesian Product is the most general join Relations r, s: r x s: Database System Concepts - 6th Edition 1.80 ©Silberschatz, Korth and Sudarshan Joining two relations Natural join is the most restrictive join Only attributes with the same name and type are matched! Database System Concepts - 6th Edition 1.81 ©Silberschatz, Korth and Sudarshan Union of two relations Relations r, s: r s: Database System Concepts - 6th Edition 1.82 ©Silberschatz, Korth and Sudarshan Set difference of two relations Relations r, s: r – s: Database System Concepts - 6th Edition 1.83 ©Silberschatz, Korth and Sudarshan Set Intersection of two relations Relation r, s: rs Database System Concepts - 6th Edition 1.84 ©Silberschatz, Korth and Sudarshan Practice Exercise 2.6 Explain in words what these expressions do: Database System Concepts - 6th Edition 1.85 ©Silberschatz, Korth and Sudarshan QUIZ: Relational Algebra Explain in words what this relational algebra expression returns: Database System Concepts - 6th Edition 1.86 ©Silberschatz, Korth and Sudarshan Individual work (try to solve before the next class) End-of-chapter Practice Exercises 2.3, 2.4, 2.5 Database System Concepts - 6th Edition 1.87 ©Silberschatz, Korth and Sudarshan Homework for Chs. 1 + 2 --1.11, 1.12, 1.15 --2.8, 2.9, 2.13, 2.14, 2.15 Due next Tuesday, Jan.31 Hint: For the Bank database used in several exercises, you may use the schema provided on the next slide. Database System Concepts - 6th Edition 1.88 ©Silberschatz, Korth and Sudarshan Schema for Bank database (fig.2.15) Database System Concepts - 6th Edition 1.89 ©Silberschatz, Korth and Sudarshan Text sections covered: 1.1 through 1.6 + 1.13 (History) Entire Ch.2 EOL 3 Database System Concepts - 6th Edition 1.90 ©Silberschatz, Korth and Sudarshan