This is a hard one (I think), on a SQL Server 2008 R2 database. I have a NotificationTemplate with zero to many NotificationSmsTemplate child bjects, because our SMS limits messages to 160 characters, I need to send several SMS’s in sequence to deliver the whole SMS communication. Per NotificationTemplate, each NotificationSmsTemplate has an SmsSequenceNumber property. This number can be anything as long as it is unique to it’s containing NotificationTemplate.
How can I ensure that the sequence numbers start 1 1 and are contiguous fro the collection of NotificationSmsTemplate objects for a containing NotificationTemplate. I can only think of using an internal sequence number to allow the user to order templates on teh screen, and then ordering by that, generate visible sequence numbers while inserting into the DB.
If I understand your requirements correctly, you can take advantage of the way MySQL will reuse AUTO_INCREMENT values when the AUTO_INCREMENT column is part of a multiple-column index in a MyISAM table. See Using AUTO_INCREMENT for more details.
Here’s an example to illustrate what I mean:
Create a table to hold your notification templates:
Create a table to hold the SMS templates:
Insert a couple of long text passages into the
notification_templatetable:Insert the 160 character chunks into the
notification_sms_templatetable, specifying theidof thenotification_templateto which they relate:If you now select the the IDs from
notification_sms_template, you will see thatsms_sequence_numberstarts at 1 for eachnotification_template: