Содержание
- 2. Main Textbook Database Systems: The Complete Book Hector Garcia-Molina Jeffrey D. Ullman Jennifer Widom
- 3. Alternative Textbook Database Management Systems Raghu Ramakrishnan Johannes Gehrke
- 4. Goals of Course To obtain a firm background in database systems, e.g., how to talk to
- 5. What will NOT be taught Advanced database technologies Geographical information systems Data mining … (This is
- 6. Teaching Style There will be a lot of in-lecture exercises Questions will be welcomed Lecture notes
- 7. Course Overview What is a database? A large collection of data organized especially for rapid search
- 8. Database and DBMS Database Database Management System User What is the average annual income of a
- 9. Tables, Relations, Relational Model Database Database Management System User Income_Table
- 10. Tables, Relations, Relational Model Database Database Management System User Income_Table What is the average annual income
- 11. Structured Query Language (SQL) Database Database Management System User Income_Table SELECT avg(Annual_Income) FROM Income_Table
- 12. Structured Query Language (SQL) Database Database Management System User Income_Table SELECT avg(Annual_Income) FROM Income_Table More details
- 13. Database Schema Design Database Database Management System User You should store XXX and YYY in two
- 14. Database Schema Design Assume that we want to capture parent-child relationships
- 15. Database Schema Design Is one column enough?
- 16. Database Schema Design Are two columns enough? Assume that two columns are enough Does everyone have
- 17. Course Content SQL Constraints and Triggers Conceptual Design Indices Relation Algebra Query Processing/Optimization Concurrency Control Recovery
- 18. What do you want from a DBMS? Why do we need it? Keep data around (persistent)
- 19. The Relational Revolution The Relational Revolution (1970’s) IBM and Univ of Berkeley A simple data model:
- 20. “Relational databases are the foundation of western civilization.” Bruce Lindsay IBM Fellow IBM Almaden Research Center
- 21. Structured Query Language (SQL)
- 22. Structured Query Language (SQL) A declarative (computer) language for managing data in a relational database management
- 23. Tables in SQL Product Table name Attribute name Tuple (i.e. row, record) Key
- 24. Data Types in SQL Character strings CHAR(20) VARCHAR(50) … Numbers INT FLOAT … Others BOOLEAN DATETIME
- 25. Simple SQL Query Product SELECT * FROM Product WHERE Category = ‘Phone’ “selection”
- 26. Simple SQL Query Product SELECT * FROM Product WHERE Category ‘Phone’
- 27. Simple SQL Query Product SELECT * FROM Product WHERE Category = ‘Phone’ AND Price > 800
- 28. Simple SQL Query Product SELECT * FROM Product WHERE Category = ‘Tablet’ OR Price > 1000
- 29. Simple SQL Query (cont.) Product SELECT PName, Price, Manufacturer FROM Product WHERE Price > 800 “selection
- 30. Details SQL is NOT case sensitive (when it comes to keywords and names) SELECT = Select
- 31. Patterns for Strings Product SELECT * FROM Product WHERE PName LIKE ‘iPh%’ % stands for “any
- 32. Patterns for Strings Product SELECT * FROM Product WHERE PName LIKE ‘%Ph%’ % stands for “any
- 33. Patterns for Strings Product SELECT * FROM Product WHERE PName LIKE ‘%P%e%’ % stands for “any
- 34. Patterns for Strings Product SELECT * FROM Product WHERE PName LIKE ‘_Phone 4’ _ stands for
- 35. Patterns for Strings Product SELECT * FROM Product WHERE PName LIKE ‘_Phone__’ _ stands for “any
- 36. Patterns for Strings Product SELECT * FROM Product WHERE PName NOT LIKE ‘_Phone__’
- 37. Eliminating Duplicates Product SELECT Category FROM Product
- 38. Eliminating Duplicates (cont.) Product SELECT DISTINCT Category FROM Product
- 39. Ordering the Results SELECT PName, Price FROM Product WHERE Price Product
- 40. Ordering the Results (cont.) SELECT PName, Price FROM Product WHERE Price Product
- 41. Ordering the Results (cont.) SELECT PName, Category FROM Product WHERE Price Product
- 42. Ordering the Results (cont.) SELECT PName, Category FROM Product WHERE Price Product
- 43. Ordering the Results (cont.) SELECT PName, Category FROM Product WHERE Price Product
- 44. Exercise SELECT DISTINCT Category FROM Product ORDER BY Category Product ?
- 45. Exercise Product SELECT DISTINCT Category FROM Product ORDER BY Category
- 46. Exercise SELECT DISTINCT Category FROM Product ORDER BY Category WHERE Price Product ?
- 47. Exercise “WHERE” should always proceed “ORDER BY” SELECT DISTINCT Category FROM Product ORDER BY Category WHERE
- 48. Exercise SELECT DISTINCT Category FROM Product ORDER BY PName Product ?
- 49. Exercise “ORDER BY” items must appear in the select list if “SELECT DISTINCT” is specified SELECT
- 50. Joins Company Product A user wants to know the names and prices of all products by
- 51. Joins Company Product SELECT PName, Price FROM Product, Company WHERE Country = ‘Japan’ AND Manufacturer =
- 52. Joins Find the names of the persons who work for companies in USA SELECT PName FROM
- 53. Joins Find the names the persons who work for companies in USA, as well as their
- 54. Joins Company Person Find the names the persons who work for companies in USA, as well
- 55. Joins Find the names the persons who work for companies in USA, as well as their
- 56. Joins Find the names the persons who work for companies in USA, as well as their
- 57. Joins Find the names the persons who work for companies in USA, as well as their
- 58. Joins Find the names the persons who work for companies in USA, as well as their
- 59. Exercise Company Product Exercise: Find the names of the companies in China that produce products in
- 60. Exercise Company Product Exercise: Find the names of the companies in China that produce products in
- 61. Exercise Exercise: Find the manufacturers that produce products in both the ‘tablet’ and ‘phone’ categories SELECT
- 62. Exercise Product Exercise: Find the manufacturers that produce products in both the ‘tablet’ and ‘phone’ categories
- 63. Subqueries A subquery is a SQL query nested inside a larger query Queries with subqueries are
- 64. A special subquery: Scalar Subquery Example Query From Sells(bar, beer, price), find the bars that serve
- 65. Example Scalar Subquery SELECT price FROM Sells WHERE bar = `Ku De Ta’ AND beer =
- 66. Example Scalar Subquery SELECT bar FROM Sells WHERE beer = ‘Heineken’ AND price = (SELECT price
- 67. Subqueries in FROM Find all products in the ‘phone’ category with prices under 1000 SELECT X.PName
- 68. Subqueries in FROM (cont.) Find all products in the ‘phone’ category with prices under 1000 SELECT
- 69. Subqueries in WHERE (cont.) Find all companies that make some products with price SELECT DISTINCT CName
- 70. Subqueries in WHERE (cont.) Find all companies that make some products with price SELECT DISTINCT CName
- 71. Subqueries in WHERE (cont.) Find all companies that make some products with price SELECT DISTINCT CName
- 72. Subqueries in WHERE (cont.) Find all companies that make some products with price SELECT DISTINCT CName
- 73. Subqueries in WHERE (cont.) Find all companies that make some products with price SELECT DISTINCT CName
- 74. Operators in Subqueries IN IN is true if and only if the tuple is a member
- 75. Avoiding Nested Queries In general, nested queries tend to be more inefficient than un-nested queries query
- 76. Subqueries in WHERE (cont.) Find all companies that do not make any product with price SELECT
- 77. Subqueries in WHERE (cont.) Find all companies that do not make any product with price SELECT
- 78. Subqueries in WHERE (cont.) Find all companies that does not make any products with price SELECT
- 79. Exercise Find all drinkers that frequent some bar that serves some beer they like SELECT DINSTINT
- 80. Exercise Find all drinkers that frequent some bar that does not serve any beer they like
- 81. Exercise Find all drinkers that do not frequent any bar that serve some beer they like
- 82. Roadmap --SQL Table SELECT FROM WHERE ORDER BY Joins Subqueries Aggregations UNION, INTERSECT, EXCEPT NULL Outerjoin
- 83. Exercise Find all drinkers that frequent some bar that does not serve any beer they like
- 85. Скачать презентацию