本文共 1061 字,大约阅读时间需要 3 分钟。
MySQL INFORMATION_SCHEMA数据库抢救:
-- First check if the table exists
IF EXISTS(SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name'
AND table_name LIKE 'wild')
-- If exists, retreive columns information from that table
THEN
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
AND table_schema = 'db_name';
-- do some action, i.e. ALTER TABLE if some columns are missing
ALTER TABLE ...
-- Table does not exist, create a new table
ELSE
CREATE TABLE ....
END IF;
更多信息:
更新:
另一个选择,可能更容易,是删除现有的表,并使用新的模式重新创建它.为此,您需要:
>创建临时表,即现有表的精确副本
>使用旧表中的数据填充临时表
>丢掉旧桌子
>使用新模式创建新表
>使用临时表中的信息填充新表
>删除临时表.
所以,在SQL代码中:
CREATE TABLE old_table_copy LIKE old_table;
INSERT INTO old_table_copy
SELECT * FROM old_table;
DROP TABLE old_table;
CREATE TABLE new_table (...new values...);
INSERT INTO new_table ([... column names from old table ...])
SELECT [...column names from old table ...]
FROM old_table_copy;
DROP TABLE old_table_copy;
其实最后一步,“放下临时表”,你可以跳过一会儿.为了防万一,你会想要对旧表进行某种备份,即“在案”.
更多信息:
转载地址:https://blog.csdn.net/weixin_32421193/article/details/113345767 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!