Drop a Unique Constraint - sql-server

I need to drop a Unique constraint on a previously existing table and create a new unique constraint that will include extra column. Can I use the name of the constraint to drop it? or will the name of the unique constraint change based on Datasource. I need to execute the script on multiple instances of same DB (eg. dev,test,prod)

Yes, you can use the name of the constraint, provided that you previously deleted and it was a named contraint.

Related

Add Check constraints in SQL Server: ADD CONSTRAINT vs ADD CHECK

I want use a check constraint for a column in any table in SQL Server 2008.
I would like give a qualified name to the check constraint.
I have seen several syntax version on how to create it:
ALTER TABLE [dbo].[Roles2016.UsersCRM] WITH CHECK
ADD CHECK (([Estado]=(4) OR [Estado]=(3) OR [Estado]=(2) OR [Estado]=(1)))
ALTER TABLE [dbo].[Roles2016.UsersCRM] WITH CHECK
ADD CONSTRAINT [CK_UsuariosCRM_Estado]
CHECK (([Estado]=(4) OR [Estado]=(3) OR [Estado]=(2) OR [Estado]=(1)))
What's difference ADD CHECK and ADD CONSTRAINT for a check constraint?
It is possible, but a very bad habit to add constraints without a name:
CREATE TABLE tbl(SomeColumn VARCHAR(10) DEFAULT('test'))
will create a CONSTRAINT with a random name. Better use this
CREATE TABLE tbl(SomeColumn VARCHAR(10) CONSTRAINT DF_YourTable_SomeColumm DEFAULT('test'))
This will do the same, but will name the constraint like you want it.
This is extremely important if you run upgrade scripts in deployed environments. Just imagine, you want to change a constraint later and the name of this constraint is all different on your customers machines... That's a real pain!
So: Always name your constraints!

Altering multiple objects in in query

The code below is attempting to alter 2 columns at once in a table. Can it be done and am I just doing it wrong?
Alter table verdata
Add Primary Key(Asset_ID)
Add foreign key(Asdes) References AssetDesc(AssetDescription)
To add constraints in alter query,
follow the below,
ALTER TABLE ADD CONSTRAINT adds a table-level constraint to an existing table. Any supported table-level constraint type can be added via ALTER TABLE. The following limitations exist on adding a constraint to an existing table:
When adding a foreign key or check constraint to an existing table, Derby checks the table to make sure existing rows satisfy the constraint. If any row is invalid, Derby throws a statement exception and the constraint is not added.
All columns included in a primary key must contain non null data and be unique.
ALTER TABLE ADD UNIQUE or PRIMARY KEY provide a shorthand method of defining a primary key composed of a single column. If PRIMARY KEY is specified in the definition of column C, the effect is the same as if the PRIMARY KEY(C) clause were specified as a separate clause. The column cannot contain null values, so the NOT NULL attribute must also be specified.
For information on the syntax of constraints, see CONSTRAINT clause. Use the syntax for table-level constraint when adding a constraint with the ADD TABLE ADD CONSTRAINT syntax.
REFERENCE

Primary key no defined access database

I am developping an application in wpf. I have to work with an existing database in Access.
I used the ORM EntityFramework.
My problem is : in the database, its exists a table with no primary key so I can't add any values in this table.
The Error I get is: no primary key defined. I can change the definition of the table.
How can I solve my problem ? thx
So as to createy a primary key before you begin you must know that
a table can contain only one PRIMARY KEY constraint.
All columns defined within a PRIMARY KEY constraint must be defined as NOT NULL. If nullability is not specified, all columns participating in a PRIMARY KEY constraint have their nullability set to NOT NULL.
Security
Permissions
Creating a new table with a primary key requires CREATE TABLE permission in the database and ALTER permission on the schema in which the table is being created.
Creating a primary key in an existing table requires ALTER permission on the table.

How do you add a unique primary key field automatically in SQL Server?

I am using SQL Server 2012 and need to add a column with a unique primary key. I am about to load several hundred thousand records BULK and just discovered repetition in the field I was going to use. Have seen SEQUENCE and GUID. Need some guidance on the best choice and how to go about setting this up so that the key field is populated during the bulk load.
When you create your table in which you want to insert information create an IDENTITY column. That will serve as an auto-populating column with a unique number for each record.
Here is a link that might help you.
If you have already created your table just change this query to what suits to your table name and run it in order to add the new column you requested.
ALTER TABLE mytable
ADD COLUMN unique_id IDENTITY (1,1)
Just a slight update on what’s already posted that includes details for adding primary key constraint
alter table database.schema.table_t
add ID_column int identity(1,1)
primary key (ID_column)
If you already set the primary key on this table just go and remove it before you execute this statement.

combining UNIQUE and EXCLUSION constraints in postgresql

I am real beginner when it comes to databases, so any advice is appriciated.
There must be a way to combine a UNIQUE constraint and a EXCLUSION constraint in PostgreSQL.
.
The one below written by me is incorrect. (Syntactically for sure, and possible logically, too)
I'm trying to achieve the following:
a single authenticator can belong to 1 useridx at a given time. (so a UNIQUE constraint is needed)
however the authenticator can belong to a different useridx at a different time (so some kind of EXCLUSION constraint is needed)
different time means outside of the interval of validform, and validuntil.
What I've tried:
ALTER TABLE authentication
ADD CONSTRAINT lenient_constraint UNIQUE (useridx, authenticator)
EXCLUDE USING gist(validfrom WITH =, validuntil WITH &&);
If you are asking if you can do it in a single alter table statement the answer is no. If you are asking if you can do it against the same table, the answer is yes. See below:
mydb# alter table test add unique(test);
NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index "test_test_key" for table "test"
ALTER TABLE
mydb# alter table test add exclude (test with =);
NOTICE: ALTER TABLE / ADD EXCLUDE will create implicit index "test_test_excl" for table "test"
ALTER TABLE

Resources