MYSQL : Instead of generating unique number at coding level what is an alternate way?
Hi EE,
I have following code in VB.NET.
Public Function GenerateUniNo(ByVal Length As Integer) As String Randomize() Dim ValidChars As String ValidChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" Dim i, x As Integer Dim password As String = "" For i = 1 To Length x = Int(Len(ValidChars) * Rnd() + 1) password = password & Mid(ValidChars, x, 1) Next GenerateUniNo = password End Function
Dim dr = dbCommE.ExecuteReader() If dr.HasRows Then txtUniNo.Text = GenerateUniNo (4) If dr(0) = Trim(txtUniNo.Text) Then txtUniNo.Text = "" txtUniNo.Text = GenerateUniNo (4) End If End If
Look at UUID(). This is a MySQL function that generates a GUID - which is guaranteed unique and the standard means by which we uniquely identify rows. You can use it in an INSERT statement or a SELECT
Example
@ Ryan Chong :
While I call procedure as below :
CALL insert_randomKey();
I get an error "Error Code: 1364. Field 'nID' doesn't have a default value"
Here, 'nID' is not Auto Increment field. The field is populated at the time of generating the new record (incremented by 1).
Actually, I need a functionality like this :
User call the procedure 'insert_randomKey()' on clicking the button.
system will auto add 100 blank records with the ID and randomkey and all the other fields will be blank (or default value).
BTW your solution will work for me.
Ryan Chong
'nID' is not Auto Increment field. The field is populated at the time of generating the new record (incremented by 1).
so, how you generate that nID at the first place? via C# code or backend database script? must it follow a sequence, etc?
Can u give me ur input data and expected output.