Alter Table to Modify Default Value of Column

Alter table to modify default value of column

Your belief about what will happen is not correct. Setting a default value for a column will not affect the existing data in the table.

I create a table with a column col2 that has no default value

SQL> create table foo(
2 col1 number primary key,
3 col2 varchar2(10)
4 );

Table created.

SQL> insert into foo( col1 ) values (1);

1 row created.

SQL> insert into foo( col1 ) values (2);

1 row created.

SQL> insert into foo( col1 ) values (3);

1 row created.

SQL> select * from foo;

COL1 COL2
---------- ----------
1
2
3

If I then alter the table to set a default value, nothing about the existing rows will change

SQL> alter table foo
2 modify( col2 varchar2(10) default 'foo' );

Table altered.

SQL> select * from foo;

COL1 COL2
---------- ----------
1
2
3

SQL> insert into foo( col1 ) values (4);

1 row created.

SQL> select * from foo;

COL1 COL2
---------- ----------
1
2
3
4 foo

Even if I subsequently change the default again, there will still be no change to the existing rows

SQL> alter table foo
2 modify( col2 varchar2(10) default 'bar' );

Table altered.

SQL> select * from foo;

COL1 COL2
---------- ----------
1
2
3
4 foo

SQL> insert into foo( col1 ) values (5);

1 row created.

SQL> select * from foo;

COL1 COL2
---------- ----------
1
2
3
4 foo
5 bar

How to alter a column and change the default value?

ALTER TABLE foobar_data MODIFY COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';

A second possibility which does the same (thanks to juergen_d):

ALTER TABLE foobar_data CHANGE COLUMN col col VARCHAR(255) NOT NULL DEFAULT '{}';

How to set a default value for an existing column

This will work in SQL Server:

ALTER TABLE Employee ADD CONSTRAINT DF_SomeName DEFAULT N'SANDNES' FOR CityBorn;

Alter column default value

I think issue here is with the confusion between Create Table and Alter Table commands.
If we look at Create table then we can add a default value and default constraint at same time as:

<column_definition> ::= 
column_name <data_type>
[ FILESTREAM ]
[ COLLATE collation_name ]
[ SPARSE ]
[ NULL | NOT NULL ]
[
[ CONSTRAINT constraint_name ] DEFAULT constant_expression ]
| [ IDENTITY [ ( seed,increment ) ] [ NOT FOR REPLICATION ]
]
[ ROWGUIDCOL ]
[ <column_constraint> [ ...n ] ]
[ <column_index> ]
ex:
CREATE TABLE dbo.Employee
(
CreateDate datetime NOT NULL
CONSTRAINT DF_Constraint DEFAULT (getdate())
)
ON PRIMARY;

you can check for complete definition here:
http://msdn.microsoft.com/en-IN/library/ms174979.aspx

but if we look at the Alter Table definition then with ALTER TABLE ALTER COLUMN you cannot add
CONSTRAINT the options available for ADD are:

 | ADD 
{
<column_definition>
| <computed_column_definition>
| <table_constraint>
| <column_set_definition>
} [ ,...n ]

Check here: http://msdn.microsoft.com/en-in/library/ms190273.aspx

So you will have to write two different statements one for Altering column as:

ALTER TABLE MyTable ALTER COLUMN CreateDate DATETIME NOT NULL;

and another for altering table and add a default constraint

ALTER TABLE MyTable ADD CONSTRAINT DF_Constraint DEFAULT GetDate() FOR CreateDate;

Hope this helps!!!

Alter default value of column in mssql

Alter table does not have an option to supply a default constraint so you have to issue two commands; your first to alter the column, then you can create its default constraint:

alter table [Table] add constraint DF_ColumnName default 'y' for [column];

MySQL - How to modify column default value?

Use ALTER TABLE to CHANGE or MODIFY the DEFAULT value of column. Check this link ALTER TABLE SYNTAX

ALTER TABLE `tableName` CHANGE `columnName` `columnName` DATE DEFAULT '0000-00-00'; 
ALTER TABLE `tableName` MODIFY `columnName` DATE DEFAULT '0000-00-00';

How to alter the default value of a column using a procedure

You need to use Dynamic SQL here, as Default clause in the Alter Table will not be able to resolve the variable value:

DELIMITER $$
CREATE PROCEDURE updateDefaultUserRole(
IN rid_in INT
) BEGIN

-- generate the query string for Alter Table
SET @alter_query_str = CONCAT('ALTER TABLE _users
MODIFY rid INT(255) NOT NULL
DEFAULT ',
rid_in); -- Modify the columns default value
-- prepare the query
PREPARE stmt FROM @alter_query_str;
-- execute the query
EXECUTE stmt;
-- deallocate the query
DEALLOCATE PREPARE stmt;

UPDATE _users SET rid = rid_in
WHERE rid < rid_in; -- Update all entries lower than the role ID.

END $$
DELIMITER ;

How to properly alter table default value

The CHANGE COLUMN alteration is used when you might want to change the name of the column, and requires you to provide the new name after the old name. If you're not renaming the column, you have to provide the name twice. Your command tries to rename the date_u column to DATETIME, and it's missing the datatype before the NULL keyword.

Use MODIFY COLUMN instead. It's the same, but doesn't allow renaming, so doesn't require you to give the column name twice.

ALTER TABLE `mydb`.`orders` MODIFY COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP;

I'm also not sure what you intended with '{}' at the end, but I don't think it's valid syntax, either, so I've removed it.



Related Topics



Leave a reply



Submit