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 9152555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:04:49+00:00 2026-06-17T12:04:49+00:00

Can a Check Constraint (or some other technique) be used to prevent a value

  • 0

Can a Check Constraint (or some other technique) be used to prevent a value from being set that contradicts its prior value when its record is updated.

One example would be a NULL timestamp indicating something happened, like “file_exported”. Once a file has been exported and has a non-NULL value, it should never be set to NULL again.

Another example would be a hit counter, where an integer is only permitted to increase, but can never decrease.

If it helps I’m using postgresql, but I’d like to see solutions that fit any SQL implementation

  • 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-06-17T12:04:51+00:00Added an answer on June 17, 2026 at 12:04 pm

    One example would be a NULL timestamp indicating something happened,
    like “file_exported”. Once a file has been exported and has a non-NULL
    value, it should never be set to NULL again.

    Another example would be a hit counter, where an integer is only
    permitted to increase, but can never decrease.

    In both of these cases, I simply wouldn’t record these changes as attributes on the annotated table; the ‘exported’ or ‘hit count’ is a distinct idea, representing related but orthogonal real world notions from the objects they relate to:

    So they would simply be different relations. Since We only want “file_exported” to occur once:

    CREATE TABLE thing_file_exported(
        thing_id INTEGER PRIMARY KEY REFERENCES(thing.id),
        file_name VARCHAR NOT NULL
    )
    

    The hit counter is similarly a different table:

    CREATE TABLE thing_hits(
        thing_id INTEGER NOT NULL REFERENCES(thing.id),
        hit_date TIMESTAMP NOT NULL,
        PRIMARY KEY (thing_id, hit_date)
    )
    

    And you might query with

    SELECT thing.col1, thing.col2, tfe.file_name, count(th.thing_id)
    FROM thing 
    LEFT OUTER JOIN thing_file_exported tfe
        ON (thing.id = tfe.thing_id)
    LEFT OUTER JOIN thing_hits th
        ON (thing.id = th.thing_id)
    GROUP BY thing.col1, thing.col2, tfe.file_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pending order table with a check constraint to prevent people from
I know that I can check whether the linux timestamp in $time is today
I'm looking for an algorithm that can check if nested regex repeats are reducible.
I have a check box list in that user can check or uncheck the
I'm aware that I can check an ActiveRecord through the rails console but it
I've found a Perl regexp that can check if a string is UTF-8 (the
There are no error codes that one can check on System.Data.SqlClient.SqlException. I want to
I need to change some primary keys from non-clustered to clustered but I can't
Can check out here alt text http://51hired.com/static/problem.bmp
I can check for iPhone with this code: (navigator.userAgent.match(/iPhone/i)) But I want to target

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.