Guest User

Untitled

a guest
Nov 21st, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. # Exploring many-to-one/one-to-many relationships
  2.  
  3. ## Deleting data in referential databases
  4. 1. Try to run the command `DELETE FROM restaurants WHERE id = '1000';`. Paste the error code below. Why do you think this happened?
  5.  
  6. ```sql
  7. ERROR: update or delete on table "restaurants" violates foreign key constraint "grades_restaurant_id_fkey" on table "grades"
  8. DETAIL: Key (id)=(1000) is still referenced from table "grades".
  9. ```
  10.  
  11. Postgres stops you from deleting the restaurant because of the foreign key constraint.
  12. Foreign key constraints stop you from destroying referential integrity of a relational database.
  13.  
  14. ## Find data on a given table
  15. 2. Run `\d restaurants` and `\d grades` and you'll see data about the table.
  16.  
  17. Specifically:
  18. * table column, type, collation, nullable, default
  19. * Indexes
  20. * Referenced by:
  21.  
  22. ```sql
  23. \d restaurants
  24. Table "public.restaurants"
  25. Column | Type | Collation | Nullable | Default
  26. -------------------------+-----------------+-----------+----------+-----------------------------------------
  27. id | integer | | not null | nextval('restaurants_id_seq'::regclass)
  28. name | text | | not null |
  29. nyc_restaurant_id | integer | | |
  30. borough | borough_options | | |
  31. cuisine | text | | |
  32. address_building_number | text | | |
  33. address_street | text | | |
  34. address_zipcode | text | | |
  35. Indexes:
  36. "restaurants_pkey" PRIMARY KEY, btree (id)
  37. Referenced by:
  38. TABLE "grades" CONSTRAINT "grades_restaurant_id_fkey" FOREIGN KEY (restaurant_id) REFERENCES restaurants(id)
  39.  
  40. ```
  41.  
  42. On grades, note the foreign-key constraints:
  43. ```sql
  44. \d grades
  45. Table "public.grades"
  46. Column | Type | Collation | Nullable | Default
  47. ---------------+--------------------------+-----------+----------+------------------------------------
  48. id | integer | | not null | nextval('grades_id_seq'::regclass)
  49. date | timestamp with time zone | | not null |
  50. grade | text | | not null |
  51. score | integer | | not null |
  52. restaurant_id | integer | | |
  53. Indexes:
  54. "grades_pkey" PRIMARY KEY, btree (id)
  55. Foreign-key constraints:
  56. "grades_restaurant_id_fkey" FOREIGN KEY (restaurant_id) REFERENCES restaurants(id)
  57.  
  58. ```
  59.  
  60. ## Creating a table with foreign-key constraints:
  61.  
  62. This is one of the methods to ensure referential integrity. Kind of like throw an error if someone tries to delete a table when it has a relation with another database.
  63. ```sql
  64. CREATE TABLE grades (
  65. id serial PRIMARY KEY,
  66. "date" timestamptz NOT NULL,
  67. grade text NOT NULL,
  68. score integer NOT NULL,
  69. restaurant_id integer REFERENCES restaurants
  70. );
  71. ```
  72.  
  73. ## Create a table with`CASCADE` alternative
  74.  
  75. Another strategy is to create a table with a cascade, which will cause the related entity to be dropped.
  76.  
  77. ```sql
  78. CREATE TABLE grades (
  79. id serial PRIMARY KEY,
  80. "date" timestamp NOT NULL,
  81. grade text NOT NULL,
  82. score integer NOT NULL,
  83. restaurant_id integer REFERENCES restaurants ON DELETE CASCADE
  84. );
  85. ```
  86.  
  87. ## Alter existing `REFERENCES` table to `CASCADE`
  88.  
  89. ```sql
  90. ALTER TABLE grades
  91. DROP CONSTRAINT grades_restaurant_id_fkey,
  92. ADD CONSTRAINT grades_restaurant_id_fkey
  93. FOREIGN KEY (restaurant_id)
  94. REFERENCES restaurants(id)
  95. ON DELETE CASCADE;
  96. ```
  97.  
  98. ### Try it out:
  99.  
  100. 1. `SELECT count(*) FROM grades;`
  101. ```sql
  102. count
  103. -------
  104. 93450
  105. (1 row)
  106. ```
  107. 2. `DELETE FROM restaurants WHERE id = 1000;`
  108. ```sql
  109. DELETE 1
  110. ```
  111. 3. `dev-restaurants-app=# SELECT count(*) from grades;`
  112. ```sql
  113. count
  114. -------
  115. 93446
  116. (1 row)
  117. ```
  118.  
  119. ## Alternatively, set `foreign-key value` to `NULL`
  120.  
  121. ```sql
  122. ALTER TABLE grades
  123. DROP CONSTRAINT grades_restaurant_id_fkey,
  124. ADD CONSTRAINT grades_restaurant_id_fkey
  125. FOREIGN KEY (restaurant_id)
  126. REFERENCES restaurants(id)
  127. ON DELETE SET NULL;
  128. ```
  129.  
  130. Try out commands:
  131.  
  132. ```sql
  133. SELECT * FROM grades WHERE id = 15; -- it has restaurant_id of 10
  134. DELETE FROM restaurants WHERE id = 10;
  135. SELECT * FROM grades WHERE id = 15; -- restaurant_id is null
  136. ```
Add Comment
Please, Sign In to add comment