Link to home
Start Free TrialLog in
Avatar of Achton
Achton

asked on

SQL and FOREIGN KEY constraint syntax


I have two tables in a MySQL database. I create them like this:

CREATE DATABASE test;

CREATE TABLE test.testtable_1 (
  did    SMALLINT
         NOT NULL
         AUTO_INCREMENT,
  kid    INT
         NOT NULL,
  PRIMARY KEY (did),
  INDEX (did),
  INDEX (kid),
  CONSTRAINT kid_fkey FOREIGN KEY (kid)
    REFERENCES testtable_2 ON DELETE NO ACTION
);

CREATE TABLE test.testtable_2 (
  kid    INT
         NOT NULL
         AUTO_INCREMENT
         PRIMARY KEY,
  INDEX (kid)
);


I want the foreign relationship between testtable1.kid and testtable2.kid to ensure that I cannot insert rows in testtable1 where kid does not match with a kid from testtable2. However, when I look at the table in PHPMyAdmin, the relationship is not shown and I can also insert whichever data I want into testtable1, regardless of the data in testtable2.

What is the correct way to create a foreign key constraint that enforces this behaviour?

/A.
SOLUTION
Avatar of cjjclifford
cjjclifford

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Achton
Achton

ASKER

No error message whatsoever. I guess MyISAM bases just ignore foreign keys.

But yes, I see now that they need to be InnoDB, and I also believe that the constraint should be tied to testtable2, and from there to testtable1's primary key. Actually, I want to have several key constraints from testtable3, 4 and 5 to fields in testtable1, but since foreign key constraints can only be tied to primary keys, how can I do this? Have several primary keys in testtable1, maybe?

I'd think maybe like this:

CREATE DATABASE test;

CREATE TABLE test.testtable_1 (
  did    SMALLINT
         NOT NULL
         AUTO_INCREMENT,
  kid    INT
         NOT NULL,
  eid    INT
         NOT NULL,
  PRIMARY KEY (did,kid,eid)
) TYPE = INNODB;

CREATE TABLE test.testtable_2 (
  kid    INT
         NOT NULL
         AUTO_INCREMENT
         PRIMARY KEY,
  INDEX (kid),
  CONSTRAINT kid_fkey FOREIGN KEY (kid)
    REFERENCES testtable_1(kid) ON DELETE NO ACTION
) TYPE = INNODB;

CREATE TABLE test.testtable_3 (
  eid    INT
         NOT NULL
         AUTO_INCREMENT
         PRIMARY KEY,
  INDEX (eid),
  CONSTRAINT eid_fkey FOREIGN KEY (eid)
    REFERENCES testtable_1(eid) ON DELETE NO ACTION
) TYPE = INNODB;


But then I get the (in)famous "#1005 - Can't create table './test/testtable_2.frm' (errno: 150)"-error. What's up?

I'm using MySQL 4.0.16 for Netware.

Thanks,
A.
You can have several foreign keys (1 per table) to "connect" to the same parent table (ie the same primary key field). Now, what are you currently designing, giving more speakable column names would be helpful to understand (you, us, and anyone later needing to read your code/db design).
Also, you need to properly understand the concept of foreign keys in order to correctly use and implement them.
CHeers
just to extend on what angelIII says, a primary key with multiple columns is still only a single primary key - all 3 need to be defined for a foreign key...
Avatar of Achton

ASKER

angelIII: Thanks for the advice, but the naming is in Danish, so I'm not sure it would make things much easier to understand. But I'll try to make it more readable.

Apart from that, I have taken a basic database administration course at uni, so I *ought* to know something about it - only i was a long time ago, and I worked primarily with pgSQL. So my main problem is figuring out how MySQL works.

cjjclifford: I'm not sure I follow you. I was surprised that multiple primary keys were possible, without them becoming a composite key, or maybe the first one becoming the primary key, and the rest remaining candidate keys. But that is probably just my misconception of the subject.

Anyway, here's what I've learned (most of it is pretty obvious now that I think about it, but still):
- Use InnoDB
- Make sure all referenced keys have EXACTLY the same type - e.g. do not AUTO_INCREMENT one key and not the other, etc.
- Make sure all referenced key types have EXACTLY the same size and signing
- Make sure you create FOREIGN KEY references only to primary keys
- Make sure all referenced and referencing keys have associated INDEXes, and that they are created in the same order, and that they are created SEPERATELY for each foreign key
- Create the referencing tables LAST

So, the proper syntax for what I wanted to achieve would be:


CREATE TABLE status_tbl (
    sid     INTEGER
            NOT NULL,
    PRIMARY KEY (sid),
) TYPE = INNODB;

CREATE TABLE unit_tbl (
    uid     INTEGER
            NOT NULL,
    PRIMARY KEY (uid),
) TYPE = INNODB;

CREATE TABLE system_status  (
    id      SMALLINT
            NOT NULL
            AUTO_INCREMENT,
    sid     INTEGER
            NOT NULL,
    uid     INTEGER
            NOT NULL,
    PRIMARY KEY (id),
    INDEX (sid),
    FOREIGN KEY (sid)
      REFERENCES status_tbl(sid)
      ON DELETE CASCADE,
    INDEX (uid),
    FOREIGN KEY (uid)
      REFERENCES unit_tbl(uid)
      ON DELETE CASCADE
) TYPE = INNODB;

This works, and does what I want.

Thanks for your help guys, I hope you're happy with the way I split the points.

/A.¨
Hi,

Thanks for the points. I was just saying that a primary key composed of 2 or more columns is still only a single primary key (composite key).

Cheers,
C.