Unique id across multiple records
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Unique id across multiple records
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
Any ideas?
EMG
- Daniel Garcia-Gil
- Posts: 2365
- Joined: Wed Nov 02, 2005 11:46 pm
- Location: Isla de Margarita
- Contact:
Re: Unique id across multiple records
Hello
create a new table with group insert ids
start transaction
insert in groupid
retrieve last id
use the id to group insert
close transaction
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;
Code: Select all
BEGIN
Code: Select all
INSERT INTO groupid (id) VALUES(NULL)
Code: Select all
@LASTID=LAST_INSERT_ID()
Code: Select all
INSERT INTO myTable (groupid, field1, field2) VALUES (@LASTID, 'VALUE1', 'VALUE2'), (@LASTID, 'VALUE3', 'VALUE4'), (@LASTID, 'VALUE5', 'VALUE6')
Code: Select all
COMMIT
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Unique id across multiple records
Daniel,
EMG
Thank you. What do you exactly mean with BEGIN and COMMIT? They aren't SQL statements, as far as I know.Daniel Garcia-Gil wrote:Hello
create a new table with group insert ids
start transactionCode: Select all
CREATE TABLE `groupid` ( `id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB;
insert in groupidCode: Select all
BEGIN
retrieve last idCode: Select all
INSERT INTO groupid (id) VALUES(NULL)
use the id to group insertCode: Select all
@LASTID=LAST_INSERT_ID()
close transactionCode: Select all
INSERT INTO myTable (groupid, field1, field2) VALUES (@LASTID, 'VALUE1', 'VALUE2'), (@LASTID, 'VALUE3', 'VALUE4'), (@LASTID, 'VALUE5', 'VALUE6')
Code: Select all
COMMIT
EMG
- Daniel Garcia-Gil
- Posts: 2365
- Joined: Wed Nov 02, 2005 11:46 pm
- Location: Isla de Margarita
- Contact:
Re: Unique id across multiple records
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
http://dev.mysql.com/doc/refman/5.0/en/commit.html
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Unique id across multiple records
Daniel,
EMG
Sorry, I need an engine-aware solution.Daniel Garcia-Gil wrote: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
EMG
- Daniel Garcia-Gil
- Posts: 2365
- Joined: Wed Nov 02, 2005 11:46 pm
- Location: Isla de Margarita
- Contact:
Re: Unique id across multiple records
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
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
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Unique id across multiple records
Daniel,
What do you think about this?
Is it going to be a reasonably valid unique id?
EMG
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()
%>
EMG
- Daniel Garcia-Gil
- Posts: 2365
- Joined: Wed Nov 02, 2005 11:46 pm
- Location: Isla de Margarita
- Contact:
Re: Unique id across multiple records
Enrico
yes, i guess
yes, i guess
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Unique id across multiple records
Daniel,
EMG
Thank you.Daniel Garcia-Gil wrote:Enrico
yes, i guess
EMG
- Rick Lipkin
- Posts: 2397
- Joined: Fri Oct 07, 2005 1:50 pm
- Location: Columbia, South Carolina USA
Re: Unique id across multiple records
Enrico
Hope this is what you had in mind ?
Rick Lipkin
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.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
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 )
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Unique id across multiple records
Rick,
I'm testing the guid (see above) right now...
EMG
I'm testing the guid (see above) right now...
EMG
- Rick Lipkin
- Posts: 2397
- Joined: Fri Oct 07, 2005 1:50 pm
- Location: Columbia, South Carolina USA
Re: Unique id across multiple records
Enrico
Interested to see how the guid turns out .. How are you going to apply that unique ID to your group of records ?
Rick
Interested to see how the guid turns out .. How are you going to apply that unique ID to your group of records ?
Rick
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Unique id across multiple records
Rick,
INSERT ...
INSERT ...
INSERT ...
EMG
I'll let you know.Rick Lipkin wrote:Enrico
Interested to see how the guid turns out ..
cGuid = GetGuid()Rick Lipkin wrote:How are you going to apply that unique ID to your group of records ?
INSERT ...
INSERT ...
INSERT ...
EMG