Rishi Jasapara
Rishi Jasapara

Reputation: 638

Set max rows INSERT limit MySQL

I have a table where the admin can upload details of more users. Before uploading he has to choose a limit; let's say he selects 30 users. So, can I set a limit on that MySQL table to allow inserting only 30 rows and not more? Is there any way to do this?

Upvotes: 2

Views: 5037

Answers (2)

user3360767
user3360767

Reputation: 986

If you are using a database access library, a sophisticated way to solve this is to open a transaction at the start of SQL query and do a commit to the data-base only in case you did not passed the limit, pseudo code (using soci):

transaction tr(*db);
//your SQL query that ends in "RETURNING id"
if (id < 30) tr.commit();
else throw;

Upvotes: 0

Sebas
Sebas

Reputation: 21522

INSERT INTO `user_details` (fields) 
SELECT d.detail
FROM 
    (SELECT 'asdasd' as `detail` FROM dual) d
    CROSS JOIN (
        SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS
        LIMIT 30
    ) i
  • For higher limit values, this trick won't work I suppose
  • You need access to the INFORMATION_SCHEMA schema.

example fiddle: http://sqlfiddle.com/#!2/15ea4/5

Upvotes: 3

Related Questions