Advertisement
infogulch

Untitled

Jun 11th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.83 KB | None | 0 0
  1. INSERT INTO parent_of (id, parent_id, isdir, name)
  2. VALUES (1, NULL, 1, 'root')
  3.      , (2, 1, 0, 'file1')
  4.      , (3, 1, 1, 'dir1')
  5.      , (4, 1, 0, 'file2')
  6.      , (5, 1, 1, 'dir2')
  7.      , (6, 5, 0, 'file3')
  8.      , (8, 7, 0, 'file4'); -- last one is orphan
  9. /* paths:
  10. 1   root
  11. 2   root\file1
  12. 3   root\dir1
  13. 4   root\file2
  14. 5   root\dir2
  15. 6   root\dir2\file3
  16. */
  17.  
  18. INSERT INTO parent_of VALUES (7, 5, 1, 'dir3'); -- orphan is resolved after this is inserted
  19. /* paths:
  20. 1   root
  21. 2   root\file1
  22. 3   root\dir1
  23. 4   root\file2
  24. 5   root\dir2
  25. 6   root\dir2\file3
  26. 7   root\dir2\dir3
  27. 8   root\dir2\dir3\file4
  28. */
  29.  
  30. UPDATE parent_of  -- move entire subtree '/dir2' to '/dir1/dir2'
  31. SET parent_id = 3
  32. WHERE id = 5;
  33. /* paths:
  34. 1   root
  35. 2   root\file1
  36. 3   root\dir1
  37. 4   root\file2
  38. 5   root\dir1\dir2
  39. 6   root\dir1\dir2\file3
  40. 7   root\dir1\dir2\dir3
  41. 8   root\dir1\dir2\dir3\file4
  42. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement