Page 1 of 1
Unique id across multiple records
Posted: Thu Jun 13, 2013 6:04 pm
by Enrico Maria Giordano
Dear friends, I need to generate one unique id for a group of newly inserted records (ie. to mark a group of record with a unique id). But I need it for SQL where I can't use explicit locking to assure id uniqueness.
Any ideas?
EMG
Re: Unique id across multiple records
Posted: Thu Jun 13, 2013 7:16 pm
by Daniel Garcia-Gil
Hello
create a new table with group insert ids
Code: Select all
CREATE TABLE `groupid` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
start transaction
insert in groupid
Code: Select all
INSERT INTO groupid (id) VALUES(NULL)
retrieve last id
use the id to group insert
Code: Select all
INSERT INTO myTable (groupid, field1, field2) VALUES (@LASTID, 'VALUE1', 'VALUE2'), (@LASTID, 'VALUE3', 'VALUE4'), (@LASTID, 'VALUE5', 'VALUE6')
close transaction
Re: Unique id across multiple records
Posted: Thu Jun 13, 2013 8:15 pm
by Enrico Maria Giordano
Daniel,
Daniel Garcia-Gil wrote:Hello
create a new table with group insert ids
Code: Select all
CREATE TABLE `groupid` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
start transaction
insert in groupid
Code: Select all
INSERT INTO groupid (id) VALUES(NULL)
retrieve last id
use the id to group insert
Code: Select all
INSERT INTO myTable (groupid, field1, field2) VALUES (@LASTID, 'VALUE1', 'VALUE2'), (@LASTID, 'VALUE3', 'VALUE4'), (@LASTID, 'VALUE5', 'VALUE6')
close transaction
Thank you. What do you exactly mean with BEGIN and COMMIT? They aren't SQL statements, as far as I know.
EMG
Re: Unique id across multiple records
Posted: Thu Jun 13, 2013 8:24 pm
by Daniel Garcia-Gil
in Mysql you can start a transaction with "BEGIN", close transaction with "COMMIT" (save the changes) or ROLLBACK to not save changes
http://dev.mysql.com/doc/refman/5.0/en/commit.html
Re: Unique id across multiple records
Posted: Thu Jun 13, 2013 9:28 pm
by Enrico Maria Giordano
Daniel,
Sorry, I need an engine-aware solution.
EMG
Re: Unique id across multiple records
Posted: Thu Jun 13, 2013 9:51 pm
by Daniel Garcia-Gil
Enrico
is only idea, a general way to do
if you use ADO
look how use transaction with ADO
http://msdn.microsoft.com/en-us/library ... l.80).aspx
if you connect ADO + MySql internally ADO call BEGIN, COMMIT, ROLLBACK of MySql to generate a transaction
try to find a equivalent to my sample above
Re: Unique id across multiple records
Posted: Thu Jun 13, 2013 10:04 pm
by Enrico Maria Giordano
Daniel,
What do you think about this?
Code: Select all
<%
Function GetGuid()
Set TypeLib = CreateObject( "Scriptlet.TypeLib" )
GetGuid = Replace( Mid( TypeLib.Guid, 2, 36 ), "-", "" )
Set TypeLib = Nothing
End Function
Response.Write GetGuid()
%>
Is it going to be a reasonably valid unique id?
EMG
Re: Unique id across multiple records
Posted: Thu Jun 13, 2013 10:24 pm
by Daniel Garcia-Gil
Enrico
yes, i guess
Re: Unique id across multiple records
Posted: Thu Jun 13, 2013 11:15 pm
by Enrico Maria Giordano
Daniel,
Daniel Garcia-Gil wrote:Enrico
yes, i guess
Thank you.
EMG
Re: Unique id across multiple records
Posted: Sat Jun 15, 2013 2:25 pm
by Rick Lipkin
Enrico
Dear friends, I need to generate one unique id for a group of newly inserted records (ie. to mark a group of record with a unique id). But I need it for SQL where I can't use explicit locking to assure id uniqueness.
Any ideas?
EMG
Do not know if you have solved this yet .. I was thinking about the possibility of creating your group rows ( as you normally would ) and as you create them AAdd the primary row id's to an array. Create your unique value cGroupId := _GenEid() and then walk through a for\next loop of the array and find each row and Update() the rows with cGroupID.
Hope this is what you had in mind ?
Rick Lipkin
Code: Select all
//-------------------
Static Func _GenEid()
LOCAL nRAND, cRAND
LOCAL oRs, cSQL, oERR
oRs:= TOleAuto():New( "ADODB.Recordset" )
oRs:CursorType := 1 // opendkeyset
oRs:CursorLocation := 3 // local cache
oRs:LockType := 3 // lockoportunistic
cSQL := "SELECT Distinct [GroupId] from [YourTable]" // groupid is char(18)
TRY
oRs:Open( cSQL,xCONNECT )
CATCH oErr
MsgInfo( "Error in Opening YourTable to Create Unique EID" )
RETURN("BOGUS")
END TRY
cRAND := 'BOGUS'
DO WHILE .T.
nRAND := nRANDOM(10000000000000000)
// 1 is reserved and 0 is a null key //
IF nRAND = 1 .or. nRAND = 0 .or. nRAND = NIL
LOOP
ENDIF
cRAND := STRzero(nRAND,18)
IF oRs:eof
ELSE
oRs:MoveFirst()
oRs:Find("GroupId = '"+cRAND+"'" )
ENDIF
IF oRs:eof
EXIT
ELSE
LOOP
ENDIF
EXIT
ENDDO
oRs:Close()
RETURN( cRAND )
Re: Unique id across multiple records
Posted: Sat Jun 15, 2013 5:33 pm
by Enrico Maria Giordano
Rick,
I'm testing the guid (see above) right now...
EMG
Re: Unique id across multiple records
Posted: Sat Jun 15, 2013 7:03 pm
by Rick Lipkin
Enrico
Interested to see how the guid turns out .. How are you going to apply that unique ID to your group of records ?
Rick
Re: Unique id across multiple records
Posted: Sat Jun 15, 2013 7:47 pm
by Enrico Maria Giordano
Rick,
Rick Lipkin wrote:Enrico
Interested to see how the guid turns out ..
I'll let you know.
Rick Lipkin wrote:How are you going to apply that unique ID to your group of records ?
cGuid = GetGuid()
INSERT ...
INSERT ...
INSERT ...
EMG