如果一列已经自动增加,如何使第二列增加?

时间:2016-09-25 03:51:42

标签: mysql

我的表中有三列(ID,Receipt_no,Name).ID是主键自动增量,因此ID将从1开始,同样认为我必须在receipt_no上设置所以它也将从1开始。是否可能?

我正在使用phpmyadmin。

提前致谢。

-- Table structure for table `temp`
--

CREATE TABLE IF NOT EXISTS `temp` (
`Id` int(11) NOT NULL,
  `Receipt_no` int(11) NOT NULL,
  `Name` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


ALTER TABLE `temp`
 ADD PRIMARY KEY (`Id`), ADD UNIQUE KEY `Receipt_no` (`Receipt_no`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `temp`
--
ALTER TABLE `temp`
MODIFY `Id` int(11) NOT NULL AUTO_INCREMENT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

1 个答案:

答案 0 :(得分:1)

创建用于安全测试的数据库:

create schema Hybreeder;
use Hybreeder;

架构:

CREATE TABLE `temp` (
    `Id` int(11) AUTO_INCREMENT PRIMARY KEY,
    `Receipt_no` int(11) NOT NULL,
    `Name` varchar(20) NOT NULL,
    UNIQUE KEY `unq_Receipt_no` (`Receipt_no`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- The following table would be useful system wide for all
-- your special incrementor needs whatever they may be
-- Especially great for those looking for Prefixes to
-- add to PK's like an id such as ABC00001
create table sequences
(   id int auto_increment primary key,
    sectionType varchar(200) not null,
    nextSequence int not null,
    unique key(sectionType)
) ENGINE=InnoDB;

-- Prime it with some "sections" (we had to call them something)
insert sequences (sectionType,nextSequence) values
('Chassis',1),('Engine Block',1),('Carburetor',1),('Receipt',1001);

存储过程:

DROP PROCEDURE if exists getNextSequence;
DELIMITER $$ -- Note: delete this line for PHPMyAdmin (you don't need it)
CREATE PROCEDURE getNextSequence(p_sectionType varchar(200))
BEGIN
    -- pass in as a parameter the "section" for next inc, such as "Chassis"
    START TRANSACTION;
    SELECT nextSequence into @mine_to_use from sequences where sectionType=p_sectionType FOR UPDATE;
    UPDATE sequences set nextSequence=nextSequence+1 where sectionType=p_sectionType;
    COMMIT; -- get and release INTENTION LOCK ASAP
    SELECT @mine_to_use as yourSeqNum; -- return as a 1 column, 1 row resultset
END;
$$ -- Note: delete this line for PHPMyAdmin (you don't need it)
DELIMITER ; -- Note: delete this line for PHPMyAdmin (you don't need it)

您的客户端程序将调用存储过程并处理结果集以获取下一个要使用的数字,例如:

call getNextSequence("Receipt");
+------------+
| yourSeqNum |
+------------+
|       1001 |
+------------+

再次打电话给它:

call getNextSequence("Receipt");
+------------+
| yourSeqNum |
+------------+
|       1002 |
+------------+

现在有1行1列结果集,列名为yourSeqNum

让我们的伪代码将NNNNN称为变量。

INSERT temp(`Receipt_no`,`Name`) VALUES (NNNNN,'Fred'); -- again this is pseudocode

IdAUTO_INCREMENT列,因此我们会在上面的列列表中跳过它。它会被MySQL自动处理。

为什么是伪代码?因为这里没有谈论你的前端语言是什么,比如PHP,Python,Java,要知道如何处理结果集以获得变量NNNNN。而且我不是在写所有内容!

以下所有内容的任务是仅调整上面的部分,将序列号转换为变量并在INSERT语句中使用它。

清理:

DROP SCHEMA Hybreeder;

现在有些人会说这整件事情看起来很愚蠢,因为NNNNN总是比Id大1000,那又有什么意义呢?如果您有收据部分的序列表的多个消费者,比如其他流程或其他公司,则情况并非如此。

请关注Here上的叙述,了解我在上面提到的更多技术方面。