簡體   English   中英

如何限制表格行數?

[英]How would I limit the number of table rows?

我有一個帶有列的簡單表:

date - DATETIME
name - varchar(50)
text - varchar(200)

如何在添加第11行之前獲取包含最早日期值的行,以便將其刪除,因此行數始終為10?

使用觸發器

create TRIGGER trigger_name before insert on table_name FOR EACH ROW BEGIN if count > 10 then delete from table_name insert into table_name values(id, name , quantity); END if; END ;

您也可以參考此鏈接http://dev.mysql.com/doc/refman/5.0/en/triggers.html

使用以下刪除查詢創建發布刪除觸發器:

     //get the row count
     IF rowCount > 10
       DELETE from myTable
       WHERE date = 
        (select date from 
          (select date from myTable
          order by date asc LIMIT 1) as tempTable
        )
      END IF;

我建議這樣做是因為刪除查詢中的相同表條件不起作用。 是我的另一個答案。

更新最舊的行,而不是執行insert + delete 當然,您需要確保表中有10行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM