Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem z tworzeniem i (chyba) auto_increment
Forum PHP.pl > Forum > Bazy danych > MySQL
darth_sidious
Witam

"Bawie" sie wlasnie skryptem typerow Prediction League i juz na samym poczatku, podczas instalacji i konfiguracji bazy (MySql) mam juz maly problem. Skrypt instalacyjny tworzy baze a nastepnie tabele potrzebne do dzialania skryptu. Baza tworzy sie bez problemu. Problem pojawil sie przy tworzeniu tabel. Z tego co zauwazylem z tabela, w ktorej primary key nie jest auto_increment nie ma problemu - tworzy sie. Lecz gdy primary key jest auto_increment pojawia sie nastepujacy error:

Query failed: create table pl.pluserdata (lid int not null , userid int not null auto_increment, username varchar(32) not null , password varchar(40), email varchar(60), icon varchar(128), lang varchar(32), usertype smallint, dflths smallint default 0, dfltas smallint default 0, since DATE, isauto enum('Y','N') default 'N', primary key (lid, userid));
Incorrect table definition; there can be only one auto column and it must be defined as a key


Ponizej kod, ktory tworzy tabele, ktorej blad widnieje powyzej:

  1. <?php
  2. $query = "create table $dbname.$dbaseUserData
  3. (lid int not null ,
  4.  userid int not null auto_increment,
  5.  username varchar($userlen) not null ,
  6.  password varchar($passlen),
  7.  email varchar($emaillen),
  8.  icon varchar($fnamelen),
  9.  lang varchar(32),
  10.  usertype smallint,
  11.  dflths smallint default 0,
  12.  dfltas smallint default 0,
  13.  since DATE,
  14.  isauto enum('Y','N') default 'N',
  15.  primary key (lid, userid));";
  16. $userresult = mysql_query($query)
  17. or die("Query failed: $query<br>n".mysql_error());
  18. ?>



A ponizej fragment, ktory nie wywala bleu i poprawnie tworzy tabele:

  1. <?php
  2. $query = "create table $dbname.$dbasePredictionData
  3.  (lid int not null ,
  4. userid int not null,
  5. matchid int not null,
  6. predtime timestamp,
  7. homescore smallint unsigned,
  8. awayscore smallint unsigned,
  9. isauto enum('Y','N') default 'N',
  10. primary key(lid, userid, matchid))";
  11. $userresult = mysql_query($query)
  12. or die("Query failed: $query<br>n".mysql_error());
  13. ?>


Co jest nie tak. Zaznaczam, ze nic sam nie kombinowale - jest to fragment oryginalnego skryptu tworzacego tabele...

Pozdrawiam
Darth
nospor
no chyba wyraźnie ci napisalo:
Cytat
Incorrect table definition; there can be only one auto column and it must be defined as a key

to co jest autoincrementem musi byc kluczem glownym. u ciebie tak nie jest. ty tworzysz taki klucz glowny:(lid, userid)
darth_sidious
Tzn, ze jesli ktores pole jest auto_increment to moze byc tylko 1 primary key?

Darth
nospor
tak. wkoncu autoincrement jednoznacznie definiuje rekord
darth_sidious
No mam dylemat... Jak juz pisalem zmieniam oryginalny skrypt a tam wlasnie mimo auto_increment sa 2 primary key... Boje sie, ze jesli usune jednen z nich (w tym przypadku lid) cos przestanie dzialac... Czy jest na to jakas rada?
wipo
Ale już Ci to napisał nospar że nie można w obrębie jednej tabeli uzyć 2 razy primary key.
Popatrz może na foreign key albo inne klucze

Tak pozatym dobrze byłoby skontaktowac się z autorem co chciał przez takie odwołanie uzyskać
nospor
wydaje mi sie troche dziwne robic klucz glowny z polaczenia autoincrement i innego pola.

To mowisz ze oni mieli tak sql? autoincrement i podwojny klucz? a pisali na jakiej bazce to ma chodzic i na jakiej wersji?

Nie wiem czy cos ci nie napsuje usuwania jednego klucza (dalej nie wiem jakim cudem im sie udalo zalozyc dwa - byc moze oczym nie wiem), wszystko zalezy jak oni tym zarządzają poźniej w skrypcie.

Cytat
nie można w obrębie jednej tabeli uzyć 2 razy primary key.
tego nie napisalem, czytaj uwaznie smile.gif (co nie zmienia faktu ze nie mozna, mozna za to raz ale na kilku polach, pod warunkiem ze nie ma autoincrement)
darth_sidious
Skrypt powinien dzialac na php 4.0 lub nowszym i bazie MySql... przeszukiwalem rozne fora, ale nigdzie nie znalazlem informacji o tym aby ktos mial podobny problem do mojego z instalacja...
W instrukcji instalacji tegoz skryptu nie ma nawet mowy o jakichs problemach z baza i ingerencji w nia... Plik instalacyjny w pierwszej kolejnosci tworzy baze (to ok) a nastepnie tabele... te w ktorych nie ma autoincrement tworza sie bez problemu, te w ktorych jest -> wynik j/w...
nospor
Cytat
na php 4.0 lub nowszym i bazie MySql
mi wlasnie chodzilo o wersje mysql a nie php. php do bazy nie ma nic wspolnego.

zgodnie z komunikatem jaki dostajesz nie mozesz utworzyc kluacza podwojnego gdy masz autoincrement. wiec albo masz zla wersje bazy (oni robili to na innej), albo sie walneli w skrypcie sql
darth_sidious
No wlasnie o wersji bazy nie ma mowy.... sadsmiley02.gif
wipo
Nie jesteś pewnien jaka baza czy która wersja?
darth_sidious
Bazy jestem pewien - mysql... nie mam pewnosci co do obslugiwanej wersji....
nospor
moze ty tworzysz tabele typu innoDB? bo znalazlem w manualu,ze dla MyISAM mozna tak tworzyc.
http://dev.mysql.com/doc/refman/5.1/en/exa...-increment.html
Cytat
For MyISAM tables you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as MAX(auto_increment_column) + 1 WHERE prefix=given-prefix. This is useful when you want to put data into ordered groups. CREATE TABLE animals ( grp ENUM('fish','mammal','bird') NOT NULL, id MEDIUMINT NOT NULL AUTO_INCREMENT, name CHAR(30) NOT NULL, PRIMARY KEY (grp,id) );


i jeszcze cos z komentow:
Kod
AUTO_INCREMENT on a secondary column in a multiple-column index:

Specify 'type=myisam' for the table if you want to avoid getting:
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

Ref. script example from manual:
CREATE TABLE animals (
grp ENUM('fish','mammal','bird') NOT NULL,
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (grp,id)
);
Does not work if your default is not MyISAM, while the script below works fine:

CREATE TABLE animals (
grp ENUM('fish','mammal','bird') NOT NULL,
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (grp,id)
) type=myisam;
darth_sidious
OK... przyjrze sie temu blizej... postaram sie cos pokombinowac i dam znac znac o efektach...
nospor
ale co tu kombinowac? musisz dopisac na koniec sql: type=myisam; tak ja ci podalem w ostanim poscie z komentow.
Kod
<?php
$query = "create table $dbname.$dbaseUserData
            (lid int not null ,
             userid int not null auto_increment,
             username varchar($userlen) not null ,
             password varchar($passlen),
             email varchar($emaillen),
             icon varchar($fnamelen),
             lang varchar(32),
             usertype smallint,
             dflths smallint default 0,
             dfltas smallint default 0,
             since DATE,
             isauto enum('Y','N') default 'N',
             primary key (lid, userid)) type=myisam";
?>
darth_sidious
Wielkie dzieki snitch.gif

Po dodaniu
  1. <?php
  2. type=myisam
  3. ?>

Wszystko dziala....

Jeszcze raz wszystkim Wam bardzo dziekuje za pomoc biggrin.gif

Pozdrawiam
Darth
jarek26
Witam. Mógłby mi ktokolwiek pomóc gdyż mam podobny problem z tym samym skryptem z tym, że jest to nowsza wersja. Po próbie tworzenia tabeli pokazuje się taki błąd
Kod
Query failed: create table fpgpl_typer.pluserdata (lid int not null , userid int not null auto_increment, username varchar(32) not null , password varchar(40), email varchar(60), icon varchar(128), lang varchar(32), usertype smallint, dflths smallint default 0, dfltas smallint default 0, since DATE, auto enum('Y','N') default 'N', primary key (lid,userid));
Incorrect table definition; there can be only one auto column and it must be defined as a key
czyli taki sam jak u osoby która rozpoczęła temat. Teoretycznie jest rozwiązanie lecz on miał inną wersję skryptu a ja mam inną. Moje pytanie brzmi. Gdzie dodać i czy wogóle dodawać wartość
  1.  
  2. <?php
  3. type=myisam
  4. ?>


jeżeli kod do tworzenia tabeli wygląda następująco
  1. if ($createtables == "TRUE") {
  2. $query = "create table $dbname.$dbaseUserData (lid int not null , userid int not null auto_increment, username varchar($userlen) not null , password varchar($passlen), email varchar($emaillen), icon varchar($fnamelen), lang varchar(32), usertype smallint, dflths smallint default 0, dfltas smallint default 0, since DATE, auto enum('Y','N') default 'N', primary key (lid,userid));";
  3. $userresult = mysql_query($query)
  4. or die("Query failed: $query<br>\n".mysql_error());
  5.  
  6.  
  7.  
  8. $query = "create table $dbname.$dbasePredictionData (lid int not null , userid int not null, matchid int not null ,predtime timestamp, homescore smallint unsigned, awayscore smallint unsigned, isauto enum('Y','N') default 'N', primary key(lid, userid, matchid))";
  9. $userresult = mysql_query($query)
  10. or die("Query failed: $query<br>\n".mysql_error());
  11.  
  12. $query = "create table $dbname.$dbaseMatchData (lid int not null, matchid int not null auto_increment, matchdate DATETIME not null, hometeam varchar($teamlen) not null, awayteam varchar($teamlen) not null, homescore smallint unsigned, awayscore smallint unsigned, primary key(lid,matchid));";
  13. $userresult = mysql_query($query)
  14. or die("Query failed: $query<br>\n".mysql_error());
  15.  
  16. $query = "create table $dbname.$dbaseStandings (lid int not null, userid int not null, position smallint not null, pld int unsigned, won int unsigned, drawn int unsigned, lost int unsigned, gfor smallint unsigned, gagainst smallint unsigned, diff smallint, points int unsigned, primary key(lid,userid));";
  17. $userresult = mysql_query($query)
  18. or die("Query failed: $query<br>\n".mysql_error());
  19.  
  20. $query = "create table $dbname.$dbaseConfigData (lid int not null, grp int not null, param varchar(32) not null, value varchar(90) not null, descr text not null, ro enum('Y','N') default 'N', primary key(lid,param));";
  21. $userresult = mysql_query($query)
  22. or die("Query failed: $query<br>\n".mysql_error());
  23.  
  24. $query = "create table $dbname.$dbaseMsgData (lid int not null, msgid int not null auto_increment, threadid int, prevmsg int, sender int not null, receiver int not null, subject varchar(255), body text, status enum(\"new\",\"read\",\"deleted\"), msgtime timestamp, primary key(lid,msgid));";
  25. $userresult = mysql_query($query)
  26. or die("Query failed: $query<br>\n".mysql_error());
  27.  
  28. }


Bardzo proszę o pomoc bo nie potrafię sę sam z tym poradzić. Z góry dziękuję za pomoc. Pozdrawiam
nospor
Przeciez masz wyraźnie napisane: Moze byc tylko jeden autoincrement i musi byc kluczem głównym.
A u ciebie klucz główny sklada sie z dwóch pol: (lid,userid), a jesli uzywasz autoincrement to moze skladac sie tylko z tego jednego pola.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.