Check Constraints

In Oracle, Check Constraints have the specific condition for each row of the table.

Using a CREATE TABLE statement

Syntax:

snippet
CREATE TABLE table_name
(
  column1 datatype null/not null,
  column2 datatype null/not null,
  ?
  CONSTRAINT constraint_name CHECK (column_name condition) [DISABLE]
);

Example 1

snippet
CREATE TABLE student (id numeric(4), name varchar2(50), CONSTRAINT check_id CHECK(id  BETWEEN 1 and 10))
Check Constraints in Oracle
Note
Note : During creating the table we have applied a constraint, in which only 1 to 10 rows can be inserted. So, in below query 12 fields are inserted. If will generate an error message.
snippet
INSERT ALL
  INTO student(id, name) VALUES (1, 'shristee')
  INTO student(id, name) VALUES (2, 'heena')
  INTO student(id, name) VALUES (3, 'mohit')
  INTO student(id, name) VALUES (4, 'shashank')
  INTO student(id, name) VALUES (5, 'avinash')
  INTO student(id, name) VALUES (6, 'shweta')
  INTO student(id, name) VALUES (7, 'suman')
  INTO student(id, name) VALUES (8, 'rohan')
  INTO student(id, name) VALUES (9, 'ali')
  INTO student(id, name) VALUES (10, 'dolly')
  INTO student(id, name) VALUES (11,?mona?)
  INTO student(id, name) VALUES (12, 'kiran')
  SELECT * FROM dual;
Check Constraints in Oracle
snippet
INSERT ALL
  INTO student(id, name) VALUES (1, 'shristee')
  INTO student(id, name) VALUES (2, 'heena')
  INTO student(id, name) VALUES (3, 'mohit')
  INTO student(id, name) VALUES (4, 'shashank')
  INTO student(id, name) VALUES (5, 'avinash')
  INTO student(id, name) VALUES (6, 'shweta')
  INTO student(id, name) VALUES (7, 'suman')
  INTO student(id, name) VALUES (8, 'rohan')
  INTO student(id, name) VALUES (9, 'ali')
  INTO student(id, name) VALUES (10, 'dolly')
  SELECT * FROM dual;
Check Constraints in Oracle
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents