I'm trying to create a table and I can only have positive values for an INT, how would I do that?
CREATE TABLE Ingredients(
IngredientID INTEGER PRIMARY KEY NOT NULL,
IngredientName VARCHAR(255),
IngredientClassID SMALLINT NOT NULL,
MeasureAmountID SMALLINT NOT NULL
);
You can force the input to contain only positive integer by adding onkeypress within the input tag. Here, event. charCode >= 48 ensures that only numbers greater than or equal to 0 are returned, while the min tag ensures that you can come to a minimum of 1 by scrolling within the input bar.
An integer is positive if it is greater than zero, and negative if it is less than zero. Zero is defined as neither negative nor positive.
The abs() function is used to get the absolute (positive) value of a given number. The argument may be an integer or a floating point number.
try this...
CREATE TABLE Ingredients(
IngredientID INTEGER PRIMARY KEY,
IngredientName VARCHAR(255),
IngredientClassID SMALLINT NOT NULL,
MeasureAmountID SMALLINT NOT NULL,
CHECK (IngredientClassID>0),
CHECK (MeasureAmountID>0)
);
You can create a Check constraint for each column to accept only positive values.
CREATE TABLE Ingredients(
IngredientID INTEGER PRIMARY KEY constraint IngredientID_Positive
check (IngredientID >= 0),
IngredientName VARCHAR(255),
IngredientClassID SMALLINT NOT NULL,
MeasureAmountID SMALLINT NOT NULL
);
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