UP PREVIOUS NEXT   Technologies Internet et Education, © TECFA
  4. Définition de données (tables)

4. Définition de données (tables)

La creation d'une table implique:

Voici la syntaxe (voir plus loin pour les explications):

CREATE TABLE [IF NOT EXISTS] tbl_name (create_definition1,...) [table_options] [select_statement]

 

1 create_definition:

col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]

[PRIMARY KEY] [reference_definition]

or PRIMARY KEY (index_col_name,...)

or KEY [index_name] KEY(index_col_name,...)

or INDEX [index_name] (index_col_name,...)

or UNIQUE [INDEX] [index_name] (index_col_name,...)

or [CONSTRAINT symbol] FOREIGN KEY index_name (index_col_name,...)

[reference_definition]

or CHECK (expr)

4.1 Les identificateurs MySQL

A. Règles générales

B. Tables et colonnes (champs)

base_de_données.table.colonne

ex: demo.demo1.login

4.2 Types de données

MySQL implémente la plupart des données SQL (mais lire le manuel pour des types exotiques !!)

Strings:
Attributs à option (dans le tableau suivant)
Paramètres à option (dans le tableau suivant)
La valeur NULL

Tableau récapitulatif des types

Type

explication

range

exemple

NOMBRES

TinyInt[(M)][UNSIGNED] [ZEROFILL]

entier minuscule

-128 à 127 (0 à 255)

TinyInt(2)

9

SmallInT[(M)]...

petit entier

-32768 à 32767 (0 à 64K)

20001

MediumINT[(M)]...

entier moyent

-8388608 to 8388607

-234567

INT[(M)] ...

entier

-2147483648 to 2147483647

 

BigINT[(M)]...

gros entier

63bits

 

FLOAT(precision)

nombre flottant

 

 

FLOAT[(M,D)]...

nombre flottant

-3.402823466E+38

to -1.175494351E-38

 

DOUBLE[(M,D)]...

grand nombre flottant

 

 

DATES

DATE

date

YYYY-MM-DD

3000-12-31

DateTime

 

YYYY-MM-DD HH:MM:SS

 

TimeStamp[(M)]

 

 

 

TIME

 

 

 

YEAR

 

 

 

Chaînes de caractères (strings)

Char(M) [binary]

String de longeur fixe

M = 1 à 225 chars

case insensitif (sauf binary)

char(4)
'ab '

VarChar(M)[binary]

String variable

M = 1 à 225 chars

login(8)[binary]

schneiDe

Texte (blobs)

TINYBLOB

TINYTEXT

petit texts

255 chars

 

BLOB

TEXT

 

65535 chars

 

MEDIUMBLOB

MEDIUMTEXT

 

16777215 chars

 

LONGBLOB

LONGTEXT

grand text

4294967295 chars

 

Enumération

Enum('val1', 'val2',...)

un string parmi la liste ou NULL

65535 distinct values

'toto'

Set('val1', 'val2', ...)

zéro ou plusieurs strings parmi la liste

64 members

('toto', 'blurp')

Exemple 4-1: Création d'une table simple (CREATE)

Voici un exemple simple:

CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE);

4.3 Les clés

A. Index simple d'une colonne (KEY)

KEY index_name (col_name)

KEY index_name (char_col_name(M))

INDEX est un synonyme de KEY

B. Clé primaire (primary KEY)

PRIMARY KEY (index_col_name, index_col_name)

id int(10) DEFAULT '0' NOT NULL auto_increment,
PRIMARY KEY (id),

4.4 Définition de colonnes

Note: L'exemple complet se trouve just après (section See Création de tables (CREATE))

Exemple 4-2: Colonnes dans l'exemple demo1

  id int(10) DEFAULT '0' NOT NULL auto_increment,
  login char(10) DEFAULT '' NOT NULL,
  password char(100),
  url char(60) DEFAULT '' NOT NULL,
  food int(11) DEFAULT '0' NOT NULL,

Définition minimaliste d'une colonne:

nom type

Ex: id int

Définition habituelle d'une colonne:

nom type (taille) DEFAULT 'valuer_défaut' NOT NULL,

Ex: login char(10) DEFAULT '' NOT NULL,

Définition d'une clé primaire:

nom int (taille) DEFAULT '0' NOT NULL auto_increment,

Ex: login char(10) DEFAULT '' NOT NULL,

PRIMARY KEY (id),
KEY login (login)

4.5 Création de tables (CREATE)

CREATE TABLE table (colonne1 spec1,
colonne2 spec2,
clés, )

Exemple 4-3: La table demo1

CREATE TABLE demo1 (
  id int(10) DEFAULT '0' NOT NULL auto_increment,
  login char(10) DEFAULT '' NOT NULL,
  password char(100),
  fullname char(40) DEFAULT '' NOT NULL,
  url char(60) DEFAULT '' NOT NULL,
  food int(11) DEFAULT '0' NOT NULL,
  work int(11) DEFAULT '0' NOT NULL,
  love int(11) DEFAULT '0' NOT NULL,
  leisure int(11) DEFAULT '0' NOT NULL,
  sports int(11) DEFAULT '0' NOT NULL,
  PRIMARY 
KEY (id),
  KEY login (login)
);
 

Voir 7. "Utilisation de MySQL" [30] si vous voulez faire vous-même

4.6 Tables relationnelles

Exemple simple:

 

Création des tables avec qq données:

Le fichier student_exercice.mysql:

DROP TABLE IF EXISTS student;
DROP TABLE IF EXISTS exercice;
CREATE TABLE student (
  id int(10) DEFAULT '0' NOT NULL auto_increment,
  name char(40) DEFAULT '' NOT NULL,
  first_name char(40) DEFAULT '' NOT NULL,
  PRIMARY KEY (id)
);
 
INSERT INTO student VALUES (NULL,'Testeur','Bill');
INSERT INTO student VALUES (NULL,'Testeur','Joe');
INSERT INTO student VALUES (NULL,'Testeuse','Sophie');
 
CREATE TABLE exercice (
  id int(10) DEFAULT '0' NOT NULL auto_increment,
  title char(40) DEFAULT '' NOT NULL,
  student_id int(10) NOT NULL,
  comments varchar(128),
  url char(60) DEFAULT '' NOT NULL,
  PRIMARY KEY (id),
  KEY student_id (student_id)
);
 
INSERT INTO exercice VALUES (NULL,"Exercice 1",'1',"pas de commentaire",'http://tecfa.unige.ch/');
INSERT INTO exercice VALUES (NULL,"Exercice 2",'1',"pas de commentaire",'http://tecfa.unige.ch/');
 

Chargement (voir 7.2 "Traitement en "batch"" [32])

mysql -h tecfa -u schneide -p demo < student_exercice.mysql
Quelques requêtes:
select * FROM student,exercice WHERE student.id = exercice.student_id;
select student.name, student.first_name, exercice.title, exercice.url FROM student,exercice WHERE student.id = exercice.student_id;
+---------+------------+------------+------------------------+
| name    | first_name | title      | url                    |
+---------+------------+------------+------------------------+
| Testeur | Bill       | Exercice 1 | http://tecfa.unige.ch/ |
| Testeur | Bill       | Exercice 2 | http://tecfa.unige.ch/ |
+---------+------------+------------+------------------------+
 

UP PREVIOUS NEXT -- TIE