简体   繁体   English

限制表可以拥有的记录数

[英]Limit the number of records a table can have

I created a table, master-domain . 我创建了一个表, master-domain This table should only have 3 records. 该表应该只有3条记录。 How can I limit mysql database to only allow NO MORE than that number of records? 如何限制mysql数据库只允许超过该数量的记录? Is there a specific sql command to acomplish this? 是否有特定的sql命令来执行此操作?

This is my current SQL: 这是我目前的SQL:

CREATE TABLE `mydatabase`.`master-domain`
(
`domain` VARCHAR( 50 ) NOT NULL COMMENT 'Domain Name',
PRIMARY KEY ( `domain` )
)

PS. PS。 I have godaddy and it includes phpMyAdmin, in addition to MySQL databases. 我有godaddy,它包括phpMyAdmin,以及MySQL数据库。

You can make a table's primary key a field of type ENUM . 您可以将表的主键设置为ENUM类型的字段。 For example: 例如:

CREATE TABLE test (
    id enum('1','2') NOT NULL, 
    domain varchar(50) NOT NULL, 
    primary key (id));

When you update it you have to explicitly set the ID to "", "1", or "2".* It can't be null and there can only be one record with each ID. 更新它时,您必须将ID明确设置为“”,“1”或“2”。*它不能为空,每个ID只能有一条记录。 The domain is still stored in the domain field, so hopefully whatever external system is querying this database won't have any problems getting the results it wants. 域仍然存储在domain字段中,因此无论外部系统查询此数据库,都希望获得所需的结果不会有任何问题。

If you want to replicate the current restriction that domain names have to be unique, you could also add unique key (domain) . 如果要复制当前限制域名必须是唯一的限制,您还可以添加unique key (domain)

* note that the empty string is allowed (and not the same as NULL) because enum is actually a type of string. *请注意,允许使用空字符串(并且与NULL不同),因为枚举实际上是一种字符串。 So you'd specify two permitted ID values in order to have three total. 因此,您要指定两个允许的ID值,以便总共有三个。


Alternately: What are you trying to achieve / prevent here? 或者:你想在这里实现/阻止什么? Is there some automated process that might add records to the table? 是否有一些自动化流程可能会向表中添加记录? Are you trying to make sure that you don't accidentally do it, or that someone who hijacks your account can't do it? 您是在尝试确保不会意外地执行此操作,还是劫持您帐户的人无法执行此操作?

If the process that might insert records is running on your user, you could put your three records into the table and then take away INSERT privileges from yourself. 如果可能在您的用户上运行可能插入记录的进程,则可以将三条记录放入表中,然后从您自己中删除INSERT权限。 You'd still be able to alter the existing records but you wouldn't be able to add any more unless you explicitly re-granted the ability. 您仍然可以更改现有记录,但除非您明确重新授予该功能,否则您将无法再添加。

You can have a look here at the MAX_ROWS parameter . 您可以在这里查看MAX_ROWS参数 However, I believe this is normally used to make the table size larger than the disk size and I don't think you would get the restriction you are looking for using it. 但是,我相信这通常用于使表大小大于磁盘大小,我认为你不会得到你正在寻找使用它的限制。 Alternatively, you could just select the top 3 rows. 或者,您可以选择前3行。

I would question the point of using a database to only store 3 rows - it seems a total waste. 我会质疑使用数据库只存储3行 - 这似乎是一种浪费。

I think there is no such inbuilt functionality provided by MySQL. 我认为MySQL没有提供这样的内置功能。 One solution is that you can create trigger. 一种解决方案是您可以创建触发器。

CREATE TRIGGER your_trigger_name
BEFORE INSERT ON master-domain
FOR EACH ROW
BEGIN
    DECLARE cnt INT;

    SELECT count(*) INTO cnt FROM master-domain;

    IF cnt = 10 THEN
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'You can store only 3 records.';
    END IF;
END;

Try above trigger on your table. 尝试上面的触发器。 Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM