Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying PostgreSQL Server Programming - Second Edition
  • Table Of Contents Toc
PostgreSQL Server Programming - Second Edition

PostgreSQL Server Programming - Second Edition - Second Edition

By : Dar, Krosing, Jim Mlodgenski
4.5 (8)
close
close
PostgreSQL Server Programming - Second Edition

PostgreSQL Server Programming - Second Edition

4.5 (8)
By: Dar, Krosing, Jim Mlodgenski

Overview of this book

This book is for moderate to advanced PostgreSQL database professionals who wish to extend PostgreSQL, utilizing the most updated features of PostgreSQL 9.4. For a better understanding of this book, familiarity with writing SQL, a basic idea of query tuning, and some coding experience in your preferred language is expected.
Table of Contents (16 chapters)
close
close
15
Index

Disallowing DELETE


What if our business requirements are such that the data can only be added and modified in some tables, but not deleted?

One way to handle this will be to just revoke the DELETE rights on these tables from all the users (remember to also revoke DELETE from PUBLIC), but this can also be achieved using triggers because of reasons such as auditing or returning custom exception messages.

A generic cancel trigger can be written as follows:

CREATE OR REPLACE FUNCTION cancel_op() 
  RETURNS TRIGGER AS $$ 
BEGIN 
    IF TG_WHEN = 'AFTER' THEN 
        RAISE EXCEPTION 'YOU ARE NOT ALLOWED TO % ROWS IN %.%', 
          TG_OP, TG_TABLE_SCHEMA, TG_TABLE_NAMENAME; 
    END IF; 
    RAISE NOTICE '% ON ROWS IN %.% WON'T HAPPEN', 
          TG_OP, TG_TABLE_SCHEMA, TG_TABLE_NAMENAME; 
    RETURN NULL; 
END; 
$$ LANGUAGE plpgsql; 

The same trigger function can be used for both the BEFORE and AFTER triggers. If you use it as a BEFORE trigger, the operation is skipped with a message. However...

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
PostgreSQL Server Programming - Second Edition
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon