How to add "on delete cascade" constraints?

how to add fractions
how to add numbers
adding math
how to add: fast
how to add mixed numbers
how to add percentages
how to add in excel
addition

In PostgreSQL 8 is it possible to add ON DELETE CASCADES to the both foreign keys in the following table without dropping the latter?

# \d scores
        Table "public.scores"
 Column  |         Type          | Modifiers
---------+-----------------------+-----------
 id      | character varying(32) |
 gid     | integer               |
 money   | integer               | not null
 quit    | boolean               |
 last_ip | inet                  |
Foreign-key constraints:
   "scores_gid_fkey" FOREIGN KEY (gid) REFERENCES games(gid)
   "scores_id_fkey" FOREIGN KEY (id) REFERENCES users(id)

Both referenced tables are below - here:

# \d games
                                     Table "public.games"
  Column  |            Type             |                        Modifiers
----------+-----------------------------+----------------------------------------------------------
 gid      | integer                     | not null default nextval('games_gid_seq'::regclass)
 rounds   | integer                     | not null
 finished | timestamp without time zone | default now()
Indexes:
    "games_pkey" PRIMARY KEY, btree (gid)
Referenced by:
    TABLE "scores" CONSTRAINT "scores_gid_fkey" FOREIGN KEY (gid) REFERENCES games(gid)

And here:

# \d users
                Table "public.users"
   Column   |            Type             |   Modifiers
------------+-----------------------------+---------------
 id         | character varying(32)       | not null
 first_name | character varying(64)       |
 last_name  | character varying(64)       |
 female     | boolean                     |
 avatar     | character varying(128)      |
 city       | character varying(64)       |
 login      | timestamp without time zone | default now()
 last_ip    | inet                        |
 logout     | timestamp without time zone |
 vip        | timestamp without time zone |
 mail       | character varying(254)      |
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)
Referenced by:
    TABLE "cards" CONSTRAINT "cards_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
    TABLE "catch" CONSTRAINT "catch_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
    TABLE "chat" CONSTRAINT "chat_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
    TABLE "game" CONSTRAINT "game_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
    TABLE "hand" CONSTRAINT "hand_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
    TABLE "luck" CONSTRAINT "luck_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
    TABLE "match" CONSTRAINT "match_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
    TABLE "misere" CONSTRAINT "misere_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
    TABLE "money" CONSTRAINT "money_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
    TABLE "pass" CONSTRAINT "pass_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
    TABLE "payment" CONSTRAINT "payment_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
    TABLE "rep" CONSTRAINT "rep_author_fkey" FOREIGN KEY (author) REFERENCES users(id)
    TABLE "rep" CONSTRAINT "rep_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
    TABLE "scores" CONSTRAINT "scores_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
    TABLE "status" CONSTRAINT "status_id_fkey" FOREIGN KEY (id) REFERENCES users(id)

And also I wonder if it makes sense to add 2 index'es to the former table?

UPDATE: Thank you, and also I've got the advice at the mailing list, that I could manage it in 1 statement and thus without explicitly starting a transaction:

ALTER TABLE public.scores
DROP CONSTRAINT scores_gid_fkey,
ADD CONSTRAINT scores_gid_fkey
   FOREIGN KEY (gid)
   REFERENCES games(gid)
   ON DELETE CASCADE;

4 Ways to Add, To add fractions there are Three Simple Steps: Step 1: Make sure the bottom numbers (the denominators) are the same, Step 2: Add the numerators, put that  Adding integers is similar to adding whole numbers, but integers can be positive or negative. To add two negetive integers, you add the same way as you would add whole numbers, but place a negative sign in front of the answer. Adding positive and negative integers can get more tricky. To do that, you subtract the larger number from the ,smaller

Based off of @Mike Sherrill Cat Recall's answer, this is what worked for me:

ALTER TABLE "Children"
DROP CONSTRAINT "Children_parentId_fkey",
ADD CONSTRAINT "Children_parentId_fkey"
  FOREIGN KEY ("parentId")
  REFERENCES "Parent"(id)
  ON DELETE CASCADE;

Addition, How to add a contact. Open WhatsApp. Go to the Chats tab. Tap New chat > New contact. To add contacts that use an international phone number, read this  One quick and easy way to add values in Excel is to use AutoSum. Just select an empty cell directly below a column of data. Then on the Formula tab, click AutoSum > Sum. Excel will automatically sense the range to be summed. (AutoSum can also work horizontally if you select an empty cell to the right of the cells to be summed.)

Usage:

select replace_foreign_key('user_rates_posts', 'post_id', 'ON DELETE CASCADE');

Function:

CREATE OR REPLACE FUNCTION 
    replace_foreign_key(f_table VARCHAR, f_column VARCHAR, new_options VARCHAR) 
RETURNS VARCHAR
AS $$
DECLARE constraint_name varchar;
DECLARE reftable varchar;
DECLARE refcolumn varchar;
BEGIN

SELECT tc.constraint_name, ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name 
FROM 
    information_schema.table_constraints AS tc 
    JOIN information_schema.key_column_usage AS kcu
      ON tc.constraint_name = kcu.constraint_name
    JOIN information_schema.constraint_column_usage AS ccu
      ON ccu.constraint_name = tc.constraint_name
WHERE constraint_type = 'FOREIGN KEY' 
   AND tc.table_name= f_table AND kcu.column_name= f_column
INTO constraint_name, reftable, refcolumn;

EXECUTE 'alter table ' || f_table || ' drop constraint ' || constraint_name || 
', ADD CONSTRAINT ' || constraint_name || ' FOREIGN KEY (' || f_column || ') ' ||
' REFERENCES ' || reftable || '(' || refcolumn || ') ' || new_options || ';';

RETURN 'Constraint replaced: ' || constraint_name || ' (' || f_table || '.' || f_column ||
 ' -> ' || reftable || '.' || refcolumn || '); New options: ' || new_options;

END;
$$ LANGUAGE plpgsql;

Be aware: this function won't copy attributes of initial foreign key. It only takes foreign table name / column name, drops current key and replaces with new one.

Adding Fractions, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  To add fractions there are Three Simple Steps: Step 1: Make sure the bottom numbers (the denominators) are the same, Step 2: Add the numerators, put that answer over the denominator, Step 3: Simplify the fraction (if needed)

Adding 8 + 7 (video), You will actually be shown how to add two numbers together, step-by-step, the "long hand" way. This selection can be sort of fun too, because it can show you how to add together very, very large numbers that can have up to 30 digits each! Type in your numbers into the boxes below, then click "Add them!". Try some really large numbers if you like! +

Adding by making a group of 10 (video), Open Internet Explorer, select the Tools button , and then select Manage add-ons. Under Show , select All add-ons . Select the add-on, Enable , and then select Close .

Adding numbers with different signs (video), To add an electronic business card. Click Business Card, and then click a contact in the Filed As list. Then click OK. To add a hyperlink. Click Insert Hyperlink, type in the information or browse to a hyperlink, click to select it, and then click OK. To add a picture. Click Picture, browse to a picture, click to select it, and then click OK. Common image file formats for pictures include .bmp, .gif, .jpg, and .png.

Comments
  • A little OT, but I notice that you have not created indexes on referencing columns (for example, pref_scores.gid). Deletes on the referenced table will take a long time without those, if you get many rows in those tables. Some databases automatically create an index on the referencing column(s); PostgreSQL leaves that up to you, since there are some cases where it isn't worthwhile.
  • Thank you! I actually noticed that delete's take long, but didn't know that's the reason
  • Which cases would that be, when indexes on foreign keys aren't worthwhile?
  • I incorporated your finding into my answer. (That single statement is also a single transaction.)
  • @AlexanderFarber: When might you want to omit an index on the referencing column(s) of a FK? When there is another index not an exact match which will work well enough (e.g., you might have a trigram index for frequent similarity searches which will be OK for FK delete, too). When deletes are infrequent and can be scheduled off-hours. When a table has frequent updates of the referencing value. When the referencing table is very small but frequently updated. Exceptions occur often enough that the PostgreSQL community prefers to have control over it rather than making it automatic.
  • Thanks, that is what I thought too - but what to do with FOREIGN KEYs? Are they just constraints (similar to NOT NULL) which can be dropped and readded easily?
  • @AlexanderFarber: Yes, they're named constraints you can drop and add easily. But you probably want to do that within a transaction. Updated my answer with more detail.
  • +1 for looking ot up in pgAdminIII. It even gives you the DROP CONSTRAINT and ADD CONSTRAINT commands, so you can just copy and paste into a query window and edit the command to what you want.
  • After writing up the query, I noticed my Postgres GUI (Navicat) let's me trivially do this change from within the GUI: dl.dropboxusercontent.com/spa/quq37nq1583x0lf/wwqne-lw.png
  • For large tables, is this possible with NOT VALID and validating in a separate transaction? I have an unanswered question about this.