/
SQL SQL Introduction Standard language for querying and manipulating data SQL SQL Introduction Standard language for querying and manipulating data

SQL SQL Introduction Standard language for querying and manipulating data - PowerPoint Presentation

natalia-silvester
natalia-silvester . @natalia-silvester
Follow
373 views
Uploaded On 2018-10-09

SQL SQL Introduction Standard language for querying and manipulating data - PPT Presentation

S tructured Q uery L anguage Many standards out there ANSI SQL SQL92 aka SQL2 SQL99 aka SQL3 Vendors support various subsetseg PLSQL from Oracle ID: 687141

price product purchase select product price select purchase category sql pname table insert count data gizmo manufacturer gadgets query

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "SQL SQL Introduction Standard language f..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

SQLSlide2

SQL Introduction

Standard language for querying and manipulating data

S

tructured Query Language

Many standards out there:

ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3), ….

Vendors support various subsets(e.g. PLSQL from Oracle)Slide3

SQL

Data Definition Language (DDL)

Create/alter/delete tables and their attributes

Data Manipulation Language (DML)

Query one or more tables – discussed next !Insert/delete/modify tuples in tablesConnect to mysql

on dbsrv2

mysql

-h dbsrv2.cs.fsu.edu -u

username

-pSlide4

Tables in SQL

PName

Price

Category

Manufacturer

Gizmo

$19.99

Gadgets

GizmoWorks

Powergizmo$29.99GadgetsGizmoWorksSingleTouch$149.99PhotographyCanonMultiTouch$203.99HouseholdHitachi

Product

Attribute names

Table name

Tuples or rowsSlide5

Create a Tables

CREATE TABLE 

table_name

(

column_name1 data_type

(

size

),

column_name2 data_type

(size),column_name3 data_type(size),....);Slide6

Tables Explained

The

schema

of a table is the table name and its attributes:

Product(PName, Price, Category, Manfacturer)A key is an attribute whose values are unique;we underline a key

Product(

PName

, Price, Category, Manfacturer)Slide7

Data Types in SQL

Atomic types:

Characters: CHAR(20), VARCHAR(50)

Numbers: INTEGER(INT), FLOAT, DOUBLE, BIGINT

Others: DATE, DATETIME, BLOB, …Data types might have different names in different database! Slide8

Tables Explained

A tuple = a record

Restriction: all attributes are of atomic type

A table = a set of tuples

Like a list……but it is unorderd: no first(), no next(), no last().Slide9

SQL QUERY

Basic form: (plus many many more bells and whistles)

SELECT

<attributes>

FROM

<one or more relations>

WHERE <conditions>Slide10

Simple SQL

QUERY

PName

Price

Category

Manufacturer

Gizmo

$19.99

Gadgets

GizmoWorksPowergizmo$29.99GadgetsGizmoWorksSingleTouch$149.99PhotographyCanonMultiTouch$203.99HouseholdHitachi

SELECT *FROM ProductWHERE category=‘Gadgets’

Product

PName

Price

Category

Manufacturer

Gizmo

$19.99

Gadgets

GizmoWorks

Powergizmo

$29.99

Gadgets

GizmoWorks

“selection”Slide11

Simple SQL QUERY

PName

Price

Category

Manufacturer

Gizmo

$19.99

Gadgets

GizmoWorks

Powergizmo$29.99GadgetsGizmoWorksSingleTouch$149.99PhotographyCanonMultiTouch$203.99HouseholdHitachi

SELECT PName, Price, ManufacturerFROM ProductWHERE Price > 100

Product

PName

Price

Manufacturer

SingleTouch

$149.99

Canon

MultiTouch

$203.99

Hitachi

“selection” and

“projection”Slide12

SQL

Details

Case insensitive:

Same: SELECT

Select selectSame: Product productDifferent: ‘Seattle’ ‘seattle’Slide13

The

LIKE Operator

LIKE

p: pattern matching on strings

p may contain two special symbols:% = any sequence of characters_ = any single character

SELECT

*

FROM

Products

WHERE PName LIKE ‘%gizmo%’Slide14

Eliminating Duplicates

SELECT

DISTINCT

categoryFROM Product

Compare to:

SELECT

category

FROM

ProductCategoryGadgetsGadgetsPhotographyHouseholdCategoryGadgetsPhotographyHouseholdSlide15

Ordering the Results

SELECT

pname, price, manufacturer

FROM

ProductWHERE category=‘gizmo’ AND price > 50

ORDER BY

price, pname

Ties are broken by the second attribute on the ORDER BY list, etc.

Ordering is ascending, unless you specify the DESC keyword.Slide16

Keys and Foreign Keys

PName

Price

Category

Manufacturer

Gizmo

$19.99

Gadgets

GizmoWorks

Powergizmo$29.99GadgetsGizmoWorksSingleTouch$149.99PhotographyCanonMultiTouch$203.99HouseholdHitachi

Product

Company

CNameStockPrice

Country

GizmoWorks

25

USA

Canon

65

Japan

Hitachi

15

Japan

Key

Foreign

keySlide17

Aggregation

SELECT

count(*)

FROM

ProductWHERE year > 1995

Except count, all aggregations apply to a single attribute

SELECT

avg(price)

FROM

ProductWHERE maker=“Toyota”SQL supports several aggregation operations: sum, count, min, max, avgSlide18

COUNT applies to duplicates, unless otherwise stated:

SELECT

Count(category)

FROM

Product

WHERE

year > 1995

same as Count(*)

We probably want:

SELECT Count(DISTINCT category)FROM ProductWHERE year > 1995Aggregation : CountSlide19

Purchase(product, date, price, quantity)

More Examples

SELECT

Sum(price * quantity)

FROM

Purchase

SELECT

Sum(price * quantity)

FROM

PurchaseWHERE product = ‘bagel’What dothey mean ?Slide20

Simple Aggregation

Purchase

Product

Date

Price

Quantity

Bagel

10/21

1

20Banana10/30.510Banana10/10110Bagel10/25

1.5020

SELECT Sum(price * quantity)FROM PurchaseWHERE product = ‘bagel’

50 (= 20+30)Slide21

Modifying the Database

Three kinds of modifications

Insertions

Deletions

UpdatesSometimes they are all called “updates”Slide22

Insertions

General form:

Missing attribute

NULL.

May drop attribute names if give them in order.

INSERT INTO

R(A1,…., An)

VALUES (v1,…., vn)INSERT INTO Purchase(buyer, seller, product, store) VALUES (‘Joe’, ‘Fred’, ‘wakeup-clock-espresso-machine’, ‘The Sharper Image’)Example: Insert a new purchase to the database:Slide23

Insertions

INSERT INTO

PRODUCT(name)

SELECT DISTINCT Purchase.product FROM Purchase WHERE Purchase.date > “10/26/01”

The query replaces the VALUES keyword.

Here we insert

many

tuples into PRODUCTSlide24

Insertion: an Example

prodName

is foreign key in

Product

.nameSuppose database got corrupted and we need to fix it:

name

listPrice

category

gizmo

100gadgetsprodNamebuyerNamepricecameraJohn200gizmoSmith80cameraSmith

225

Task: insert in Product all prodNames from Purchase

Product

Product(

name

,

listPrice

, category)

Purchase(

prodName

,

buyerName

, price)

PurchaseSlide25

Insertion: an Example

INSERT INTO

Product(name)

SELECT DISTINCT prodName FROM Purchase WHERE prodName

NOT IN

(

SELECT

name

FROM Product)namelistPricecategorygizmo100Gadgetscamera--Slide26

Insertion: an Example

INSERT INTO

Product(name, listPrice)

SELECT DISTINCT prodName, price FROM Purchase

WHERE

prodName

NOT IN

(

SELECT name FROM Product)namelistPricecategorygizmo100Gadgetscamera200-camera ??225 ??-Depends on the implementationSlide27

Deletions

DELETE FROM

PURCHASE

WHERE

seller = ‘Joe’ AND product = ‘Brooklyn Bridge’

Factoid about SQL: there is no way to delete only a single

occurrence of a tuple that appears twice

in a relation.

Example:Slide28

Updates

UPDATE

PRODUCT

SET

price = price/2WHERE Product.name IN (SELECT product

FROM

Purchase

WHERE Date =‘Oct, 25, 1999’);Example:Slide29

Alter-Add

ALTER TABLE table_name

ADD column_name datatypeSlide30

Alter- Drop

ALTER TABLE table_name

DROP column_name