在SQL Server 2008 R2数据库上,这是一个很难的(我认为)。我有一个 NotificationTemplate ,包含零到多个 NotificationSmsTemplate 子对象,因为我们的短信限制消息为160个字符,我需要按顺序发送几条短信来提供整个短信通信。每 NotificationTemplate ,每个 NotificationSmsTemplate 都有一个SmsSequenceNumber属性。这个数字可以是任何数字,只要它包含 NotificationTemplate 是唯一的。
如何确保序列号从包含 NotificationTemplate 的 NotificationSmsTemplate 对象的集合开始11并且是连续的。我只能考虑使用内部序列号来允许用户在屏幕上订购模板,然后按顺序排序,在插入数据库时生成可见的序列号。
答案 0 :(得分:1)
如果我正确理解了您的要求,那么当AUTO_INCREMENT列是MyISAM表中多列索引的一部分时,您可以利用MySQL重用AUTO_INCREMENT值的方式。有关详细信息,请参阅Using AUTO_INCREMENT。
这是一个说明我的意思的例子:
创建一个表格来保存通知模板:
CREATE TABLE IF NOT EXISTS `notification_template` (
`id` INT NOT NULL AUTO_INCREMENT ,
`notification` TEXT NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = MyISAM;
创建一个表来保存SMS模板:
CREATE TABLE IF NOT EXISTS `notification_sms_template` (
`notification_template_id` INT NOT NULL ,
`sms_sequence_number` INT NOT NULL AUTO_INCREMENT ,
`sms` VARCHAR(160) NOT NULL ,
PRIMARY KEY (`notification_template_id`, `sms_sequence_number`) )
ENGINE = MyISAM;
将几个长文本段落插入notification_template
表:
INSERT INTO `notification_template` (`id`, `notification`) VALUES
(1, 'Tell me, O Muse, of that ingenious hero who travelled far and wide after he had sacked the famous town of Troy. Many cities did he visit, and many were the nations with whose manners and customs he was acquainted; moreover he suffered much by sea while trying to save his own life and bring his men safely home; but do what he might he could not save his men, for they perished through their own sheer folly in eating the cattle of the Sun-god Hyperion; so the god prevented them from ever reaching home. Tell me, too, about all these things, oh daughter of Jove, from whatsoever source you may know them.'),
(2, 'There were once a man and a woman who had long in vain wished for a child. At length the woman hoped that God was about to grant her desire. These people had a little window at the back of their house from which a splendid garden could be seen, which was full of the most beautiful flowers and herbs. It was, however, surrounded by a high wall, and no one dared to go into it because it belonged to an enchantress, who had great power and was dreaded by all the world.');
将160个字符的块插入notification_sms_template
表,指定它们所涉及的id
的{{1}}:
notification_template
如果您现在从INSERT INTO `notification_sms_template` (`notification_template_id`, `sms`) VALUES
(1, 'Tell me, O Muse, of that ingenious hero who travelled far and wide after he had sacked the famous town of Troy. Many cities did he visit, and many were the nat'),
(1, 'ions with whose manners and customs he was acquainted; moreover he suffered much by sea while trying to save his own life and bring his men safely home; but do'),
(1, ' what he might he could not save his men, for they perished through their own sheer folly in eating the cattle of the Sun-god Hyperion; so the god prevented th'),
(1, 'em from ever reaching home. Tell me, too, about all these things, oh daughter of Jove, from whatsoever source you may know them.'),
(2, 'There were once a man and a woman who had long in vain wished for a child. At length the woman hoped that God was about to grant her desire. These people had a'),
(2, ' little window at the back of their house from which a splendid garden could be seen, which was full of the most beautiful flowers and herbs. It was, however, '),
(2, 'surrounded by a high wall, and no one dared to go into it because it belonged to an enchantress, who had great power and was dreaded by all the world.');
选择ID,您会看到每个notification_sms_template
的{{1}}从1开始:
sms_sequence_number