Problem solve Get help with specific problems with your technologies, process and projects.

CHECK constraints

SQL expert Rudy Limeback explains how to use a CHECK constraint.

How can I create a constraint to not allow a date prior to 1 Jan 2007 to be entered?
This is accomplished with a CHECK constraint. Here's an example:

create table documents
( id    integer     not null primary key 
, title varchar(99) not null
, added date        not null
, constraint only_new_ones 
     check ( added >= '2007-01-01' )         

The CHECK constraint should always be given a descriptive name, because this helps identify the source of the error when the constraint is violated. The constraint name is optional, but the difference is between getting an error message that looks like this:

INSERT statement terminated. CHECK constraint "CK__documents__added__73852659" was violated.

and one that looks like this:

INSERT statement terminated. CHECK constraint "only_new_ones" was violated.

Use a constraint name that describes the constraint accurately.

This was last published in March 2008

Dig Deeper on Oracle development languages



Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.