1 Quick recap of the SQL & the GUI way in Management Studio The Adwentureworks database from the...

17
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

Transcript of 1 Quick recap of the SQL & the GUI way in Management Studio The Adwentureworks database from the...

Page 1: 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.

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

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

Attaching the Adwentureworks database

4

Page 5: 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.

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!

Page 6: 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.

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

Page 7: 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.

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

Page 8: 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.

SQL DML - UPDATE

Is used to change/update one or more attributes/columns in a table

Also possible with expressions

8

Page 9: 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.

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

Page 10: 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.

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

Page 11: 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.

SELECT – WHERE clause operators 1

11

Page 12: 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.

SELECT – WHERE clause operators 2

12

Page 13: 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.

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

Page 14: 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.

SELECT – GROUP BY clause

14

The GROUP BY clause is used to aggregate info

Alias

Page 15: 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.

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

Page 16: 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.

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!

Page 17: 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.

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