Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Microsoft SQL Server wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Concurrency control wikipedia , lookup
Clusterpoint wikipedia , lookup
Relational algebra wikipedia , lookup
ContactPoint wikipedia , lookup
Revision for Mid 1 Functional Dependencies FDs defined over two sets of attributes: X, R YR Notation: X Y reads as “X determines Y” If X Y, then all tuples that agree X Y Z 1 2 3 2 4 5 1 2 4 1 2 7 2 4 8 3 7 9 on X must also agree on Y Database System Concepts 7.2 ©Silberschatz, Korth and Sudarshan Functional Dependencies (example) X Database System Concepts Y Z 7.3 X Y Z 1 2 3 2 4 5 1 2 4 1 2 7 2 4 8 3 7 9 ©Silberschatz, Korth and Sudarshan Candidate Keys an attribute (or set of attributes) that uniquely identifies a row primary key is a special candidate key values cannot be null e.g. ENROLL (Student_ID, Name, Address, …) PK = Student_ID candidate key = Name, Address Database System Concepts 7.4 ©Silberschatz, Korth and Sudarshan Database System Concepts 7.5 ©Silberschatz, Korth and Sudarshan 2NF a relation is in second normal form if it is in first normal form AND every nonkey attribute is fully functionally dependant on the primary key i.e. remove partial functional dependencies, so no nonkey attribute depends on just part of the key Database System Concepts 7.6 ©Silberschatz, Korth and Sudarshan EMPLOYEE2 (Emp_ID, Course_Title, Name, Dept_Name, Salary, Date_Completed) Emp_ID Course_ Title Name Dept_ Name Salary Date_Com p. not fully functionally dependant on the primary key Database System Concepts 7.7 ©Silberschatz, Korth and Sudarshan Second Normal Form ( 2NF ) it is based on the concept of full functional dependency. A functional dependency XY is a full functional dependency , for any attribute A X, {X - {A}} Y. Database System Concepts 7.8 ©Silberschatz, Korth and Sudarshan 2NF A B (Example) C D R (A B C D) 1 1 2 3 2 1 3 2 2 Candidate Keys 3 1 2 3 R with key{AB} is NOT 2NF 1 2 1 3 R with key{AC} is NOT 2NF Database System Concepts 7.9 ©Silberschatz, Korth and Sudarshan Second Normal Form Second normal form: Let R’ be a relation, and let F be the set of governing FDs. An attribute belongs to R’ is prime if a key of R’ contains A. In other words, A is prime in R’ if there exists K<R’ such that (1) K->R’, (2) for all B belongs to K, (K-B)->R’ not belongs to F+, and (3) A belongs to K Database System Concepts 7.10 ©Silberschatz, Korth and Sudarshan Database System Concepts 7.11 ©Silberschatz, Korth and Sudarshan General Definitions of Second Normal Form A relation schema R is in second normal form (2NF) if every nonprime attribute A in R is fully functionally dependent on every key of R. Database System Concepts 7.12 ©Silberschatz, Korth and Sudarshan Third Normal Form A relation R is in 3NF if, for all X A that holds over R A X ( i.e., X A is a trivial FD ), or X is a superkey, or A is part of some key for R If R is in BCNF, obviously it is in 3NF. The definition of 3NF is similar to that of BCNF, with the only difference being the third condition. Recall that a key for a relation is a minimal set of attributes that uniquely determines all other attributes. A must be part of a key (any key, if there are several). It is not enough for A to be part of a superkey, because this condition is satisfied by every attribute. Database System Concepts 7.13 ©Silberschatz, Korth and Sudarshan Suppose that a dependency X A causes a violation of 3NF. There are two cases: X is a proper subset of some key K. Such a dependency is sometimes called a partial dependency. In this case, we store (X,A) pairs redundantly. X is not a proper subset of any key. Such a dependency is sometimes called a transitive dependency, because it means we have a chain of dependencies K XA. Database System Concepts 7.14 ©Silberschatz, Korth and Sudarshan Key Attributes X Attributes A A not in a key Partial Dependencies Key Key Attributes X Attributes A Attributes A Attributes X A not in a key A in a key Transitive Dependencies Database System Concepts 7.15 ©Silberschatz, Korth and Sudarshan Motivation of 3NF By making an exception for certain dependencies involving key attributes, we can ensure that every relation schema can be decomposed into a collection of 3NF relations using only decompositions. Such a guarantee does not exist for BCNF relations. It weaken the BCNF requirements just enough to make this guarantee possible. Unlike BCNF, some redundancy is possible with 3NF. The problems associate with partial and transitive dependencies persist if there is a nontrivial dependency XA and X is not a superkey, even if the relation is in 3NF because A is part of a key. Database System Concepts 7.16 ©Silberschatz, Korth and Sudarshan Reserves Assume: sid cardno (a sailor uses a unique credit card to pay for reservations). Reserves is not in 3NF sid is not a key and cardno is not part of a key In fact, (sid, bid, day) is the only key. (sid, cardno) pairs are redundantly. Database System Concepts 7.17 ©Silberschatz, Korth and Sudarshan Reserves Assume: sid cardno, and cardno sid (we know that credit cards also uniquely identify the owner). Reserves is in 3NF (cardno, sid, bid) is also a key for Reserves. sid cardno does not violate 3NF. Database System Concepts 7.18 ©Silberschatz, Korth and Sudarshan Lecture 12: Further relational algebra, further SQL www.cl.cam.ac.uk/Teaching/current/Databases/ Today’s lecture Where does SQL differ from relational model? What are some other features of SQL? How can we extend the relational algebra to match more closely SQL? Database System Concepts 7.20 ©Silberschatz, Korth and Sudarshan Duplicate rows Consider our relation instances from lecture 6, Reserves, Sailors and Boats Consider SELECT rating,age FROM Sailors; We get a relation that doesn’t satisfy our definition of a relation! RECALL: We have the keyword DISTINCT to remove duplicates Database System Concepts 7.21 ©Silberschatz, Korth and Sudarshan Multiset semantics A relation in SQL is really a multiset or bag, rather than a set as in the relational model A multiset has no order (unlike a list), but allows duplicates E.g. {1,2,1,3} is a bag select, project and join work for bags as well as sets Just work on a tuple-by-tuple basis Database System Concepts 7.22 ©Silberschatz, Korth and Sudarshan Extended relational algebra Add features needed for SQL 1. Bag semantics 2. Duplicate elimination operator, 3. Sorting operator, 4. Grouping and aggregation operator, Outerjoin operators, oV, Vo, oVo 5. Database System Concepts 7.23 ©Silberschatz, Korth and Sudarshan Duplicate-elimination operator (R) = relation R with any duplicated tuples removed (R)= R= A B A B This is used 1 2to model the DISTINCT 1 2 feature of SQL Database System Concepts 3 4 1 2 3 4 7.24 ©Silberschatz, Korth and Sudarshan Sorting L1,… Ln(R) returns a list of tuples of R, ordered according to the attributes L1, …, Ln Note: does not return a relation R= B(R)= [(5,2),(1,3),(3,4)] A B 1 3 3 4 ORDER BY in SQL, e.g. SELECT * 5 2 FROM Sailors WHERE rating>7 ORDER BY age, sname; Database System Concepts 7.25 ©Silberschatz, Korth and Sudarshan Extended projection SQL allows us to use arithmetic operators SELECT age*5 FROM Sailors; We extend the projection operator to allow the columns in the projection to be functions of one or more columns in the argument relation, e.g. A+B,A,A(R)= R= Database System Concepts A B A+B A.1 A.2 1 2 3 1 1 3 4 7 3 3 7.26 ©Silberschatz, Korth and Sudarshan Arithmetic Arithmetic (and other expressions) can not be used at the top level i.e. 2+2 is not a valid SQL query How would you get SQL to compute 2+2? Database System Concepts 7.27 ©Silberschatz, Korth and Sudarshan Aggregation SQL provides us with operations to summarise a column in some way, e.g. SELECT COUNT(rating) FROM Sailors; SELECT COUNT(DISTINCT rating) FROM Sailors; SELECT COUNT(*) FROM Sailors WHERE rating>7; We also have SUM, AVG, MIN and MAX Database System Concepts 7.28 ©Silberschatz, Korth and Sudarshan Grouping These aggregation operators have been applied to all qualifying tuples. Sometimes we want to apply them to each of several groups of tuples, e.g. For each rating, find the average age of the sailors For each rating, find the age of the youngest sailor Database System Concepts 7.29 ©Silberschatz, Korth and Sudarshan GROUP BY in SQL SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list; The target-list contains 1. List of column names 2. Aggregate terms NOTE: The variables in target-list must be contained in groupinglist Database System Concepts 7.30 ©Silberschatz, Korth and Sudarshan GROUP BY cont. For each rating, find the average age of the sailors SELECT rating,AVG(age) FROM Sailors GROUP BY rating; For each rating find the age of the youngest sailor SELECT rating,MIN(age) FROM Sailors GROUP BY rating; Database System Concepts 7.31 ©Silberschatz, Korth and Sudarshan Grouping and aggregation L(R) where L is a list of elements that are either Individual column names (“Grouping attributes”), or Of the form (A), where is an aggregation operator (MIN, SUM, …) and A is the column it is applied to For example, rating,AVG(age)(Sailors) Database System Concepts 7.32 ©Silberschatz, Korth and Sudarshan Example Let R= bar beer price Anchor 6X 2.50 Anchor Adnam’s 2.40 Mill 6X 2.60 Mill Fosters 2.80 Eagle Fosters 2.90 Compute beer,AVG(price)(R) Database System Concepts 7.33 ©Silberschatz, Korth and Sudarshan Example cont. 1. 2. Group according to the grouping attribute, beer: bar beer price Anchor 6X 2.50 Mill 6X 2.60 Anchor Adnam’s 2.40 Fosters 2.80 Fosters 2.90 Compute average Mill Eagle Database System Concepts 7.34 of price within groups: beer price 6X 2.55 Adnam’s 2.40 Fosters 2.85 ©Silberschatz, Korth and Sudarshan NULL values Sometimes field values are unknown (e.g. rating not known yet), or inapplicable (e.g. no spouse name) SQL provides a special value, NULL, for both these situations This complicates several issues Special operators needed to check for NULL Is NULL>8? Is (NULL OR TRUE)=TRUE? We need a three-valued logic Need to carefully re-define semantics Database System Concepts 7.35 ©Silberschatz, Korth and Sudarshan NULL values Consider INSERT INTO Sailors (sid,sname) VALUES (101,”Julia”); SELECT * FROM Sailors; SELECT rating FROM Sailors; SELECT sname FROM Sailors WHERE rating>0; Database System Concepts 7.36 ©Silberschatz, Korth and Sudarshan Entity integrity constraint An entity integrity constraint states that no primary key value can be NULL Database System Concepts 7.37 ©Silberschatz, Korth and Sudarshan Outer join Note that with the usual join, a tuple that doesn’t ‘join’ with any from the other relation is removed from the resulting relation Instead, we can ‘pad out’ the columns with NULLs This operator is called an full outer join, written oVo Database System Concepts 7.38 ©Silberschatz, Korth and Sudarshan Example of full outer join Let R= Let S= A B B C 1 2 4 5 Then RVS =3 4 6 7 But RoVoS = Database System Concepts A B C 3 4 5 A B C 1 2 NULL 3 4 5 NULL 6 7 7.39 ©Silberschatz, Korth and Sudarshan Outer joins in SQL SQL/92 has three variants: LEFT OUTER JOIN (algebra: oV) RIGHT OUTER JOIN (algebra: Vo) FULL OUTER JOIN (algebra: oVo) For example: SELECT * FROM Reserves r LEFT OUTER JOIN Sailors s ON r.sid=s.sid; Database System Concepts 7.40 ©Silberschatz, Korth and Sudarshan Views A view is a query with a name that can be used in further SELECT statements, e.g. CREATE VIEW ExpertSailors(sid,sname,age) AS SELECT sid,sname,age FROM Sailors WHERE rating>9; Note that ExpertSailors is not a stored relation (WARNING: mysql does not support views ) Database System Concepts 7.41 ©Silberschatz, Korth and Sudarshan Querying views So an example query SELECT sname FROM ExpertSailors WHERE age>27; is translated by the system to the following: SELECT sname FROM Sailors WHERE rating>9 AND age>27; Database System Concepts 7.42 ©Silberschatz, Korth and Sudarshan Relational Algebra The Relational Algebra is used to define the ways in which relations (tables) can be operated to manipulate their data. It is used as the basis of SQL for relational databases, and illustrates the basic operations required of any DML. This Algebra is composed of Unary operations (involving a single table) and Binary operations (involving multiple tables). Database System Concepts 7.43 ©Silberschatz, Korth and Sudarshan SQL Structured Query Language (SQL) Standardised by ANSI Supported by modern RDBMSs Commands fall into three groups Data Definition Language (DLL) Create tables, etc Data Manipulation Language (DML) Retrieve and modify data Data Control Language Control what users can do – grant and revoke privileges Database System Concepts 7.44 ©Silberschatz, Korth and Sudarshan Selection The selection or operation selects rows from a table that satisfy a condition: < condition > < tablename > Example: course = ‘CM’ Students Students stud# name course 100 Fred PH stud# name course 200 Dave CM 200 Dave CM 300 Bob CM 300 Bob CM Database System Concepts 7.45 ©Silberschatz, Korth and Sudarshan Projection The projection or operation selects a list of columns from a table. < column list > < tablename > Example: stud#, name Students Students stud# name course stud# name 100 PH 100 Fred Fred 200 Dave CM 200 300 Bob CM 300 Database System Concepts 7.46 Dave Bob ©Silberschatz, Korth and Sudarshan Selection / Projection Selection and Projection are usually combined: stud#, name ( course = ‘CM’ Students) Students stud# name course 100Fred PH stud# name 200Dave CM 200 Dave 300Bob 300 Bob CM Database System Concepts 7.47 ©Silberschatz, Korth and Sudarshan Cartesian Product Concatenation of every row in the first relation (R) with every row in the second relation (S): R X S Database System Concepts 7.48 ©Silberschatz, Korth and Sudarshan Cartesian Product - Example Students stud# name 100 Fred 200 Dave 300 Bob Courses course# PH CM course PH CM CM name Pharmacy Computing Students X Courses = stud# Students.name course 100 Fred 100 Fred 200 Dave 200 Dave 300 Bob 300 Bob Database System Concepts course# PH PH CM CM CM CM PH CM PH CM PH CM 7.49 Courses.name Pharmacy Computing Pharmacy Computing Pharmacy Computing ©Silberschatz, Korth and Sudarshan Theta Join A Cartesian product with a condition applied: R ⋈ <condition> S Database System Concepts 7.50 ©Silberschatz, Korth and Sudarshan Theta Join - Example Students Courses stud# name course course# name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing 300 Bob CM Students ⋈ stud# = 200 Courses stud# Students.name course course# 200 Dave CM PH Pharmacy 200 Dave CM CM Computing Database System Concepts Courses.name 7.51 ©Silberschatz, Korth and Sudarshan Inner Join (Equijoin) A Theta join where the <condition> is the match (=) of the primary and foreign keys. R⋈ Database System Concepts <R.primary_key = S.foreign_key> 7.52 S ©Silberschatz, Korth and Sudarshan Inner Join - Example Students Courses stud# name course course# name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing 300 Bob CM Students ⋈ course = course# Courses stud# Students.name course course# 100 Fred PH PH Pharmacy 200 Dave CM CM Computing 300 Bob CM CM Computing Database System Concepts Courses.name 7.53 ©Silberschatz, Korth and Sudarshan Natural Join Inner join produces redundant data (in the previous example: course and course#). To get rid of this duplication: < stud#, Students.name, course, Courses.name > (Students ⋈ Or R1= Students ⋈ R2= <course = course#> Courses) <course = course#> Courses < stud#, Students.name, course, Courses.name > R1 The result is called the natural join of Students and Courses Database System Concepts 7.54 ©Silberschatz, Korth and Sudarshan Natural Join - Example Students Courses stud# name course course# name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing 300 Bob R1= Students ⋈ CM <course = course#> Courses R2= < stud#, Students.name, course, Courses.name > R1 stud# Students.name course Courses.name 100 Fred PH Pharmacy 200 Dave CM Computing 300 Bob CM Computing Database System Concepts 7.55 ©Silberschatz, Korth and Sudarshan Outer Joins Inner join + rows of one table which do not satisfy the <condition>. Left Outer Join: R <R.primary_key = S.foreign_key> S All rows from R are retained and unmatched rows of S are padded with NULL Right Outer Join: R <R.primary_key = S.foreign_key> S All rows from S are retained and unmatched rows of R are padded with NULL Database System Concepts 7.56 ©Silberschatz, Korth and Sudarshan Left Outer Join - Example Students Courses stud# name course course# name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing EN CH Chemistry course# Courses.name 400 Peter Students <course = course#> Courses stud# Students.name course 100 Fred PH PH Pharmacy 200 Dave CM CM Computing EN NULL NULL 400 Peter Database System Concepts 7.57 ©Silberschatz, Korth and Sudarshan Right Outer Join - Example Students Courses stud# name course course# name 100 Fred PH PH Pharmacy 200 Dave CM CM Computing EN CH Chemistry 400 Peter Students <course = course#> Courses stud# Students.name course course# 100 Fred PH PH Pharmacy 200 Dave CM CM Computing NULL NULL Database System Concepts NULL Courses.name CH Chemistry 7.58 ©Silberschatz, Korth and Sudarshan Combination of Unary and Join Operations Students stud# name 100 Fred 200 Dave 300 Bob address Aberdeen PH Dundee Aberdeen CM course PH CM Courses course# name Pharmacy CM Computing Show the names of students (from Aberdeen) and the names of their courses R1= Students ⋈ <course=course#> Courses R2= <address=“Aberdeen”> R1 R3= <Students.name, Course.name> R2 Students.name Fred Bob Database System Concepts 7.59 Courses.name Pharmacy Computing ©Silberschatz, Korth and Sudarshan Union Takes the set of rows in each table and combines them, eliminating duplicates Participating relations must be compatible, ie have the same number of columns, and the same column names, domains, and data types R S RS A a1 a2 Database System Concepts B b1 b2 A a2 a3 B b2 b3 7.60 A a1 a2 a3 B b1 b2 b3 ©Silberschatz, Korth and Sudarshan Intersection Takes the set of rows that are common to each relation Participating relations must be compatible R A a1 a2 Database System Concepts RS S B b1 b2 A a2 a3 B b2 b3 7.61 A a2 B b2 ©Silberschatz, Korth and Sudarshan Difference Takes the set of rows in the first relation but not the second Participating relations must be compatible R A a1 a2 Database System Concepts S B b1 b2 A a2 a3 R-S B b2 b3 7.62 A a1 B b1 ©Silberschatz, Korth and Sudarshan Exercise (May 2004 Exam) Employee WorkLoad Project empid name empid* projid* duration E100 Fred E100 P001 17 P001 DB E200 Dave E200 P001 12 P002 Access E300 Bob E300 P002 15 P003 SQL E400 Peter projid name Determine the outcome of the following operations: A natural join between Employee and WorkLoad A left outer join between Employee and WorkLoad A right outer join between WorkLoad and Project Database System Concepts 7.63 ©Silberschatz, Korth and Sudarshan Unary Operations Selection Projection course = ‘Computing’ Students stud#, name Students In SQL: In SQL: Select * Select stud#, name From Students From Students; Where course = ‘Computing’; Selection & Projection stud#, name ( course = ‘Computing’ Students) In SQL: Select stud#, name From students Where course = ‘Computing’; Database System Concepts 7.64 ©Silberschatz, Korth and Sudarshan Binary Operations/Joins Cartesian Product: Students X Courses In SQL: Select * From Students, Courses; Theta Join: Students ⋈ <stud# =200> Courses In SQL: Select * From Students, Courses Where stud# = 200; Database System Concepts 7.65 ©Silberschatz, Korth and Sudarshan Binary Operations/Joins Inner Join (Equijoin): Students ⋈ <course=course#> Courses In SQL: Select * From Students, Courses Where course=course#; Natural Join: R1= Students ⋈ <course = course#> Courses R2= < stud#, Students.name, course, Courses.name > R1 In SQL: Select stud#, Students.name, course, Courses.name From Students, Courses Where course=course#; Database System Concepts 7.66 ©Silberschatz, Korth and Sudarshan Outer Joins Left Outer Join Students <course = course#> Courses In SQL: Select * From Students, Courses Where course = course#(+) Right Outer Join Students <course = course#> Courses In SQL: Select * From Students, Courses Where course(+) = course# Database System Concepts 7.67 ©Silberschatz, Korth and Sudarshan Combination of Unary and Join Operations R1= Students ⋈ <course=course#> Courses R2= <address=“Aberdeen”> R1 R3= <Students.name, Course.name> R2 In SQL: Select Students.name, Courses.name From Students, Courses Where course=course# AND address=“Aberdeen”; Database System Concepts 7.68 ©Silberschatz, Korth and Sudarshan Set Operations Union: R S Intersection: R S In SQL: In SQL: Select * From R Select * From R Union Intersect Select * From S; Select * From S; Difference: R - S In SQL: Select * From R Minus Select * From S; Database System Concepts 7.69 ©Silberschatz, Korth and Sudarshan SQL Operators SELECT * FROM Book WHERE catno BETWEEN 200 AND 400; SELECT * FROM Product WHERE prod_desc BETWEEN ‘C’ AND ‘S’; SELECT * FROM Book WHERE catno NOT BETWEEN 200 AND 400; Database System Concepts 7.70 ©Silberschatz, Korth and Sudarshan SELECT Catno SQL Operators FROM Loan WHERE Date-Returned IS NULL; SELECT Catno FROM Loan WHERE Date-Returned IS NOT NULL; Database System Concepts 7.71 ©Silberschatz, Korth and Sudarshan SELECT Name SQL Operators FROM Member WHERE memno IN (100, 200, 300, 400); SELECT Name FROM Member WHERE memno NOT IN (100, 200, 300, 400); Database System Concepts 7.72 ©Silberschatz, Korth and Sudarshan SQL Operators SELECT Name FROM Member WHERE address NOT LIKE ‘%Aberdeen%’; SELECT Name FROM Member WHERE Name LIKE ‘_ES%’; Note: In MS Access, use * and # instead of % and _ Database System Concepts 7.73 ©Silberschatz, Korth and Sudarshan Selecting Distinct Values Student stud# 100 Fred 200 Dave 300 Bob name Aberdeen Dundee Aberdeen address SELECT Distinct address FROM Student; address Aberdeen Dundee Database System Concepts 7.74 ©Silberschatz, Korth and Sudarshan