280 likes | 478 Views
Indra Budi indra@cs.ui.ac.id. More on SQL. True/False. SQL stands for Standard Query Language . SQL is only a data manipulation language (DML). The SQL keyword CREATE is used to name a new table and describe the table's columns.
E N D
Indra Budi indra@cs.ui.ac.id More on SQL
True/False • SQL stands for Standard Query Language. • SQL is only a data manipulation language (DML). • The SQL keyword CREATE is used to name a new table and describe the table's columns. • An asterisk (*) following the SELECT verb means that all columns are to be obtained. • The WHERE clause contains the condition that specifies which columns are to be selected.
Comparison Operators Equals = Not equals <> Greater than > Less than < Greater than or equal to >= Less than or equal to <= Within a list of values IN A logical NOT Within a range BETWEEN
The DISTINCT Qualifier • Eliminating duplicate rows on the output… SELECT DISTINCT StateAddress FROM Employee;
IN a List of Values SELECT StudentName FROM Student WHERE State IN [‘PA’, ‘MA’, ‘CA’];
The Logical NOT SELECT StudentName FROM Students WHERE State NOT IN [‘NJ’, ‘NM’, ‘NY’]; SELECT StudentName FROM Students WHERE NOT GradePointAverage >= 3.0;
Within a Range of Values SELECT StudentName FROM Student WHERE StudentID BETWEEN 250 and 300;
Using Wildcard Character Substitutions • The LIKE keyword is used in place of the = sign when you use wildcard characters. • The underscore character (_) is a single character substitution • The percent character (%) is a multi-character substitution
Using LIKE SELECT StudentID FROM Student WHERE StudentName LIKE ‘K%’; SELECT PartName FROM Part WHERE PartNumber LIKE ‘_ABC%’;
NULL Means Nothing • A NULL character means that nothing has been entered. This is different from a space or a zero. SELECT Name FROM Student WHERE Major IS NULL;
ORDER BY… Sorting Outputs • Sorting in descending order… SELECT StudentID, Name FROM Student ORDER BY Name DESC; • Sorting in ascending order… SELECT StudentID, Name FROM Student ORDER BY Name ASC;
Built-in Functions • Counting number of rows COUNT • Adding the values in a column SUM • Averaging the values in a column AVG • Finding the maximum value in a column MAX • Finding the minimum value in a column MIN
Built-in Functions SELECT Count (*) FROM Student WHERE State = ‘WI’; SELECT Sum (Amount) FROM SalesReceipt; SELECT Max (Score) FROM Assignments;
Grouping the Output SELECT Name, State FROM Student GROUP BY State;
Reducing the Groups Displayed SELECT Name, State FROM Student GROUP BY State HAVING Count (*) > 4;
Sub-Queries SELECT Name FROM Student WHERE SID IN (SELECT StudentNumber FROM Enrollment WHERE ClassName = ‘MIS445’);
Joining Tables SELECT Student.SID, Student.Name, Enrollment.ClassName FROM Student, Enrollment WHERE Student.SID = Enrollment.StudentNumber AND Student.State = ‘OH’;
EXISTS SELECT DISTINCT StudentNumber FROM Enrollment A WHERE EXISTS (SELECT * FROM Enrollment B WHERE A.StudentNumber = B.StudentNumber AND A.ClassName NOT = B.ClassName);
Input, Delete & Modify Inputting Data INSERT INTO Enrollment VALUES (400, ‘MIS445’, 44); Deleting Data DELETE Student WHERE Student.SID = 100; Modify Data UPDATE Enrollment SET SeatNumber = 44 WHERE SID = 400;
Suppliers(sid, sname, address) • Parts(pid, pname, color, lifetime) • Catalog(sid, pid, price)
Write the Relational Algebra & SQL • Find the names of suppliers who supply some red part • Find the sids of suppliers who supply some red or green part.
Exercise • Flights(fno, from, to,distance,departs, arrives) • Aircraft(aid, aname, cruisingrange) • Certified(eid, aid) • Employees(eid, ename, salary)
Find the eids of pilots certified for some Boeing aircraft. • Find the eids of employees who make the highest salary.
Is this Relation in 1NF, 2 NF & 3NF ? • Book(Book_title, Authorname, Book_type, Listprice, Author_affil, Publisher) • FDs • Book_title Publisher, Book_type • Book_type --> Listprice • Authorname Author_affil
The key for this relation is Book_title,Authorname. This relation is in 1NF and not in 2NF as no attributes are FFD (Fully Functionally Dependent) on the key. It is also not in 3NF. • 2NF decomposition: • Book0(Book_title, Authorname) • Book1(Book_title, Publisher, Book_type, Listprice) • Book2(Authorname, Author_affil) • This decomposition eliminates the partial dependencies. • 3NF decomposition: • Book0(Book_title, Authorname) • Book1-1(Book_title, Publisher, Book_type) • Book1-2(Book_type, Listprice) • Book2(Authorname, Author_affil) • This decomposition eliminates the transitive dependency of Listprice
Given a relation Rwith four attributes, XYZW, and a set of functional dependencies F • F: XY --> Z, XY --> W, Z --> X, W --> Y • Identify the candidate key(s) for R. • Identify the best normal form that Rsatisfies (1NF, 2NF, 3NF, or BCNF). • If R is not in BCNF decompose it into a set of BCNF relations that preserve the dependencies.
Candidate keys: XY, ZW, ZY, XW • R is in 3NF but not BCNF due to the FDs: Z --> X and W --> Y. Neither W nor Z is a candidate key. • One possible decomposition that is in BCNF is XZ, WZ and WY. But this decomposition does not preserve the dependencies XY --> Z and XY --> W. If we include a relation such as XYZ then we are left with the violating FD: Z --> X. Similarly for XYW. So, there is no dependency-preserving decomposition into BCNF unless we do a join.