gpt4 book ai didi

sql - MySQL 导出索引

转载 作者:行者123 更新时间:2023-11-29 09:18:31 25 4
gpt4 key购买 nike

我确信以前一定有人问过它,但我找不到它:

我有一个正在不断开发的 CMS。我使用CMS建立了许多网站,因此它们存在许多数据库。

我想从开发数据库中提取索引并将其应用于生产数据库。有没有一种简单的方法可以将索引提取为 SQL?

大致如下:

create index idx1 on one (lft, rght);
create index idx1 on two (alias);
create index acos_idx3 on three (model, related_id);

最佳答案

您可以从 INFORMATION_SCHEMA 数据库中提取索引,然后将它们添加到另一个数据库中,但这并不容易。

举个例子(来自用于部署的存储过程的代码),这会添加一个唯一的 key (如果尚不存在):

if not exists (select * from information_schema.KEY_COLUMN_USAGE 
where table_schema = 'database_name'
and table_name='your_table'
and constraint_name ='key_name')
then
alter table your_table add unique key `key_name` ('column_name');
end if;

基本上你可以在INFORMATION_SCHEMA中找到你需要的任何内容。我相信您可以编写代码来动态检查所有这些索引,但我不确定这对您来说是否容易。

更新:

您还可以使用 show index from database.table,正如您在 MaasSql 的答案提供的链接中看到的那样。然后循环遍历结果并添加每个索引(如果数据库中没有)。

或者你可以尝试这个:

if not exists (select * from information_schema.STATISTICS
where table_schema = 'database_name'
and table_name='table_name'
and index_name ='key_name')
then
...
end if;

关于sql - MySQL 导出索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3140160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com