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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:28:39+00:00 2026-06-01T14:28:39+00:00

CREATE TABLE my_table ( fk INTEGER, field_1 INTEGER, field_2 INTEGER, field_3 INTEGER ) VALID:

  • 0
CREATE TABLE my_table
(
    fk      INTEGER,
    field_1 INTEGER,
    field_2 INTEGER,
    field_3 INTEGER
)

VALID:

    fk    |    field_1    |    field_2    |    field_3              
----------+---------------+---------------+---------------
     1    |      1        |      null     |      null
     1    |      null     |      1        |      null
     1    |      null     |      null     |      1

It’s possible to create check constraint which allows only 1 field of 3 can be not null for 1 fk ?

  • 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-01T14:28:40+00:00Added an answer on June 1, 2026 at 2:28 pm

    It is not quite clear what you’d like to achieve.

    If you need only one column being NOT NULL per row, then Nitram’s answer will do, you can also try:

    CHECK ((sign(coalesce(field_1,0)) +
            sign(coalesce(field_2,0)) + sign(coalesce(field_3,0))) <= 1)
    

    Otherwise, if you need to have only single NOT NULL column per all rows with the given FK, you should look into the CONSTRAINT TRIGGER, something like this:

    CREATE OR REPLACE FUNCTION only_one() RETURNS trigger AS $only_one$
    DECLARE
        cnt  int4;
    BEGIN
        SELECT sign(coalesce(field_1,0)) +
               sign(coalesce(field_2,0)) +
               sign(coalesce(field_3,0)) +
               sign(coalesce(NEW.field_1,0)) +
               sign(coalesce(NEW.field_2,0))+
               sign(coalesce(NEW.field_3,0))
          INTO cnt
          FROM my_table WHERE fk = NEW.fk;
    
        IF cnt > 1 THEN
            RAISE EXCEPTION 'Sorry, too much NOT NULL values for FK=%', NEW.fk;
        END IF;
    
        RETURN NEW;
    END;
    $only_one$ LANGUAGE plpgsql;
    CREATE CONSTRAINT TRIGGER my_table_only_one 
     AFTER INSERT OR UPDATE ON my_table
       FOR EACH ROW EXECUTE PROCEDURE only_one();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My table: CREATE TABLE `beer`.`matches` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `hashId` int(10)
I've got a table where I used integer on a field which needs decimal
I want a table which can only have one record. My current solution is:
If I had the following table. create_table :my_table, :id => false do |t| t.string
If my table looks like this: CREATE TABLE `daily_individual_tracking` ( `daily_individual_tracking_id` int(10) unsigned NOT
I've created a compound unique index on my table: CREATE TABLE [dbo].[SearchIndexWord]( [ID] [int]
How can i get result My Dream Table Result CREATE TABLE #temp( [count] int
I have created an index on my table like this: CREATE INDEX index_typ_poplatky ON
My table 'Post' has field: id, name, content, post_id When i create new post,
i am trying to create a foreign key in my table. But when i

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.