首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >确保子记录的序号从1开始

确保子记录的序号从1开始
EN

Stack Overflow用户
提问于 2010-08-20 09:49:41
回答 1查看 76关注 0票数 0

在Server 2008 R2数据库中,这是一个很难的问题(我认为)。我有一个NotificationTemplate的零到多个NotificationSmsTemplate子对象,因为我们的短信限制为160个字符,我需要发送几个短信的顺序,以提供整个短信通信。每个NotificationTemplate,每个NotificationSmsTemplate都有一个SmsSequenceNumber属性。这个数字可以是任何东西,只要它是包含NotificationTemplate.的唯一的

如何确保序列号从11开始,并与包含NotificationSmsTemplateNotificationTemplate.对象的集合相邻。我只能考虑使用内部序列号来允许用户在teh屏幕上订购模板,然后按此顺序排序,在插入DB时生成可见的序列号。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-20 10:40:49

如果我正确理解您的需求,您可以利用MySQL重用AUTO_INCREMENT值的方式,当AUTO_INCREMENT列是MyISAM表中多列索引的一部分时。有关更多详细信息,请参阅增量

这里有一个例子来说明我的意思:

创建一个表来保存通知模板:

代码语言:javascript
复制
CREATE  TABLE IF NOT EXISTS `notification_template` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `notification` TEXT NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = MyISAM;

创建一个表来保存SMS模板:

代码语言:javascript
复制
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表中插入几段较长的文本:

代码语言:javascript
复制
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表中,指定与其相关的notification_templateid

代码语言:javascript
复制
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.');

如果您现在从notification_sms_template中选择ID,您将看到每个notification_templatesms_sequence_number从1开始

代码语言:javascript
复制
SELECT `notification_template_id`, `sms_sequence_number`
FROM `notification_sms_template`;

+--------------------------+---------------------+
| notification_template_id | sms_sequence_number |
+--------------------------+---------------------+
|                        1 |                   1 |
|                        1 |                   2 |
|                        1 |                   3 |
|                        1 |                   4 |
|                        2 |                   1 |
|                        2 |                   2 |
|                        2 |                   3 |
+--------------------------+---------------------+
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3529772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档