Our First Lab
SQL Server Software
Dreamspark
Steps have been taken
Logging into SQL Server
Select the Management Studio.
This is where well do most of our
SQL Server work.
Connecting to the database server
Write busmorpheus\teamroom in the
Server name area.
Select Windows Authentication
It should automatically insert your
identikey username and pw
Click on Connect
Northwind DB
Examine Object
Explorer
Note list of tables
Entities!
Customers
Examine Object
Explorer
Note list of tables
Entities!
Note columns folder
PK = Primary key
Customers
Orders
Examine Object
Explorer
Note list of tables
Entities!
Note columns folder
PK = Primary key
Now: Orders
Note FK = Foreign key
SQL (Structured Query Language)
DML (Data Manipulation Language)
First we need a window for typing in our queries
Select New Query from upper left of menu
DML
SELECT
Used to retrieve data from the database
INSERT
Used to add data to the database
UPDATE
Used to change existing records in the
database
DELETE
you get it
NOTE: Make sure you have
the right database
Select
Select
* Means all columns in the customers table
Another way of finding all data
Right-click on employees table and select Select
Top 1000 rows
Get help with your queries
Notice that this
provided you with a
script suggesting how
to select records from
that table.
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP 1000 [EmployeeID]
,[LastName]
,[FirstName]
,[Title]
,[TitleOfCourtesy]
,[BirthDate]
,[HireDate]
,[Address]
,[City]
,[Region]
,[PostalCode]
,[Country]
,[HomePhone]
,[Extension]
,[Photo]
,[Notes]
,[ReportsTo]
,[PhotoPath]
FROM [Northwind].[dbo].[Employees]
select * from customers
where country = 'Norway'
By using the WHERE
clause, we restrict the
number of rows returned to
only those of interest
Note: This is clearly a substandard company since they
have only managed to attract
one Norsk customer
SELECT customerid
, contactname
FROM customers
WHERE country = 'Norway'
By specifying which
columns (think attributes
from your ER diagram), you
focus in on what is of
interest to you.
Answer page: 1 + 2
Test yourself!
Create a select statement that tells you
the names of all employees with employee
id over 7.
Create a select statement that tells you all
information about customers who are
owners and from Mexico.
Hint: owner information is somewhere in the
customer table.
Logic
You can use these:
AND
OR
where city = 'Berlin' OR city = 'London'
IN
WHERE country IN ('Mexico', 'Norway')
NOT
where contacttitle NOT IN ('owner, general
manager)
NOT is used with text operators
Operators
Operators:
=
LIKE
<>
numbers and direct string comparisons
strings where wildcards are used
same as not for numerical data
IS
when looking for null values
Aggregates
Often requires a GROUP BY statement so that we can
find the SUM, COUNT, MAX, or AVG for the group
SELECT AVG(quantity)
FROM [order details]
Or if you want the sum for each order (remember, this
table has multiple records per order):
SELECT orderid, SUM(quantity)
FROM [order details]
GROUP BY orderid
Aggregates
Often requires a GROUP BY statement so that we
can find the SUM, COUNT, MAX, or AVG for the
group
SELECT AVG(quantity)
FROM [order details]
Or if you want the sum for each order (remember, this table
has multiple records per order):
SELECT orderid, SUM (quantity)
FROM [order details]
The item(s) in the column list that are not
aggregated must be listed in a GROUP
GROUP by orderid
BY statement towards the end of the
query.
Now try these with different aggregates as well as adding
productid to the analysis (not as an aggregate)
Insert
INSERT INTO Categories
(CategoryName
,Description
,Picture)
VALUES
(NorwegianStuff
,'Caviar in a tube'
,''
Two individual
quotes
Try it outwith your own
category name/description
Update
UPDATE Categories
SET Description = 'DELICIOUS
Caviar in a tube'
WHERE categoryid = 9
Delete
DELETE FROM categories
WHERE categoryid = 9
Next Classes
Entity Relationship Diagramming, then
Joins
Homework
Try the commands out at http://sqlzoo.net/
Make sure you select SQL Server as the database
type
Do as many of the assignments as you can:
1) SELECT basics (1-5 only),
2) SELECT from World and
3) SELECT from Nobel
For those you cannot do, explain how you would logically
accomplish that goal even if you cant do it with SQL. For 3), do
not attempt #8. (individual homework)
Do screenshots of your query and paste into MS
Word before printing and turning in.
See example to the right for example screenshot. On a Mac,
use Command+SHIFT+4. On PC use Snippingtool
Explore the AdventureWorks database on
teamroom.colorado.edu
Use SELECT to understand the content of the tables
Nothing to turn in
Read Lecture 03. ERDiagrams.pdf
Teams do all exercises in chapters 2+3 and turn in next class
If an exercise is impossible due to changes in a website since publication of the
book, use an equivalent website such as IMDB.com
Answer Page
1:
SELECT employeeid, firstname, lastname
from employees
where employeeid > 7
2:
select * from customers
where contacttitle = 'owner'
and country = 'Mexico'