Intro to SQL - Week 2

advertisement
IMAT1408 Database Design Concepts
Tutorial handout: Week 2
Introduction to SQL (Structured Query
Language)
************
Learning Outcomes
************
This session will introduce:


Structured Query Language used in databases
How to write simple SELECT statements
*******************************************
Introduction
You may have used Access or another database before. Databases are used to store data,
but that is only half of the story. The user also needs to be able to easily see and use the
data.
For example, if you worked for Premiere Products and needed to send a letter to all your
customers about some special offers, you would not want to look up the customers one at
a time and then write a separate letter to each customer. You would want the database to
extract the names and addresses and supply them to the letter automatically, saving a lot
of time.
If you need information from several tables, then it saves even more time if you get the
database to do the work for you. For example you could get the database to pull out all
the information about one particular order and display it on screen, including the
customer’s name and the details of each product they wanted.
To get the information we require quickly we use something called a query. A query tells
the database what information you want it to find and what to do with it. You may have
used Query By Example (QBE) in Access before but we are going to use SQL, which is a
standard database manipulation language used in most commercial databases as well as
Access.
1
There are several types of query so you need to tell the database what you want to do
with the data:

SELECT means you want the data to be shown on the screen or in a report (a
paper document).

UPDATE means you want to change some of the data.

DELETE means you want to delete or remove some of the rows from a table.
In this module we will concentrate on SELECT statements. The database insists that
information is supplied in a certain order using certain words. This is known as syntax the rules of the language.
For this example we are going to use a database belonging to a book company. The
company owns a number of shops known as branches which sell a range of books to the
public. Systems analysis has been carried out and it has been determined that information
needs to be kept on books, publishers (this is where they buy the books from), branches
and the quantity of each book in each branch (known as the inventory). The table designs
are shown below (there is no data in the tables):
Branch
BranchNum
BranchName
Publisher
PublisherCode
Book
BookCode
Inventory
BookCode
Title
BranchLocation
NumEmployees
PublisherName
City
PublisherCode Type
Price
BranchNum
QuantityInStock
Paperback
The relationships between tables are shown using a foreign key. A foreign key is shown
in italics. That means there is a primary key with the same name in another table which
you can use to look up extra details.
2
IMPORTANT FOR LATER
To write an SQL statement:
1. You need to understand the structure of the database, in particular what the tables
are called, and the names of the attributes in each table.
2. You need to decide what information you want to display. Which attributes are
required, what are their names? Make a list of the attributes you want to see on
the screen or report.
3. Determine which tables these attributes are found in. Make a list of all the tables
involved. (We will start with queries that use just one table).
4. Decide whether you want to see all the rows of the table, or whether you only
want some of them. If you don’t want them all, you will need to tell the database
what conditions to use to select the rows you want. We will do more on
conditions in the lab.
5. You need to test that the query always works correctly, whatever data is put in the
tables. Decide how to do this. This will involve entering data into the tables which
will meet your conditions, and other data that will not meet the conditions. Then
you run the query and check that the correct rows are always selected.
Example 1
Write a query to list all the names and locations of branches owned by the book
company:
Which attributes are required?
--------------------------------------------------------------------------------------------------------------------------Which table(s) are these attributes found in?
--------------------------------------------------------------------------------------------------------------------------Conditions (if any)
---------------------------------------------------------------------------------------------------------------------------
3
Writing the SQL query
To write the SQL statement we need to follow the rules for writing it:
REMEMBER THIS
SQL SELECT statements follow this format:
SELECT {Attribute Names}
FROM {Table Names}
WHERE {Condition};
Words in bold above appear as they are written. The items in italics will be names from
our database.
SELECT is the SQL command that tells the database to find information. This is then
followed by the list of attributes you want to be displayed.
The word FROM followed by the table names tells the DBMS which table(s) to use.
The word WHERE is followed by the conditions that say how to select rows from the
tables - more about this later. If you want the whole table contents you don’t need a
WHERE clause.
All SQL statements end with a semi colon (;). This tells the DBMS that this is the end of
the instructions.
This is the SQL statement for our example
SELECT BranchName, BranchLocation
FROM Branch;
Note: The attribute and table names must be spelt exactly the same as in the database.
When there is more than one attribute, list them in the order you want them to be
displayed, with commas in between the different attributes. (The same applies to tables
when there is more than one). When you complete the lab work for this week you will see
more examples.
Finally we need to think about some data to enter into the Branch table. We obviously
need to have several branches but we don’t need many.
Complete the following table with test data. What you put in to test this query is not
important so long as you have some data. Some queries need more thought, in particular
when there is more than one table involved, or conditions. We will consider this later.
4
Your test data for the Branch table
BranchNum
BranchName
BranchLocation
NumEmployees
Finally you need to create a test plan to record what you expect to happen when the
query is run. With SQL statements a good way is to take a copy of the table with your
data in it and highlight the rows/columns you expect to be selected (in more complex
queries some rows will not be selected). Keep this to one side. This is my test data. I have
highlighted the rows I expect to be selected, in this case all the rows.
BranchNum
B1
B2
B3
BranchName
DE
NTU
DMU
BranchLocation
DERBY
NOTTINGHAM
LEICESTER
NumEmployees
3
4
5
When you run the query take a copy of the answer you get to compare it with the
predicted outcome. If they are the same your query is OK (assuming you worked the data
out correctly). If not, you will need to work out what is wrong.
If the query will not run at all, check the spelling of the names of tables and attributes.
Also check you have commas between lists of attributes and that you spelt SELECT and
FROM correctly and finished the statement with a semi colon.
Exercises
Now it is your turn to try to write some queries. Complete the sheets:
1. List the publisher name followed by the city
Attributes required
------------------------------------------------------------------------------------------------------------Tables attributes found in
-------------------------------------------------------------------------------------------------------------
5
Now write the SQL statement:
SELECT…………………………..
FROM…………………………..
Test data for example 1:
2.
List the titles of the books with their prices, for all books the company sells.
Attributes required
------------------------------------------------------------------------------------------------------------Tables attributes found in
------------------------------------------------------------------------------------------------------------SQL statement
Test data
Try these statements in the lab some time. You can take a copy of the database tables
(without data) from Blackboard. The database is called Books.
6
Adding Conditions to SQL Queries
REMEMBER THIS
Adding conditions to queries
Conditions are used when we do not need all the rows of a database table and wish to
restrict the answer to only those rows we are interested in.
Last week in labs you used the Phones database. You will continue to work on this
database in lab sessions which will show you lots of examples of SQL queries. We are
going to use a version of this database to show some examples of how conditions are
useful. There is a copy of the tables with some test data at the end of this handout.
Example
A customer has just come into the shop to ask if you supply the Nokia phone 6210, if so
they want to know how much it would cost to buy and what features it has.
To write the SQL we want to know if that model (6210) is on our database. We need to
identify what information we are looking for:
Attributes required
ModelNo, Price, Features
Table they will be found in
Phone
Condition
We know we have a table called Phone, which contains information about the
phones we offer to our customers.
We know that the Phone table is made up of 4 attributes and that the attribute
ModelNo contains the model number. We need to know the type of the attribute
to ensure that we specify it correctly. If the attribute is of type Text we need to
surround it by quotes (‘’). In this example ModelNo is of type Text.
We are just seeing if one row of our database contains the ModelNo 6210, so the
condition would be specified as
WHERE ModelNo =’6210’
7
The complete SQL query
SELECT ModelNo, Price, Features
FROM Phone
WHERE ModelNo=’6210’;
Looking at the test data in the tables at the end of this handout, what would you expect to
be the answer from this query?
The WHERE clause looks through all the rows of the database and selects those rows
which meet the condition set. In this example it will only select rows which contain the
value 6210 in the ModelNo column.
How would you change the query for this scenario:
A customer has come into the shop to ask if you stock the Nokia 3670 (it is a new one),
and if so, what the price is.
Query
Expected result of the query
More than one condition
Example
A customer comes in and wishes to know if you stock the Nokia 6210 or the Nokia 6230.
We could use our old query and run it once for the 6210 and then again for the 6230 and
we would be able to answer the customer. Or we could combine both questions into one
query.
The only thing we would need to change is the WHERE clause to select rows of the table
which have a ModelNo 6210 or 6230. We do this by writing the WHERE clause as:
WHERE ModelNo=’6210’ OR ModelNo=’6230’
8
Query
When we use an OR we will select rows which contain value 6210 or 6230 in the
ModelNo column.
How would you find the names and addresses of customers who live in Gondor or
Moria?
Attributes required
Tables required
SQL
Expected output
There is a lot more about conditions in the lab work.
9
Summary of the rules of SQL

SELECT (Attribute
name(s))





FROM (Table name(s) )


There must be one word for each table name. We use [ ] if the
table name is more than one word.
There can be more than one table. Use commas to separate
different tables.

If we wish to select some of the rows of the table (not all) we
need to use the WHERE clause and give the conditions upon
which to select.
AND will only select those rows that are true for all conditions.
OR will select those rows that are true for one of the conditions.
When there is more than one condition we often put each
condition in brackets, to keep them separate.
Check you have complete sets of brackets.

Every SQL query ends with a semi-colon ;
WHERE (Attribute = Value)



;
There must be one word for each attribute. We use [ ] round any
attribute name that is more than one word.
Use commas to separate different attributes.
If we require all the attributes we can use * instead of listing
them all.
The order we list the attributes is the order they will be displayed
in.
If there is more than one table, the name of the attribute is
tablename.attributename
o e.g Phone.Modelno
more about this later.
We use separate lines for each part of the SQL statement as it helps us to read it,
especially if it is complicated. But the DBMS does not need it on separate lines.
Similarly it helps us to read SQL if we put the SQL command words in CAPITALS – so
we say SELECT, not select, and so on. But the DBMS does not need the capitals.
10
Self Study Exercises
1. Phones Database Queries
1. Write the SQL statement to find the address of the customer called Elrond.
2. Write the SQL statement to list all the phones made by Nokia.
3. Write the SQL statement to list all contract types that supply a free phone.
Try running these statements and those you did earlier in the tutorial.
2. Premiere Products Database Queries
Refer back to the Premiere Products database from last week’s tutorial. You should have
some notes on a series of queries. Use these to help you to write SQL to answer the
following questions:
1. List the names of all customers that have a credit limit of at least $10,000.
2. List the descriptions of all parts in item class HW and located in warehouse
number 3.
3. List the order numbers for all orders placed by customer number 608 on October
23, 2003.
Try running these statements. You will need to add data to the tables to check they work
properly. Keep a copy of the SQL statement, a copy of the table with data in it (highlight
the rows you expect to be displayed) and a copy of the actual result.
3. Writing conditions
Many people find conditions difficult to write so you need to practice. All of these
exercises use the phones database but you will need to decide which table is being used.
Exercise 1 – attributes of type Number
Key points
When the attribute contains a number value you can look for values that
 equal a given value using the = symbol
 are greater than a given value using the > symbol
 are less than a given value using the < symbol
 are not equal to a given value using the < and > symbols like this <>
If you combine the > and the = symbols this will return rows which are equal to the value
or greater than the value
The same is true for the < and the = symbols
11
Example
Cost >= 45
This will find rows where the cost attribute contains a value which equals 45 or has a
value greater than 45.
What condition would you use to find :
1.
2.
3.
4.
5.
A phone that costs £100
A phone that costs less than £80
A phone that costs £50 or less
A phone that does not cost £75
Exercise 2 Attributes of type Text
Key points
 When an attribute contains text values you can use the = symbol, or for ‘not
equals’ use <> symbols.
 Text fields must be enclosed in quotes – this tells Access we are looking at a text
based attribute.
 > and < symbols are not used in text fields.
 there are additional things you can use in text based conditions, these are covered
in the lab book.
Example Manufacturer=’Siemens’
This will find the rows where the manufacturer attribute contains a value equal to
Siemens.
What condition would you use to find
 Phones not manufactured by Nokia?
 Customers who live in Leicester?
Exercise 3 Attributes of Type Date
Key points
 When an attribute value is a date the normal number operators will work (<, =,
>=, <=, <>).
 Date values must be enclosed between hash symbols= # . This tell Access that it
is a date value.
 Use month names not month numbers (to avoid problems with the American date
format).
12
Example
Startdate > #1 Jan 2009#
This condition will select all rows with a start date after the 1st January 2009. It will not
include the 1st January 2009.
If you wanted to include the 1st of January 2009 you would need to change the condition
to
Startdate => #1 Jan 2009#
What conditions would you use to
1. Find all agreements started before the 1st December 2008?
2. Find all agreements started after and including the 31st May 2009?
Exercise 4 Dealing with Date Ranges
When we write queries for users we need to be very careful to find out exactly what they
mean so we can translate that into the correct condition. This is particularly true for date
based queries. Users will often ask for queries to be written to find all orders in a certain
month or year.
If I wish to find all agreements taken out in the year 2008 I am in fact looking for all
agreements with a start date between the 1st January 2008 and the 31st December 2008
including those two dates. The condition would look like this.
Startdate > =#1 Jan 2008# and Startdate <= #31 Dec 2009#
What conditions would you use to
1. Find all agreements started after the 30 April 2008
2. Find all agreements started during the month of May 2008
3. Find all agreements ended during 2008
13
Phones Database
Customer table
CustomerNo Name
1 Frodo
2 Sam
3 Gimli
4 Elrond
5 Aragorn
6 Eowyn
Street
1 Bag End
3 Bag End
The Caves
Rivendell
The Citadel
The hall
Town
Hobbiton
Hobbiton
Moria
The Valley
Gondor
Rohan
Phone table
ModelNo
3400
4500
6210
6230
6240
Manufacturer
Siemens
Siemens
Nokia
Nokia
Nokia
Features
Camera
Camera
Camera
Colour screen
Camera and colour
screen
Cost
£78.00
£45.00
£89.00
£78.00
£100.00
ContractType table
ContractTypeNo
Description
MonthlyCost Freephone
1 Pay as you go
£0.00
False
2 60 mins free calls a month
£20.00
True
3 30 mins free calls a month
£15.00
True
4 60 mins and all texts free
£25.00
True
5 free calls and texts
£50.00
True
Agreement table
ContractNo CustomerNo ModelNo PhoneNo StartDate EndDate ContractTypeNo
1
1 6210
0794657890 12/05/03
1
2
2 6230
0794623432 04/05/04
1
3
4
5
3 3400
3 4500
3 6210
0794634567 03/04/02 03/04/03
0794656432 03/04/03 03/04/04
0794656432 03/04/04
4
3
2
6
4 6240
0794698765 12/12/03
5
7
5 6240
0794621321 14/05/04
2
14
Download