Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

55
Database Modelling Lecture 1: Introduction to Databases Akhtar Ali June 19, 2022 1

Transcript of Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Page 1: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Database Modelling

Lecture 1: Introduction to DatabasesAkhtar Ali

April 11, 2023 1

Page 2: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Learning Objectives

To consider “What is a Database ?”To consider “What is a Relational Database ?” i.e. what is a “relation”, and therefore what

differentiates a relational database from any other kind of database.

To consider how Set Theory defines the structure of relations

To consider how relations can be exploited to provide the equivalent of bags of duplicates & ordered sequences.

April 11, 2023 2

Page 3: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

What is a Database ?

In order to understand what we mean by the term database, we will provide a definition and some of the main features associated with it.

Definition :

“A collection of data that is permanently stored on a computer”.  In a database we should be able to :

have different types of data in the collection

record relationships between different data items

have varying sizes of data collections to suit our needs

April 11, 2023 3

Page 4: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

We need to be able to

Insert new data, delete old data, and amend

existing data in the collection. Retrieve data from the collection. Manage the collection so that it can be

permanently stored in the face of various hazards that would otherwise corrupt or lose data in it.

April 11, 2023 4

Page 5: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Permanent Data Storage

By “permanent” is meant that once data is put into the computer, it stays there until someone explicitly removes it, or until the computer is damaged or broken and this causes the data to be lost.

The data is not lost when the computer is switched off or the database is not used for a long time.

A computer has 2 classes of data storage : Random Access Memory (RAM). This is used by the computer’s Central

Processing Unit (CPU) to temporarily hold data that the CPU is processing.Backing Store. This is used to store data permanently. Typically magnetic discs

are used for this purpose, although other types of storage device, e.g. compact discs and magnetic tapes, are also used.

Thus the database invariably uses the backing store to make the data storage

permanent.

April 11, 2023 5

Page 6: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Other Memory Types

In future, as RAM becomes ever cheaper, it may be used to store databases, but it will need to use backing store to keep the data permanently as the RAM can not when the computer is switched off.

For completeness, a third category of modern

usage is that of ‘switch’ in communication links, e.g. in computerised telephone exchanges and Internet routing nodes.

April 11, 2023 6

Page 7: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Different Types of Data An item of data represents a fact. There are different kinds of fact, and hence there are different kinds or types of data to represent different kinds of fact. Some of the different kinds of data are shown here:

Numbers: Dates: Audio: Videos:

Text: Pictures: Maps: etc.

1, -999, 169.58

14 January 2004

‘Janet & John’

April 11, 2023 7

Page 8: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

DescriptionsTo store useful information, many types of data may be needed

together. As well as the obvious ones such as numbers, text, dates etc. there are also more complex types of data such as pictures, audio and video clips.

In order to describe something, such as a species of bird for example, a

number of different kinds of facts might be necessary to complete the description.

Example: Recording observations of a species of bird.

Number of birds observed, textual description of characteristics, dates of observations, map of migration routes, pictures of birds, audio recording of bird calls, video of flight.

April 11, 2023 8

Page 9: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Relationships Among Data• In practice, we are not interested in just isolated items of data. We also want

to know the relationships among various data items. – Certain relationships are essential to make sense of the data. – Isolated facts are rarely meaningful. – The facts need to be given a context to make them meaningful, and the

context is commonly provided by relating the data that represent facts.

• For example, a number on its own has no significance; but if we relate it, say, to a particular bird species and a particular date, then it becomes meaningful if we interpret it as the number of birds seen of that species on that date. – This would be an essential relationship.

Example: Species of bird observations

It is important to know which bird numbers, descriptions, etc relate to which bird species. It may also be useful to know which bird species have similar migration routes.

April 11, 2023 9

Page 10: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Open-ended Relationships

Other relationships may be ‘optional’ in the sense that some users may find it useful to compare (say) migration routes, but many other users of the data will not use the relationship between these different facts.

All sorts of relationships are possible: some

essential to make sense of data, some useful for different purposes.

April 11, 2023 10

Page 11: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

What is a Relational DB ?

Definition :

“A database in which all the data is stored in relations”.

April 11, 2023 11

Page 12: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Relation Example

• A relation is a simple logical structure, which contains related data (hence the name “relation”). It can be pictured as a table of data:

Example : A relation storing the employee numbers (= ENo), employee names (= EName), marital status (= M-S), and salaries (= Sal) of 4 employees. Row data is related because it all refers to the same employee. The rows are related because they all relate to employees.

2 4 6

8

M-S

6

2 4

8

2 S

M

D

8 M

5 6 7

8

EName

7

5 6

8

5 Smith

Mitchell

Robson

8 Blake

1 2 3

4

View : CAR_OWNER

1 2

4

1 E3

E5

E1

4 E6

2 4 6

8

Sal

6

2 4

8

2 12,500

21,000

32,500

8 54,000

ENo

April 11, 2023 12

Page 13: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Purpose of Relation

Thus a relation not only holds a collection of data, but also relates together the data items that it holds.

This is important because, as we have noted already, in reality we need to relate data together to ensure that it is meaningful to us.

April 11, 2023 13

Page 14: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

A Relation Example

4 attributes, depicted as columns

4 tuples, depicted as rows 2

4

6

8

M-S

6

2

4

8

2 S

M

D

8 M

5

6

7

8

EName

7

5

6

8

5 Smith

Mitchell

Robson

8 Blake

1

2

3

4

ENo

3

1

2

4

1 E3

E5

E1

4 E6

2

4

6

8

Sal

6

2

4

8

2 12,500

21,000

32,500

8 54,000

A relation consists of a set of tuples. Each tuple must hold the same kinds of data. In the example below, each tuple represents one employee.

April 11, 2023 14

Page 15: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Tuples/types• Each tuple consists of a set of attributes. The data in each attribute

is related. – In this case, each tuple relates the data pertaining to a single

employee. Each attribute can contain data of any type. In this simple example, each attribute contains textual or numeric data.

• Technically, data types are orthogonal to relations. – This means they are independent of relations, and therefore data

types do not constrain relations and vice versa.

• Commercial relational DBMSs that apply this orthogonality are typically called Object-Relational DBMSs – because they can hold any kind of object as a data type in a relation.

• As relations are just structures, they can hold any kind of data. – So they can hold all the different kinds of data mentioned earlier, and

more besides, without limit.

April 11, 2023 15

Page 16: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Relational DatabasesIn computing, a way of looking at data and thinking about its handling is

called a model. A relation is a way of looking at data. Relations themselves are based on simple mathematical principles. Relations can be manipulated by users in ways that are conceptually simple. A relational DB is perceived and used as a collection of relations. The relational DBMS manages the physical processing of data so that the user

doesn‘t need to know what underlying computer processing goes on and merely perceives things in terms of relations.

April 11, 2023 16

Page 17: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Relational Data Model• So, a relational database utilises the relational

data model.• The relational model raises the level of

abstraction, compared to the normal 3GL level of programming with its use of files to store data. – It enables the database programmer to think about

the DB in a way that is further from the way that the hardware actually works and nearer to the way that the outside world is perceived.

– This makes it easier for the programmer to solve problems and hence makes him/her more productive.

April 11, 2023 17

Page 18: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Importance of Relational DBMSs• Because relational databases are built on sound theoretical

mathematical principles, the theory is very practical. – It makes relational databases easy to learn and use. – Nevertheless, relational databases can be very powerful and

flexible. • Therefore, relational databases :

– are the most common type of database on the market (over 90% share);

– are extremely important in practice.

• Commercial relational DBMSs usually use a database programming language called SQL.

April 11, 2023 18

Page 19: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

SQL• SQL stands for Structured Query Language.

• SQL is sometimes pronounced “S Q L” and sometimes as “Sequel” (which was its original name when it was invented by IBM). The latter pronunciation is particularly prevalent in America.

• Terminology • Because relations are usually depicted as tables, the word table is used in

SQL instead of relation. – Consequently, as tables have columns and rows, the following tabular names

are used in SQL (and often more generally) instead:

table instead of relationcolumn instead of attributerow instead of tuple

April 11, 2023 19

Page 20: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Principles

Graphical                                                  Text

QBE SQL

Oracle

MS Access

MS SQL Server

Calculus                      Algebra

Domain               TupleRAQUEL

IBM’s DB2

Sybase

Ingres

Relational Implementations

The following diagram illustrates the relational implementation and example software systems.

April 11, 2023 20

Page 21: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Two Languages • Relational databases were developed mathematically using

two kinds of language to manipulate relations:

– relational calculus (based on predicate calculus)

– relational algebra (based on traditional algebra, of which arithmetic is the most common example)

• Relational calculus comes in two forms:

– domain calculus– tuple calculus.

April 11, 2023 21

Page 22: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Status for Relational DB• The domain calculus can be represented graphically, and an IBM product

called QBE typifies this. – QBE is no longer used commercially,– but the graphical interface to Microsoft Access represents a watered-down

version of it.

• SQL uses a combination of tuple calculus and algebra ideas in its design, together with a number of its own idiosyncrasies. – It is entirely a textual language. – The five commercial products shown in the diagram are the main ones to

implement SQL. IBM and Oracle have the major market shares, – although Microsoft’s SQL Server is now gaining a significant share as well.

• RAQUEL is a purely algebra language developed at the School of

Informatics in the University of Northumbria. – It will be used occasionally to describe ideas because of its inherent simplicity.

April 11, 2023 22

Page 23: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Relational Standards

• QBE and relational algebra have not been standardised, whereas SQL has and later standards include additions to SQL compared to earlier standards.

• There has been a sequence of SQL standards :

– SQL:87 came out in 1987 (known as SQL1).– In 1989 there was an addendum to SQL1.– SQL:92 came out in 1992 (known as SQL2).– SQL:99 came out in 1999 (known as SQL3).– SQL:2003 came out in 2003 (known as SQL4).

• Another SQL standard is in preparation .

• However, it is important to note that the SQL standards and products do not conform in every respect to relational principles, thereby increasing the complexity of the language while weakening its power.

April 11, 2023 23

Page 24: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

SQL Products• The major SQL vendors, who seek to make the latest standard

relate to their products and be backwards compatible with earlier standards, dominate the SQL standards body. – Vendors’ SQL implementations often don’t adhere completely to SQL

standards, and so are even more ad hoc.

• No product today supports all of SQL:92 yet, although many have features additional to the standard that are peculiar to themselves, and some support some of the features of SQL:99.

• This module uses Oracle SQL, because it is popular in the database market, but note that it does not adhere completely to the SQL standards.

April 11, 2023 24

Page 25: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

What is a Relation?

The relational model is based on mathematical sets. Definition: “A set is a collection of things” A set has two main characteristics:

It has no sequence or structure

It has no duplicates Example: { 2, 7, 5, 4 } { 7, 5, 4, 2 }

They are the same set because sets have no order. Example: { 2, 2, 7, 5, 4, 8, 8 }

is not a set because it has duplicates.

April 11, 2023 25

Page 26: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Set/bagA ‘set’ whose values are ordered is a SequenceA ‘set’ with duplicates is a Bag (or Multiset)

• A set is the simplest construct that exists in mathematics. – It is merely a collection of things, called the members of the set. – It has no other properties. So sequences and bags are not sets.

• Traditionally the contents of a set are written enclosed in curly

brackets.

• In principle in mathematics, a set can be a collection of any kinds of things. For example, a set could consist of:

{ a car, a bus ticket, a building, a person, a flight, a colour }

April 11, 2023 26

Page 27: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Typed Sets

• However, in computing, sets are invariably constrained to be typed sets, meaning that the members of a set all have to be of the same kind or type. – Thus, the above example is not a typed set (unless you can

find some way of generalising them all so that they are indeed members of some strange type !).

– Typed sets would be a set of cars, a set of bus tickets, etc.

• Databases in general and relational databases in particular all use typed sets. Look out for how this applies as you work through the module.

April 11, 2023 27

Page 28: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Relation = Set of Tuples

3 3 E1 7 7 Robson 6 6 D 6 6 32,500

4 4 4 E6

E8

8 8 8 Blake

Jones

8 8 8 M

W

8 8 8 54,000

68,000

1

2

ENo

1

2

1 E3

E5

5

6

EName

5

6

5 Smith

Mitchell

2

4

M-S

2

4

2 S

M

2

4

Sal

2

4

2 12,500

21,000

2 2 E5 6 6 Mitchell 4 4 M 4 4 21,000

4 4 4 E6 8 8 8 Blake 8 8 8 M 8 8 8 54,000

E8 Jones W 68,000

1 1 1 E3 5 5 5 Smith 2 2 2 S 2 2 2 12,500

3 3 E1 7 7 Robson 6 6 D 6 6 32,500

ENo EName M-S Sal

Definition : A relation is a set of tuples.

Hence the two relations below are the same, even though their tuples appear in a different order.

April 11, 2023 28

Page 29: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Ordering of Tuples• Neither version of the relations has any particular rhyme or reason for the

ordering of its tuples.

• Because a relation is a set of tuples, its tuples are not fixed in a particular order, because an ordering is a kind of structure and sets have no structure. – Or put another way, the same relation can appear with its tuples in any order.

• In practice, a relation might have a default sequence in which it appears, this being typically determined by the physically storage of the relation.

• The number of tuples in a relation is called the cardinality of the relation. So the example relation above has a cardinality of 5.

April 11, 2023 29

Page 30: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Relation - No Duplicate Tuples

2 2 E5 6 6 Mitchell 4 4 M 4 4 21,000

3 3 E1 7 7 Robson 6 6 D 6 6 32,500

4 4 4 E6 8 8 8 Blake 8 8 8 M 8 8 8 54,000

E8 Jones W 68,000

1 1 1 E3 5 5 5 Smith 2 2 2 S 2 2 2 12,500

ENo EName M-S Sal

1 1 1 E3 5 5 5 Smith 2 2 2 S 2 2 2 12,500

4 4 4 E6 8 8 8 Blake 8 8 8 M 8 8 8 54,000

4 4 4 E6 8 8 8 Blake 8 8 8 M 8 8 8 54,000

2 Smiths

3 Blakes

This cannot be a relation as some tuples appear more than once.

April 11, 2023 30

Page 31: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Ordering• Since a relation is a set of tuples, it cannot have duplicate

tuples, because duplicates don’t exist in a set. • Sometimes it is important to deal with matters of

duplication and ordering, because these things occur in reality.

• We will now consider how these two aspects can be handled using relations.

• We will consider duplication first and then ordering……..

April 11, 2023 31

Page 32: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Storing a Bag in a Relation

The number of copies of the book.

Data about the books.

5

6

7

8

Title

7

5

6

8

5 Computing

Relational DBMS

Nuclear Physics

8 Genetics

Solar Energy

2

4

6

8

Author

6

2

4

8

2 Smith & Jones

C J Date

D Dewhirst

8 M Platt

Will & Smythe

2

4

6

8

No

6

2

4

8

2 12

21

5

8 4

6

Example: A library holds information about books, but it often has many copies of the same book.

April 11, 2023 32

Page 33: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Strategy for Storing Bags• The above example still uses a relation – each

tuple is unique. • It uses the strategy of recording one instance of

each item of data about some entity (in this case, a book), and how many times that entity occurs. – This saves storage space and is generally less

confusing. • It is what manual recording systems generally do, e.g. a

shopping list.• Use this method when the entities whose data

appear in a tuple are identical, but there can be many instances of them.

April 11, 2023 33

Page 34: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Handling a Sequence with a Relation

1 1 1 E3 5 5 5 Smith 2 2 2 S 2 2 2 12,500

2 2 E5 6 6 Mitchell 4 4 M 4 4 21,000

3 3 E1 7 7 Robson 6 6 D 6 6 32,500

4 4 4 E6 8 8 8 Blake 8 8 8 M 8 8 8 54,000

E8 Jones W 68,000

ENo EName M-S Sal

Sort sequence: ascending order of ‘Eno’

Although the DBMS presents a relation to the user as a set of tuples, we can retrieve a relation in any tuple sequence which depends on the values in the tuples.

April 11, 2023 34

Page 35: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Sequencing is Flexible• Although the relation has no particular sequence, when the DBMS retrieves a

relation full of data for the user, its tuples can be sorted into any order we like. – Therefore the user or application program can see a sequence of data.

• All DBMSs provide this feature because it is so useful in practice.

• There are many cases where it is very important, if not essential, for the tuples to appear in the right sequence.

• Example : A telephone directory. – The directory is a set of entities each consisting of a name, address and telephone number,

but it would be completely unusable if it were not sorted into alphabetic order of names !

• The advantage of the relational approach is that it can provide any sequence of tuples; different users and/or applications might want the same relation of data in different sequences, and the relational DBMS can satisfy them all.

April 11, 2023 35

Page 36: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

A ‘Sequenced’ Relation

1 1 1 E3 5 5 5 Smith 2 2 2 S 2 2 2 12,500

2 2 E5 6 6 Mitchell 4 4 M 4 4 21,000

3 3 E1 7 7 Robson 6 6 D 6 6 32,500

4 4 4 E6 8 8 8 Blake 8 8 8 M 8 8 8 54,000

E8 Jones W 68,000

ENo EName M-S Sal

Sort sequence: ascending order of ‘Sal’

1 1 1 E3 5 5 5 Smith 2 2 2 S 2 2 2 12,500

2 2 E5 6 6 Mitchell 4 4 M 4 4 21,000

3 3 E1 7 7 Robson 6 6 D 6 6 32,500

4 4 4 E6 8 8 8 Blake 8 8 8 M 8 8 8 54,000

E8 Jones W 68,000

ENo EName M-S Sal

Sort sequence: descending order of ‘Sal’

The following examples illustrate how the same relation of data can be sequenced differently depending on user/application requirements.

April 11, 2023 36

Page 37: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

A ‘Sequenced’ Relation 2

1 1 1 E3 5 5 5 Smith 2 2 2 S 2 2 2 12,500

2 2 E5 6 6 Mitchell 4 4 M 4 4 21,000

3 3 E1 7 7 Robson 6 6 D 6 6 32,500

4 4 4 E6 8 8 8 Blake 8 8 8 M 8 8 8 54,000

E8 Jones W 68,000

ENo EName M-S Sal

Sort sequence : ascending order of ‘EName’ within ascending ‘M-S’

April 11, 2023 37

Page 38: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Sorting a Relation into Sequences

Summary: A relation is sorted on the values of one or more of its attributes. Thus any sort attribute must contain orderable data. Examples. Numbers have a natural sequence

Text can be sorted alphabeticallyPhotos : there is no agreed way of sorting them

Any orderable attribute’s data can be sorted into ascending or descending

order. If more than one attribute is used for sorting, a major to minor order must be

specified.

April 11, 2023 38

Page 39: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Sorting ApproachWhere several attributes are used for sorting, then the approach is : Sort tuples using the 1st attribute’s values Where more than one tuple has the same value for the 1st attribute, then sort those

tuples on the 2nd attribute’s values Where more than one tuple has the same value for the 1st and 2nd attributes, then sort

those tuples on the 3rd attribute’s values etc. The order of 1st, 2nd, .. nth attributes is called the major to minor order, the 1st attribute

being the major attribute, and the last attribute being the minor attribute. Alternatively, tuples are said to be sorted “on the 3rd attribute within the 2nd attribute

within the 1st attribute”.

April 11, 2023 39

Page 40: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

SQL : Retrieving a Sequenced Relation

Select * From EMPLOYEE Order By Sal Asc ;

Keyword Select indicates : • Do a retrieval. • The attributes to get : * means all attributes.

Keyword From indicates the relation to use.

Keyword Order By indicates the sequencing.

Asc indicates ascending.

To retrieve the relation EMPLOYEE in order of increasing employee salary :

April 11, 2023 40

Page 41: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

SQL Sort Example• The above example retrieves the whole of the relation

EMPLOYEE from the current DB.

• Asc is the default sort order, so it could be omitted if desired. If Asc had been replaced by Desc (for descending), then the sequence would have been in order of decreasing salaries.

• The above retrieval is an example of an SQL statement, which must always be terminated with a semi-colon; – this is because some SQL statements can be very long, so the

semi-colon (rather than the end of the line) is used to indicate the end of the statement.

April 11, 2023 41

Page 42: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

SQL code• Every SQL retrieval statement must include the

Select and From phrases in it. – All other phrases - like Order By - are optional.

• The following SQL example illustrates a sequencing that uses a major and a minor attribute :

Select *From EMPLOYEEOrder By M-S Asc, EName Asc ;

April 11, 2023 42

Page 43: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Limitations of Sequencing

Job Address Owner

Fix Dripping Tap

Repair Toilet

Install Central Heating

Install new bath

21, Byker Grove, NE4 6TY

100, Osborne Rd., Jesmond

10, Conaught Drive Whitley Bay

16A, Racing View, Blaydon

Mr Bloggs

Ms J Wareing

Prof Caldwell

Mrs Johnson

This relation contains a plumber’s job list, in order of priority:-

April 11, 2023 43

Page 44: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Tuple Sequence

• Its tuple sequence is based on the urgency of the job, followed by the profit margin the plumber can make on the job. – As this data is not stored in the relation it cannot be

used for sorting on tuple values; – so the DBMS cannot retrieve the tuples in this order.

• Thus the limitation on sequencing is that it is value-based, i.e. it must be based on values held in the relation.

April 11, 2023 44

Page 45: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Overcoming Sequencing Limitations

Job Address Owner

Fix Dripping Tap

Repair Toilet

Install Central Heating

Install new bath

21, Byker Grove, NE4 6TY

100, Osborne Rd., Jesmond

10, Conaught Drive Whitley Bay

16A, Racing View, Blaydon

Mr Bloggs

Ms J Wareing

Prof Caldwell

Mrs Johnson

2

1

3

4

Priority

The solution is to add another attribute to the relation, on whose values the tuples can be sorted to create the required sequence.

Example: Add a priority attribute to the plumber‘s relation and sort on it :-

The only way to overcome this limitation is to add one or more attributes whose value(s) do reflect the sequence required, and then sort on these.April 11, 2023 45

Page 46: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Tuple Definition

• Tuple = A Set of Attributes • Definition: A tuple is a set of attributes.

• Because a relation is a set of tuples, the relation has that same set of attributes. – When depicted as a table, the attributes for each

tuple are always shown in the same sequence, for simplicity and convenience.

April 11, 2023 46

Page 47: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Column Order does not Matter

1

2

3

4

ENo

3

1

2

4

1 E3

E5

E1

4 E6

E8

5

6

7

8

EName

7

5

6

8

5 Smith

Mitchell

Robson

8 Blake

Jones

2

4

6

8

M-S

6

2

4

8

2 S

M

D

8 M

W

2

4

6

8

Sal

6

2

4

8

2 12,500

21,000

32,500

8 54,000

68,000

1

2

3

4

ENo

3

1

2

4

1 E3

E5

E1

4 E6

E8

5

6

7

8

EName

7

5

6

8

5 Smith

Mitchell

Robson

8 Blake

Jones

2

4

6

8

M-S

6

2

4

8

2 S

M

D

8 M

W

2

4

6

8

Sal

6

2

4

8

2 12,500

21,000

32,500

8 54,000

68,000

These two relations are the same, even though their attributes appear in a different sequence.

April 11, 2023 47

Page 48: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Arity of Tuples• Each tuple in the relation has the same set of named attributes.

– Of course the values in those attributes can differ in different tuples.

• The number of attributes in a tuple, and hence in a relation, is called the degree (or arity) of the tuple or relation. – So the example relations above both have a degree (or arity) of 4.

“Degree” is generally the preferred term in Britain, “arity” in America.

• A tuple/relation of degree one is said to be unary, of degree two binary, of degree three ternary, and so on; – in general a tuple/relation of degree n is said to be n-ary. – The corresponding terms of 1-tuple, 2-tuple, 3-tuple, …, n-tuple can

also be used for tuples.

April 11, 2023 48

Page 49: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Order in RetrievalIn a retrieval, such as

Select *From EMPLOYEE ;

the order in which the attributes appear in the result is a default order, typically determined by the relation’s physical storage and/or by the order in which the attributes were referenced when the relation was created.

To obtain a specific sequence of attributes in the retrieval,

simply list their names in the Select phrase in the desired order.

April 11, 2023 49

Page 50: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Sequences in SQL

Thus the attribute sequences above would be produced by the following retrievals respectively :

Select ENo, EName, M-S, SalFrom EMPLOYEE ;

Select EName, Sal, M-S, ENoFrom EMPLOYEE ;

April 11, 2023 50

Page 51: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Relation - No Duplicate Attributes

1

2

3

4

ENo

3

1

2

4

1 E3

E5

E1

4 E6

E8

5

6

7

8

EName

7

5

6

8

5 Smith

Mitchell

Robson

8 Blake

Jones

2

4

6

8

M-S

6

2

4

8

2 S

M

D

8 M

W

2

4

6

8

Sal

6

2

4

8

2 12,500

21,000

32,500

8 54,000

68,000

2

4

6

8

M-S

6

2

4

8

2 S

M

D

8 M

W

This cannot be a relation because attribute ‘M-S’ appears twice.

Duplicate attributes are those with duplicate names, whether their contents are duplicated or not. April 11, 2023 51

Page 52: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Flexibility

• If 2 attributes have the same name, we cannot tell the computer which of these 2 attributes we want, even if they do contain different data.

• The lack of a fixed attribute sequence is useful, – because we can retrieve data from the DB in any

attribute sequence we like; – and if we don‘t care what sequence the attributes

appear in, we need not bother to specify any sequence and just get the default.

April 11, 2023 52

Page 53: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

A Relation - its Definition so far

• A relation is a set of tuples, each of which is a set of attributes.

• Each tuple in the relation contains the same set of attributes by name.

• Each named attribute holds data of one specific type (domain).

• Consequently it is convenient to consider the relation as comprising named attributes, each with its particular data type/domain.

April 11, 2023 53

Page 54: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Intension/extension

• A relation’s Heading (or Intension) is its type, which is a composite type based on that of its attributes.

• A relation’s Body (or Extension) is its value, which is a composite being the value of all of its attributes in all of its tuples.

April 11, 2023 54

Page 55: Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August 20141.

Data Types Available

• In principle, any data type can be used in an attribute, – but in practice you will be limited by the range of

types available with your DBMS.

• A DB relation is in fact a variable. – It is a named object in the DB whose value can

change over time.

• Future sessions will consider relation/attribute data types in greater detail.

April 11, 2023 55