472,986 Members | 2,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

FOREIGN KEY

Given the following table:
CREATE table1 (a INTEGER PRIMARY KEY) TYPE=INNODB;

It is possible to create a relation between table1 i table2 using the
following syntax:
CREATE table2 (a INTEGER PRIMARY KEY, b INTEGER, c INTEGER, FOREIGN
KEY(b) REFERENCES table1(a)) TYPE=INNODB;

How can I make that relation within the column definition? This won't
work:
CREATE table2 (a INTEGER PRIMARY KEY, b INTEGER REFERENCES table1(a), c
INTEGER) TYPE=INNODB;
Sincerly,
Zeljko

Mar 14 '06 #1
5 1325
ze***********@gmail.com wrote:
Given the following table:
CREATE table1 (a INTEGER PRIMARY KEY) TYPE=INNODB; It is possible to create a relation between table1 i table2 using the
following syntax:
CREATE table2 (a INTEGER PRIMARY KEY, b INTEGER, c INTEGER, FOREIGN
KEY(b) REFERENCES table1(a)) TYPE=INNODB; How can I make that relation within the column definition? This won't
work:
CREATE table2 (a INTEGER PRIMARY KEY, b INTEGER REFERENCES table1(a), c
INTEGER) TYPE=INNODB;

http://dev.mysql.com/doc/refman/4.1/en/alter-table.html

CREATE table2 (a INTEGER PRIMARY KEY, b INTEGER, c INTEGER) TYPE=INNODB;
alter table table2 add foreign key fk_table1_key (b) references table1(a);

Sincerly,
Zeljko

Mar 14 '06 #2

noone wrote:
http://dev.mysql.com/doc/refman/4.1/en/alter-table.html

CREATE table2 (a INTEGER PRIMARY KEY, b INTEGER, c INTEGER) TYPE=INNODB;
alter table table2 add foreign key fk_table1_key (b) references table1(a);


Thank you for fast reply.
I am currently using the ALTER TABLE statement, but I wanted somehow to
eliminate it and make reference within table definition.

According to http://dev.mysql.com/doc/refman/5.0/...ate-table.html I
shoud acomplish that with the following query:

CREATE TABLE table2 (a INTEGER PRIMARY KEY, b INTEGER REFERENCES
table1(a), c INTEGER) TYPE=INNODB;

The previous query executes, but no relations are created.

(CREATE TABLE tbl_name (col_name type reference_definition))
Zeljko

Mar 14 '06 #3
It seems there is a bug in MySQL: http://bugs.mysql.com/bug.php?id=13301

Mar 14 '06 #4
ze***********@gmail.com wrote:

noone wrote:
http://dev.mysql.com/doc/refman/4.1/en/alter-table.html

CREATE table2 (a INTEGER PRIMARY KEY, b INTEGER, c INTEGER) TYPE=INNODB;
alter table table2 add foreign key fk_table1_key (b) references table1(a);
Thank you for fast reply.
I am currently using the ALTER TABLE statement, but I wanted somehow to
eliminate it and make reference within table definition. According to http://dev.mysql.com/doc/refman/5.0/...ate-table.html I
shoud acomplish that with the following query:


According the docs the syntax is (not tested):

CREATE TABLE table2 (a INTEGER PRIMARY KEY, b INTEGER constraint
[constr-name] foreign key REFERENCES table1(a), c INTEGER) TYPE=INNODB;

Mar 14 '06 #5
noone wrote:
According the docs the syntax is (not tested):

CREATE TABLE table2 (a INTEGER PRIMARY KEY, b INTEGER constraint
[constr-name] foreign key REFERENCES table1(a), c INTEGER) TYPE=INNODB;


It breaks on both of the following queries (with or without CONSTRAINT
keyword):

CREATE TABLE table2 (a INTEGER PRIMARY KEY, b INTEGER constraint
whatever foreign key REFERENCES table1(a), c INTEGER) TYPE=INNODB;

CREATE TABLE table2 (a INTEGER PRIMARY KEY, b INTEGER foreign key
REFERENCES table1(a), c INTEGER) TYPE=INNODB;
Never mind, I'll put FOREIGN KEY contstraints in table definition,
although I wanted them in the column definition. As I understand, there
is a bug in MySQL, as stated in my previous post. Thanks anyway.
Zeljko

Mar 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: dcp | last post by:
I just installed the 4.1.0-alpha-max-nt version of MySql and have just started playing around with it. My first test was to try to create a couple of tables, one with a foreign key constraint. ...
0
by: Jeremiah Jacks | last post by:
I just upgraded to MySQL 4.0.14-standard for RedHat Linux and am using = the pre-compiled binaries. I have a database with INNODB tables. When I insert a row into one of the child tables, I get...
2
by: geoff | last post by:
The table creation script(at the end of this post) works fine on 4.0.1-alpha-win, but the foreign key constraints fail on 4.0.15-win. I am starting the server with the same command for both...
10
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed...
31
by: Robert Brown | last post by:
Let's say I have a type hierarchy: (just an example) the general entity customer: CREATE TABLE customer(customer_id int, customer_name varchar(250), customer_type int) three specific...
4
by: Simon Bond | last post by:
Hi there, I should point out I am very new to MySql, im trying to learn what I can. Basically im using Navicat to work with my databases. When making a foreign key, i am asked to enter the...
2
by: adammitchell | last post by:
How can you indicate that a FOREIGN KEY constraint references two columns in two different tables? "SQL Server Books Online" show an example of how to reference two columns in the SAME table:...
0
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A...
10
by: Shawn Chisholm | last post by:
Hi, I am trying to deal with a deadlock situation caused by foreign key references on insert and I was wondering if anyone knows what order the foreign keys are locked (or evaluated) in for a...
2
by: Ian Davies | last post by:
I have created a database with about 17 tables. I have been creating foreign keys some of which have worked but when creating others I get the message below ************************* 1005...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.