Download presentation
Presentation is loading. Please wait.
Published byMelvin Morris Modified over 9 years ago
1
Chapter 3 Query Language Database Application SAK 3408
2
What is SQL? SQL or Structured Query Language is an English-like language used to create and manipulate databases. SQL is an ANSI (American National Standards Institute) standard for accessing database systems. SQL statements are used to retrieve and update data in a database. SQL works with database programs like MS Access, DB2, Informix, MS SQL Server, Oracle, Sybase, etc. With SQL, we can query a database and have a result set returned.
3
Types of Statements Three types of statements: Data Definition Language (DDL) Data Manipulation Language (DML) Data Control Language (DCL) Data Definition Language creates and manipulates the structure, delete or define indexes of table in database Data Manipulation Language manipulates data in the table Data Control Language determines who is allowed to what within the database
4
SQL Grammar SQL statements are always terminated by a semi-colon (;) SELECT CUSTOMER_ID FROM CUSTOMER;
5
SQL Grammar SQL statements can be entered on a single line or split across multiple lines (preferred) SELECT CUSTOMER_ID, CUSTOMER_NAME, CREDIT_LINE FROM CUSTOMER WHERE CREDIT_LINE > 1000000 AND CUSTOMER_ID IN [1000,2000]; SELECT CUSTOMER_ID, CUSTOMER_NAME, CREDIT_LINE FROM CUSTOMER WHERE CREDIT_LINE > 1000000 AND CUSTOMER_ID IN [1000,2000];
6
SQL Grammar SQL statements are not case sensitive; however data is. SeLEct * fROm REQueST wHErE aCcT_sT In [‘GA’,’WA’,’NJ’]; Returns data for all requests where the state is either GA, WA or NJ. No records would have been returned if the where clause had been: wHErE aCcT_sT In [‘ga’,’wA’,’Nj’];
7
Qualifying a Field Name When the same field name occurs in two or more tables used in a single SQL statement, you must qualify the field name much like you would include the last name to avoid confusion if two people have the same first name. Tablename.fieldname Customer.last_name
8
Data Definition Language (DDL) Used to create and modify database objects Create Drop Alter
9
Data Manipulation Language (DML) Used to create, modify and retrieve data Insert Select Update Delete
10
Data Control Language (DCL) Used to control database privileges Grant Revoke
11
DDL - Creating a Table Use the Create keyword and specify: table name field (column) name(s) field type constraints primary key - unique identifier for a record foreign key - establishes relationship check - value must be in the specified list not null - must have a value unique - value must be unique
12
Table with Primary Key Only CREATE TABLE room( roomIDnumber, bldgchar(1) CHECK (bldg IN ('A','B')), roomNovarchar2(10), maxCapacity number, stylevarchar2(15) CHECK (style IN ('LECTURE','LECTURE/LAB','LAB','OFFICE')), CONSTRAINT room_pk PRIMARY KEY (roomID)); Constraint_name Constraint_type Constraint_attributes
13
Table with Foreign Key CREATE TABLE faculty( facultyIDnumber, lnamevarchar2(30)NOT NULL, fnamevarchar2(20)NOT NULL, deptvarchar2(5), officeIDnumber, phone varchar2(15), email varchar2(75)UNIQUE, rankchar(4) CHECK (rank IN ('INST', 'ASOC','ASST','FULL','SENR')), CONSTRAINT faculty_pk PRIMARY KEY (facultyID), CONSTRAINT faculty_fk FOREIGN KEY (officeID) REFERENCES room(roomID));
14
Table with Compound Primary Key create table participation( eventIDnumber, memberIDnumber, constraint participation_pk primary key (eventID, memberID), constraint participation_event_fk foreign key (eventID) references event(eventID), constraint participation_member_fk foreign key (memberID) references member(memberID));
15
DDL - Altering a Table Use the Alter and Add/Modify keywords and specify: table name new or existing field name(s) field type alter table invoice add(sent_dt date); alter table invoice modify (invoice_nbr varchar2(5));
16
DDL - Removing a Table Use the Drop keyword and specify: table name drop table invoice; If this table is referenced by a foreign key, use the cascade constraints clause drop table invoice cascade constraints;
17
Indexes Conceptually similar to book index Increases data retrieval efficiency Automatically assigns record numbers Used by DBMS, not by users Fields on which index built called Index Key Have a sorted order Can guarantee uniqueness Can include one or more fields
18
Indexes – Syntax CREATE UNIQUE INDEX cust_num_ind ON customer(Name); CREATE INDEX CustomerName ON Customer (CustomerNum); CREATE INDEX CreditLimitRep_ind ON Customer(CreditLimit, RepNum); DROP INDEX RepBal;
19
Customer Table with Record Numbers Figure 4.10
20
Customer Table Index on CustomerNum Figure 4.11
21
Table Indexes on CreditLimit, RepNum Figure 4.12
22
Indexes PROs Faster/more efficient data retrieval CONs Requires additional space Increased overhead
23
Data Manipulation Language (DML) Used to create, modify and retrieve data Insert Select Update Delete
24
DML - Adding Data to a Table Use the Insert keyword and specify: table name field names - optional values for each field insert into customer values(‘Teplow’,’MA’,23445.67); OR insert into customer (last_name, state_cd, sales) values (‘Teplow’,’MA’,23445.67);
25
DML - Updating Data in a Table Use the Update and Set keywords and specify: table name field name(s) where clause (optional) update inventory set price = price*1.05; update inventory set price = price*1.05 where product_id = 'P103';
26
DML-Deleting Records Use the Delete From keywords and specify: table name where clause (optional) delete from customer; delete from customer where sales < 10000;
27
Parts of a DML Select Statement Select From Where (optional) Order By (optional) Group By (optional) Having (optional)
28
SELECT Which fields do you want retrieved? * is used to select all fields in the table
29
FROM Which table(s) are these fields in?
30
Two Sample Tables
31
Select - Simplest Show everything in a single table SELECT * FROM customer; Returns LAST_NAMESTATE_CDSALES ----------------------------------------- TeplowMA 23445.67 AbbeyCA 6969.96 PorterCA 6989.99 MartinCA 2345.45 LaursenCA 34.34 BambiCA 1234.55 McGrawNJ 123.45
32
Select Statement - Simple SELECT last_name, state_cd FROM customer; Returns LAST_NAMESTATE_CD ------------------------ TeplowMA AbbeyCA PorterCA MartinCA LaursenCA BambiCA McGrawNJ
33
WHERE - Optional Use to: specify how to join two tables restrict the data returned
34
SQL Operators Logical Operators and or Comparison Operators =equality !=or <>inequality Likestring search Inlist of values Betweenrange of values
35
Select Statement - Equals SELECT * FROM customer WHERE state_cd = ‘CA’; Returns LAST_NAMESTSALES ------------------- --------- AbbeyCA 6969.96 PorterCA6989.99 MartinCA2345.45 LaursenCA 34.34 BambiCA1234.55
36
Select Statement – Not Equals SELECT * FROM customer WHERE state_cd <> ‘CA’; Returns LAST_NAMESTSALES ---------------- ---------- Teplow MA 23445.67 McGrawNJ 123.44
37
Select Statement - Like SELECT last_name, state_cd, sales FROM customer WHERE upper(state_cd) LIKE ‘%A’; Returns LAST_NAMESTSALES ----------------- ---------------- TeplowMA 23445.67 AbbeyCA 6969.96 PorterCA 6989.99 MartinCA 2345.45 LaursenCA 34.34 BambiCA 1234.55
38
Select Statement - In SELECT last_name, state_cd FROM customer WHERE state_cd IN (’MA’,’NJ’); Returns LAST_NAMEST --------------- TeplowMA McGrawNJ
39
Select Statement – Between SELECT * FROM customer WHERE sales BETWEEN 6500 AND 25000; Returns LAST_NAMESTSALES ---------------- ---------- TeplowMA 23445.67 AbbeyCA 6969.96 PorterCA 6989.99
40
Select Statement – Multiple Conditions Using OR SELECT * FROM customer WHERE state_cd <> ‘CA’ OR sales BETWEEN 6500 AND 25000; Returns LAST_NAMESTSALES ---------------- ---------- TeplowMA 23445.67 AbbeyCA 6969.96 PorterCA 6989.99 McGrawNJ 123.44
41
Select Statement – Multiple Conditions Using AND SELECT * FROM customer WHERE state_cd <> ‘CA’ AND sales BETWEEN 6500 AND 25000; Returns LAST_NAMESTSALES ---------------- ---------- TeplowMA 23445.67
42
Select Statement – Calculated Field SELECT last_name, sales, sales*05 as Tax FROM customer; Returns LAST_NAME SALES TAX ----------- ---------- -------- Teplow23445.67 1172.28 Abbey 6969.96 348.50 Porter 6989.99 349.50 Martin 2345.45 117.27 Laursen 34.34 1.71 Bambi 1234.55 61.73
43
ORDER BY - Optional Used to specify sorting order Can sort by multiple fields in ascending or descending order
44
Select Statement - Sorting select state_name, last_name, sales from customer, state where customer.state_cd = state.state_cd and state_cd in (‘CA’,’MA’,’NJ’) order by state_name; Returns STATE_NAMELAST_NAME SALES ------------------- --------------------------------- CaliforniaAbbey 6969.96 CaliforniaPorter 6989.99 MassachusettsTeplow 23445.67 New JerseyMcGraw 123.45
45
Overview Select Statements continued Table Joins Distinct Group By Having Decode function Sequence numbers Subqueries Insert Update Delete
46
Select - Table Join SELECT state_name, last_name, sales FROM customer, state WHERE customer.state_cd = state.state_cd; Returns STATE_NAME LAST_NAME SALES -------------- ------------------------- California Abbey 6969.96 California Porter 6989.99 Massachusetts Teplow 23445.67 New Jersey McGraw 123.45
47
Table Joins Process of combining data from two or more tables, normally using primary and/or foreign keys. Basic types of joins: Equijoin (Equal or Inner Join) Left join (Outer Join) Right join
48
Equi Joins Most common type of join Look for records which have matching values of the join fields
49
Join Processing Table joins are done by matching the first record from the primary table’s first record with each record in the secondary table, then matching the second record in the primary table with each record in the secondary table. Continue until each record from the primary table has been combined with each record from the secondary table. This is called a Cartesian product.
50
Join Processing - cont Oracle then goes through the Cartesian product, discarding combined records that have non-matching join fields. The remaining records are returned.
51
Cartesian Product Example Select ename, emp.deptno dept.deptno, dname FROM emp, dept Note: This example is only showing a few fields from the first three employees but all fields and all records are used to create the Cartesian product. The Cartesian product has 56 rows (14 employees * 4 depts)
52
Cartesian Product Example cont
53
Cartesian Product Example cont.
54
Left Joins Used when you may not have matching data in the secondary table, but still want to include the data you have in the primary table. NOTE: The primary table is normally listed on the left side of the equation, the secondary on the right side.
55
Left Join Example Create a brand new dept (deptno=50), but there are no employees in that dept yet Select dname, dept.deptno, emp.deptno, ename FROM dept, emp WHERE + dept.deptno = emp.deptno;
56
Right Joins Not as common as left joins. Used when you may have data in the secondary table with no matching data in the primary table.
57
Right Join Example Assign an employee to a brand new dept, deptno=50, but there’s no record in dept yet Select ename, dept.deptno deptno, dname FROM emp, dept WHERE + emp.deptno = dept.deptno;
58
Advanced Select - Distinct Used to omit duplicate values in a select statement Select distinct(student_id) From course_section Where term = '1001';
59
DISTINCT – example SELECT Category FROM Animal; Category Fish Dog Fish Cat Dog Fish Dog Bird Dog Fish Cat Dog SELECT DISTINCT Category FROM Animal; Category Bird Cat Dog Fish
60
Advanced Select - Group By Clause Allow you to group data together for summary calculations avg (column) count (*) max (column) min (column) sum (column)
61
Advanced Select- Group By Clause Group by clause must be included in a select statement that uses a group function (count, sum, min, max, and avg) All fields in the select clause which are not part of a group function must be included in the Group By clause.
62
Advanced Select - Group By Clause avg (column) 1 select deptno, avg(sal) 2 from scott.emp 3* group by deptno; DEPTNO AVG(SAL) -------------- ---------------- 10 2916.6667 20 2175 30 1566.6667
63
Advanced Select - Having Clause Allows you to specify conditions for a record set instead of a single record Must have a group by clause 1 select deptno, avg(sal) 2 from scott.emp 3 group by deptno 4* having avg(sal) > 2000 DEPTNO AVG(SAL) ------------- ---------------- 10 2916.6667 20 2175
64
Where vs Having Where - a constraint on individual records. Having - a constraint on a group of records. They can be used together in the same SQL statement Select ename From emp Where upper(loc) = 'NEW YORK' Group by deptno Having avg(sal) > 2000
65
Subquery Characteristics Can be used in the SELECT, CREATE, INSERT, UPDATE and DELETE statements Can be used as part of a condition in the WHERE clause Can be used in multiple AND or OR predicates of the same SQL statement
66
Subquery Characteristics - Cont Is enclosed in parenthesis and must appear on the right in a WHERE clause condition May or may not retrieve data from a table used in the main, or outer, SQL statement in which it’s embedded. Cannot include an ORDER BY clause
67
Subquery Characteristics - Cont The number of rows returned by the subquery must match the number expected by the main query The number of columns returned must also match the number expected
68
Combining Subqueries Multiple subqueries can be used to check for more than one condition in a statement. Same or different types can be nested. NOTE: Nested subqueries are executed from the most deeply nested to the least deeply nested subquery.
69
Show all customers who have placed an order SELECT CUSTOMER_NAME FROM CUSTOMER_T WHERE CUSTOMER_ID IN (SELECT DISTINCT CUSTOMER_ID FROM ORDER_T); Subquery - Example Subquery is embedded in parentheses. In this case it returns a list that will be used in the WHERE clause of the outer query
70
Subquery for Calculation Which cats sold for more than the average sale price of cats? Assume we know the average price is $170. Usually we need to compute it first. SELECT SaleAnimal.AnimalID, Animal.Category, SaleAnimal.SalePrice FROM Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE ((Animal.Category="Cat") AND (SaleAnimal.SalePrice>170)); SELECT SaleAnimal.AnimalID, Animal.Category, SaleAnimal.SalePrice FROM Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE ((Animal.Category="Cat") AND (SaleAnimal.SalePrice> (SELECT AVG(SalePrice) FROM Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE (Animal.Category=“Cat”) ) ) );
71
Using IN with a Sub-query List all customers who bought items for cats. SELECT Customer.LastName, Customer.FirstName, SaleItem.ItemID FROM (Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID) INNER JOIN SaleItem ON Sale.SaleID = SaleItem.SaleID WHERE (SaleItem.ItemID In (SELECT ItemID FROM Merchandise WHERE Category="Cat") );
72
Which animals have not been sold? Start with list of all animals. Subtract out list of those who were sold. SELECT Animal.AnimalID, Animal.Name, Animal.Category FROM Animal WHERE (Animal.AnimalID Not In (SELECT AnimalID From SaleAnimal)); Using NOT IN with a Sub-query
73
Subqueries vs Table Joins Always use subqueries instead of table joins when possible. Subqueries are significantly less resource intensive. Table joins are only required when the Select clause contains fields from multiple tables.
74
UNION, INTERSECT, EXCEPT T1T2 ABC SELECT EID, Name FROM EmployeeEast INTERSECT SELECT EID, Name FROM EmployeeWest List the name of any employee who has worked for both the East and West regions.
75
UNION Operator Offices in Los Angeles and New York. Each has an Employee table (East and West). Need to search data from both tables. Columns in the two SELECT lines must match. SELECT EID, Name, Phone, Salary, ‘East’ AS Office FROM EmployeeEast UNION SELECT EID, Name, Phone, Salary, ‘West’ AS Office FROM EmployeeWest EIDNamePhoneSalaryOffice 352Jones335245,000East 876Inez873647,000East 372Stoiko763238,000East 890Smythe980362,000West 361Kim773673,000West
76
Reflexive Join Need to connect a table to itself. Common example: Employee(EID, Name,..., Manager) A manager is also an employee. Use a second copy of the table and an alias. SELECT Employee.EID, Employee.Name, Employee.Manager, E2.Name FROM Employee INNER JOIN Employee AS E2 ON Employee.Manager = E2.EID EIDName...Manager 115Sanchez765 462Miller115 523Hawk115 765Munoz886 Employee EIDNameManagerName 115Sanchez765Munoz 462Miller115Sanchez 523Hawk115Sanchez SQL Result
77
CASE Function Used to change data to a different context. Example: Define age categories for the animals. Less than 3 months Between 3 months and 9 months Between 9 months and 1 year Over 1 year Select AnimalID, CASE WHEN Date()-DateBorn < 90 Then “Baby” WHEN Date()-DateBorn >= 90 AND Date()-DateBorn < 270 Then “Young” WHEN Date()-DateBorn >= 270 AND Date()-DateBorn < 365 Then “Grown” ELSE “Experienced” END FROM Animal; Not available in Microsoft Access. It is in SQL Server and Oracle.
78
Inequality Join AR(TransactionID, CustomerID, Amount, DateDue) AccountsReceivable Categorize by Days Late 30, 90, 120+ Three queries? New table for business rules LateCategory(Category, MinDays, MaxDays, Charge, …) Month30903% Quarter901205% Overdue120999910% SELECT * FROM AR INNER JOIN LateCategory ON ((Date() - AR.DateDue) >= LateCategory.MinDays) AND ((Date() - AR.DateDue) < LateCategory.MaxDays)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.