Petit soucis dans SQL

francdrum Messages postés 874 Statut Membre -  
francdrum Messages postés 874 Statut Membre -
Bonjour,
Je débute en SQL ...
En insérant un fichier .sql pour créer une table, je me retrouve avec l'erreur suivante :

Erreur

requête SQL:

CREATE TABLE admin(
id int( 5 ) DEFAULT '0' NOT NULL AUTO_INCREMENT ,
user varchar( 50 ) NOT NULL ,
pass varchar( 50 ) NOT NULL ,
lang varchar( 20 ) DEFAULT 'english' NOT NULL ,
active tinyint( 2 ) DEFAULT '0' NOT NULL ,
lastlogin datetime DEFAULT '0000-00-00 00:00:00' NOT NULL ,
permissions varchar( 20 ) DEFAULT '00011' NOT NULL ,
sid varchar( 255 ) NOT NULL ,
addplace tinyint( 2 ) DEFAULT '0' NOT NULL ,
placerights tinyint( 2 ) DEFAULT '0' NOT NULL ,
placeautoupdate tinyint( 2 ) DEFAULT '0' NOT NULL ,
PRIMARY KEY ( id ) ,
KEY id( id ) ,
UNIQUE id_2( id )
);

MySQL a répondu:?
#1067 - Invalid default value for 'id'

Le "?" à la fin est un lien vers la doc, mais je n'y comprends rien :-(
Merci de me donner un petit tuyau :-)

11 réponses

avion-f16 Messages postés 20367 Statut Contributeur 4 509
 
Bonjour,

Essaye en ayant retirer les apostrophes autour du 0 (2ème ligne) :
id int( 5 ) DEFAULT 0 NOT NULL AUTO_INCREMENT , 
au lieu de :
id int( 5 ) DEFAULT '0' NOT NULL AUTO_INCREMENT , 
0
francdrum Messages postés 874 Statut Membre 48
 
Merci pour ta réponse
Mais il me donne le même message d'erreur avec aussi à la fin :
#1067 - Invalid default value for 'id'

J'ai aussi essayé de mettre '1', mais ça ne va pas non plus :-(
0
pyfeu Messages postés 40 Statut Membre
 
il faut pas changer la valeur mais enlever les simples cotes, sinon pourquoi tu mets default ?? Je comprends pas à quoi ça te sert..
0
Alain_42 Messages postés 5413 Statut Membre 894
 
Bonjour,

id int( 5 ) DEFAULT '0' NOT NULL AUTO_INCREMENT ,

si tu le mets en AUTOINCREMENT tu ne peux pas lui mettre une valeur par defaut= 0
car il ne eput pas être à la fois égal à 0 et prendre une valeur 1 2 3 4 etc...

il faut:

id int( 5 )  AUTO_INCREMENT ,

tout simplement
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
francdrum Messages postés 874 Statut Membre 48
 
Merci @ vous 2 :-)
C'est un script que j'ai trouvé (phpmyagenda)
Maintenant, avec cette modif, il me donne :

Erreur

requête SQL:

CREATE TABLE contacts ( id int(5) DEFAULT '0' NOT NULL auto_increment, name varchar(100) NOT NULL, email varchar(100) NOT NULL, url smallint(5) DEFAULT '0' NOT NULL, phone varchar(100) NOT NULL, fax varchar(100) NOT NULL, address varchar(255) NOT NULL, town varchar(100) NOT NULL, zipcode varchar(20) NOT NULL, country varchar(50) NOT NULL, valid tinyint(1) DEFAULT '0' NOT NULL, name1 varchar(50) NOT NULL, name2 varchar(50) NOT NULL, name3 varchar(50) NOT NULL, name4 varchar(50) NOT NULL, name5 varchar(50) NOT NULL, surname1 varchar(20) NOT NULL, surname2 varchar(20) NOT NULL, surname3 varchar(20) NOT NULL, surname4 varchar(20) NOT NULL, surname5 varchar(20) NOT NULL, function1 varchar(20) NOT NULL, function2 varchar(20) NOT NULL, function3 varchar(20) NOT NULL, function4 varchar(20) NOT NULL, function5 varchar(20) NOT NULL, [...]

MySQL a répondu:Documentation
#1067 - Invalid default value for 'id'








Je mets le ".sql" en entier tel qu'il est d'origine (c'est long) :
Merci @ vs





# phpMyAdmin MySQL-Dump

# http://www.phpwizard.net

#

# Host: localhost Base de données: phpmyagenda



# --------------------------------------------------------

#

# Structure de la table 'admin'

#



DROP TABLE IF EXISTS admin;

CREATE TABLE admin (

id int(5) DEFAULT '0' NOT NULL auto_increment,

user varchar(50) NOT NULL,

pass varchar(50) NOT NULL,

lang varchar(20) DEFAULT 'english' NOT NULL,

active tinyint(2) DEFAULT '0' NOT NULL,

lastlogin datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,

permissions varchar(20) DEFAULT '00011' NOT NULL,

sid varchar(255) NOT NULL,

addplace tinyint(2) DEFAULT '0' NOT NULL,

placerights tinyint(2) DEFAULT '0' NOT NULL,

placeautoupdate tinyint(2) DEFAULT '0' NOT NULL,

PRIMARY KEY (id),

KEY id (id),

UNIQUE id_2 (id)

);



#

# Contenu de la table 'admin'

#



INSERT INTO admin VALUES ( '3', 'write', 'efb2a684e4afb7d55e6147fbe5a332ee', 'english', '1', '2001-08-27 23:21:19', '022', '', '1', '2', '1');

INSERT INTO admin VALUES ( '4', 'admin', '1a1dc91c907325c69271ddf0c944bc72', 'francais', '1', '2001-09-19 21:54:30', '122', 'c3ab456b36dd2d9ef6cf3ed6b1b1ba95', '0', '2', '1');



# --------------------------------------------------------

#

# Structure de la table 'contacts'

#



DROP TABLE IF EXISTS contacts;

CREATE TABLE contacts (

id int(5) DEFAULT '0' NOT NULL auto_increment,

name varchar(100) NOT NULL,

email varchar(100) NOT NULL,

url smallint(5) DEFAULT '0' NOT NULL,

phone varchar(100) NOT NULL,

fax varchar(100) NOT NULL,

address varchar(255) NOT NULL,

town varchar(100) NOT NULL,

zipcode varchar(20) NOT NULL,

country varchar(50) NOT NULL,

valid tinyint(1) DEFAULT '0' NOT NULL,

name1 varchar(50) NOT NULL,

name2 varchar(50) NOT NULL,

name3 varchar(50) NOT NULL,

name4 varchar(50) NOT NULL,

name5 varchar(50) NOT NULL,

surname1 varchar(20) NOT NULL,

surname2 varchar(20) NOT NULL,

surname3 varchar(20) NOT NULL,

surname4 varchar(20) NOT NULL,

surname5 varchar(20) NOT NULL,

function1 varchar(20) NOT NULL,

function2 varchar(20) NOT NULL,

function3 varchar(20) NOT NULL,

function4 varchar(20) NOT NULL,

function5 varchar(20) NOT NULL,

email1 varchar(255) NOT NULL,

email2 varchar(255) NOT NULL,

email3 varchar(255) NOT NULL,

email4 varchar(255) NOT NULL,

email5 varchar(255) NOT NULL,

phone1 varchar(20) NOT NULL,

phone2 varchar(20) NOT NULL,

phone3 varchar(20) NOT NULL,

phone4 varchar(20) NOT NULL,

phone5 varchar(20) NOT NULL,

fax1 varchar(20) NOT NULL,

fax2 varchar(20) NOT NULL,

fax3 varchar(20) NOT NULL,

fax4 varchar(20) NOT NULL,

fax5 varchar(20) NOT NULL,

owner int(5) DEFAULT '0' NOT NULL,

PRIMARY KEY (id),

KEY id (id),

UNIQUE id_2 (id),

KEY contacturl (url)

);



# --------------------------------------------------------

#

# Structure de la table 'date'

#



DROP TABLE IF EXISTS date;

CREATE TABLE date (

id int(5) DEFAULT '0' NOT NULL auto_increment,

start datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,

end datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,

event smallint(5) DEFAULT '1' NOT NULL,

place smallint(5) DEFAULT '1' NOT NULL,

valid tinyint(1) DEFAULT '0' NOT NULL,

owner int(5) DEFAULT '0' NOT NULL,

PRIMARY KEY (id),

KEY id (id),

UNIQUE id_2 (id)

);



# --------------------------------------------------------

#

# Structure de la table 'events'

#



DROP TABLE IF EXISTS events;

CREATE TABLE events (

id int(5) DEFAULT '0' NOT NULL auto_increment,

title varchar(255) NOT NULL,

description text NOT NULL,

url smallint(5) DEFAULT '0' NOT NULL,

type smallint(5) DEFAULT '0' NOT NULL,

contact1 smallint(5) DEFAULT '0' NOT NULL,

contact2 smallint(5) DEFAULT '0' NOT NULL,

contact3 smallint(5) DEFAULT '0' NOT NULL,

contact4 smallint(5) DEFAULT '0' NOT NULL,

contact5 smallint(5) DEFAULT '0' NOT NULL,

valid smallint(5) DEFAULT '0' NOT NULL,

owner int(5) DEFAULT '0' NOT NULL,

image varchar(255) NOT NULL,

KEY id (id),

UNIQUE id_2 (id),

PRIMARY KEY (id),

KEY referer (contact1)

);



# --------------------------------------------------------

#

# Structure de la table 'eventtype'

#



DROP TABLE IF EXISTS eventtype;

CREATE TABLE eventtype (

id smallint(5) DEFAULT '0' NOT NULL auto_increment,

type varchar(50) NOT NULL,

type_en varchar(50) NOT NULL,

owner int(5) DEFAULT '0' NOT NULL,

PRIMARY KEY (id),

KEY id (id),

UNIQUE id_2 (id)

);



# --------------------------------------------------------

#

# Structure de la table 'place'

#



DROP TABLE IF EXISTS place;

CREATE TABLE place (

id int(5) DEFAULT '0' NOT NULL auto_increment,

name varchar(100) NOT NULL,

address varchar(255) NOT NULL,

town varchar(100) NOT NULL,

zipcode varchar(20) NOT NULL,

country varchar(50) NOT NULL,

contact1 smallint(5) DEFAULT '0' NOT NULL,

url smallint(5) DEFAULT '0' NOT NULL,

phone varchar(100) NOT NULL,

fax varchar(100) NOT NULL,

valid tinyint(1) DEFAULT '0' NOT NULL,

contact2 smallint(5) DEFAULT '0' NOT NULL,

contact3 smallint(5) DEFAULT '0' NOT NULL,

contact4 smallint(5) DEFAULT '0' NOT NULL,

contact5 smallint(5) DEFAULT '0' NOT NULL,

owner int(5) DEFAULT '0' NOT NULL,

PRIMARY KEY (id),

KEY id (id),

UNIQUE id_2 (id),

KEY url (url)

);



# --------------------------------------------------------

#

# Structure de la table 'placerights'

#



DROP TABLE IF EXISTS placerights;

CREATE TABLE placerights (

id int(11) DEFAULT '0' NOT NULL auto_increment,

userid int(11) DEFAULT '0' NOT NULL,

placeid int(11) DEFAULT '0' NOT NULL,

PRIMARY KEY (id, userid),

KEY userid (userid)

);



# --------------------------------------------------------

#

# Structure de la table 'url'

#



DROP TABLE IF EXISTS url;

CREATE TABLE url (

id int(5) DEFAULT '0' NOT NULL auto_increment,

url varchar(255) NOT NULL,

owner int(5) DEFAULT '0' NOT NULL,

PRIMARY KEY (id),

KEY id (id),

UNIQUE id_2 (id)

);







0
francdrum Messages postés 874 Statut Membre 48
 
Personne n'aurait une petite idée ?
Ce serait sympa :-)
Merci
0
francdrum Messages postés 874 Statut Membre 48
 
Je colle ici juste le début du ".sql", ce sera plus clair :
(enfin, l'erreur a l'air de venir de là, je pense)

# --------------------------------------------------------

#

# Structure de la table 'admin'

#



DROP TABLE IF EXISTS admin;

CREATE TABLE admin (

id int(5) DEFAULT 0 NOT NULL auto_increment,

user varchar(50) NOT NULL,

pass varchar(50) NOT NULL,

lang varchar(20) DEFAULT 'english' NOT NULL,

active tinyint(2) DEFAULT '0' NOT NULL,

lastlogin datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,

permissions varchar(20) DEFAULT '00011' NOT NULL,

sid varchar(255) NOT NULL,

addplace tinyint(2) DEFAULT '0' NOT NULL,

placerights tinyint(2) DEFAULT '0' NOT NULL,

placeautoupdate tinyint(2) DEFAULT '0' NOT NULL,

PRIMARY KEY (id),

KEY id (id),

UNIQUE id_2 (id)

);



#

# Contenu de la table 'admin'

#



INSERT INTO admin VALUES ( '3', 'write', 'efb2a684e4afb7d55e6147fbe5a332ee', 'english', '1', '2001-08-27 23:21:19', '022', '', '1', '2', '1');

INSERT INTO admin VALUES ( '4', 'admin', '1a1dc91c907325c69271ddf0c944bc72', 'francais', '1', '2001-09-19 21:54:30', '122', 'c3ab456b36dd2d9ef6cf3ed6b1b1ba95', '0', '2', '1');



# --------------------------------------------------------


Merci d'avance :-)
0
Alain_42 Messages postés 5413 Statut Membre 894
 
je te renouvèle mon conseil dont tu n'as pas tenu compte:

id int( 5 ) DEFAULT '0' NOT NULL AUTO_INCREMENT ,

si tu le mets en AUTOINCREMENT tu ne peux pas lui mettre une valeur par defaut= 0
car il ne peut pas être à la fois égal à 0 et prendre une valeur 1 2 3 4 etc...

il faut:

id int( 5 ) AUTO_INCREMENT ,


tout simplement
0
francdrum Messages postés 874 Statut Membre 48
 
Oui :-)
La j'avais collé le code d'origine, je m'étais mélangé avec un ".sauvg.sql" ...
Mais sinon, j'ai bien fait cette modif et ça roule impec :-)

Une autre petite question :
C'est possible, normalement, que le visiteur puisse trier les résultats dans le tableau
Par "titre", "auteur" ...... et ceci ASC, ou DESC, au choix
(J'ai mis les champs à ma sauce depuis hier)
Donc, il faut bien que je modifie la fin de :
$query = 'SELECT * FROM '.$table.' ORDER BY name ASC';

J'ai essayé :
$query = 'SELECT * FROM '.$table.' ORDER BY $value $tri';
ou meme :
$query = 'SELECT * FROM '.$table.' ORDER BY name $tri';

Mais évidement, ça ne va pas ...
Pô facile de coder avec des moufles .... ;-)
0
Alain_42 Messages postés 5413 Statut Membre 894
 
$query = 'SELECT * FROM '.$table.' ORDER BY $value $tri';

normal que ça ne marche pas

dans une chaine qui commence et finit pas des ' les variables ne sont pas interprétées

$value, $tri

il faut:

$query = 'SELECT * FROM '.$table.' ORDER BY '.$value.' '. $tri.' '; 


ou:

$query = "SELECT * FROM $table ORDER BY $value $tri "; 

entre " les variables sont interprétées
0
francdrum Messages postés 874 Statut Membre 48
 
Merci
Impecc :-)
J'ai pris la 1ere solution, qui ressemble plus au reste de mon code
@++
0