473,396 Members | 1,914 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,396 software developers and data experts.

Limiting number of rows in a table

Hey all -

I'm trying to implement a basic log in a table. Instead of writing to a text
file on the server, I want to insert entries into a 'log' table. However, I
want to limit this table to only have the last 500 rows or so. How do I do
this?

Here's my pseudocode, but it seems as if there would be a better way:

logEntry($message){
insert message into table
get number of rows in table
if (numrows > 500){
delete from table where ((numrows - id) > 500)
}
}

Is that right??

Thanks,
Shane
Jul 23 '05 #1
6 1642
Shane Niebergall wrote:
Hey all -

I'm trying to implement a basic log in a table. Instead of writing to a text
file on the server, I want to insert entries into a 'log' table. However, I
want to limit this table to only have the last 500 rows or so. How do I do
this?

Here's my pseudocode, but it seems as if there would be a better way:
This would be easy to do with PHP but I'm not sure if you want to use
PHP...IMHO

logEntry($message){
insert message into table
get number of rows in table
if (numrows > 500){
delete from table where ((numrows - id) > 500)
}
}

Is that right??

Thanks,
Shane

Jul 23 '05 #2
I will be using PHP - how will that make it easier?

Thanks,
Shane

"Hal Halloway" <Ha******@nospam.net> wrote in message
news:REUJd.253$u45.160@trnddc08...
Shane Niebergall wrote:
Hey all -

I'm trying to implement a basic log in a table. Instead of writing to a text file on the server, I want to insert entries into a 'log' table. However, I want to limit this table to only have the last 500 rows or so. How do I do this?

Here's my pseudocode, but it seems as if there would be a better way:


This would be easy to do with PHP but I'm not sure if you want to use
PHP...IMHO

logEntry($message){
insert message into table
get number of rows in table
if (numrows > 500){
delete from table where ((numrows - id) > 500)
}
}

Is that right??

Thanks,
Shane

Jul 23 '05 #3
Shane Niebergall wrote:
logEntry($message){
insert message into table
get number of rows in table
if (numrows > 500){
delete from table where ((numrows - id) > 500)
}
}


I'd do the delete before I do the insert, if you want the number of rows
never to exceed 500. Also, you only need to delete one row if your
$message always results in inserting one row.

logEntry($message){
get number of rows in table
if numrows = 500 {
delete first row of table
}
insert $message into table
}

Be careful about multiple clients doing this operation simultaneously;
you might want to lock the table. See the docs on LOCK TABLES.

Also you can specify the MAX_ROWS in your CREATE TABLE statement if you
want MySQL to enforce it! See the docs of the CREATE TABLE statement.

Regards,
Bill K.
Jul 23 '05 #4
Hi Bill -

Thanks for the tips. Do you mind expanding on this:
"delete first row of table"

How would that delete query look?

Thanks so much!
Shane

"Bill Karwin" <bi**@karwin.com> wrote in message
news:ct*********@enews1.newsguy.com...
Shane Niebergall wrote:
logEntry($message){
insert message into table
get number of rows in table
if (numrows > 500){
delete from table where ((numrows - id) > 500)
}
}


I'd do the delete before I do the insert, if you want the number of rows
never to exceed 500. Also, you only need to delete one row if your
$message always results in inserting one row.

logEntry($message){
get number of rows in table
if numrows = 500 {
delete first row of table
}
insert $message into table
}

Be careful about multiple clients doing this operation simultaneously;
you might want to lock the table. See the docs on LOCK TABLES.

Also you can specify the MAX_ROWS in your CREATE TABLE statement if you
want MySQL to enforce it! See the docs of the CREATE TABLE statement.

Regards,
Bill K.

Jul 23 '05 #5
Shane Niebergall wrote:
Thanks for the tips. Do you mind expanding on this:
"delete first row of table"

How would that delete query look?


This could do it:

delete from myTable where primary_key_id =
(select min(primary_key_id) from myTable)

Regards,
Bill K.
Jul 23 '05 #6
Perfect - exactly what I was looking for. Thanks Bill!!
"Bill Karwin" <bi**@karwin.com> wrote in message
news:ct*********@enews3.newsguy.com...
Shane Niebergall wrote:
Thanks for the tips. Do you mind expanding on this:
"delete first row of table"

How would that delete query look?


This could do it:

delete from myTable where primary_key_id =
(select min(primary_key_id) from myTable)

Regards,
Bill K.

Jul 23 '05 #7

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

Similar topics

3
by: Xizor | last post by:
Ok, what I want to do is find out the number of rows in a table. The most obvious solution is to do something like the following: $sql = "SELECT blah FROM blah WHERE 1"; $result =...
2
by: nzanella | last post by:
Hello, I am running SQL Server 2000. I would like to know whether Microsoft Transact-SQL has a method for limiting the result set from a query in a way analogous to MySQL's LIMIT keyword, so...
1
by: BF | last post by:
Hello, I am trying to find out if there is a way to limit the number of rows returned when a cursor is opened. I am using DB2 version 7 on z/OS. The SELECT statement in my DECLARE CURSOR...
6
by: Gerry Abbott | last post by:
Hi all, Have written some code to limit the concurrent users of a database. Use the Autoexec macro to open a form. When the form opens it increments a value in a table. When the form closes...
2
by: duraisridhar | last post by:
Hi all., I am using the Listview control to display my data in table fromat Is there any way ( simple or complex) to Limit the number of rows displayed to avoid the empty rows which is also ...
6
by: Ward Germonpré | last post by:
Hi, I have a reference to a dom table. How can I retrieve the number of columns in that table ? The stop value below doesn't work, nor did my experimenting with tbodies and childNodes.. .......
15
by: Hexman | last post by:
Hello All, How do I limit the number of detail records selected in a Master-Detail set using SQL? I want to select all master records for a date, but only the first 3 records for the details...
8
by: sqlservernewbie | last post by:
Hi Everyone, Here is a theoretical, and definition question for you. In databases, we have: Relation
1
by: yakitori | last post by:
I'm very sorry because I suspect that this is the same-old-same-old to some of you. I did search first. With MySQL 5.0.27, I have the query shown below. It *almost* does what I want. In fact it...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.