sql_require_primary_key参数详解

MySQL 8.0.13版本新引进一个参数,该参数非常实用,要求表有主键,减少了DBA对于代码的审计。官方文件信息:sql_require_primary_key: Whether tables must have a primary key. Added in MySQL 8.0.13.

实验

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like '%primary%';
+---------------------------------------+-------+
| Variable_name                         | Value |
+---------------------------------------+-------+
| group_replication_single_primary_mode | OFF   |
| sql_require_primary_key               | ON    |
+---------------------------------------+-------+
2 rows in set (0.01 sec)

mysql> use crmsb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table kk(id int,name varchar(20));
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.
mysql> 
mysql> 
mysql> create table kk(id int primary key,name varchar(20));
Query OK, 0 rows affected (0.01 sec)

mysql> 

延伸

MySQL 8.0.20版本又新增了一个参数:REQUIRE_TABLE_PRIMARY_KEY_CHECK,在配置主从复制的过程中可以指定为{ON|OFF|STREAM},官方说法如下:REQUIRE_TABLE_PRIMARY_KEY_CHECK (available as of MySQL 8.0.20) enables a replica to select
its own policy for primary key checks. When the option is set to ON for a replication channel, the replica
always uses the value ON for the sql_require_primary_key system variable in replication operations,
requiring a primary key. When the option is set to OFF, the replica always uses the value OFF for the
sql_require_primary_key system variable in replication operations, so that a primary key is never
required, even if the source required one. When the REQUIRE_TABLE_PRIMARY_KEY_CHECK option is
set to STREAM, which is the default, the replica uses whatever value is replicated from the source for each
transaction.
STREAM:根据主库配置情况制定主键强制检查

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值