1 Replies - 369 Views - Last Post: 29 November 2011 - 08:03 AM Rate Topic: -----

Topic Sponsor:

#1 Andreika_86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 20-November 11

Check constraint

Posted 27 November 2011 - 07:19 PM

Hello everyone,

I have a small difficulty writing a constraint, for example if I had a row series and it had to be between s00 and s99, how do I do it? I use SQL developer. Thanks a lot!!!
Is This A Good Question/Topic? 0
  • +

Replies To: Check constraint

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1754
  • View blog
  • Posts: 2,685
  • Joined: 08-June 10

Re: Check constraint

Posted 29 November 2011 - 08:03 AM

I take it you are using Oracle then?


What exactly does the "s" in those values stand for? It's quite a bit easier to validate a number than it is to validate the format of a string. Especially when you don't just have to validate the string format, but also extract and validate a part of the string as a separate value.

I would suggested you remove the "s" and save the value as a number. Then you could restrict it using a very simple CHECK constraint.
CREATE TABLE example (
	some_id NUMBER PRIMARY KEY,
	the_column NUMBER NOT NULL
		CONSTRAINT check_the_column
		CHECK (the_column BETWEEN 0 AND 99)
);



If you need the value to read "sNN" on the way out, you can compile that string in your SQL queries using the number value stored in the table.
SELECT 
	some_id,
	's' || LPAD(the_column, 2, '0')
FROM
	example;



Check out "Check Constraints" in the page about constraints in the manual.
http://docs.oracle.c....htm#SQLRF52180
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1