Jump to content

Foreign Key Issues!


mattclements

Recommended Posts

Hello, I use the following code to create relational tables:

/* Remove all old Tables that may cause an error */ 
DROP TABLE behaviour_code;
DROP TABLE behaviour_log;
DROP TABLE teacher;
DROP TABLE student;

/* Create All Tables */
/* Create Behaviour Code Table */ 
CREATE TABLE behaviour_code (
  `code` VARCHAR(6) NOT NULL,
  `name` TEXT(50) NOT NULL,
  `description` LONGTEXT,
  PRIMARY KEY (`code`)
)
ENGINE = InnoDB;

/* Create Student Table */
CREATE TABLE student (
  `id` INTEGER(3) UNSIGNED NOT NULL AUTO_INCREMENT,
  `first_name` TEXT(75) NOT NULL,
  `last_name` TEXT(75) NOT NULL,
  `form` VARCHAR(5) NOT NULL,
  PRIMARY KEY (`id`)
)
ENGINE = InnoDB;

/* Create Teacher Table */
CREATE TABLE teacher (
  `id` INTEGER(2) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` TEXT(150) NOT NULL,
  `form` VARCHAR(5),
  PRIMARY KEY (`id`)
)
ENGINE = InnoDB;

/* Create Behaviour Log Table */
CREATE TABLE behaviour_log (
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `date` DATETIME NOT NULL,
  `code` VARCHAR(45) NOT NULL,
  `student` VARCHAR(45) NOT NULL,
  `teacher` VARCHAR(45) NOT NULL,
  `description` LONGTEXT,
  `action_taken` LONGTEXT,
  PRIMARY KEY (`id`),
)
ENGINE = InnoDB;

/* Create Behaviour Log Relationships */
ALTER TABLE behaviour_log 
  ADD Foreign Key (student) references teacher(id),
  ADD Foreign Key (teacher) references student(id),
  ADD Foreign Key (code) references behaviour_code(code);

 

However when I run this in SQL I get the following error:

/* Create Behaviour Log Relationships */ ALTER TABLE behaviour_log ADD FOREIGN KEY ( student ) REFERENCES teacher( id ) ,

ADD FOREIGN KEY ( teacher ) REFERENCES student( id ) ,

ADD FOREIGN KEY ( code ) REFERENCES behaviour_code( code ) ;

 

 

 

MySQL said: 

 

#1005 - Can't create table './unit13/#sql-656_29ed.frm' (errno: 150)

 

Please help!

 

P.S. Im using: phpMyAdmin 2.10.1, SQL: 5.0.32-Debian_7etch3-log, PHP 5 with Apache Latest

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.