zoukankan      html  css  js  c++  java
  • 通过替换frm文件方式修改表结构

    版本:5.6.16

    在自己的虚拟环境中,测试创建一个表,表结构如下:
    mysql> drop table yoon_temp;
    Query OK, 0 rows affected (0.09 sec)


    mysql> show create table yoonG
    *************************** 1. row ***************************
           Table: yoon
    Create Table: CREATE TABLE `yoon` (
      `id` int(11) DEFAULT NULL,
      `name` varchar(20) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    1 row in set (0.00 sec)


    再创建一个相同的表结构,name varchar(30) 为30,并通过替换.frm来实现yoon表的字段修改:
    mysql> show create table yoon_tempG
    *************************** 1. row ***************************
           Table: yoon_temp
    Create Table: CREATE TABLE `yoon_temp` (
      `id` int(11) DEFAULT NULL,
      `name` varchar(30) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    1 row in set (0.00 sec)


    不小心直接删除yoon_temp.frm文件,无法替换,在数据库上删除yoon_temp提示表不存在:
    [root@hank-yoon yoon]# rm -rf yoon_temp.frm

    mysql> show tables;
    +----------------+
    | Tables_in_yoon |
    +----------------+
    | yoon           |
    +----------------+
    1 row in set (0.00 sec)


    删除表:
    mysql> drop table yoon_temp;
    ERROR 1051 (42S02): Unknown table 'yoon.yoon_temp'

    重新创建表:
    mysql> create table yoon_temp (id int,name varchar(30));
    ERROR 1813 (HY000): Tablespace for table '`yoon`.`yoon_temp`' exists. Please DISCARD the tablespace before IMPORT.

    mysql> alter table yoon_temp DISCARD tablespace;
    ERROR 1146 (42S02): Table 'yoon.yoon_temp' doesn't exist


    在目录下通过yoon.frm拷贝创建yoon_temp.frm
    [root@hank-yoon yoon]# cp  yoon.frm  yoon_temp.frm

    [root@hank-yoon yoon]# chown mysql.mysql yoon_temp.frm

    查看表:
    mysql> show tables;
    +----------------+
    | Tables_in_yoon |
    +----------------+
    | yoon           |
    | yoon_temp      |
    +----------------+
    2 rows in set (0.00 sec)


    mysql> show create table yoon_tempG
    *************************** 1. row ***************************
           Table: yoon_temp
    Create Table: CREATE TABLE `yoon_temp` (
      `id` int(11) DEFAULT NULL,
      `name` varchar(20) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    1 row in set (0.00 sec)


    mysql> drop table yoon_temp;
    Query OK, 0 rows affected (0.03 sec)

    重新创建再测试:
    mysql> create table yoon_temp (id int,name varchar(30));
    Query OK, 0 rows affected (0.02 sec)

    当表结构文件不小心删除时,可通过其他表结构来创建进行修复删除,表结构要相同,字段varcahr(xxx) xxx大小不同无所谓,也间接实现了通过替换frm的方式修改表结构。

  • 相关阅读:
    【LeetCode 104_二叉树_遍历】Maximum Depth of Binary Tree
    【LeetCode 110_二叉树_遍历】Balanced Binary Tree
    【LeetCode 111_二叉树_遍历】Minimum Depth of Binary Tree
    【剑指Offer】36两个链表的第一个公共结点
    【剑指Offer】34第一个只出现一次的字符
    【剑指Offer】33丑数
    【剑指Offer】32把数组排成最小的数
    xgboost的原理没你想像的那么难(转载)
    【剑指Offer】31整数中1出现的次数(从1到n整数中1出现的次数)
    【剑指Offer】28连续子数组的最大和
  • 原文地址:https://www.cnblogs.com/hankyoon/p/5169738.html
Copyright © 2011-2022 走看看