TP2 : Construire des tables…. Les Types de données pour les attibuts ou colonnes



Rappel

article sur les mots clés

article sur la Conception d’un système d’information

Objectif

Rappel : Depuis le terminal, se connecter au serveur mysql

mysql --user=votreLogin --password=JJMM --host=serveurmysql --database=BDD_votreLogin

Présentation des différents types de données dans MySQL

Les types standards (fondamentaux) sont :

Rappel : signification du mot alias en informatique

les entiers

type “Numérique”

Access MySQL Oracle PostgreSQL SQLserver
INT INT NUMBER(10) INTEGER INT
byte tinyint NUMBER(3) tinyint tinyint
smallint smallint NUMBER(5) smallint smallint
BIGINT (large number) BIGINT NUMBER(19) BIGINT BIGINT



CREATE TABLE test_int (
x1 INT,
x2 INT(6) 
);
INSERT INTO test_int (x1,x2) VALUES (1,1);
INSERT INTO test_int (x1,x2) VALUES (12,12);
INSERT INTO test_int (x1,x2) VALUES (12345678,12345678);
INSERT INTO test_int (x1,x2) VALUES (123456789,-123456789);
INSERT INTO test_int (x1,x2) VALUES (1234567890,-1234567890);
INSERT INTO test_int (x1,x2) VALUES (9999999999,9999999999);


SELECT x1,x2 FROM test_int;
DESCRIBE test_int;
DROP TABLE test_int;

# remplacer  
# par 
# x INT   
# puis par 
# x TINYINT     => conclusion :


les types “entiers”


les chiffres à virgules

type “Numérique”

Access MySQL Oracle PostgreSQL SQL server
decimal(n,d) decimal(n,d) (mariadb) number(n,d) decimal(n,d) decimal(n,d)
numeric(n,d) (mysql) number(n,d) numeric(n,d) numeric(n,d)



Sur la documentation d’Oracle, on peut lire NUMBER(p,s) qui a la même signification que DECIMAL(n,d) avec pour p “the maximum number of significant decimal digits”, donner la signification du mot digit



Conseil : FLOAT versus DECIMAL Il est conseillé d’utiliser DECIMAL qui est un nombre exact, plutôt que FLOAT qui est un nombre approximatif, si la précision requise est suffisante. FLOAT sera réservé typiquement à des calculs scientifiques nécessitant un degré de précision supérieur.


CREATE TABLE test_numeric (
y NUMERIC(4,2)
);

INSERT INTO test_numeric (y) VALUES (12.2222);
INSERT INTO test_numeric (y) VALUES (-12.888);
INSERT INTO test_numeric (y) VALUES (123.2222);
INSERT INTO test_numeric (y) VALUES (999.2222);
INSERT INTO test_numeric (y) VALUES (999.9944);
INSERT INTO test_numeric (y) VALUES (999.9955);
INSERT INTO test_numeric (y) VALUES (9999999999.2222);
INSERT INTO test_numeric (y) VALUES (-9999999999.2222);

SELECT * FROM test_numeric;
DESCRIBE test_numeric;
DROP TABLE test_numeric;

# y NUMERIC(5,2)    => conclusion :
 
# Important : différence entre NUMERIC et FLOAT :


type “Numérique” : réel

Access MySQL Oracle PostgreSQL SQL server
real FLOAT FLOAT(49) ou FLOAT(23) real FLOAT
double double binary_double double precision real


REMARQUE : différence entre FLOAT et REAL sur ‘SQL server’



CREATE TABLE test_float (
y FLOAT(4,2)
);

INSERT INTO test_float (y) VALUES (12.2222);
INSERT INTO test_float (y) VALUES (-12.777);
INSERT INTO test_float (y) VALUES (123.2222);
INSERT INTO test_float (y) VALUES (999.2222);
INSERT INTO test_float (y) VALUES (999.9944);
INSERT INTO test_float (y) VALUES (999.9955);
INSERT INTO test_float (y) VALUES (9999999999.2222);
INSERT INTO test_float (y) VALUES (-9999999999.2222);

SELECT * FROM test_float;
DESCRIBE test_float;
DROP TABLE test_float;

NUMERIC (enregistre la valeur exacte d’un réel) DECIMAL[(M[,D])] est équivalent à NUMERIC [(M,D)] Occupe M+2 octets si D > 0, M+1 octets si D = 0
Contient des nombres flottants stockés comme des chaînes de caractères.


REEL : FLOAT, DOUBLE, REAL: Stocke un nombre de type flottant.(-1.175494351E-38 à 3.402823466E+38), avec Signe Exposant Mantisse (FLOAT : 4 octets DOUBLE : 8 octets) Exemple float(4,2)=> valeur maximum 99,99

Les chaînes de caractères


type “TEXTE”

Access MySQL Oracle PostgreSQL
VARCHAR(n) VARCHAR(n) VARCHAR2(n) VARCHAR(n)
CHAR(n) CHAR(n) CHAR(n) CHAR(n)
TEXT TEXT TEXT clob



CREATE TABLE test_char (
c VARCHAR(7)
);

INSERT INTO test_char (c) VALUES ('essai essai');
INSERT INTO test_char (c) VALUES ('Monsieur Monsieur Monsieur        ' );
INSERT INTO test_char (c) VALUES ('M.' );

SELECT * FROM test_char;
DESCRIBE test_char;
DROP TABLE test_char;

# remplacer par le type ci dessous le type de l'enregistrement
# c CHAR(13)                => conclusion :
# c TEXT

les dates/heures

type “date/heure”

Access MySQL Oracle PostgreSQL
date date date date



CREATE TABLE test_date1 (
date1 DATE
);
INSERT INTO test_date1 (date1) VALUES ('2019-03-1');
INSERT INTO test_date1 (date1) VALUES ('2019-13-13');
INSERT INTO test_date1 (date1) VALUES ('01-03-2019');
INSERT INTO test_date1 (date1) VALUES ('2017/03/19');
INSERT INTO test_date1 (date1) VALUES ('2019#03#25');
INSERT INTO test_date1 (date1) VALUES ('3200-03-26');
INSERT INTO test_date1 (date1) VALUES ('200-03-27');
INSERT INTO test_date1 (date1) VALUES ('0000-00-00');
INSERT INTO test_date1 (date1) VALUES ('-200-03-28');
SELECT * FROM test_date1;
DESCRIBE test_date1;
DROP TABLE test_date1;






Annexes : les autres types

les dates/heures

type “date/heure”

Access MySQL Oracle PostgreSQL
date date date date
time time date time
datetime datetime date timestamp


CREATE TABLE test_heure1 (
heure1 TIME
);
INSERT INTO test_heure1 (heure1) VALUES ('14:30');
INSERT INTO test_heure1 (heure1) VALUES ('14:30:55');
INSERT INTO test_heure1 (heure1) VALUES ('24:00');
INSERT INTO test_heure1 (heure1) VALUES ('28:00');
INSERT INTO test_heure1 (heure1) VALUES ('700:59:59');
INSERT INTO test_heure1 (heure1) VALUES ('99:60:59');
INSERT INTO test_heure1 (heure1) VALUES ('838:59:59'); -- 839
SELECT * FROM test_heure1;
DESCRIBE test_heure1;
DROP TABLE test_heure1;


CREATE TABLE test_datetime1 (
date_time1 DATETIME
);
INSERT INTO test_datetime1 (date_time1) VALUES ('2019-03-01');
INSERT INTO test_datetime1 (date_time1) VALUES (NULL);
INSERT INTO test_datetime1 (date_time1) VALUES ('2017-10-10 14:30:55');
INSERT INTO test_datetime1 (date_time1) VALUES ('2019-03-01 24:30:55');
INSERT INTO test_datetime1 (date_time1) VALUES ('3200-03-26');
INSERT INTO test_datetime1 (date_time1) VALUES ('200-03-27');
SELECT * FROM test_datetime1;
DESCRIBE test_datetime1;
DROP TABLE test_datetime1;

les booleens

valeur “vrai” ou “faux” codée sur un octet

Access MySQL Oracle PostgreSQL SQL server
logical tinyint(1) number(1) booleen byte
CREATE TABLE test_bool (
x1 bool,
x2 boolean 
);
INSERT INTO test_bool (x1,x2) VALUES (true,false);
INSERT INTO test_bool (x1,x2) VALUES (1,-1);
INSERT INTO test_bool (x1,x2) VALUES (12,-12);
INSERT INTO test_bool (x1,x2) VALUES (123,-123);

SELECT x1,x2 FROM test_bool;
DESCRIBE test_bool;
DROP TABLE test_bool;

pour la monnaie (€)

currency : valeur monétaire codée sur 8 octets comprenant 15 chiffres + 4 décimales

Access MySQL Oracle PostgreSQL SQL server
currency decimal(19,4) number(19,4) money money

cas particulier le “TIMESTAMP”


CREATE TABLE test_date2 (
date2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO test_date2 (date2) VALUES ('2019-03-1');
INSERT INTO test_date2 (date2) VALUES (NULL);
INSERT INTO test_date2 (date2) VALUES ('2017-10-10 14:30:55');
INSERT INTO test_date2 (date2) VALUES ('2019-13-13');
INSERT INTO test_date2 (date2) VALUES ('01-03-2019');
INSERT INTO test_date2 (date2) VALUES ('2017/03/19');
INSERT INTO test_date2 (date2) VALUES ('2019#03#25');
INSERT INTO test_date2 (date2) VALUES ('3200-03-26');
INSERT INTO test_date2 (date2) VALUES ('200-03-27');
INSERT INTO test_date2 (date2) VALUES ('0000-00-00');
INSERT INTO test_date2 (date2) VALUES ('-200-03-28');
SELECT * FROM test_date2;
DESCRIBE test_date2;
DROP TABLE test_date2;



CREATE TABLE test_date3 (
date3 DATETIME
);
INSERT INTO test_date3 (date3) VALUES ('2019-03-1');
INSERT INTO test_date3 (date3) VALUES (NULL);
INSERT INTO test_date3 (date3) VALUES ('2017-10-10 14:30:55');
INSERT INTO test_date3 (date3) VALUES ('2019-13-13');
INSERT INTO test_date3 (date3) VALUES ('01-03-2019');
INSERT INTO test_date3 (date3) VALUES ('2017/03/19');
INSERT INTO test_date3 (date3) VALUES ('2019#03#25');
INSERT INTO test_date3 (date3) VALUES ('3200-03-26');
INSERT INTO test_date3 (date3) VALUES ('200-03-27');
INSERT INTO test_date3 (date3) VALUES ('0000-00-00');
INSERT INTO test_date3 (date3) VALUES ('-200-03-28');
SELECT * FROM test_date3;
DESCRIBE test_date3;
DROP TABLE test_date3;

## conclusion

# format DATE  =>
# format TIME =>
# format TIMESTAMP =>
# format DATETIME =>