I have a problem. I created a table in Microsoft SQL and I would like one column to take not negative values. For example, EmployeeSalary column type is int and it hasn't a negative value.
33 Answers
Here is the solution of your problem:
Use CHECK while creating Table like this:
CREATE TABLE Table_name ( col1 int CHECK (col1 >= 0) ) OR
If you want to make a column to reject negative number, after creating the table then you can do like this:
ALTER TABLE Table_Name ADD CONSTRAINT constraint_name CHECK (col_name >= 0); If you want to set condition when creating the table try:
EmployeeSalary money CHECK (EmployeeSalary>= 0) If you just want to check during some script execution you can use if :
if(@Var > 0) begin .... end 1go tools>options>designer
"prevent changes that require table re-creation" this one should be unchecked.