Subject | Re: Dropdown list for variable domain |
---|---|
Author | Adam |
Post date | 2005-04-09T09:35:23Z |
Hi Rob,
What you seem to be describing is the perfect candidate for database
normalisation.
If I understand your problem right, you have something like this:
TableA
(
ID,
Name,
Address,
Obstruction
)
Now you are running into trouble because you have defined Obstruction
as a varchar but you want to ensure that only particular values are
accepted, like "Tower" and "Building", but if someone enters "Henry",
you don't want that to go through.
The best way to do this is to define a table with an exhaustive list
of valid obstructions, and have your table(s) link to this via a
foreign key:
Obstructions
(
ID,
Name
)
TableA
(
ID,
Name,
Address,
ObstructionsID
)
There are several problems with having it as a varchar. Firstly it
normally takes up more room to store. Secondly, you risk dirty data
slipping in if there is a bug in your user interface.
Your dropdown box source is now pretty simple.
Select ID, Name
from Obstructions
order by Name
When you declare the foreign key, you can define what you want to
happen if an obstruction is deleted. For example, if you delete
"Tower", do you want all the records in TableA which have the
obstruction "Tower" to be deleted, or do you want the Obstruction set
to null, or do you want the delete to fail.
I hope that helps.
Adam.
What you seem to be describing is the perfect candidate for database
normalisation.
If I understand your problem right, you have something like this:
TableA
(
ID,
Name,
Address,
Obstruction
)
Now you are running into trouble because you have defined Obstruction
as a varchar but you want to ensure that only particular values are
accepted, like "Tower" and "Building", but if someone enters "Henry",
you don't want that to go through.
The best way to do this is to define a table with an exhaustive list
of valid obstructions, and have your table(s) link to this via a
foreign key:
Obstructions
(
ID,
Name
)
TableA
(
ID,
Name,
Address,
ObstructionsID
)
There are several problems with having it as a varchar. Firstly it
normally takes up more room to store. Secondly, you risk dirty data
slipping in if there is a bug in your user interface.
Your dropdown box source is now pretty simple.
Select ID, Name
from Obstructions
order by Name
When you declare the foreign key, you can define what you want to
happen if an obstruction is deleted. For example, if you delete
"Tower", do you want all the records in TableA which have the
obstruction "Tower" to be deleted, or do you want the Obstruction set
to null, or do you want the delete to fail.
I hope that helps.
Adam.