Toon posts:

[MySQL] Foreign keys met InnoDB implementeren

Pagina: 1
Acties:

Verwijderd

Topicstarter
Zoals jullie eerder hebben kunnen lezen ben ik een database op aan het zetten aan de hand van MySQL met InnoDB. Na wat prutswerk is dit goed gelukt.

Nadat ik mijn database had aangemaakt was het tijd voor tables. Dit ging echter niet goed. Er zijn 2 tables, te weten "persoon" en "tekening". Persoon heeft als primary key het veld "gebr_id" en dat is een automatisch ophogende interger.
Tekening heeft als primary key "tek_id" en een veld "owner" wat een foreign key van persoon(gebr_id) is.

Alles goed en wel, het lukt me niet om de table "tekening" aan te maken. Ik krijg dan foutmelding 1005/errno 150.

Quote van de MySQL site:
If MySQL gives the error number 1005 from a CREATE TABLE statement, and the error message string refers to errno 150, then the table creation failed because a foreign key constraint was not correctly formed. Similarly, if an ALTER TABLE fails and it refers to errno 150, that means a foreign key definition would be incorrectly formed for the altered table.

Starting from version 3.23.50 InnoDB allows you to add a new foreign key constraint to a table through

ALTER TABLE yourtablename
ADD [CONSTRAINT symbol] FOREIGN KEY (...) REFERENCES anothertablename(...)

Remember to create the required indexes first, though. In InnoDB versions < 3.23.50 ALTER TABLE or CREATE INDEX should not be used in connection with tables which have foreign key constraints or which are referenced in foreign key constraints: Any ALTER TABLE removes all foreign key constrainst defined for the table. You should not use ALTER TABLE to the referenced table either, but use DROP TABLE and CREATE TABLE to modify the schema. When MySQL does an ALTER TABLE it may internally use RENAME TABLE, and that will confuse the foreign key costraints which refer to the table. A CREATE INDEX statement is in MySQL processed as an ALTER TABLE, and these restrictions apply also to it.
De foreign key heb ik als volgt gedefinieerd:
FOREIGN KEY (owner) REFERENCES PERSOON(gebr_id)

Wat doe ik hier fout? Uit de quote kun je concluderen dat
1. ik een statement ADD[Constraint symbol] moet toepassen.... wat is dit?
2. ik een create index moet doorvoeren... wat is dit?

Sorry dat ik een beetje knullig alles formuleer, dit is niet echt mijn vakgebied. Bij voorbat dank :).

  • Bosmonster
  • Registratie: Juni 2001
  • Laatst online: 29-08 19:47

Bosmonster

*zucht*

JE hebt een vreemde sleutel, maar daar moeten nog "constraints" aan verbonden worden. Oftewel, wat moet er gebeuren als een van de twee gewijzigd of verwijderd wordt. Bijvoorbeeld (geen idee hoe de juiste syntax hier is):

ON DELETE CASCADE
ON UPDATE NO ACTION

  • cameodski
  • Registratie: Augustus 2002
  • Laatst online: 06-11-2023
Ik denk dat je statement niet helemaal klopt. Kan het helaas niet testen, maar ik denk dat het zoiets moet worden:
code:
1
2
3
4
ALTER TABLE tekening
ADD CONSTRAINT FK_tekening_persoon
FOREIGN KEY (owner)
REFERENCES Persoon (gebr_id)

Never underestimate the power of


Verwijderd

Topicstarter
Mijn create ziet er als volgt uit:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
CREATE TABLE PERSOON(
 gebr_id    INT NOT NULL auto_increment,
 nickname   VARCHAR(20) DEFAULT '' NOT NULL,
 voornaam   VARCHAR(20) DEFAULT '' NOT NULL,
 achternaam VARCHAR(30) DEFAULT '' NOT NULL,
 adres      VARCHAR(50) DEFAULT '' NOT NULL,
 postcode   VARCHAR(7) DEFAULT '####-##' NOT NULL,
 woonplaats VARCHAR(20) DEFAULT '' NOT NULL,
 geb_datum  DATE DEFAULT '##-##-####' NOT NULL,
 geslacht   VARCHAR(1) DEFAULT '' NOT NULL,
 telefoon   VARCHAR(10) DEFAULT '',
 email      VARCHAR(50) DEFAULT '' NOT NULL,
 PRIMARY KEY    (gebr_id)
) TYPE = InnoDB;

CREATE TABLE TEKENING(
 tek_id     INT NOT NULL auto_increment,
 owner      INT NOT NULL,
 voorbeeldid    INT NOT NULL,
 timestamp  TIMESTAMP NOT NULL,
 lijnid     INT NOT NULL,
 lijnstart_x    INT NOT NULL,
 lijnstart_y    INT NOT NULL,
 lijneinde_x    INT NOT NULL,
 lijneinde_y    INT NOT NULL,
 PRIMARY KEY    (tek_id),
 FOREIGN KEY (owner) REFERENCES PERSOON(gebr_id) ON DELETE CASCADE 
) TYPE = InnoDB;


Dit ziet er toch niet zo vreemd uit? Tenminste, voor iemand die met Oracle heeft gewerkt niet ;)

  • cameodski
  • Registratie: Augustus 2002
  • Laatst online: 06-11-2023
Ziet er inderdaad niet echt vreemd uit, maar misschien vindt MySQL het niet leuk als je gelijk in de CREATE TABLE ook de foreign keys toevoegt.

Never underestimate the power of


  • D2k
  • Registratie: Januari 2001
  • Laatst online: 10:19

D2k

mjah zolang de volgorde van aanmaken tabellen en foreign keys klopt zou er weinig aan de hand moeten zijn

Doet iets met Cloud (MS/IBM)


Verwijderd

code:
1
alter table a add constraint b foreign key bla_id references c (id) on delete cascade


werkt lijkt mij altijd, en anders misschien zoiets....

code:
1
2
3
4
5
6
7
8
create table a (
  id int auto_increment
);

create table b (
  id int auto_increment,
  a_id int constraint FK_aid references a (id) on delete cascade
);

Verwijderd

Topicstarter
Wat ik ook probeer, krijg het niet aan de gang!

Wat me wel gelukt is, is het aanmaken van de tabellen tekening en persoonm, maar zonder FK's erin.

  • robjanssen
  • Registratie: September 2001
  • Laatst online: 02-08 16:10

robjanssen

Software Developer

Probeer het eens zo:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
CREATE TABLE PERSOON(
 gebr_id    INT NOT NULL auto_increment CONSTRAINT PK_PERSOON PRIMARY KEY,
 nickname   VARCHAR(20) DEFAULT '' NOT NULL,
 voornaam   VARCHAR(20) DEFAULT '' NOT NULL,
 achternaam VARCHAR(30) DEFAULT '' NOT NULL,
 adres      VARCHAR(50) DEFAULT '' NOT NULL,
 postcode   VARCHAR(7) DEFAULT '####-##' NOT NULL,
 woonplaats VARCHAR(20) DEFAULT '' NOT NULL,
 geb_datum  DATE DEFAULT '##-##-####' NOT NULL,
 geslacht   VARCHAR(1) DEFAULT '' NOT NULL,
 telefoon   VARCHAR(10) DEFAULT '',
 email      VARCHAR(50) DEFAULT '' NOT NULL
) TYPE = InnoDB;

CREATE TABLE TEKENING(
 tek_id     INT NOT NULL auto_increment CONSTRAINT PK_TEKENING PRIMARY KEY,
 owner      INT NOT NULL CONSTRAINT FK_OWNER REFERENCES PERSOON (gebr_id),
 voorbeeldid    INT NOT NULL,
 timestamp  TIMESTAMP NOT NULL,
 lijnid     INT NOT NULL,
 lijnstart_x    INT NOT NULL,
 lijnstart_y    INT NOT NULL,
 lijneinde_x    INT NOT NULL,
 lijneinde_y    INT NOT NULL
) TYPE = InnoDB;

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Weet je zeker dat je innodb support hebt?
Als je een Type=InnoDB tabel aangemaakt hebt, is die dan ook daadwerkelijk InnoDB?

Ow en je hebt natuurlijk wel mysql 3.2.50 of hoger he? :)
Pagina: 1