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

  • SEARCH
  • Home
  • 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 7535945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:26:19+00:00 2026-05-30T06:26:19+00:00

There is a way to create a constraint or something where I can have

  • 0

There is a way to create a constraint or something where I can have only one record for a Day for a FK column? I.E.

PK(IDENTITY(1,1))  FK_FIELD DATETIME
------------------------------
1                  1        2012-02-22 4:50 p.m.
2                  2        2012-02-23 12:00 p.m.
3                  2        2012-02-23 9:00 p.m.  -- This one should not be allowed because the FK_Field 2 would have 2 record in one day.
4                  1        2012-02-23 08:00 a.m 

Hopefully someone understand what im trying to archieve.

Thanks in advance.

  • 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-30T06:26:22+00:00Added an answer on May 30, 2026 at 6:26 am

    Consider using:

    • 1 or many persisted computed column(s)
    • Unique constraint on the persisted computed column(s)

    Some examples of a variety of persisted columns (Much thanks to Aaron Bertrand):

    • Y AS YEAR(SOMEDATE) PERSISTED
    • M AS MONTH(SOMEDATE) PERSISTED
    • D AS MONTH(SOMEDATE) PERSISTED
    • YMD AS YEAR(SOMEDATE) * 10000 + MONTH(SOMEDATE) * 100 + DAY(SOMEDATE) PERSISTED
    • SOMEDATEDIFF AS DATEDIFF(DAY, 0, SOMEDATE) PERSISTED
    • SOMEDATESMALL AS DATEADD(DAY, 0, DATEDIFF(DAY, 0, SOMEDATE)) PERSISTED
    • SOMEDATESMALL AS CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, SOMEDATE, 105)), 105) PERSISTED
    • SOMEDATESMALL AS CONVERT(DATE, SOMEDATE) PERSISTED — Requires SQL Server 2008

    N.B. These examples were chosen carefully to ensure function determinism.

    Here’s a sample SQL:

    CREATE TABLE DATA
    (
         ID INT NOT NULL IDENTITY,
         FK_FIELD INT NOT NULL,
         SOMEDATE DATETIME NOT NULL,
         SOMEDATESMALL AS DATEADD(DAY, 0, DATEDIFF(DAY, 0, SOMEDATE)) PERSISTED,
         CONSTRAINT PK_DATA PRIMARY KEY (ID),
         CONSTRAINT UQ_DATA_SOMEDATESMALL UNIQUE (FK_FIELD, SOMEDATESMALL)
    );
    

    If you attempted the INSERTs, this is what you get:

    INSERT INTO DATA (FK_FIELD, SOMEDATE) VALUES (1, '2012-02-22 4:50PM'); -- SUCCESS
    INSERT INTO DATA (FK_FIELD, SOMEDATE) VALUES (2, '2012-02-23 12:00PM'); -- SUCCESS
    INSERT INTO DATA (FK_FIELD, SOMEDATE) VALUES (2, '2012-02-23 9:00PM'); -- CAUGHT
    INSERT INTO DATA (FK_FIELD, SOMEDATE) VALUES (1, '2012-02-23 8:00AM'); -- SUCCESS
    

    The message shown in SQL Server Management Studio is:

    Msg 2627, Level 14, State 1, Line 3
    Violation of UNIQUE KEY constraint ‘UQ_DATA_SOMEDATESMALL’.
    Cannot insert duplicate key in object ‘dbo.DATA’.
    The statement has been terminated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to create a Generic Method that uses the new() constraint
I have a column with a DEFAULT constraint. I'd like to create a script
Is there any way to create a naming convention for my primary key constraints
Is there a way to create a JButton with your own button graphic and
Is there any way to create a virtual drive in (My) Computer and manipulate
Is there any way to create the query parameters for doing a GET request
Is there a way to create an html link using h:outputLink, other JSF tag
Is there any way to create C# 3.0 anonymous object via Reflection at runtime
Is there a way to create an instance of a class based on the
Is there a way to create C# objects and call methods from unmanaged C++,

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.