473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

reflexive foreign keys

Hi!

I have a question how to solve the folloing task:

I have 2 tables referencing each over: An user table and an address
table. Each user has one or no address. If an user is dropped the
address should be dropped, too. If an address is dropped its reference
in the user table should be set to null. I wrote the following create
statements:

CREATE TABLE user (
id int(10) unsigned NOT NULL auto_increment,
username varchar(255) NOT NULL default '',
password varchar(255) binary NOT NULL default '',
address int(10) unsigned default NULL,
PRIMARY KEY (id),
UNIQUE KEY username (username),
KEY address (address),
CONSTRAINT 0_1301
FOREIGN KEY (address)
REFERENCES address (id)
ON DELETE SET NULL
ON UPDATE CASCADE
) TYPE=InnoDB;
CREATE TABLE address (
id int(10) unsigned NOT NULL auto_increment,
lastname varchar(255) default NULL,
firstname varchar(255) default NULL,
[...]
PRIMARY KEY (id),
CONSTRAINT 0_1303
FOREIGN KEY (id)
REFERENCES user (address)
ON DELETE CASCADE
ON UPDATE CASCADE
) TYPE=InnoDB;
Now I insert an user:

INSERT INTO user (id, username, password, address)
VALUES (1,'username','password', NULL);

Later I want to assign an address to the user, but this is impossible,
because if insert the address first it fails, because its id is not in
user.address and if i first update user.address it fails because the
address is still not inserted.

How can I solve this without setting foreign_key_checks=0? If there is
no possibility, how can I be sure, that all foreign keys are correct
after switching back to foreign_key_checks=1?

thanks for help,
rainer
Jul 19 '05 #1
0 1817

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

Similar topics

5
by: Olivier Crèvecoeur | last post by:
Hello, Excuse me for my poor english. I would kike know if create index on the foreign key it's necessary or if Oracle, are optimized for using foreign key whithout index. Best regards ...
0
by: Rainer Collet | last post by:
Hi! I have a question how to solve the folloing task: I have 2 tables referencing each over: An user table and an address table. Each user has one or no address. If an user is dropped the...
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...
1
by: Vinodh Kumar P | last post by:
I understand the number of foreign keys allowed is restricted by the DBMS I use. In a general relational schema design perspective how many foreign keys a table shall have? If I have large number...
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...
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...
9
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for...
1
by: rbarber | last post by:
I have to synchronize 2 databases hourly but am having difficulty maintaining foreign key relations. These tables use auto-increment columns as primary keys, with child records in other tables...
1
by: apax999 | last post by:
Kinda new to SQL, using SQL Server 2005. I have some foreign keys in a couple of tables. I need to drop these tables, but can't since I'll get the error: Msg 3726, Level 16, State 1, Line...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.