Database Management Systems Relational Model Sule H. Turgut Uyar - - PowerPoint PPT Presentation

database management systems
SMART_READER_LITE
LIVE PREVIEW

Database Management Systems Relational Model Sule H. Turgut Uyar - - PowerPoint PPT Presentation

Database Management Systems Relational Model Sule H. Turgut Uyar O g ud uc u February 2005 Contents Relational Model Domains Relation Structure Data Definition SQL Data Types SQL Commands Data Manipulation SQL


slide-1
SLIDE 1

Database Management Systems

Relational Model

  • H. Turgut Uyar

¸ Sule ¨ O˘ g¨ ud¨ uc¨ u February 2005

slide-2
SLIDE 2

Contents

Relational Model Domains Relation Structure Data Definition SQL Data Types SQL Commands Data Manipulation SQL Commands

slide-3
SLIDE 3

Data Models

◮ previous models:

◮ inverted list ◮ hierarchic ◮ network

◮ relational model:

◮ Dr. E. F. Codd, 1970

◮ recent models:

◮ object ◮ object / relation

slide-4
SLIDE 4

Relational Model

◮ data is modelled as relations:

α ⊆ A × B × C × ...

◮ every element of a relation is a tuple ◮ every data of an element is an attribute ◮ relations are represented by tables

◮ user should perceive all the data as tables ◮ relation → table, tuple → row, attribute → column

slide-5
SLIDE 5

Relation Example

Example (Movie Data)

Table: MOVIE

TITLE YR DIRECTOR COUNTRY SCORE VOTES Usual Suspects 1995 Bryan Singer UK 8.7 3502 Suspiria 1977 Dario Argento IT 7.1 1004 Being John Malkovich 1999 Spike Jonze US 8.3 13809 ... ... ... ... ... ...

◮ the element (Usual Suspects, 1995, Bryan Singer, UK, 8.7,

3502) is a tuple of the movie relation

◮ YR is an attribute of the elements of the movie relation

slide-6
SLIDE 6

Relation Predicate

Definition

relation predicate: sentence that states the meaning of the relation

◮ every tuple is either True or False with respect to predicate

slide-7
SLIDE 7

Predicate Example

Example

the film titled TITLE was directed by DIRECTOR in the country of COUNTRY in the year YEAR; the average of the votes VOTES is SCORE.

◮ (Suspiria, 1977, Dario Argento, IT, 1004, 7.1)

tuple is true

◮ (Suspiria, 1877, Dario Argento, IT, 1004, 7.1)

tuple is false

slide-8
SLIDE 8

Order of Tuples

◮ tuples are unordered

Example

these two relations have no difference:

TITLE ... Usual Suspects ... Suspiria ... Being John Malkovich ... TITLE ... Suspiria ... Being John Malkovich ... Usual Suspects ...

slide-9
SLIDE 9

Order of Attributes

◮ attributes are unordered

Example

these two relations have no difference:

TITLE YR ... Usual Suspects 1995 ... Suspiria 1977 ... Being John Malkovich 1999 ... YR TITLE ... 1995 Usual Suspects ... 1977 Suspiria ... 1999 Being John Malkovich ...

slide-10
SLIDE 10

Duplicate Tuples

◮ there are no duplicate tuples

◮ each tuple should be distinguished from each other

Example

TITLE YR DIRECTOR COUNTRY SCORE VOTES Usual Suspects 1995 Bryan Singer UK 8.7 3502 Suspiria 1977 Dario Argento IT 7.1 1004 Being John Malkovich 1999 Spike Jonze US 8.3 13809 ... ... ... ... ... ... Suspiria 1977 Dario Argento IT 7.1 1004 ... ... ... ... ... ...

❍❍ ❍ ❥ ✟✟ ✟ ✯

slide-11
SLIDE 11

Domain

◮ the values of the same attribute should be on the same domain ◮ comparison is only meaningful when it is between two values

  • f the same domain

◮ in practice, only data types are used

slide-12
SLIDE 12

Domain Example

Example

◮ TITLE should be taken from titles domain, YR should be

taken from years domain, COUNTRY should be taken from countries domain ...

◮ using the data types:

TITLE string, YR integer, COUNTRY string, ...

◮ COUNTRY attribute might have the value“Woody Allen”but

it won’t be meaningful

◮ YR and VOTES values are integers but comparing them is not

meaningful

slide-13
SLIDE 13

Null Value

the value of an attribute in a tuple is unknown

Example

the director of“Blade”is unknown tuple does not have a value for that attribute

Example

no one has voted for“Star Wars” , division by zero is not allowed, therefore SCORE is null

slide-14
SLIDE 14

Default Value

◮ a default value may be used for an unknown attribute ◮ default value should be not a valid value

Example

the attribute SCORE might have a default value of -1

slide-15
SLIDE 15

Relation Structure

relation heading

◮ set of attributes that create

the relation

◮ stated when the relation is

being created

◮ effected by data definition

language commands

relation body

◮ set of tuples in a relation ◮ effected by data

manipulation language commands

slide-16
SLIDE 16

Relation Structure Example

Example

Table: MOVIE

ID TITLE YR DIRECTOR COUNTRY SCORE VOTES ... ... ... ... ... ... ... 6 Usual Suspects 1995 Bryan Singer ... 1512 Suspiria 1977 Dario Argento ... 70 Being John Malkovich 1999 Spike Jonze ... ... ... ... ... ... ... ...

◮ the row with blue background color is heading ◮ the rows with white background color is body

slide-17
SLIDE 17

Data Definition Language

◮ creating and destroying relations ◮ changes in relation heading

◮ changing relation name ◮ adding attribute ◮ changing attribute name

◮ adding and deleting constraints

slide-18
SLIDE 18

Logical Type

◮ BOOLEAN

slide-19
SLIDE 19

Numerical Types

◮ INTEGER

◮ SMALLINT

◮ NUMERIC (precision, scale)

◮ precision: number of total digits ◮ scale: number of digits after dot ◮ DECIMAL (precision, scale) synonymous

◮ FLOAT (p)

◮ p: least acceptable precision

slide-20
SLIDE 20

String Types

◮ CHARACTER [VARYING] (n)

◮ CHARACTER (n) if the characters of the value are less than n,

value is padded by spaces

◮ CHAR (n) instead of CHARACTER (n)

VARCHAR (n) instead of CHARACTER VARYING (n)

slide-21
SLIDE 21

Large Object Types

◮ random length ◮ no query on them ◮ binary: BINARY LARGE OBJECT (n)

◮ BLOB ◮ picture, sound, etc.

◮ text: CHARACTER LARGE OBJECT (n)

◮ CLOB

slide-22
SLIDE 22

Date/Time Types

◮ DATE

◮ example value: 2005-09-26

◮ TIME

◮ example value: 11:59:22.078717

◮ TIMESTAMP

◮ example value: 2005-09-26 11:59:22.078717

◮ INTERVAL

◮ example value: 3 days

slide-23
SLIDE 23

Creating Tables

Command

CREATE TABLE table name ( column name type [DEFAULT d e f a u l t v a l u e ] [ , column name type [DEFAULT d e f a u l t v a l u e ] ] ∗ )

Drop Table Command

DROP TABLE table name

slide-24
SLIDE 24

Create Table Example

Example

CREATE TABLE MOVIE ( TITLE VARCHAR(80) , YR NUMERIC(4) , DIRECTOR VARCHAR(40) , COUNTRY CHAR(2) , SCORE FLOAT, VOTES INTEGER DEFAULT 0 )

slide-25
SLIDE 25

Changing Table Names

Command

ALTER TABLE table name RENAME TO new name

Example

ALTER TABLE MOVIE RENAME TO FILM

slide-26
SLIDE 26

Adding Columns

Command

ALTER TABLE table name ADD [COLUMN] column name type [DEFAULT d e f a u l t v a l u e ]

Example

ALTER TABLE MOVIE ADD COLUMN LANGUAGE CHAR(2)

slide-27
SLIDE 27

Deleting Columns

Command

ALTER TABLE table name DROP [COLUMN] column name

Example

ALTER TABLE MOVIE DROP COLUMN LANGUAGE

slide-28
SLIDE 28

Changing Column Names

Command

ALTER TABLE table name RENAME [COLUMN] column name TO new name

Example

ALTER TABLE MOVIE RENAME COLUMN TITLE TO NAME

slide-29
SLIDE 29

Changing Default Value of a Column

Command

ALTER TABLE table name ALTER [COLUMN] column name SET DEFAULT new value

Example

ALTER TABLE MOVIE ALTER COLUMN SCORE SET DEFAULT −1

slide-30
SLIDE 30

Deleting Default Value of a Column

Command

ALTER TABLE table name ALTER [COLUMN] column name DROP DEFAULT

Example

ALTER TABLE MOVIE ALTER COLUMN SCORE DROP DEFAULT

slide-31
SLIDE 31

Data Manipulation Language

◮ tuple

◮ insert ◮ update ◮ delete

◮ processed on set of tuples

◮ all the tuples in a specific condition ◮ inserts generally add one tuple

slide-32
SLIDE 32

Insert Row

Command

INSERT INTO table name [ ( column name [ , column name ] ∗ ) ] VALUES ( value [ , value ] ∗ )

◮ order of the values should match the order of columns ◮ unspecified columns will have defalut values ◮ if the coulmn names aren’t specified, all the values of the

columns should be in the order of the table creation

slide-33
SLIDE 33

Insert Row Examples

Example

INSERT INTO MOVIE VALUES ( ’ Usual Suspects ’ , 1995 , ’ Bryan Singer ’ , ’UK ’ , 8.7 , 35027 )

slide-34
SLIDE 34

Insert Row Examples

Example

INSERT INTO MOVIE (YR, TITLE) VALUES ( 1995 , ’ Usual Suspects ’ )

slide-35
SLIDE 35

Delete Row

Command

DELETE FROM table name [WHERE c o n d i t i o n ]

◮ if no conditions are provided, then all the rows will be deleted

slide-36
SLIDE 36

Delete Row Examples

Example (delete the movies having score less than 3)

DELETE FROM MOVIE WHERE (SCORE<3)

slide-37
SLIDE 37

Delete Row Examples

Example (delete the movies having score less than 3 and votes at least 5)

DELETE FROM MOVIE WHERE ((SCORE<3) AND (VOTES>=5))

slide-38
SLIDE 38

Update Rows

Command

UPDATE table name SET column name=value [ , column name=value ]∗ [WHERE c o n d i t i o n ]

◮ if no conditions are provided, then all the rows will be updated

slide-39
SLIDE 39

Update Row Example

Example (clear the scores and the votes of all the movies directed before 1997)

UPDATE MOVIE SET SCORE=0, VOTES=0 WHERE (YR<1997)

slide-40
SLIDE 40

Delete Row Example

Example (Update the votes and the scores of the movies directed before 1997 such that they have one more vote of 5 points)

UPDATE MOVIE SET SCORE=(SCORE∗VOTES+5)/(VOTES+1) , VOTES=VOTES+1 WHERE (YR<1997)