28
Data Modeling Seminar February 18, 2012 Lesson 3 Standard SQL

February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Embed Size (px)

Citation preview

Page 1: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Data ModelingSeminar

February 18, 2012

Lesson 3Standard SQL

Page 2: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Lesson 3

Standard SQL

Page 3: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Standard SQL Data Statements – query and modify tables and columns

SELECT Statement – query tables and views in the database INSERT Statement – add rows to tables UPDATE Statement – modify columns in table rows DELETE Statement – remove rows from tables

Schema Statements – maintain schema (catalog) CREATE TABLE Statement – create tables CREATE VIEW Statement – create views DROP TABLE Statement -- drop tables DROP VIEW Statement -- drop views

Constraints & Programmability Constraints Procedures Function Triggers

Page 4: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Standard SQL – Data Statements SELECT Statement -- query tables and views in the database

SELECT Statement Basics SELECT Clause FROM Clause WHERE Clause

Extended Query Capabilities ORDER BY Clause Value Expressions Joining Tables Subqueries Grouping Queries (GROUP BY) Aggregate Queries (Set Functions) Union Queries (UNION)

SQL Modification Statements INSERT Statement -- add rows to tables VALUES Clause

UPDATE Statement -- modify columns in table rows SET Clause

DELETE Statement -- remove rows from tables

Page 5: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

SELECT Statement BasicsThe SQL SELECT statement queries data from tables in the

database. The statement begins with the SELECT keyword. The basic SELECT statement has 3 clauses:

SELECT FROM WHERE

The SELECT clause specifies the table columns that are retrieved. The FROM clause specifies the tables accessed. The WHERE clause specifies which table rows are used.

The WHERE clause is optional; if missing, all table rows are used. For example, SELECT name FROM s WHERE city='Rome'• This query accesses rows from the table - s. • It then It then filters those rows where the city column contains Rome. • Finally, the query retrieves the name column from each filtered row.

Page 6: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

SELECT - Extended Query Capabilities Sorting Query Results -- using the ORDER BY clause Expressions -- in the SELECT clause and WHERE clause

Literal -- self-defining values Function Call -- expression functions System Value -- builtin system values Special Construct -- special expression construct Numeric or String Operator -- expression operators

Joining Tables -- in the FROM clause Outer Join -- extended join Self Join -- joining a table to itself

Subqueries -- embedding a query in another Predicate Subqueries -- subqueries in logical expressions Scalar Subqueries -- subqueries in scalar expressions Table Subqueries -- subqueries in the FROM clause

Grouping Queries -- using the GROUP BY clause, Set Function and HAVING clause GROUP BY Clause -- specifying grouping columns Set Functions -- summary functions HAVING Clause -- filtering grouped rows

Aggregate Queries -- using Set Functions and the HAVING clause Union Queries -- using the query operator, UNION

Union-Compatible Queries -- query requirements for Union

Page 7: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Joining TablesThe FROM clause allows more than 1 table in

its list, however simply listing more than one table will very rarely produce the expected results.

The rows from one table must be correlated with the rows of the others.

This correlation is known as joining.

Page 8: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Join ExampleAn example can best illustrate the rationale behind joins. The following query:

SELECT * FROM sp, p

Produces:

Page 9: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

sno pno qty pno descr color

S1 P1 NULL P1 Widget Blue

S1 P1 NULL P2 Widget Red

S1 P1 NULL P3 Dongle Green

S2 P1 200 P1 Widget Blue

S2 P1 200 P2 Widget Red

S2 P1 200 P3 Dongle Green

S3 P1 1000 P1 Widget Blue

S3 P1 1000 P2 Widget Red

S3 P1 1000 P3 Dongle Green

S3 P2 200 P1 Widget Blue

S3 P2 200 P2 Widget Red

S3 P2 200 P3 Dongle Green

SELECT * FROM sp, p

Each row in sp is arbitrarily combined with each

Page 10: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Inner Join ExampleA more usable query would correlate the

rows from sp with rows from p, for instance matching on the common column -- pno:

SELECT * FROM sp, p WHERE sp.pno = p.pno

sno pno qty pno descr color

S1 P1 NULL P1 Widget Blue

S2 P1 200 P1 Widget Blue

S3 P1 1000 P1 Widget Blue

S3 P2 200 P2 Widget Red

This produces:

Page 11: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Outer JoinsAn outer join provides the ability to include unmatched rows in the

query results. The outer join combines the unmatched row in one of the tables with

an artificial row for the other table. This artificial row has all columns set to null.

The outer join is specified in the FROM clause and has the following general format: table-1 { LEFT | RIGHT | FULL } OUTER JOIN table-2 ON predicate-1

predicate-1 is a join predicate for the outer join. It can only reference columns from the joined tables.

The LEFT, RIGHT or FULL specifiers give the type of join: LEFT -- only unmatched rows from the left side table (table-1) are retained RIGHT -- only unmatched rows from the right side table (table-2) are

retained FULL -- unmatched rows from both tables (table-1 and table-2) are

retained

Page 12: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Outer Join Example: SELECT pno, descr, color, sno, qty FROM p LEFT OUTER JOIN sp ON p.pno = sp.pno

pno descr color sno qty

P1 Widget Blue S1 NULL

P1 Widget Blue S2 200

P1 Widget Blue S3 1000

P2 Widget Red S3 200

P3 Dongle Green NULL NULL

Page 13: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

INSERT StatementThe INSERT Statement adds one or more rows to a table. It has two formats:

INSERT INTO table-1 [(column-list)] VALUES (value-list)&INSERT INTO table-1 [(column-list)] (query-specification)

The first form inserts a single row into table-1 and explicitly specifies the column values for the row.

The second form uses the result of query-specification to insert one or more rows into table-1. The result rows from the query are the rows added to the insert table. Note: the query cannot reference table-1.

Page 14: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

UPDATE StatementThe UPDATE statement modifies columns in selected table rows. It has the following general format:

UPDATE table-1 SET set-list [WHERE predicate]

The optional WHERE Clause has the same format as in the SELECT Statement. The WHERE clause chooses which table rows to update. If it is missing, all rows are in table-1 are updated.

Page 15: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

SET ClauseThe SET Clause in the UPDATE Statement updates (assigns new value to) columns in the selected table rows. It has the following general format:

SET column-1 = value-1 [, column-2 = value-2] ...

column-1 and column-2 are columns in the Update table.

value-1 and value-2 are expressions that can reference columns from the update table. They also can be the keyword -- NULL, to set the column

to null.

Page 16: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

DELETE StatementThe DELETE Statement removes selected rows from a table. It has the following general format:

DELETE FROM table-1 [WHERE predicate]

The optional WHERE Clause has the same format as in the SELECT Statement.

The WHERE clause chooses which table rows to delete. If it is missing, all rows are in table-1 are removed.

The WHERE Clause predicate can contain subqueries, but the subqueries cannot reference table-1. This prevents situations where results are dependent on

the order of processing.

Page 17: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Schema Statements Maintain Schema (catalog)

CREATE TABLE Statement – create tablesCREATE VIEW Statement – create viewsDROP TABLE Statement – drop tables DROP VIEW Statement – drop views

Page 18: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

ConstraintsCheckUniquePrimary KeyForeign KeyNot Null

Page 19: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

CHECK constrains A constraint is an object which tells the DBMS: don't permit updates of the

database which would break rules established for business/integrity reasons.

A CHECK constraint works thus:

CREATE TABLE TABLE_1 (COLUMN_1 SMALLINT); -- make the table

ALTER TABLE TABLE_1 ADD CONSTRAINT CONSTRAINT_1 CHECK (COLUMN_1 > 0); -- make the constraint

INSERT INTO TABLE_1 VALUES (5); -- this succeeds

UPDATE TABLE_1 SET COLUMN_1 = -5; -- this fails

Page 20: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

PRIMARY KEY constraintA PRIMARY KEY constraint works thus:

CREATE TABLE TABLE_1 (COLUMN_1 SMALLINT);

ALTER TABLE TABLE_1 ADD CONSTRAINT CONSTRAINT_1 PRIMARY KEY (COLUMN_1);

INSERT INTO TABLE_1 VALUES (5);

INSERT INTO TABLE_1 VALUES (5); • A table's primary key must contain unique values, so the second

INSERT in this example will fail. • Alternative: if all we want to say is "values must be unique", we

could say UNIQUE (COLUMN_1) instead of PRIMARY KEY (COLUMN_1).

Page 21: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

FOREIGN KEY constraintA FOREIGN KEY constraint works thus:

CREATE TABLE TABLE_1 (COLUMN_1 SMALLINT);

ALTER TABLE TABLE_1 ADD CONSTRAINT CONSTRAINT_1 PRIMARY KEY (COLUMN_1);

INSERT INTO TABLE_1 VALUES (5);

CREATE TABLE TABLE_2 (COLUMN_1 SMALLINT);

ALTER TABLE TABLE_2 ADD CONSTRAINT FOREIGN KEY (COLUMN_1) REFERENCES TABLE_1;

INSERT INTO TABLE_2 VALUES (5);

INSERT INTO TABLE_2 VALUES (6);

• The first INSERT into TABLE_2 will succeed, because our foreign-key value (5) exists in the primary-key table that we're referencing.

• But we never inserted a 6 into TABLE_1, so we can't insert a 6 into TABLE_2, so the second INSERT in this example will fail.

Page 22: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

ProgrammabilityStored ProcedureFunction

Scalar valueTable value

Trigger

Page 23: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Stored Procedure

Page 24: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Function – Scalar value

Page 25: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Function – Table value

Page 26: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Trigger

Page 27: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Questions

Page 28: February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL

Lab 2Class Scheduling System in SQL Server

Create ClassSchedule databaseLoad dataExecute Queries