Jump to content

Custom keys


Edgewalker81

Recommended Posts

Is a way to make custom primary keys based on auto-increment?

Now I insert a row, then use mysql_insert_id() to get the number, alter it (adding a prefix such as "E-") and save it in another field.

 

Is there a way to simplify this?

 

I want the prefixes is so I can know what kind of object it is, and so which table it is in.  I can't just make one table because the items have no data fields in common.

 

I thought of just making a table with an autoincrement field and a field for which table the item belongs to, but I'm worried that if the number of items grows large, it would be quicker to have my PHP check the prefix and just do one query instead.

 

Any advice appreciated.

Link to comment
Share on other sites

My question is why are you looking at primary keys from different tables at the same time if it is a different set of data fields? If this is from an sql query, then just set an additional field for each table, example:

 

SELECT 'E' AS persontype, name, address FROM TABLE_employees UNION SELECT 'C',name,address FROM TABLE_clients

Link to comment
Share on other sites

My question is why are you looking at primary keys from different tables at the same time if it is a different set of data fields? If this is from an sql query, then just set an additional field for each table, example:

 

SELECT 'E' AS persontype, name, address FROM TABLE_employees UNION SELECT 'C',name,address FROM TABLE_clients

 

Then, you just do a check on what the "persontype" field is, if it's E, you handle it one way, if it's C, you handle it another.

Link to comment
Share on other sites

I just knew you'd ask  :)

I'd rather not, to be honest, but the project is for barcoding a large number of items, and I need to be able to answer the question "what is this thing?" It might be an order form, a service request, a CD, a bag of photos, or many other things.

 

They are all scanned into one field by the user, so the system has to be able to figure out what the item is so it can display the info relevant to that item type upon being scanned.

 

I'm thinking an table that indexes what each item is might be in order...

Link to comment
Share on other sites

If I was starting from scratch, I would have created one table for all the different items, and a category for each item, then in your php code, you can say that if it's in the order form category, then you need a field or order number, or total amount, or whatever your fields are, where as cd might have a title and description.

 

The other option would be to do the index file as you said, but I wouldn't create a seperate index, just use the indexes that already exist, but also include the tablename, so your data looks like this

 

[pre]

barcode        tablename        tableid

0000000001    cds              5

0000000002    orderforms      3

0000000003    servicerequest  4

[/pre]

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.