Jump to content

Linking tables?


sser

Recommended Posts

Hi group,

 

My MySQL server version is 6.

Basically, I've created a database with multiple tables and I'm trying to link/cross-reference some of the tables based on a specific index. All tables will have an ID associated with them and these IDs will be used to link them. Is it possible to achieve this?

 

Thanks!

Link to comment
Share on other sites

I'm pretty much stuck in the assigning ids to tables stages. So far, I have:

 

//I have all the files pushed to an array called $all_file

for($i=0; $i<count($all_file); $i++){

  $test_table = basename($all_file[$i], ".dat");

  $newstring = substr($test_table, 6, 9);

echo $newstring;

  $query = "CREATE TABLE `$test_table` (field1 INT, field2 CHAR(20) NOT NULL, field3 CHAR(50) NOT NULL)";

  mysql_query($query);

 

//each table needs to have a unique identifier based on the filename

  alter table '$test_table' add primary key ($newstring);

  $loadsql = "LOAD DATA LOCAL INFILE '$all_file[$i]' INTO table `$test_table` FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\n' ";

  mysql_query($loadsql);

}

 

I would like all tables to be associated with a key based on the filename of their associated files. But, of course I can't get the code working with the alter table function.

Any help would be greatly appreciated.

Link to comment
Share on other sites

As an example, I have tables such that:

a001

a002

b001

b003

b004

etc..

 

I'm trying to link tables that have similar last 3 digits, in this particular case  cross-reference a001 and b001.

I'm not sure if this is possible to make in php?

Link to comment
Share on other sites

I think I'm starting to get an idea of your business logic but I think you've chosen the wrong db logic to support it. I don't think I've ever seen table names used like that. I've seen people use a third table to help handle a many to many relationship scenario. Maybe that is where you are wanting to go with this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.