2010-08-20 59 views
0

在SQL Server 2008 R2數據庫上,這是一個很難的(我認爲)。我有一個NotificationTemplate與零對多NotificationSmsTemplate子對象,因爲我們的短信限制爲160個字符的消息,我需要發送幾個短信順序傳遞整個短信通信。每NotificationTemplate,每個NotificationSmsTemplate具有SmsSequenceNumber屬性。此號碼可以是任何東西,只要它包含NotificationTemplate即可。確保子記錄的序列號從1開始

我如何能確保序列號開始11且連續來回NotificationSmsTemplate對象的集合含有NotificationTemplate。我只能想到使用內部序列號來允許用戶在屏幕上訂購模板,然後按順序排序,在插入數據庫時​​生成可見的序列號。

+0

您將使用哪些DBMS? – Mike 2010-08-20 09:59:31

回答

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; 

創建一個表來保存的短信模板:

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表,指定notification_templateid其所涉及:

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,你會看到,sms_sequence_number從1開始對於每個notification_template

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 | 
+--------------------------+---------------------+ 
+0

+1的信息,但我使用SQL Server 2008.對不起,我沒有提到它,但我現在編輯並添加了。 – ProfK 2010-08-20 11:08:51

+0

@ProfK:啊,值得一試;-) – Mike 2010-08-20 11:12:48

+0

非常值得。我在一些項目中使用MySQL。 – ProfK 2010-08-20 12:05:46