1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data...

28
1 Relational Data Relational Data Model Model CS 157B CS 157B Nidhi Patel Nidhi Patel

Transcript of 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data...

Page 1: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

11

Relational Data ModelRelational Data Model

CS 157BCS 157B

Nidhi PatelNidhi Patel

Page 2: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

22

What is a Data Model?What is a Data Model? A notation for describing data or informationA notation for describing data or information Description consists of 3 parts:Description consists of 3 parts:

1) Structure of the data1) Structure of the data-Arrays and structures or objects-Arrays and structures or objects

2) Operations on the data2) Operations on the data-Queries (Operations that retrieve -Queries (Operations that retrieve

information) information) -Modifications (Operations that change the -Modifications (Operations that change the

database)database)3) Constraints on the data3) Constraints on the data

-Limitations-Limitations

Page 3: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

33

Important Data ModelsImportant Data Models

1) The relational model (including object-1) The relational model (including object-relational extensions)relational extensions)

-Present in all commercial database -Present in all commercial database management systemsmanagement systems

2) The semistructured-data model (including 2) The semistructured-data model (including XML and related standards)XML and related standards)

Page 4: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

44

The Relational ModelThe Relational Model

Based on tablesBased on tables The structure portion resemble an array of The structure portion resemble an array of

structs in Cstructs in C Column headers Column headers Field namesField names Each row Each row values of struct in the arrayvalues of struct in the array

Page 5: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

55

An example relationAn example relation

TitleTitle YearYear LengthLength GenreGenre

Gone With the Gone With the WindWind

19391939 231231 DramaDrama

Star WarsStar Wars 19771977 124124 sciFisciFi

Wayne’s WorldWayne’s World 19921992 9595 comedycomedy

Page 6: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

66

The Semistructured ModelThe Semistructured Model

Semistructured data resembles trees or graphsSemistructured data resembles trees or graphs Represent data by hierarchically nested tagged Represent data by hierarchically nested tagged

elementselements The tags are similar to those used in HTMLThe tags are similar to those used in HTML Tags define the role played by different pieces Tags define the role played by different pieces

of dataof data

Page 7: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

77

Movie data as XMLMovie data as XML<Movies><Movies>

<Movie title=“Gone With the Wind”><Movie title=“Gone With the Wind”><Year>1939</Year><Year>1939</Year><Length>231</Length><Length>231</Length><Genre>drama</Genre><Genre>drama</Genre>

</Movies></Movies><Movie title=“Star Wars”><Movie title=“Star Wars”>

<Year>1977</Year><Year>1977</Year><Length>124</Length><Length>124</Length><Genre>sciFi</Genre><Genre>sciFi</Genre>

</Movies></Movies><Movie title=“Wayne’s World”><Movie title=“Wayne’s World”>

<Year>1992</Year><Year>1992</Year><Length>95</Length><Length>95</Length><Genre>comedy</Genre><Genre>comedy</Genre>

</Movies></Movies></Movies></Movies>

Page 8: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

88

Other Data ModelsOther Data Models

Object-oriented featuresObject-oriented features

-Values can have structure-Values can have structure

-Relations can have associated methods-Relations can have associated methods

Object-relational model --- which are analogous Object-relational model --- which are analogous to the way structs in C were extended to to the way structs in C were extended to objects in C++objects in C++

Page 9: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

99

Comparison of Modeling Comparison of Modeling ApproachesApproaches

Semistructured models have more flexibility than Semistructured models have more flexibility than relationsrelations

Nevertheless, the relational model is still preferredNevertheless, the relational model is still preferred Because database are large,Because database are large,

-efficiency of access to data-efficiency of access to data

-efficiency of modifications to data-efficiency of modifications to data

-ease of use -ease of use These all goals can be achieved with a model, These all goals can be achieved with a model,

particularly the relational modelparticularly the relational model

Page 10: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

1010

Comparison of Modeling Comparison of Modeling ApproachesApproaches

Relational model providesRelational model provides

- A simple, limited approach to structuring - A simple, limited approach to structuring

data, yet is reasonably flexible so anything data, yet is reasonably flexible so anything

can be modeledcan be modeled

- A limited, yet useful, collection of - A limited, yet useful, collection of

operations on dataoperations on data

Page 11: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

1111

Basics of the Relational ModelBasics of the Relational Model Represent data as a two-dimensional table called a Represent data as a two-dimensional table called a

relationrelation

Page 12: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

1212

Basics of the Relational ModelBasics of the Relational Model

Domain: a particular elementary typeDomain: a particular elementary type For example: integer, stringFor example: integer, string

Movies (title:string, year:integer, Movies (title:string, year:integer,

length:integer, genre:string)length:integer, genre:string)

Page 13: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

1313

Keys of RelationsKeys of Relations

A set of attributes forms a key for a relation, if we A set of attributes forms a key for a relation, if we don’t allow two tuples in a relation instance to have don’t allow two tuples in a relation instance to have the same values in all the attributes of the keythe same values in all the attributes of the key

Relation Relation MoviesMovies has a key consisting of the two has a key consisting of the two attributes attributes titletitle and and yearyear

Attribute or attributes that form a key for a relation by Attribute or attributes that form a key for a relation by underlining the key attribute(s).underlining the key attribute(s).

Movies (Movies (titletitle, , yeayear, length, genre)r, length, genre)

Page 14: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

1414

Defining a Relation Schema in SQLDefining a Relation Schema in SQL

SQL (pronounced “sequel”) is the principal SQL (pronounced “sequel”) is the principal language used to describe and manipulate language used to describe and manipulate relational databasesrelational databases

Two aspects to SQLTwo aspects to SQL

1) Data-Definition 1) Data-Definition for declaring for declaring

database schemasdatabase schemas

2) Data-Manipulation 2) Data-Manipulation for querying and for querying and

modifying databasemodifying database

Page 15: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

1515

Relations in SQLRelations in SQL

SQL makes difference between 3 kinds of SQL makes difference between 3 kinds of relations:relations:

- Stored relations - Stored relations tables tables

- Views- Views

- Temporary tables- Temporary tables

Page 16: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

1616

Data TypesData Types

All attributes must have a data typeAll attributes must have a data type

1) Character strings of fixed or varying 1) Character strings of fixed or varying

lengthlength

CHAR(n)CHAR(n) a fixed-length string of up a fixed-length string of up

to to nn characters characters

OROR

VARCHAR (n)VARCHAR (n)

Page 17: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

1717

Data TypesData Types

2) Bit strings of fixed or varying length2) Bit strings of fixed or varying lengthBIT(n)BIT(n) bit strings of length bit strings of length nnBIT VARYING(n)BIT VARYING(n) bit strings of bit strings of

length length up to up to nn 3) The type 3) The type BOOLEANBOOLEAN denotes an attribute denotes an attribute

whose value is logicalwhose value is logical- The possible values are - The possible values are TRUE,TRUE,

FALSE,FALSE, andand UNKNOWNUNKNOWN

Page 18: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

1818

Data TypesData Types

4) The type 4) The type INTINT or or INTEGERINTEGER denotes typical denotes typical integer valuesinteger values

- The type SHORTINT also denotes integers, but the - The type SHORTINT also denotes integers, but the number of bits permitted may be less depending onnumber of bits permitted may be less depending on the implementationthe implementation5) The type 5) The type FLOATFLOAT denotes floating-point numbers denotes floating-point numbers

- Real numbers with a fixed decimal point - Real numbers with a fixed decimal point - - DECIMAL (n,d)DECIMAL (n,d) allows values that consist of allows values that consist of nn

decimal digits, with the decimal point assumed to decimal digits, with the decimal point assumed to be be d d positions from the rightpositions from the right

Page 19: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

1919

Data TypesData Types

6) Dates and times can be represented by the 6) Dates and times can be represented by the data types data types DATEDATE and and TIME TIME respectivelyrespectively

Page 20: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

2020

Simple Table DeclarationsSimple Table Declarations

CREATE TABLE Movies (CREATE TABLE Movies (titletitle CHAR(100),CHAR(100),yearyear INT,INT,lengthlength INT,INT,genregenre CHAR(10),CHAR(10),studioNamestudioName CHAR(30),CHAR(30),producerC#producerC# INTINT

););

Page 21: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

2121

Modifying Relation SchemasModifying Relation Schemas

ALTER TABLE MovieStar ADD phone ALTER TABLE MovieStar ADD phone CHAR(16);CHAR(16);

ALTER TABLE MovieStar DROP birthdate;ALTER TABLE MovieStar DROP birthdate;

Page 22: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

2222

Default ValuesDefault Values

gender CHAR(1) DEFAULT ‘?’,gender CHAR(1) DEFAULT ‘?’,

birthdate DATE DEFAULT DATE ‘0000-00-birthdate DATE DEFAULT DATE ‘0000-00-00’00’

ALTER TABLE MovieStar ADD phone ALTER TABLE MovieStar ADD phone CHAR(16) DEFAULT ‘unlisted’;CHAR(16) DEFAULT ‘unlisted’;

Page 23: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

2323

Declaring Primary KeysDeclaring Primary Keys

CREATE TABLE MovieStar (CREATE TABLE MovieStar (

namename CHAR(30),CHAR(30),

address address VARCHAR(255),VARCHAR(255),

gender gender CHAR(1),CHAR(1),

birthdate birthdate DATE,DATE,

PRIMARY KEY (name)PRIMARY KEY (name)

););

Page 24: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

2424

Declaring Foreign KeysDeclaring Foreign Keys

Example:Example: Suppose we wish to declare the relation Suppose we wish to declare the relation Studio (name, address, presC#)Studio (name, address, presC#)Whose primary key is Whose primary key is namename and which has a foreign key and which has a foreign key presC#presC# that references that references cert#cert# of relation of relationMovieExec (name, address, cert#, netWorth)MovieExec (name, address, cert#, netWorth)

Solution:Solution: CREATE TABLE Studio (CREATE TABLE Studio ( name CHAR(30) PRIMARY KEY,name CHAR(30) PRIMARY KEY, address VARCHAR(255),address VARCHAR(255),

presC# INT,presC# INT, FOREIGN KEY (presC#) REFERENCES FOREIGN KEY (presC#) REFERENCES

MovieExec (cert#)MovieExec (cert#)

Page 25: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

2525

Adding and Deleting TuplesAdding and Deleting Tuples Insert a single tuple using:Insert a single tuple using:

INSERT INTO StarsInINSERT INTO StarsInVALUES (‘The Maltese Falcon’, 1942, ‘Sydney VALUES (‘The Maltese Falcon’, 1942, ‘Sydney Greenstreet’);Greenstreet’);

Delete all tuples satisfying some condition:Delete all tuples satisfying some condition:DELETE FROM StarsInDELETE FROM StarsInWHERE movieTitle = ‘The Maltese Falcon’ ANDWHERE movieTitle = ‘The Maltese Falcon’ AND

movie Year = 1942 AND movie Year = 1942 AND starName = ‘Sydney Greenstreet’;starName = ‘Sydney Greenstreet’;

Page 26: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

2626

Relational Data ModelRelational Data Model

It is almost impossible to store a huge amount It is almost impossible to store a huge amount of data without proper manage.of data without proper manage.

To manage and store data, many methods and To manage and store data, many methods and models have been developed.models have been developed.

Relational Database Model, which has proved Relational Database Model, which has proved to be the best data management model. to be the best data management model.

Page 27: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

2727

Invented byInvented by Relational data model was Relational data model was

invented by Edgar F. Coddinvented by Edgar F. Codd Subsequently maintained and Subsequently maintained and

developed by Chris Date and developed by Chris Date and Hugh Darwen among othersHugh Darwen among others

The relational data model is The relational data model is based on the predicate logic and based on the predicate logic and set theory of mathematics. set theory of mathematics.

Codd used mathematical n-ary Codd used mathematical n-ary relations as a base to represent relations as a base to represent data, which is a subset of the data, which is a subset of the Cartesian product of n sets. Cartesian product of n sets.

Codd

Date

Page 28: 1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.

2828

SummarySummary

Data ModelsData Models Relational Model Relational Model SchemasSchemas KeysKeys Semistructured Data ModelSemistructured Data Model SQLSQL Data DefinitionData Definition Altering SchemasAltering Schemas