Unix Technical Forum

Foreign Keys - New tables - Help

This is a discussion on Foreign Keys - New tables - Help within the MySQL forums, part of the Database Server Software category; --> Hello, and a good day to you. I am new to MYSQL, and was wondering how I go about ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-02-2008, 01:34 PM
Bob De Mars
 
Posts: n/a
Default Foreign Keys - New tables - Help

Hello, and a good day to you. I am new to MYSQL, and was wondering how
I go about creating tables using foreign keys.

My question is, I create a table called carrier.

+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| carrier_id | int(4) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
+------------+-------------+------+-----+---------+----------------+

I would like to make another table called booking to include the carrier_id.

When I create the booing table, do I just specify FOREIGN KEY
(carrier_id), and use the same attributes
carrier_id int(4) not null?

Thanks,

Bob
--

A man without an answer
Is like a bird with broken wings
wrapped up in its misery
forgetting how to sing
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-02-2008, 01:34 PM
Paul Lautman
 
Posts: n/a
Default Re: Foreign Keys - New tables - Help

Bob De Mars wrote:
> Hello, and a good day to you. I am new to MYSQL, and was wondering
> how I go about creating tables using foreign keys.

When I am new to something, I read the manual. I find it saves me having to
wonder about things like this.

> When I create the booing table, do I just specify FOREIGN KEY
> (carrier_id), and use the same attributes
> carrier_id int(4) not null?

No, you have to follow the instructions in the manual. In general in
programming, you will find that it is not possible to just make up your own
syntax on a whim. You will find this section coinsidentally called FOREIGN
KEY Constraints useful:
http://dev.mysql.com/doc/refman/5.0/...nstraints.html

>
> Thanks,
>
> Bob

You're welcome.



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-02-2008, 01:34 PM
Rik Wasmus
 
Posts: n/a
Default Re: Foreign Keys - New tables - Help

On Fri, 30 May 2008 20:20:19 +0200, Bob De Mars <bob@grunners.com_nospam>
wrote:

> Hello, and a good day to you. I am new to MYSQL, and was wondering how
> I go about creating tables using foreign keys.
>
> My question is, I create a table called carrier.
>
> +------------+-------------+------+-----+---------+----------------+
> | Field | Type | Null | Key | Default | Extra |
> +------------+-------------+------+-----+---------+----------------+
> | carrier_id | int(4) | NO | PRI | NULL | auto_increment |
> | name | varchar(30) | NO | | NULL | |
> +------------+-------------+------+-----+---------+----------------+
>
> I would like to make another table called booking to include the
> carrier_id.
>
> When I create the booing table, do I just specify FOREIGN KEY
> (carrier_id), and use the same attributes
> carrier_id int(4) not null?


Read the manual regarding Foreign Keys, especially the syntax, and keep 2
important things in mind:
- Not all engines will support foreign keys, for instance MyISAM doesn't,
InnoDB does, however, when setting a Foreign Key constraint on a table
which' engine doesn't support it, MySQL will not complain, allthough your
constraint that just appeared to be set will do absolutely nothing.
- The columns in a constraint have to be of the exact same type, which
could include character set & collation, however, a column can be defined
as NULL while the column referenced is NOT NULL, creating the possiblity
to add record not bound by the constraint. Of course, only use that if
that's what you want.

And a warning: MySQL's error messages about failing settings constraints
are incredibly cryptic and almost never helpfull. Basically, if creating a
constraint fails check & recheck the columns you'd like to match, a SHOW
FULL COLUMNS FORM tablename can give you some extra insight in those.
--
Rik Wasmus
....spamrun finished
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 05:28 PM.


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