如何限制mysql表中可以存储的行数? [英] How to limit number of rows can be store in mysql table?
问题描述
我需要将MySQL表中可以插入的行数限制为,仅10条.我只需要从MySQL实现此功能,即无需使用任何编程语言即可.
I need to limit the number of rows can be insert in a MySQL Table to 10 only. I need to implement this from MySQL only i.e without using any programming language.
我已经创建了一个实现 MAX_ROWS = 10
的表,但是它不起作用.
I have created a table with implementing MAX_ROWS = 10
but its not working.
请检查这些屏幕截图.
我很困惑该怎么做.如果有人会为我提供解决方案,那对我真的很有帮助.
I'm very confused how to do this. If someone will provide me the solution, that would be really helpful for me.
谢谢.
推荐答案
我认为MySQL没有提供这种内置功能.一种解决方案是您可以创建触发器.
I think there is no such inbuilt functionality providede by MySQL. One solution is that you can create trigger.
CREATE TRIGGER your_trigger_name
BEFORE INSERT ON your_table_name
FOR EACH ROW
BEGIN
DECLARE cnt INT;
SELECT count(*) INTO cnt FROM your_table_name;
IF cnt = 10 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'You can store only 10 records.';
END IF;
END;
在桌上尝试上面的触发器.将您的表名替换为您的表名.
Try above trigger on your table. Replace your table_name with your_table_name.
希望这会对您有所帮助.
Hope this will help you.
这篇关于如何限制mysql表中可以存储的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!