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
https://forums.phpfreaks.com/topic/184702-foreign-keys-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.