Home » SQL & PL/SQL » SQL & PL/SQL » How to give Check Constraint
How to give Check Constraint [message #20717] Fri, 14 June 2002 20:34 Go to next message
karunamoorthy_p
Messages: 13
Registered: April 2002
Junior Member
I have the following table:

Employee(e_no, e_name, basic_pay, deptno)

How to give constraint for
minimum basic_pay is 10000 and maximum is 50000.
Re: How to give Check Constraint [message #20722 is a reply to message #20717] Sat, 15 June 2002 04:45 Go to previous messageGo to next message
sureshraj
Messages: 9
Registered: May 2002
Junior Member
Employee(e_no, e_name, basic_pay, deptno)

How to give constraint for
minimum basic_pay is 10000 and maximum is 50000.

answer:
create table employee(e_no number,e_name varchar2(15),basic_pay number(8,2) constraint ck check(basic_pay <10000 and basic_pay >50000),deptno number(4));
Re: How to give Check Constraint [message #20732 is a reply to message #20717] Sun, 16 June 2002 13:18 Go to previous messageGo to next message
Su
Messages: 154
Registered: April 2002
Senior Member
There is a syntax error in your solution. The 'constraint' clause is not allowed at column level, it is allowed for table level. And the solution is not correct as it checks whether the basic pay is less than 10000 and greater than 50000 same time, which is not possible all at once. The correct one is the following. And instead of giving < and > better to use BETWEEN operator.
SQL> r
1 create table employee (
2 e_no number not null,
3 e_name varchar2(20),
4 basicpay number(8,2) check (basicpay between 10000 and 50000),
5* depno integer)

Table created.

The basic pay should be in between 10000 (minimum) and 50000 (maximum). Check it out.

Good luck :)
Re: How to give Check Constraint [message #20774 is a reply to message #20717] Wed, 19 June 2002 16:34 Go to previous message
Siva Ram
Messages: 22
Registered: November 2001
Junior Member
Hi

The best method to do this is:

SQL> Create table employee(e_no number,e_name varchar2(15),basic_pay number(8,2) constraint chkbasic check(basic_pay between 10000 and 50000),deptno number(4));

Thank You
Siva Ram
Previous Topic: Subselect in the Select clause
Next Topic: WITH READ ONLY
Goto Forum:
  


Current Time: Thu Apr 18 06:26:50 CDT 2024