alter column with default value [duplicate] - sql

This question already has answers here:
Add a column with a default value to an existing table in SQL Server
(43 answers)
Alter column, add default constraint
(7 answers)
Closed 5 years ago.
alter table enquiry alter column RejectReson int default 0
I want alter the column data type with default value zero,but it is not happening.

ALTER TABLE enquiry ADD DEFAULT 0 FOR RejectReson

If you want to affect existing rows, then use with values:
alter table enquiry
add constraint dft_enquiry_rejectreason default 0 with values;
This is explained in the documentation:
. . . WITH VALUES can be used to store the default value in the new column for each existing row in the table.

Use ADD CONSTRAINT.
For example:
ALTER TABLE enquiry ADD CONSTRAINT some_name DEFAULT 0 FOR RejectReson;

Related

Incorrect syntax near default in alter table

I want to alter the existing column Site_SiteId in SQL Server to make it as not null with default value 1 but getting a syntax error:
ALTER TABLE dbo.ImagingEvents
ALTER COLUMN Site_SiteId bit NOT NULL DEFAULT 1
default is a constraint so you need to add it to the table:
ALTER TABLE dbo.ImagingEvents ADD DEFAULT 1 FOR Site_SiteId
First you need to ALTER the column:
ALTER TABLE dbo.ImagingEvents ALTER COLUMN Site_SiteId bit NOT NULL;
Note that if you have any rows that already have the value NULL you will need to UPDATE them first, before performing the ALTER.
Then, personally, I would recommend creating a named constraint, like so:
ALTER TABLE dbo.ImagingEvents ADD CONSTRAINT DF_Site_SiteId DEFAULT 1 FOR Site_SiteId;
Having named constraints, rather than the automatically named ones, is far better for transferable code.

create default value in oracle [duplicate]

This question already has answers here:
How to create id with AUTO_INCREMENT on Oracle?
(18 answers)
Closed 4 years ago.
How I can set default value in oracle? my query is like this :
alter table talend_job modify mtj_id varchar(10) not null default
('JASG'|| (nextval('mtj_id_seq')))
In Oracle 11g, you cannot specify a sequence number as a DEFAULT value for a column; however, you can emulate this functionality using a trigger. Even if a column is declared NOT NULL, you can still omit the column from INSERT statements to be populated in the trigger
You can refer to How to create id with AUTO_INCREMENT on Oracle?

How do i add a constraint in Oracle SQL to limit a value? [duplicate]

This question already has answers here:
SQL constraint minvalue / maxvalue?
(5 answers)
Closed 5 years ago.
How can i set a column in oracle to not accept numbers above 10000 and below 0?
You are looking for a check constraint:
alter table t add constraint chk_t_col check (col >= 0 and col <= 10000);
This will prevent inserting or updating any values in the column that are not in the specified range.

How do I add a column, with a default value, to an existing table in SQL Server 2008 or 2012? [duplicate]

This question already has answers here:
Add a column with a default value to an existing table in SQL Server
(43 answers)
Closed 9 years ago.
How do I add a column, with a default value, to an existing table in SQLServer 2008?
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
Add a column with a default value to an existing table in SQL Server
Use ALTER TABLE. For example
ALTER TABLE Table1
ADD col2 INT DEFAULT 0;
SQLFiddle

Upgrade a Database table to add a new column

I need to convert an existing db table to a new db table which adds a new column.
I have the following sql code which set the default value of this column to be -1.
// Insert new column for holding appWidgetIds
db.execSQL("ALTER TABLE favorites " +
"ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
But how can i set the default value of the column to increment one by one for each row?
It depends on your DBMS. MySQL uses AUTO_INCREMENT:
ALTER TABLE favorites ADD COLUMN appWidgetId INTEGER
NOT NULL DEFAULT -1 AUTO_INCREMENT
PostgreSQL uses CREATE SEQUENCE:
CREATE SEQUENCE favorites_seq;
ALTER TABLE favorites ADD COLUMN appWidgetId INTEGER
NOT NULL DEFAULT -1 nextval('favorites_seq')
SQL Server uses IDENTITY:
ALTER TABLE favorites ADD COLUMN appWidgetId INTEGER
IDENTITY(1,1) NOT NULL DEFAULT -1
As a side note, I don't know why you're setting your default as -1. Why would you make your integers signed if they will always be positive? You should make the default 0 for efficiency.
This depends on which DBMS you're using. For MySQL, you can use:
ALTER TABLE x ADD COLUMN y INTEGER NOT NULL DEFAULT -1 AUTO_INCREMENT
See http://dev.mysql.com/doc/refman/5.0/en/alter-table.html
Are values auto-incremented for new rows AFTER your conversion too? If not, you could do 3 steps:
ALTER the table with NULLable column
UPDATE the new column with whatever rules you have
ALTER again to set NOT NULL.