sql >> データベース >  >> RDS >> Mysql

外部キー制約が存在する場合のMySQL主キーの変更

    主キーを削除する前に、old_pkにインデックス(UNIQUEの場合もあります)を追加します:

    mysql> CREATE TABLE parent (
        ->     old_pk CHAR(8) NOT NULL PRIMARY KEY
        -> ) ENGINE=InnoDB;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> CREATE TABLE child (
        ->     parent_key CHAR(8),
        ->     FOREIGN KEY (parent_key) REFERENCES parent(old_pk)
        ->         ON UPDATE CASCADE ON DELETE CASCADE
        -> ) ENGINE=InnoDB;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> INSERT INTO parent VALUES ('a');
    Query OK, 1 row affected (0.01 sec)
    
    mysql> CREATE INDEX old_pk_unique ON parent (old_pk);
    Query OK, 1 row affected (0.01 sec)
    Records: 1  Duplicates: 0  Warnings: 0
    
    mysql> ALTER TABLE parent DROP PRIMARY KEY;
    Query OK, 1 row affected (0.01 sec)
    Records: 1  Duplicates: 0  Warnings: 0
    
    mysql> INSERT INTO child VALUES ('a');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> SHOW CREATE TABLE parent;
    +--------+------------------------------------------------------------------------------------------------------------------------------+
    | Table  | Create Table                                                                                                                 |
    +--------+------------------------------------------------------------------------------------------------------------------------------+
    | parent | CREATE TABLE `parent` (
      `old_pk` char(8) NOT NULL,
      KEY `old_pk_unique` (`old_pk`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
    +--------+------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> INSERT INTO child VALUES ('b');
    ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test/child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_key`) REFERENCES `parent` (`old_pk`) ON DELETE CASCADE ON UPDATE CASCADE)
    
    mysql> INSERT INTO parent VALUES ('b');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> INSERT INTO child VALUES ('b');
    Query OK, 1 row affected (0.01 sec)
    
    mysql> ALTER TABLE parent ADD id INT;
    Query OK, 2 rows affected (0.00 sec)
    Records: 2  Duplicates: 0  Warnings: 0
    
    mysql> UPDATE parent SET id = 1 WHERE old_pk = 'a';
    Query OK, 1 row affected (0.01 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> UPDATE parent SET id = 2 WHERE old_pk = 'b';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> ALTER TABLE parent ADD PRIMARY KEY (id);
    Query OK, 2 rows affected (0.00 sec)
    Records: 2  Duplicates: 0  Warnings: 0
    
    mysql> SHOW CREATE TABLE parent;
    +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table  | Create Table                                                                                                                                                                             |
    +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | parent | CREATE TABLE `parent` (
      `old_pk` char(8) NOT NULL,
      `id` int(11) NOT NULL default '0',
      PRIMARY KEY  (`id`),
      KEY `old_pk_unique` (`old_pk`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
    +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    


    1. 指定された次元のすべての長方形のマトリックスを検索します(座席のブロックを選択します)

    2. SQLServerで日付と時刻をフォーマットする方法

    3. Postgresの大文字と小文字の区別

    4. jdbcからMYSQLへのエラー:jdbc:mysql:// localhost:3306 / test?user ='root'&password=''に適したドライバーが見つかりません