onsdag the concepts in a relation data model sql ddl dml

35
Onsdag The concepts in a relation data model SQL DDL DML

Upload: abraham-elliott

Post on 02-Jan-2016

233 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Onsdag The concepts in a relation data model SQL DDL DML

Onsdag

The concepts in a relation data model

SQL

DDL

DML

Page 2: Onsdag The concepts in a relation data model SQL DDL DML

Concepts

Ansat Ansat Nr

Fornavn Efter navn

Foedsels dato

Uddannelse Maaneds Loen

Afdeling

19 Peter Knudsen 240165 Murer 20000 1 34 Mads Michelsen 081151 Murer 23000 1 2 Anne Andersen 230245 Sekretær 19000 1 123 Marianne Jensen 240165 Ingeniør 25000 2 23 Svend Michelsen 111253 Tømrer 20000 2 102 Hans

Henrik Pedersen 021170 Byggetekniker 23000 3

Afdeling AfdelingNr

AfdelingNavn Chef ChefStartDato

1 Nybygning 34 110164 2 Renovering 123 230671 3 Nedrivning 102 061193

Relation navn Attributter

Records/Tupler

Page 3: Onsdag The concepts in a relation data model SQL DDL DML

create table Employee (fname varchar(15) not null, minit char, lname varchar(15) not null, ssn char(9), bdate datetime, address varchar(30), sex char, salary decimal(10,2), superssn char(9), dno int not null, primary key (ssn), foreign key(superssn) references Employee(ssn), foreign key (dno) references Department(dnumber));

Page 4: Onsdag The concepts in a relation data model SQL DDL DML

Create table

create table Employee(……. dno int not null default 1, primary key (ssn), foreign key(superssn) references

Employee(ssn) on update cascade, foreign key (dno) references Department(dnumber) on delete set default

on update cascade);)

Page 5: Onsdag The concepts in a relation data model SQL DDL DML

constraints On attributes

not null

check (dno > 0 and dno < 20)

unique

primary key (defines the PK, implicit not null)

Page 6: Onsdag The concepts in a relation data model SQL DDL DML

Referential integrity constraints

Foreign keyforeign key(dno) references deparment(dnumber)

Cascadeforeign key(superssn) references Employee(ssn) on

update cascade

Page 7: Onsdag The concepts in a relation data model SQL DDL DML

Alter table

alter table employee add email varchar(40);

alter table employee add constraint fkmr foreign key (dept) references

department(dnumber);

Page 8: Onsdag The concepts in a relation data model SQL DDL DML

SQL - DML

Modifying the data

insert into …,

update ... set ... where ...,

delete from … where ...

Query on the data

select ... from ... where ...

Page 9: Onsdag The concepts in a relation data model SQL DDL DML

insert

insert into employee values ('James','E','Borg','888665555','19371110','450 Stone, Houston,TX','M',55000,null,1);

Page 10: Onsdag The concepts in a relation data model SQL DDL DML

Update

update employee

set salary = 25000

where ssn = ’123456789’

update employee

set salary = salary * 1.2

where salary >= 30000

Page 11: Onsdag The concepts in a relation data model SQL DDL DML

Deleting data

Delete, deletes tuples from the database if the fulfill the where condition

Deleting a tuple with referentiel integritet constraints defined with cascade, can delete tuples in other tables

If there are no where condition, all tuples from at table are deleted

delete from department

Page 12: Onsdag The concepts in a relation data model SQL DDL DML

delete

delete from employee

where ssn = ”123456789”

//delete departments which has no employees

delete from department

where dnumber not in (select dno from employee)

Page 13: Onsdag The concepts in a relation data model SQL DDL DML

Select-From-Where Statements

SELECT <attributerlist>

FROM <tables>

WHERE <condition>

Page 14: Onsdag The concepts in a relation data model SQL DDL DML

Single-Relation Query

Starts with the FROM clause.

Then the WHERE clause.

At last the attributes from the SELECT clause.

Algerbra operations?

Page 15: Onsdag The concepts in a relation data model SQL DDL DML

* in SELECT clauses

SELECT *FROM EmployeeWHERE ssn = ‘123456789’;

Page 16: Onsdag The concepts in a relation data model SQL DDL DML

Renaming Attributes

SELECT fname AS firstNameFROM employeeWHERE ssn = ‘123456789’

Page 17: Onsdag The concepts in a relation data model SQL DDL DML

NULL Values

Tuples in SQL relations can have NULL as a value for one or more components.

Meaning depends on context. Two common cases:

Missing value : e.g., we know Joe’s Bar has some address, but we don’t know what it is.

Inapplicable : e.g., the value of attribute spouse for an unmarried person.

Page 18: Onsdag The concepts in a relation data model SQL DDL DML

Comparing NULL’s to Values

The logic of conditions in SQL is really 3-valued logic: TRUE, FALSE, UNKNOWN.

When any value is compared with NULL, the truth value is UNKNOWN.

But a query only produces a tuple in the answer if its truth value for the WHERE clause is TRUE (not FALSE or UNKNOWN).

Page 19: Onsdag The concepts in a relation data model SQL DDL DML

Use is or is not

select Fname, lname

from employee

where superssn is null

Instead of = or !=

Since sql considers each null value as being distinct from every other null value

Page 20: Onsdag The concepts in a relation data model SQL DDL DML

Multirelation Queries

Interesting queries often combine data from more than one relation.

We can address several relations in one query by listing them all in the FROM clause.

Distinguish attributes of the same name by “<relation>.<attribute>”

Page 21: Onsdag The concepts in a relation data model SQL DDL DML

Example

select fname

from employee, dependent

where fname = dependent_name

and ssn = essn;

Page 22: Onsdag The concepts in a relation data model SQL DDL DML

Formal Semantics

Almost the same as for single-relation queries:

1. Start with the product of all the relations in the FROM clause.

2. Apply the selection condition from the WHERE clause.

3. Project onto the list of attributes and expressions in the SELECT clause.

Algebra operations

Page 23: Onsdag The concepts in a relation data model SQL DDL DML

Explicit Tuple-Variables

Sometimes, a query needs to use two copies of the same relation.

Distinguish copies by following the relation name by the name of a tuple-variable, in the FROM clause.

It’s always an option to rename relations this way, even when not essential.

Page 24: Onsdag The concepts in a relation data model SQL DDL DML

Example

Foreach employee select the name from the employee and his supervisor

select e.fname,e.lname, s.fname, s.lname

from employee e, employee swhere e.superssn = s.ssn;

Page 25: Onsdag The concepts in a relation data model SQL DDL DML

Subqueries

A parenthesized SELECT-FROM-WHERE statement (subquery) can be used as a value in a number of places, including FROM and WHERE clauses.

Example: in place of a relation in the Where clause, we can place another query, and then query its result.

Page 26: Onsdag The concepts in a relation data model SQL DDL DML

Subqueries That Return One Tuple

If a subquery is guaranteed to produce one tuple, then the subquery can be used as a value.

Usually, the tuple has one component.

Also typically, a single tuple is guaranteed by keyness of attributes.

A run-time error occurs if there is no tuple or more than one tuple.

Page 27: Onsdag The concepts in a relation data model SQL DDL DML

Example

From Sells(bar, beer, price), find the bars that serve Miller for the same price Joe charges for Bud.

Two queries would surely work:

1. Find the price Joe charges for Bud.

2. Find the bars that serve Miller at that price.

Page 28: Onsdag The concepts in a relation data model SQL DDL DML

Query + Subquery Solution

SELECT bar

FROM Sells

WHERE beer = ‘Miller’ AND

price = (SELECT price

FROM Sells

WHERE bar = ‘Joe’’s Bar’

AND beer = ‘Bud’);The price atwhich Joesells Bud

Page 29: Onsdag The concepts in a relation data model SQL DDL DML

The IN Operator

<tuple> IN <relation> is true if and only if the tuple is a member of the relation.<tuple> NOT IN <relation> means the

opposite.IN-expressions can appear in WHERE

clauses.The <relation> is often a subquery.

Page 30: Onsdag The concepts in a relation data model SQL DDL DML

Example

From Beers(name, manf) and Likes(drinker, beer), find the name and manufacturer of each beer that Fred likes.

SELECT *

FROM Beers

WHERE name IN (SELECT beer

FROM Likes

WHERE drinker = ‘Fred’);The set ofbeers Fredlikes

Page 31: Onsdag The concepts in a relation data model SQL DDL DML

The Exists Operator

EXISTS( <relation> ) is true if and only if the <relation> is not empty.

Being a boolean-valued operator, EXISTS can appear in WHERE clauses.

Page 32: Onsdag The concepts in a relation data model SQL DDL DML

Example Query with EXISTS

select e.fname, e.lname

from employee e

where exists (select *

from dependent

where e.ssn = essn and e.sex = sex

and e.fname = dependent_name)

Page 33: Onsdag The concepts in a relation data model SQL DDL DML

Important Points

Two single quotes inside a string represent the single-quote (apostrophe).

Conditions in the WHERE clause can use AND, OR, NOT

SQL is case-insensitive. In general, upper and lower case characters are the same, except inside quoted strings.

Page 34: Onsdag The concepts in a relation data model SQL DDL DML

Patterns

WHERE clauses can have conditions in which a string is compared with a pattern, to see if it matches.

General form: <Attribute> LIKE <pattern> or <Attribute> NOT LIKE <pattern>

Pattern is a quoted string with % = ‘any string’; _ = “any character.”

Page 35: Onsdag The concepts in a relation data model SQL DDL DML

Exercises

8.13 (Company databasen)

8.14 (Company databasen

FlereSQLØvelser (word doc. )

8.10 and 8.11 create an database and queries on the database