Take the following data set:
Users
(int) (varchar) (varchar) (varchar)
id | username | password | stuff |
1 john cat stuff
2 jack dog stuff
3 bill bird stuff
.
.
.
n userN lol stuff
and
Colors
(int) (varchar)
userid | color
1 green
2 green
3 yellow
. .
. .
. .
n purple
Now we obviously can see that the colors and users are linked by the userid. Our proper user deletion script goes and removes all the matching userids from the colors table and then deletes the user in the users table. Let's say we have a lazy admin who manually SSH's into the server and deletes a user from the users table. Let's assume that "john" was deleted.
We then have an issue when our color retrieval script runs (PHP for simplicity):
<?
$query = "SELECT * FROM colors WHERE color = 'green'";
$result = mysql_query($query);
while ($row = mysql_fetch_asoc($result)) {
//The code bricks here.
$query2 = "SELECT * FROM users WHERE id = " . $row['id']. " LIMIT 1";
$result2 = mysql_query($query2);
$row2 = mysql_fetch_asoc($result2);
echo $row2['username'] . "<br />"
}
?>
Is there a concept in MySQL itself that will not allow an admin to DELETE the entry in the users table if that userid is in the colors table. Pardon my ignorance, I am not a DBA.