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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:10:01+00:00 2026-06-08T20:10:01+00:00

I have an auction application with these two tables (this is highly simplified, obviously):

  • 0

I have an auction application with these two tables (this is highly simplified, obviously):

create table auctions (
   auction_id int,
   end_time datetime
);

create table bids (
   bid_id int,
   auction_id int,
   user_id int,
   amount numeric,
   bid_time timestamp,
   constraint foreign key (auction_id) references auctions (auction_id)
);

I don’t want bids on an auction after that auction has ended. In other words, rows in the bids table should be allowed only when the bid_time is earlier than the end_time for that auction. What’s the simplest way to do this in MySQL?

  • 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-08T20:10:03+00:00Added an answer on June 8, 2026 at 8:10 pm

    Ufortunately MySQL does not have a CHECK constraint feature. But You should be able to enforce this using a trigger. However, MySQL trigger support isn’t as advanced or well optimized as it is in other RDBMS-es, and you will suffer a considerable performance hit if you do it this way. So if this is a real-time trading system with massive amounts of concurrent users, you should look for another solution.

    CREATE TRIGGER bir_bids
    BEFORE INSERT ON bids
    FOR EACH ROW
    BEGIN
        DECLARE v_end_time datetime;
        -- declare a custom error condition. 
        -- SQLSTATE '45000' is reserved for that.
        DECLARE ERR_AUCTION_ALREADY_CLOSED CONDITION FOR SQLSTATE '45000';
    
        SELECT end_time INTO v_end_time
        FROM   auctions
        WHERE  auction_id = NEW.auction_id;
    
        -- the condition is written like this so that a signal is raised 
        -- only in case the bid time is greater than the auction end time.
        -- if either bid time or auction end time are NULL, no signal will be raised.
        -- You should avoid complex NULL handling here if bid time or auction end time
        -- must not be NULLable - simply define a NOT NULL column constraint for those cases.
        IF NEW.bid_time > v_end_time THEN
            SIGNAL ERR_AUCTION_ALREADY_CLOSED;
        END IF;
    END:
    

    Note that the SIGNAL syntax is available only since MySQL 5.5 (currently GA). Triggers are available since MySQL 5.0. So if you need to implement this in a MySQL version prior to 5.5, you need to hack your way around not being able to raise a signal. You can do that by causing some change in the data that will guarantee the INSERT to fail. For instance you could write:

        IF NEW.bid_time > v_end_time THEN
            SET NEW.auction_id = NULL;
        END IF;
    

    Since acution_id is declared NOT NULL in the table, the state of the data will be such that it cannot be inserted. The drawback is that you will get a NOT NULL constraint violation, and the application will have to guess whether this is due to this trigger firing or due to a “real” NOT NULL constraint violation.

    For more information, see: http://rpbouman.blogspot.nl/2009/12/validating-mysql-data-entry-with_15.html and http://rpbouman.blogspot.nl/2006/02/dont-you-need-proper-error-handling.html

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

Sidebar

Related Questions

I am working on an auction web application. All auctions have an end date.
I am working on an auction web application. Now i have a table with
I have an ASP.NET MVC 3 application which has a post action called Create
I have a VS2010 solution with 5 projects. Two of these projects are called:
In my web application I have a user profile page. That page has these
I have two tables with a many to one relationship which represent lots and
I have a symfony 1.4 project with the single application frontend . This application
I'm hitting a wall with this, currently I have two web-service endpoints configured and
In my application I have two Master pages and they are used in various
I'm starting a new application in rhodes and I'm trying to associate two tables.

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.