Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:43:10+00:00 2026-05-31T02:43:10+00:00

Supposedly I have a table called Person defined like this: CREATE TABLE Persons (

  • 0

Supposedly I have a table called Person defined like this:

CREATE TABLE Persons
(
    P_Id uniqueidentifier Default newsequentialid() NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) DEFAULT 'Sandnes'
)

How can I remove the default value for that column using an sql query? Furthermore how can I add it if it is not present to begin with.
I know it is possible to add it via altering the table and adding a constraint over the P_Id column but I am not sure if that is the only way. I have come across this article however what is suggested there doesn’t seem to really work.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:43:12+00:00Added an answer on May 31, 2026 at 2:43 am

    In SQL Server, defaults are defined as constraints associated with a specific column in a table. All constraints are assigned a name; this is important, because once the constraint is created, if you want to modify it you have to reference it by this name. (And I’ll be watching this question, to see if I’m wrong.)

    Based on this sample table:

    CREATE TABLE MyTable
     (
       MyTableId  int          not null
      ,SomeData   varchar(50)  not null  default 'Foo'
      ,MoreData   datetime     not null  default CURRENT_TIMESTAMP
     )
    

    Step 1: Determine if a constraint exists on a column. Several ways to do this, all involving system views and/or metadata functions. Here’s a quick one, where ‘MyTable’ and ‘SomeData’ could be set as parameters:

    SELECT name
     from sys.default_constraints
     where parent_object_id = object_id('MyTable')
      and parent_column_id = columnproperty(object_id('MyTable'), 'SomeData', 'ColumnId')
    

    Try it, and you’ll see that the name generated is essentially random blather. To determine if a default exists on a column, you could do:

    IF exists (select name
                from sys.default_constraints
                where parent_object_id = object_id('MyTable')
                 and parent_column_id = columnproperty(object_id('MyTable'), 'SomeData', 'ColumnId'))
        PRINT 'Default found'
    ELSE
        PRINT 'NoDefault'
    

    To drop an existing default where you don’t know the name, you’ll have to build dynamic SQL. (that code in the referenced article is wrong, and clearly never tested.) @Călin does it slightly differently than I’d do it, but the idea’s the same:

    DECLARE @Command nvarchar(max)
    
    SELECT @Command = 'ALTER TABLE MyTable drop constraint ' + name
     from sys.default_constraints
     where parent_object_id = object_id('MyTable')
      and parent_column_id = columnproperty(object_id('MyTable'), 'SomeData', 'ColumnId')
    
    IF @Command is not null
        EXECUTE sp_executeSQL @Command
    

    (With error checking, parameters for the table and and column being checked, and so on.)

    Lastly, you can avoid most of this by naming the defaults when you create the constraint, like so:

    CREATE TABLE MyTable
     (
       MyTableId  int          not null
      ,SomeData   varchar(50)  not null
        constraint DF_MyTable__SomeData  default 'Foo'
      ,MoreData   datetime     not null
        constraint DF_MyTable__MoreData  default CURRENT_TIMESTAMP
     )
    

    Or like so:

    IF not exists (select name
                    from sys.default_constraints
                    where parent_object_id = object_id('MyTable')
                     and parent_column_id = columnproperty(object_id('MyTable'), 'SomeData', 'ColumnId'))
        ALTER TABLE MyTable
         add constraint DF_MyTable__SomeData
          default 'Foo' for SomeData
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, I have a table called products that has a relationship with itself. This
I have a database (NexusDB (supposedly SQL-92 compliant)) which contains and Item table, a
I have a table called posts having an INT field posted and a table
I have a data table. Each row of the table has a commandButton called
I have a model called shipments. I added some columns to the shipments table
I have a table called Area, that stores areas in a hierarchical manner. There
I'm using a dataset. I have a table Adapter called PackageTableAdapter , which contains
I have a table in my MySQL database called studentname . I made an
So I have a database that has a structure something like this: [user_table] user_id,
I have an images table with a column called type . I simply want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.