473,809 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($messa ge){
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 1662
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($messa ge){
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******@nospa m.net> wrote in message
news:REUJd.253$ u45.160@trnddc0 8...
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($messa ge){
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($messa ge){
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($messa ge){
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.co m> wrote in message
news:ct******** *@enews1.newsgu y.com...
Shane Niebergall wrote:
logEntry($messa ge){
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($messa ge){
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.co m> wrote in message
news:ct******** *@enews3.newsgu y.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
24103
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 = mysql_query($sql, $db); $num = mysql_num_rows($result); If you have a large table, to me that seems like it would be a system hog if all you want is the number of rows but not the data. I'm not sure if using mysql_unbuffered_query() would solve this, would...
2
2436
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 that, for instance, if the result set contains 10,000 rows, then only the first 10 rows from the record set are output. Thank you,
1
13449
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 statement could potentially return 400,000+ rows, but I really only want the first 15 records found. I can put a lower-bound (col >= "xxx") in the WHERE clause but cannot specify an upper-bound (i.e. AND col <= "yyy"); therefore, depending on the value...
6
1857
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 (i.e. when the database closes, then the number in the table is decremented. I plan to use this on a front/back end setup, with a number of front end users on the network. What i'm concerned about is if for some reason the network fails, and the...
2
3195
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 displayed now . Is there any other C# component/control to show my data as table like view .
6
4995
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.. .... var thistable = document.getElementById('resultaattbl'); for (var j=0, stop = thistable.tbody.rows.length; j<stop; j++) {
15
1799
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 (based on the primary key of the detail record). I've been trying with "TOP 3", but can't get anywhere. Using Access 2000. Something like: SELECT t1.*, TOP 3 t2.*
8
783
by: sqlservernewbie | last post by:
Hi Everyone, Here is a theoretical, and definition question for you. In databases, we have: Relation
1
1705
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 does exactly what I want if there is only one row in p_ferritin table. However, I am trying to limit the number of rows available to one of the tables - specifically p_ferritin. I want that table only to make available a single row (to the entire join...
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10387
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.