mySQL - How to autogenerate field values
I have a table that contains thousands of URLs. Each URL will have a corresponding folder, so I was considering using mySQL to create a short and usable folder name for each existing URL and for new URLs as they are added to the table.
What I would like is to specify something like a field 5 chars lon开发者_如何转开发g of chars a-z, and have mySQL autogenerate values for that field.
Can the mySQL engine do this natively, or would I have to do this in code?
Just use an auto-generated long/bigint ID and convert it after all from/to base 36 or 64.
Depending on whether you want to be able to easily determine the URL the folder name is associated with, you might find that using a UUID will provide you with a suitable way.
INSERT INTO t (`url`, `uuid`) VALUES('http://www.example.com/', UUID());
Other mechanisms along the same line include using MD5() and SHA1()
http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_uuid http://dev.mysql.com/doc/refman/5.0/en/other-functions.html
Note that these are going to be longer than 5 chars that you mentioned, though it's possible you'll need more than that to allow sufficient space.
精彩评论