title: Use triggers on inherited tables to replace foreign keys
body: <p>I'm new to PostgreSQL. I have tables like:</p>
  
  <pre><code>CREATE TABLE Person (
    ID SERIAL PRIMARY KEY,
    Name VARCHAR(32) NOT NULL DEFAULT '',
    Surname VARCHAR(32) NOT NULL DEFAULT '',
    Birthday DATE,
    Gender VARCHAR(8)
  );
  
  -- Student table inherits from person
  CREATE TABLE Student (
    ID_Student SERIAL PRIMARY KEY,
    MajorDept VARCHAR(32),
  ) INHERITS(Person);
  
  -- Student table inherits from person
  CREATE TABLE Employee (
    ID_Employee SERIAL PRIMARY KEY,
    Position VARCHAR(32),
    Rank VARCHAR(32),
    Salary NUMERIC(12,2)
  ) INHERITS(Person);
  
  -- Address table references person
  CREATE TABLE Address (
    ID_Address SERIAL PRIMARY KEY,
    Person_id INTEGER REFERENCES Person(ID) NOT NULL,
    Email VARCHAR(32) UNIQUE,
    Country VARCHAR(32),
    CityCode INTEGER,
    City VARCHAR(32),
    AddressLine VARCHAR(60),
  );
  </code></pre>
  
  <p>According to these tables, when I want to INSERT data into <code>Adress</code> table, Postgres gives that error:</p>
  
  <blockquote>
    <p>ERROR:  insert or update on table "address" violates foreign key
    constraint "address_person_id_fkey" DETAIL:  Key (person_id)=(1) is
    not present in table "person".</p>
  </blockquote>
  
  <p>I've learned that in Postgres</p>
  
  <blockquote>
    <p>indexes (including unique constraints) and foreign key constraints
    only apply to single tables, not to their inheritance children.</p>
  </blockquote>
  
  <p>My question is how can I fix this with using triggers? Sample code would be very useful.</p>
  
  <p>After inserting a few rows to child tables, I can see the data with 'SELECT * FROM Person;' as well. It looks like:</p>
  
  <p><strong><em>Person Table</em></strong> </p>
  
  <pre><code>1;"Bill";"Smith";"1985-05-10";"male"
  2;"Jenny";"Brown";"1986-08-12";"female"
  3;"Bob";"Morgan";"1986-06-11";"male"
  4;"Katniss";"Everdeen";"1970-08-12";"female"
  5;"Peter";"Everdeen";"1968-08-12";"male"
  </code></pre>
  
  <p><strong><em>Student Table</em></strong></p>
  
  <pre><code>1;"Bill";"Smith";"1985-05-10";"male";1;"chemistry"
  2;"Jenny";"Brown";"1986-08-12";"female";2;"physics"
  3;"Bob";"Morgan";"1986-06-11";"male";3;"physics"
  </code></pre>
  
  <p><strong><em>Employee Table</em></strong></p>
  
  <pre><code>4;"Katniss";"Everdeen";"1970-08-12";"female";1;"Prof";"1";3500.00
  5;"Peter";"Everdeen";"1968-08-12";"male";2;"Assist-Prof";"5";1800.00
  </code></pre>