- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
努力
创建在 INSERT 上调用的触发器并设置 originId = id (AUTO_INCREMENT)
,
我用过SQL提示here in 1st block :
CREATE TRIGGER insert_example
BEFORE INSERT ON notes
FOR EACH ROW
SET NEW.originId = (
SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'notes'
);
由于 information_schema
缓存,我还设置了
information_schema_stats_expiry = 0
在 my.cnf
文件中。正如我所注意到的,现在信息几乎在每次插入时立即更新。
但是,通过控制台以大约 2 分钟的间隔执行“直接”插入,我一直没有更新 originId
中的 AUTO_INCREMENT
值。
(它们应该等于 id
字段)
显式查询时,获取 AUTO_) 会导致更新的正确值。
因此我怀疑 SELECT AUTO_INCREMENT...
子查询的结果不知何故……什么?缓存?
如何解决这个问题?
谢谢。
我打算以这种方式实现某种 VCS:
originId
应该填充一个 id
的“origin”注解(通过应用程序逻辑)。等等。originId
进行分区,仅将最新版本获取到 UI。 初始问题:
如果“origin”Note 的 originId
为 NULL,则 MySQL 8 窗口函数默认(且仅?)RESPECT_NULL 模式执行的框架不符合预期(“好吧,呃,这都是关于您在分组列中的 NULL”)。
假设的解决方案:
将“origin”注释的 originId
设置为 id
在它们的initial and only INSERT 中,期望有 2 个好处:
originId
= id
轻松获取“来源”笔记,originId
执行正确的 PARTITION。产生的问题:id
是 AUTO_INCREMENT,因此无法(我知道)通过后端(即 PHP)在 INSERT 上获取其新值(对于新行)。
假设的解决方案:
所以,我希望找到一些 MySQL 机制来解决这个问题(避免对 id
字段进行操作),TRIGGER 似乎是正确的方法...
我相信在 MySQL 中自动复制 id
AUTO_INCREMENT 字段(或任何字段)非常快速和 super 简单,但现在完全没有出现......
因此,可能更好的方法是使用 vcsGroupId UNSIGNED INT
字段,负责“关联”Note 的版本:
MAX(vcsGroupId) + 1
填充它,vcsGroupId
值(通过 CTE 获取)填充它,PARTITION BY vcsGroupId
、ORDER BY id
或时间戳 使用 Window Function 执行框架DESC
,然后只使用第一行(或使用最后一行的升序),这似乎更容易,不是吗?
最佳答案
你这是在玩火。我不知道你的触发器到底会出什么问题(除此之外它对你不起作用),但我有一种强烈的感觉,很多事情都可能而且将会出错。例如:如果在单个语句中插入多行怎么办?我认为引擎不会为每一行更新 information_schema
。如果您运行 INSERT ... SELECT
语句,情况会更糟。因此,将 information_schema
用于此任务是一个非常糟糕的主意。
但是 - 第一个问题是:您为什么需要它?如果您需要保存“原始 ID”,那么您可能打算更新 id
列。这已经是个坏主意了。并假设您会找到解决问题的方法 - 什么可以保证 originId
不会在触发器外更改?
但是 - 另一种方法是在插入时将 originId
列保留为空白,并改为在 UPDATE 触发器中更新它。
假设这是你的 table :
create table vcs_test(
id int auto_increment,
origin_id int null default null,
primary key (id)
);
使用 UPDATE 触发器来保存原始 ID,当它第一次被改变时:
delimiter //
create trigger vcs_test_before_update before update on vcs_test for each row begin
if new.id <> old.id then
set new.origin_id = coalesce(old.origin_id, old.id);
end if;
end;
delimiter //
您的 SELECT 查询将是这样的:
select *, coalesce(origin_id, id) as origin_id from vcs_test;
您甚至可以使用以下架构保存完整的 ID 历史记录:
create table vcs_test(
id int auto_increment,
id_history text null default null,
primary key (id)
);
delimiter //
create trigger vcs_test_before_update before update on vcs_test for each row begin
if new.id <> old.id then
set new.id_history = concat_ws(',', old.id_history, old.id);
end if;
end;
delimiter //
下面的测试
insert into vcs_test (id) values (null), (null), (null);
update vcs_test set id = 5 where id = 2;
update vcs_test set id = 4 where id = 5;
select *, concat_ws(',', id_history, id) as full_id_history
from vcs_test;
会回来
| id | id_history | full_id_history |
| --- | ---------- | --------------- |
| 1 | | 1 |
| 3 | | 3 |
| 4 | 2,5 | 2,5,4 |
关于MySQL 8 - 插入时触发 - VCS 的重复 AUTO_INCREMENT id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58122151/
我们有一张 table : id int(11) auto_increment name varchar(255) 自动增量等于 1。插入行: INSERT INTO `projects` ( `id
我有一个大表,其 ID 列设置为 bigint auto_increment。在历史上的某个时候,我们决定在客户端生成一些伪随机数的 ID。 我的问题很简单:鉴于表列仍设置为 auto_increme
我有一个 MySQL 数据库,包含一个带有 auto_increment 主键的表。 考虑以下定义足以满足此示例: create table test(id integer not null auto
我只是想知道如果两个用户同时注册帐户会发生什么。AUTO_INCRMENT id 号是相同还是不同? 最佳答案 自增锁定 自动增量主键的便利性需要与并发进行一些权衡。在最简单的情况下,如果一个事务正在
用于与 MySQL++(C++ 库)一起工作 数据库有一些字段,您可以对其进行 AUTO_INCREMENT。如何知道向表中插入新行时这些字段的值是多少? 最佳答案 虽然 stacker 的答案会起作
我有一个sqlite表: 例如 CREATE TABLE TEST (id integer primary key, number integer unique, name text); 但是,我需要
是否有一种标准化的方法可以在 SQL 中创建一个带有自动增量列(我们称之为 ID)的表,以便我基本上可以在所有数据库中使用它? (例如在 SQL-92 中标准化) 如果是这样 - 如何?如果不是,为什
在 MySQL 表中,id 列设置为 auto_increment,下一个 auto_increment 在 phpmyadmin 中设置为 128,但添加的任何新行都将 127 作为 id 行中的值
我的问题是:我有一个包含 auto_increment 列的表。当我插入一些值时,一切正常。插入第一行:ID 1插入第二行:ID 2 现在我想在 ID 10 处插入一行。 我的问题是,在此之后只有 I
您好,我知道这个问题以前有人问过,但我问的不同,我想要的是在删除表中的一行后重置 auto_increment 列。如果我有 51 行的表并且我删除了第 48 行,我希望第 49 行重置为 48,50
我正在尝试将 AUTO_INCREMENT 设置为某个值,但我遇到了语法错误。我不确定它是什么,因为我查看了文档并且看起来没问题。但是肯定有错误。 编辑 - 抱歉,我正在使用 mysql phpmya
在我的程序中,我将记录插入表tbl_name。但是我收到了“主键重复条目”错误,这让我很困惑,这是我的代码和错误。 表: CREATE TABLE `tbl_name` ( `id` int(20)
我使用 MySQL 的 AUTO_INCRMENT 字段和 InnoDB 来支持事务。我注意到当我回滚事务时,AUTO_INCRMENT 字段没有回滚?我发现它是这样设计的,但是有什么解决方法吗? 最
我使用 MySQL 的 AUTO_INCRMENT 字段和 InnoDB 来支持事务。我注意到当我回滚事务时,AUTO_INCRMENT 字段没有回滚?我发现它是这样设计的,但是有什么解决方法吗? 最
我需要一些有关 MySQL 的帮助。我的 news 表上有 id 列,并且通常为第一个新闻分配 id=1,id= 2 第二个等等。但我希望它以 001 而不是 1 开头,然后是 002, 003 等等
我需要创建一个带有自动递增主键的表,所以我这样做: String sql = "CREATE TABLE Home" + "(id INTEGER not
user 表在 user_id 上定义时未使用 AUTO_INCRMENT。我想将其更改为 AUTO_INCRMENT 字段。但问题是 user_id 被外键约束中的许多表引用。 我们可以在不删除所有
我正在使用 MySQL 进行数据库项目,并且我的一个表使用组合键作为 PK,entry_ID+user_ID(FK)。 这是表结构: | address_list | CREATE TABLE `ad
我有一些使用带有自动增量功能的主键的MySQL表,这一切似乎都是在第一次插入记录时发生的,但是当我删除中间的任何记录时,它不会重置自动增量计数器,而是继续使用最后一个ID . 例如,我已插入具有以下
我有如下表格 个人信息 CREATE TABLE personalInfo(userid BIGINT AUTO_INCREMENT PRIMARY KEY) 专利信息 CREATE TABLE pa
我是一名优秀的程序员,十分优秀!