0

I would like to know how to create a default value.

For example:

CREATE TABLE something 
(
name varchar(20),
rank int
)

i would like to set the RANK value to 1.

(whenever i add new records, rank is automatically set to 1).

Biker John
  • 2,621
  • 10
  • 33
  • 52
  • 2
    There are lots and lots of questions on this topic here at SO, e.g. http://stackoverflow.com/questions/5242990/default-value-for-a-text-column – Till Helge Nov 21 '11 at 14:54
  • possible duplicate of [How do you set a default value for a MySQL Datetime column?](http://stackoverflow.com/questions/168736/how-do-you-set-a-default-value-for-a-mysql-datetime-column) – Johan Nov 21 '11 at 15:17

2 Answers2

6
CREATE TABLE something 
(
    name varchar(20),
    rank int default 1
)
勿绮语
  • 9,170
  • 1
  • 29
  • 37
1

If the table already exists and you want to add the default value the fastest way to do it is with ALTER TABLE like this:

ALTER TABLE something
  ALTER COLUMN rank SET DEFAULT 1;
Ike Walker
  • 64,401
  • 14
  • 110
  • 109