I am trying to add following constraint to my DB2 table but it gives error.
ALTER TABLE Table_name ADD CONSTRAINT VALID_BINDING
CHECK((LOWER(REQ_BINDING) IN ('http-post','http-redirect'))
AND ((LOWER(RESP_BINDING) IN ('http-post','http-redirect')));
Is this a valid query. Can I use AND operator in it?
Isn't it about parentheses?
ALTER TABLE Table_name ADD CONSTRAINT VALID_BINDING
CHECK(
LOWER(REQ_BINDING) IN ('http-post','http-redirect') AND
LOWER(RESP_BINDING) IN ('http-post','http-redirect')
);
Related
I'm new in using SQL Server and right now I'm trying to change "Id" column from default ID to GUID.
When using this code
ALTER TABLE dbo.Bookings ALTER COLUMN Id UNIQUEIDENTIFIER DEFAULT NEWID();
It gets me an error
Incorrect syntax near 'DEFAULT'
And I don't understand where is the syntax problem.
Can anyone point it out what is causing this error?
You should use the add constraint syntax:
ALTER TABLE dbo.Bookings ADD CONSTRAINT DF_dbo_Bookings_Id DEFAULT(NEWID()) FOR Id;
Also, if your table have the primary key clustered on that Id column, you could use the NEWSEQUENTIALID for creating unique identifiers that will have less impact on your writes:
ALTER TABLE dbo.Bookings ADD CONSTRAINT DF_dbo_Bookings_Id DEFAULT(NEWSEQUENTIALID()) FOR Id;
Example, on dbfiddle
ALTER TABLE dbo.Bookings
ALTER COLUMN id UNIQUEIDENTIFIER ;
ALTER TABLE dbo.Bookings
ALTER id SET DEFAULT NEWID();
Its possible using the alters separately.
Its the best to backup current table to avoid potential data loss.
I have a table dbo.ExceptionMessage and now I want to change the column datatype nvarchar(100) to nvarchar(MAX). I used alter query for changing this
ALTER TABLE dbo.ExceptionMessage ALTER COLUMN Address nvarchar(MAX)
and while excecuting this query it shows some error like.
The object 'DF_ExceptionMessage_Address' is dependent on column 'Address'.
ALTER TABLE ALTER COLUMN Address failed because one or more objects access this column.
How can we solve this...
First Delete all Constraint Like this
ALTER TABLE TableName DROP CONSTRAINT [DF__TableName__ColumnName__FieldName]
and then perform change
ALTER TABLE dbo.ExceptionMessage ALTER COLUMN Address nvarchar(MAX)
then re enter the constraints
You have to find out which type of constraint DF_ExceptionMessage_Address is, drop it, alter the column type and then re-create the constraint if you need it.
you try first:
ALTER TABLE <tablename> DROP CONSTRAINT <Con_Name>;
And Then Do your Alter
ALTER TABLE dbo.ExceptionMessage ALTER COLUMN Address nvarchar(MAX)
Again Add Constraint
thiz help you to alter
alter table TableName
alter column ColumnName nvarchar(200);
When generating a diff script between two dbschema with vsdbcmd.exe, I sometime obtain an unexpected output, containing some drop constraint without the name of the constraint :
GO
PRINT N'Dropping On column: ColumnName ...';
GO
ALTER TABLE TableName DROP CONSTRAINT ;
In our schema, this column has a default value constraint, with an auto generated name. I expected vsdbcmd.exe to generate a valid ALTER TABLE sql statement, as specified in the msdn library :
ALTER TABLE [ database_name . [ schema_name ] . | schema_name . ] table_name DROP { [ CONSTRAINT ] constraint_name | COLUMN column_name }
Do you have any idea of what could prevent vsdbcmd.exe to generate a valid sql statement ?
This issue only occurs when the constraint has a generated name. Explicitly named constraint are not impacted.
Therefore, this solution is to name every constraint.
What is the syntax to enable/disable the constraint? I tried google
in oracle I write it like this
alter table OPT DISABLE constraint FK_param
how to do that in sybase?
You simply can't disable foreign keys constraints in sybase. But you can drop and recreate
them.
Drop constraint:
alter table table_name drop constraint constraint_name
Add constraint:
alter table table_name add constraint constraint_name
I know that in other DB's, there is something like a CHECK and NOCHECK on these constraints. So you could just NOCHECK the constraint you want to disable.
ALTER TABLE a1 NOCHECK CONSTRAINT a1_TestConstraint
You can't disable constraints in sybase. You can drop/add constraints only.
I need to add a constraint to an existing SQL server table but only if it does not already exist.
I am creating the constraint using the following SQL.
ALTER TABLE [Foo] ADD CONSTRAINT [FK_Foo_Bar] FOREIGN KEY ([BarId]) REFERENCES [Bar] ([BarId]) ON UPDATE CASCADE ON DELETE CASCADE
I'm hoping I can add some SQL to the begining of the SQL to test for the existence of the constraint but I have no idea how.
Personally I would drop the existing constraint, and recreate it - in case the one that is already there is in some way different
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[MyFKName]') AND OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE dbo.MyTableName DROP CONSTRAINT MyFKName
GO
ALTER TABLE dbo.MyTableName ADD CONSTRAINT [MyFKName] ...
The current, more modern, code I am using is:
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[MyFKName]') AND parent_object_id = OBJECT_ID(N'[dbo].[MyTableName]'))
ALTER TABLE dbo.[MyTableName] DROP CONSTRAINT [MyFKName]
GO
ALTER TABLE dbo.[MyTableName] ADD CONSTRAINT [MyFKName] FOREIGN KEY ...
not sure if there is any advantage of checking sys.objects ... or sys.foreign_keys ... but at some point I decided on sys.foreign_keys
Starting with SQL2016 new "IF EXISTS" syntax was added which is a lot more readable:
-- For SQL2016 onwards:
ALTER TABLE dbo.[MyTableName] DROP CONSTRAINT IF EXISTS [MyFKName]
GO
ALTER TABLE dbo.[MyTableName] ADD CONSTRAINT [MyFKName] FOREIGN KEY ...
I'd recommend using the INFORMATION_SCHEMA.TABLE_CONSTRAINTS view. It's portable across different database engines:
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME='Foo'
AND CONSTRAINT_NAME='FK_Foo_Bar'
AND CONSTRAINT_TYPE='FOREIGN KEY'
Check if the constraint already exists before adding it -
IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME = 'FK_Foo_Bar')
BEGIN
ALTER TABLE dbo.MyTableName ADD CONSTRAINT [MyFKName] ...
END
Alter table tableName add constraint constraintname default 0 for columnname
You can provide constraintname as you want without single quote
Drop the default constraint and create your own. ALTER table TABLE_NAME drop constraint CONSTRAINT NAME
GO
ALTER TABLE [dbo].[TABLE_NAME] ADD CONSTRAINT [DF_TABLE_NAME_COLUMN_NAME] FOR [COLUMN_NAME]
Very simple:
IF OBJECT_ID('Schema.keyname') IS NULL
ALTER TABLE Schema.tablename ADD CONSTRAINT keyname...