1 Quick recap of the SQL & the GUI way in Management Studio The Adwentureworks database from the...
-
Upload
darby-jared -
Category
Documents
-
view
215 -
download
1
Transcript of 1 Quick recap of the SQL & the GUI way in Management Studio The Adwentureworks database from the...
SQL DML
1
•Quick recap of the SQL & the GUI way in Management Studio
•The Adwentureworks database from the book
•A script to create tables & insert data for the Amazon example
•INSERT•UPDATE•DELETE
•SELECT (simple)•SELECT ... ORDER BY•SELECT ... GROUP BY•SELECT - aggregates•DISTINCT & ALL predicates
Steen Jensen, autumn 2013
Quick recap the SQL way & the visual way in SQL Server Management Studio
You can either type in SQL commands in the Query window
Or you can do it visually by right-clicking
A small live demo!
2
New database ’Demo’ created and expanded
The Adwentureworks database from the book
The Adwentureworks database can be downloaded, so all examples from the book can be tried out
Unfortunately there seems to be a problem with Windows 8 – it works fine on my old Windows 7 machine, but doesn’t work on Windows 8
The database can be unzipped from Adwentureworks database
Some good advice:• Unpack the zip file into the C drive (avoid long/deep paths)• After you have unpacked the database, you can attach it in
SQL Server Studio by following the screenshots shown in the next slide
3
Attaching the Adwentureworks database
4
Attaching the Adwentureworks database – try it
5
Try to install the Adwentureworks database on your machine
Use max. 15 minutes – if it doesn’t work, then skip it!
A script to create and insert content into Amazon tables
To help you get up an running quickly with your Amazon example, you can execute a script, which will create the necessary tables for you and put content into them (if you prefer to make it yourself, this is perfectly fine!)
Before you can execute the script, you must make a database and call it Amazon
Open a new query window and copy paste the content from the file booksCreateInsert.txt into the query window
Press on the execute button, and the tables will now be created with content
If you expand the Amazon database, you should be able to see the new tables under Tables
6
SQL DML - INSERT
The following slides will be based upon the examples, which can be unzipped from the file 102282 Beginning SQL - Final.zip – all examples will be from the file Chap03.sql
Is used to insert new tuples/rows into a table
In two flavours:• Without column names• With column names
You can also insert more than one tuple/row at a time (multirow insert)
7
SQL DML - UPDATE
Is used to change/update one or more attributes/columns in a table
Also possible with expressions
8
SQL DML - DELETE
Is used to delete one or more tuples/rows in a table
NB!!! SQL Server may refuse to delete specific rows due to referential integrity violation
9
SQL DML - SELECT
Is used to select/show one or more attribute(s) / column(s) from a table
This is called a simple select – also possible with two or more tables join (covered next time)
SELECT * FROM ...: the asterisk (*) means select all attributes/columns
A select will automatically select all tuples/rows, unless you add a WHERE clause
10
SELECT – WHERE clause operators 1
11
SELECT – WHERE clause operators 2
12
SELECT – ORDER BY clause
13
The ORDER BY clause is used to order the shown tuples/rows in a specific order
You can add the keyword ASC or DESC after ORDER BY to specify the order :• ASC: ascending order – default• DESC: descending order
SELECT – GROUP BY clause
14
The GROUP BY clause is used to aggregate info
Alias
SELECT – aggregates
15
Aggregates are functions, which work on groups of data
Apart from GROUP BY other functions exist:• AVG – computing averages
• MIN and MAx selects the smallest/biggest value within a group
• COUNT counts the number of occurrences within a group
• HAVING can be used to specify limitations/rules for a group
DISTINCT & ALL predicates
16
The DISTINCT predicate can be used to avoid showing duplicate tuples/rows
The ALL predicate works just the oppsite – not very common!
Exercise in SQL DML
Experiment trying out the different SQL DML commands with your Amazon example (and/or The AdwentureWorks database, if it works!)
Try the following commands:• INSERT• UPDATE• DELETE (you can e.g. delete one of the new rows, you insert)• SELECT
Select with all attributes/columns Select with chosen attributes/columns Select with WHERE clause Select with ORDER BY Select with aggregates (GROUP BY … see slide 15) Select with DISTINCT
17