Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way to achieve the behaviour you seek (delete unused labels from the database) would be to use triggers.</p> <p>You could try writing something like:</p> <pre><code>CREATE OR REPLACE TRIGGER tr_LabelPosts_chk_no_more_associated_posts AFTER DELETE ON LabelPosts FOR EACH ROW EXECUTE PROCEDURE f_LabelPosts_chk_no_more_associated_posts(); CREATE OR REPLACE FUNCTION f_LabelPosts_chk_no_more_associated_posts() RETURNS TRIGGER AS $$ DECLARE var_associated_post_count INTEGER; BEGIN SELECT Count(*) AS associated_post_count INTO var_associated_post_count FROM LabelPosts WHERE labelId = OLD.labelId; IF(var_associated_post_count = 0) THEN DELETE FROM Labels WHERE labelId = OLD.labelId; END IF; END $$ LANGUAGE 'plpgsql'; </code></pre> <p>Basically, what happens here is: </p> <ol> <li>A row is deleted from table <code>Posts</code>. </li> <li>The deletion is cascaded to all associated rows in <code>LabelPosts</code> (thanks to your foreign key constraint).</li> <li>After the deletion of every single row in <code>LabelPosts</code> the trigger is activated, which in turn calls the PostgreSQL function.</li> <li>The function checks whether there are any other posts connected with the <code>labelId</code> in question. If so, then it finishes without any further modification. However, if there aren't any other rows in the relationship table, then the label is not used elsewhere and can thus be deleted.</li> <li>The function executes a delete DML on the <code>Labels</code> table, effectively removing the (now) unused label.</li> </ol> <p>Obviously the naming isn't the best and there must be a ton of syntax errors in there, so see <a href="http://www.postgresql.org/docs/9.1/static/sql-createtrigger.html" rel="nofollow">here</a> and <a href="http://www.postgresql.org/docs/9.1/static/sql-createfunction.html" rel="nofollow">here</a> for more information. There may be better ways to taking this thing down, however at the moment I can't think of a fast method that would not destroy the nice generic-looking table structure.</p> <p>Although bare in mind - it is not generally a good practice to overburden your database with triggers. It makes every associated query/statement run a tat slower &amp; also makes administration considerably more difficult. (Sometimes you need to disable triggers to perform certain DML operations, etc. depending on the nature of your triggers). </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload