BTD210 Lab #3 Winter 2011 If you previously selected the tables of

advertisement
BTD210
Lab #3
Winter 2011
1.
If you previously selected the tables of the Premiere database “into” your account, please
drop these tables now.
2. Check your e-mail. I have sent you the SQL script to create and populate the tables of
the Premiere database.
3. Copy and paste this script into your SQL server account. You should have 5 tables
populated with data.
4. A review of the script will tell you that the only ‘constraint’ so far accounted for is entity
integrity (primary keys) . The other constraints that could be included with this database
are for referential integrity (Foreign Keys), the check constraint (validation) and the
unique constraint.
Add foreign keys: (after a table has already been created, script is as below)
Alter table customer
Add foreign key (rep_num) references rep(rep_num)
There are 3 more foreign keys to be scripted. Do those now.
5. A review of the tables (see Parts table) should indicate data to be validated. For
instance, there are only 3 warehouses. You can insert a check constraint to ensure that
only warehouse 1, 2, or 3 will be allowed. In addition, there are only 3 classes of parts.
A check constraint can also be written to validate these 3 choices.
Alter table part
Add check (warehouse in (1,2,3))
Write the script that produces a check constraint for the warehouses.
Review the tables to determine if a unique constraint exists. If you find one, the script will
be as follows:
Alter table xxx
Add unique (<attributename>)
6. If you want to query more than one table at a time:
Eg: List the number and name of each customer together with the number, last name and first
name of the sales rep who represents the customer.
Because the customer names and numbers are in the customer table, and the rep numbers and
names are in the rep table, you need to include both tables in the sql command so you can
retrieve data from both tables.
The table is constructed as follows:
1. In the Select clause, list all the columns you want to display
2. In the From clause, list all tables involved in the query
3. In the where clause, list the condition that restricts the data to be retrieved to only those
rows from the two tables that match; that is, restrict it to the rows that have common
values in matching columns.
With these points in mind, the command query to be written for the example above is as follows:
Select customer.customer_num, customer.customer_name, rep.rep_num, rep.last_name,
rep.first_name
From customer, rep
Where customer.rep_num = rep.rep_num
Note: because this is a long and unwieldy process, we can use an alias for the table names
Select c.customer_num, c.customer_name, r.rep_num, r.last_name, r.first_name
From customer c, rep r
Where c.rep_num = r.rep_num
Write the following queries:
a) For each order, list the order number and order date along with the number and name of
customer who placed that order.
b) For each order, list the order number, order date, part number, number of units ordered,
and quoted price for each order line that makes up the order.
c) Find the number and name of each customer that did NOT place an order on October 23,
2010.
d) For each order, list the order number, order date, part number, part description, and item
class for each part that makes up the order; order the rows by item class and then by order
number
e) Find the number and name for each customer that currently has an order on file for a
range.
Copy and paste your scripts for #4,5,6 to my e-mail account. And as always, please test your
scripts in SQL server.
Download