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.

3

3 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 
1

go tools>options>designer

"prevent changes that require table re-creation" this one should be unchecked.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.