Structured Query Language (SQL)

Post on 30-Dec-2015

37 views 4 download

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

transcript

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) Show examples in ArcMap

Query Language A computer language used in database management

systems to retrieve, add, modify, or delete data

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

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”

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

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

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

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

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

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

Class Demonstration Using characteristics from the class (Person

and Clothing) show how SQL works, conceptually.

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

Query Design View

QBE

Grid

Constructed SQL Statement

Project

Restrict

Join

Application in ArcMap

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?

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)

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

The derived table

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

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

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

The Result

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