Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Usuwanie kaskadowe w MySql
Forum PHP.pl > Forum > Bazy danych > MySQL
leo70
Czy istnieje coś takiego jak usuwanie kaskadowe w mysql? Czyli usuwam rekord z jednej tabeli o kluczu głównym (np. id) i automatycznie usuwane sa rekordy z innych tabel w których kluczem obcym jest właśnie to id z pierwszej tabeli.
FiDO
Istnieje, ale domyslnie dopiero od wersji 4.0 (dostepny ogolnie od 3.23.34a ale tam trzeba skompilowac z opcja --with-innodb). Trzeba miec tez inny typ bazy, nie MyISAM, tylko InnoDB, w manualu jest o tym dosc obszerny kawalek textu, polecam poczytanie.
leo70
No dobra ale czy da się przekonwertować istniejącą bazę na ten typ?
FiDO
Masz napisane w manualu jak to zrobic.
leo70
Zrobiłem wszystko według Twoich wskazówek i wszystko jest ok w przypadku dwóch powiązanych tabel, ale gdy są np. 3 to z trzeciej juz nie usuwa. Nie wiem dlaczego.
uboottd
Pewnie masz zle zalozone klucze obce. Klucz obcy dziala dowolna ilosc tabeli wglab.

daj SHOW CREATE TABLE <nazwa> i zobacz czy rzecywiscie to wyglada tak jak powinno.
leo70
Tak wygląda struktura, ale dalej działa tylko na dwóch tabelach, poradź mi co trzeba zmienić bo ja już sam nie wiem
[sql:1:b98287a8f7]# phpMyAdmin MySQL-Dump
# version 2.4.0
# http://www.phpmyadmin.net/ (download page)
#
# Host: localhost
# Czas wygenerowania: 25 Lis 2003, 23:58
# Wersja serwera: 4.0.16
# Wersja php: 4.3.2-RC1
# Baza danych : `skc`
# --------------------------------------------------------

#
# Struktura tabeli dla `admin_skc`
#

CREATE TABLE admin_skc (
nazwa_pr char(16) binary NOT NULL default '',
haslo char(16) binary NOT NULL default '',
PRIMARY KEY (nazwa_pr)
) TYPE=InnoDB COMMENT='Przechowuje login i hasło administratora';
# --------------------------------------------------------

#
# Struktura tabeli dla `artykuly`
#

CREATE TABLE artykuly (
id_art int(3) unsigned NOT NULL auto_increment,
strona int(3) unsigned NOT NULL default '0',
id_autor int(4) unsigned NOT NULL default '0',
tytul_art char(40) binary NOT NULL default '',
PRIMARY KEY (id_art),
KEY id_art (id_art),
KEY id_autor (id_autor),
CONSTRAINT `0_56` FOREIGN KEY (`id_autor`) REFERENCES `autorzy` (`id_autor`) ON DELETE CASCADE
) TYPE=InnoDB COMMENT='Zawiera spis artykułów';
# --------------------------------------------------------

#
# Struktura tabeli dla `autorzy`
#

CREATE TABLE autorzy (
id_autor int(4) unsigned NOT NULL auto_increment,
imie_aut char(15) binary NOT NULL default '',
nazwisko_aut char(25) binary NOT NULL default '',
PRIMARY KEY (id_autor),
KEY id_autor (id_autor)
) TYPE=InnoDB COMMENT='Zawiera dane o autorach artykułów';
# --------------------------------------------------------

#
# Struktura tabeli dla `czasopisma`
#

CREATE TABLE czasopisma (
id_czasopisma int(5) unsigned NOT NULL auto_increment,
id_kategorii int(5) unsigned NOT NULL default '0',
tytul_czas char(20) binary NOT NULL default '',
PRIMARY KEY (id_czasopisma),
KEY id_czasopisma (id_czasopisma),
KEY id_kategorii (id_kategorii),
CONSTRAINT `0_40` FOREIGN KEY (`id_kategorii`) REFERENCES `kategorie_czasopism` (`id_kategorii`)
) TYPE=InnoDB COMMENT='Zawiera spis tytułów czasopism';
# --------------------------------------------------------

#
# Struktura tabeli dla `katalogi`
#

CREATE TABLE katalogi (
id_num char(10) binary NOT NULL default '',
data_wyd date NOT NULL default '0000-00-00',
nr_rok char(8) NOT NULL default '',
id_czasopisma int(5) unsigned NOT NULL default '0',
PRIMARY KEY (id_num),
KEY id_num (id_num),
KEY id_czasopisma (id_czasopisma),
CONSTRAINT `0_43` FOREIGN KEY (`id_czasopisma`) REFERENCES `czasopisma` (`id_czasopisma`)
) TYPE=InnoDB COMMENT='Zawiera spis pozycji katalogowych';
# --------------------------------------------------------

#
# Struktura tabeli dla `kategorie_czasopism`
#

CREATE TABLE kategorie_czasopism (
id_kategorii int(5) unsigned NOT NULL auto_increment,
nazwa_kat char(15) binary NOT NULL default '',
PRIMARY KEY (id_kategorii),
KEY id_kategorii (id_kategorii)
) TYPE=InnoDB COMMENT='Zawiera spis kategorii czasopism';
# --------------------------------------------------------

#
# Struktura tabeli dla `lista_art`
#

CREATE TABLE lista_art (
id_num char(10) binary NOT NULL default '',
id_art int(3) unsigned NOT NULL default '0',
PRIMARY KEY (id_num,id_art),
KEY id_art (id_art),
KEY id_num (id_num),
CONSTRAINT `0_45` FOREIGN KEY (`id_num`) REFERENCES `katalogi` (`id_num`) ON DELETE CASCADE,
CONSTRAINT `0_53` FOREIGN KEY (`id_art`) REFERENCES `artykuly` (`id_art`) ON DELETE CASCADE
) TYPE=InnoDB COMMENT='Zawiera listę artykułów w pozycjach katalogowych';
# --------------------------------------------------------

#
# Struktura tabeli dla `lista_slow`
#

CREATE TABLE lista_slow (
id_slowa int(5) unsigned NOT NULL default '0',
id_art int(3) unsigned NOT NULL default '0',
PRIMARY KEY (id_slowa,id_art),
KEY id_art (id_art),
KEY id_slowa (id_slowa),
CONSTRAINT `0_59` FOREIGN KEY (`id_art`) REFERENCES `artykuly` (`id_art`) ON DELETE CASCADE,
CONSTRAINT `0_61` FOREIGN KEY (`id_slowa`) REFERENCES `slowa_kluczowe` (`id_slowa`) ON DELETE CASCADE
) TYPE=InnoDB COMMENT='Zawiera listę słów kluczowych w artykułach';
# --------------------------------------------------------

#
# Struktura tabeli dla `relacje`
#

CREATE TABLE relacje (
master_db varchar(64) NOT NULL default '',
master_table varchar(64) NOT NULL default '',
master_field varchar(64) NOT NULL default '',
foreign_db varchar(64) NOT NULL default '',
foreign_table varchar(64) NOT NULL default '',
foreign_field varchar(64) NOT NULL default '',
PRIMARY KEY (master_db,master_table,master_field),
KEY foreign_field (foreign_db,foreign_table)
) TYPE=InnoDB COMMENT='Zawiera relacje';
# --------------------------------------------------------

#
# Struktura tabeli dla `rezerwacje`
#

CREATE TABLE rezerwacje (
id_rezerwacji int(5) unsigned NOT NULL auto_increment,
czas_rez char(2) NOT NULL default '3',
data_rez date NOT NULL default '0000-00-00',
id_student char(11) binary NOT NULL default '',
id_num char(10) binary NOT NULL default '',
PRIMARY KEY (id_rezerwacji),
KEY id_rezerwacji (id_rezerwacji),
KEY id_student (id_student),
KEY id_num (id_num),
CONSTRAINT `0_65` FOREIGN KEY (`id_num`) REFERENCES `katalogi` (`id_num`),
CONSTRAINT `0_67` FOREIGN KEY (`id_student`) REFERENCES `studenci` (`id_student`)
) TYPE=InnoDB COMMENT='Zawiera listę rezerwacji';
# --------------------------------------------------------

#
# Struktura tabeli dla `slowa_kluczowe`
#

CREATE TABLE slowa_kluczowe (
id_slowa int(5) unsigned NOT NULL auto_increment,
slowo char(30) NOT NULL default '',
PRIMARY KEY (id_slowa),
KEY id_słowa (id_slowa)
) TYPE=InnoDB COMMENT='Zawiera spis słów kluczowych';
# --------------------------------------------------------

#
# Struktura tabeli dla `studenci`
#

CREATE TABLE studenci (
id_student char(11) binary NOT NULL default '',
imie char(15) binary NOT NULL default '',
nazwisko char(25) binary NOT NULL default '',
PRIMARY KEY (id_student),
KEY id_student (id_student)
) TYPE=InnoDB COMMENT='Zawiera dane studentów';
# --------------------------------------------------------

#
# Struktura tabeli dla `student_skc`
#

CREATE TABLE student_skc (
id_student char(11) binary NOT NULL default '',
haslo char(16) binary NOT NULL default '',
PRIMARY KEY (id_student),
KEY id_student (id_student),
CONSTRAINT `0_76` FOREIGN KEY (`id_student`) REFERENCES `studenci` (`id_student`) ON DELETE CASCADE
) TYPE=InnoDB COMMENT='Zawiera login i hasło studenta';
# --------------------------------------------------------

#
# Struktura tabeli dla `wypozyczenia`
#

CREATE TABLE wypozyczenia (
id_wypozyczenia int(5) unsigned NOT NULL auto_increment,
data_wypozyczenia date NOT NULL default '0000-00-00',
data_zwrotu date NOT NULL default '0000-00-00',
id_num char(10) binary NOT NULL default '',
id_student char(11) binary NOT NULL default '',
PRIMARY KEY (id_wypozyczenia),
KEY id_wypozyczenia (id_wypozyczenia),
KEY id_num (id_num),
KEY id_student (id_student),
CONSTRAINT `0_71` FOREIGN KEY (`id_num`) REFERENCES `katalogi` (`id_num`),
CONSTRAINT `0_73` FOREIGN KEY (`id_student`) REFERENCES `studenci` (`id_student`)
) TYPE=InnoDB COMMENT='Zawiera listę wypożyczeń';

[/sql:1:b98287a8f7]
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.