Database column with default constraint - sql

I am creating a database column like this:
Alter table tablename
add column columnname null
add constraint df_columnname default 0
After executing above SQL, the new column is added to table with null values.
Does the constraint df_cloumnname have no meaning here?
Please clarify on this..

If your column is nullable, then adding it with a default constraint has no impact - it can be null, and will remain null. The DEFAULT CONSTRAINT in that case only applies to new rows that are being added (and that do not explicitly specify a value for your column).
If your column were NOT NULL, then the default constraint would be applied right away.
If you're using SQL Server (you didn't specify clearly enough - SQL is the query language - but not a database product...), and you want a nullable column witha default constraint and you want the value to be applied to the existing rows, use this syntax:
ALTER TABLE dbo.tablename
ADD columnname NULL
CONSTRAINT df_columnname DEFAULT 0 WITH VALUES
Add the WITH VALUES to your command and you should get the desired result.

Related

How do you add a Column with a Case as the default in SQLite?

ALTER TABLE [table_name] ADD COLUMN alloc_strat varchar(25) NOT NULL
DEFAULT(CASE
WHEN (shelf_life_unit = 'Months') THEN 'Min Remaining Shelf Life'
ELSE 'FIFO')
;
This isn't working, Is there a way to do this in one sql statement
In SQLite the added columns are not allowed to have expressions are their default.
The following illustrates the syntax of ALTER TABLE ADD COLUMN
statement:
ALTER TABLE table_name ADD COLUMN column_definition; Code language:
SQL (Structured Query Language) (sql) There are some restrictions on
the new column:
The new column cannot have a UNIQUE or PRIMARY KEY constraint. If the
new column has a NOT NULL constraint, you must specify a default value
for the column other than a NULL value. The new column cannot have a
default of CURRENT_TIMESTAMP, CURRENT_DATE, and CURRENT_TIME, or an
expression. If the new column is a foreign key and the foreign key
constraint check is enabled, the new column must accept a default
value NULL.
Source

Numeric Data type SQL

i want to add a new column to an existing table. I want to have a numeric data type and the default value of the column must be zero. So here is what i am trying.
ALTER TABLE COUNTRY
ADD MOBILE_ACTIVE NUMERIC(1,0) NOT NULL
and i am getting the following error
ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'MOBILE_ACTIVE' cannot be added to non-empty table 'COUNTRY' because it does not satisfy these conditions.
You are trying to create a not null column, in a table that already has data.
You will need to run
ALTER TABLE COUNTRY
ADD MOBILE_ACTIVE NUMERIC(1,0) NOT NULL
CONSTRAINT DF_MOBILE_ACTIVE DEFAULT 0 --create DEFAULT CONSTRANTI
WITH VALUES --must be used to populate the col for the existing rows
another note... why use numeric(1,0)? it just means you get a one-digit number
The proper data type for a true/false value is bit, and if you need a small integer there are fitting data types like tinyint
As #Sergey suggested in comments You should add default as in the error:
ALTER TABLE COUNTRY
ADD MOBILE_ACTIVE NUMERIC(1,0) NOT NULL DEFAULT 0
Or you should allow null as null is a non-value while 0 is a value of 0, so if you want it should have a value you should default it to 0, and if not you should allow null.
For dates you should use null, but for text and int some say never use null.
But one thing to note is, that by using default you add a constraint which will not allow you to drop the column without dropping the constraint (at least in SQL Server which I use, don't know about other), so you should add a constraint name, if you would want to be able to drop it.

How to add a not null constraint on column containing null values

I have a table with a column that contains a few null values. I want to add a NOT NULL constraint on that column without updating the existing nulls to a non-null value. I want to keep the existing null values and check for future rows that they contain a not null value for this column. Is this possible? How?
You can add an unvalidated constraint - it will not look at existing rows, but it will be checked for any new or updated rows.
ALTER TABLE mytable MODIFY mycolumn NOT NULL NOVALIDATE;
Just be aware that you won't be able to update an existing row unless it satisfies the constraint.
Also, be aware of the downside that the optimizer will not be able to take advantage of this constraint in making its plans - it has to assume that some rows may still have a null.
ALTER TABLE table_name
SET column_name = '0'
WHERE column_name IS NULL;
ALTER TABLE table_name
MODIFY COLUMN(column_name NUMBER CONSTRAINT constraint_identifier NOT NULL);
This is of course assuming that your column is a number but it's the same thing really, you would just change the '0' to a default value that isn't null.
Hammad:
I face the problem and solve like following:
Alter table thr_empl_info modify THR_EM_DESIGNATION_ID not null

How to add unique column in sybase?

I have a sybase db table in which i need to add a new column.
The conditions: The column must not allow nulls and be unique.
what is the alter table sql to achieve this?
EDIT:
It is a varchar type column.Yes the table as of now is empty, but when filled it is ensured that unique values would be filled in.
I tired executing
alter table Test add Name varchar not null unique
i get error saying default value must be specified as not null is given.
but i want to add unique constraint so do i really need to specify default?
thanks
Unique values are specified as part of an index on the column, not in the column definition itself.
Try:
alter table Test add Name varchar not null
create unique index index_name_unique on Test (Name)
The ASE reference manual can help with more detail.
Once a table has been created Sybase ASE does not allow addition of NOT NULL column directly unless a default is specified for the column. However, if the table is still empty you can do the following -
First add the new column as a NULL column to the table using alter table command -
alter table Test add Name varchar(100) null
Once this has been done, try modifying the same column Name in the table Test using the alter table script -
alter table Test modify Name varchar(100) NOT NULL
and you will see that you are able to modify the Name column to a NOT NULL column using these steps. This is because at this time Sybase server checks as there is no data in the table hence the NOT NULL constraint is not checked and the column is made NOT NULL. Hence, we are able to skip the default constraint.
In case there would have been some data already present in the table Test, then we need to add one more step in between steps 1 and 2 which will add default values to the existing rows in the table. This can be done via a script for previous data and then following the step 2.
To make the column only allow unique values for the column you need to add a unique key constraint using the following syntax -
alter table Test add constraint UK1 unique(Name)

Removing a default value from a Sql Server 2005 table

I have a tabel in an Sql Server 2005 database. I have the following column
IndPL INT DEFAULT 0 NULL
I want to change the column to be of the type NVARCHAR but I receive a constraint violation due to the fact that the column has a default value constraint attached to it.
I need to find out how to remove a default value constraint from a table column or how to change the column type without impeding the constraint.
Added :
I need to do it by using T-SQL in a script that will later be executed on an another machine.
You need to remove the constraint first. In Management Studio open the table, open the Contraints and click on the contraint in question. Delete it. Change the data-type and then add any necessary constraints.
Script:
ALTER TABLE YourTable
DROP CONSTRAINT Your_Contraint
ALTER TABLE YourTable
ALTER COLUMN IndPL NVARCHAR(150)