SQL SERVER – How to ALTER CONSTRAINT

After reading my earlier blog post SQL SERVER – Prevent Constraint to Allow NULL. I recently received question from user regarding how to alter the constraint.

No. We cannot alter the constraint, only thing we can do is drop and recreate it.

Here is the CREATE and DROP script.

CREATE DATABASE TestDB
GO
USE TestDB
GO
CREATE TABLE TestTable (ID INT, Col1 INT, Col2 INT)
GO
-- Create Constraint on Col1
ALTER TABLE TestTable ADD CONSTRAINT CK_TestTable_Col1
CHECK (Col1 > 0)
GO
-- Dropping Constraint on Col1
ALTER TABLE TestTable DROP CONSTRAINT CK_TestTable_Col1
GO
-- Clean up
USE MASTER
GO
ALTER DATABASE TestDB
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
DROP DATABASE TestDB
GO

If you try to alter the constraint it will throw error.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

About these ads

12 thoughts on “SQL SERVER – How to ALTER CONSTRAINT

  1. Hello Pinal,

    I want to make a table, with expression a coloumn_id combination varchar and identity int.
    example :
    CREATE TABLE [dbo].[TestTableCity]
    (
    [City] AS (‘City’+CAST(IDENTITY(1,1)) AS VARCHAR(50)))
    ,CityName VARCHAR(50)
    )

    please check my query, help help

  2. @Sbn

    I don;t see a reason why you have to do concatination in table, you can always do this in view that does a select on this table.

    For Ex:

    CREATE TABLE [dbo].[TestTableCity]
    (
    ID int IDENTITY(1,1) not null
    ,CityName VARCHAR(50)
    )

    CREATE VIEW [Vw_TestTableCity]
    AS
    Select ‘City’+ Convert(varchar(46),ID) AS City
    ,CityName
    From [TestTableCity]

    go

    Insert into TestTableCity values (‘Hyderabad’)

    go
    Select * from [Vw_TestTableCity]

    But if you want to do it your own way which I don;t recommend, below is a way,

    CREATE TABLE [dbo].[TestTableCity]
    (
    [City] AS (‘City’+ Convert(varchar(46),ID))
    ,CityName VARCHAR(50)
    ,ID int IDENTITY(1,1) not null
    )
    go
    Insert into TestTableCity values (‘Hyderabad’)
    go
    Select * from TestTableCity

    ~ IM.

  3. We can prevent constraints…….

    create table x
    (
    id int not null,
    salary money not null constraint check_sale check(salary>0)
    )
    select * from x
    insert into x values (1,-8)
    alter table x nocheck constraint check_sale

  4. Pingback: SQL SERVER – Various Ways to Create Constraints – Quiz – Puzzle – 17 of 31 « SQL Server Journey with SQL Authority

  5. Pingback: SQL SERVER – Weekly Series – Memory Lane – #026 | SQL Server Journey with SQL Authority

  6. how to remove constraint which i dont gave the constraint name my syntax is
    CREATE TABLE emp
    (
    e_id NUMBER,
    first_name VARCHAR2 (15),
    last_name VARCHAR2 (15),
    doj DATE,
    salary NUMBER (10),
    d_id NUMBER (3),
    l_id NUMBER (3),
    week_off VARCHAR2 (15) NOT NULL,
    mngr_id NUMBER (3),
    pan_no VARCHAR2 (10) UNIQUE,
    phone_no NUMBER (20)
    )
    i want remove the mngr_id constraint
    plz reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s