* If a constraint only applies to individual fields, you can define the constraint at the field level or at the table level. However, if a constraint acts on multiple fields,
Constraints must be defined at the table level
* When defining a constraint, you can use the constraint keyword to name the constraint. If not specified, oracle automatically creates a default name for the constraint.
SQL>
SQL> create table emp (empno number (4) not null,
2 ename varchar2 (10 ),
3 job varchar2 (9 ),
4 mgr number (4 ),
5 hiredate date,
6 sal number (7, 2 ),
7 comm number (7, 2 ),
8 deptno number (2 ));
Table created.
SQL>
SQL> insert into emp values (7369, 'Smith ', 'cler', 7902, to_date ('17-dec-1980', 'dd-mon-yyyy'), 800, null, 20 );
1 row created.
SQL> insert into emp values (7499, 'allen', 'salesman', 7698, to_date ('20-feb-1981 ', 'dd-mon-yyyy'), 1600,300, 30 );
1 row created.
SQL> insert into emp values (7521, 'ward ', 'salesman', 7698, to_date ('22-feb-1981', 'dd-mon-yyyy'), 1250,500, 30 );
1 row created.
SQL> insert into emp values (7566, 'Jones ', 'manager', 7839, to_date ('2-apr-1981', 'dd-mon-yyyy '), 2975, null, 20 );
1 row created.
SQL> insert into emp values (7654, 'martin ', 'salesman', 7698, to_date ('28-sep-1981', 'dd-mon-yyyy'), 1250,140 0, 30 );
1 row created.
SQL> insert into emp values (7698, 'bucke', 'manager', 7839, to_date ('1-may-1981 ', 'dd-mon-yyyy'), 2850, null, 30 );
1 row created.
SQL> insert into emp values (7782, 'clark', 'manager', 7839, to_date ('9-jun-1981 ', 'dd-mon-yyyy'), 2450, null, 10 );
1 row created.
SQL> insert into emp values (7788, 'Scott ', 'analyst', 7566, to_date ('09-dec-1982', 'dd-mon-yyyy'), 3000, null, 20 );
1 row created.
SQL> insert into emp values (7839, 'King', 'President ', null, to_date ('17-nov-1981', 'dd-mon-yyyy '), 5000, null, 10 );
1 row created.
SQL> insert into emp values (7844, 'turner ', 'salesman', 7698, to_date ('8-sep-1981', 'dd-mon-yyyy'), 1500, 0, 30 );
1 row created.
SQL> insert into emp values (7876, 'adams', 'cler', 7788, to_date ('12-jan-1983 ', 'dd-mon-yyyy'), 1100, null, 20 );
1 row created.
SQL> insert into emp values (7900, 'James ', 'cler', 7698, to_date ('3-dec-1981', 'dd-mon-yyyy '), 950, null, 30 );
1 row created.
SQL> insert into emp values (7902, 'Ford ', 'analyst', 7566, to_date ('3-dec-1981', 'dd-mon-yyyy '), 3000, null, 20 );
1 row created.
SQL> insert into emp values (7934, 'miller ', 'cler', 7782, to_date ('23-jan-1982', 'dd-mon-yyyy'), 1300, null, 10 );
1 row created.
SQL>
SQL> select * from emp;
Empno ename job mgr hiredate sal comm deptno
------------------------------------------------------------------------------
7369 smith clerk 7902 17-dec-80 800 20
7499 allen salesman 7698 20-feb-81 1600 300 30
7521 ward salesman 7698 22-feb-81 1250 500 30
7566 jones manager 7839 02-apr-81 2975 20
7654 martin salesman 7698 28-sep-81 1250 1400 30
7698 blake manager 7839 01-may-81 2850 30
7782 clark manager 7839 09-jun-81 2450 10
7788 scott analyst 7566 09-dec-82 3000 20
7839 king president 17-nov-81 5000 10
7844 turner salesman 7698 08-sep-81 1500 0 30
7876 adams clerk 7788 12-jan-83 1100 20
Empno ename job mgr hiredate sal comm deptno
------------------------------------------------------------------------------
7900 james clerk 7698 03-dec-81 950 30
7902 ford analyst 7566 03-dec-81 3000 20
7934 miller clerk 7782 23-jan-82 1300 10
14 rows selected.
SQL>
SQL> alter table emp
2 add (
3 gender varchar (10 ));
Table altered.
SQL>
SQL> alter table emp
2 add constraint ck_gender
3 check (gender in ('male', 'female '));
Table altered.
SQL>
SQL> update emp set gender = 'male' where mod (empno, 2) = 0;
10 rows updated.
SQL>
SQL> select * from emp;
Empno ename job mgr hiredate sal comm deptno gender
----------------------------------------------------------------------------------------
7369 smith clerk 7902 17-dec-80 800 20
7499 allen salesman 7698 20-feb-81 1600 300 30
7521 ward salesman 7698 22-feb-81 1250 500 30
7566 jones manager 7839 02-apr-81 2975 20 male
7654 martin salesman 7698 28-sep-81 1250 1400 30 male
7698 blake manager 7839 01-may-81 2850 30 male
7782 clark manager 7839 09-jun-81 2450 10 male
7788 scott analyst 7566 09-dec-82 3000 20 male
7839 king president 17-nov-81 5000 10
7844 turner salesman 7698 08-sep-81 1500 0 30 male
7876 adams clerk 7788 12-jan-83 1100 20 male
Empno ename job mgr hiredate sal comm deptno gender
----------------------------------------------------------------------------------------
7900 james clerk 7698 03-dec-81 950 30 male
7902 ford analyst 7566 03-dec-81 3000 20 male
7934 miller clerk 7782 23-jan-82 1300 10 male
14 rows selected.
SQL>
SQL> update emp set gender = 'female' where mod (empno, 2) = 1;
4 rows updated.
SQL>
SQL> select ename, job, gender from emp order by gender, ename;
Ename job gender
-----------------------------
Allen salesman female
King president female
Smith clerk female
Ward salesman female
Adams clerk male
Blake manager male
Clark manager male
Ford analyst male
James clerk male
Jones manager male
Martin salesman male
Ename job gender
-----------------------------
Miller clerk male
Scott analyst male
Turner salesman male
14 rows selected.
SQL>
SQL> insert into emp (empno, ename, gender) values (8001, 'pat ', 'unknown ');
Insert into emp (empno, ename, gender) values (8001, 'pat ', 'unknown ')
*
Error at line 1:
Ora-02290: check constraint (sys. ck_gender) violated
SQL>
SQL> drop table emp;
Table dropped.