SitePoint Sponsor |
|
User Tag List
Results 1 to 2 of 2
Thread: MySQL and constraints
-
Apr 23, 2001, 00:41 #1
- Join Date
- Mar 2001
- Location
- the Netherlands
- Posts
- 519
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm currently working on a MySQL database in phpMyAdmin.
I would like to add some kind of cascade-functionality.
Could someone help me with coding for the following problem?
I have two tables:
'Address' and 'User'. I have these two apart because a User can have more than one Address. The two tables both have the Key 'UserID'... to link them together.
Because MySQL doesn't have a standard foreign-key function.. could someone help me with would I should do to link these two tables together.
All help will be appreciated!
Jazz
-
Apr 23, 2001, 04:33 #2
- Join Date
- Jun 2000
- Location
- Sydney, Australia
- Posts
- 3,798
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You have to maintain the relationship yourself. So say you wanted to delete a user from table User. You would have to write two sql statements; one to delete the user from User and one two delete the related records in Address. So you could do something in PHP like this: (assumes that there is a db connection already established)
PHP Code:
function deleteUser($id) {
$global $dbconnection;
if(!$dbconnection) {
return 0; // return false
}
$sql = "DELETE FROM User WHERE UserID = $id";
$result = mysql_query($sql);
if(!$result) {
return 0; // return false
}
$sql = "DELETE FROM Address WHERE UserID=$id";
$result = mysql_query($sql);
return $result;
}
Bookmarks