Download presentation
Presentation is loading. Please wait.
Published byNora Barrett Modified over 9 years ago
1
1 Relational Data Model CS 157B Nidhi Patel
2
2 What is a Data Model? A notation for describing data or information A notation for describing data or information Description consists of 3 parts: Description consists of 3 parts: 1) Structure of the data -Arrays and structures or objects 2) Operations on the data -Queries (Operations that retrieve information) information) -Modifications (Operations that change the database) database) 3) Constraints on the data -Limitations
3
3 Important Data Models 1) The relational model (including object- relational extensions) -Present in all commercial database management systems 2) The semistructured-data model (including XML and related standards)
4
4 The Relational Model Based on tables Based on tables The structure portion resemble an array of structs in C The structure portion resemble an array of structs in C Column headers Field names Column headers Field names Each row values of struct in the array Each row values of struct in the array
5
5 An example relation TitleYearLengthGenre Gone With the Wind 1939231Drama Star Wars 1977124sciFi Wayne’s World 199295comedy
6
6 The Semistructured Model Semistructured data resembles trees or graphs Semistructured data resembles trees or graphs Represent data by hierarchically nested tagged elements Represent data by hierarchically nested tagged elements The tags are similar to those used in HTML The tags are similar to those used in HTML Tags define the role played by different pieces of data Tags define the role played by different pieces of data
7
7 Movie data as XML <Movies> <Year>1939</Year><Length>231</Length><Genre>drama</Genre></Movies> <Year>1977</Year><Length>124</Length><Genre>sciFi</Genre></Movies> <Year>1992</Year><Length>95</Length><Genre>comedy</Genre> </Movies>
8
8 Other Data Models Object-oriented features Object-oriented features -Values can have structure -Relations can have associated methods Object-relational model --- which are analogous to the way structs in C were extended to objects in C++
9
9 Comparison of Modeling Approaches Semistructured models have more flexibility than relations Semistructured models have more flexibility than relations Nevertheless, the relational model is still preferred Nevertheless, the relational model is still preferred Because database are large, Because database are large, -efficiency of access to data -efficiency of modifications to data -ease of use These all goals can be achieved with a model, particularly the relational model These all goals can be achieved with a model, particularly the relational model
10
10 Comparison of Modeling Approaches Relational model provides Relational model provides - A simple, limited approach to structuring data, yet is reasonably flexible so anything data, yet is reasonably flexible so anything can be modeled can be modeled - A limited, yet useful, collection of operations on data operations on data
11
11 Basics of the Relational Model Represent data as a two-dimensional table called a relation Represent data as a two-dimensional table called a relation
12
12 Basics of the Relational Model Domain: a particular elementary type Domain: a particular elementary type For example: integer, string For example: integer, string Movies (title:string, year:integer, length:integer, genre:string) length:integer, genre:string)
13
13 Keys of Relations A set of attributes forms a key for a relation, if we don’t allow two tuples in a relation instance to have the same values in all the attributes of the key A set of attributes forms a key for a relation, if we don’t allow two tuples in a relation instance to have the same values in all the attributes of the key Relation Movies has a key consisting of the two attributes title and year Relation Movies has a key consisting of the two attributes title and year Attribute or attributes that form a key for a relation by underlining the key attribute(s). Attribute or attributes that form a key for a relation by underlining the key attribute(s). Movies (title, year, length, genre)
14
14 Defining a Relation Schema in SQL SQL (pronounced “sequel”) is the principal language used to describe and manipulate relational databases SQL (pronounced “sequel”) is the principal language used to describe and manipulate relational databases Two aspects to SQL Two aspects to SQL 1) Data-Definition for declaring database schemas database schemas 2) Data-Manipulation for querying and modifying database modifying database
15
15 Relations in SQL SQL makes difference between 3 kinds of relations: SQL makes difference between 3 kinds of relations: - Stored relations tables - Views - Temporary tables
16
16 Data Types All attributes must have a data type All attributes must have a data type 1) Character strings of fixed or varying length length CHAR(n) a fixed-length string of up to n characters to n charactersOR VARCHAR (n)
17
17 Data Types 2) Bit strings of fixed or varying length BIT(n) bit strings of length n BIT VARYING(n) bit strings of length up to n up to n 3) The type BOOLEAN denotes an attribute whose value is logical - The possible values are TRUE, FALSE, and UNKNOWN FALSE, and UNKNOWN
18
18 Data Types 4) The type INT or INTEGER denotes typical integer values integer values - The type SHORTINT also denotes integers, but the number of bits permitted may be less depending on number of bits permitted may be less depending on the implementation the implementation 5) The type FLOAT denotes floating-point numbers - Real numbers with a fixed decimal point - DECIMAL (n,d) allows values that consist of n decimal digits, with the decimal point assumed to decimal digits, with the decimal point assumed to be d positions from the right be d positions from the right
19
19 Data Types 6) Dates and times can be represented by the data types DATE and TIME respectively
20
20 Simple Table Declarations CREATE TABLE Movies ( titleCHAR(100), yearINT, lengthINT, genreCHAR(10), studioNameCHAR(30), producerC#INT );
21
21 Modifying Relation Schemas ALTER TABLE MovieStar ADD phone CHAR(16); ALTER TABLE MovieStar ADD phone CHAR(16); ALTER TABLE MovieStar DROP birthdate; ALTER TABLE MovieStar DROP birthdate;
22
22 Default Values gender CHAR(1) DEFAULT ‘?’, gender CHAR(1) DEFAULT ‘?’, birthdate DATE DEFAULT DATE ‘0000-00- 00’ ALTER TABLE MovieStar ADD phone CHAR(16) DEFAULT ‘unlisted’; ALTER TABLE MovieStar ADD phone CHAR(16) DEFAULT ‘unlisted’;
23
23 Declaring Primary Keys CREATE TABLE MovieStar ( CREATE TABLE MovieStar ( nameCHAR(30), address VARCHAR(255), gender CHAR(1), birthdate DATE, PRIMARY KEY (name) ); );
24
24 Declaring Foreign Keys Example: Suppose we wish to declare the relation Studio (name, address, presC#) Whose primary key is name and which has a foreign key presC# that references cert# of relation MovieExec (name, address, cert#, netWorth) Solution: 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#)
25
25 Adding and Deleting Tuples Insert a single tuple using: Insert a single tuple using: INSERT INTO StarsIn VALUES (‘The Maltese Falcon’, 1942, ‘Sydney Greenstreet’); Delete all tuples satisfying some condition: Delete all tuples satisfying some condition: DELETE FROM StarsIn WHERE movieTitle = ‘The Maltese Falcon’ AND movie Year = 1942 AND movie Year = 1942 AND starName = ‘Sydney Greenstreet’; starName = ‘Sydney Greenstreet’;
26
26 Relational Data Model It is almost impossible to store a huge amount of data without proper manage. It is almost impossible to store a huge amount of data without proper manage. To manage and store data, many methods and models have been developed. To manage and store data, many methods and models have been developed. Relational Database Model, which has proved to be the best data management model. Relational Database Model, which has proved to be the best data management model.
27
27 Invented by Relational data model was invented by Edgar F. Codd Relational data model was invented by Edgar F. Codd Subsequently maintained and developed by Chris Date and Hugh Darwen among others Subsequently maintained and developed by Chris Date and Hugh Darwen among others The relational data model is based on the predicate logic and set theory of mathematics. The relational data model is based on the predicate logic and set theory of mathematics. Codd used mathematical n-ary relations as a base to represent data, which is a subset of the Cartesian product of n sets. Codd used mathematical n-ary relations as a base to represent data, which is a subset of the Cartesian product of n sets. Codd Date
28
28 Summary Data Models Data Models Relational Model Relational Model Schemas Schemas Keys Keys Semistructured Data Model Semistructured Data Model SQL SQL Data Definition Data Definition Altering Schemas Altering Schemas
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.