[Solved] How to limit number of entries in MySQL database?


You probably can’t get it right in PHP since the trip back and forth to the database leaves room for another part of your application to create an entry. Normally we achieve this sort of thing by putting a unique index on the table that prevents duplication of data. For example:

CREATE TABLE alf_mimetype
(
   id BIGINT NOT NULL AUTO_INCREMENT,
   version BIGINT NOT NULL,
   mimetype_str VARCHAR(100) NOT NULL,
   PRIMARY KEY (id),
   UNIQUE (mimetype_str)
) ENGINE=InnoDB;

If you attempt to insert a row with duplicate mimetype_str, the database will generate an exception. Catch it in your application and you’ll know that your single entry for that particular row is already there.

You can create UNIQUE keys on multiple columns as well. Your primary key also represents a unique constraint and can consist of multiple columns.

0

solved How to limit number of entries in MySQL database?