How to add unique column in sybase? - sql

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)

Related

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.

Can´t add field without foreign key

I have two database one local and other in production.
In one of them I have column IdNivelDominio without FK but if I open Constraints folder and I have something like:
DF_EvaluacionDetalleCompetenciasFuncionales_IdNivelDominio
So I want to reply this field into my another database as:
ALTER TABLE Reclutamiento.EvaluacionDetalleCompetenciasFuncionales
ADD IdNivelDominio INT NOT NULL;
But I get 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 'IdNivelDominio' cannot be added to non-empty
table 'EvaluacionDetalleCompetenciasFuncionales' because it does not
satisfy these conditions.
Problem is that field is not linked with a foreign key (into original table) so I can´t add constraint.
Can anyone explain me how it occurs? or there any way to do constraint without foreign key? Regards
ALTER TABLE Reclutamiento.EvaluacionDetalleCompetenciasFuncionales
ADD IdNivelDominio INT NOT NULL DEFAULT 0;
You're trying to add a new column that should be NOT NULL to table with data. You need to specify what value will be in that new column because it's NOT NULL

How to add not null unique column to existing table

Is there any way to simply add not null unique column to existing table. Something like default = 1++ ? Or simply add unique column?
I tried to add column and then put unique contrain but MS SQL says that:
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name (...) The duplicate key value is ( < NULL > ).
Any way to simply add column to existing working table with unique constrain? Should MS SQL really think that null IS a value?
Add not null column with some default.
Update column to be a sequential integers (see row_number() function)
Add UNIQUE constraint or UNIQUE index over new column
You can add IDENTITY column to a table (but from question it is not clear if you need it or not).
IDENTITY is all you need:
ALTER TABLE TestTable
ADD ID INT IDENTITY(1, 1)
If you want an autonumber, you can add an identity.
If you need to populate the values yourself, you add the column allowing nulls, update the values, check to make sure they are unique and that there are no nulls, then add the unique constraint and trhe not null property. It is best to do this during a maintenance window when no one else would be changing data in that table.

SQL Server : trying to add non-null column to an existing table (table is empty)

I'm trying to add a new column to my exiting table but I keep getting an error trying to insert non-null columns.
I understand how this would be an issue to existing databases with lots of data, but my database has no data yet so I'm confused as to why I'm getting this error.
Is there an easy solution for this (I don't want to add default values)
You've got two options, feed it a default value, or add the field without the non-null constraint, then alter the table later to add the non-null constraint after you've populated the field.
This works fine when the table is empty.
CREATE TABLE X(Y INT)
ALTER TABLE T ADD Y INT NOT NULL
There is no need to add a temporary default constraint or create it as NULL, populate then ALTER except if the table is, in fact, not empty as claimed.
I don't think that table is as empty as you think it is.
Try this:
Truncate Table <YourTableName>
Then try the alter table statement.

SQL: Create new column with default, unique value

I have added a new column, called Ordinal, to a table called Activity. The problem is that I gave it a UNIQUE constraint, set it to allow NULL (though this I won't want in the end.. I just needed to set it to that to get a little farther with the script), and did not give it a default value. I'm now running a RedGate SQL Compare script that was generated by comparing this table to a version of the Activity table that does not have the column. But I'm getting the following error:
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'iwt.Activity' and the index name 'IX_Activity'. The duplicate key value is (1).
So based on my research, it's trying to create a unique key constraint on the Ordinal column, but NULL is not unique. So my next step was to give it a unique value of 1 just to let the script pass. But 1 isn't going to be unique either. So, finally, my question:
Preferably in SQL Server Management Studio, how do I set a column as having a unique default value? Isn't that what I would need to create this constraint?
Thanks.
try this:
NULL will be the first constraint when you create the column.
UNIQUE will be as add constraint, you should add the second constraint.
they can run on this order with no problem (tested):
--first constraint
alter table Table_Name
add Column_Name int null
--second constraint
alter table Table_Name
add constraint Constraint_Name unique (Column_Name)
In my example :
PaymentGatewayHash is column
Cart is a table
--first query
alter table Cart
add PaymentGatewayHash NVARCHAR(20) null
--second query
CREATE UNIQUE INDEX PaymentGatewayHashUnique
ON Cart (PaymentGatewayHash)
WHERE PaymentGatewayHash IS NOT NULL
I just tested that :D