SQL check constraint on column - sql

I want to implement a constraint on a column to ensure that one particular value is only stored once in the column.
For example in a column values, I would like to have a constraint such that the value 0 can only be stored once.
How can I implement this in postgres?

Create partial unique index:
create table test(col int);
create unique index on test (col) where col = 0;

Related

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.

Postgres add unique constraint

I need to add a constraint to a table x which has many to one relation to other table. So the table x has field other_table_id.
There is other column in table x called primary which is boolean type.
I want to make sure that there is none or only one primary=true per one other_table_id.
Multiple rows can have other_table_id equals some same value and primary=false but only one true per other_table_id.
How do I create this constraint?
You need a partial unique index for that:
create unique index idx_unique_other
on table_x (other_table_id)
where primary;
This will only index rows where the value of primary column is true. And for those, the other_table_id has to be unique.

A nice way to add unique constraint to existing table for Oracles DB?

There is an existing table with three columns which all form the primary key.
What is the best way to add a unique column to it? Prefferably creating a sequence for it while Im at it.
You can add an additional column to a table with an ALTER TABLE
ALTER TABLE table_name
ADD( new_column_name NUMBER UNIQUE );
You can create a new sequence and then create a trigger that populates the new column using that sequence
CREATE SEQUENCE sequence_name;
CREATE TRIGGER trigger_name
BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
:new.new_column_name := sequence_name.nextval;
END;
If you are using a version of Oracle prior to 11g, your trigger would need to do a SELECT from DUAL in order to populate the :new.new_column_name column rather than doing a direct assignment
CREATE TRIGGER trigger_name
BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
SELECT sequence_name.nextval
INTO :new.new_column_name
FROM dual;
END;
Presumably, you'd also want to initialize all the existing rows using the sequence value before you started inserting new rows
UPDATE table_name
SET new_column_name = sequence_name.nextval
WHERE new_column_name IS NULL
But it seems very odd to add a new sequence-generated column to an existing table with a composite primary key unless the goal was to use that new column as the primary key. The whole point of having a sequence generated column is so that you have a stable, synthetic primary key that doesn't depend on the actual business data. So it would seem to make much more sense to drop the existing primary key, add the new column, populate the data, declare the new column as the new primary key, and then define a unique constraint on the three columns that comprised the old primary key.

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

Table without a identity column but this column can't have repeated values

I'm creating a table that needs to have 2 columns. The first column can't be repeated. The thing is, I will insert the value of the first column. How do I create this column?
SQLServer 2005
Make the first column the primary key of the table.
Set the column as a primary key. I doesn't have to be an identity column to has a PK.
Create it the same way you would any other column: create table sometable (column1 varchar(10), column2 varchar(20)) or whatever.
Do you mean: How can you get the database to force it to be unique? Either declare it to be the primary key, or create a unique index on the column.
Perhaps you're thinking that a primary key must be auto-generated? There's no such rule. Whether you invent the value yourself or use an autonumber feature has nothing to do with whether a field can be a primary key.
Why not just put a unique constraint on it:
ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name>
UNIQUE(<column_name>)