You are on page 1of 75

Database

Design

Claire Belliard
Introduction
Introduction

Imagine yourself with a relatively complex data set

Example: A company needs to keep a large volume of


information:
- names, addresses, salaries, supplier addresses,
quantities, prices of items, financial balance sheet, etc.

Imagine that you would also like to store this information in a


structured way.

Since the beginning of the computer age, data management has


always been a challenge
>>How to organise and store this data?
Introduction

A first approach: file management systems.


 we associate a set of data, in the form of files, for each application developed.

Data in files

Data in files
Introduction

A first approach: file management systems.


 Disadvantages
 Redundancy and duplication of data
 Some identical information is replicated in several files
Example: Address and telephone number of an employee
• in the payroll system file
• in the personnel management file
 Over time, unnecessary growth of:
• the file set;
• the size of the files;
• file access times.
 Risk of data inconsistencies
Example: Risk of data inconsistency if the change of address is not done in both files
Introduction

the database approach


 Solution: a common, fully centralised data bank
>> the database

 In the database, there will be :


 Only one copy of each piece of data
ex : an employee's telephone number managed in one place
 Management of several million pieces of data
 All users will have access to the data by communicating only with the database
(no intermediaries).
 Protection measures for confidential information
 Data backup on a disk (dedicated server)
Introduction

the database approach


Definitions
 A DBMS (Database Management System) is a set of programs that:
DBMS

 manages a set of files (database).


 allows users to retrieve or store data.

 A DB (database) is a collection of "persistent" data used by computer systems.


"Persistent" = The life span exceeds that of the execution of a program
Databases are designed to handle large volumes of information.

 SQL (Structured Query Language)


 A language used to interact with a database.

 DBA (DataBase Administrator)


 Manages the installation and maintenance of DBMS and databases
Introduction

the database approach


Scheme :

Organised data users


DBMS
Database server
Hierarchical data

users
Classified data

Users: Administrators,
programmers, end users
(application related)
Introduction

A bit of history
The concept of the database was born in the 1960s, with the widespread use of
magnetic disks allowing direct access to data

End of the 1960s: appearance of the first DBMSs, network and hierarchical systems

From 1970: second generation of DBMS, relational systems


1973: first definition of the SEQUEL language, which was renamed SQL
1979: first commercial version of the Oracle DBMS
1983: DB2, IBM's relational DBMS, is released
1993: Microsoft markets Sybase under the name SQL Server

Early 1990s: third generation of DBMSs, object-oriented relational systems


Introduction

A bit of history
The main DBMSs today:
• Object-oriented relational DBMS: data is represented in different tables that can be
linked together.
• NoSQL DBMS: data is organised in other structures:
• key-value: for example, a dictionary that associates a definition (value) with each
word (key)
• graph-oriented: associates each element with related elements (e.g. a person's
friends)
• document oriented...

We will choose a DBMS adapted to our context.

In this training, we will focus on object-oriented relational DBMS


Introduction

A bit of history
The main DBMS today :

2019
Introduction

A bit of history
The main DBMSs today :
 The different DBMSs differ in cost, the volume of data that can be managed, the number
of users who can simultaneously query the database, the ease of interfacing with other
applications, etc.
 Main publishers: IBM, Oracle, Microsoft
 PC: Access, Foxpro, Paradox ...
 Mainframe: Oracle, DB2, Sybase, SQL Server, ...
 Freewares and Sharewares: MySQL, PostgreSQL or MariaDB .
 A DBMS consists mainly of an engine and a graphical interface. For example, the
phpmyadmin web application for MySQL
 DBMS server: computer hardware on which a DBMS is installed It must have the
qualities of a file server (good access to disks) and an application server (well-sized
central unit, sufficient RAM)
Object-oriented
relational DBMS
Object-oriented DBMS

Object-oriented relational DBMS

Let's adopt an object-oriented approach (AOO)

An object represents a concept or an entity in the "real" world.


ex: a chair, a table, a person...

The functional domain is seen as a composition of all these


objects.
Ex: functional domain: school
A school is composed of the object chair, the object table...

We will use the vocabulary of the functional domain of our


project
Ex: teaching, teacher, pupil, evaluation...
Object-oriented DBMS

Functional domain
We will organise our set of objects in a structured way

Multi customer
media
HIFI
tv category
house
garden
décoratio Garden
n lounge order

bill article

Garden tools Article


line
Object-oriented DBMS

Object-oriented approach (OOA) : basic concepts


name
The notions manipulated in object

the concept of object Object : Musician1


an object is a stand-alone element. Name : Bob Smith
It can be identified as a 'real' physical element (a Age : 32
pencil, a car...) or as an abstract or conceptual Gender : Man
element (a set, a list...). Passions : ski, guitar

The object has several characteristics, among others :


• a name: unique common name, it gives the type of the
object (ex: person)
• attributes: these are the properties of the object (ex: name, attributes values

age, gender, passions...)


• specific values for its attributes
Object-oriented DBMS

Object-oriented approach (OOA) : basic concepts


The notions manipulated in object
name

the concept of class


a class is the abstract model of an object. Class : Musician
It defines the attributes of this object.
It is from this abstract model that will be created Name
"concrete" objects Age
Gender
Passions
-> the class defines the characteristics of all the
objects of this class (name, list of attributes).

List of attributes
Object-oriented DBMS

Object-oriented approach (OOA) : basic concepts


Class : Musician
The notions manipulated in object Name
Instantiation
Age
Gender
The concept of instance Passions
Instantiation is the action of creating an object from
Object : Musician1
a class.
Name : Bob Smith
The result of this creation is an instance of the class. Age : 32
The instances of a class are the occurences of Genre : Man
objects corresponding to this class (ex: the person of Passions : ski, guitar
28 years).
Object : Musician2
Name : Diane Cope
In practice, the terms object and instance are often Age : 28
equivalent. Gender : Woman
Passions : boxing
Object-oriented DBMS

Classe : Musicien
Object-oriented approach (OOA) : basic concepts
Name
Age
The concepts manipulated by the database Gender
Passions

A relational database is a set of tables (called


"tables").
These tables correspond to the classes in OOA. Table : Musicien

Each column of a table represents an attribute. Column 1 : Column 2 : Column 3 :


Name Age Gender

Each row (also called tuple), represents an row 1 Bob Smith 32 Man
instance, giving the value of each attribute of the
object row 2 Diane 28 Woman
Cope
Object-oriented DBMS

Object-oriented approach (OOA) : "translation" in a relational database

The concepts manipulated by the database

A relational database is composed of a set of tables,


which can be linked together.
This way of organising the elements is based on what
is known as a relational model.
Conceptual modelling
Conceptual modelling

In order to model the object composition of the functional


domain of the database, we will use a design step.

The two most commonly used methods are :

• the use of the standardised graphic modelling language called


UML
-> via the class diagram

• the method of analysis and design of computer projects,


called Merise
-> via the Conceptual Data Model (CDM)
-> via the Physical Data Model (PDM)
Conceptual modelling

The class diagram (UML)


describes what you think you need to record as information, but
also how to record it (structuring the information)

Example of a class diagram


Conceptual modelling

Merise
MERISE is a French method born in the 1970s, developed by Hubert Tardieu. It was then
put forward in the 1980s, at the request of the Ministry of Industry, which wanted a
method for designing Information Systems.

MERISE is therefore a method of analysis and design of Information Systems, based on the
principle of the separation of data and processing.
It has a certain number of models (or schemas) which are divided into 3 levels:
• The conceptual level,
• The logical level,
• The physical level

=> this is the method we were going to study in this training


Conceptual modelling

Modelling a database at the conceptual level


We will develop the conceptual data model (CDM)
The CDM is a graphic and structured representation of the information stored by an
Information System.

The CDM is based on two main concepts: entities and associations, hence its second
name: the Entity/Association diagram.

The development of the CDM involves the following stages:


1. Setting up management rules (if these are not given to you),
2. Drawing up the data dictionary,
3. The development of the CDM (creation of entities and then associations and
addition of cardinalities).
Conceptual modelling

1- CDM – management rules


Before you start creating your tables, you need to know the needs of the future users of
your application.
And from these needs, you must be able to establish the rules for managing the data to be
kept.

Let's take the example of a developer who has to model a music group.
He is given the following management rules:
• For each musician, we must know the name, age, gender, passion
• For each group, we must know the name
• A group can be made up of one or more musicians, whose names are known

These rules are sometimes given to you, but you may have to establish them yourself.
You should ask the future users of your project to establish these rules yourself with
sufficient precision
Conceptual modelling

2- CDM - The data dictionary


The data dictionary is a document that groups together, all the data that you will have to
keep in your database (and which will therefore appear in the CDM).
For each data item, it indicates :
• The code: a label designating the data (for example, "title_b" for the title of a book)
Be careful when choosing the code:
- If the code is not appropriate, the system can quickly become unintelligible
- The code for an element must be unique
- Write in unaccented letters, without spaces (replace with a _)
• The designation: a statement describing what the data corresponds to (ex: "book title")
Conceptual modelling

2- CDM - The data dictionary


For each data item, it indicates :
• The type of data:
• A or Alphabetic: data composed only of alphabetic characters (from 'A' to 'Z' and
from 'a' to 'z')
• N or Numeric: data composed only of numbers (integers or real numbers)
• AN or Alphanumeric: data composed of both alphabetic and numeric characters
• Date: the data is a date (in YYYY-MM-DD format)
• Boolean: True or False
• Size: expressed in number of characters or digits.
• Additional remarks (ex: if a data item is strictly greater than 0, etc).
Conceptual modelling

2- CDM - The data dictionary


For the example of our music group, we can establish the following data dictionary:

code Designation Type Size remarks


id_m Numerical identifier of a musician N
name_m Name of a musician AN 30
age_m Age of a musician N 2
gender_m Gender of a musician AN 15
passion_m Passions of a musician AN 1000
id_g Numerical identifier of a group N 6
name_g Name of a group AN 30
Conceptual modelling

2- CDM - The data dictionary

The id attribute

In the data dictionary, in order to be able to identify a row in a unique way, we will
generally add an attribute (a column) reserved to receive a unique identifier.

You can call it as you wish, but in general, it is named id .


Then we add the name of the entity as a suffix: id_m for the identifier of the musician
entity
Conceptual modelling

3-1 CDM - The entities Class : Musician

In the CDM, the class is called Entity Nom


Age
Genre
An entity is represented by a rectangular box, including
Passions
several parts separated by horizontal rows:
• in the upper part: the name
• in the lower part: the list of attributes
• The first attribute is the identifier of the entity Musician
(underlined to distinguish it). n° musician
name
age
gender
passion
Entity Musician
Conceptual modelling

3-1 CDM - The entities

Thus, if we take our previous data dictionary, we schematize for example the entities
"Musician" and "Group" as follows:
Musician Group
id_m id_g
name_m name_g
age_m
gender_m
passion_m
Entity Group
Entity Musician
Conceptual modelling

3-2 CDM - The associations


A relational database is a system with relations between its different parts (the tables)

In the CDM, the relationship between two entities is called an association.

To model an association between two entities, draw a row between them.

Musician
Group
id_m
id_g
name_m
name_g
age_m
gender_m
passion_m
Association between the Musician entity and the Group entity
Conceptual modelling

3-2 CDM - The associations


You can, if necessary, specify a name for this association in the middle of the row
This is usually a verb in the infinitive, represented graphically by a rounded rectangle

Musician Group
id_m id_g
name_m form name_g
age_m
gender_m
passion_m
The relationship then reads as follows:
• "A musician forms groups "
• "A group is formed by musicians "
Conceptual modelling

3-2 CDM - The associations : attributes


It is possible to add attributes to an association.
Imagine an association representing concerts given by a music group in show
locations : I can add, for example, a date attribute that specifies the date of the
concert.

Group play Location


id_g date n° location
name_g name
city
Conceptual modelling

3-3 CDM - Cardinalities


Cardinalities are used to place numerical constraints on the association: how many
instances of a class can be linked to an instance of the class on the other side of the
association.
Min1 : how many times at least the association can occur
between entity 1 and entity 2.
Max1 how many times at most the association can occur
between entity 1 and entity 2
Min2 how many times at least the association can occur
between entity 2 and entity 1
Max2 how many times at most the association can occur
between entity 2 and entity 1.

To remember the order of cardinalities, you can use the question :


• For an entity1 how many entity2 ? Between min1 and max1
• For an entity 2 how many entity 1 ? Between min2 and max2
Conceptual modelling

3-3 CDM - Cardinalities


For the musicians' CDM :
• For one musician, how many Groups?
A musician can belong to none, one or several groups: 0 to n groups
• For a group, how many musicians?
A group can be formed by one or several musicians: 1 to n musicians
Musician Group
id_m 0,n id_g
name_m form
1,n name_g
age_m
gender_m
passion_m

Cardinalities between Musician and Groups


Conceptual modelling

Normalization of cardinalities: a minimum cardinality is


3-3 CDM - Cardinalities always 0 or 1 or n (and not 2, 3) and a maximum cardinality
is always 0, 1 or n (and not 2, 3...).

notation abbreviation signification

0,0 0 No instance
0,1 None or only one instance
1,1 1 Exactly one instance
0,n 0,* None, one or more instances (no limit)
1,n 1,* At least one instance (no maximum limit)
n,n n * At least several instances (no maximum limit)

Summary table of cardinalities


Conceptual modelling

3-4 CDM - Binary association categories

binary associations: linking two entities

Binary associations are classified into three categories, according to the cardinalities
attached to them:
[1,1] (or one to one)
[1,n] (or one to many) (or many to one),
[n,n] (or many to many)

To find the type of a binary association, it is sufficient to consider the maximum


cardinalities of the association:
• if the two maximum cardinalities are 1, we have an association [1,1].
• if one of the two maximum cardinalities is 1, but the other is not: we have an
association [1,n].
• If the two maximum cardinalities are different from 1: we have an association [n,n].
Conceptual modelling

3-4 CDM - Binary association categories


Associations are classified into three categories according to the cardinalities attached to
them

1- [1,1] : one-to-one
Person Chair
0,1
Sitting
0,1

•For one person, how many chairs ?


One person can sit on none or 1 chair (at a time)
0 to 1 chair
•For one chair, how many persons ?
A chair can be sat on by none or one person
0 to 1 person
Conceptual modelling

3-4 CDM - Binary association categories


Associations are classified into three categories according to the cardinalities attached to
them
2- [1,n] : one-to-many or [n,1] many-to-one
newspaper article
0,n
contain
0,1
•For a newspaper, how many articles?
A newspaper contains none (newspaper in preparation), one or several articles
0 to n articles
.For one article, how many newspapers?
An article is contained in none (in the process of being written) or only one
newspaper
0 to 1 newspaper
Conceptual modelling

3-4 CDM - Binary association categories


Associations are classified into three categories according to the cardinalities attached to
them
3- [n,n] : many-to-many

musician group
0,n
form
1,n

•For one musician, how many groups?


A musician can be part of none, one or several groups: 0 to n groups
•For one group, how many musicians?
A group can be formed by one or several musicians: 1 to n musicians
Conceptual modelling

3-4 CDM - Binary association categories

Functional domain: maternity


woman children

•For one woman, how many children?


•For one child, how many women?

Functional domain : home

woman children
Conceptual modelling

3-4 CDM - Binary association categories

Functional domain: maternity

1,n children
woman many-to-one binary association
Give birth
1,1

•For one woman, how many children? 1 to n children. A woman can give birth to 1 or several children
•For one child, how many women? 1 to 1 woman. A child can be given to birth by a single woman

Functional domain : home

0,n
woman children
welcome many-to-many binary association
0,n

•For one woman, how many children? 0 à n children. A woman may have no children or several children in her home
•For one child, how many women? 0 à n woman. A child may be cared for by one or several women in a home
Conceptual modelling

3-4 CDM - Binary association categories

Functional domain : engineering school

school Classroom subject


Conceptual modelling

3-4 CDM - Binary association categories

Functional domain : engineering school

1,n Classroom 1,n


school subject
comporter composer
1,1 1,n
Data types
Data types

Data types

In computer programming, a data type, or simply a type, defines the nature of


the values that a given data can take

there are different types of data:


• Numeric : integer and decimal numbers
• Alphanumeric: text, strings
• Time : date, hour
Data types
Numeric data types
Integers

The difference between these types


is the number of bytes (so the place
in memory) reserved for the value of
the field

The INT type is the most common


choice. It offers a good compromise
between storage size, range of
possible values and performance

If you try to store a value outside the range allowed by the type of your field, the DBMS will
store the nearest value.
For example, if you try to store 12457 in a TINYINT, the stored value will be 127
Data types
Numeric data types
Integers
attribut UNSIGNED
• You can also specify that your columns are UNSIGNED
• UNSIGNED allows you to say that you will always have a positive value.
• In this case, the length of the interval remains the same, but the possible values
are shifted, with the minimum being 0

Example: column of type TINYINT UNSIGNED :


we can go from 0 to 255 (instead of -128 to 127).
Data types
Numeric data types
Integers
display size with (x)
You can specify the minimum number of digits to be displayed in a column of integer
type. This number must be specified between brackets: INT(x).
Note that :
• You can always store a number with a number of digits higher than the defined
number. (within the limit of the accepted range)
• if you store a number with less than the defined number of digits, the default
character will be added to the left of the number, so that it takes the right size. If not
specified, the default character is the space
Example: column of type INT(2)
• There are at least two integer digits in the display
• Integers with more than two digits can always be stored (ex: 45282)
• the value "9" will be stored as “ 9" (with a space to the left)
Data types
Numeric data types
Integers
display size with (x)

This display size is usually used in combination with the ZEROFILL attribute.
This attribute adds zeros to the left of the number when it is displayed, so it changes the
default character to '0‘

Example: column of type INT(4) ZEROFILL


Number stored Number displayed
23 0023
5689 5689
785164 785164
Data types
Numeric data types

Decimal numbers: NUMERIC and DECIMAL


The type to use is DECIMAL(p,s), or NUMERIC(p,s): DECIMAL and NUMERIC are synonyms.
The parameters p (precision) and s (scale) represent :
• p (precision): the total number of maximum digits in the number.
- For example, if p is 4, we accept digits such as 12, 1234, 1.2, 12.12, 1.234
• s (scale): the maximum number of digits after the decimal point
- For example if s is 4: we accept a maximum of 4 digits after the decimal point:
1.1234, 1.12, 1
- The parameter s (scale) is optional and is set to 0 by default:
DECIMAL(4) is equivalent to writing DECIMAL(4, 0) -> we store integers!
- If there are too many digits after the decimal point, MySQL will round up to the
defined scale
Data types
Numeric data types

Decimal numbers: NUMERIC and DECIMAL

Example: column of type DECIMAL(5,3)


• you can store numbers with a maximum of 5 significant digits, 3 of which are after the
decimal point.
• For example: 12.354, -54.258, 89.2 or -56.
Data types
Numeric data types

The boolean type exists as a keyword in MySQL.


BOOLEAN or BOOL

But it is a "false" boolean: boolean is a synonym of TINYINT(1) (i.e. an integer of size 1)


Boolean can therefore take 19 integer values (from -9 to 9), and not the values TRUE or
FALSE.

You must therefore fill in this field correctly, with by convention


• FALSE => 0
• TRUE => 1

But MySQL will not block a "2" value in this field...


Data types
alphanumeric data types
Text strings: CHAR(x)

• the CHAR type is used to store text of less


than 255 characters
• It is used with a parameter (x) which specifies
the size that your text can take,
so the size (x) can be from 1 to 255.
• A CHAR(x) always stores x characters, filling
the text with spaces if necessary
• if you enter text longer than the maximum
size defined for the field, it will be truncated.
• CHAR(x) is used when you know you will
always have x characters,
ex: a postcode.
Data types
alphanumeric data types
Text strings: VARCHAR(x)
• The VARCHAR type is used to store text of
less than 255 characters
• It is used with a parameter (x) which
specifies the size that your text can take,
so the size (x) can be from 1 to 255.
• a VARCHAR(x) will store up to x characters
(between 0 and x), so it adapts to the size of
the stored text
• if you enter text longer than the maximum
size defined for the field, it will be
truncated.
• A VARCHAR(x) is used when the length of
your text may vary from line to line
The VARCHAR(255) type is the most common choice
Data types

alphanumeric data types


Text strings: TEXT
To store text longer than 255 characters
It is sufficient to use the TEXT type, or one of its derivatives TINYTEXT,
MEDIUMTEXT or LONGTEXT.
The difference between them is the space they allow to occupy in memory
Data types

alphanumeric data types


Text strings
UTF-8 encoding allows strings with accents.
An "é" will take more space than an "e" (two bytes instead of one).

In a text with accents and encoded in UTF-8, the accented characters take two bytes in
memory

It is therefore possible for a CHAR(5) to occupy more than 5 bytes in memory (but
impossible to store more than 5 characters).
Data types

alphanumeric data types


Binary strings

A binary string type is used to store "raw" data


Example: image, audio, EXE file, ZIP...

Binary types are defined in the same way as text string types.
• VARBINARY(x) and BINARY(x) are used to store binary strings of up to x characters
(with the same memory management as VARCHAR(x) and CHAR(x)).
• For longer strings, there are the TINYBLOB, BLOB, MEDIUMBLOB and LONGBLOB types,
with the same storage limits as the TEXT types.
Data types

alphanumeric data types


Binary strings

Warning
Databases cannot read or understand the unstructured contents of a binary string and
can only record them globally.
The database can only read the file name, file type and file size.

Database features such as sorting, filtering and searching for specific contents are
therefore not possible in a binary string.
Data types

temporal data types

The DATE type is used to enter a date


To enter a date, the order of the data is the only constraint.
• The year must be given first (two or four digits), YY or YYYY
• then the month (two digits), MM
• and finally the day (two digits), DD
You can enter a date as a number or as a string. If it is a string, any punctuation can be used
to delimit the parts (or none).
Some correct examples:
• 'YYYY-MM-DD' (this is the format in which a DATE is stored in MySQL)
• 'YYMMDD'
• 'YYYY/MM/DD' When the century is not specified, MySQL will decide:
• 'YY+MM+DD' • if the year given is between 00 and 69, the 21st century
will be used, i.e. from 2000 to 2069;
• YYYYMMDD (number)
• on the other hand, if the year is between 70 and 99, the
• YYMMDD (number) 20th century will be used, i.e. between 1970 and 1999.
Data types
temporal data types

The DATETIME type allows you to store a date AND a time.


To enter a DATETIME, it is the same principle as for DATE :
• for the date, year-month-day,
• and for the time,
• you must first give the hour, HH
• then the minutes, MM
• then the seconds, SS
If a string is used, the date and time must be separated by a space.

Some correct examples:


• 'YYYY-MM-DD HH:MM:SS' (this is the format in which a DATETIME is stored in MySQL)
• 'YY*MM*DD HH+MM+SS'
• YYYYMMDDHHMMSS (number)
MySQL supports DATETIME from '1001-01-01 00:00:00' to
'9999-12-31 23:59:59'.
Data types
temporal data types

The TIME type allows not only to store a precise time, but also a number of days.
It is therefore not limited to 24 hours.
As in DATETIME, the hour must be given first, then the minutes, then the seconds, each
part being separated from the others by the character.

Some correct examples:


• 'HH:MM:SS'.
• 'HHH:MM:SS'.
• 'MM:SS'.
• 'D HH:MM:SS' -> When specifying a number of days, then the days come first and are
separated from the rest by a space.
• 'HHMMSS'
• HHMMSS (number)
MySQL supports TIME from '-838:59:59' to '838:59:59'.
Data types

temporal data types


Checking the default date
When you store a date in MySQL, some checks are made on the validity of the date
entered.
However, these are basic checks:
• the day must be between 1 and 31
• and the month between 1 and 12.

It is therefore possible to enter a date such as 31 February 2011...


Data types

temporal data types


The default date

When MySQL encounters an incorrect date/time, or one that is not within the validity range
of the field, the default value is stored instead.
This is the "zero" value of the type.
Data types

Warning
It is important to understand the uses and particularities of each data type, in order to
choose the best possible type when defining the columns of your tables.
Indeed, choosing the wrong data type could lead to :
 wasting memory (ex: if you store very small data in a column designed to store large
amounts of data) ;
 performance problems (ex: it is faster to search on a number than on a string);
 behaviour contrary to that expected (ex: sorting on a number stored as an INT, or on a
number stored as a VARCHAR will not give the same result);
 the impossibility of using features specific to a data type (ex: storing a date as a string
deprives you of the many temporal functions available).
Data types
Data types

For these different fields, find the most appropriate data type
• The type of value returned should match the values you are recording, so that their
treatment is optimal.
• The limits should be as restrictive as possible, but you must remember to leave enough
place for the column to meet all your needs.
• The weight should be as small as possible (number of bytes)

IP Addresses
E-mail address
Postcode
Name of a city
Currency
Phone number
Age
Data types
Data types
IP addresses
Most often, they are stored in a CHAR(15) column (15 bytes)
However, space can be saved by using an INT UNSIGNED integer (4 bytes), but we lose the
information on the : or the .

Value examples:
100.64.0.0/10
255.255.255.255
2000::/3

ip v4 : char(15)
ip v6 : char(11)
Data types
Data types
E-mail address
An e-mail address consists of three parts:
• a local part: RFC 2821 indicates that it must not exceed 64 characters
• and a domain name: limited to 255 characters
• These two elements are separated by the @ sign.

The maximum length would therefore be 64 + 1 + 255, i.e. 320 characters. We must
therefore use a TEXT.
Data types
Data types

Postcode
• In France, it is 5 digits. We can therefore use a TINYINT UNSIGNED which takes 1 byte
and can store values from 0 to 255.
We could have chosen CHAR(5) but it takes more memory space than a TINYINT
• Internationally, postcodes can also contain letters and be up to 10 characters long (for
example, the ZIP+4 code in the USA).
A CHAR(10) will therefore be more suitable.
Data types
Data types

Name of a city
According to Wikipedia, the longest city name in France is 45 characters long.
For the rest of the world, we should be wide enough with a field sized to accommodate 60
letters.
Data types
Data types

Currency
The ISO 4217 codes can be used.
For the Euro, the value EUR will be used and USD for the American dollar.
The column type in MySQL will therefore be a CHAR(3).
Data types
Data types

Phone number
INT(10) UNSIGNED ZEROFILL
the ZEROFILL attribute is used to add the missing zeros to make 10 digits.
Data types
Data types
Age
TINYINT UNSIGNED

You might also like