0

I am wondering how I could, using PHP and mysql, create a table with a unique name every time.

So example if i click submit, a table will be created that is named "1". then if i do it again another table is added and it is named "2"

I searched around but could only find answers to how to auto_increment the columns inside the table so I hoped it would be the same code, I tried this:

mysql_query("CREATE TABLE INT NOT NULL AUTO_INCREMENT PRIMARY KEY(TestColumn CHAR(30))");

It did not work.

So how do you create an auto_incremented table ???

4
  • I'm not sure if that's even possible within MySQL. I guess you could potentially use CREATE TABLE IF NOT EXISTS, and if it fails, increment the number and try again. It goes without saying that this approach is sub-optimal. I don't know why you need this, but I'm sure there's a better way to do it. Have you attempted to create temporary tables??
    – ILikeTacos
    Oct 17, 2013 at 19:13
  • Put in in a loop, concatenate to some standard name the counter.
    – Mihai
    Oct 17, 2013 at 19:15
  • You could also parse the output of "show tables", fetch the highest "table name" and cast to integer, increment one and create new table.
    – ILikeTacos
    Oct 17, 2013 at 19:16
  • Are you sure you want to do this? Creating tables with meaningless names like 2 is something that infuriates people trying to work with your data.
    – tadman
    Oct 17, 2013 at 19:31

4 Answers 4

1

Create a simple file which store a serial number. Then when your script creates a table, you increment the counter in the file with one. Next time, you read the number and use that for the table name. Naturally, you could do this in a table or a flat file.

Just for knowing which tables exist, and what they are for, you'd best create one master table storing not just the latest, but all tables created.

I am lost as to why you would want to do this.. I see no good reason for wanting this.

2
  • Or have a table which holds info about the dynamically created tables?
    – Atle
    Oct 17, 2013 at 19:16
  • or create 10,000 tables and alter the structure as needed. I mean the possibilities are endless, but that system will be a hell to maintain.
    – ILikeTacos
    Oct 17, 2013 at 19:17
0

Create a table of tables and store the number or the number name in that table. Then you can look up MAX number there.

0

First at all, this function don't exist in PHP or in MySQL. Or maybe I don't know it.

There is 2 solutions to your problem :

Solution number 1 :

As AlanChavez said, you can use this request :

CREATE TABLE IF NOT EXIST ....

But, if you have to create 1000000000 table (it's an example), it will not be optimized.

Solution number 2 :

You can create a table with a single row, where is stored the last name used for your table. I don't know if it's really optimized, but I think it can work.

0

I will never recommend to name a database table just with a digit. To keep track of number of click / page-load you can use file, session or another table.

1
  • You can get largest table by this query : *SELECT MAX(TABLE_NAME)+1 FROM information_schema.TABLES T WHERE TABLE_SCHEMA = 'YOUR_DB_NAME'* And then add another table. Oct 17, 2013 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.