Lec2 - Introduction To The Relational Model (2D Table)

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Database

programming
Lecture 2: Introduction to the relational model
(2D table)
By: Dr. Ahmad Barghash
Where to
start?
What is a data model

• A structure of data:
• Similar to structs in C language.
• Data structures are frequently labelled as the physical model. However, the actual
physical model is at the hardware side.
• With data operations:
• Using programming.
• Queries and modifications.
• Wide set of algorithms such as search algorithms.
• And constraints on the data
• Data type constraints (int char,..).
Working with structures
• Structures are tools used for grouping elements and storing data.
• The data is grouped under one name but still the individual entities can be reached using the dot (.)
• All you need is definition, declaration, then accessing .

1. Definition 2. Declaration 3. Accessing


struct date struct date today; today.day = 25;
{
int month;
int day;
int year;
};
Lets start with a simple program
#include <stdio.h>
int main (void)
{ The output is:
struct date
{
Today's date is 9/25/04.
int month, day, year; A
char place; 90.500000
float weight;
};
struct date today;
today.month = 9;
today.day = 25;
today.year = 2004;
today.place ='A';
today.weight=90.5;
printf ("Today's date is %i/%i/%.2i.\n %c\n %f", today.month, today.day, today.year % 100, today.place, today.weight);
return 0;
}
Another example
#include <stdio.h>
struct student{
char name; The output is:
int roll; Enter information of students:
float marks;
}; Enter name: A
Enter roll number: 21
int main(){ Enter marks: 334.5
struct student s;
printf("Enter information of students:\n\n");
printf("Enter name: "); Displaying Information
scanf("%c",&s.name); name: A
printf("Enter roll number: "); Roll: 21
scanf("%d",&s.roll); Marks: 334.50
printf("Enter marks: ");
scanf("%f",&s.marks);
printf("\nDisplaying Information\n");
printf("Name: %c\n",s.name);
printf("Roll: %d\n",s.roll);
printf("Marks: %.2f\n",s.marks);
return 0;
}
Storing time using structs slide1

#include <stdio.h>
struct time timeUpdate (struct time now);
struct time
{
int hour;
int minutes;
int seconds;
};

int main (void)


{
struct time currentTime, nextTime;
printf ("Enter the time (hh:mm:ss): ");
scanf ("%i:%i:%i", &currentTime.hour, &currentTime.minutes, &currentTime.seconds);
nextTime = timeUpdate (currentTime);
printf ("Updated time is %.2i:%.2i:%.2i\n", nextTime.hour, nextTime.minutes, nextTime.seconds );
return 0;
}
Storing time using structs slide2
struct time timeUpdate (struct time now)
{
++now.seconds;
if ( now.seconds == 60 )
{ // next minute
now.seconds = 0;
++now.minutes;
if ( now.minutes == 60 )
{ // next hour
now.minutes = 0;
++now.hour;
if ( now.hour == 24 )
now.hour = 0;
}
}
return now;
}

The output is: Another output:


Enter the time (hh:mm:ss): 12:23:55 Enter the time (hh:mm:ss): 16:12:59
Updated time is 12:23:56 Updated time is 16:13:00
Important data models

• Today, the two data models of preeminent importance for database systems are:
1. The relational model, including object-relational extensions.
2. The semistructured-data model, including XML and related standards.
• The first, which is present in all commercial database management systems, is the
subject of this chapter. The semistructured model, of which XML is the primary
manifestation, is an added feature of most relational DBMS’s, and appears in a
number of other contexts as well.
The Relational Model
• The relational model is based on tables.
• This relation, or table, describes movies: their title, the year in which they were made, their
length in minutes, and the genre of the movie.
• The structure portion of the relational model might appear to resemble an array of structs in C.
Array of structures
struct time
{
int hour;
int minutes;
int seconds;
};
.
.
.
struct time timeUpdate (struct time now);
struct time testTimes[5] = { { 11, 59, 59 }, { 12, 0, 0 }, { 1, 29, 59 }, { 23, 59, 59 }, { 19, 12, 27 }};
What is the output of this program slide1
#include <stdio.h>
struct time
{
int hour;
int minutes;
int seconds;
};
int main (void)
{
struct time testTimes[5] =
{ { 11, 59, 59 }, { 12, 0, 0 }, { 1, 29, 59 },
{ 23, 59, 59 }, { 19, 12, 27 }};
int i;
for ( i = 0; i < 5; ++i )
{
printf ("Time is %.2i:%.2i:%.2i", testTimes[i].hour,testTimes[i].minutes, testTimes[i].seconds);
testTimes[i] = timeUpdate (testTimes[i]);
printf (" ...one second later it's %.2i:%.2i:%.2i\n",testTimes[i].hour, testTimes[i].minutes, testTimes[i].seconds);
}
return 0;
}
What is the output of this program slide2
struct time timeUpdate (struct time now)
{
++now.seconds;
if ( now.seconds == 60 ) The output is:
{ // next minute Time is 11:59:59 ...one second later it's 12:00:00
now.seconds = 0; Time is 12:00:00 ...one second later it's 12:00:01
++now.minutes; Time is 01:29:59 ...one second later it's 01:30:00
if ( now.minutes == 60 ) Time is 23:59:59 ...one second later it's 00:00:00
{ // next hour Time is 19:12:27 ...one second later it's 19:12:28
now.minutes = 0;
++now.hour;
if ( now.hour == 24 )
now.hour = 0;
}
}
return now;
}
The semi-structured model

• Semistructured data resembles trees or graphs, rather than tables or


arrays.
• The principal manifestation of this viewpoint today is XML, a way to
represent data by hierarchically nested tagged elements.
• The operations on semi-structured data usually involve following paths in
the implied tree from an element to one or more of its nested sub-
elements, then to sub-elements nested within those, and so on.
<Movies>
<Movie title="Gone With the Wind">
<Year>1939</Year>
<Length>231</Length>

How to store <Genre>drama</Genre>

XML data?
</Movie>
<Movie title="Star Wars">
<Year>1977</Year>
<Length>124</Length>
<Genre>sciFi</Genre>
</Movie>
<Movie title="Wayne’s World">
<Year>1992</Year>
<Length>95</Length>
<Genre> comedy</Genre>
</Movie>
</Movies>
Why the Provides a simple, limited approach to structuring
data, yet is reasonably versatile, so anything can be
relational modeled.
model is
widely
Provides a limited, yet useful, collection of
accepted operations on data.

Even with the known limitations, it was possible to


implement languages, such as SQL, that enable the
programmer to express their wishes at a very high
level.
• The relational model gives us a single way to represent
data: as a two-dimensional table called a relation including
1. Attributes:The columns of a relation are named by
attributes.

Basics of the 2. Schemas:The name of a relation and the set of


relational model attributes for a relation is called the schema for that
relation
slide 1 Example: Movies(title , year, length , genre)

3. Tuples: The rows of a relation, other than the


header row containing the attribute names
Example: (Gone With the Wind, 1939, 231, drama)
4. Domains: The elementary type of what will be
stored
Example: Movies(title:string, year:integer, length:integer,
genre:string)

Basics of the 5. Instances: Any defined set of tuples for a given


relation.

relational model 6. Keys: A set of attributes forms a key for a relation if


slide 2 we do not allow two tuples in a relation instance to
have the same values in all the attributes of the key.
One main example is the student ID. Keys are
Underlined
Example: Movies( title , year , length , genre)
More about the relational model

• The relational model requires that each component of each tuple be of some elementary type such
as integer or string. It can not be a a record structure, set, list, array, or any other type that
reasonably can have its values broken into smaller components.
• In the relational model, a database consists of one or more relations.
• The set of schemas for the relations of a database is called a Relational database schema, or just a
database schema.
• Follow the convention that relation names begin with a capital letter, and attribute names begin
with a lower-case letter.
• Ordering the relations by a specific attribute generates an acceptable equivalent view of the
database.
Full schema can look like
Lets test our
knowledge
Indicate the following:
a) The attributes of each relation.
b) The tuples of each relation.
c) The components of one tuple from each
relation.
d) The database schema.
e) A suitable domain for each attribute.
f) Another equivalent way to present each
relation…..And how many can we get?
g) Give examples about possible keys.

You might also like