Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Constraint question

Constraint question

Scheduled Pinned Locked Moved Database
questiondatabasebusinesshelp
8 Posts 6 Posters 13 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    ToddHileHoffer
    wrote on last edited by
    #1

    I have a table like below. CREATE TABLE [dbo].[BatchProcessRule]( [BatchProcessRuleId] [int] IDENTITY(1,1) NOT NULL, [EffectiveDate] [datetime] NOT NULL, [EndDate] [datetime] NULL, [LastModifiedBy] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [LastModifiedOn] [datetime] NOT NULL) How can I create a constraint so that only one row may exists with a null EndDate? I tried using the constraint text (select count (*) from BatchProcessRuleDetail where EndDate is null ) > 1 but I got an error saying subqueries are not allowed. Any thought?

    I didn't get any requirements for the signature

    A D M 3 Replies Last reply
    0
    • T ToddHileHoffer

      I have a table like below. CREATE TABLE [dbo].[BatchProcessRule]( [BatchProcessRuleId] [int] IDENTITY(1,1) NOT NULL, [EffectiveDate] [datetime] NOT NULL, [EndDate] [datetime] NULL, [LastModifiedBy] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [LastModifiedOn] [datetime] NOT NULL) How can I create a constraint so that only one row may exists with a null EndDate? I tried using the constraint text (select count (*) from BatchProcessRuleDetail where EndDate is null ) > 1 but I got an error saying subqueries are not allowed. Any thought?

      I didn't get any requirements for the signature

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      How about this ?

      CREATE TABLE [dbo].[BatchProcessRule](
      [BatchProcessRuleId] [int] IDENTITY(1,1) NOT NULL,
      [EffectiveDate] [datetime] NOT NULL,
      [EndDate] [datetime] CONSTRAINT BatchProcessRule_UNIQUE UNIQUE,
      [LastModifiedBy] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [LastModifiedOn] [datetime] NOT NULL)

      Making a column unique means it would take its value NULL but only once.:thumbsup:

      Abhishek Sur


      My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

      **Don't forget to click "Good Answer" if you like to.

      I 1 Reply Last reply
      0
      • A Abhishek Sur

        How about this ?

        CREATE TABLE [dbo].[BatchProcessRule](
        [BatchProcessRuleId] [int] IDENTITY(1,1) NOT NULL,
        [EffectiveDate] [datetime] NOT NULL,
        [EndDate] [datetime] CONSTRAINT BatchProcessRule_UNIQUE UNIQUE,
        [LastModifiedBy] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
        [LastModifiedOn] [datetime] NOT NULL)

        Making a column unique means it would take its value NULL but only once.:thumbsup:

        Abhishek Sur


        My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

        **Don't forget to click "Good Answer" if you like to.

        I Offline
        I Offline
        i j russell
        wrote on last edited by
        #3

        That will only work if the EndDates are unique.

        A 1 Reply Last reply
        0
        • I i j russell

          That will only work if the EndDates are unique.

          A Offline
          A Offline
          Abhishek Sur
          wrote on last edited by
          #4

          Isnt that what he wanted ?:confused:

          Abhishek Sur


          My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

          **Don't forget to click "Good Answer" if you like to.

          J 1 Reply Last reply
          0
          • A Abhishek Sur

            Isnt that what he wanted ?:confused:

            Abhishek Sur


            My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

            **Don't forget to click "Good Answer" if you like to.

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            No, he wanted only 1 null - that says nothing about the uniqueness of the non-nulls.

            1 Reply Last reply
            0
            • T ToddHileHoffer

              I have a table like below. CREATE TABLE [dbo].[BatchProcessRule]( [BatchProcessRuleId] [int] IDENTITY(1,1) NOT NULL, [EffectiveDate] [datetime] NOT NULL, [EndDate] [datetime] NULL, [LastModifiedBy] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [LastModifiedOn] [datetime] NOT NULL) How can I create a constraint so that only one row may exists with a null EndDate? I tried using the constraint text (select count (*) from BatchProcessRuleDetail where EndDate is null ) > 1 but I got an error saying subqueries are not allowed. Any thought?

              I didn't get any requirements for the signature

              D Offline
              D Offline
              David Skelly
              wrote on last edited by
              #6

              I don't think you can do this via a constraint. You might be able to do this with a trigger that runs on insert/update to check the date and raise an error if it violates your rule. Another possibility might be to restrict update and insert permissions to the table so that the only way to update/insert is via a stored procedure which does the check for null dates and raises an error accordingly.

              T 1 Reply Last reply
              0
              • D David Skelly

                I don't think you can do this via a constraint. You might be able to do this with a trigger that runs on insert/update to check the date and raise an error if it violates your rule. Another possibility might be to restrict update and insert permissions to the table so that the only way to update/insert is via a stored procedure which does the check for null dates and raises an error accordingly.

                T Offline
                T Offline
                ToddHileHoffer
                wrote on last edited by
                #7

                I am only using a stored proc to do the update, so I'm just going to leave it that way. I just like to set up all my relationships / constraints before I start developing. I can recall at least 3 or 4 times where database rules have prevented small bugs from causing major headaches.

                I didn't get any requirements for the signature

                1 Reply Last reply
                0
                • T ToddHileHoffer

                  I have a table like below. CREATE TABLE [dbo].[BatchProcessRule]( [BatchProcessRuleId] [int] IDENTITY(1,1) NOT NULL, [EffectiveDate] [datetime] NOT NULL, [EndDate] [datetime] NULL, [LastModifiedBy] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [LastModifiedOn] [datetime] NOT NULL) How can I create a constraint so that only one row may exists with a null EndDate? I tried using the constraint text (select count (*) from BatchProcessRuleDetail where EndDate is null ) > 1 but I got an error saying subqueries are not allowed. Any thought?

                  I didn't get any requirements for the signature

                  M Offline
                  M Offline
                  Member 14571937
                  wrote on last edited by
                  #8

                  >MyAARPMedicare is one of the most popular medical insurance plans in the United States. The main reason for the popularity of MyAARPMedicare plans is its service quality and the rewards/benefits that customers get when they enroll for a Medicare insurance plan under AARP.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups