943,590 Members | Top Members by Rank

Ad:
  • Oracle Discussion Thread
  • Marked Solved
  • Views: 877
  • Oracle RSS
Jul 31st, 2009
0

help with coloumn values

Expand Post »
hi
I have a coulumn named Status which can have only three values:
1. Available (Default)
2. Unavailable
3. CheckedOut

How can I add this functionality while creating the table? i.e. while creating the table itself how can i specify that the column can hold only these 3 values and populate all rows with the default value?
Please help
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
madhura09 is offline Offline
6 posts
since Nov 2007
Aug 1st, 2009
0

Re: help with coloumn values

Use check constraints. A check constraint allows you to specify a condition on each row in a table.

Try this
Oracle Syntax (Toggle Plain Text)
  1. CREATE TABLE YourTableName
  2. (
  3. COLUMN 1...,
  4. column2...,
  5. STATUS numeric(1),
  6. CONSTRAINT check_status
  7. CHECK (STATUS IN (1,2,3))
  8. );
Last edited by Ramesh S; Aug 1st, 2009 at 4:00 am.
Reputation Points: 165
Solved Threads: 113
Posting Pro
Ramesh S is offline Offline
580 posts
since Jun 2009
Aug 1st, 2009
0

Re: help with coloumn values

you nee to create a table with check constraint and default values.

try this

sql Syntax (Toggle Plain Text)
  1. CREATE TABLE table1
  2. (ID INT,
  3. status varchar2(10) DEFAULT 'Available',
  4. CONSTRAINT con_chk1 CHECK (status IN ('Available','Unavailable','CheckedOut')
  5. ))
Featured Poster
Reputation Points: 665
Solved Threads: 427
Posting Genius
debasisdas is offline Offline
6,406 posts
since Feb 2007
Aug 1st, 2009
0

Re: help with coloumn values

You can create the constraint at the moment of the creation of the table
Oracle Syntax (Toggle Plain Text)
  1. DROP TABLE itself;
  2. CREATE TABLE itself (
  3. status VARCHAR2(11) DEFAULT 'Available' CHECK (status IN ('Available','Unavailable','CheckedOut'))
  4. );

Or you can create the constraint on the fly:
Oracle Syntax (Toggle Plain Text)
  1. ALTER TABLE itself ADD CONSTRAINT ck_itself_status CHECK ((status IN ('Available','Unavailable','CheckedOut')));
Reputation Points: 10
Solved Threads: 1
Newbie Poster
anubina is offline Offline
17 posts
since Jun 2009
Aug 7th, 2009
0

Re: help with coloumn values

Pretty much as mentioned the constraints are the way to go. You can use a lookup table to easily do this as well. So you have your lkp_Status table, just with those 3 values. And you can create a primary foreign key constraint as well. This way in the future if a value is added and there needs to be 4 values you can just add the value in the lookup table and the primary foreign key relationship remains the same. It doesn't need to change.
Reputation Points: 33
Solved Threads: 4
Newbie Poster
ferensick is offline Offline
20 posts
since Feb 2008
Aug 7th, 2009
0

Re: help with coloumn values

hey everyone
thanks a lot.
i think constraint is the way to go and its fitting perfectly with my database design.
thanks a lot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
madhura09 is offline Offline
6 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Oracle Forum Timeline: vista DSN name oracle odbc driver missing
Next Thread in Oracle Forum Timeline: Internal Server Error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC