SQL (Chapter 2: Simple queries; Chapter 7 and 8: Nested and DML queries) Many of the examples in this document are based on the tables in the next slide. These are similar to the tables in the test database CS451 Supplier. Just about any query involving two tables connected by a many-many relationship will be similar
65
Embed
SQL (Chapter 2: Simple queries; Chapter 7 and 8: Nested and DML queries) Many of the examples in this document are based on the tables in the next slide.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
SQL (Chapter 2: Simple queries; Chapter 7 and 8: Nested and DML queries)
Many of the examples in this document are based on the tables in the next slide.
These are similar to the tables in the test database CS451 Supplier.
Just about any query involving two tables connected by a many-many relationship will be similar to something from the following slides (adapted from a book by C. J. Date).
S# SNAME STATUS CITY P# PNAME COLOR WEIGHTS1 Smith 20 London P1 Nut Blue 12S2 Jones 10 Paris P2 Bolt Green 17S3 Blake 30 Paris P3 Screw Blue 17S4 Clark 20 London P4 Screw Red 14S5 Adams 30 Athens P5 Cam Blue 12
Find the names of suppliers who do NOT supply part ‘P2’.
You can negate nested queries by using NOT IN or NOT EXISTS
Find the names of suppliers who do NOT supply part ‘P2’.
Select Sname
From S
Where S# Not IN( Select S#
From SP
Where P# = ‘P2’)
Select Sname
From S
Where not EXISTS( Select *
From SP
Where S# = S.S# and P# = ‘P2’)
List suppliers who supply at least one red part.
Select Sname
from S, SP, P
where S.S#=SP.S# and
SP.P#=P.P# and
P.color=‘Red’
Multiple nested queries: List suppliers who supply at least one red part.
Select Sname
From S
Where S# IN
( Select S#
from SP
where P# IN
( Select P#
from P
where color = ‘Red’))
Which is better?
The former is probably more clear – but maybe not to all.
The latter allows variations more easily.
Negating multiple nested queries:NOT prior to first IN: List suppliers who do NOT supply a red part.
NOT prior to second IN: List suppliers who supply parts that are NOT red (non-red parts).
NOT prior to both: List suppliers who do NOT supply parts that are NOT red
i.e. ONLY red parts.
NOTE: Two negations do NOT cancel
Select Sname
From S
Where EXISTS
( Select *
from P
where color=’Red’ and
EXISTS
( Select *
from SP
where SP.S#=S.S# and
P.P#=SP.P#))
Negating multiple nested queries:
NOT prior to first EXISTS: List suppliers who do NOT supply a red part.
NOT prior to second EXISTS: List suppliers for which there is a red part they don’t supply.
NOT prior to both: List suppliers for which there is no red part they do not supply (i.e. supply ALL red parts).
NOTE: Two negations do NOT cancel
List supplier names of those who supply all parts:
An alternative form is: List supplier names of those for whom there is no part they do not supply.
Select Sname
From S
Where NOT EXISTS
( Select *
from P
where NOT EXISTS
( Select *
from SP
where SP.S# = S.S# and SP.P# = P.P#))
Nested queries and multiple queries on the same table:
Find supplier numbers for suppliers who supply at least all those part supplied by S2. NOTE: Nothing outside of SP is needed.
Create view Temp as
( Select P#
from SP
where S# = ‘S2’)
Find numbers of suppliers who supply all parts in temp
Select Distinct S#
From SP SPX
Where NOT EXISTS
( Select *
from Temp
where NOT EXISTS
( Select *
from SP SPY
where SPY.S# = SPX.S# and SPY.P# = Temp.P#))
NOTE: if both of the previous SQL commands are in the same SQL Server window, place the word go between then.
It’s a syntax thing.
Alternative with NO temp table:
Select Distinct S#
From SP SPX
Where NOT EXISTS
( select *
from SP SPY
where S# = ‘S2’ and NOT EXISTS
( Select *
from SP
where S# = SPX.S# and P# = SPY.P#))
This uses aliases SPX and SPY to be able to state conditions related to a specific selection when there is more than one selection on the same table.
Alternative syntax for SQL and join operations
A join operation combines two entries from two (or more) tables based on a condition, typically involving the equality of a common attribute from each table.
See examples starting on page 261.
Sometimes these are called inner joins.
A join or inner join operation concatenates two entries when a common attribute from each has the same value.
However consider:
select *
from S, SP
where S.S# = SP.S#
Where is S8?
If there is an S8 that supplies NO parts, then S8 is not listed.
If there is NO S8 then S8 is not listed.
Should these be distinguished?
Perhaps with an indication that S8 supplies NO parts if there IS an S8?
If not, what we have is OK.
If a distinction is needed, we need an outer join.
See author’s note on page 265 for the significance of the difference.
There are two types of outer joins – a left outer join and a right outer join.
A left outer join is as follows. Suppose the connecting attribute from the left table matches NONE of the connecting attributes from the right table.
Then the entry from the left table is included in the result, with NULLS filling in positions that would have been occupied from a matching entry of the right table
select *
from S left outer join SP
on S.S# = SP.S#
Will show ALL suppliers – even those who supply no parts.
A right outer join is defined analogously
Views
You can create a view, which defines the results of a query.
The view’s definition is stored in the data dictionary so that you don’t have to reenter the SQL each time you need it.
Create view view_name as
select statement……..
To create a view in SQL Server
Right click on the views folder and select New View.
Select the tables or other views you want to include in your view definition. Click the Add button.
You can enter the sql in the text area shown or you can use the gui interface to select your attributes and conditions.
See views from the demo databases for examples.
You can right click on a view and select Design to see its definition.
You just can’t change the view’s definitions
Importing views from other databases
You must first download base tables using the instructions from the previous SQL server powerpoint file
The next powerpoint file will discuss how to import views using script generation.
A view can be updated---sometimes
Views represent data from underlying base tables.
Question: can an update to a view be applied unambiguously to the underlying base table.
In CS451 Supplier you could change the city of a row in the Good Supplier view.
Usually cannot update views that contain aggregate functions or distinct clauses or one in which a single row represents several rows in a base table.
Example: can’t change the name of an entry in the view aboveaveragesupplier.
SQL DML (Data Manipulation Language): Chapter 7
Can use SQL to insert data into tables, modify data in tables, or remove entries from tables.
Some examples follow.
This is extremely useful when writing stored procedures (later).
Insert a record into the S table
insert into S (S#, Sname, City, Status)
values ('S8', 'zyzzy', 'Chicago', 10)
Modify an existing record in the S table
Update S
set status=40
where S# = 'S8'
Delete an existing record from S
delete from S
where S# = 'S8'
SQL is also used to create tables
The DBMS uses the gui with which you work to generate code.