`CREATE TABLE IF NOT EXISTS` fails with ERROR when `sql_require_primary_key=1 ` and table exists
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
Please answer these questions before submitting your issue. Thanks!
### 1. Minimal reproduce step (Required)
```SQL
mysql> SET SESSION sql_require_primary_key = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @@sql_require_primary_key;
+---------------------------+
| @@sql_require_primary_key |
+---------------------------+
| 0 |
+---------------------------+
1 row in set (0.00 sec)
mysql> CREATE TABLE t_repro (id INT);
Query OK, 0 rows affected (0.02 sec)
mysql> SET SESSION sql_require_primary_key = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @@sql_require_primary_key;
+---------------------------+
| @@sql_require_primary_key |
+---------------------------+
| 1 |
+---------------------------+
1 row in set (0.01 sec)
mysql> CREATE TABLE IF NOT EXISTS t_repro (id INT);
ERROR 3750 (HY000): Unable to create or change a table without a primary key, when the system variable 'sql_require_primary_key' is set. Add a primary key to the table or unset this variable to avoid this message. Note that tables without a primary key can cause performance problems in row-based replication, so please consult your DBA before changing this setting.
```
### 2. What did you expect to see? (Required)
When `sql_require_primary_key = 1` , MySQL allows creating a table with `IF NOT EXISTS` without a primary key, returning a warning (not an error):
```SQL
mysql> SET SESSION sql_require_primary_key = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @@sql_require_primary_key, version();
+---------------------------+-----------+
| @@sql_require_primary_key | version() |
+---------------------------+-----------+
| 0 | 8.4.4 |
+---------------------------+-----------+
1 row in set (0.00 sec)
mysql> CREATE TABLE t_repro (id INT);
Query OK, 0 rows affected (0.07 sec)
mysql> SET SESSION sql_require_primary_key = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @@sql_require_primary_key, version();
+---------------------------+-----------+
| @@sql_require_primary_key | version() |
+---------------------------+-----------+
| 1 | 8.4.4 |
+---------------------------+-----------+
1 row in set (0.01 sec)
mysql> CREATE TABLE IF NOT EXISTS t_repro (id INT);
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> show warnings;
+-------+------+--------------------------------+
| Level | Code | Message |
+-------+------+--------------------------------+
| Note | 1050 | Table 't_repro' already exists |
+-------+------+--------------------------------+
1 row in set (0.00 sec)
```
### 3. What did you see instead (Required)
```SQL
mysql> CREATE TABLE IF NOT EXISTS t_repro (id INT);
ERROR 3750 (HY000): Unable to create or change a table without a primary key, when the system variable 'sql_require_primary_key' is set. Add a primary key to the table or unset this variable to avoid this message. Note that tables without a primary key can cause performance problems in row-based replication, so please consult your DBA before changing this setting.
```
### 4. What is your TiDB version? (Required)
```SQL
mysql> select tidb_version();
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version() |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v9.0.0-beta.2.pre-1225-gc6d1fffb98
Edition: Community
Git Commit Hash: c6d1fffb98c66aca65f9751f2cd01a049092f9d8
Git Branch: master
UTC Build Time: 2026-02-11 02:03:40
GoVersion: go1.25.6
Race Enabled: false
Check Table Before Drop: false
Store: unistore
Kernel Type: Classic |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```
Contributor guide
Assessment
This issue has not been assessed yet.