Domanda

I have a column that is called NumberInStock. However, in the database I want to limit the values that can be entered into this column between 1 and 100. What Constraint would I use on the column and would would be the proper syntax of the SQL query? What I got so far is:

ALTER TABLE Inventory
ALTER COLUMN NumberInStock 

Now what do I put after that to put the constraint and what constraint would I use

I'm using MySQL that has a 1999 ANSI/ISO standard.

È stato utile?

Soluzione

Usually this type of problem should be solved using a CHECK constraint as follows-

ALTER TABLE Inventory ADD CONSTRAINT chk1 CHECK(NumberInStock BETWEEN 1 AND 100);

This could have been done in ORACLE But MySQL has a BUG reported regarding the implementation of check constraint.

Hence u must implement two triggers i.e. one for INSERT and another for UPDATE on that table.

Altri suggerimenti

Use Check constraint .

ALTER TABLE <TABLE NAME>
ADD CONSTRAINT <CONSTRAINT NAME> CHECK(<PREDICATE> OR/AND/NOT <PREDICATE> ...)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top