What is SQLSTATE?
SQLSTATE is a five-character code that identifies a database condition. An error code gives a stable identifier to use alongside the server’s full message.
Read an SQLSTATE code
Section titled “Read an SQLSTATE code”Preserve every character, including letters, when you copy a code.
For example, PostgreSQL uses 23505 for a unique constraint violation and 40P01 for a deadlock.
The message identifies the affected object and gives details that the code alone cannot provide.
PostgreSQL uses 40001 for a serialization failure; see transaction isolation levels for context.
Class and subclass
Section titled “Class and subclass”The first two characters are the class: the general kind of condition. The last three are the subclass: the specific condition inside that class. A subclass of 000 means the class has no more specific condition.
| Code | Class | Subclass |
|---|---|---|
23505 |
23, integrity constraint violation |
505, unique violation |
40001 |
40, transaction rollback |
001, serialization failure |
42P01 |
42, syntax error or access rule violation |
P01, undefined table |
Classes 00, 01 and 02 mean success, warning and no data. Most other classes are errors.
To handle a whole group of errors, test only the class. For example, any code that starts with 23 is a constraint violation.
A code can contain letters, as in 42P01. PostgreSQL uses many of these.
Distinguish codes from error numbers
Section titled “Distinguish codes from error numbers”Database error numbers and SQLSTATEs use separate numbering systems. A MySQL deadlock message illustrates both:
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transactionHere, 1213 is the MySQL error number and 40001 is the SQLSTATE.
Use the database name, code, and message together when looking up an error.
Find recovery steps
Section titled “Find recovery steps”TableAI shows the database cause when a query or data edit fails. Use database error codes to locate the details and select a repair guide.
For execution and transaction outcomes, see write and run SQL. Review error text for credentials and private values before sharing it.