RSS Forums RSS
Please support our MySQL advertiser: Programming Forums
Views: 20006 | Replies: 6
Reply
Join Date: Oct 2004
Location: San Francisco, CA
Posts: 338
Reputation: paradox814 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

generate unique primary key

  #1  
Jun 6th, 2005
i want to generate a unique primary key, something similar to the auto increment feature, but i do not want it to be auto increment.

The reasoning for this is: this table will have rows deleted by the hundreds every day, and maybe even thousands in a few years (I hope!), All the while having more rows replace the ones that were erased. Is there a way to have mysql create a unique key that is just randomly generated?

Thanks in advance!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Kansas City, Missouri, USA
Posts: 345
Reputation: Troy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: generate unique primary key

  #2  
Jun 9th, 2005
Just use the auto number anyway. It doesn't matter if you delete thousands of records. Gaps in the number sequence are not a problem. It's still a unique id.
Reply With Quote  
Join Date: Oct 2004
Location: San Francisco, CA
Posts: 338
Reputation: paradox814 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: generate unique primary key

  #3  
Jun 10th, 2005
Originally Posted by Troy
Just use the auto number anyway. It doesn't matter if you delete thousands of records. Gaps in the number sequence are not a problem. It's still a unique id.

is there a way to INSERT AND SELECT unique_id at the same time? or does it have to be done as two seperate queries
Reply With Quote  
Join Date: Jun 2005
Location: Kansas City, Missouri, USA
Posts: 345
Reputation: Troy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: generate unique primary key

  #4  
Jun 10th, 2005
The most common scenario is you insert into a table, then you immediately need that new ID to insert into a child table. If this is your case, and you only have one secondary insert (or update) to make, then you can just perform your two queries like so:

MySQL example from mysql.com (in the second insert, the ID is inserted in another table):
INSERT INTO foo (auto,text)VALUES(NULL,'text');
INSERT INTO foo2 (id,text)VALUES(LAST_INSERT_ID(),'text');

If you need that id for other purposes, then you have two choices. You can select it out using a query to SELECT LAST_INSERT_ID(), or you can use PHP's function:
[php]
mysql_query("INSERT INTO mytable (product) values ('SnippetEdit')");
$last_id = mysql_insert_id();
[/php]
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote  
Join Date: Jun 2005
Location: no idea
Posts: 63
Reputation: trafficman is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
trafficman's Avatar
trafficman trafficman is offline Offline
Junior Poster in Training

Re: generate unique primary key

  #5  
Jun 13th, 2005
With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables.

For example:

INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
Reply With Quote  
Join Date: Jun 2005
Location: Texas
Posts: 120
Reputation: jwshepherd is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
jwshepherd's Avatar
jwshepherd jwshepherd is offline Offline
Junior Poster

Re: generate unique primary key

  #6  
Jun 16th, 2005
you may also manage the uniqueness from the application.
set the field as char and write your own unique id so that you will know what it is every time before and after you submit.
Reply With Quote  
Join Date: Jun 2005
Location: Kansas City, Missouri, USA
Posts: 345
Reputation: Troy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: generate unique primary key

  #7  
Jun 16th, 2005
All the answers posted are correct, but I think you are still better off to allow MySql's auto-number feature to manage your unique id key. Make sure you set the id field type to BIGINT.

Like I said before, gaps in the sequence do not matter. Auto-number's purpose is not to maintain a nice sequence of id's but rather to maintain a unique id.

As mentioned by another poster, you can always generate your own unique id within your code, and use that when inserting new records, but that increases the chance of hurting data integrity. Especially if you have a large application with multiple places in the code the table is inserted into. Database systems started implementing methods to manage unique ID's and relational integrity because it's too easy for us programmers to screw it up if we try to manage it in our code!
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:42 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC