我必须优化我的数据库,它有212个表。一个表主键充当94个表的外键。例如,我有这张表:
create table CUSTOMER (
CUSTOMER_ID BIGINT NOT NULL,
CUSTOMER_NAME VARCHAR(255) NOT NULL UNIQUE,
CONTACT_PERSON VARCHAR(255) NOT NULL,
CUSTOMER_EMAIL VARCHAR(255) NOT NULL,
ADDRESS VARCHAR(255),
CITY VARCHAR(255),
PHONE VARCHAR(255),
FAX VARCHAR(255),
constraint CUSTOMER_PK PRIMARY KEY (CUSTOMER_ID)
);
此处CUSTOMER_ID
充当引用,它是94个表的外键。这是一个表格作为例子:
create table CUSTOMER_STATUS (
CUSTOMER_DETAILS_ID BIGINT NOT NULL,
CUSTOMER_ID BIGINT NOT NULL,
USER_ID BIGINT NOT NULL,
TRIAL_PERIOD BIGINT NOT NULL,
TRIAL_PERIOD_TYPE BIGINT NOT NULL,
EXPIRY_DATE BIGINT NOT NULL,
MAXIMUM_CLIENTS BIGINT NOT NULL,
STATUS BIGINT NOT NULL,
AUTO_AUTHORIZATION BIGINT NOT NULL,
constraint CUSTOMER_STATUS_PK PRIMARY KEY (CUSTOMER_ID,CUSTOMER_DETAILS_ID),
constraint CUSTOMER_STATUS_FK1 FOREIGN KEY (CUSTOMER_ID) references CUSTOMER(CUSTOMER_ID)
);
对于所有表格,我需要将CUSTOMER_ID
数据类型从BIGINT
更改为TINYINT
,但由于外键约束,我收到错误。
错误:
mysql> alter table CUSTOMER MODIFY CUSTOMER_ID TINYINT;
ERROR 1025 (HY000): Error on rename of '.\uds7\#sql-670_cdd' to '.\uds7\customer' (errno: 150)
如何在不影响表数据的情况下进行更改?
答案 0 :(得分:2)
你不能直接这样做。
你要做的事情
CUSTOMER
表主键的所有约束(外键)答案 1 :(得分:0)
您可以在进行架构更改时禁用外键约束检查:
SET foreign_key_checks = 0;
ALTER TABLE CUSTOMER MODIFY CUSTOMER_ID TINYINT;
ALTER TABLE CUSTOMER_STATUS MODIFY CUSTOMER_ID TINYINT;
SET foreign_key_checks = 1;
请注意,重新启用外键检查时,引用列和引用列的数据类型(包括整数大小和符号)必须相同。