Jump to content

Foreign keys problem


bionic25

Recommended Posts

hey guys, i am having problems with foreign keys. my code below all executes fine in mysql, returning data for all the tables, except for 'activities' which is usinf foreign keys to reference the correctly functioning other tables. Can anyone see what is wrong because after looking at other examples for hours i cant figure it out!

 

CREATE TABLE modules (
module_id INT AUTO_INCREMENT,
deptcode VARCHAR(10),
module VARCHAR(10),
title VARCHAR(255),
Primary Key(module_id)
);

CREATE TABLE lecturers (
lecturer_id INT AUTO_INCREMENT,
fname VARCHAR(50),
lname VARCHAR(50),
Primary Key(lecturer_id)
);

CREATE TABLE groups (
group_id INT AUTO_INCREMENT,
groupName VARCHAR(12),
Primary Key(group_id)
);

INSERT INTO modules (deptcode, module, title) SELECT deptcode, module1, title1 FROM tempHolder;
INSERT INTO lecturers (fname, lname) SELECT SUBSTRING_INDEX(lecturer1, ', ', -1), SUBSTRING_INDEX(lecturer1, ', ', 1) FROM tempHolder;
INSERT INTO groups (groupName) SELECT group1 FROM tempHolder;


CREATE TABLE activities (
activity_id INT AUTO_INCREMENT, 
module_id integer, 
lecturer_id integer, 
group_id integer,
Primary Key (activity_id),
Foreign Key (module_id) references modules(module_id), 
Foreign Key (lecturer_id) references lecturers(lecturer_id), 
Foreign Key (group_id) references groups(group_id) 
);

//For debugging, output all results
select * from modules where module_id=3;
select * from lecturers where lecturer_id=3;
select * from groups where group_id=3;
select * from activities;

 

thanks in advance for any help  :D

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.