Sei sulla pagina 1di 35

Structure Query Language

Background
History
SEQUEL (Structures English QUery Language) early 70s, IBM Research SQL (ANSI 1986), SQL1 or SQL86 SQL2 or SQL92

SQL3 or SQL99
Core specification and optional specialized packages

SQL consists of ~20 basic commands


A lot of research money for each SQL command

Standard language for all commercial DBMS

Why a query language?


Given some data,
how should users
and computer programs communicate with it?

we need an interface to the data

One more example


Show the resulting salaries if every employee working on the ProductX project is given a 10 percent raise

SELECT FNAME, LNAME, 1.1*SALARY AS INC_SAL FROM EMPLOYEE, WORKS_ON,


PROJECT WHERE SSN=ESSN AND PNO=PNUMBER

SQL join
Recall this query
SELECT FROM WHERE EMPLOYEE.LNAME, ADDRESS EMPLOYEE, DEPARTMENT DEPARTMENT.NAME=Research AND DNUMBER=DNO;

Equijoin
of employee and department tables w.r.t. employee.dnumber and department.dno.

Joins are cartesian products with some selection criteria.

SQL and the relational data model


SELECT FROM WHERE

Assignment operator
Rename relations

projection, cartesian product,

Join join

selection
Set operations
Union

Equijoin
Natural join
Operands must be union compatible

Difference
Intersection

Basic query statement: select from where


SELECT A1, A2, , An

FROM r1, r2, , rm


WHERE P;
A1, A2, , An list of attribute names to be retrieved
r1, r2, , rm List of tables required to process the

query
P Conditional expression identifying the tuples to be

retrieved
AND, OR, NOT, <, <=, =, >=, >

Result of the query is a table

Terminology
Theoretical foundation:
relation

The relational data model


relation tuple attribute table row column

Attribute1

Attribute2

Attribute2

column1

columnn <row 2>

<row n>

An Overview of SQL
SQL stands for Structured Query Language. It is the most commonly used relational database language today. SQL works with a variety of different fourthgeneration (4GL) programming languages, such as Visual Basic.

NULL
Find out who is The Big Boss
select fname, lname

from employee
where superssn is NULL;

Sometimes an attribute is
Unknown Unavailable/withheld #) Not applicaple (date of birth unknown) (refuses to list home phone (last college degree)

NULL values

Need to represent these cases in a DB! Solution: NULL.


What about logical operations involving NULL?
Need to extend logic

Processing Capabilities of SQL


Data definition language-The SQL DDL provides commands for defining relation schemas, deleting relations creating indexes, and modifying relation schemas. Interactive Data Manipulation LanguageThe SQL DML includes a query language based on both the relational algebra and the tuple relational calculus.
12

Embedded Data Manipulation LanguageThe Embedded form of SQL is designed for use within general purpose programming languages such as PL/1,Cobol,Fortan,Pascal and C. View Definition-The SQL DDL also includes commands for defining views.

Authorization-The SQL DDL includes commands for specifying access rights to relations and views.

13

Integrity-The SQL provides limited forms of integrity checking. Future products and standards of SQL are likely to include enhanced features for integrity checking.
Transaction Control-SQL includes commands for specifying the beginning and ending of transactions along with commands to have a control over a transaction processing.
14

SQL is used for:


Data Manipulation
Data Definition Data Administration All are expressed as an SQL statement or command.

15

SQL Requirements
SQL Must be embedded in a programming language, or used with a 4GL like VB
SQL is a free form language so there is no limit to the the number of words per line or fixed line break. Syntax statements, words or phrases are always in lower case; keywords are in uppercase.
16

SQL is a Relational Database


A Fully Relational Database Management System must:
Represent all info in database as tables
Keep logical representation of data independent from its physical storage characteristics Use one high-level language for structuring, querying, and changing info in the database Support the main relational operations Support alternate ways of looking at data in tables

Provide a method for differentiating between unknown values and nulls (zero or blank)
Support Mechanisms for integrity, authorization, transactions, and recovery 17 Brad Lloyd & Michelle Zukowski

SQL

Design
SQL represents all information in the form of tables
Supports three relational operations: selection, projection, and join. These are for specifying exactly what data you want to display or use

SQL is used for data manipulation, definition and administration


18

Table Design
Columns describe one characteristic of the entity Rows describe the Occurrence of an Entity

Name Jane Doe John Smith Mary Poe

Address 123 Main Street 456 Second Street 789 Third Ave

19

Data Retrieval (Queries)


Queries search the database, fetch info, and display it. This is done using the keyword SELECT SELECT * FROM publishers
pub_id
0736 0987 1120

pub_name
New Age Books Binnet & Hardley Algodata Infosys

address
1 1st Street 2 2nd Street 3 3rd Street

state
MA DC CA

The Operator asks for every column in the table.

20

Data Retrieval (Queries)


Queries can be more specific with a few more lines
SELECT * from publishers where state = CA
pub_id
0736 0987 1120

pub_name
New Age Books Binnet & Hardley Algodata Infosys

address
1 1st Street 2 2nd Street 3 3rd Street

state
MA DC CA

Only publishers in CA are displayed


21

Data Input
Putting data into a table is accomplished using the keyword INSERT
Variable INSERT INTO publishers VALUES (0010, pragmatics, 4 4th Ln, chicago, il) Keyword
pub_id 0736 0010 0987 0736 1120 0987 1120 pub_name New Age Books Pragmatics Binnet & Hardley New Age Books Algodata Infosys Binnet & Hardley Algodata Infosys address
st Street 11 4 4th Ln nd Street 21 2st Street 1 rd Street 32 3nd 2 Street

state MA IL DC MA CA DC CA

3 3rd Street

Table is updated with new information


22

Types of Tables
There are two types of tables which make up a relational database in SQL
User Tables: contain information that is the database management system System Tables: contain the database description, kept up to date by DBMS itself
pub_id 0010 0736 0987 1120 pub_name address 4 4th Ln 1 1st Street 2 2nd Street 3 3rd Street state IL MA DC CA Pragmatics New Age Books Binnet & Hardley Algodata Infosys

Relation Tuple

Table Row

Attribute

Column

Using SQL
SQL statements can be embedded into a program (cgi or perl script, Visual Basic, MS Access)
OR SQL statements can be entered directly at the command prompt of the SQL software being used (such as mySQL)

24

Using SQL
To begin, you must first CREATE a database using the following SQL statement:

CREATE DATABASE database_name


Depending on the version of SQL being used the following statement is needed to begin using the database:

USE database_name

25

Using SQL
To create a table in the current database, use the CREATE TABLE keyword CREATE TABLE authors (auth_id int(9) not null, auth_name char(40) not null)
auth_id (9 digit int) auth_name (40 char string)

26

Using SQL
To insert data in the current table, use the keyword INSERT INTO
INSERT INTO authors values(000000001, John Smith)

Then issue the statement


SELECT * FROM authors
auth_id 000000001 auth_name John Smith
27

Using SQL
If you only want to display the authors name and city from the following table:
auth_id auth_name auth_city Dearborn Taylor auth_state MI MI 123456789 Jane Doe 000000001 John Smith

SELECT auth_name, auth_city FROM publishers


auth_name Jane Doe
John Smith

auth_city Dearborn
Taylor
28

Using SQL
To delete data from a table, use the DELETE statement: DELETE from authors WHERE auth_name=John Smith
auth_id auth_name auth_city Dearborn Taylor auth_state MI MI

123456789 Jane Doe 000000001 John Smith

29

Using SQL
To Update information in a database use the UPDATE keyword UPDATE authors SET auth_name=hello
auth_id auth_name auth_city Dearborn Taylor auth_state MI MI

Hello Doe 123456789 Jane Hello Smith 000000001 John

Sets all auth_name fields to hello


30

Using SQL
To change a table in a database use ALTER TABLE. ADD adds a characteristic.
Type Initializer ALTER TABLE authors ADD birth_date datetime null

auth_id

auth_name

auth_city Dearborn Taylor

auth_state MI MI

birth_date . .

123456789 Jane Doe 000000001 John Smith

ADD puts a new column in the table called birth_date


31

Using SQL
To delete a column or row, use the keyword DROP ALTER TABLE authors DROP birth_date
auth_id auth_name auth_city Dearborn Taylor auth_state MI MI auth_state . .

123456789 Jane Doe 000000001 John Smith

DROP removed the birth_date characteristic from the table


32

Using SQL
The DROP statement is also used to delete an entire database.

auth_id

auth_name

auth_city Dearborn Taylor

auth_state MI MI

123456789 Jane Doe 000000001 John Smith

DROP removed the database and returned the memory to system


33

Conclusion
SQL is a versatile language that can integrate with numerous 4GL languages and applications SQL simplifies data manipulation by reducing the amount of code required.
More reliable than creating a database using files with linked-list implementation
34

THANK YOU
35

Potrebbero piacerti anche