Foreign Key Problem

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!
coosa [ Mi, 18 Oktober 2006 19:38 ] [ ID #1504802 ]

Re: Foreign Key Problem

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!
coosa [ Mi, 18 Oktober 2006 23:12 ] [ ID #1504806 ]

Re: Foreign Key Problem

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!
peterloh [ Do, 19 Oktober 2006 07:39 ] [ ID #1506234 ]

Re: Foreign Key Problem

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!
coosa [ Do, 19 Oktober 2006 11:50 ] [ ID #1506237 ]

Re: Foreign Key Problem

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!
peterloh [ Fr, 20 Oktober 2006 03:14 ] [ ID #1507637 ]

Re: Foreign Key Problem

I also can't find it since i use xampp

Peter wrote:
> 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!
coosa [ So, 22 Oktober 2006 06:18 ] [ ID #1509258 ]
Datenbanken » mailing.database.mysql » Foreign Key Problem

Vorheriges Thema: setting a collumn to be unique in existing table
Nächstes Thema: Table join or union?