adding column to all tables in a mysql database (unknown table names)

前端 未结 4 827
予麋鹿
予麋鹿 2021-02-08 08:49

I need to add a primary key to a set of tables in a given database. I do not know how many tables there will be or what their specific names are. They will all be of the for dat

4条回答
  •  你的背包
    2021-02-08 09:27

    select concat('alter table ',table_name,' add column ID int auto_increment not null primary key first;')
    from information_schema.tables
    where table_schema = 'db_name' and table_type = 'base table';
    

    Once you have the output, copy and paste and run them all.

提交回复
热议问题