Gossamer Forum
Home : General : Databases and SQL :

Restricting values according to another table

Quote Reply
Restricting values according to another table
Hello.

I was wondering if it is possible in MySQL to restrict the values you can enter into a field, according to the contents of another table. I know you can use ENUM('value', 'value', 'value') to do this normally, but I would like to make it look at the values in a column in a different table to determine what those acceptable values are. Is this possible?

Thanks in advance,
--
Dave.
Quote Reply
Re: [gingercardwell] Restricting values according to another table In reply to
Yea,you can. All you'd have to do is run a query of that other table in your code, like the following:

SELECT tbl.PrimaryKeyColumn
FROM tbl
ORDER BY tbl.ColumnName

Then you should insert the PrimaryKeyColumn value in the other table.

Example:

TABLE1:

PK
ColumnName

TABLE2:

PK
FKColumnName (TABLE1.PK)
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Stealth] Restricting values according to another table In reply to
Yeah, if foreign keys are available.
By default, foreign keys are not available e.g. in MySQL, yet.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Restricting values according to another table In reply to
Uh...not through referential integrity or constraints, per se, but you can use FK constructs when inserting data into corresponding tables.
========================================
Buh Bye!

Cheers,
Me