25
Structured Query Language (SQL) Lecture 8 CONS 340

Structured Query Language (SQL)

Embed Size (px)

DESCRIPTION

Structured Query Language (SQL). Lecture 8 CONS 340. Learning Objectives. Synthesize database concepts from previous weeks E/R Diagrams to Database Demonstrate concepts of database query Learn how to construct a SQL statement Use Relational Query By Example (RQBE ) - PowerPoint PPT Presentation

Citation preview

Page 1: Structured  Query Language (SQL)

Structured Query Language (SQL)

Lecture 8

CONS 340

Page 2: Structured  Query Language (SQL)

Learning Objectives Synthesize database concepts from previous weeks

E/R Diagrams to Database Demonstrate concepts of database query Learn how to construct a SQL statement

Use Relational Query By Example (RQBE) Show examples in ArcMap

Page 3: Structured  Query Language (SQL)

Query Language A computer language used in database management

systems to retrieve, add, modify, or delete data

Page 4: Structured  Query Language (SQL)

Computer Languages

First generation -- machine language

Byte level – 01100101 Second generation -- assembly language

A close approximation of machine language – ASCII10011011 = e

Third generation -- “high level” languages Procedural Language – Linear programming –

Visual Basic, Java – must define objects and actions

Fourth generation -- “4GL” languagesCloser to data, more like spoken languages – SQL

machine

data

Page 5: Structured  Query Language (SQL)

SQL: Structured Query Language Standard language used with relational databases to

build complex logical expressions to access data Developed by IBM in the 1970s, has become an

industry standard Considered to be a “4GL”

Page 6: Structured  Query Language (SQL)

Basic SQL Statements SELECT column_list FROM table_list {WHERE

where_clause} {ORDER BY column_list} {GROUP BY column_list} Retreive data from one or more tables in the database

INSERT source INTO target(columns) VALUES (value_list) Insert a tuple (row) into a table in the database

UPDATE target SET col_val_pair_list {WHERE where_clause} Modify data of one or more tuples in a table

DELETE FROM table {WHERE where_clause} Delete a tuple from a table

Page 7: Structured  Query Language (SQL)

E.F. Codd’s 8 Operators

Traditional Union Intersection Difference Product

Special Restrict Project Join Divide

1970, A Relational Model of Data for Large Shared Data Banks, in Communications of the ACM, Vol. 13, No. 6, June 1970, pp. 377-387

Page 8: Structured  Query Language (SQL)

SELECT statement For user’s intents and purposes the SELECT

statement is often used The basic structure is as follows:

SELECT column(s) FROM table(s) WHERE clause(s)

Project

Join

Restrict

Page 9: Structured  Query Language (SQL)

Project

Returns a relation consisting of all tuples that remain as (sub)tuples in a specified relation after specified attributes have been eliminated.

Students

Students [Name, Major]

city

Stu

dent

_no

Age

GP

A

Gen

der A

ddre

ss

Nam

e Maj

or

Returns a relation of the Name and Major attributes of the Students relation

Page 10: Structured  Query Language (SQL)

Join

Returns a relation consisting of all possible tuples that are a combination of two tuples, one from each of two specified relations, such that the two tuples contributing to any given combination have a common value for the common attribute(s) of the two relations (and that common value appears just once, not twice, in the resulting tuple).

a1 b1 a2 b1 a3 b2

b1 c1 b2 c2 b3 c3

a1 b1 c1 a2 b1 c1 a3 b2 c2

Join

Common Attributes

a2 b2

a1 b3

a2 b2 c2

a1 b3 c3

Page 11: Structured  Query Language (SQL)

Restrict

Returns a relation consisting of all tuples from a specified relation that meet a specified condition. Usually expressed as a WHERE clause.

S

Rome

London

Paris

London

London

Madrid

London

Rome

London

London

London

LondonS WHERE City = London

Page 12: Structured  Query Language (SQL)

Class Demonstration Using characteristics from the class (Person

and Clothing) show how SQL works, conceptually.

Page 13: Structured  Query Language (SQL)

Apply this with a RDBMS in RQBE Relational Query by Example A simpler method of creating queries Generates SQL behind the scenes Facilitated by specific software interfaces

Page 14: Structured  Query Language (SQL)

Query Design View

QBE

Grid

Page 15: Structured  Query Language (SQL)

Constructed SQL Statement

Project

Restrict

Join

Page 16: Structured  Query Language (SQL)

Application in ArcMap

Page 17: Structured  Query Language (SQL)
Page 18: Structured  Query Language (SQL)

Who manages which park? We need to know how to join tables in ArcMap

so we can connect the park contact with the park itself… How do we join tables?

Page 19: Structured  Query Language (SQL)

Joining and SELECT in ArcMap Rarely will you need to know the full SELECT

statement while using ArcMap So, how would we join two tables without

using standard SELECT statement? Remember: SELECT column(s) FROM table(s) WHERE clause(s)

Page 20: Structured  Query Language (SQL)

Join Without using standard

SELECT statement we must simulate the Join first

Once completed we have a new table containing linked data with both original tables

Page 21: Structured  Query Language (SQL)

The derived table

The new table keeps the original table name, but data is not permanently linked.

Page 22: Structured  Query Language (SQL)

Another SELECT example Let’s use the newly joined table to find parks

not controlled by Mr. Jones or not controlled by Ms. Cavallaro?

Page 23: Structured  Query Language (SQL)

The Result

Page 24: Structured  Query Language (SQL)

The other operators in ArcMap Insert

Start typing in blank space

Update Start modifying data

Delete From Right click or SELECT and

press delete key

Page 25: Structured  Query Language (SQL)