Let me produce a table that has an integer primary key limited between 000 and 999. Can there be in whatever way to enforce this 3 digit limit inside the sql?

I am using sqlite3. Thanks.

You might have the ability to achieve this utilizing a CHECK constraint.


CHECK constraints are supported by version 3.3.. Just before version 3.3., CHECK constraints were parsed although not enforced.

(from here)

So unless of course SQLite 3 = SQLite 3.3 this most likely will not work

SQLite supports two methods for carrying this out:

Define a CHECK constraint around the primary key column:

CREATE TABLE mytable (
  mytable_id INT PRIMARY KEY CHECK (mytable_id BETWEEN 0 and 999)

Produce a trigger up for grabs that aborts any INSERT or UPDATE that tries to set the main key column to some value you wouldn't want.

CREATE TRIGGER mytable_pk_enforcement
  WHEN mytable_id NOT BETWEEN 0 AND 999
  RAISE(ABORT, 'primary key out of range');

If you are using a car-designated primary key, as proven above, you may want to run the trigger AFTER INSERT rather than before place. The main key value might not be produced yet at that time the BEFORE trigger executes.

You may even have to write a trigger on UPDATE to avoid people from altering the worthiness outdoors the number. Essentially, the CHECK constraint is more suitable if you are using SQLite 3.3 or later.

note: I haven't examined the code above.

jmisso, I wouldn't recommend re-using primary secrets which have been erased. You may create data integrity issues that way if other tables that may obtain that type in them weren't erased first (one good reason to continually enforce establishing foreign key associations inside a database to avoid orphaned data such as this). Don't do that unless of course you're positive you have no orphaned data that may get connected to the new record.

Why can you even wish to limit the main answer to 1000 possible values? What goes on when you really need 1500 records within the table? This does not strike me like a very positive thing to be also attempting to do.

Indeed while using CHECK constraint it will not allow amounts outdoors that range, but that did not work the same as I needed (and I apologize I did not point out that), because I needed it to make use of the very first available primary key (freed by erased rows) after it tries the 1000th key.

Just solutions, I'll consider the TRIGGER option.

HLGEM, you are most likely right. I had been attempting to enforce that because that secret is a part of a bigger 7-digit code, that we can't let it grow bigger. Most likely this table will not even achieve 500 rows over a couple of years, as it is for any small store. Allows hope that for now, they are able to manage to get RFIDs.

Why do an integer? You stated it was between 000 and 999, therefore it seems like you are not really doing math around the integer.

When not something you are doing arithmetic with, you ought to be using figures.

How about pre-inhabiting the table using the 1000 rows in the beginning. Toggle the accessible rows with a few type of 1/ column like Is_Available or similar. Then do not let card inserts or removes, only updates. Under this your application only needs to be coded for updates.