LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-03-2015, 09:44 AM   #1
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Rep: Reputation: 16
Question Delete all data from a table exept the newest for 2 months


Hi

I want to delete all data from a table in my database but keep the last 2 months there (newest entries)...

How can i do it?

Thanks
 
Old 04-03-2015, 12:01 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,633

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by ASTRAPI View Post
Hi
I want to delete all data from a table in my database but keep the last 2 months there (newest entries)...How can i do it?
Lots of different ways. But, since you (again) do not provide any useful details, there is nothing we can tell you. We can't guess as to what kind of database you're talking about, version/distro of Linux, or how you want to do this, so what do you think we'll be able to tell you??? Doing this via an ksh script running on RHEL 5 is far different than doing the same operation via typing in a direct SQL command to an Oracle RAC server. Both accomplish the same thing, but are far different. Also, you don't say whether you want to do this once, or repeatedly...scripting something makes sense if you're going to do it over and over, but not much if it's one time.

Again, as you've been asked many times in the past, can you show us what you have done/tried so far to accomplish this??

Last edited by TB0ne; 04-03-2015 at 12:16 PM.
 
Old 04-03-2015, 04:27 PM   #3
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Original Poster
Rep: Reputation: 16
I am using Centos 64bit 6.6 and MariaDB mysql v10.0.17 and i want to do this only once.

Thanks and sorry for not providing the info on my topic....

Database name: hal9000_insomniacs
Table name: inline_notifications
Table columns: notify_id, notify_to_id, notify_sent, notify_read, notify_title, notify_text, notify_from_id, notify_type_key, notify_url, notify_meta_app, notify_meta_area, notify_meta_id, notify_meta_key

Target is to keep the data from last 2 months (February, March, and from April the rest days)

Thanks

Last edited by ASTRAPI; 04-03-2015 at 04:34 PM.
 
Old 04-03-2015, 05:12 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,633

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by ASTRAPI View Post
I am using Centos 64bit 6.6 and MariaDB mysql v10.0.17 and i want to do this only once. Thanks and sorry for not providing the info on my topic....
We don't like asking each time...please read the "Question Guidelines" link in my posting signature.
Quote:
Database name: hal9000_insomniacs
Table name: inline_notifications
Table columns: notify_id, notify_to_id, notify_sent, notify_read, notify_title, notify_text, notify_from_id, notify_type_key, notify_url, notify_meta_app, notify_meta_area, notify_meta_id, notify_meta_key

Target is to keep the data from last 2 months (February, March, and from April the rest days)
Ok...so your column names don't tell us anything, and if you want to purge older records, you will very obviously need one of those columns to have SOME sort of date in it. Are any of them dates?? What is the data type on those fields???

And, as you were asked previously, what have you done/tried/researched ON YOUR OWN so far???? Again, this is something that is easily looked up...the MySQL documentation covers deleting records, the syntax of the command, and the options.
Code:
DELETE FROM sometable WHERE somefield < date_add(current_date, interval -60 day)'
...is one of the very easily-found examples. Please, again, try to show some effort of your own.
 
Old 04-03-2015, 06:21 PM   #5
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Original Poster
Rep: Reputation: 16
I have on those fields simple text and nothing like date or id that increase or something that i can tell to my command to use and delete until that point.....

Yes i found searching on Google the same command as you but as i do not have any date related info i post here.

The only thing that it may help is that i have there 2311323 total rows as phpmyadmin report for that table and maybe i can delete from 1 up to 2.000.000 ?

I know this is nt accurate at all as it is not per day that i was looking for but it will help me a lot ...

Last edited by ASTRAPI; 04-03-2015 at 06:34 PM.
 
Old 04-03-2015, 06:28 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by ASTRAPI View Post
I have on those fields simple text and nothing like date or id that increase or something that i can tell to my command to use and delete until that point.....
Then you are probably out of luck...

If you cannot describe to us how you determine the age of a record, then you probably cannot describe it to the DBMS, and neither we nor MariaDB are mind readers.
 
1 members found this post helpful.
Old 04-03-2015, 06:38 PM   #7
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Original Poster
Rep: Reputation: 16
Quote:
If you cannot describe to us how you determine the age of a record
There is nothing related to describe as i can't determine the age of any record....

It will be help me if i can from 2311323 total rows to delete from 1 up to 2.000.000 and leave there the rest....
 
Old 04-03-2015, 06:42 PM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by ASTRAPI View Post
There is nothing related to describe as i can't determine the age of any record....

It will be help me if i can from 2311323 total rows to delete from 1 up to 2.000.000 and leave there the rest....
But here you have the same problem... how do you identify the row ordering that would let you identify "row 1" or "row 2,000,000" and those in between?

Can you tell us the domain (definition) of the data in each column that might be used for such ordering?
 
Old 04-03-2015, 06:50 PM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,633

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by ASTRAPI View Post
I have on those fields simple text and nothing like date or id that increase or something that i can tell to my command to use and delete until that point.....

Yes i found searching on Google the same command as you but as i do not have any date related info i post here.

The only thing that it may help is that i have there 2311323 total rows as phpmyadmin report for that table and maybe i can delete from 1 up to 2.000.000 ?

I know this is nt accurate at all as it is not per day that i was looking for but it will help me a lot ...
If you have nothing that identifies a date, how do you think you'll be able to perform date functions???? And do you have any other tables? What software generated that database?

Again, you're omitting what you've done and tried, and are making us guess.
 
Old 04-03-2015, 06:51 PM   #10
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Original Poster
Rep: Reputation: 16
There is no any data that can help ordering so it seems that i am out of luck

Thanks anyway all of you !
 
Old 04-03-2015, 07:02 PM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by ASTRAPI View Post
There is no any data that can help ordering so it seems that i am out of luck

Thanks anyway all of you !
If there is no data that establishes an ordering, then there is no magic way to infer such an ordering.

It is worth noting that in the relational database model (i.e. what MariaDB supports) there is, by definition, no inherent ordering of rows - they are a "set", and members of sets have no order. Any desired ordering must be based on pairing of some property of the members of some subset (i.e., some column value) with members of some other set (i.e., set of integers, range of timestamps, etc.). In the absence of some column based ordering, there is, by definition, no order to the rows (i.e., members of the set).
 
Old 04-03-2015, 07:25 PM   #12
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Original Poster
Rep: Reputation: 16
Ok i found something that can help

In the column notify_id it starts from 13652220 and increase up to 18661584

Some entries are missing for example but this i think it should not be a problem as if not exits just goes to the next one....

It is like.... 13652220, 13652221, 13652224, 13652227

So can i use this to delete from 13652220 up to 18000000 for example?

Last edited by ASTRAPI; 04-03-2015 at 07:26 PM.
 
Old 04-03-2015, 07:49 PM   #13
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by ASTRAPI View Post
Ok i found something that can help

In the column notify_id it starts from 13652220 and increase up to 18661584

Some entries are missing for example but this i think it should not be a problem as if not exits just goes to the next one....

It is like.... 13652220, 13652221, 13652224, 13652227

So can i use this to delete from 13652220 up to 18000000 for example?
You can use that to delete the range of rows corresponding to those values of the notify_id column.

We cannot say whether that corresponds to some range of time values because we have no idea at all what domain those values belong to (i.e., what they mean).

If you assume that the numeric values of notify_id correspond to some monotinically increasing function of time then it might do what you want. But only you can know that - we still have no idea what those notify_id values mean or how they are generated or whether they are unique, etc.

If the application that uses the database depends on key integrity, it is possible that removing those values may break other things too. This possibility is also indicated by the fact that there are missing values - it raises questions such as were they there and removed, or were they never there? If they were removed, what process removed them and based on what criteria? If we manually remove others will some process fail because they do not exist? These are all questions related to key integrity and model rules.

I point this out instead of just saying, sure delete them, so that you will know that it might have unexpected effects.

And again, only you know what the application is and only you are able to explore answers to these questions. And obviously, it is you who must be responsible if something unexpected does happen...

My suggestion would be to spend some time learning the basics of the relational model, then trying to understand the application within the context of the relational model, and then making decisions about deleting data based on all that solid knowledge.

Last edited by astrogeek; 04-03-2015 at 08:06 PM.
 
Old 04-03-2015, 08:16 PM   #14
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Original Poster
Rep: Reputation: 16
They are just notifications for members for example that you have one new pm.....

I can delete the old ones without any problem don't worry.

Can i use something like this to delete them?

Code:
DELETE FROM inline_notifications WHERE notify_id < '8000000'
or

Code:
DELETE * FROM inline_notifications WHERE notify_id < '8000000'

Last edited by ASTRAPI; 04-03-2015 at 08:41 PM.
 
Old 04-04-2015, 09:34 AM   #15
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,633

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by ASTRAPI View Post
They are just notifications for members for example that you have one new pm.....
I can delete the old ones without any problem don't worry. Can i use something like this to delete them?
Code:
DELETE FROM inline_notifications WHERE notify_id < '8000000'
or
Code:
DELETE * FROM inline_notifications WHERE notify_id < '8000000'
Again, have you tried reading the MySQL documentation (as you were told to before), and look at the syntax of the delete command?? It took you longer to post this, than it would have taken you to actually TRY those commands and see if they worked. Again, you need to start showing effort of your own.

And again, as you were asked before, WHAT SOFTWARE generated this database??? Are there any other tables that may have dates in them??? Unless you answer questions asked of you and provide details, there isn't much point in posting.

:: EDIT ::
A brief Google search turns up that name, as part of the Sphinx software. Amazingly, it also has documentation on deleting records...did you TRY to look this up?
http://sphinxsearch.com/docs/current...phinxql-delete

Last edited by TB0ne; 04-04-2015 at 09:40 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] doesn't contain a valid partition table? Working for months... dorlack Linux - Newbie 3 01-22-2015 11:39 AM
newest data file wolfindark Linux - Newbie 1 07-04-2008 01:42 AM
add/delete record in mysql table on linux from a WinXp machine using visual c++ SyncMaster Linux - Enterprise 2 05-30-2005 12:53 AM
delete records on table vickr1z Linux - Newbie 1 11-25-2004 05:27 AM
SELECT data from Table Gerardoj Programming 2 04-16-2004 11:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration