954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Check Constraints

Hi everyone,

I've been trying different commands that check inputs that will be entered into the tables I've got. I want to check that Amount/Ename columns can not be NULL.

What's <> do in SQL?

// first command
 ALTER TABLE Emp ADD CONSTRAINT CK_AMOUNT CHECK ( AMOUNT NOT NULL);

 // second command
 ALTER TABLE Emp ADD CONSTRAINT CK_AMOUNT CHECK ( AMOUNT != NULL);


They accept null values and the default value is 0

//first command
 ALTER TABLE Emp ADD CONSTRAINT CK_ENAME CHECK ( Ename NOT NULL);

// second command
 ALTER TABLE Emp ADD CONSTRAINT CK_ENAME CHECK ( Ename != NULL);

I also want my tables to accept specific values, for example, payment method has to be one of these options such as master card, visa or cash.

ALTER TABLE Emp ADD CONSTRAINT CK_Pay CHECK ( Pay="master card" OR "visa" OR "cash");

Thanks for the help..

rotten69
Posting Whiz
346 posts since May 2011
Reputation Points: 3
Solved Threads: 16
 

Try these out...

Alter Table Emp Modify (Amount NOT Null);
Alter Table Emp Modify (EName NOT Null);
Alter Table Emp Add Constraint CK_Pay Check (Pay in ('master card', 'visa', 'cash'));

Of course, the data in the tables have to already be valid for the constraints to work.
It is better to put your constraints on when you create the table.

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You