Command for adding a default constraint - sql

There appears to be at least two ways to add a default constraint using straight T-SQL. Am I correct that the only difference between the two below is that the second method specifically creates a name for the constraint, and the first method has one generated by SQL Server?
ALTER TABLE [Common].[PropertySetting] ADD DEFAULT ((1)) FOR [Active];
ALTER TABLE [Common].[PropertySetting] ADD CONSTRAINT [DF_PropertySetting_Active) DEFAULT ((1)) FOR [Active];

Pretty much, yes for an ALTER TABLE
You can add a columnn with default in one step for CREATE or ALTER too.
ALTER TABLE dbo.TableName
ADD bar varchar(100) CONSTRAINT DF_Foo_Bar DEFAULT ('bicycle');
ALTER TABLE dbo.TableName
ADD bar varchar(100) DEFAULT ('bicycle');
As you noted, the system generates a name if one is not supplied. CONSTRAINT constraint_name is optional says MSDN. The same applies to any column or table CONSTRAINT
If the column was already created, and you only want to add a (named) DEFAULT constraint, then use:
ALTER TABLE dbo.TableName
ADD CONSTRAINT DF_Foo_Bar DEFAULT 'bicycle' FOR FieldName;
To have the system generate the DEFAULT constraint name (which will be of the form DF_{TableName}_{Column}_{8RandomChars}, e.g. DF_TableName_FieldName_12345678) then omit the CONSTRAINT <name> part, like so:
ALTER TABLE dbo.TableName
ADD DEFAULT 'bicycle' FOR FieldName;

Related

Naming a default constraint

I'm trying to create a default constraint here, but the system is generating a weird name for it. If I want to name it df_MY_TABLE_GUID or something, how could I specify that name be used?
ALTER TABLE MY_TABLE
ADD MY_GUID uniqueidentifier NOT NULL
CONSTRAINT uq_MY_TABLE_GUID UNIQUE (MY_TABLE_GUID)
DEFAULT NEWID() WITH VALUES
Just specify the constraint name with the full syntax, like the UNIQUE in your example:
ALTER TABLE MY_TABLE ADD MY_GUID UNIQUEIDENTIFIER NOT NULL
CONSTRAINT uq_MY_TABLE_GUID UNIQUE (MY_TABLE_GUID)
CONSTRAINT df_MY_TABLE_GUID DEFAULT NEWID() WITH VALUES ;
As a matter of routine, I always prefer and encourage to always name every single constraint I create, for the sake of easy reference latter on.

How to create column with default and required values?

I'm trying this code but I'm getting this error message: An expression of non-boolean type specified in a context where a condition is expected, near ','.
ALTER TABLE customers
ADD active int
DEFAULT (1)
CONSTRAINT chk_active
CHECK (0,1);
Thanks in advance!
When defining a check constraint you have to actually refer to the column name, i.e. CHECK (Active IN (0,1) instead of just CHECK (0, 1). So your syntax would be
ALTER TABLE Customers ADD
active INT DEFAULT (1)
CONSTRAINT chk_active CHECK (Active IN (0,1));
It is also probably a good idea to name your default constraint, and include the table name in the constraint name so that you don't conflict with similar constraints on other tables:
ALTER TABLE Customers ADD
Active INT CONSTRAINT DF_Customers__Active DEFAULT (1)
CONSTRAINT CHK_Customers__Active CHECK (Active IN (0,1));
However, it would seem more appropriate to have a not null bit column so the check constraint is not required:
ALTER TABLE Customers ADD Active BIT NOT NULL CONSTRAINT DF_Customers__Active DEFAULT(1);
ALTER TABLE customers
ADD active int
GO
ALTER TABLE customers
ADD CONSTRAINT [DF_active ] DEFAULT (1) FOR active
GO
ALTER TABLE customers
ADD CONSTRAINT [chk_active] CHECK (active>=0 AND active <=1)
I would suggest you do it like this. Also always conside adding a name to your constraint.
As a facut, why don't u use active as bit column so you won't need the CHECK constraint.
try this
ALTER TABLE Customers ADD
active INT DEFAULT (1)
CONSTRAINT chk_active CHECK (Active IN (0,1));

Check constraint on existing column with PostgresQL

I'm trying to put check constraint on existing column.
Is there any way to achieve this from PostgreSQL?
Use alter table to add a new constraint:
alter table foo
add constraint check_positive check (the_column > 0);
More details and examples are in the manual:
http://www.postgresql.org/docs/current/static/sql-altertable.html#AEN70043
Edit
Checking for specific values is done in the same way, by using an IN operator:
alter table foo
add constraint check_positive check (some_code in ('A','B'));
If you are okay with (or want) Postgres to generate a constraint name you can use the following shorthand syntax.
ALTER TABLE foo
ADD CHECK (column_1 > 2);
You can add a new constraint with with alter table command. From documentation this example:
ALTER TABLE distributors
ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT;
You will have to replace constraint name as well as table name and content by your local requirements.
This should do it:
create table test (
id serial
);
alter table test
add constraint id_not_null
check (id is not null);

SQL - Add Unique Constraint Failure

Trying to alter a table in SQL Server. I want to add a unique constraint to a column called Names in table ReportingItemNames:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames$Name UNIQUE ([ReportingItemNames,Name])
But I am getting this error:
Column name 'ReportingItemNames,Name' does not exist in the target table or view
Where am I going wrong?
Use this:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames UNIQUE ([Name])
You can refer to ALTER TABLE (Transact-SQL) documentation for more information.
Shouldn't it be:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames$Name UNIQUE ([Name])

Database column with default constraint

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.