Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Usuwanie kaskadowe w MySql
leo70
post 25.11.2003, 13:03:19
Post #1





Grupa: Zarejestrowani
Postów: 8
Pomógł: 0
Dołączył: 25.11.2003

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


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.
Go to the top of the page
+Quote Post
FiDO
post 25.11.2003, 13:19:26
Post #2





Grupa: Przyjaciele php.pl
Postów: 1 717
Pomógł: 0
Dołączył: 12.06.2002
Skąd: Wolsztyn..... Studia: Zielona Góra

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


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.


--------------------
Brak czasu :/
Go to the top of the page
+Quote Post
leo70
post 25.11.2003, 13:36:39
Post #3





Grupa: Zarejestrowani
Postów: 8
Pomógł: 0
Dołączył: 25.11.2003

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


No dobra ale czy da się przekonwertować istniejącą bazę na ten typ?
Go to the top of the page
+Quote Post
FiDO
post 25.11.2003, 15:22:45
Post #4





Grupa: Przyjaciele php.pl
Postów: 1 717
Pomógł: 0
Dołączył: 12.06.2002
Skąd: Wolsztyn..... Studia: Zielona Góra

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


Masz napisane w manualu jak to zrobic.


--------------------
Brak czasu :/
Go to the top of the page
+Quote Post
leo70
post 25.11.2003, 19:00:47
Post #5





Grupa: Zarejestrowani
Postów: 8
Pomógł: 0
Dołączył: 25.11.2003

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


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.
Go to the top of the page
+Quote Post
uboottd
post 25.11.2003, 22:41:47
Post #6





Grupa: Zarejestrowani
Postów: 384
Pomógł: 0
Dołączył: 3.04.2003
Skąd: Chorzow

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


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.
Go to the top of the page
+Quote Post
leo70
post 26.11.2003, 00:07:02
Post #7





Grupa: Zarejestrowani
Postów: 8
Pomógł: 0
Dołączył: 25.11.2003

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


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]
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 Wersja Lo-Fi Aktualny czas: 18.07.2025 - 10:56