dongwuzun4630 2013-03-25 11:08
浏览 38
已采纳

MySQL存储:存档数据的最佳方式

I'm quiet new in the SQL field. Thus I have a way of working question.

Every week, I will send data from an Excel spreadsheet on my MySQL DB through a pHp code. This is already working. Thus I have a table which I can update.

Actually I'm sending price of a specific underlying on my DB. What is the best way to archive my data as following.

On my DB, I have the below structure :

tabe t_index
Label = VARCHAR(255)
Price = float
Date = date()

Let's say I sent my data on my db last week, thus I stored :

Stock A
102.85
2013-03-18

Today, I want to send new price for the same Stock A which is 103.54, but I would like to archive and keep the 102.85 price to be able to make some return or whatever between the two prices.

How should I proceed ?

I hope my question is not too messy... Thank you for your help

  • 写回答

2条回答 默认 最新

  • dongqun1656 2013-03-25 11:16
    关注

    One way of doing this is, create a UPDATE trigger which inserts old value in another table. So when you update an existing entry, old data will be copied/archived to another table.

    CREATE TABLE t_index_archive (
    Label VARCHAR(255),
    Price float,
    Date datetime);
    

    Now create a trigger on your existing table

    DROP TRIGGER IF EXISTS archive_t_index;
    DELIMITER $$
    CREATE DEFINER = CURRENT_USER TRIGGER archive_t_index BEFORE UPDATE ON t_index
         FOR EACH ROW BEGIN 
              INSERT INTO t_index_archive VALUES (OLD.Label, OLD.Price, OLD.Date);
         END;
    $$
    DELIMITER ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据