Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Powiązanie tabel po przez pole typu varchar ?
netvalue
post
Post #1





Grupa: Zarejestrowani
Postów: 199
Pomógł: 2
Dołączył: 9.06.2008

Ostrzeżenie: (0%)
-----



mam takie dwie tabele :

  1. CREATE TABLE IF NOT EXISTS `product` (
  2. `id_product` INT(11) NOT NULL,
  3. `indeks` VARCHAR(255) NULL,
  4. PRIMARY KEY (`id_product`))
  5. ENGINE = InnoDB;
  6.  
  7.  
  8. CREATE TABLE IF NOT EXISTS `numbers` (
  9. `id_number` VARCHAR(255) NULL,
  10. `indeks` VARCHAR(255) NULL,
  11. PRIMARY KEY (`id_number`))
  12. ENGINE = InnoDB;


przkladowo tabela product

id_product | indeks
1 | ABC123
2 | ZZ2333


tabela numbers
id_number | indeks
1 | ABC123
2 | ABC123
3 | ZZ333
4 | ZZ333




Czy można powiązać tabele ze sobą po przez pole indeks ?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 16)
Crozin
post
Post #2





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

Ostrzeżenie: (0%)
-----


Tak. Absolutnie niczym się to nie różni od dowolnego innego powiązania.
Go to the top of the page
+Quote Post
Pyton_000
post
Post #3





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Tylko pozakładaj na index klucze
Go to the top of the page
+Quote Post
netvalue
post
Post #4





Grupa: Zarejestrowani
Postów: 199
Pomógł: 2
Dołączył: 9.06.2008

Ostrzeżenie: (0%)
-----


ok więc Tworze tabele

tabela product

  1. CREATE TABLE IF NOT EXISTS `product` (
  2. `id_product` INT(11) NOT NULL,
  3. `indeks` VARCHAR(255) NULL,
  4. PRIMARY KEY (`id_product`))
  5. ENGINE = InnoDB;


oraz tabela numbers

  1. CREATE TABLE IF NOT EXISTS `numbers` (
  2. `id_number` INT(11) NOT NULL,
  3. `indeks` VARCHAR(45) NOT NULL,
  4. PRIMARY KEY (`id_number`),
  5. INDEX `fk_product_numbers_product2_idx` (`indeks` ASC),
  6. CONSTRAINT `fk_product_numbers_product2`
  7. FOREIGN KEY (`indeks`)
  8. REFERENCES `product` (`indeks`)
  9. ON DELETE NO ACTION
  10. ON UPDATE NO ACTION)
  11. ENGINE = InnoDB;


ale przy tworzeniu tabeli numbers dostaje błąd ze nie moze utrworzyc tabeli i "Supports transactions, row-level locking, and foreign keys "

Ten post edytował netvalue 26.08.2014, 10:11:30
Go to the top of the page
+Quote Post
nospor
post
Post #5





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Jeden ma 255 drugi 45....
Go to the top of the page
+Quote Post
netvalue
post
Post #6





Grupa: Zarejestrowani
Postów: 199
Pomógł: 2
Dołączył: 9.06.2008

Ostrzeżenie: (0%)
-----


Cytat(nospor @ 26.08.2014, 11:11:39 ) *
Jeden ma 255 drugi 45....

trafne spostrzeżenie , jednakże dalej otrzymuje ten sam błąd (IMG:style_emoticons/default/sad.gif)
Go to the top of the page
+Quote Post
nospor
post
Post #7





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




To pokaz kod po zmianach i dokładnie blad jaki dostajesz
Go to the top of the page
+Quote Post
netvalue
post
Post #8





Grupa: Zarejestrowani
Postów: 199
Pomógł: 2
Dołączył: 9.06.2008

Ostrzeżenie: (0%)
-----


  1. CREATE TABLE IF NOT EXISTS `product` (
  2. `id_product` INT(11) NOT NULL,
  3. `indeks` VARCHAR(255) NULL,
  4. PRIMARY KEY (`id_product`))
  5. ENGINE = InnoDB;
  6.  
  7.  
  8. CREATE TABLE IF NOT EXISTS `numbers` (
  9. `id_number` INT(11) NOT NULL,
  10. `indeks` VARCHAR(255) NOT NULL,
  11. PRIMARY KEY (`id_number`),
  12. INDEX `fk_product_numbers_product2_idx` (`indeks` ASC),
  13. CONSTRAINT `fk_product_numbers_product2`
  14. FOREIGN KEY (`indeks`)
  15. REFERENCES `product` (`indeks`)
  16. ON DELETE NO ACTION
  17. ON UPDATE NO ACTION)
  18. ENGINE = InnoDB;
  19.  



Bład:

  1. #1005 - Can't create table 'cdcol.numbers' (errno: 150)
  2. Mechanizmy składowania InnoDB
  3. Supports transactions, row-level locking, AND FOREIGN KEYS
Go to the top of the page
+Quote Post
nospor
post
Post #9





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Brakuje klucza/indeksu w tabeli produkt na pole indeks
Powód edycji: [nospor]:
Go to the top of the page
+Quote Post
netvalue
post
Post #10





Grupa: Zarejestrowani
Postów: 199
Pomógł: 2
Dołączył: 9.06.2008

Ostrzeżenie: (0%)
-----


dzięki Nospor !
Go to the top of the page
+Quote Post
nospor
post
Post #11





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Proszę.

A możesz opisać słownie co to ma robić i czemu to ma służyć? Bo na chwilę obecną wygląda to tak trochę bez sensu.
Go to the top of the page
+Quote Post
netvalue
post
Post #12





Grupa: Zarejestrowani
Postów: 199
Pomógł: 2
Dołączył: 9.06.2008

Ostrzeżenie: (0%)
-----


indeks to unikalny varchar dla produktu , w tabeli numbers dojda jeszcze kolejne pola jak foreign_nr
Chodzi o to zeby wyswietlac dla produktu jego inne numery (foreign_nr)
Go to the top of the page
+Quote Post
nospor
post
Post #13





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Cytat
Chodzi o to zeby wyswietlac dla produktu jego inne numery
No dobra, to po co ci do tego indeks? Czemu nie skorzystasz z id_product?
Go to the top of the page
+Quote Post
netvalue
post
Post #14





Grupa: Zarejestrowani
Postów: 199
Pomógł: 2
Dołączył: 9.06.2008

Ostrzeżenie: (0%)
-----


poniewaz bazuje na gotowych danych i nie mam powiazania id_product
Go to the top of the page
+Quote Post
nospor
post
Post #15





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Ale dodajac dany produkt do swojej bazy, już masz id_product.

Dobra, nie wnikam wiecej (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
netvalue
post
Post #16





Grupa: Zarejestrowani
Postów: 199
Pomógł: 2
Dołączył: 9.06.2008

Ostrzeżenie: (0%)
-----


dane do tabelki products i number mam juz gotowe i nie mam mozliwosci polaczyc ich inaczej niz po przez indeks
Go to the top of the page
+Quote Post
nospor
post
Post #17





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Co to znaczy "gotowe"?
Jesli importujesz je z jakiegoś xls czy csv czy xml, to wkladajac je do swojej bazy masz id_product i mozesz spokojnie przez to łączyc z tabelą number. Oszczedzisz miejsca i czasu.

Ale jesli nie chcesz tak robic, to nie ma sensu dalej nad tym dyskutowac (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 7.10.2025 - 15:27