Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Klucz obcy na kilku polach
Indeo
post
Post #1





Grupa: Zarejestrowani
Postów: 295
Pomógł: 7
Dołączył: 26.03.2004
Skąd: Opole

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


Czy da się w mysql zbudować klucz obcy będący kombinacją kilku pól?
Są dwie tabele
Nadrzędna A:
Kod
col1,col2,col3
   1|   1| Ala
   1|   2| Kasia
   2|   1| Basia
   2|   2| Zosia
PRIMARY KEY on (col1,col2)

Podrzędna B:
Kod
col1,col2,col4
   1|  1| 34
   1|  1| 12
   1|  2| 67
   2|  1| 98
PRIMARY KEY on (col1,col2,col4)

i chcę teraz żeby usuwając rekord z tabeli A o danej wartości klucza (col1,col2)
osunięte zostały z tabeli B wszystkie rekordy dla których B(col1,col2)=A(col1,col2)
Jak to zapisać?


--------------------
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
nospor
post
Post #2





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




a czy zajrzenie do manuala:
http://dev.mysql.com/doc/refman/5.1/en/inn...onstraints.html
i wyczytanie czy jest to możliwe czy nie to tak cięzko?


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
Indeo
post
Post #3





Grupa: Zarejestrowani
Postów: 295
Pomógł: 7
Dołączył: 26.03.2004
Skąd: Opole

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


Mam taki kod sql tworzący dwie tabele i na końcu foreign key:
  1. CREATE TABLE IF NOT EXISTS `app_forms` (
  2. `window_id` varchar(255) COLLATE utf8_polish_ci NOT NULL,
  3. `form_id` varchar(255) COLLATE utf8_polish_ci NOT NULL,
  4. `form_title` varchar(255) COLLATE utf8_polish_ci NOT NULL,
  5. `form_width` int(4) NOT NULL DEFAULT '300',
  6. `action` varchar(255) COLLATE utf8_polish_ci NOT NULL DEFAULT 'index.php',
  7. `method` enum('GET','POST') COLLATE utf8_polish_ci NOT NULL DEFAULT 'GET',
  8. PRIMARY KEY (`window_id`(64),`form_id`(64)),
  9. KEY `window_id` (`window_id`),
  10. KEY `form_id` (`form_id`)
  11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci;
  12.  
  13. CREATE TABLE IF NOT EXISTS `app_datasets` (
  14. `window_id` varchar(255) COLLATE utf8_polish_ci NOT NULL,
  15. `form_id` varchar(255) COLLATE utf8_polish_ci NOT NULL,
  16. `data_id` varchar(255) COLLATE utf8_polish_ci NOT NULL,
  17. `query` text COLLATE utf8_polish_ci NOT NULL,
  18. `klucz` varchar(255) COLLATE utf8_polish_ci DEFAULT NULL,
  19. `outer_klucz` varchar(255) COLLATE utf8_polish_ci NOT NULL,
  20. `readonly` enum('yes','no') COLLATE utf8_polish_ci NOT NULL DEFAULT 'no',
  21. PRIMARY KEY (`window_id`(64),`form_id`(64),`data_id`(64)),
  22. KEY `window_id` (`window_id`),
  23. KEY `form_id` (`form_id`),
  24. KEY `data_id` (`data_id`)
  25. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci;
  26.  
  27. ALTER TABLE `app_datasets` ADD
  28. CONSTRAINT `app_datasets_window_id_form_id_fk`
  29. FOREIGN KEY (`window_id`,`form_id`)
  30. REFERENCES
  31. `app_forms` (`window_id`,`form_id`)
  32. ON DELETE CASCADE;


i dostaje error:
Kod
#1005 - Can't create table 'test.#sql-848_1cb' (errno: 150)

i wielki płacz
Co robię nie tak?


--------------------
Go to the top of the page
+Quote Post
nospor
post
Post #4





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




NIe zebym sie czepial, ale na stronie do ktorej dalem ci linka masz ten problem opisany...
Cytat
Also, it is interesting to note that while this query works (Note the PRIMARY KEY line):

CREATE TABLE `ffxi_characterJob` (
`serverID` int(11) NOT NULL,
`userid` int(10)unsigned NOT NULL,
`characterName` varchar(255) NOT NULL,
`jobAbbr` char(4) NOT NULL,
`jobLevel` int(11) default '0',
PRIMARY KEY (`serverID`,`userid`,`characterName`,`jobAbbr`),
INDEX (`jobAbbr`),
CONSTRAINT FOREIGN KEY (`serverID`,`userid`,`characterName`) REFERENCES `ffxi_characters` (`serverID`,`userid`,`characterName`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (`jobAbbr`) REFERENCES `ffxi_jobType` (`jobAbbr`) ON DELETE CASCADE ON UPDATE CASCADE
) TYPE=InnoDB;

This query will give you an error 1005 and errno 150:

CREATE TABLE `ffxi_characterJob` (
`serverID` int(11) NOT NULL,
`userid` int(10)unsigned NOT NULL,
`characterName` varchar(255) NOT NULL,
`jobAbbr` char(4) NOT NULL,
`jobLevel` int(11) default '0',
PRIMARY KEY (`jobAbbr`,`serverID`,`userid`,`characterName`),
INDEX (`jobAbbr`),
CONSTRAINT FOREIGN KEY (`serverID`,`userid`,`characterName`) REFERENCES `ffxi_characters` (`serverID`,`userid`,`characterName`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (`jobAbbr`) REFERENCES `ffxi_jobType` (`jobAbbr`) ON DELETE CASCADE ON UPDATE CASCADE
) TYPE=InnoDB;

In order to make the second one work, you have to add:
INDEX (`serverID`,`userid`,`characterName`)
before the the foreign key is made.

Jakby co to podane tu rozwiązanie działa.


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 21.08.2025 - 09:21