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 See Création de tables (CREATE))
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:
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)
CREATE TABLE table (colonne1 spec1,
colonne2 spec2,
clés, )
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
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
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/ |
+---------+------------+------------+------------------------+