1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
SQL DML
description
Transcript of SQL DML
Data modeling using ER
SQL DML1Quick 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
INSERTUPDATEDELETE
SELECT (simple)SELECT ... ORDER BYSELECT ... GROUP BYSELECT - aggregatesDISTINCT & ALL predicates
Steen Jensen, autumn 2013
Quick recap the SQL way & the visual way in SQL Server Management StudioYou 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 expandedThe Adwentureworks database from the bookThe 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 doesnt 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 slide3Attaching the Adwentureworks database4
Attaching the Adwentureworks database try it5Try to install the Adwentureworks database on your machine
Use max. 15 minutes if it doesnt work, then skip it!A script to create and insert content into Amazon tablesTo 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
6SQL DML - INSERTThe 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 namesWith column names
You can also insert more than one tuple/row at a time (multirow insert)7SQL DML - UPDATEIs used to change/update one or more attributes/columns in a table
Also possible with expressions 8SQL DML - DELETEIs used to delete one or more tuples/rows in a table
NB!!! SQL Server may refuse to delete specific rows due to referential integrity violation9
SQL DML - SELECTIs 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 clause10SELECT WHERE clause operators 111
SELECT WHERE clause operators 212
SELECT ORDER BY clause13The 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 defaultDESC: descending order SELECT GROUP BY clause14The GROUP BY clause is used to aggregate info
Alias
SELECT aggregates15Aggregates 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 groupDISTINCT & ALL predicates16The DISTINCT predicate can be used to avoid showing duplicate tuples/rows The ALL predicate works just the oppsite not very common!
Exercise in SQL DMLExperiment trying out the different SQL DML commands with your Amazon example (and/or The AdwentureWorks database, if it works!)
Try the following commands:INSERTUPDATEDELETE (you can e.g. delete one of the new rows, you insert)SELECTSelect with all attributes/columnsSelect with chosen attributes/columnsSelect with WHERE clauseSelect with ORDER BYSelect with aggregates (GROUP BY see slide 15)Select with DISTINCT
17