0

Hi I am trying to use the CHECK constraint to stop one row from being larger than the other.

create table myTable (
begin int(10),
end int(10),
check (begin < end)
);

The table is created however there is no constraint being applied when inserting rows.

Any help on what I'm doing wrong would be great.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • is this in SQL Server? I'm just quite unsure how'd you successfullly created the table with int(10). Anyway you can check the constraint created using 'sp_help myTable'. – hallie Jun 05 '13 at 03:43
  • What RDBMS are you using (MySql, SQL Server, Oracle etc.)? – peterm Jun 05 '13 at 04:08
  • @hallie oracle is what im using – user2447832 Jun 05 '13 at 04:53
  • Can you give an example of inserts that should be rejected but aren't? Your current table definition allows `NULL`s, and `NULL` treatment by constraints can surprise some people, if that's a factor. – Damien_The_Unbeliever Jun 05 '13 at 06:04

1 Answers1

1

You have to give a name to the constraint.

CREATE TABLE myTable
(
   begin   NUMBER (10),
   end     NUMBER (10),
   CONSTRAINT constr_begin_end CHECK (begin < end)
);

Also, begin and end are keywords in Oracle. Avoid this in column names, variable names.

Noel
  • 10,152
  • 30
  • 45
  • 67