Вопрос

After reading through possible options here on Stackoverflow, I don't see an answer for the exact case I'm considering...

CASE: I have a php system that auto-generates mysql-connected templates.

In order for the system to work, I need each generated template to have a unique 6-digit integer assigned upon it's creation. Instead of messing with randomizers, etc...

A) could I simply create a table in the database called 'pageid', with int(11) / unique / autoincrement with starting value 100000, and then during the template creation process, simply perform an insert on this table, and then a 'select last_insert_id()' to pull the last value, which is then injected into the template as it's created? (in otherwords, the table's only function is to generate and spit back the last unique id).

B) if so, are there any issues I should be concerned about using this method? (aside from the fact that it means a limit of only 899,999 templates can be generated - which is not an issue in this case).

Any thoughts or better methods appreciated...

Это было полезно?

Решение

  1. could I simply create a table in the database called 'pageid', with int(11) / unique / autoincrement with starting value 100000, and then during the template creation process, simply perform an insert on this table, and then a 'select last_insert_id()' to pull the last value, which is then injected into the template as it's created? (in otherwords, the table's only function is to generate and spit back the last unique id).

    Yes:

    CREATE TABLE pages (
      pageid MEDIUMINT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY
    ) AUTO_INCREMENT = 100000;
    

    Note that I am using MEDIUMINT(6) as it requires 1 byte less storage than INT and has a more appropriate display width for your needs.

  2. if so, are there any issues I should be concerned about using this method? (aside from the fact that it means a limit of only 899,999 templates can be generated - which is not an issue in this case).

    You could instead just have that column in your templates table, and have the id automatically assigned whenever a new record is inserted.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top