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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:00:06+00:00 2026-06-02T15:00:06+00:00

I currently have a parent table: CREATE TABLE members ( member_id SERIAL NOT NULL,

  • 0

I currently have a parent table:

CREATE TABLE members (
    member_id SERIAL NOT NULL, UNIQUE, PRIMARY KEY
    first_name varchar(20)
    last_name varchar(20)
    address address (composite type)
    contact_numbers varchar(11)[3]
    date_joined date
    type varchar(5)
);

and two related tables:

CREATE TABLE basic_member (
    activities varchar[3])
    INHERITS (members)
);

CREATE TABLE full_member ( 
    activities varchar[])
    INHERITS (members)
);

If the type is full the details are entered to the full_member table or if type is basic into the basic_member table. What I want is that if I run an update and change the type to basic or full the tuple goes into the corresponding table.

I was wondering if I could do this with a rule like:

 CREATE RULE tuple_swap_full
 AS ON UPDATE TO full_member
 WHERE new.type = 'basic'
 INSERT INTO basic_member VALUES (old.member_id, old.first_name, old.last_name,
 old.address, old.contact_numbers, old.date_joined, new.type, old.activities);

… then delete the record from the full_member

Just wondering if my rule is anywhere near or if there is a better way.

  • 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-02T15:00:14+00:00Added an answer on June 2, 2026 at 3:00 pm
    • You don’t need

      member_id SERIAL NOT NULL, UNIQUE, PRIMARY KEY
      

      A PRIMARY KEY implies UNIQUE NOT NULL automatically:

      member_id SERIAL PRIMARY KEY
      
    • I wouldn’t use hard coded max length of varchar(20). Just use text and add a check constraint if you really must enforce a maximum length. Easier to change around.

    • Syntax for INHERITS is mangled. The key word goes outside the parens around columns.

      CREATE TABLE full_member ( 
          activities text[]
      ) INHERITS (members);
      
    • Table names are inconsistent (members <-> member). I use the singular form everywhere in my test case.

    • Finally, I would not use a RULE for the task. A trigger AFTER UPDATE seems preferable.

    Consider the following

    Test case:

    Tables:

    CREATE SCHEMA x;  -- I put everything in a test schema named "x".
    
    -- DROP TABLE x.members CASCADE;
    CREATE TABLE x.member (
         member_id SERIAL PRIMARY KEY
        ,first_name text
        -- more columns ...
        ,type text);
    
    CREATE TABLE x.basic_member (
        activities text[3]
    ) INHERITS (x.member);
    
    CREATE TABLE x.full_member ( 
        activities text[]
    ) INHERITS (x.member);
    

    Trigger function:

    Data-modifying CTEs (WITH x AS ( DELETE ..) are the best tool for the purpose. Requires PostgreSQL 9.1 or later.
    For older versions, first INSERT then DELETE.

    CREATE OR REPLACE FUNCTION x.trg_move_member()
      RETURNS trigger AS
    $BODY$
    BEGIN
    
    CASE NEW.type
    WHEN 'basic' THEN
        WITH x AS (
            DELETE FROM x.member
            WHERE member_id = NEW.member_id
            RETURNING *
            )
        INSERT INTO x.basic_member (member_id, first_name, type) -- more columns
        SELECT member_id, first_name, type -- more columns
        FROM   x;
    
    WHEN 'full' THEN
        WITH x AS (
            DELETE FROM x.member 
            WHERE member_id = NEW.member_id
            RETURNING *
            )
        INSERT INTO x.full_member (member_id, first_name, type) -- more columns
        SELECT member_id, first_name, type -- more columns
        FROM   x;
    END CASE;
    
    RETURN NULL;
    
    END;
    $BODY$
      LANGUAGE plpgsql VOLATILE;
    

    Trigger:

    Note that it is an AFTER trigger and has a WHEN condition.
    WHEN condition requires PostgreSQL 9.0 or later. For earlier versions, you can just leave it away, the CASE statement in the trigger itself takes care of it.

    CREATE TRIGGER up_aft
      AFTER UPDATE
      ON x.member
      FOR EACH ROW
      WHEN (NEW.type IN ('basic ','full')) -- OLD.type cannot be IN ('basic ','full')
      EXECUTE PROCEDURE x.trg_move_member();
    

    Test:

    INSERT INTO x.member (first_name, type) VALUES ('peter', NULL);
    
    UPDATE x.member SET type = 'full' WHERE first_name = 'peter';
    SELECT * FROM ONLY x.member;
    SELECT * FROM x.basic_member;
    SELECT * FROM x.full_member;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have three tables. GrandParent( tel int, G_Counter int, GField varchar(10)); Parent( tel
I have a table pictured below that has a surrogate Key as the Primary
I have an sql statement that currently is just returning all the end parent
I am currently trying to communicate a parent process which should have multiple children
Currently, I have a Maven project which inherits from a parent pom defining two
Currently in my application I have a single table that is giving me a
Assuming you have the following database table: create table Names ( Id INT IDENTITY
I currently have two models: Product and Service. Both share the same table structure
I'm using EF4.1 POCO. I have two tables [Table(Parent)] public class Parent { public
Guys i currently have 2 tables that reference a lookup table. Ive been tasked

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.