Delete oldest records from database

前端 未结 3 543
悲哀的现实
悲哀的现实 2020-12-23 21:01

I have a database with 1000 records. I am trying to create an SQL statement so if the number of records grows above 1000, then the oldest records are deleted (i.e. the new r

3条回答
  •  太阳男子
    2020-12-23 22:02

    Assuming that your table has a Primary Key and a column with a timestamp indicating when the record was inserted), you can use a query along the lines of

    delete from tableToDeleteFrom
    where tablePK in 
    (select tablePK 
    from tableToDeleteFrom
    where someThresholdDate <= @someThresholdDate)
    

提交回复
热议问题