简体   繁体   中英

How do I set the default value for a column?

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))

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';

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.

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

alter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM