Reputation: 638
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
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
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
INFORMATION_SCHEMA
schema.example fiddle: http://sqlfiddle.com/#!2/15ea4/5
Upvotes: 3