tags:

views:

179

answers:

3

Note the table below. I am wanting to set the default value for the newly created BEST_SELLER column to "N".
How do I go about doing that?

Create Table Mystery
(Book_Code Char(4) Primary Key,
 Title Varchar2(40),
 Publisher_Code Char(2),
 Price Number(4,2))
+3  A: 

Basic MySQL Alter Table command

If the column doesn't exist:

alter table Mystery add column BEST_SELLER enum('N','Y') default 'N';

and if column exists:

alter table Mystery alter column BEST_SELLER set default 'N';
Jaan J
A: 

If you added the column after the table was created you can do something similar with the alter statement.

alter

Carter
+3  A: 

I am adding a second answer because of your response to my first answer. This answer applies since:

  1. You are using Oracle, and
  2. You have already created the table, so you need to use "ALTER TABLE" syntax.

Please find enclosed the following:

alter table
    mystery
modify
    BEST_SELLER char(1) DEFAULT 'N'

Please modify the type char(1) to whatever the column actually is. After running this query to correct the table, you will need to issue a second query to update the existing rows, such as:

UPDATE 
    mystery 
SET 
    BEST_SELLER = 'N' 
WHERE 
       BEST_SELLER = '' 
    OR BEST_SELLER IS NULL

Hope this helps.

Dereleased
Will that then populate existing rows?
Murph
Nothing at first, you will need to issue a general purpose update query like: `UPDATE mystery SET BEST_SELLER = 'N' WHERE BEST_SELLER = '' OR BEST_SELLER IS NULL` (that's based on my knowledge of MySQL syntax, although I think it's generic enough to carry to oracle), but the default value should future-proof you in this situation.
Dereleased
THANKS WORKED WONDERFULLY!
Michael
aww, lost my 'accepted' mark
Dereleased