Index: error-handling/src/odbc/odbc-functions.lisp
===================================================================
--- error-handling/src/odbc/odbc-functions.lisp	(revision error-handling,1)
+++ error-handling/src/odbc/odbc-functions.lisp	(revision error-handling,3)
@@ -110,4 +110,26 @@
     ))
 
+
+(defun print-odbc-error (condition stream)
+  (with-slots (message code) condition
+    (format stream "[ODBC error] ~a; state: ~a" message code)))
+
+(define-condition odbc-error (error)
+  ((message :initarg :message :reader error-message)
+   (code :initarg :code :reader error-code))
+  (:report print-odbc-error))
+
+(define-condition odbc-foreign-key-error (odbc-error) ())
+
+(defun read-and-throw-error (henv hdbc hstmt)
+  (multiple-value-bind (error-message sql-state)
+      (handle-error (or henv (cffi:null-pointer))
+                    (or hdbc (cffi:null-pointer))
+                    (or hstmt (cffi:null-pointer)))
+    (let ((type (cdr (assoc sql-state '(("23503" . odbc-foreign-key-error))
+                            :test #'string=))))
+      (error (if type type 'odbc-error)
+             :message error-message
+             :code sql-state))))
 
 ;;; rav:
@@ -154,9 +176,5 @@
           (error "[ODBC error] Still executing"))
          (#.$SQL_ERROR
-          (multiple-value-bind (error-message sql-state)
-                               (handle-error (or ,henv (cffi:null-pointer))
-                                             (or ,hdbc (cffi:null-pointer))
-                                             (or ,hstmt (cffi:null-pointer)))
-            (error "[ODBC error] ~a; state: ~a" error-message sql-state)))
+          (read-and-throw-error ,henv ,hdbc ,hstmt))
          (otherwise
            (progn ,result-code ,@body))
Index: error-handling/src/odbc/plain-odbc-package.lisp
===================================================================
--- error-handling/src/odbc/plain-odbc-package.lisp	(revision error-handling,1)
+++ error-handling/src/odbc/plain-odbc-package.lisp	(revision error-handling,3)
@@ -12,4 +12,7 @@
    "COMMON-LISP" #+mcl "CCL" #+cormanlisp "WIN32" "CFFI")
   (:export
+   "ODBC-ERROR"
+   "ODBC-FOREIGN-KEY-ERROR"
+
    "EXEC-QUERY" 
    "EXEC-UPDATE" 
