sql - cs.tut.fidip/slides/sql.pdf · sql • learn to make sql ... – ansi (american national...

23
Luento 4 - sivu 1 3. SQL Learn to make SQL queries Access data from a DB using SQL Structure of an SQL query Changing the state of a DB with SQL insert data change and remove data

Upload: nguyenkiet

Post on 31-Mar-2018

237 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 1

3. SQL

• Learn to make SQL queries– Access data from a DB using

SQL– Structure of an SQL query

• Changing the state of a DB with SQL– insert data– change and remove data

Page 2: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 2

SQL Query Language

• SQL (Structured Query Language)– Most popular query and management language for RDBMS

• Various dialects– ANSI (American National Standads Institute) SQL, eli SQL1– SQL2

• vuoden 1992 standardi, laajennettu SQL1:stä– SQL´99 (SQL3) … viimeisin SQL 2003

• laajennettu SQL2:sta (mm. rekursiolla, laukaisimilla ja olioilla)• Käytännössä ”olio-relationaalisen” mallin mukainen

– Ohjelmistotuottajien SQL-murteet• tyypillisesti SQL2:n laajennuksia omilla ja SQL3:n piirteillä

Page 3: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 3

3.2 SQL Queries

• Practically, each SQL queries contains keywords SELECT, FROM ja WHERE in the form:

SELECT < list of attributes>FROM < list of relations>WHERE < condition>

• FROM part lists the relation(s) for applying the query• WHERE part contins the conditions which the chosen tuples must

satisfy.• SELECT part defines the schema for the resulting relation meaning,

which attributes are included (* denotes all)

Page 4: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 4

Example DB

• The following queries are based on the DB below

Movie(title, year, length, inColor, studioName, producerC#)

StarsIn(movieTitle, movieYear, starName)

MovieStar(name, address, gender, birthdate)

MovieExec(name, address, cer#, netWorth)

Studio(name, address, presC#)

Page 5: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 5

Example

• Target tableMovie(title, year, length, inColor, studioName, producerC#)

query “the movies produced by the studio Disney at 1990”.

SELECT *FROM MovieWHERE studioName = ‘Disney’ and year = 1990;

The reulting relation could be:

title year length inColor studioName producerC#Pretty Woman 1990 119 true Disney 999

Page 6: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 6

SELECT

• Resulting relation can have different names for the attributes than the originating ones.

• Let us change the previous query to the following:

SELECT title AS name, length AS durationFROM MovieWHERE studioName = ‘Disney’ and year = 1990;

jolloin kyselyn tulosrelaatio:

name durationPretty Woman 119

Page 7: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 7

Example

• SELECT part may contain mathematical formulas.

• Let us change the previous query to express the length in hours instead of minutes:

SELECT title AS name, length *0.016667 AS lengthInHours;

• The resulting relation is:

name lengthInHoursPretty Woman 1.98334

Page 8: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 8

Duplicates

• In SQL the resulting relations are not sets but they are bags. This means that the same tuple may occur several times.

• If the duplicates need to be removed from the results the SELECT part of a query must contain keyword DISTINCT. For example the relation:Movie (title, year, length, inColor, studioName, producerC#)

query: SELECT titleFROM Movie;

may produce doubles, since title is not the primary key. The following query removes the duplicates:

SELECT DISTINCT title FROM Movie;

.

Page 9: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 9

FROM

• A query in SQL may refer to several relations by giving the names of the refered relations in the FROM part. The other parts of the query can then refer to the attributes of these relations.

• Example: We want to retrieve the producer of Star Wars. Now, the query has to be targeted to relations

Movie( title, year, length, inColor, studioName, producerC#) andMovieExec(name, address, cert#, netWorth),

where the attributes producerC# ja cer# are the certification numbers identifying the producer.

Page 10: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 10

Queries that are Targered to Several Relations

• The query could be executed as two queries1. The certificate number (producerC#) of the producer from relation Movie.2. Retrieving the corresponding person from the MovieExec relation.

• These two steps can be given in a single query:

SELECT name FROM Movie, MovieExecWHERE title= ‘Star Wars’ AND producerC# =cert#;

Page 11: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 11

WHERE

• WHERE part may contain the following relational operators-osa voi sisältää seuraavia vertailuoperaattoreita:=, <>, <, >, <=, ja >=. The values being compared can be constants or attributes of relations defined in the FROM part.

• Arithmetical operators can be used:( +, -, *, /).

Example: (year - 1930) * (year - 1930) < 100 is true for years 1921 - 1939.

Page 12: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 12

Example

• ”The names of the movies made after 1970 or are shorter that 90 minutes made by MGM”.

SELECT titleFROM MovieWHERE (year > 1970 OR length < 90)

AND studioName = ‘MGM’;

Page 13: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 13

String Comparison

• SQL makes it possible compare certain similarities ofstrings with the operator LIKE

• LIKE expression is of the form:

s LIKE p

where s on a string and p is a pattern.

• The pattern can contain special characters : % and _ • % denotes that a substring of s (of any length) can contain any

characters. • _ denotes a one-character substring of any character.

• The expression s LIKE p is true, if p matches to s

Page 14: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 14

String Comparison

• Example SELECT titleFROM MovieWHERE title LIKE ‘Star _ _ _ _’;

retrieves the movies whose name consists of nine characters so, thatthe first four are Star following a space and the last four can be any characters.

• Example. SELECT titleFROM MovieWHERE title LIKE ‘% `s %’;

retrieves all movies which are in the genetic foem.

Page 15: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 15

Sorting the Resukting Relation

• The tuples of the resulting relation can be defined with one or more attributes:

– The order is declared by adding order qualifier to the SELECT part

ORDER BY < list of attributes>

– The order is ascending (ASC) by default

Page 16: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 16

Example

• Query

SELECT *FROM MovieWHERE studioName = ‘Disney’ AND year = 1990ORDER BY length, title;

results in a relation of movies ordered from the shortes to the longest one and if the movies are equally length they appear in the alphabetical order.

Page 17: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 17

Example 2

SELECT title, year, length, inColor, studioName, producerC#FROM MovieWHERE studioName = ‘Disney’ AND year = 1990ORDER BY 3, 1 DESC;

Page 18: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 18

Aggregation Operators

• Calculate a scalar from a column of a relation• The operators are:

1. SUM2. AVG3. MIN4. MAX5. COUNT the number of values, or the number of different values,

if DISTINCT is used

• Esim. The following query prints the average salary of the producers:

SELECT AVG(netWorth) FROM MovieExec;

Page 19: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 19

Aggregation Operators

• If * is used as a parameter to COUNT operator, then the query results in the number of rows in the relation:

SELECT COUNT(*)FROM MovieExec;

• Query

SELECT COUNT(DISTINCT name)FROM MovieExec;

outputs the number of different names the relation

Page 20: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 20

Grouping

• By using GROUP BY part, the aggregate operators can be targeted to groups of values of a column. Now, the query outputs a single row for each group.

• Example query

SELECT studioName, SUM(length)FROM MovieGROUP BY studioName;

outputs the overall length of all movies for each studio. The resulting relation has as many rows as there are studios with different name in the studioName relation.

Page 21: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 21

Example

Grouping by producers

Movie( title, year, length, inColor, studioName, producerC#)MovieExec(name, address, cert#, netWorth)

SELECT name, SUM(length)FROM MovieExec, MovieWHERE producerC# = cer#GROUP BY nameORDER BY 2 DESC;

Page 22: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 22

Insert, Delete and Update

• Modification operators change the stste of a database.

• The operators are:1. INSERT

adding one or more rows to a relation

2. DELETE removing one or more rows from a relation

3. UPDATEupdating one or more rows in a relation

Page 23: SQL - cs.tut.fidip/slides/SQL.pdf · SQL • Learn to make SQL ... – ANSI (American National Standads Institute) SQL, eli SQL1 – SQL2 • vuoden 1992 standardi, laajennettu SQL1:st

Luento 4 - sivu 23

Defining Schema with SQL

• Schema is defined withCREATE TABLE

Example. CREATE TABLE MovieStar (name CHAR(30), address VARCHAR(255),gender CHAR(1),birthdate DATE );

• Removing a table:Command DROP TABLE removes relation R