Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.96 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # check_constraints.sh
  4. # --------------------
  5. # Check foreign key contraints on MySQL database.
  6. #
  7. # Written by Frank Vanderhallen, licensed under GPL.
  8.  
  9. if [ -z "$1" ]
  10. then
  11. echo "\nUsage:\n\t./`uname $0` <database> [-h <host>] [-u user] [-p <passwd>]\n"
  12. exit
  13. fi
  14.  
  15. CONSTRAINTS=`mysqldump $* | grep "CREATE\|CONSTRAINT" | sed 's/ /+/g'`
  16.  
  17. for c in $CONSTRAINTS
  18. do
  19. if [ "`echo $c | cut -d '+' -f 3`" = "CONSTRAINT" ]
  20. then
  21. CONSTRAINT=`echo $c | cut -d '+' -f 4 | tr -d '\`'`
  22. CHILD_KEY=`echo $c | cut -d '+' -f 7 | tr -d '()\`,'`
  23. PARENT_TABLE=`echo $c | cut -d '+' -f 9 | tr -d '\`'`
  24. PARENT_KEY=`echo $c | cut -d '+' -f 10 | tr -d '()\`,'`
  25. QUERY="select c.$CHILD_KEY from $CHILD_TABLE as c left join $PARENT_TABLE as p on p.$PARENT_KEY=c.$CHILD_KEY where c.$CHILD_KEY is not null and p.$PARENT_KEY is null;"
  26. echo "Checking table '$CHILD_TABLE' constraint '$CONSTRAINT'"
  27. mysql -verbose $* -e "$QUERY"
  28. else
  29. CHILD_TABLE=`echo $c | cut -d '+' -f 3`
  30. fi
  31. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement