简体   繁体   中英

script to remove primary key from multiple tables of a database in MySQL

I have many tables in my database and each table has primary key, unique key and auto-increment in it. I want to remove all three of these. How i can do it in a script without doing it manually for each table in the database. Is there any automate script for droping all these in all the tables of a database at once.

(From Comment)

CREATE TABLE emp3 (
    id int NOT NULL, 
    name varchar(30) DEFAULT NULL, 
    PRIMARY KEY (id), 
    UNIQUE KEY c_unique (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

If you are using ENGINE=InnoDB , then you really need to have a PRIMARY KEY on each table.

If you have JOINs between tables, you need to avoid dropping the column you are JOINing on.

Are the PK, unique key and AI column named consistently across the tables?

Please show us an example SHOW CREATE TABLE so we can tailor the answer to your need.

The Answer will include something like

SELECT
    CONCAT("ALTER TABLE ", table_name, "... ;")
    FROM information_schema
    WHERE table_schema = 'db_name';

Plus instructions on how to either use the "mysql" commandline tool or a stored proc to execute the commands created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM