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]
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,...)
MySQL implémente la plupart des données SQL (mais lire le manuel pour des types exotiques !!)
Note: L’exemple complet se trouve just après (section Création de tables (CREATE))
id int(10) NOT NULL auto_increment,
login varchar(10) DEFAULT '' NOT NULL,
password varchar(100),
url varchar(60) DEFAULT '' NOT NULL,
food int(11) DEFAULT '0' NOT NULL,
Définition minimaliste d’une colonne typique:
Attention, certains types exigent une taille, notamment VarChar et Char !!
Définition habituelle d’une colonne:
nom type (taille) DEFAULT 'valuer_défaut' NOT NULL,
Ex: login varchar(10) DEFAULT '' NOT NULL,
Définition d’une clé primaire:
nom int (taille) DEFAULT '0' NOT NULL auto_increment,
Ex: login varchar(10) DEFAULT ’’ NOT NULL,
PRIMARY KEY (id),
KEY login (login)
CREATE TABLE table (colonne1 spec1, colonne2 spec2, clés, )
CREATE TABLE demo1 (
id int(10) NOT NULL auto_increment,
login varchar(10) DEFAULT '' NOT NULL,
password varchar(100),
fullname varchar(40) DEFAULT '' NOT NULL,
url varchar(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)
);
DROP TABLE IF EXISTS student;
DROP TABLE IF EXISTS exercice;
CREATE TABLE student (
id int(10) NOT NULL auto_increment,
name varchar(40) DEFAULT ’’ NOT NULL,
first_name varchar(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) NOT NULL auto_increment,
title varchar(40) DEFAULT ’’ NOT NULL,
student_id int(10) NOT NULL,
comments varchar(128),
url varchar(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/’);
mysql -p demo < student_exercice.mysql
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/ |
+---------+------------+------------+------------------------+