Unix Technical Forum

mysql - foreign keys

This is a discussion on mysql - foreign keys within the MySQL forums, part of the Database Server Software category; --> Hello I have a problem with creating a foreign key in table members: I create a foreign key for ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-20-2008, 12:47 PM
123betty@gmail.com
 
Posts: n/a
Default mysql - foreign keys

Hello

I have a problem with creating a foreign key in table members:

I create a foreign key for userlevel(members) to level(levels)
I put in table levels two values: 0 and 9.
When I fill the table members, and try to give value 5 for userlevel,
I don't get an error (isn't this a foreign key)?

Following is my script.sql, can anyone please have a look at it what I
am doing wrong, and explain me what was wrong?

Thank you in advance, Betty

DROP TABLE IF EXISTS members;

CREATE TABLE members (
id INT(4) NOT NULL AUTO_INCREMENT,
userlevel TINYINT(1) UNSIGNED NOT NULL,
username VARCHAR(65) NOT NULL,
password VARCHAR(65) NOT NULL,
PRIMARY KEY (id)
);


DROP TABLE IF EXISTS levels;

CREATE TABLE levels (
level TINYINT(1) UNSIGNED NOT NULL PRIMARY KEY
);


ALTER TABLE members
ADD FOREIGN KEY(userlevel) REFERENCES levels(level);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-20-2008, 12:47 PM
Kees Nuyt
 
Posts: n/a
Default Re: mysql - foreign keys

On Mon, 17 Mar 2008 11:29:18 -0700 (PDT),
123betty@gmail.com wrote:

>Hello
>
>I have a problem with creating a foreign key in table members:
>
>I create a foreign key for userlevel(members) to level(levels)
>I put in table levels two values: 0 and 9.
>When I fill the table members, and try to give value 5 for userlevel,
>I don't get an error (isn't this a foreign key)?
>
>Following is my script.sql, can anyone please have a look at it what I
>am doing wrong, and explain me what was wrong?
>
>Thank you in advance, Betty
>


For foreign keys (REFERENCES CONSTRAINT) you have to
use InnoDB. For MyISAM tables, the syntax is accepted,
but the referential integrity is not enforced.

DROP TABLE IF EXISTS levels;

CREATE TABLE levels (
level TINYINT(1) UNSIGNED NOT NULL PRIMARY KEY
);

DROP TABLE IF EXISTS members;

CREATE TABLE members (
id INT(4) NOT NULL AUTO_INCREMENT,
userlevel TINYINT(1) UNSIGNED
NOT NULL
REFERENCES levels(level),
username VARCHAR(65) NOT NULL,
password VARCHAR(65) NOT NULL,
PRIMARY KEY (id)
) ENGINE InnoDB;
--
( Kees
)
c[_] The fecal material has hit the air circulating device. (#151)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-20-2008, 12:47 PM
123betty@gmail.com
 
Posts: n/a
Default Re: mysql - foreign keys

On Mar 17, 8:06 pm, Kees Nuyt <k.n...@nospam.demon.nl> wrote:
> On Mon, 17 Mar 2008 11:29:18 -0700 (PDT),
>
> 123be...@gmail.com wrote:
> >Hello

>
> >I have a problem with creating a foreign key in table members:

>
> >I create a foreign key for userlevel(members) to level(levels)
> >I put in table levels two values: 0 and 9.
> >When I fill the table members, and try to give value 5 for userlevel,
> >I don't get an error (isn't this a foreign key)?

>
> >Following is my script.sql, can anyone please have a look at it what I
> >am doing wrong, and explain me what was wrong?

>
> >Thank you in advance, Betty

>
> For foreign keys (REFERENCES CONSTRAINT) you have to
> use InnoDB. For MyISAM tables, the syntax is accepted,
> but the referential integrity is not enforced.
>
> DROP TABLE IF EXISTS levels;
>
> CREATE TABLE levels (
> level TINYINT(1) UNSIGNED NOT NULL PRIMARY KEY
> );
>
> DROP TABLE IF EXISTS members;
>
> CREATE TABLE members (
> id INT(4) NOT NULL AUTO_INCREMENT,
> userlevel TINYINT(1) UNSIGNED
> NOT NULL
> REFERENCES levels(level),
> username VARCHAR(65) NOT NULL,
> password VARCHAR(65) NOT NULL,
> PRIMARY KEY (id)
> ) ENGINE InnoDB;
> --
> ( Kees
> )
> c[_] The fecal material has hit the air circulating device. (#151)


Thank you for your help Kees.

But it's still not working (used innodb) correctly?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-20-2008, 12:47 PM
123betty@gmail.com
 
Posts: n/a
Default Re: mysql - foreign keys

On Mar 17, 8:37 pm, 123be...@gmail.com wrote:
> On Mar 17, 8:06 pm, Kees Nuyt <k.n...@nospam.demon.nl> wrote:
>
>
>
> > On Mon, 17 Mar 2008 11:29:18 -0700 (PDT),

>
> > 123be...@gmail.com wrote:
> > >Hello

>
> > >I have a problem with creating a foreign key in table members:

>
> > >I create a foreign key for userlevel(members) to level(levels)
> > >I put in table levels two values: 0 and 9.
> > >When I fill the table members, and try to give value 5 for userlevel,
> > >I don't get an error (isn't this a foreign key)?

>
> > >Following is my script.sql, can anyone please have a look at it what I
> > >am doing wrong, and explain me what was wrong?

>
> > >Thank you in advance, Betty

>
> > For foreign keys (REFERENCES CONSTRAINT) you have to
> > use InnoDB. For MyISAM tables, the syntax is accepted,
> > but the referential integrity is not enforced.

>
> > DROP TABLE IF EXISTS levels;

>
> > CREATE TABLE levels (
> > level TINYINT(1) UNSIGNED NOT NULL PRIMARY KEY
> > );

>
> > DROP TABLE IF EXISTS members;

>
> > CREATE TABLE members (
> > id INT(4) NOT NULL AUTO_INCREMENT,
> > userlevel TINYINT(1) UNSIGNED
> > NOT NULL
> > REFERENCES levels(level),
> > username VARCHAR(65) NOT NULL,
> > password VARCHAR(65) NOT NULL,
> > PRIMARY KEY (id)
> > ) ENGINE InnoDB;
> > --
> > ( Kees
> > )
> > c[_] The fecal material has hit the air circulating device. (#151)

>
> Thank you for your help Kees.
>
> But it's still not working (used innodb) correctly?


Got it!

Your INNODB-suggestion helped me a lot.

It was necessary to create an index on the foreign key + both tables
had to be innodb.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:27 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com