Problem Solution Discussion Generating Unique Table Names

t able t hat does not have quit e t he sam e st ruct ure.

3.27 Generating Unique Table Names

3.27.1 Problem

You need t o creat e a t able wit h a nam e t hat is guarant eed not t o exist already.

3.27.2 Solution

I f you can creat e a TEMPORARY t able, it doesnt m at t er if t he nam e exist s already. Ot herwise, t ry t o generat e a value t hat is unique t o your client program and incorporat e it int o t he t able nam e.

3.27.3 Discussion

MySQL is a m ult iple- client dat abase server, so if a given script t hat creat es a t ransient t able m ight be invoked by several client s sim ult aneously, you m ust t ake care t o keep m ult iple invocat ions of t he script from fight ing over t he sam e t able nam e. I f t he script creat es t ables using CREATE TEMPORARY TABLE , t here is no problem because different client s can creat e t em porary t ables having t he sam e nam e wit hout clashing. I f you cant use CREATE TEMPORARY TABLE because t he server version is older t han 3.23.2, you should m ake sure t hat each invocat ion of t he script creat es a uniquely nam ed t able. To do t his, incorporat e int o t he nam e som e value t hat is guarant eed t o be unique per invocat ion. A t im est am p wont work, because it s easily possible for t wo inst ances of a script t o be invoked wit hin t he sam e second. A random num ber m ay be som ewhat bet t er. For exam ple, in Java, you can use t he java.util.Random class t o creat e a t able nam e like t his: import java.util.Random; import java.lang.Math; Random rand = new Random ; int n = rand.nextInt ; generate random number n = Math.abs n; take absolute value String tblName = tmp_tbl_ + n; Unfort unat ely, random num bers only reduce t he possibilit y of nam e clashes, t hey do not elim inat e it . Process I D PI D values are a bet t er source of unique values. PI Ds are reused over t im e, but never for t wo processes at t he sam e t im e, so a given PI D is guarant eed t o be unique am ong t he set of current ly execut ing processes. You can use t his fact t o creat e unique t able nam es as follows: Perl: my tbl_name = tmp_tbl_; PHP: tbl_name = tmp_tbl_ . posix_getpid ; Pyt hon: import os tbl_name = tmp_tbl_d os.getpid Not e t hat even if you creat e a t able nam e using a value like a PI D t hat is guarant eed t o be unique t o a given script invocat ion, t here m ay st ill be a chance t hat t he t able will exist . This can happen if a previous invocat ion of t he script wit h t he sam e PI D creat ed a t able wit h t he sam e nam e, but crashed before rem oving t he t able. On t he ot her hand, any such t able cannot st ill be in use because it will have been creat ed by a process t hat is no longer running. Under t hese circum st ances, it s safe t o rem ove t he t able if it does exist by issuing t he following st at em ent : DROP TABLE IF EXISTS tbl_name Then you can go ahead and creat e t he new t able.

Chapter 4. Working with Strings

Sect ion 4.1. I nt roduct ion Sect ion 4.2. Writ ing St rings That I nclude Quot es or Special Charact er s Sect ion 4.3. Preserving Trailing Spaces in St ring Colum ns Sect ion 4.4. Test ing St ring Equalit y or Relat ive Ordering Sect ion 4.5. Decom posing or Com bining St rings Sect ion 4.6. Checking Whet her a St ring Cont ains a Subst ring Sect ion 4.7. Pat t ern Mat ching wit h SQL Pat t erns Sect ion 4.8. Pat t ern Mat ching wit h Regular Expressions Sect ion 4.9. Mat ching Pat t ern Met acharact ers Lit erally Sect ion 4.10. Cont rolling Case Sensit ivit y in St ring Com parisons Sect ion 4.11. Cont rolling Case Sensit ivit y in Pat t ern Mat ching Sect ion 4.12. Using FULLTEXT Searches Sect ion 4.13. Using a FULLTEXT Search wit h Short Words Sect ion 4.14. Requiring or Excluding FULLTEXT Search Words Sect ion 4.15. Perform ing Phrase Searches wit h a FULLTEXT I ndex