Mysql
 sql >> Baza danych >  >> RDS >> Mysql

Aktualizowanie klucza podstawowego MySQL

Następnym razem użyj pojedynczej instrukcji „alter table”, aby zaktualizować klucz podstawowy.

alter table xx drop primary key, add primary key(k1, k2, k3);

Aby naprawić rzeczy:

create table fixit (user_2, user_1, type, timestamp, n, primary key( user_2, user_1, type) );
lock table fixit write, user_interactions u write, user_interactions write;

insert into fixit 
select user_2, user_1, type, max(timestamp), count(*) n from user_interactions u 
group by user_2, user_1, type
having n > 1;

delete u from user_interactions u, fixit 
where fixit.user_2 = u.user_2 
  and fixit.user_1 = u.user_1 
  and fixit.type = u.type 
  and fixit.timestamp != u.timestamp;

alter table user_interactions add primary key (user_2, user_1, type );

unlock tables;

Blokada powinna zatrzymać dalsze aktualizacje, gdy to robisz. Jak długo to potrwa, zależy oczywiście od wielkości twojego stołu.

Główny problem polega na tym, że masz kilka duplikatów z tym samym znacznikiem czasu.



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQL InnoDB Cluster 8.0 — pełny opis operacji:część druga

  2. BŁĄD 1115 (42000):Nieznany zestaw znaków:„utf8mb4”

  3. Uruchamianie dwóch zapytań SQL na jednej stronie php (SET + SELECT)

  4. Golang ORDER BY problem z MySql

  5. Zestaw znaków konfiguracyjnych CodeIgniter i obsługa UTF-8