V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
esolve
V2EX  ›  问与答

Mysql: alter 操作(增加和删除唯一约束)可以事务隔离吗?

  •  
  •   esolve · Oct 20, 2017 · 1461 views
    This topic created in 3114 days ago, the information mentioned may be changed or developed.

    要对某个表 table 进行批插入,但是不可以插入重复的记录

    表中有一个唯一约束 (phone, sales)

    其中 sales 可以为 null

    现在要插入一批数据,里面只有(phone )

    发现重复插入同一批数据是可以成功的,因为 sales 为 null 是可以重复的

    于是想法是在批插入的事务中加入唯一约束的添加和删除,大致如下:

    begin transaction
    alter table add unique key(phone)
    batch insert
    alter table drop unique key(phone)
    end transaction
    

    这样会有什么问题吗? 会对同表操作的其他的事务造成影响吗?

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2535 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 09:03 · PVG 17:03 · LAX 02:03 · JFK 05:03
    ♥ Do have faith in what you're doing.