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

Mysql 如果一列已经自动递增,如何使第二列递增?,mysql,Mysql,我的表中有三列(ID、收据号、名称)。ID是主键自动递增,所以ID将从1开始,我认为我必须设置收据号,所以它也将从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 )

我的表中有三列(ID、收据号、名称)。ID是主键自动递增,所以ID将从1开始,我认为我必须设置收据号,所以它也将从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 */;

为安全测试创建数据库:

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)
您的客户端程序将调用存储的进程并处理结果集,以获得下一个要使用的num,例如:

call getNextSequence("Receipt");
+------------+
| yourSeqNum |
+------------+
|       1001 |
+------------+
请再打一次电话给我:

call getNextSequence("Receipt");
+------------+
| yourSeqNum |
+------------+
|       1002 |
+------------+
它现在有一个列名为yourSeqNum的1行1列结果集

让我们用伪代码将其作为变量调用
NNNNN

INSERT temp(`Receipt_no`,`Name`) VALUES (NNNNN,'Fred'); -- again this is pseudocode
Id
AUTO_INCREMENT
列,因此我们在上面的列列表中跳过它。它由MySQL自动处理

为什么是伪代码?因为这里没有讨论前端语言是什么,比如PHP、Python、Java,要知道如何处理结果集以获得变量
NNNNN
。我不是什么都写

接下来的任务就是将上面获取序列号的部分转换为变量,并在
INSERT
语句中使用它

清理:

DROP SCHEMA Hybreeder;
现在有人会说这整件事看起来很傻,因为
NNNNN
总是比
Id
大1000,那么这有什么意义呢?如果收据部分的序列表有多个使用者,例如其他流程或其他公司,则情况并非如此


请按照上面的叙述了解我在上面提到的更多技术方面。

否。每个故事只有一个自动递增列。当然。使用我的序列号thingie并获取非自动增量列的下一个数值。有了它,在非AI列的insert上使用它。容易吗?没有。没有人说编程总是很容易的。这意味着我必须设置收据\没有唯一的键?你能帮我进行简单的查询吗?你想让我向你展示一个完整的工作演示,其中有一个自动公司和另一个递增的数字(假设它从1000开始)?但前提是您可以通过PHPMyAdmin写入存储的过程(我的意思是保存它们)。如果您希望
收据编号
始终等于
id
,则甚至不需要
收据编号