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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:46:31+00:00 2026-05-31T03:46:31+00:00

I am trying to create a simple database where I have a table of

  • 0

I am trying to create a simple database where I have a table of customer data and a table of order data. I am trying to write a constraint that makes it so a customer can’t order more than a specific amount of items on a given day. Here’s what I have:

CREATE TABLE CUSTOMER
(
    CUSTOMER_NUM CHAR(3) PRIMARY KEY,
    CUSTOMER_NAME CHAR(35) NOT NULL,
    STREET CHAR(15),
    CITY CHAR(15),
    STATE CHAR(3),
    ZIP CHAR(5),
);
CREATE TABLE ORDERS
(
    ORDER_NUM CHAR(5) PRIMARY KEY,
    ORDER_DATE DATE,
    CUSTOMER_NUM CHAR(3),

    CONSTRAINT CUSTOMER_NUM_FKEY FOREIGN KEY (CUSTOMER_NUM)
        REFRENCES CUSTOMER (CUSTOMER_NUM) MATCH SIMPLE
        ON UPDATE CASCADE ON DELETE CASCADE 
);

And this is what I wrote to enforce this constraint but it does not work. I assume its because ORDER_NUM and ORDER_DATE never have equal values.

CREATE ASSERTION ITEM_LIMIT
CEHCK(
        (   SELECT COUNT(*)
            FROM CUSTOMER C1, ORDERS O1
            WHERE C1.CUSTOMER_NUM = O1.CUSTOMER_NUM AND
                O1.ORDER_DATE = O1.ORDER_NUM
     ) <= 1000

My question is how to get this constraint to work, like how to I limit the amount of orders per day.

  • 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-31T03:46:33+00:00Added an answer on May 31, 2026 at 3:46 am

    As @ruakh already cleared up, there is no CREATE ASSERTION in PostgreSQL. Just check the list of SQL commands. It’s not there.

    You can use triggers that update a count per customer combined with a CHECK constraint, but you have to cover all relevant DML statements: INSERT, UPDATE, DELETE. Could look like this:

    Prepare existing customer table:

    ALTER TABLE customer ADD COLUMN order_ct integer DEFAULT 0;
    UPDATE customer SET order_ct = 0;
    ALTER TABLE customer ALTER order_ct SET NOT NULL;
    ALTER TABLE customer ADD CONSTRAINT order_ct_max1000 CHECK (order_ct <= 1000);
    

    Create trigger functions and triggers:

    CREATE OR REPLACE FUNCTION trg_order_upaft()
      RETURNS trigger AS
    $BODY$
    BEGIN
    
    IF OLD.customer_num <> NEW.customer_num THEN
        UPDATE customer
        SET    order_ct = order_ct - 1
        WHERE  customer_num = OLD.customer_num;
    
        UPDATE customer
        SET    order_ct = order_ct + 1
        WHERE  customer_num = NEW.customer_num;
    END IF;
    
    RETURN NULL;
    
    END;
    $BODY$
      LANGUAGE plpgsql;
    
    CREATE TRIGGER upaft
      AFTER UPDATE ON orders FOR EACH ROW
      EXECUTE PROCEDURE trg_order_upaft();
    
    
    CREATE OR REPLACE FUNCTION trg_order_insaft()
      RETURNS trigger AS
    $BODY$
    BEGIN
    
    UPDATE customer
    SET    order_ct = order_ct + 1
    WHERE  customer_num = NEW.customer_num;
    
    RETURN NULL;
    
    END;
    $BODY$
      LANGUAGE plpgsql;
    
    CREATE TRIGGER insaft
      AFTER INSERT ON orders FOR EACH ROW
      EXECUTE PROCEDURE trg_order_insaft();
    
    
    CREATE OR REPLACE FUNCTION trg_order_delaft()
      RETURNS trigger AS
    $BODY$
    BEGIN
    
    UPDATE customer
    SET    order_ct = order_ct - 1;
    WHERE  customer_num = OLD.customer_num;
    
    RETURN NULL;
    
    END;
    $BODY$
      LANGUAGE plpgsql;
    
    CREATE TRIGGER delaft
      AFTER DELETE ON orders FOR EACH ROW
      EXECUTE PROCEDURE trg_order_delaft();
    

    I made all those triggers AFTER triggers – that’s why it is ok to RETURN NULL. AFTER is preferable to BEFORE in this case. It performs better if any other conditions could cancel DML statements in the middle (like other triggers).

    If you have nothing of the sort, then BEFORE triggers may be preferable. Be sure to make the trigger functions RETURN NEW / OLD accordingly in this case.

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

Sidebar

Related Questions

I'm trying to create a simple button that deletes a row from the database.
I'm trying to reindex a table in a simple database that I created using
I am trying to create a simple page that enters data in to a
Hello, i'm trying to take detailed information from the database, i create a simple
I'm trying to create a simple custom validator for my project, and I can't
I'm trying to create script, that will import keywords from images to database. Keywords
I am trying to create a simple setting in Orchard that appears in the
I'm trying to create a very simple database abstraction, one part of it using
I am trying to make a simple web app. I have a User table
I'm studying webrat and cucumber and trying to create simple example. Here is my

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.