-- Generated by CompileSchemaScripts.pl from:
-- 20210419-mbs-11456-fks.sql
-- 20210702-mbs-11760.sql
-- 20210916-mbs-11896.sql
-- 20210924-mbs-10327.sql
-- 20211008-mbs-11903.sql
-- 20211216-mbs-12140-12141.sql
-- 20220207-mbs-12224-standalone.sql
-- 20220309-mbs-12241.sql
-- 20220314-mbs-12252-standalone.sql
-- 20220314-mbs-12253-standalone.sql
-- 20220314-mbs-12254-standalone.sql
-- 20220314-mbs-12255-standalone.sql
-- 20220322-mbs-12256-standalone.sql
-- 20220408-immutable-link-tables-standalone.sql
-- 20220408-mbs-12249-standalone.sql
-- 20220412-mbs-12190-fks.sql
-- 20220512-mbs-12369.sql
\set ON_ERROR_STOP 1
BEGIN;
SET search_path = musicbrainz, public;
SET LOCAL statement_timeout = 0;
--------------------------------------------------------------------------------
SELECT '20210419-mbs-11456-fks.sql';

SET search_path = musicbrainz;


ALTER TABLE artist_credit_gid_redirect
   ADD CONSTRAINT artist_credit_gid_redirect_fk_new_id
   FOREIGN KEY (new_id)
   REFERENCES artist_credit(id);

--------------------------------------------------------------------------------
SELECT '20210702-mbs-11760.sql';


DROP TRIGGER IF EXISTS delete_unused_tag ON event_tag;
DROP TRIGGER IF EXISTS delete_unused_tag ON place_tag;
DROP TRIGGER IF EXISTS delete_unused_tag ON recording_tag;
DROP TRIGGER IF EXISTS delete_unused_tag ON release_tag;

CREATE CONSTRAINT TRIGGER delete_unused_tag
AFTER DELETE ON event_tag DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW EXECUTE PROCEDURE trg_delete_unused_tag_ref();

CREATE CONSTRAINT TRIGGER delete_unused_tag
AFTER DELETE ON place_tag DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW EXECUTE PROCEDURE trg_delete_unused_tag_ref();

CREATE CONSTRAINT TRIGGER delete_unused_tag
AFTER DELETE ON recording_tag DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW EXECUTE PROCEDURE trg_delete_unused_tag_ref();

CREATE CONSTRAINT TRIGGER delete_unused_tag
AFTER DELETE ON release_tag DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW EXECUTE PROCEDURE trg_delete_unused_tag_ref();

--------------------------------------------------------------------------------
SELECT '20210916-mbs-11896.sql';


DROP TRIGGER IF EXISTS unique_primary_for_locale ON area_alias;
DROP TRIGGER IF EXISTS unique_primary_for_locale ON artist_alias;
DROP TRIGGER IF EXISTS unique_primary_for_locale ON event_alias;
DROP TRIGGER IF EXISTS unique_primary_for_locale ON genre_alias;
DROP TRIGGER IF EXISTS unique_primary_for_locale ON instrument_alias;
DROP TRIGGER IF EXISTS unique_primary_for_locale ON label_alias;
DROP TRIGGER IF EXISTS unique_primary_for_locale ON place_alias;
DROP TRIGGER IF EXISTS unique_primary_for_locale ON recording_alias;
DROP TRIGGER IF EXISTS unique_primary_for_locale ON release_alias;
DROP TRIGGER IF EXISTS unique_primary_for_locale ON release_group_alias;
DROP TRIGGER IF EXISTS unique_primary_for_locale ON series_alias;
DROP TRIGGER IF EXISTS unique_primary_for_locale ON work_alias;

DROP FUNCTION IF EXISTS unique_primary_area_alias();
DROP FUNCTION IF EXISTS unique_primary_artist_alias();
DROP FUNCTION IF EXISTS unique_primary_event_alias();
DROP FUNCTION IF EXISTS unique_primary_genre_alias();
DROP FUNCTION IF EXISTS unique_primary_instrument_alias();
DROP FUNCTION IF EXISTS unique_primary_label_alias();
DROP FUNCTION IF EXISTS unique_primary_place_alias();
DROP FUNCTION IF EXISTS unique_primary_recording_alias();
DROP FUNCTION IF EXISTS unique_primary_release_alias();
DROP FUNCTION IF EXISTS unique_primary_release_group_alias();
DROP FUNCTION IF EXISTS unique_primary_series_alias();
DROP FUNCTION IF EXISTS unique_primary_work_alias();

--------------------------------------------------------------------------------
SELECT '20210924-mbs-10327.sql';


CREATE OR REPLACE FUNCTION del_collection_sub_on_private()
RETURNS trigger AS $$
  BEGIN
    IF NEW.public = FALSE AND OLD.public = TRUE THEN
      UPDATE editor_subscribe_collection sub
         SET available = FALSE,
             last_seen_name = OLD.name
       WHERE sub.collection = OLD.id
         AND sub.editor != NEW.editor
         AND sub.editor NOT IN (SELECT ecc.editor
                                  FROM editor_collection_collaborator ecc
                                 WHERE ecc.collection = sub.collection);
    END IF;

    RETURN NEW;
  END;
$$ LANGUAGE 'plpgsql';

--------------------------------------------------------------------------------
SELECT '20211008-mbs-11903.sql';


DROP TRIGGER IF EXISTS restore_collection_sub_on_public ON editor_collection;

CREATE OR REPLACE FUNCTION restore_collection_sub_on_public()
RETURNS trigger AS $$
  BEGIN
    IF NEW.public = TRUE AND OLD.public = FALSE THEN
      UPDATE editor_subscribe_collection sub
         SET available = TRUE,
             last_seen_name = NEW.name
       WHERE sub.collection = OLD.id
         AND sub.available = FALSE;
    END IF;

    RETURN NULL;
  END;
$$ LANGUAGE 'plpgsql';

-- Create triggers
CREATE TRIGGER restore_collection_sub_on_public AFTER UPDATE ON editor_collection
    FOR EACH ROW EXECUTE PROCEDURE restore_collection_sub_on_public();

--------------------------------------------------------------------------------
SELECT '20211216-mbs-12140-12141.sql';


-- NOTE: Make sure this script runs *before* any that recalculates
-- count/ref_count for the schema change.

DO $$
DECLARE
  empty_tag_ids INTEGER[];
  -- An "uncontrolled for whitespace" tag.
  ufw_tag RECORD;
  -- An existing "controlled for whitespace" tag ID that would conflict with
  -- ufw_tag if it were cleaned.
  existing_cfw_tag_id INTEGER;
  tag_cursor REFCURSOR;
BEGIN
  SELECT array_agg(id)
    FROM tag
   WHERE name ~ E'^\\s*$'
    INTO empty_tag_ids;

  RAISE NOTICE 'Deleting empty tag IDs: %', empty_tag_ids;

  DELETE FROM area_tag_raw WHERE tag = any(empty_tag_ids);
  DELETE FROM artist_tag_raw WHERE tag = any(empty_tag_ids);
  DELETE FROM event_tag_raw WHERE tag = any(empty_tag_ids);
  DELETE FROM instrument_tag_raw WHERE tag = any(empty_tag_ids);
  DELETE FROM label_tag_raw WHERE tag = any(empty_tag_ids);
  DELETE FROM place_tag_raw WHERE tag = any(empty_tag_ids);
  DELETE FROM recording_tag_raw WHERE tag = any(empty_tag_ids);
  DELETE FROM release_tag_raw WHERE tag = any(empty_tag_ids);
  DELETE FROM release_group_tag_raw WHERE tag = any(empty_tag_ids);
  DELETE FROM series_tag_raw WHERE tag = any(empty_tag_ids);
  DELETE FROM work_tag_raw WHERE tag = any(empty_tag_ids);

  DELETE FROM area_tag WHERE tag = any(empty_tag_ids);
  DELETE FROM artist_tag WHERE tag = any(empty_tag_ids);
  DELETE FROM event_tag WHERE tag = any(empty_tag_ids);
  DELETE FROM instrument_tag WHERE tag = any(empty_tag_ids);
  DELETE FROM label_tag WHERE tag = any(empty_tag_ids);
  DELETE FROM place_tag WHERE tag = any(empty_tag_ids);
  DELETE FROM recording_tag WHERE tag = any(empty_tag_ids);
  DELETE FROM release_tag WHERE tag = any(empty_tag_ids);
  DELETE FROM release_group_tag WHERE tag = any(empty_tag_ids);
  DELETE FROM series_tag WHERE tag = any(empty_tag_ids);
  DELETE FROM work_tag WHERE tag = any(empty_tag_ids);

  -- delete_unused_tag would normally kick in to delete these, but not if
  -- they were completely unreferenced prior to running this script.
  DELETE FROM tag WHERE id = any(empty_tag_ids);

  -- Find tags with uncontrolled whitespace and clean them up.
  --
  -- We may find that for any unclean tag, an existing tag with the
  -- "cleaned up" name already exists.  In that case, we update all
  -- *_tag_raw and *_tag rows to use the existing clean tag, and delete
  -- the unclean one.
  FOR ufw_tag IN (
    SELECT * FROM tag WHERE NOT controlled_for_whitespace(name)
  ) LOOP
    RAISE NOTICE 'Tag with uncontrolled whitespace found: id=%, name=%',
      ufw_tag.id, to_json(ufw_tag.name);

    SELECT t2.id
      FROM tag t1
      JOIN tag t2
        ON (t1.id = ufw_tag.id
            AND t2.id != ufw_tag.id
            AND t2.name = regexp_replace(btrim(t1.name), E'\\s{2,}', ' ', 'g'))
      INTO existing_cfw_tag_id;

    IF existing_cfw_tag_id IS NULL THEN
      UPDATE tag
         SET name = regexp_replace(btrim(name), E'\\s{2,}', ' ', 'g')
       WHERE id = ufw_tag.id;
    ELSE
      RAISE NOTICE 'Conflicting tag with controlled whitespace found: id=%',
        existing_cfw_tag_id;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM area_tag_raw WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE area_tag_raw SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM area_tag_raw WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM area_tag WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE area_tag SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM area_tag WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM artist_tag_raw WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE artist_tag_raw SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM artist_tag_raw WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM artist_tag WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE artist_tag SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM artist_tag WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM event_tag_raw WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE event_tag_raw SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM event_tag_raw WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM event_tag WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE event_tag SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM event_tag WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM instrument_tag_raw WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE instrument_tag_raw SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM instrument_tag_raw WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM instrument_tag WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE instrument_tag SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM instrument_tag WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM label_tag_raw WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE label_tag_raw SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM label_tag_raw WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM label_tag WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE label_tag SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM label_tag WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM place_tag_raw WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE place_tag_raw SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM place_tag_raw WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM place_tag WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE place_tag SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM place_tag WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM recording_tag_raw WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE recording_tag_raw SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM recording_tag_raw WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM recording_tag WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE recording_tag SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM recording_tag WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM release_tag_raw WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE release_tag_raw SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM release_tag_raw WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM release_tag WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE release_tag SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM release_tag WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM release_group_tag_raw WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE release_group_tag_raw SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM release_group_tag_raw WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM release_group_tag WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE release_group_tag SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM release_group_tag WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM series_tag_raw WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE series_tag_raw SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM series_tag_raw WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM series_tag WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE series_tag SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM series_tag WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM work_tag_raw WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE work_tag_raw SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM work_tag_raw WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      OPEN tag_cursor NO SCROLL FOR SELECT * FROM work_tag WHERE tag = ufw_tag.id FOR UPDATE;
      LOOP
        MOVE tag_cursor;
        IF FOUND THEN
          BEGIN
            UPDATE work_tag SET tag = existing_cfw_tag_id WHERE CURRENT OF tag_cursor;
          EXCEPTION WHEN unique_violation THEN
            DELETE FROM work_tag WHERE CURRENT OF tag_cursor;
          END;
        ELSE
          CLOSE tag_cursor;
          EXIT;
        END IF;
      END LOOP;

      DELETE FROM tag WHERE id = ufw_tag.id;
    END IF;
  END LOOP;
END
$$;

ALTER TABLE tag DROP CONSTRAINT IF EXISTS control_for_whitespace;
ALTER TABLE tag DROP CONSTRAINT IF EXISTS only_non_empty;

ALTER TABLE tag
  ADD CONSTRAINT control_for_whitespace CHECK (controlled_for_whitespace(name)),
  ADD CONSTRAINT only_non_empty CHECK (name != '');

--------------------------------------------------------------------------------
SELECT '20220207-mbs-12224-standalone.sql';


DELETE FROM area_tag a WHERE NOT EXISTS (
    SELECT 1
      FROM area_tag_raw r
     WHERE r.area = a.area AND r.tag = a.tag
);

UPDATE area_tag a SET count = (
    SELECT sum(CASE WHEN is_upvote THEN 1 ELSE -1 END)
      FROM area_tag_raw r
     WHERE r.area = a.area AND r.tag = a.tag
  GROUP BY r.area, r.tag
);

DELETE FROM artist_tag a WHERE NOT EXISTS (
    SELECT 1
      FROM artist_tag_raw r
     WHERE r.artist = a.artist AND r.tag = a.tag
);

UPDATE artist_tag a SET count = (
    SELECT sum(CASE WHEN is_upvote THEN 1 ELSE -1 END)
      FROM artist_tag_raw r
     WHERE r.artist = a.artist AND r.tag = a.tag
  GROUP BY r.artist, r.tag
);

DELETE FROM event_tag a WHERE NOT EXISTS (
    SELECT 1
      FROM event_tag_raw r
     WHERE r.event = a.event AND r.tag = a.tag
);

UPDATE event_tag a SET count = (
    SELECT sum(CASE WHEN is_upvote THEN 1 ELSE -1 END)
      FROM event_tag_raw r
     WHERE r.event = a.event AND r.tag = a.tag
  GROUP BY r.event, r.tag
);

DELETE FROM instrument_tag a WHERE NOT EXISTS (
    SELECT 1
      FROM instrument_tag_raw r
     WHERE r.instrument = a.instrument AND r.tag = a.tag
);

UPDATE instrument_tag a SET count = (
    SELECT sum(CASE WHEN is_upvote THEN 1 ELSE -1 END)
      FROM instrument_tag_raw r
     WHERE r.instrument = a.instrument AND r.tag = a.tag
  GROUP BY r.instrument, r.tag
);

DELETE FROM label_tag a WHERE NOT EXISTS (
    SELECT 1
      FROM label_tag_raw r
     WHERE r.label = a.label AND r.tag = a.tag
);

UPDATE label_tag a SET count = (
    SELECT sum(CASE WHEN is_upvote THEN 1 ELSE -1 END)
      FROM label_tag_raw r
     WHERE r.label = a.label AND r.tag = a.tag
  GROUP BY r.label, r.tag
);

DELETE FROM place_tag a WHERE NOT EXISTS (
    SELECT 1
      FROM place_tag_raw r
     WHERE r.place = a.place AND r.tag = a.tag
);

UPDATE place_tag a SET count = (
    SELECT sum(CASE WHEN is_upvote THEN 1 ELSE -1 END)
      FROM place_tag_raw r
     WHERE r.place = a.place AND r.tag = a.tag
  GROUP BY r.place, r.tag
);

DELETE FROM recording_tag a WHERE NOT EXISTS (
    SELECT 1
      FROM recording_tag_raw r
     WHERE r.recording = a.recording AND r.tag = a.tag
);

UPDATE recording_tag a SET count = (
    SELECT sum(CASE WHEN is_upvote THEN 1 ELSE -1 END)
      FROM recording_tag_raw r
     WHERE r.recording = a.recording AND r.tag = a.tag
  GROUP BY r.recording, r.tag
);

DELETE FROM release_tag a WHERE NOT EXISTS (
    SELECT 1
      FROM release_tag_raw r
     WHERE r.release = a.release AND r.tag = a.tag
);

UPDATE release_tag a SET count = (
    SELECT sum(CASE WHEN is_upvote THEN 1 ELSE -1 END)
      FROM release_tag_raw r
     WHERE r.release = a.release AND r.tag = a.tag
  GROUP BY r.release, r.tag
);

DELETE FROM release_group_tag a WHERE NOT EXISTS (
    SELECT 1
      FROM release_group_tag_raw r
     WHERE r.release_group = a.release_group AND r.tag = a.tag
);

UPDATE release_group_tag a SET count = (
    SELECT sum(CASE WHEN is_upvote THEN 1 ELSE -1 END)
      FROM release_group_tag_raw r
     WHERE r.release_group = a.release_group AND r.tag = a.tag
  GROUP BY r.release_group, r.tag
);

DELETE FROM series_tag a WHERE NOT EXISTS (
    SELECT 1
      FROM series_tag_raw r
     WHERE r.series = a.series AND r.tag = a.tag
);

UPDATE series_tag a SET count = (
    SELECT sum(CASE WHEN is_upvote THEN 1 ELSE -1 END)
      FROM series_tag_raw r
     WHERE r.series = a.series AND r.tag = a.tag
  GROUP BY r.series, r.tag
);

DELETE FROM work_tag a WHERE NOT EXISTS (
    SELECT 1
      FROM work_tag_raw r
     WHERE r.work = a.work AND r.tag = a.tag
);

UPDATE work_tag a SET count = (
    SELECT sum(CASE WHEN is_upvote THEN 1 ELSE -1 END)
      FROM work_tag_raw r
     WHERE r.work = a.work AND r.tag = a.tag
  GROUP BY r.work, r.tag
);

UPDATE tag t SET ref_count = (
  (SELECT count(*) FROM area_tag_raw r WHERE r.tag = t.id) +
  (SELECT count(*) FROM artist_tag_raw r WHERE r.tag = t.id) +
  (SELECT count(*) FROM event_tag_raw r WHERE r.tag = t.id) +
  (SELECT count(*) FROM instrument_tag_raw r WHERE r.tag = t.id) +
  (SELECT count(*) FROM label_tag_raw r WHERE r.tag = t.id) +
  (SELECT count(*) FROM place_tag_raw r WHERE r.tag = t.id) +
  (SELECT count(*) FROM recording_tag_raw r WHERE r.tag = t.id) +
  (SELECT count(*) FROM release_tag_raw r WHERE r.tag = t.id) +
  (SELECT count(*) FROM release_group_tag_raw r WHERE r.tag = t.id) +
  (SELECT count(*) FROM series_tag_raw r WHERE r.tag = t.id) +
  (SELECT count(*) FROM work_tag_raw r WHERE r.tag = t.id)
);

-- Unused, non-replicated table that holds FKs to tag.
TRUNCATE tag_relation;

DELETE FROM tag WHERE ref_count = 0;

CREATE TRIGGER update_counts_for_insert AFTER INSERT ON area_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_insert('area');

CREATE TRIGGER update_counts_for_update AFTER UPDATE ON area_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_update('area');

CREATE TRIGGER update_counts_for_delete AFTER DELETE ON area_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_delete('area');

CREATE TRIGGER update_counts_for_insert AFTER INSERT ON artist_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_insert('artist');

CREATE TRIGGER update_counts_for_update AFTER UPDATE ON artist_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_update('artist');

CREATE TRIGGER update_counts_for_delete AFTER DELETE ON artist_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_delete('artist');

CREATE TRIGGER update_counts_for_insert AFTER INSERT ON event_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_insert('event');

CREATE TRIGGER update_counts_for_update AFTER UPDATE ON event_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_update('event');

CREATE TRIGGER update_counts_for_delete AFTER DELETE ON event_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_delete('event');

CREATE TRIGGER update_counts_for_insert AFTER INSERT ON instrument_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_insert('instrument');

CREATE TRIGGER update_counts_for_update AFTER UPDATE ON instrument_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_update('instrument');

CREATE TRIGGER update_counts_for_delete AFTER DELETE ON instrument_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_delete('instrument');

CREATE TRIGGER update_counts_for_insert AFTER INSERT ON label_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_insert('label');

CREATE TRIGGER update_counts_for_update AFTER UPDATE ON label_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_update('label');

CREATE TRIGGER update_counts_for_delete AFTER DELETE ON label_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_delete('label');

CREATE TRIGGER update_counts_for_insert AFTER INSERT ON place_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_insert('place');

CREATE TRIGGER update_counts_for_update AFTER UPDATE ON place_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_update('place');

CREATE TRIGGER update_counts_for_delete AFTER DELETE ON place_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_delete('place');

CREATE TRIGGER update_counts_for_insert AFTER INSERT ON recording_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_insert('recording');

CREATE TRIGGER update_counts_for_update AFTER UPDATE ON recording_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_update('recording');

CREATE TRIGGER update_counts_for_delete AFTER DELETE ON recording_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_delete('recording');

CREATE TRIGGER update_counts_for_insert AFTER INSERT ON release_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_insert('release');

CREATE TRIGGER update_counts_for_update AFTER UPDATE ON release_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_update('release');

CREATE TRIGGER update_counts_for_delete AFTER DELETE ON release_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_delete('release');

CREATE TRIGGER update_counts_for_insert AFTER INSERT ON release_group_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_insert('release_group');

CREATE TRIGGER update_counts_for_update AFTER UPDATE ON release_group_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_update('release_group');

CREATE TRIGGER update_counts_for_delete AFTER DELETE ON release_group_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_delete('release_group');

CREATE TRIGGER update_counts_for_insert AFTER INSERT ON series_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_insert('series');

CREATE TRIGGER update_counts_for_update AFTER UPDATE ON series_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_update('series');

CREATE TRIGGER update_counts_for_delete AFTER DELETE ON series_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_delete('series');

CREATE TRIGGER update_counts_for_insert AFTER INSERT ON work_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_insert('work');

CREATE TRIGGER update_counts_for_update AFTER UPDATE ON work_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_update('work');

CREATE TRIGGER update_counts_for_delete AFTER DELETE ON work_tag_raw
    FOR EACH ROW EXECUTE PROCEDURE update_tag_counts_for_raw_delete('work');

--------------------------------------------------------------------------------
SELECT '20220309-mbs-12241.sql';


CREATE OR REPLACE FUNCTION controlled_for_whitespace(TEXT) RETURNS boolean AS $$
  SELECT NOT padded_by_whitespace($1);
$$ LANGUAGE SQL IMMUTABLE SET search_path = musicbrainz, public;

DROP FUNCTION IF EXISTS whitespace_collapsed(TEXT);

--------------------------------------------------------------------------------
SELECT '20220314-mbs-12252-standalone.sql';


ALTER TABLE edit_genre
   ADD CONSTRAINT edit_genre_fk_edit
   FOREIGN KEY (edit)
   REFERENCES edit(id);

ALTER TABLE edit_genre
   ADD CONSTRAINT edit_genre_fk_genre
   FOREIGN KEY (genre)
   REFERENCES genre(id)
   ON DELETE CASCADE;

--------------------------------------------------------------------------------
SELECT '20220314-mbs-12253-standalone.sql';


ALTER TABLE l_area_genre ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_area_genre ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_artist_genre ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_artist_genre ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_event_genre ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_event_genre ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_genre_genre ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_genre_genre ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));
ALTER TABLE l_genre_genre ADD CONSTRAINT non_loop_relationship CHECK (entity0 != entity1);

ALTER TABLE l_genre_instrument ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_genre_instrument ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_genre_label ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_genre_label ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_genre_place ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_genre_place ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_genre_recording ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_genre_recording ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_genre_release ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_genre_release ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_genre_release_group ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_genre_release_group ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_genre_series ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_genre_series ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_genre_url ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_genre_url ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_genre_work ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_genre_work ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));


ALTER TABLE l_area_genre
   ADD CONSTRAINT l_area_genre_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_area_genre
   ADD CONSTRAINT l_area_genre_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES area(id);

ALTER TABLE l_area_genre
   ADD CONSTRAINT l_area_genre_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES genre(id);

ALTER TABLE l_artist_genre
   ADD CONSTRAINT l_artist_genre_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_artist_genre
   ADD CONSTRAINT l_artist_genre_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES artist(id);

ALTER TABLE l_artist_genre
   ADD CONSTRAINT l_artist_genre_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES genre(id);

ALTER TABLE l_event_genre
   ADD CONSTRAINT l_event_genre_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_event_genre
   ADD CONSTRAINT l_event_genre_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES event(id);

ALTER TABLE l_event_genre
   ADD CONSTRAINT l_event_genre_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES genre(id);

ALTER TABLE l_genre_genre
   ADD CONSTRAINT l_genre_genre_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_genre_genre
   ADD CONSTRAINT l_genre_genre_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES genre(id);

ALTER TABLE l_genre_genre
   ADD CONSTRAINT l_genre_genre_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES genre(id);

ALTER TABLE l_genre_instrument
   ADD CONSTRAINT l_genre_instrument_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_genre_instrument
   ADD CONSTRAINT l_genre_instrument_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES genre(id);

ALTER TABLE l_genre_instrument
   ADD CONSTRAINT l_genre_instrument_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES instrument(id);

ALTER TABLE l_genre_label
   ADD CONSTRAINT l_genre_label_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_genre_label
   ADD CONSTRAINT l_genre_label_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES genre(id);

ALTER TABLE l_genre_label
   ADD CONSTRAINT l_genre_label_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES label(id);

ALTER TABLE l_genre_place
   ADD CONSTRAINT l_genre_place_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_genre_place
   ADD CONSTRAINT l_genre_place_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES genre(id);

ALTER TABLE l_genre_place
   ADD CONSTRAINT l_genre_place_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES place(id);

ALTER TABLE l_genre_recording
   ADD CONSTRAINT l_genre_recording_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_genre_recording
   ADD CONSTRAINT l_genre_recording_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES genre(id);

ALTER TABLE l_genre_recording
   ADD CONSTRAINT l_genre_recording_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES recording(id);

ALTER TABLE l_genre_release
   ADD CONSTRAINT l_genre_release_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_genre_release
   ADD CONSTRAINT l_genre_release_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES genre(id);

ALTER TABLE l_genre_release
   ADD CONSTRAINT l_genre_release_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES release(id);

ALTER TABLE l_genre_release_group
   ADD CONSTRAINT l_genre_release_group_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_genre_release_group
   ADD CONSTRAINT l_genre_release_group_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES genre(id);

ALTER TABLE l_genre_release_group
   ADD CONSTRAINT l_genre_release_group_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES release_group(id);

ALTER TABLE l_genre_series
   ADD CONSTRAINT l_genre_series_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_genre_series
   ADD CONSTRAINT l_genre_series_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES genre(id);

ALTER TABLE l_genre_series
   ADD CONSTRAINT l_genre_series_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES series(id);

ALTER TABLE l_genre_url
   ADD CONSTRAINT l_genre_url_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_genre_url
   ADD CONSTRAINT l_genre_url_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES genre(id);

ALTER TABLE l_genre_url
   ADD CONSTRAINT l_genre_url_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES url(id);

ALTER TABLE l_genre_work
   ADD CONSTRAINT l_genre_work_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_genre_work
   ADD CONSTRAINT l_genre_work_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES genre(id);

ALTER TABLE l_genre_work
   ADD CONSTRAINT l_genre_work_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES work(id);


CREATE TRIGGER b_upd_l_area_genre BEFORE UPDATE ON l_area_genre
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_artist_genre BEFORE UPDATE ON l_artist_genre
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_event_genre BEFORE UPDATE ON l_event_genre
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_genre_genre BEFORE UPDATE ON l_genre_genre
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_genre_instrument BEFORE UPDATE ON l_genre_instrument
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_genre_label BEFORE UPDATE ON l_genre_label
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_genre_place BEFORE UPDATE ON l_genre_place
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_genre_recording BEFORE UPDATE ON l_genre_recording
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_genre_release BEFORE UPDATE ON l_genre_release
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_genre_release_group BEFORE UPDATE ON l_genre_release_group
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_genre_url BEFORE UPDATE ON l_genre_url
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_genre_work BEFORE UPDATE ON l_genre_work
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_area_genre DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_artist_genre DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_event_genre DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_genre_genre DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_genre_instrument DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_genre_label DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_genre_place DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_genre_recording DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_genre_release DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_genre_release_group DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_genre_url DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_genre_work DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER url_gc_a_upd_l_genre_url
AFTER UPDATE ON l_genre_url DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW EXECUTE PROCEDURE remove_unused_url();

CREATE CONSTRAINT TRIGGER url_gc_a_del_l_genre_url
AFTER DELETE ON l_genre_url DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW EXECUTE PROCEDURE remove_unused_url();


ALTER TABLE documentation.l_area_genre_example
   ADD CONSTRAINT l_area_genre_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_genre(id);

ALTER TABLE documentation.l_artist_genre_example
   ADD CONSTRAINT l_artist_genre_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_artist_genre(id);

ALTER TABLE documentation.l_event_genre_example
   ADD CONSTRAINT l_event_genre_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_event_genre(id);

ALTER TABLE documentation.l_genre_genre_example
   ADD CONSTRAINT l_genre_genre_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_genre_genre(id);

ALTER TABLE documentation.l_genre_instrument_example
   ADD CONSTRAINT l_genre_instrument_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_genre_instrument(id);

ALTER TABLE documentation.l_genre_label_example
   ADD CONSTRAINT l_genre_label_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_genre_label(id);

ALTER TABLE documentation.l_genre_place_example
   ADD CONSTRAINT l_genre_place_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_genre_place(id);

ALTER TABLE documentation.l_genre_recording_example
   ADD CONSTRAINT l_genre_recording_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_genre_recording(id);

ALTER TABLE documentation.l_genre_release_example
   ADD CONSTRAINT l_genre_release_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_genre_release(id);

ALTER TABLE documentation.l_genre_release_group_example
   ADD CONSTRAINT l_genre_release_group_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_genre_release_group(id);

ALTER TABLE documentation.l_genre_series_example
   ADD CONSTRAINT l_genre_series_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_genre_series(id);

ALTER TABLE documentation.l_genre_url_example
   ADD CONSTRAINT l_genre_url_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_genre_url(id);

ALTER TABLE documentation.l_genre_work_example
   ADD CONSTRAINT l_genre_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_genre_work(id);

--------------------------------------------------------------------------------
SELECT '20220314-mbs-12254-standalone.sql';


ALTER TABLE genre_annotation
   ADD CONSTRAINT genre_annotation_fk_genre
   FOREIGN KEY (genre)
   REFERENCES genre(id);

ALTER TABLE genre_annotation
   ADD CONSTRAINT genre_annotation_fk_annotation
   FOREIGN KEY (annotation)
   REFERENCES annotation(id);

--------------------------------------------------------------------------------
SELECT '20220314-mbs-12255-standalone.sql';


ALTER TABLE genre_alias
   ADD CONSTRAINT genre_alias_fk_type
   FOREIGN KEY (type)
   REFERENCES genre_alias_type(id);

ALTER TABLE genre_alias
   ADD CONSTRAINT genre_alias_fk_genre
   FOREIGN KEY (genre)
   REFERENCES genre(id);

ALTER TABLE genre_alias_type
   ADD CONSTRAINT genre_alias_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES genre_alias_type(id);

CREATE TRIGGER end_date_implies_ended BEFORE UPDATE OR INSERT ON genre_alias
    FOR EACH ROW EXECUTE PROCEDURE end_date_implies_ended();
    
CREATE TRIGGER b_upd_genre_alias BEFORE UPDATE ON genre_alias
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER search_hint BEFORE UPDATE OR INSERT ON genre_alias
    FOR EACH ROW EXECUTE PROCEDURE simplify_search_hints(2);

--------------------------------------------------------------------------------
SELECT '20220322-mbs-12256-standalone.sql';


CREATE TRIGGER update_aggregate_rating_for_insert AFTER INSERT ON artist_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_insert('artist');

CREATE TRIGGER update_aggregate_rating_for_update AFTER UPDATE ON artist_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_update('artist');

CREATE TRIGGER update_aggregate_rating_for_delete AFTER DELETE ON artist_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_delete('artist');

CREATE TRIGGER update_aggregate_rating_for_insert AFTER INSERT ON event_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_insert('event');

CREATE TRIGGER update_aggregate_rating_for_update AFTER UPDATE ON event_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_update('event');

CREATE TRIGGER update_aggregate_rating_for_delete AFTER DELETE ON event_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_delete('event');

CREATE TRIGGER update_aggregate_rating_for_insert AFTER INSERT ON label_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_insert('label');

CREATE TRIGGER update_aggregate_rating_for_update AFTER UPDATE ON label_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_update('label');

CREATE TRIGGER update_aggregate_rating_for_delete AFTER DELETE ON label_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_delete('label');

CREATE TRIGGER update_aggregate_rating_for_insert AFTER INSERT ON place_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_insert('place');

CREATE TRIGGER update_aggregate_rating_for_update AFTER UPDATE ON place_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_update('place');

CREATE TRIGGER update_aggregate_rating_for_delete AFTER DELETE ON place_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_delete('place');

CREATE TRIGGER update_aggregate_rating_for_insert AFTER INSERT ON recording_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_insert('recording');

CREATE TRIGGER update_aggregate_rating_for_update AFTER UPDATE ON recording_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_update('recording');

CREATE TRIGGER update_aggregate_rating_for_delete AFTER DELETE ON recording_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_delete('recording');

CREATE TRIGGER update_aggregate_rating_for_insert AFTER INSERT ON release_group_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_insert('release_group');

CREATE TRIGGER update_aggregate_rating_for_update AFTER UPDATE ON release_group_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_update('release_group');

CREATE TRIGGER update_aggregate_rating_for_delete AFTER DELETE ON release_group_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_delete('release_group');

CREATE TRIGGER update_aggregate_rating_for_insert AFTER INSERT ON work_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_insert('work');

CREATE TRIGGER update_aggregate_rating_for_update AFTER UPDATE ON work_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_update('work');

CREATE TRIGGER update_aggregate_rating_for_delete AFTER DELETE ON work_rating_raw
    FOR EACH ROW EXECUTE PROCEDURE update_aggregate_rating_for_raw_delete('work');

--------------------------------------------------------------------------------
SELECT '20220408-immutable-link-tables-standalone.sql';


DROP TRIGGER IF EXISTS deny_deprecated ON link;

CREATE TRIGGER deny_deprecated BEFORE INSERT ON link
    FOR EACH ROW EXECUTE PROCEDURE deny_deprecated_links();

DROP TRIGGER IF EXISTS b_upd_link ON link;

CREATE TRIGGER b_upd_link BEFORE UPDATE ON link
    FOR EACH ROW EXECUTE PROCEDURE b_upd_link();

DROP TRIGGER IF EXISTS b_ins_link_attribute ON link_attribute;

CREATE TRIGGER b_ins_link_attribute BEFORE INSERT ON link_attribute
    FOR EACH ROW EXECUTE PROCEDURE prevent_invalid_attributes();

DROP TRIGGER IF EXISTS b_upd_link_attribute ON link_attribute;

CREATE TRIGGER b_upd_link_attribute BEFORE UPDATE ON link_attribute
    FOR EACH ROW EXECUTE PROCEDURE b_upd_link_attribute();

DROP TRIGGER IF EXISTS b_upd_link_attribute_credit ON link_attribute_credit;

CREATE TRIGGER b_upd_link_attribute_credit BEFORE UPDATE ON link_attribute_credit
    FOR EACH ROW EXECUTE PROCEDURE b_upd_link_attribute_credit();

DROP TRIGGER IF EXISTS b_upd_link_attribute_text_value ON link_attribute_text_value;

CREATE TRIGGER b_upd_link_attribute_text_value BEFORE UPDATE ON link_attribute_text_value
    FOR EACH ROW EXECUTE PROCEDURE b_upd_link_attribute_text_value();

--------------------------------------------------------------------------------
SELECT '20220408-mbs-12249-standalone.sql';


ALTER TABLE area_containment
   ADD CONSTRAINT area_containment_fk_descendant
   FOREIGN KEY (descendant)
   REFERENCES area(id);

ALTER TABLE area_containment
   ADD CONSTRAINT area_containment_fk_parent
   FOREIGN KEY (parent)
   REFERENCES area(id);

DROP TRIGGER IF EXISTS a_ins_l_area_area ON l_area_area;

CREATE TRIGGER a_ins_l_area_area AFTER INSERT ON l_area_area
    FOR EACH ROW EXECUTE PROCEDURE a_ins_l_area_area_mirror();

DROP TRIGGER IF EXISTS a_upd_l_area_area ON l_area_area;

CREATE TRIGGER a_upd_l_area_area AFTER UPDATE ON l_area_area
    FOR EACH ROW EXECUTE PROCEDURE a_upd_l_area_area_mirror();

DROP TRIGGER IF EXISTS a_del_l_area_area ON l_area_area;

CREATE TRIGGER a_del_l_area_area AFTER DELETE ON l_area_area
    FOR EACH ROW EXECUTE PROCEDURE a_del_l_area_area_mirror();

--------------------------------------------------------------------------------
SELECT '20220412-mbs-12190-fks.sql';


-- FKs

ALTER TABLE edit_mood
   ADD CONSTRAINT edit_mood_fk_edit
   FOREIGN KEY (edit)
   REFERENCES edit(id);

ALTER TABLE edit_mood
   ADD CONSTRAINT edit_mood_fk_mood
   FOREIGN KEY (mood)
   REFERENCES mood(id)
   ON DELETE CASCADE;

ALTER TABLE l_area_mood
   ADD CONSTRAINT l_area_mood_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_area_mood
   ADD CONSTRAINT l_area_mood_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES area(id);

ALTER TABLE l_area_mood
   ADD CONSTRAINT l_area_mood_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES mood(id);

ALTER TABLE l_artist_mood
   ADD CONSTRAINT l_artist_mood_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_artist_mood
   ADD CONSTRAINT l_artist_mood_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES artist(id);

ALTER TABLE l_artist_mood
   ADD CONSTRAINT l_artist_mood_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES mood(id);

ALTER TABLE l_event_mood
   ADD CONSTRAINT l_event_mood_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_event_mood
   ADD CONSTRAINT l_event_mood_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES event(id);

ALTER TABLE l_event_mood
   ADD CONSTRAINT l_event_mood_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES mood(id);

ALTER TABLE l_genre_mood
   ADD CONSTRAINT l_genre_mood_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_genre_mood
   ADD CONSTRAINT l_genre_mood_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES genre(id);

ALTER TABLE l_genre_mood
   ADD CONSTRAINT l_genre_mood_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES mood(id);

ALTER TABLE l_instrument_mood
   ADD CONSTRAINT l_instrument_mood_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_instrument_mood
   ADD CONSTRAINT l_instrument_mood_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES instrument(id);

ALTER TABLE l_instrument_mood
   ADD CONSTRAINT l_instrument_mood_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES mood(id);

ALTER TABLE l_label_mood
   ADD CONSTRAINT l_label_mood_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_label_mood
   ADD CONSTRAINT l_label_mood_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES label(id);

ALTER TABLE l_label_mood
   ADD CONSTRAINT l_label_mood_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES mood(id);

ALTER TABLE l_mood_mood
   ADD CONSTRAINT l_mood_mood_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_mood_mood
   ADD CONSTRAINT l_mood_mood_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES mood(id);

ALTER TABLE l_mood_mood
   ADD CONSTRAINT l_mood_mood_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES mood(id);

ALTER TABLE l_mood_place
   ADD CONSTRAINT l_mood_place_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_mood_place
   ADD CONSTRAINT l_mood_place_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES mood(id);

ALTER TABLE l_mood_place
   ADD CONSTRAINT l_mood_place_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES place(id);

ALTER TABLE l_mood_recording
   ADD CONSTRAINT l_mood_recording_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_mood_recording
   ADD CONSTRAINT l_mood_recording_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES mood(id);

ALTER TABLE l_mood_recording
   ADD CONSTRAINT l_mood_recording_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES recording(id);

ALTER TABLE l_mood_release
   ADD CONSTRAINT l_mood_release_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_mood_release
   ADD CONSTRAINT l_mood_release_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES mood(id);

ALTER TABLE l_mood_release
   ADD CONSTRAINT l_mood_release_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES release(id);

ALTER TABLE l_mood_release_group
   ADD CONSTRAINT l_mood_release_group_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_mood_release_group
   ADD CONSTRAINT l_mood_release_group_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES mood(id);

ALTER TABLE l_mood_release_group
   ADD CONSTRAINT l_mood_release_group_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES release_group(id);

ALTER TABLE l_mood_series
   ADD CONSTRAINT l_mood_series_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_mood_series
   ADD CONSTRAINT l_mood_series_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES mood(id);

ALTER TABLE l_mood_series
   ADD CONSTRAINT l_mood_series_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES series(id);

ALTER TABLE l_mood_url
   ADD CONSTRAINT l_mood_url_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_mood_url
   ADD CONSTRAINT l_mood_url_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES mood(id);

ALTER TABLE l_mood_url
   ADD CONSTRAINT l_mood_url_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES url(id);

ALTER TABLE l_mood_work
   ADD CONSTRAINT l_mood_work_fk_link
   FOREIGN KEY (link)
   REFERENCES link(id);

ALTER TABLE l_mood_work
   ADD CONSTRAINT l_mood_work_fk_entity0
   FOREIGN KEY (entity0)
   REFERENCES mood(id);

ALTER TABLE l_mood_work
   ADD CONSTRAINT l_mood_work_fk_entity1
   FOREIGN KEY (entity1)
   REFERENCES work(id);

ALTER TABLE mood_alias
   ADD CONSTRAINT mood_alias_fk_mood
   FOREIGN KEY (mood)
   REFERENCES mood(id);

ALTER TABLE mood_alias
   ADD CONSTRAINT mood_alias_fk_type
   FOREIGN KEY (type)
   REFERENCES mood_alias_type(id);

ALTER TABLE mood_alias_type
   ADD CONSTRAINT mood_alias_type_fk_parent
   FOREIGN KEY (parent)
   REFERENCES mood_alias_type(id);

ALTER TABLE mood_annotation
   ADD CONSTRAINT mood_annotation_fk_mood
   FOREIGN KEY (mood)
   REFERENCES mood(id);

ALTER TABLE mood_annotation
   ADD CONSTRAINT mood_annotation_fk_annotation
   FOREIGN KEY (annotation)
   REFERENCES annotation(id);

ALTER TABLE documentation.l_area_mood_example
   ADD CONSTRAINT l_area_mood_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_mood(id);

ALTER TABLE documentation.l_artist_mood_example
   ADD CONSTRAINT l_artist_mood_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_artist_mood(id);

ALTER TABLE documentation.l_event_mood_example
   ADD CONSTRAINT l_event_mood_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_event_mood(id);

ALTER TABLE documentation.l_genre_mood_example
   ADD CONSTRAINT l_genre_mood_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_genre_mood(id);

ALTER TABLE documentation.l_instrument_mood_example
   ADD CONSTRAINT l_instrument_mood_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_mood(id);

ALTER TABLE documentation.l_label_mood_example
   ADD CONSTRAINT l_label_mood_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_label_mood(id);

ALTER TABLE documentation.l_mood_mood_example
   ADD CONSTRAINT l_mood_mood_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_mood_mood(id);

ALTER TABLE documentation.l_mood_place_example
   ADD CONSTRAINT l_mood_place_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_mood_place(id);

ALTER TABLE documentation.l_mood_recording_example
   ADD CONSTRAINT l_mood_recording_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_mood_recording(id);

ALTER TABLE documentation.l_mood_release_example
   ADD CONSTRAINT l_mood_release_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_mood_release(id);

ALTER TABLE documentation.l_mood_release_group_example
   ADD CONSTRAINT l_mood_release_group_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_mood_release_group(id);

ALTER TABLE documentation.l_mood_series_example
   ADD CONSTRAINT l_mood_series_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_mood_series(id);

ALTER TABLE documentation.l_mood_url_example
   ADD CONSTRAINT l_mood_url_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_mood_url(id);

ALTER TABLE documentation.l_mood_work_example
   ADD CONSTRAINT l_mood_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_mood_work(id);


-- Constraints

ALTER TABLE l_area_mood ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_area_mood ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_artist_mood ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_artist_mood ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_event_mood ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_event_mood ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_genre_mood ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_genre_mood ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_instrument_mood ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_instrument_mood ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_label_mood ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_label_mood ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_mood_mood ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_mood_mood ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));
ALTER TABLE l_mood_mood ADD CONSTRAINT non_loop_relationship CHECK (entity0 != entity1);

ALTER TABLE l_mood_place ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_mood_place ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_mood_recording ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_mood_recording ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_mood_release ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_mood_release ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_mood_release_group ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_mood_release_group ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_mood_series ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_mood_series ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_mood_url ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_mood_url ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

ALTER TABLE l_mood_work ADD CONSTRAINT control_for_whitespace_entity0_credit CHECK (controlled_for_whitespace(entity0_credit));
ALTER TABLE l_mood_work ADD CONSTRAINT control_for_whitespace_entity1_credit CHECK (controlled_for_whitespace(entity1_credit));

-- Triggers

CREATE TRIGGER b_upd_l_area_mood BEFORE UPDATE ON l_area_mood
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_artist_mood BEFORE UPDATE ON l_artist_mood
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_event_mood BEFORE UPDATE ON l_event_mood
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_genre_mood BEFORE UPDATE ON l_genre_mood
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_instrument_mood BEFORE UPDATE ON l_instrument_mood
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_label_mood BEFORE UPDATE ON l_label_mood
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_mood_mood BEFORE UPDATE ON l_mood_mood
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_mood_place BEFORE UPDATE ON l_mood_place
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_mood_recording BEFORE UPDATE ON l_mood_recording
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_mood_release BEFORE UPDATE ON l_mood_release
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_mood_release_group BEFORE UPDATE ON l_mood_release_group
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_mood_url BEFORE UPDATE ON l_mood_url
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_l_mood_work BEFORE UPDATE ON l_mood_work
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER b_upd_mood BEFORE UPDATE ON mood
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER end_date_implies_ended BEFORE UPDATE OR INSERT ON mood_alias
    FOR EACH ROW EXECUTE PROCEDURE end_date_implies_ended();

CREATE TRIGGER b_upd_mood_alias BEFORE UPDATE ON mood_alias
    FOR EACH ROW EXECUTE PROCEDURE b_upd_last_updated_table();

CREATE TRIGGER search_hint BEFORE UPDATE OR INSERT ON mood_alias
    FOR EACH ROW EXECUTE PROCEDURE simplify_search_hints(2);

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_area_mood DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_artist_mood DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_event_mood DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_genre_mood DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_instrument_mood DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_label_mood DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_mood_mood DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_mood_place DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_mood_recording DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_mood_release DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_mood_release_group DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_mood_url DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER remove_unused_links
    AFTER DELETE OR UPDATE ON l_mood_work DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW EXECUTE PROCEDURE remove_unused_links();

CREATE CONSTRAINT TRIGGER url_gc_a_upd_l_mood_url
AFTER UPDATE ON l_mood_url DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW EXECUTE PROCEDURE remove_unused_url();

CREATE CONSTRAINT TRIGGER url_gc_a_del_l_mood_url
AFTER DELETE ON l_mood_url DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW EXECUTE PROCEDURE remove_unused_url();

--------------------------------------------------------------------------------
SELECT '20220512-mbs-12369.sql';


-- This excludes genre & mood FKs, since those are already being created in
-- separate schema-27 upgrade scripts.

ALTER TABLE documentation.l_area_area_example DROP CONSTRAINT IF EXISTS l_area_area_example_fk_id;
ALTER TABLE documentation.l_area_artist_example DROP CONSTRAINT IF EXISTS l_area_artist_example_fk_id;
ALTER TABLE documentation.l_area_event_example DROP CONSTRAINT IF EXISTS l_area_event_example_fk_id;
ALTER TABLE documentation.l_area_instrument_example DROP CONSTRAINT IF EXISTS l_area_instrument_example_fk_id;
ALTER TABLE documentation.l_area_label_example DROP CONSTRAINT IF EXISTS l_area_label_example_fk_id;
ALTER TABLE documentation.l_area_place_example DROP CONSTRAINT IF EXISTS l_area_place_example_fk_id;
ALTER TABLE documentation.l_area_recording_example DROP CONSTRAINT IF EXISTS l_area_recording_example_fk_id;
ALTER TABLE documentation.l_area_release_example DROP CONSTRAINT IF EXISTS l_area_release_example_fk_id;
ALTER TABLE documentation.l_area_release_group_example DROP CONSTRAINT IF EXISTS l_area_release_group_example_fk_id;
ALTER TABLE documentation.l_area_series_example DROP CONSTRAINT IF EXISTS l_area_series_example_fk_id;
ALTER TABLE documentation.l_area_url_example DROP CONSTRAINT IF EXISTS l_area_url_example_fk_id;
ALTER TABLE documentation.l_area_work_example DROP CONSTRAINT IF EXISTS l_area_work_example_fk_id;
ALTER TABLE documentation.l_artist_artist_example DROP CONSTRAINT IF EXISTS l_artist_artist_example_fk_id;
ALTER TABLE documentation.l_artist_event_example DROP CONSTRAINT IF EXISTS l_artist_event_example_fk_id;
ALTER TABLE documentation.l_artist_instrument_example DROP CONSTRAINT IF EXISTS l_artist_instrument_example_fk_id;
ALTER TABLE documentation.l_artist_label_example DROP CONSTRAINT IF EXISTS l_artist_label_example_fk_id;
ALTER TABLE documentation.l_artist_place_example DROP CONSTRAINT IF EXISTS l_artist_place_example_fk_id;
ALTER TABLE documentation.l_artist_recording_example DROP CONSTRAINT IF EXISTS l_artist_recording_example_fk_id;
ALTER TABLE documentation.l_artist_release_example DROP CONSTRAINT IF EXISTS l_artist_release_example_fk_id;
ALTER TABLE documentation.l_artist_release_group_example DROP CONSTRAINT IF EXISTS l_artist_release_group_example_fk_id;
ALTER TABLE documentation.l_artist_series_example DROP CONSTRAINT IF EXISTS l_artist_series_example_fk_id;
ALTER TABLE documentation.l_artist_url_example DROP CONSTRAINT IF EXISTS l_artist_url_example_fk_id;
ALTER TABLE documentation.l_artist_work_example DROP CONSTRAINT IF EXISTS l_artist_work_example_fk_id;
ALTER TABLE documentation.l_event_event_example DROP CONSTRAINT IF EXISTS l_event_event_example_fk_id;
ALTER TABLE documentation.l_event_instrument_example DROP CONSTRAINT IF EXISTS l_event_instrument_example_fk_id;
ALTER TABLE documentation.l_event_label_example DROP CONSTRAINT IF EXISTS l_event_label_example_fk_id;
ALTER TABLE documentation.l_event_place_example DROP CONSTRAINT IF EXISTS l_event_place_example_fk_id;
ALTER TABLE documentation.l_event_recording_example DROP CONSTRAINT IF EXISTS l_event_recording_example_fk_id;
ALTER TABLE documentation.l_event_release_example DROP CONSTRAINT IF EXISTS l_event_release_example_fk_id;
ALTER TABLE documentation.l_event_release_group_example DROP CONSTRAINT IF EXISTS l_event_release_group_example_fk_id;
ALTER TABLE documentation.l_event_series_example DROP CONSTRAINT IF EXISTS l_event_series_example_fk_id;
ALTER TABLE documentation.l_event_url_example DROP CONSTRAINT IF EXISTS l_event_url_example_fk_id;
ALTER TABLE documentation.l_event_work_example DROP CONSTRAINT IF EXISTS l_event_work_example_fk_id;
ALTER TABLE documentation.l_instrument_instrument_example DROP CONSTRAINT IF EXISTS l_instrument_instrument_example_fk_id;
ALTER TABLE documentation.l_instrument_label_example DROP CONSTRAINT IF EXISTS l_instrument_label_example_fk_id;
ALTER TABLE documentation.l_instrument_place_example DROP CONSTRAINT IF EXISTS l_instrument_place_example_fk_id;
ALTER TABLE documentation.l_instrument_recording_example DROP CONSTRAINT IF EXISTS l_instrument_recording_example_fk_id;
ALTER TABLE documentation.l_instrument_release_example DROP CONSTRAINT IF EXISTS l_instrument_release_example_fk_id;
ALTER TABLE documentation.l_instrument_release_group_example DROP CONSTRAINT IF EXISTS l_instrument_release_group_example_fk_id;
ALTER TABLE documentation.l_instrument_series_example DROP CONSTRAINT IF EXISTS l_instrument_series_example_fk_id;
ALTER TABLE documentation.l_instrument_url_example DROP CONSTRAINT IF EXISTS l_instrument_url_example_fk_id;
ALTER TABLE documentation.l_instrument_work_example DROP CONSTRAINT IF EXISTS l_instrument_work_example_fk_id;
ALTER TABLE documentation.l_label_label_example DROP CONSTRAINT IF EXISTS l_label_label_example_fk_id;
ALTER TABLE documentation.l_label_place_example DROP CONSTRAINT IF EXISTS l_label_place_example_fk_id;
ALTER TABLE documentation.l_label_recording_example DROP CONSTRAINT IF EXISTS l_label_recording_example_fk_id;
ALTER TABLE documentation.l_label_release_example DROP CONSTRAINT IF EXISTS l_label_release_example_fk_id;
ALTER TABLE documentation.l_label_release_group_example DROP CONSTRAINT IF EXISTS l_label_release_group_example_fk_id;
ALTER TABLE documentation.l_label_series_example DROP CONSTRAINT IF EXISTS l_label_series_example_fk_id;
ALTER TABLE documentation.l_label_url_example DROP CONSTRAINT IF EXISTS l_label_url_example_fk_id;
ALTER TABLE documentation.l_label_work_example DROP CONSTRAINT IF EXISTS l_label_work_example_fk_id;
ALTER TABLE documentation.l_place_place_example DROP CONSTRAINT IF EXISTS l_place_place_example_fk_id;
ALTER TABLE documentation.l_place_recording_example DROP CONSTRAINT IF EXISTS l_place_recording_example_fk_id;
ALTER TABLE documentation.l_place_release_example DROP CONSTRAINT IF EXISTS l_place_release_example_fk_id;
ALTER TABLE documentation.l_place_release_group_example DROP CONSTRAINT IF EXISTS l_place_release_group_example_fk_id;
ALTER TABLE documentation.l_place_series_example DROP CONSTRAINT IF EXISTS l_place_series_example_fk_id;
ALTER TABLE documentation.l_place_url_example DROP CONSTRAINT IF EXISTS l_place_url_example_fk_id;
ALTER TABLE documentation.l_place_work_example DROP CONSTRAINT IF EXISTS l_place_work_example_fk_id;
ALTER TABLE documentation.l_recording_recording_example DROP CONSTRAINT IF EXISTS l_recording_recording_example_fk_id;
ALTER TABLE documentation.l_recording_release_example DROP CONSTRAINT IF EXISTS l_recording_release_example_fk_id;
ALTER TABLE documentation.l_recording_release_group_example DROP CONSTRAINT IF EXISTS l_recording_release_group_example_fk_id;
ALTER TABLE documentation.l_recording_series_example DROP CONSTRAINT IF EXISTS l_recording_series_example_fk_id;
ALTER TABLE documentation.l_recording_url_example DROP CONSTRAINT IF EXISTS l_recording_url_example_fk_id;
ALTER TABLE documentation.l_recording_work_example DROP CONSTRAINT IF EXISTS l_recording_work_example_fk_id;
ALTER TABLE documentation.l_release_group_release_group_example DROP CONSTRAINT IF EXISTS l_release_group_release_group_example_fk_id;
ALTER TABLE documentation.l_release_group_series_example DROP CONSTRAINT IF EXISTS l_release_group_series_example_fk_id;
ALTER TABLE documentation.l_release_group_url_example DROP CONSTRAINT IF EXISTS l_release_group_url_example_fk_id;
ALTER TABLE documentation.l_release_group_work_example DROP CONSTRAINT IF EXISTS l_release_group_work_example_fk_id;
ALTER TABLE documentation.l_release_release_example DROP CONSTRAINT IF EXISTS l_release_release_example_fk_id;
ALTER TABLE documentation.l_release_release_group_example DROP CONSTRAINT IF EXISTS l_release_release_group_example_fk_id;
ALTER TABLE documentation.l_release_series_example DROP CONSTRAINT IF EXISTS l_release_series_example_fk_id;
ALTER TABLE documentation.l_release_url_example DROP CONSTRAINT IF EXISTS l_release_url_example_fk_id;
ALTER TABLE documentation.l_release_work_example DROP CONSTRAINT IF EXISTS l_release_work_example_fk_id;
ALTER TABLE documentation.l_series_series_example DROP CONSTRAINT IF EXISTS l_series_series_example_fk_id;
ALTER TABLE documentation.l_series_url_example DROP CONSTRAINT IF EXISTS l_series_url_example_fk_id;
ALTER TABLE documentation.l_series_work_example DROP CONSTRAINT IF EXISTS l_series_work_example_fk_id;
ALTER TABLE documentation.l_url_url_example DROP CONSTRAINT IF EXISTS l_url_url_example_fk_id;
ALTER TABLE documentation.l_url_work_example DROP CONSTRAINT IF EXISTS l_url_work_example_fk_id;
ALTER TABLE documentation.l_work_work_example DROP CONSTRAINT IF EXISTS l_work_work_example_fk_id;
ALTER TABLE documentation.link_type_documentation DROP CONSTRAINT IF EXISTS link_type_documentation_fk_id;

DELETE FROM documentation.l_area_area_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_area_area);
DELETE FROM documentation.l_area_artist_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_area_artist);
DELETE FROM documentation.l_area_event_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_area_event);
DELETE FROM documentation.l_area_instrument_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_area_instrument);
DELETE FROM documentation.l_area_label_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_area_label);
DELETE FROM documentation.l_area_place_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_area_place);
DELETE FROM documentation.l_area_recording_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_area_recording);
DELETE FROM documentation.l_area_release_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_area_release);
DELETE FROM documentation.l_area_release_group_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_area_release_group);
DELETE FROM documentation.l_area_series_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_area_series);
DELETE FROM documentation.l_area_url_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_area_url);
DELETE FROM documentation.l_area_work_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_area_work);
DELETE FROM documentation.l_artist_artist_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_artist_artist);
DELETE FROM documentation.l_artist_event_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_artist_event);
DELETE FROM documentation.l_artist_instrument_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_artist_instrument);
DELETE FROM documentation.l_artist_label_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_artist_label);
DELETE FROM documentation.l_artist_place_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_artist_place);
DELETE FROM documentation.l_artist_recording_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_artist_recording);
DELETE FROM documentation.l_artist_release_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_artist_release);
DELETE FROM documentation.l_artist_release_group_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_artist_release_group);
DELETE FROM documentation.l_artist_series_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_artist_series);
DELETE FROM documentation.l_artist_url_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_artist_url);
DELETE FROM documentation.l_artist_work_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_artist_work);
DELETE FROM documentation.l_event_event_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_event_event);
DELETE FROM documentation.l_event_instrument_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_event_instrument);
DELETE FROM documentation.l_event_label_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_event_label);
DELETE FROM documentation.l_event_place_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_event_place);
DELETE FROM documentation.l_event_recording_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_event_recording);
DELETE FROM documentation.l_event_release_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_event_release);
DELETE FROM documentation.l_event_release_group_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_event_release_group);
DELETE FROM documentation.l_event_series_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_event_series);
DELETE FROM documentation.l_event_url_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_event_url);
DELETE FROM documentation.l_event_work_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_event_work);
DELETE FROM documentation.l_instrument_instrument_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_instrument_instrument);
DELETE FROM documentation.l_instrument_label_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_instrument_label);
DELETE FROM documentation.l_instrument_place_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_instrument_place);
DELETE FROM documentation.l_instrument_recording_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_instrument_recording);
DELETE FROM documentation.l_instrument_release_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_instrument_release);
DELETE FROM documentation.l_instrument_release_group_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_instrument_release_group);
DELETE FROM documentation.l_instrument_series_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_instrument_series);
DELETE FROM documentation.l_instrument_url_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_instrument_url);
DELETE FROM documentation.l_instrument_work_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_instrument_work);
DELETE FROM documentation.l_label_label_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_label_label);
DELETE FROM documentation.l_label_place_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_label_place);
DELETE FROM documentation.l_label_recording_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_label_recording);
DELETE FROM documentation.l_label_release_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_label_release);
DELETE FROM documentation.l_label_release_group_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_label_release_group);
DELETE FROM documentation.l_label_series_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_label_series);
DELETE FROM documentation.l_label_url_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_label_url);
DELETE FROM documentation.l_label_work_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_label_work);
DELETE FROM documentation.l_place_place_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_place_place);
DELETE FROM documentation.l_place_recording_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_place_recording);
DELETE FROM documentation.l_place_release_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_place_release);
DELETE FROM documentation.l_place_release_group_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_place_release_group);
DELETE FROM documentation.l_place_series_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_place_series);
DELETE FROM documentation.l_place_url_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_place_url);
DELETE FROM documentation.l_place_work_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_place_work);
DELETE FROM documentation.l_recording_recording_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_recording_recording);
DELETE FROM documentation.l_recording_release_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_recording_release);
DELETE FROM documentation.l_recording_release_group_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_recording_release_group);
DELETE FROM documentation.l_recording_series_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_recording_series);
DELETE FROM documentation.l_recording_url_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_recording_url);
DELETE FROM documentation.l_recording_work_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_recording_work);
DELETE FROM documentation.l_release_group_release_group_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_release_group_release_group);
DELETE FROM documentation.l_release_group_series_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_release_group_series);
DELETE FROM documentation.l_release_group_url_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_release_group_url);
DELETE FROM documentation.l_release_group_work_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_release_group_work);
DELETE FROM documentation.l_release_release_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_release_release);
DELETE FROM documentation.l_release_release_group_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_release_release_group);
DELETE FROM documentation.l_release_series_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_release_series);
DELETE FROM documentation.l_release_url_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_release_url);
DELETE FROM documentation.l_release_work_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_release_work);
DELETE FROM documentation.l_series_series_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_series_series);
DELETE FROM documentation.l_series_url_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_series_url);
DELETE FROM documentation.l_series_work_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_series_work);
DELETE FROM documentation.l_url_url_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_url_url);
DELETE FROM documentation.l_url_work_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_url_work);
DELETE FROM documentation.l_work_work_example WHERE id NOT IN (SELECT id FROM musicbrainz.l_work_work);
DELETE FROM documentation.link_type_documentation WHERE id NOT IN (SELECT id FROM musicbrainz.link_type);

ALTER TABLE documentation.l_area_area_example
   ADD CONSTRAINT l_area_area_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_area(id);

ALTER TABLE documentation.l_area_artist_example
   ADD CONSTRAINT l_area_artist_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_artist(id);

ALTER TABLE documentation.l_area_event_example
   ADD CONSTRAINT l_area_event_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_event(id);

ALTER TABLE documentation.l_area_instrument_example
   ADD CONSTRAINT l_area_instrument_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_instrument(id);

ALTER TABLE documentation.l_area_label_example
   ADD CONSTRAINT l_area_label_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_label(id);

ALTER TABLE documentation.l_area_place_example
   ADD CONSTRAINT l_area_place_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_place(id);

ALTER TABLE documentation.l_area_recording_example
   ADD CONSTRAINT l_area_recording_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_recording(id);

ALTER TABLE documentation.l_area_release_example
   ADD CONSTRAINT l_area_release_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_release(id);

ALTER TABLE documentation.l_area_release_group_example
   ADD CONSTRAINT l_area_release_group_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_release_group(id);

ALTER TABLE documentation.l_area_series_example
   ADD CONSTRAINT l_area_series_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_series(id);

ALTER TABLE documentation.l_area_url_example
   ADD CONSTRAINT l_area_url_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_url(id);

ALTER TABLE documentation.l_area_work_example
   ADD CONSTRAINT l_area_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_area_work(id);

ALTER TABLE documentation.l_artist_artist_example
   ADD CONSTRAINT l_artist_artist_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_artist_artist(id);

ALTER TABLE documentation.l_artist_event_example
   ADD CONSTRAINT l_artist_event_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_artist_event(id);

ALTER TABLE documentation.l_artist_instrument_example
   ADD CONSTRAINT l_artist_instrument_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_artist_instrument(id);

ALTER TABLE documentation.l_artist_label_example
   ADD CONSTRAINT l_artist_label_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_artist_label(id);

ALTER TABLE documentation.l_artist_place_example
   ADD CONSTRAINT l_artist_place_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_artist_place(id);

ALTER TABLE documentation.l_artist_recording_example
   ADD CONSTRAINT l_artist_recording_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_artist_recording(id);

ALTER TABLE documentation.l_artist_release_example
   ADD CONSTRAINT l_artist_release_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_artist_release(id);

ALTER TABLE documentation.l_artist_release_group_example
   ADD CONSTRAINT l_artist_release_group_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_artist_release_group(id);

ALTER TABLE documentation.l_artist_series_example
   ADD CONSTRAINT l_artist_series_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_artist_series(id);

ALTER TABLE documentation.l_artist_url_example
   ADD CONSTRAINT l_artist_url_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_artist_url(id);

ALTER TABLE documentation.l_artist_work_example
   ADD CONSTRAINT l_artist_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_artist_work(id);

ALTER TABLE documentation.l_event_event_example
   ADD CONSTRAINT l_event_event_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_event_event(id);

ALTER TABLE documentation.l_event_instrument_example
   ADD CONSTRAINT l_event_instrument_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_event_instrument(id);

ALTER TABLE documentation.l_event_label_example
   ADD CONSTRAINT l_event_label_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_event_label(id);

ALTER TABLE documentation.l_event_place_example
   ADD CONSTRAINT l_event_place_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_event_place(id);

ALTER TABLE documentation.l_event_recording_example
   ADD CONSTRAINT l_event_recording_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_event_recording(id);

ALTER TABLE documentation.l_event_release_example
   ADD CONSTRAINT l_event_release_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_event_release(id);

ALTER TABLE documentation.l_event_release_group_example
   ADD CONSTRAINT l_event_release_group_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_event_release_group(id);

ALTER TABLE documentation.l_event_series_example
   ADD CONSTRAINT l_event_series_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_event_series(id);

ALTER TABLE documentation.l_event_url_example
   ADD CONSTRAINT l_event_url_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_event_url(id);

ALTER TABLE documentation.l_event_work_example
   ADD CONSTRAINT l_event_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_event_work(id);

ALTER TABLE documentation.l_instrument_instrument_example
   ADD CONSTRAINT l_instrument_instrument_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_instrument(id);

ALTER TABLE documentation.l_instrument_label_example
   ADD CONSTRAINT l_instrument_label_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_label(id);

ALTER TABLE documentation.l_instrument_place_example
   ADD CONSTRAINT l_instrument_place_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_place(id);

ALTER TABLE documentation.l_instrument_recording_example
   ADD CONSTRAINT l_instrument_recording_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_recording(id);

ALTER TABLE documentation.l_instrument_release_example
   ADD CONSTRAINT l_instrument_release_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_release(id);

ALTER TABLE documentation.l_instrument_release_group_example
   ADD CONSTRAINT l_instrument_release_group_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_release_group(id);

ALTER TABLE documentation.l_instrument_series_example
   ADD CONSTRAINT l_instrument_series_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_series(id);

ALTER TABLE documentation.l_instrument_url_example
   ADD CONSTRAINT l_instrument_url_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_url(id);

ALTER TABLE documentation.l_instrument_work_example
   ADD CONSTRAINT l_instrument_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_instrument_work(id);

ALTER TABLE documentation.l_label_label_example
   ADD CONSTRAINT l_label_label_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_label_label(id);

ALTER TABLE documentation.l_label_place_example
   ADD CONSTRAINT l_label_place_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_label_place(id);

ALTER TABLE documentation.l_label_recording_example
   ADD CONSTRAINT l_label_recording_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_label_recording(id);

ALTER TABLE documentation.l_label_release_example
   ADD CONSTRAINT l_label_release_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_label_release(id);

ALTER TABLE documentation.l_label_release_group_example
   ADD CONSTRAINT l_label_release_group_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_label_release_group(id);

ALTER TABLE documentation.l_label_series_example
   ADD CONSTRAINT l_label_series_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_label_series(id);

ALTER TABLE documentation.l_label_url_example
   ADD CONSTRAINT l_label_url_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_label_url(id);

ALTER TABLE documentation.l_label_work_example
   ADD CONSTRAINT l_label_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_label_work(id);

ALTER TABLE documentation.l_place_place_example
   ADD CONSTRAINT l_place_place_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_place_place(id);

ALTER TABLE documentation.l_place_recording_example
   ADD CONSTRAINT l_place_recording_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_place_recording(id);

ALTER TABLE documentation.l_place_release_example
   ADD CONSTRAINT l_place_release_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_place_release(id);

ALTER TABLE documentation.l_place_release_group_example
   ADD CONSTRAINT l_place_release_group_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_place_release_group(id);

ALTER TABLE documentation.l_place_series_example
   ADD CONSTRAINT l_place_series_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_place_series(id);

ALTER TABLE documentation.l_place_url_example
   ADD CONSTRAINT l_place_url_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_place_url(id);

ALTER TABLE documentation.l_place_work_example
   ADD CONSTRAINT l_place_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_place_work(id);

ALTER TABLE documentation.l_recording_recording_example
   ADD CONSTRAINT l_recording_recording_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_recording_recording(id);

ALTER TABLE documentation.l_recording_release_example
   ADD CONSTRAINT l_recording_release_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_recording_release(id);

ALTER TABLE documentation.l_recording_release_group_example
   ADD CONSTRAINT l_recording_release_group_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_recording_release_group(id);

ALTER TABLE documentation.l_recording_series_example
   ADD CONSTRAINT l_recording_series_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_recording_series(id);

ALTER TABLE documentation.l_recording_url_example
   ADD CONSTRAINT l_recording_url_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_recording_url(id);

ALTER TABLE documentation.l_recording_work_example
   ADD CONSTRAINT l_recording_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_recording_work(id);

ALTER TABLE documentation.l_release_group_release_group_example
   ADD CONSTRAINT l_release_group_release_group_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_release_group_release_group(id);

ALTER TABLE documentation.l_release_group_series_example
   ADD CONSTRAINT l_release_group_series_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_release_group_series(id);

ALTER TABLE documentation.l_release_group_url_example
   ADD CONSTRAINT l_release_group_url_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_release_group_url(id);

ALTER TABLE documentation.l_release_group_work_example
   ADD CONSTRAINT l_release_group_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_release_group_work(id);

ALTER TABLE documentation.l_release_release_example
   ADD CONSTRAINT l_release_release_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_release_release(id);

ALTER TABLE documentation.l_release_release_group_example
   ADD CONSTRAINT l_release_release_group_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_release_release_group(id);

ALTER TABLE documentation.l_release_series_example
   ADD CONSTRAINT l_release_series_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_release_series(id);

ALTER TABLE documentation.l_release_url_example
   ADD CONSTRAINT l_release_url_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_release_url(id);

ALTER TABLE documentation.l_release_work_example
   ADD CONSTRAINT l_release_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_release_work(id);

ALTER TABLE documentation.l_series_series_example
   ADD CONSTRAINT l_series_series_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_series_series(id);

ALTER TABLE documentation.l_series_url_example
   ADD CONSTRAINT l_series_url_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_series_url(id);

ALTER TABLE documentation.l_series_work_example
   ADD CONSTRAINT l_series_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_series_work(id);

ALTER TABLE documentation.l_url_url_example
   ADD CONSTRAINT l_url_url_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_url_url(id);

ALTER TABLE documentation.l_url_work_example
   ADD CONSTRAINT l_url_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_url_work(id);

ALTER TABLE documentation.l_work_work_example
   ADD CONSTRAINT l_work_work_example_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.l_work_work(id);

ALTER TABLE documentation.link_type_documentation
   ADD CONSTRAINT link_type_documentation_fk_id
   FOREIGN KEY (id)
   REFERENCES musicbrainz.link_type(id);

COMMIT;
