Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7994935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:15:42+00:00 2026-06-04T14:15:42+00:00

I have a table which looks like this: CREATE TABLE `library_mix` ( `lib_mix_id` VARCHAR(64)

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:15:43+00:00Added an answer on June 4, 2026 at 2:15 pm

    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:

    SELECT  CONCAT("CONSTRAINT `fk_library_mix_lib_id`",COLUMN_NAME,"
        FOREIGN KEY (`",COLUMN_NAME,"` )
        REFERENCES `library` (`lib_id` )
        ON DELETE NO ACTION
        ON UPDATE NO ACTION ") AS SQLStatement
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE Table_NAME ="library_mix"
    AND COLUMN_NAME LIKE "index%"
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database table that looks like this: CREATE TABLE IF NOT EXISTS
I have a PDO prepared statement which looks like this: $STH = $DBH->(CREATE TABLE
In MSSQL 2008 I have table and data which looks like this create table
I have a table that looks like this: Id (PK, int, not null) ReviewedBy
I have a legacy SQL schema which looks something like this: CREATE TABLE `User`
I have a table which looks like this: <table id='t'> <thead> .... </thead> <tbody
I have a table which looks like this one: | id | fk_book |
i have a html table which looks like this: <table> <thead> <tr> <th >title1</th>
I have a table which currently looks a bit like this- CASEID ¦ FORMNAME
I have this mysql table called comments which looks like this: commentID parentID type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.