I created a table, master-domain. This table should only have 3 records.
How can I limit mysql database to only allow NO MORE than that number of records?
Is there a specific sql command to acomplish this?
This is my current SQL:
CREATE TABLE `mydatabase`.`master-domain`
(
`domain` VARCHAR( 50 ) NOT NULL COMMENT 'Domain Name',
PRIMARY KEY ( `domain` )
)
PS. I have godaddy and it includes phpMyAdmin, in addition to MySQL databases.
You can make a table's primary key a field of type 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. 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.
If you want to replicate the current restriction that domain names have to be unique, you could also add unique key (domain).
* note that the empty string is allowed (and not the same as NULL) because enum is actually a type of string. So you'd specify two permitted ID values in order to have three total.
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. 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. 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.
I would question the point of using a database to only store 3 rows - it seems a total waste.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With