I have a table which looks like this:
CREATE TABLE `library_mix` (
`lib_mix_id` VARCHAR(64) NOT NULL ,
`date` DATE NOT NULL ,
`index1` VARCHAR(64) NULL ,
`index2` VARCHAR(64) NULL ,
`index3` VARCHAR(64) NULL ,
`index4` VARCHAR(64) NULL ,
`index5` VARCHAR(64) NULL ,
...
`index96` VARCHAR(64) NULL ,
...
)
For each of index1 through index96, I want to add the same foreign key constraint. Ideally, I’d like to create both the indices and the fk constraint in a programmatic loop, but I’m unclear on how to do that in MySQL, and haven’t found any good examples. Something like:
CREATE TABLE `library_mix` (
`lib_mix_id` VARCHAR(64) NOT NULL ,
`date` DATE NOT NULL ,
for i = 1 to 96
`index`$i VARCHAR(64) NULL,
end
...
for i = 1 to 96
CONSTRAINT `fk_library_mix_lib_id`$i
FOREIGN KEY (`lib_id` )
REFERENCES `library` (`lib_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
end)
Is there an easy way to do this instead of typing everything out 96 times?
EDIT
My answer to some of the comments below was too long, so I’m adding it here.
- The term “library” above refers to a physical collection of DNA fragments (in a test tube) (I think library is a horrible term for this, but I didn’t make it up and am stuck with it).
- Each library has barcode attached to each fragment (also made from DNA), and is mixed with other DNA libraries (as long as those libraries have different barcodes)
- There are 96 possible barcodes (here identified by “index1” through “index96”).
So in this table, I’m attempting to model particular mixes of DNA libraries. The foreign key constraints are to make sure the library id’s exist in the library table.
Additional information that might be useful:
- different DNA libraries can have the same barcode (and therefore can’t be mixed together)
- a DNA library can be in more than one mix
- a single DNA sample can have multiple DNA libraries (with the same or different barcodes)
I’d be very happy to hear better ways to model this than with one-column-per-barcode.
Thanks!
Kevin
EDIT 2
The suggestion by @eggyal in the comments is the right way to deal with this. (Newbie mistake on my part.) Thanks!
I am going to answer your question but it seems that you have a many to many relationship between library and you are not addressing it properly. The comments by eggyal and Thomas are very valid. Please look into how to create a proper one to many and many to one relationship in your database. It would make this problem a lot easier.
To create the constraints easily you would open a connection to the MySQL server and execute the following:
You can copy and paste the results into a new query window and execute. I have not tested the syntax that is produced, however you should with minimal effort be able to modify the query to create the proper SQL command strings. I leave this as a exercise for you as it will definitely enhance your knowledge of the MySQL data dictionary. Basically go read about the INFORMATION_SCHEMA views in MySQL they make things like this a no brainer.