473,395 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Tracking record insertion and deletion

I am writing some software that, among other things, needs to track
the state of database tables. This includes occasionally checking the
table to see what records or added, modified, or deleted. The added
and modified parts of this have not proven too difficult to implement,
but is there an easy/elegant way to determine what records have been
deleted from a mySQL table ??

I'm very to mySQL, and after reading through the docs about logs, the
only thing I can come up with is to make a slave of the target table,
and somehow catch the delete statements, but do a select instead
before deleting them from the slave.

Has anyone tackled this problem and can offer some advice, or pointers
to advice..
??

Thanks in advance,
Dan
Jul 20 '05 #1
12 5127
"Dan Greenblatt" <an******@yahoo.com> wrote in message
I am writing some software that, among other things, needs to track
the state of database tables. This includes occasionally checking the
table to see what records or added, modified, or deleted. The added
and modified parts of this have not proven too difficult to implement,
but is there an easy/elegant way to determine what records have been
deleted from a mySQL table ??

I'm very to mySQL, and after reading through the docs about logs, the
only thing I can come up with is to make a slave of the target table,
and somehow catch the delete statements, but do a select instead
before deleting them from the slave.

Has anyone tackled this problem and can offer some advice, or pointers
to advice..
??


Perhaps the simplest suggestion is to log the SQL statements. Within the
command center you can say "tee c:\mylog\statements.txt". You can also
start mysql with the --tee option.

Then of course you have to parse the tee file. If so desired you can store
the pieces of statements into another MySql table (but don't use tee as you
want to log these insert statements). It all depends what you want to do
with the information.
Jul 20 '05 #2
if it won't impact peformance too much i'd suggest setting up a log
table, and append to it straight after (or before) such changes. I'm
starting to do this with my projects and it's been great for tracking
who did what when, plus exactly what has been changed. Great also for
debugging. A log file will probably give you more info though - depends
i guess.
Dan Greenblatt wrote:
I am writing some software that, among other things, needs to track
the state of database tables. This includes occasionally checking the
table to see what records or added, modified, or deleted. The added
and modified parts of this have not proven too difficult to implement,
but is there an easy/elegant way to determine what records have been
deleted from a mySQL table ??

I'm very to mySQL, and after reading through the docs about logs, the
only thing I can come up with is to make a slave of the target table,
and somehow catch the delete statements, but do a select instead
before deleting them from the slave.

Has anyone tackled this problem and can offer some advice, or pointers
to advice..
??

Thanks in advance,
Dan

Jul 20 '05 #3

"Dan Greenblatt" <an******@yahoo.com> wrote in message
news:a2**************************@posting.google.c om...
I am writing some software that, among other things, needs to track
the state of database tables. This includes occasionally checking the
table to see what records or added, modified, or deleted. The added
and modified parts of this have not proven too difficult to implement,
but is there an easy/elegant way to determine what records have been
deleted from a mySQL table ??

I'm very to mySQL, and after reading through the docs about logs, the
only thing I can come up with is to make a slave of the target table,
and somehow catch the delete statements, but do a select instead
before deleting them from the slave.

Has anyone tackled this problem and can offer some advice, or pointers
to advice..
??


Hope this helps:

I think the log files automatically contain this information.

mysql> show binlog events;

or

mysql> show binlog events in '<logfile>';

will show the create statements, updates, and deletes.

Reference: ( TIP 24: and TIP 2) in
http://osdn.dl.sourceforge.net/sourc...ADME_mysql.txt

It's also possible to write a C api that will automatically extract this
information. For an example program you may want to download

http://prdownloads.sourceforge.net/s...ar.gz?download

and take a look at "selectbinlog.c". Or, a perl script can be written as
well...(See TIP 4: ) for the basics on perl with mysql.

This can contain a lot of information. Instead of putting this information
into a mysql table, you may want to put it into a BerkeleyDB file. If you're
running Linux or Unix you probably have this installed. BerkeleyDB is used
by MySQL. It's not really a database but a fast hash or b-tree, depending
on format option selected. For an example of how to write BerkeleyDB api's
see the following:
http://prdownloads.sourceforge.net/c...ar.gz?download

The latest version of BerkeleyDB allows for encryption, so that this data
can be protected. I use BerkeleyDB for this type of logging because I need
to do extremely quick lookups on this information.

Regards,

Mike Chirico
Jul 20 '05 #4
"Mike Chirico" <mc******@comcast.net> wrote in message
news:zsOdnWTnUKpN0V7dRVn-
I think the log files automatically contain this information.

mysql> show binlog events;

or

mysql> show binlog events in '<logfile>';


Do you have to turn on binlog events too? Can you use the command on the
tee file?

Jul 20 '05 #5

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:u8*********************@bgtnsc05-news.ops.worldnet.att.net...
"Mike Chirico" <mc******@comcast.net> wrote in message
news:zsOdnWTnUKpN0V7dRVn-
I think the log files automatically contain this information.

mysql> show binlog events;

or

mysql> show binlog events in '<logfile>';


Do you have to turn on binlog events too? Can you use the command on the
tee file?


I've always used the "tee file" for a single session. I think it returns
everything include the results of the select, delete statements? I don't
know of an easy way to parse the tee file, or to get information on all
users. The "show binlog events" has options for listing start positions
and limiting the number of events. For instance, "mysql> show binlog event
from 201 limit 2;" will show the 2 events starting at position 201, which is
handy. True, the results or return rows of a query are not shown; but, I
think this is a big advantage for a heavily used database?

I've never done anything special to turn on binlog events. However, doing a
"mysql> reset master" will delete and clear the log files.

FYI: I think in version 5 of Mysql these log files will be "all" binary.
Whereas in version 4-alpha, I can currently, using Linux, do the "strings"
command on these files "$ strings /usr/local/var/third-fl-71-bin.000001" and
get readable output, but, then there is no position tracking.

Regards,

Mike Chirico
Jul 20 '05 #6

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:u8*********************@bgtnsc05-news.ops.worldnet.att.net...
"Mike Chirico" <mc******@comcast.net> wrote in message
news:zsOdnWTnUKpN0V7dRVn-
I think the log files automatically contain this information.

mysql> show binlog events;

or

mysql> show binlog events in '<logfile>';


Do you have to turn on binlog events too? Can you use the command on the
tee file?

/etc/my.cnf

[mysqld]
port = 3306
socket = /tmp/mysql.sock
log-bin = /usr/local/var/mysqlLOGb.log
or just

log-bin
Regards,

Mike Chirico
Jul 20 '05 #7
"Mike Chirico" <mc******@comcast.net> wrote in message
news:4_GdnQgFSZmPxF7dRVn-
Do you have to turn on binlog events too? Can you use the command on the tee file?
I've always used the "tee file" for a single session. I think it returns
everything include the results of the select, delete statements? I don't
know of an easy way to parse the tee file, or to get information on all
users. The "show binlog events" has options for listing start positions
and limiting the number of events. For instance, "mysql> show binlog

event from 201 limit 2;" will show the 2 events starting at position 201, which is handy. True, the results or return rows of a query are not shown; but, I
think this is a big advantage for a heavily used database?

I've never done anything special to turn on binlog events. However, doing a "mysql> reset master" will delete and clear the log files.

FYI: I think in version 5 of Mysql these log files will be "all" binary.
Whereas in version 4-alpha, I can currently, using Linux, do the "strings"
command on these files "$ strings /usr/local/var/third-fl-71-bin.000001" and get readable output, but, then there is no position tracking.


Thanks for your answers.
Jul 20 '05 #8
Excellent !! This looks like exactly the kind of thing I need..
Thanks for all the great info -
Dan
"Mike Chirico" <mc******@comcast.net> wrote in message news:<2e********************@comcast.com>...
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:u8*********************@bgtnsc05-news.ops.worldnet.att.net...
"Mike Chirico" <mc******@comcast.net> wrote in message
news:zsOdnWTnUKpN0V7dRVn-
I think the log files automatically contain this information.

mysql> show binlog events;

or

mysql> show binlog events in '<logfile>';


Do you have to turn on binlog events too? Can you use the command on the
tee file?

/etc/my.cnf

[mysqld]
port = 3306
socket = /tmp/mysql.sock
log-bin = /usr/local/var/mysqlLOGb.log
or just

log-bin
Regards,

Mike Chirico

Jul 20 '05 #9
Hi David -
Would you be able to clarify your suggestion? Specifically, what do
you put in your 'log tables' ? What gets appended to it?
Thanks,
Dan
David L <dl@nospam.com> wrote in message news:<85*****************@news-server.bigpond.net.au>...
if it won't impact peformance too much i'd suggest setting up a log
table, and append to it straight after (or before) such changes. I'm
starting to do this with my projects and it's been great for tracking
who did what when, plus exactly what has been changed. Great also for
debugging. A log file will probably give you more info though - depends
i guess.
Dan Greenblatt wrote:
I am writing some software that, among other things, needs to track
the state of database tables. This includes occasionally checking the
table to see what records or added, modified, or deleted. The added
and modified parts of this have not proven too difficult to implement,
but is there an easy/elegant way to determine what records have been
deleted from a mySQL table ??

I'm very to mySQL, and after reading through the docs about logs, the
only thing I can come up with is to make a slave of the target table,
and somehow catch the delete statements, but do a select instead
before deleting them from the slave.

Has anyone tackled this problem and can offer some advice, or pointers
to advice..
??

Thanks in advance,
Dan

Jul 20 '05 #10
it depends what you want to track. For my projects i make a selection
of the important fields in the data table, plus other fields such as the
user name who's making the change, a transaction number, what the action
is, and a timestamp. The action is just a text field that could say
"before-edit", "after-edit", "new", etc. All these fields would make up
the log table. If there are other fields from other tables that would
be helpful to put things into context, then those would be included as
well.

Whenever there is an update on a record, even on just one of the fields,
i append all these fields into the log table, before the update, and
append another row after the update. Because the 2 log records share a
transaction number (i just generate a random number for the purpose),
this allows me to see at a glance, what change look place. Of course to
be precise i probably should flag which field is changed, but i want to
be as lazy as possible.

Note that I'm actually appending the contents of my original table(s),
not sql statements.

And if the project has several main tables that don't relate closely to
each other, then i would have several of these log tables as well.

Dan Greenblatt wrote:
Hi David -
Would you be able to clarify your suggestion? Specifically, what do
you put in your 'log tables' ? What gets appended to it?
Thanks,
Dan
David L <dl@nospam.com> wrote in message news:<85*****************@news-server.bigpond.net.au>...
if it won't impact peformance too much i'd suggest setting up a log
table, and append to it straight after (or before) such changes. I'm
starting to do this with my projects and it's been great for tracking
who did what when, plus exactly what has been changed. Great also for
debugging. A log file will probably give you more info though - depends
i guess.
Dan Greenblatt wrote:
I am writing some software that, among other things, needs to track
the state of database tables. This includes occasionally checking the
table to see what records or added, modified, or deleted. The added
and modified parts of this have not proven too difficult to implement,
but is there an easy/elegant way to determine what records have been
deleted from a mySQL table ??

I'm very to mySQL, and after reading through the docs about logs, the
only thing I can come up with is to make a slave of the target table,
and somehow catch the delete statements, but do a select instead
before deleting them from the slave.

Has anyone tackled this problem and can offer some advice, or pointers
to advice..
??

Thanks in advance,
Dan

Jul 20 '05 #11
"David L" <dl@nospam.com> wrote in message news:85mwc.7220$rz4.2487@news-
if it won't impact peformance too much i'd suggest setting up a log
table, and append to it straight after (or before) such changes. I'm
starting to do this with my projects and it's been great for tracking
who did what when, plus exactly what has been changed. Great also for
debugging. A log file will probably give you more info though - depends
i guess.


An unrelated question. If there is a field date_last_updated, how can I
automatically set that field whenever I update a record? I know in MySql if
you have

create table product
(
id int ( 1) unsigned not null
auto_increment
, date_created timestamp not null
default ''

then when you insert a new record date_created defaults to now. But how to
set date_last_updated, so that whenever I issue an update statement, the
system automatically updates this column.

update product
set name = 'abc'
where name = 'def';

The above statement should automatically set product.date_last_updated to
now. Is this possible in MySql?
Jul 20 '05 #12
Hi Siemel, sorry i haven't checked posts till now.
In my experience when a field is the timestamp type, the timestamp will
update itself automatically whenever there is a change in the contents
of any other fields in the record.

Regards.

Siemel Naran wrote:
"David L" <dl@nospam.com> wrote in message news:85mwc.7220$rz4.2487@news-

if it won't impact peformance too much i'd suggest setting up a log
table, and append to it straight after (or before) such changes. I'm
starting to do this with my projects and it's been great for tracking
who did what when, plus exactly what has been changed. Great also for
debugging. A log file will probably give you more info though - depends
i guess.

An unrelated question. If there is a field date_last_updated, how can I
automatically set that field whenever I update a record? I know in MySql if
you have

create table product
(
id int ( 1) unsigned not null
auto_increment
, date_created timestamp not null
default ''

then when you insert a new record date_created defaults to now. But how to
set date_last_updated, so that whenever I issue an update statement, the
system automatically updates this column.

update product
set name = 'abc'
where name = 'def';

The above statement should automatically set product.date_last_updated to
now. Is this possible in MySql?

Jul 20 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: salamol | last post by:
I has a strange question. My company is using a old system with Win NT 4.0 Server + MS SQL 7.0. The system is busy and handle a lot of SELECTs and INSERTs all the time. Sometimes, some...
5
by: byrocat | last post by:
Sybase and DB2 both have the capability of tracking user activities at a number of levels: invalid access attempts to databases, table, etc.; creation/deletion/modification of database...
0
by: NB | last post by:
Hi A part of my application involves management of stocks in the warehouse. Incoming/outgoing transactions, stocktake, quantity on hand have all been satisfactorily managed. The app has run...
19
by: MaXX | last post by:
Hi, I hope I'm not OT. I have the following issue: I want to delete a record from my db with a php script. Let's say I'm auth'd and I want to delete the record id 440. With a simple form (get...
2
by: shahrukh | last post by:
Plz If Anyone Can Tell Me Insertion And Deletion In Linked List And In Array Then Reply Here There Programs. Even Reply Any One Program Either For Linked List And Either For Array.
0
by: polocar | last post by:
Hi, I have noticed a strange behaviour of CurrencyManager objects in C# (I use Visual Studio 2005 Professional Edition). Suppose that you have a SQL Server database with 2 tables called "Cities"...
4
by: Phil Stanton | last post by:
Sorry to repost, but am having another look at deleting a record. I have a form (Member) and have removed all the event procedures associated with the Form (OnCurrent, OnDelete, OnActivate etc)...
0
by: toughguy | last post by:
Hi Friends, Iam Leela from India. I want to track when a Trigger defined on a particular table is run in MS SQL 2000. I have a table on which i have trigger for insertion, update, deletion...
6
by: barcaroller | last post by:
If I insert/remove an element in a set, will an iterator to this set automatically become invalid? Does the position of the iterator before the insertion/removal matter? How are vectors and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.