Errors
Errors in SQL occur when a query or operation fails to execute properly or generates incorrect results.
There are mainly three types of SQL errors. Each of them are discussed below.
Syntax Errors
A syntax error occurs when a SQL query does not follow the correct syntax or violate any linguistic rules, like the use of wrong keywords or wrong clauses.
Runtime Errors
A runtime error occurs when the execution of a query takes place. This is why it is named as runtime error. These errors can occur due to performing inapplicable operations, like division by zero.
Logical Errors
Logical errors arise when a query executes successfully, but produces an incorrect or unintended result due to the existence of logical flaws in it.
Syntax
The below syntax demonstrates how a syntactical error occurs in SQL. Here the word FORM
causes a syntactical error:
SELECT * FORM users;
Example
The following example shows how different errors arise in SQL:
-- Create a table with a UNIQUE constraintCREATE TABLE users (id INT PRIMARY KEY,email VARCHAR(100) UNIQUE);-- Insert data with syntax error-- Insert data with unique constraint violation-- Data type mismatchINSERT INTO users (id, email) VALUES ('three', '[email protected]'); -- Error: 'id' should be an integer
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.