Skip to content

Commit 6355d8d

Browse files
docs: error code PG error 42P01 | MySQL error 1064 | MySQL error 1146 (#1048)
* add error code * update * update * rm time
1 parent 0f93546 commit 6355d8d

File tree

5 files changed

+306
-0
lines changed

5 files changed

+306
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
title: 'ERROR 1064 (42000): You Have an Error in Your SQL Syntax'
3+
---
4+
5+
## Error Message
6+
7+
```sql
8+
ERROR 1064 (42000): You have an error in your SQL syntax;
9+
check the manual that corresponds to your MySQL server version
10+
for the right syntax to use near '...' at line N
11+
```
12+
13+
## Description
14+
15+
MySQL error 1064 means the parser could not understand your SQL statement. The `near '...'` portion of the message shows where MySQL gave up trying to parse. Everything before that point was accepted; the error starts at (or just before) the quoted text.
16+
17+
This is the single most common MySQL error. It covers everything from a missing comma to using a reserved word as a column name.
18+
19+
## Causes
20+
21+
- **Typos in SQL keywords.** `SELCET`, `UDPATE`, `FORM` instead of `SELECT`, `UPDATE`, `FROM`.
22+
- **Missing or extra punctuation.** Unmatched parentheses, missing commas between column definitions, trailing commas before a closing parenthesis.
23+
- **Using a reserved word as an identifier.** Column or table names like `order`, `group`, `key`, `status`, `rank`, `index` conflict with MySQL keywords.
24+
- **Version mismatch.** Syntax valid in MySQL 8.0 (like `LATERAL`, window functions, or `WITH` CTEs) fails on MySQL 5.7.
25+
- **Importing a dump from a different engine.** A PostgreSQL or SQL Server dump contains syntax MySQL does not recognize.
26+
- **Wrong string quoting.** Using double quotes for strings instead of single quotes (unless `ANSI_QUOTES` SQL mode is enabled).
27+
- **Invisible characters.** Copy-pasting SQL from a web page or PDF can introduce non-breaking spaces or zero-width characters that look fine but break parsing.
28+
29+
## Solutions
30+
31+
1. **Read the error message carefully.** MySQL tells you exactly where parsing failed. The text after `near` is the first thing it couldn't parse:
32+
33+
```sql
34+
-- Error: ... near 'FROM users WHERE id = 1' at line 1
35+
-- The problem is right before FROM — likely a missing column name or comma
36+
```
37+
38+
2. **Escape reserved words with backticks:**
39+
40+
```sql
41+
-- Fails: order is a reserved word
42+
SELECT * FROM order WHERE id = 1;
43+
44+
-- Works
45+
SELECT * FROM `order` WHERE id = 1;
46+
```
47+
48+
3. **Check for missing commas and parentheses:**
49+
50+
```sql
51+
-- Fails: missing comma between columns
52+
CREATE TABLE users (
53+
id INT PRIMARY KEY
54+
name VARCHAR(100) -- missing comma on previous line
55+
);
56+
57+
-- Works
58+
CREATE TABLE users (
59+
id INT PRIMARY KEY,
60+
name VARCHAR(100)
61+
);
62+
```
63+
64+
4. **Remove trailing commas:**
65+
66+
```sql
67+
-- Fails: trailing comma before closing parenthesis
68+
INSERT INTO users (name, email,) VALUES ('Alice', 'alice@example.com');
69+
70+
-- Works
71+
INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com');
72+
```
73+
74+
5. **Check your MySQL version:**
75+
76+
```sql
77+
SELECT VERSION();
78+
```
79+
80+
If you're using MySQL 5.7 and the query uses 8.0+ syntax (CTEs, window functions, `GROUPING`), rewrite it or upgrade.
81+
82+
6. **Strip invisible characters.** Re-type the query manually in your SQL client rather than pasting from a browser or document.
83+
84+
## Common scenarios
85+
86+
**WordPress and CMS database imports:** Importing a `.sql` dump into a different MySQL version often triggers 1064 on statements like `CREATE TABLE` that use newer column types or options. Check the MySQL version at the source and target.
87+
88+
**ORM-generated queries:** When an ORM builds SQL that references a reserved word as a column name without backticks, you get 1064. Most ORMs have a setting to force-quote identifiers.
89+
90+
**Stored procedures and triggers:** Syntax errors inside `BEGIN...END` blocks are harder to spot. Simplify the procedure to the minimum and add statements back one at a time.
91+
92+
<HintBlock type="info">
93+
94+
Bytebase's [SQL Review](https://www.bytebase.com/docs/sql-review/review-rules/) checks SQL syntax and common mistakes before changes reach your database. You can configure rules for [MySQL naming conventions](/reference/mysql/how-to/top-mysql-commands-with-examples/) and reserved word usage.
95+
96+
</HintBlock>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
title: "ERROR 1146 (42S02): Table Doesn't Exist"
3+
---
4+
5+
## Error Message
6+
7+
```sql
8+
ERROR 1146 (42S02): Table 'mydb.users' doesn't exist
9+
```
10+
11+
## Description
12+
13+
MySQL error 1146 means the query references a table that MySQL cannot find in the specified database. The error message includes the fully qualified name (`database.table`) so you can see exactly which table and database MySQL looked in.
14+
15+
## Causes
16+
17+
- **Typo in the table name.** `SELECT * FROM uesrs` fails when the table is `users`.
18+
- **Wrong database.** The table exists in `production` but the connection is to `staging`. MySQL only sees tables in the currently selected database (or explicitly qualified ones).
19+
- **Table was dropped or never created.** A migration failed, a manual `DROP TABLE` happened, or the CREATE statement was never run.
20+
- **Case sensitivity on Linux.** MySQL table names map to files on disk. On Linux (case-sensitive filesystem), `Users` and `users` are different tables. On macOS and Windows, they're the same.
21+
- **InnoDB data dictionary mismatch.** The `.frm` file (MySQL 5.7 and earlier) or the InnoDB data dictionary (MySQL 8.0+) is out of sync with the actual tablespace files. This happens after a crash, incomplete restore, or manual file manipulation.
22+
- **Corrupted table.** Improper server shutdown, disk failure, or interrupted operations can corrupt table metadata.
23+
- **Missing privileges.** If the user has no privileges on the table at all, MySQL may report it as not existing rather than access denied (depending on configuration).
24+
25+
## Solutions
26+
27+
1. **Verify the table exists:**
28+
29+
```sql
30+
-- List all tables in the current database
31+
SHOW TABLES;
32+
33+
-- Search across databases
34+
SELECT table_schema, table_name
35+
FROM information_schema.tables
36+
WHERE table_name LIKE '%users%';
37+
```
38+
39+
2. **Check which database you're connected to:**
40+
41+
```sql
42+
SELECT DATABASE();
43+
44+
-- Switch to the correct database
45+
USE production;
46+
```
47+
48+
3. **Check case sensitivity:**
49+
50+
```sql
51+
-- Check the server setting
52+
SHOW VARIABLES LIKE 'lower_case_table_names';
53+
```
54+
55+
| Value | Behavior |
56+
|-------|----------|
57+
| 0 | Case-sensitive (Linux default) |
58+
| 1 | Stored lowercase, compared case-insensitively (Windows default) |
59+
| 2 | Stored as given, compared case-insensitively (macOS default) |
60+
61+
4. **Run pending migrations:**
62+
63+
```bash
64+
# Check migration status with your tool
65+
flyway info
66+
# Or check in Bytebase's change history
67+
```
68+
69+
5. **Check and repair tables after a crash:**
70+
71+
```sql
72+
-- MySQL 8.0+: check table status
73+
CHECK TABLE mydb.users;
74+
75+
-- For MyISAM tables
76+
REPAIR TABLE mydb.users;
77+
```
78+
79+
```bash
80+
# Check all tables in a database
81+
mysqlcheck -u root -p --check mydb
82+
```
83+
84+
6. **Restore from backup if table data is lost:**
85+
86+
```bash
87+
# Restore a single table from a mysqldump backup
88+
mysql -u root -p mydb < backup.sql
89+
```
90+
91+
7. **Verify user privileges:**
92+
93+
```sql
94+
SHOW GRANTS FOR CURRENT_USER;
95+
```
96+
97+
## Common scenarios
98+
99+
**After server crash or unclean shutdown:** InnoDB tables may show as missing if the redo log recovery didn't complete. Restart MySQL and check the error log (`/var/log/mysql/error.log`) for recovery messages. If the tablespace file (`.ibd`) still exists, InnoDB will usually recover the table on restart.
100+
101+
**During mysqldump or backup:** If a table is dropped by another session while mysqldump is running, the dump will fail with 1146. Use `--single-transaction` for InnoDB tables to get a consistent snapshot.
102+
103+
**In Docker or Kubernetes:** Containers with ephemeral storage lose all data on restart. If you see 1146 after a container restart, the database volume was not persisted. Mount a volume for `/var/lib/mysql`.
104+
105+
<HintBlock type="info">
106+
107+
Bytebase tracks all schema changes with a full [audit trail](https://www.bytebase.com/docs/change-database/change-workflow/), so you can identify when and by whom a table was dropped. For MySQL installation and client setup, see [How to install MySQL client](/reference/mysql/how-to/how-to-install-mysql-client-on-mac-ubuntu-centos-windows/).
108+
109+
</HintBlock>

content/reference/mysql/error/_layout.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@
3131

3232
## Query Syntax & Logic
3333

34+
### [ERROR 1064 (42000): You have an error in your SQL syntax](/reference/mysql/error/1064-syntax-error)
35+
3436
### [ERROR 1055 (42000): SELECT list is not in GROUP BY clause and contains nonaggregated column](/reference/mysql/error/1055-select-list-not-in-group-by)
3537

3638
### [ERROR 1111 (HY000): Invalid use of group function](/reference/mysql/error/1111-invalid-use-of-group-function)
3739

3840
### [ERROR 1175 (HY000): You are using safe update mode](/reference/mysql/error/1175-using-safe-update-mode)
3941

42+
## Table & Schema
43+
44+
### [ERROR 1146 (42S02): Table doesn't exist](/reference/mysql/error/1146-table-doesnt-exist)
45+
4046
## Data Integrity
4147

4248
### [ERROR 1062 (23000): Duplicate entry](/reference/mysql/error/1062-duplicate-entry)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: 'ERROR 42P01: Relation Does Not Exist in Postgres'
3+
---
4+
5+
## Error Message
6+
7+
```sql
8+
ERROR: relation "users" does not exist
9+
SQLSTATE: 42P01
10+
```
11+
12+
## Description
13+
14+
PostgreSQL raises error 42P01 when a query references a table, view, or other relation that the database cannot find. The full SQLSTATE code is 42P01 (`undefined_table`). This is one of the most common PostgreSQL errors and almost always comes down to one of a few predictable causes.
15+
16+
## Causes
17+
18+
- **Typo in the table name.** `SELCET * FROM uers` fails because the table is actually `users`.
19+
- **Wrong schema.** The table exists in a non-default schema but the query doesn't qualify it. `SELECT * FROM my_table` won't find `analytics.my_table` unless `analytics` is on the `search_path`.
20+
- **Case sensitivity.** If the table was created with double quotes (`CREATE TABLE "Orders"`), you must always reference it with the same quotes and casing: `SELECT * FROM "Orders"`. Without quotes, PostgreSQL folds identifiers to lowercase.
21+
- **Wrong database.** Each PostgreSQL database is isolated. A table in the `dev` database is not visible from a connection to `prod`.
22+
- **Table not yet created.** Migrations may not have run, or the CREATE TABLE statement failed silently in a script.
23+
- **Dropped table.** Someone (or a migration rollback) dropped the table.
24+
- **Missing permissions.** If the user lacks `USAGE` on the schema, PostgreSQL hides the table entirely and reports it as not existing.
25+
26+
## Solutions
27+
28+
1. **Check the table exists and its exact name:**
29+
30+
```sql
31+
SELECT table_schema, table_name
32+
FROM information_schema.tables
33+
WHERE table_name ILIKE '%users%';
34+
```
35+
36+
2. **Qualify the schema explicitly:**
37+
38+
```sql
39+
SELECT * FROM myschema.users;
40+
```
41+
42+
3. **Check and set the search path:**
43+
44+
```sql
45+
SHOW search_path;
46+
47+
-- Add the schema to the search path
48+
SET search_path TO myschema, public;
49+
```
50+
51+
4. **Handle case-sensitive names:**
52+
53+
```sql
54+
-- If created with double quotes
55+
SELECT * FROM "Orders";
56+
```
57+
58+
5. **Verify you are connected to the correct database:**
59+
60+
```sql
61+
SELECT current_database();
62+
```
63+
64+
6. **Run pending migrations:**
65+
66+
If using a migration tool, check that all migrations have been applied. For example:
67+
68+
```bash
69+
# Check migration status
70+
flyway info
71+
# Or with Bytebase — check the change history in the project dashboard
72+
```
73+
74+
7. **Grant schema permissions if hidden by access control:**
75+
76+
```sql
77+
GRANT USAGE ON SCHEMA myschema TO app_user;
78+
GRANT SELECT ON ALL TABLES IN SCHEMA myschema TO app_user;
79+
```
80+
81+
## Common scenarios
82+
83+
**In ORMs and application frameworks:** ORMs like Django, SQLAlchemy, or Prisma generate SQL referencing tables by model name. If the migration hasn't run or the model-to-table mapping is wrong, you get 42P01 at runtime. Check `\dt` in psql to confirm the table exists, then compare against your ORM's expected table name.
84+
85+
**In CI/CD pipelines:** A test suite connecting to a freshly created database will hit 42P01 if the schema setup step was skipped or failed. Add a pre-test check that verifies expected tables exist before running queries.
86+
87+
**With [Postgres case sensitivity](/blog/postgres-case-sensitivity/):** A table created as `CREATE TABLE "MyTable"` requires double-quoting everywhere. If you can, avoid double-quoted identifiers entirely and use lowercase names.
88+
89+
<HintBlock type="info">
90+
91+
Bytebase's [SQL Review](https://www.bytebase.com/docs/sql-review/review-rules/) can catch common issues like unqualified table references and missing schema prefixes before they reach production. See also [How to list tables in PostgreSQL](/reference/postgres/how-to/how-to-list-tables-postgres/) for quick ways to verify table existence.
92+
93+
</HintBlock>

content/reference/postgres/error/_layout.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
## Schema & Object Management
2525

26+
### [ERROR 42P01: relation does not exist](/reference/postgres/error/42p01-undefined-table-postgres)
27+
2628
### [ERROR 42P07: relation already exists](/reference/postgres/error/42p07-relation-already-exists-postgres)
2729

2830
### [ERROR 2B000: cannot drop role because objects depend on it](/reference/postgres/error/2b000-dependent-privilege-descriptors)

0 commit comments

Comments
 (0)