Click here to get back home

Foreign Key Problem

 HomeNewsGroups | Search | About
 mailing.database.mysql    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Foreign Key Problem coosa 10-18-2006
Get Chitika Premium
Posted by coosa on October 18, 2006, 1:38 pm
Please log in for more thread options
Dear all,

I'm new under mysql and have installed mysql5.0.24a community edition
for win32.
I have tried to implement a foreign key for this following sample
scenario:

CREATE TABLE student (
student_id INTEGER NOT NULL AUTO_INCREMENT,
student_name VARCHAR(100) NOT NULL,
CONSTRAINT PK_student PRIMARY KEY (student_id)
) ENGINE=INNODB;


CREATE TABLE faculty (
faculty_id INTEGER NOT NULL AUTO_INCREMENT,
faculty_name VARCHAR(100) NOT NULL,
CONSTRAINT PK_faculty PRIMARY KEY (faculty_id)
) ENGINE=INNODB;

CREATE TABLE student_faculty (
student_id INTEGER NOT NULL,
faculty_id INTEGER NOT NULL,
CONSTRAINT PK_student_faculty PRIMARY KEY (student_id, faculty_id)
) ENGINE=INNODB;

ALTER TABLE student_faculty ADD CONSTRAINT student_student_faculty
FOREIGN KEY (student_id) REFERENCES student (student_id);

ALTER TABLE student_faculty ADD CONSTRAINT faculty_student_faculty
FOREIGN KEY (faculty_id) REFERENCES faculty (faculty_id);

insert into student (student_name) values ('John');
insert into student (student_name) values ('Robert');

insert into faculty (faculty_name) values ('Information Technology');
insert into faculty (faculty_name) values ('Engineering');

Now there are student_id 1 and 2; same goes for faculty_id
so this statement should be ok:
insert into student_faculty (student_id, faculty_id) values (1,1);
However, there is no student_id or faculty_id greater than 2 yet, but
this statement still executes from mysql:
insert into student_faculty (student_id, faculty_id) values (3,1);
whereby it shouldn't since it does not inforce integrity based on the
foreign keys i have created!


Posted by coosa on October 18, 2006, 5:12 pm
Please log in for more thread options
Ok, i have checked back again and it seems the my storage engine is not
Innodb but ISAM!
Is there a way i can change the engine into Innodb?
I have the default MySql Administrator from mysql.com and phpmyadmin
and none of them offer a a way to change that


coosa wrote:
> Dear all,
>
> I'm new under mysql and have installed mysql5.0.24a community edition
> for win32.
> I have tried to implement a foreign key for this following sample
> scenario:
>
> CREATE TABLE student (
> student_id INTEGER NOT NULL AUTO_INCREMENT,
> student_name VARCHAR(100) NOT NULL,
> CONSTRAINT PK_student PRIMARY KEY (student_id)
> ) ENGINE=INNODB;
>
>
> CREATE TABLE faculty (
> faculty_id INTEGER NOT NULL AUTO_INCREMENT,
> faculty_name VARCHAR(100) NOT NULL,
> CONSTRAINT PK_faculty PRIMARY KEY (faculty_id)
> ) ENGINE=INNODB;
>
> CREATE TABLE student_faculty (
> student_id INTEGER NOT NULL,
> faculty_id INTEGER NOT NULL,
> CONSTRAINT PK_student_faculty PRIMARY KEY (student_id, faculty_id)
> ) ENGINE=INNODB;
>
> ALTER TABLE student_faculty ADD CONSTRAINT student_student_faculty
> FOREIGN KEY (student_id) REFERENCES student (student_id);
>
> ALTER TABLE student_faculty ADD CONSTRAINT faculty_student_faculty
> FOREIGN KEY (faculty_id) REFERENCES faculty (faculty_id);
>
> insert into student (student_name) values ('John');
> insert into student (student_name) values ('Robert');
>
> insert into faculty (faculty_name) values ('Information Technology');
> insert into faculty (faculty_name) values ('Engineering');
>
> Now there are student_id 1 and 2; same goes for faculty_id
> so this statement should be ok:
> insert into student_faculty (student_id, faculty_id) values (1,1);
> However, there is no student_id or faculty_id greater than 2 yet, but
> this statement still executes from mysql:
> insert into student_faculty (student_id, faculty_id) values (3,1);
> whereby it shouldn't since it does not inforce integrity based on the
> foreign keys i have created!


Posted by Peter on October 19, 2006, 1:39 am
Please log in for more thread options
First of all, issue the SHOW ENGINES query to determine if you have
InnoDB.

If not, the easiest way is probably to reinstall MySQL, making sure to
do a detailed installation and selecting InnoDB.

coosa wrote:
> Ok, i have checked back again and it seems the my storage engine is not
> Innodb but ISAM!
> Is there a way i can change the engine into Innodb?
> I have the default MySql Administrator from mysql.com and phpmyadmin
> and none of them offer a a way to change that
>
>
> coosa wrote:
> > Dear all,
> >
> > I'm new under mysql and have installed mysql5.0.24a community edition
> > for win32.
> > I have tried to implement a foreign key for this following sample
> > scenario:
> >
> > CREATE TABLE student (
> > student_id INTEGER NOT NULL AUTO_INCREMENT,
> > student_name VARCHAR(100) NOT NULL,
> > CONSTRAINT PK_student PRIMARY KEY (student_id)
> > ) ENGINE=INNODB;
> >
> >
> > CREATE TABLE faculty (
> > faculty_id INTEGER NOT NULL AUTO_INCREMENT,
> > faculty_name VARCHAR(100) NOT NULL,
> > CONSTRAINT PK_faculty PRIMARY KEY (faculty_id)
> > ) ENGINE=INNODB;
> >
> > CREATE TABLE student_faculty (
> > student_id INTEGER NOT NULL,
> > faculty_id INTEGER NOT NULL,
> > CONSTRAINT PK_student_faculty PRIMARY KEY (student_id, faculty_id)
> > ) ENGINE=INNODB;
> >
> > ALTER TABLE student_faculty ADD CONSTRAINT student_student_faculty
> > FOREIGN KEY (student_id) REFERENCES student (student_id);
> >
> > ALTER TABLE student_faculty ADD CONSTRAINT faculty_student_faculty
> > FOREIGN KEY (faculty_id) REFERENCES faculty (faculty_id);
> >
> > insert into student (student_name) values ('John');
> > insert into student (student_name) values ('Robert');
> >
> > insert into faculty (faculty_name) values ('Information Technology');
> > insert into faculty (faculty_name) values ('Engineering');
> >
> > Now there are student_id 1 and 2; same goes for faculty_id
> > so this statement should be ok:
> > insert into student_faculty (student_id, faculty_id) values (1,1);
> > However, there is no student_id or faculty_id greater than 2 yet, but
> > this statement still executes from mysql:
> > insert into student_faculty (student_id, faculty_id) values (3,1);
> > whereby it shouldn't since it does not inforce integrity based on the
> > foreign keys i have created!


Posted by coosa on October 19, 2006, 5:50 am
Please log in for more thread options
Is there a way to plaay around with my.ini file instead of
reinstalling?

Peter wrote:
> First of all, issue the SHOW ENGINES query to determine if you have
> InnoDB.
>
> If not, the easiest way is probably to reinstall MySQL, making sure to
> do a detailed installation and selecting InnoDB.
>
> coosa wrote:
> > Ok, i have checked back again and it seems the my storage engine is not
> > Innodb but ISAM!
> > Is there a way i can change the engine into Innodb?
> > I have the default MySql Administrator from mysql.com and phpmyadmin
> > and none of them offer a a way to change that
> >
> >
> > coosa wrote:
> > > Dear all,
> > >
> > > I'm new under mysql and have installed mysql5.0.24a community edition
> > > for win32.
> > > I have tried to implement a foreign key for this following sample
> > > scenario:
> > >
> > > CREATE TABLE student (
> > > student_id INTEGER NOT NULL AUTO_INCREMENT,
> > > student_name VARCHAR(100) NOT NULL,
> > > CONSTRAINT PK_student PRIMARY KEY (student_id)
> > > ) ENGINE=INNODB;
> > >
> > >
> > > CREATE TABLE faculty (
> > > faculty_id INTEGER NOT NULL AUTO_INCREMENT,
> > > faculty_name VARCHAR(100) NOT NULL,
> > > CONSTRAINT PK_faculty PRIMARY KEY (faculty_id)
> > > ) ENGINE=INNODB;
> > >
> > > CREATE TABLE student_faculty (
> > > student_id INTEGER NOT NULL,
> > > faculty_id INTEGER NOT NULL,
> > > CONSTRAINT PK_student_faculty PRIMARY KEY (student_id, faculty_id)
> > > ) ENGINE=INNODB;
> > >
> > > ALTER TABLE student_faculty ADD CONSTRAINT student_student_faculty
> > > FOREIGN KEY (student_id) REFERENCES student (student_id);
> > >
> > > ALTER TABLE student_faculty ADD CONSTRAINT faculty_student_faculty
> > > FOREIGN KEY (faculty_id) REFERENCES faculty (faculty_id);
> > >
> > > insert into student (student_name) values ('John');
> > > insert into student (student_name) values ('Robert');
> > >
> > > insert into faculty (faculty_name) values ('Information Technology');
> > > insert into faculty (faculty_name) values ('Engineering');
> > >
> > > Now there are student_id 1 and 2; same goes for faculty_id
> > > so this statement should be ok:
> > > insert into student_faculty (student_id, faculty_id) values (1,1);
> > > However, there is no student_id or faculty_id greater than 2 yet, but
> > > this statement still executes from mysql:
> > > insert into student_faculty (student_id, faculty_id) values (3,1);
> > > whereby it shouldn't since it does not inforce integrity based on the
> > > foreign keys i have created!


Posted by Peter on October 19, 2006, 9:14 pm
Please log in for more thread options
You could try using the C:\Program Files\MySQL\MySQL Server
5.0\bin\MySQLInstanceConfig.exe tool to enable InnoDB.

coosa wrote:
> Is there a way to plaay around with my.ini file instead of
> reinstalling?
>
> Peter wrote:
> > First of all, issue the SHOW ENGINES query to determine if you have
> > InnoDB.
> >
> > If not, the easiest way is probably to reinstall MySQL, making sure to
> > do a detailed installation and selecting InnoDB.
> >
> > coosa wrote:
> > > Ok, i have checked back again and it seems the my storage engine is not
> > > Innodb but ISAM!
> > > Is there a way i can change the engine into Innodb?
> > > I have the default MySql Administrator from mysql.com and phpmyadmin
> > > and none of them offer a a way to change that
> > >
> > >
> > > coosa wrote:
> > > > Dear all,
> > > >
> > > > I'm new under mysql and have installed mysql5.0.24a community edition
> > > > for win32.
> > > > I have tried to implement a foreign key for this following sample
> > > > scenario:
> > > >
> > > > CREATE TABLE student (
> > > > student_id INTEGER NOT NULL AUTO_INCREMENT,
> > > > student_name VARCHAR(100) NOT NULL,
> > > > CONSTRAINT PK_student PRIMARY KEY (student_id)
> > > > ) ENGINE=INNODB;
> > > >
> > > >
> > > > CREATE TABLE faculty (
> > > > faculty_id INTEGER NOT NULL AUTO_INCREMENT,
> > > > faculty_name VARCHAR(100) NOT NULL,
> > > > CONSTRAINT PK_faculty PRIMARY KEY (faculty_id)
> > > > ) ENGINE=INNODB;
> > > >
> > > > CREATE TABLE student_faculty (
> > > > student_id INTEGER NOT NULL,
> > > > faculty_id INTEGER NOT NULL,
> > > > CONSTRAINT PK_student_faculty PRIMARY KEY (student_id, faculty_id)
> > > > ) ENGINE=INNODB;
> > > >
> > > > ALTER TABLE student_faculty ADD CONSTRAINT student_student_faculty
> > > > FOREIGN KEY (student_id) REFERENCES student (student_id);
> > > >
> > > > ALTER TABLE student_faculty ADD CONSTRAINT faculty_student_faculty
> > > > FOREIGN KEY (faculty_id) REFERENCES faculty (faculty_id);
> > > >
> > > > insert into student (student_name) values ('John');
> > > > insert into student (student_name) values ('Robert');
> > > >
> > > > insert into faculty (faculty_name) values ('Information Technology');
> > > > insert into faculty (faculty_name) values ('Engineering');
> > > >
> > > > Now there are student_id 1 and 2; same goes for faculty_id
> > > > so this statement should be ok:
> > > > insert into student_faculty (student_id, faculty_id) values (1,1);
> > > > However, there is no student_id or faculty_id greater than 2 yet, but
> > > > this statement still executes from mysql:
> > > > insert into student_faculty (student_id, faculty_id) values (3,1);
> > > > whereby it shouldn't since it does not inforce integrity based on the
> > > > foreign keys i have created!


Similar ThreadsPosted
Foreign key problem January 10, 2006, 8:11 am
Foreign key on the same table update problem November 15, 2006, 9:25 am
problem with foreign keys to datetime field September 15, 2005, 3:50 am
FOREIGN KEY March 14, 2006, 11:18 am
Why Should I Create A Foreign Key? November 2, 2005, 2:45 pm
how do you reference a foreign key May 29, 2006, 3:50 pm
INSERT ID from foreign key table September 5, 2005, 12:28 am
creating foreign fields January 22, 2006, 12:25 pm
Foreign Keys Don't Work? April 18, 2006, 6:29 am
How do you drop a foreign key constraint? June 30, 2006, 12:07 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap