Guest User

Untitled

a guest
May 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. CREATE TABLE foo ( x int PRIMARY KEY );
  2. CREATE TABLE bar (
  3. x int,
  4. CONSTRAINT asdf FOREIGN KEY (x) REFERENCES foo(x)
  5. );
  6.  
  7.  
  8. MariaDB [test]> DROP TABLE foo;
  9. ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
  10.  
  11. # CREATE TABLE foo ( x int PRIMARY KEY );
  12. # CREATE TABLE bar ( x int REFERENCES foo );
  13. # DROP TABLE foo CASCADE;
  14. NOTICE: drop cascades to constraint bar_x_fkey on table bar
  15. DROP TABLE
  16.  
  17. CREATE TABLE foo
  18. (
  19. x int PRIMARY KEY
  20. );
  21.  
  22. CREATE TABLE bar
  23. (
  24. x int,
  25. CONSTRAINT asdf FOREIGN KEY (x) REFERENCES foo(x) ON DELETE CASCADE
  26. );
  27.  
  28. INSERT INTO foo VALUES (3), (45), (7);
  29. INSERT INTO bar VALUES(3), (3), (45), (7), (7);
  30.  
  31. DELETE FROM foo WHERE x = 3;
  32.  
  33. SELECT * FROM bar;
  34.  
  35. x
  36. 7
  37. 7
  38. 45
  39.  
  40. ERROR: cannot drop table foo because other objects depend on it
  41. DETAIL: constraint asdf on table bar depends on table foo
  42. HINT: Use DROP ... CASCADE to drop the dependent objects too
  43.  
  44. DROP TABLE foo CASCADE;
Add Comment
Please, Sign In to add comment