I'm trying to wrap my head around many-to-many table connections.
I have three tables: Modules
, Phases
, and Module_Phases
(junction).
Both Modules
and Phases
have primary keys as auto_increment.
create table Modules
(
MID int not null auto_increment primary key,
modulesID int unique,
title varchar(20),
article Text,
dateAdded datetime default null,
UID int,
pictureurl varchar(100),
linkurl varchar(50),
KID int
);
create table Phases
(
PID int not null auto_increment primary key,
phasesID int(1),
phasename varchar(30),
phaseage varchar(10),
phasearea varchar (30),
MID int
);
create table Phases_Modules
(
PID int not null,
MID int not null
);
When I insert into a table, I usually disregard the primary key and just use:
insert into Modules(modulesID,title,article,pictureurl,linkurl) values (1,"namex","textcontent","text2","text3");
On the web, I've seen people suggest using:
insert into Phases_Modules(PID,MID) values (x,x);
But since both of the tables have auto_increment on the primary key, how do I go about using the correct ID key? I've seen some examples where they use LAST_INSERT_ID
. But can I, for example, use something like this:
insert into Phases_Modules(PID,MID) values (PID where Phases.phaseID=x, Modules.Last_Insert_ID);
I know this was kind of a bogus line, but it was just to try to explain the issue. :)