help with coloumn values

Thread Solved

Join Date: Nov 2007
Posts: 6
Reputation: madhura09 is an unknown quantity at this point 
Solved Threads: 0
madhura09's Avatar
madhura09 madhura09 is offline Offline
Newbie Poster

help with coloumn values

 
0
  #1
Jul 31st, 2009
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
God made the World
We program IT
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 433
Reputation: Ramesh S will become famous soon enough Ramesh S will become famous soon enough 
Solved Threads: 82
Ramesh S Ramesh S is offline Offline
Posting Pro in Training

Re: help with coloumn values

 
0
  #2
Aug 1st, 2009
Use check constraints. A check constraint allows you to specify a condition on each row in a table.

Try this
  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 2,093
Reputation: debasisdas will become famous soon enough debasisdas will become famous soon enough 
Solved Threads: 126
debasisdas's Avatar
debasisdas debasisdas is offline Offline
Postaholic

Re: help with coloumn values

 
0
  #3
Aug 1st, 2009
you nee to create a table with check constraint and default values.

try this

  1. CREATE TABLE table1
  2. (ID INT,
  3. status varchar2(10) DEFAULT 'Available',
  4. CONSTRAINT con_chk1 CHECK (status IN ('Available','Unavailable','CheckedOut')
  5. ))
Share your Knowledge.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 13
Reputation: anubina is an unknown quantity at this point 
Solved Threads: 1
anubina anubina is offline Offline
Newbie Poster

Re: help with coloumn values

 
0
  #4
Aug 1st, 2009
You can create the constraint at the moment of the creation of the table
  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:
  1. ALTER TABLE itself ADD CONSTRAINT ck_itself_status CHECK ((status IN ('Available','Unavailable','CheckedOut')));
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 20
Reputation: ferensick is an unknown quantity at this point 
Solved Threads: 4
ferensick ferensick is offline Offline
Newbie Poster

Re: help with coloumn values

 
0
  #5
Aug 7th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: madhura09 is an unknown quantity at this point 
Solved Threads: 0
madhura09's Avatar
madhura09 madhura09 is offline Offline
Newbie Poster

Re: help with coloumn values

 
0
  #6
Aug 7th, 2009
hey everyone
thanks a lot.
i think constraint is the way to go and its fitting perfectly with my database design.
thanks a lot
God made the World
We program IT
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC