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 ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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 |
| |||
| 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. |
| ||||
| 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 |