内存表id,name解决方案,举例(workspaces表)

内存表id,name解决方案,举例(workspaces表)
1. 为workspaces表建立内存表
CREATE TABLE `mem_workspaces` (
  `id` int(11) NOT NULL,
  `name` char(255) NOT NULL,
  `pretty_name` char(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MEMORY DEFAULT CHARSET=utf8;

2. 为workspaces表建立更新触发器
CREATE TRIGGER update_mem_workspaces AFTER UPDATE ON workspaces
FOR EACH ROW   
     UPDATE mem_workspaces
        SET mem_workspaces.name = NEW.name, mem_workspaces.pretty_name = NEW.pretty_name
     WHERE mem_workspaces.id = NEW.id;

3. 为workspaces表建立新增时触发器
CREATE TRIGGER insert_mem_workspaces AFTER INSERT ON workspaces
FOR EACH ROW   
     INSERT INTO mem_workspaces
        VALUES (NEW.id,NEW.name,NEW.pretty_name);

4. 为workspaces表建立删除时触发器
CREATE TRIGGER delete_mem_workspaces AFTER DELETE ON workspaces
FOR EACH ROW   
     DELETE FROM mem_workspaces
        WHERE mem_workspaces.id = OLD.id;

5. 为数据库创建启动时初始化内存表的脚本/data/init_memory_tables.sql,如果要初始化其它的内存表,只需要在文件中加入一条初始化sql
   SET SQL_LOG_BIN=0;
   # 初始化workspaces表对应的内存表mem_workspaces
   INSERT INTO tapd3.mem_workspaces(id,name,pretty_name) SELECT id,name,pretty_name FROM tapd3.workspaces;

6. 修改/etc/my.cnf文件,以便启动mysql时执行/data/init_memory_tables.sql脚本
   [mysqld]
   init-file=/data/init_memory_tables.sql

你可能感兴趣的:(workspace)