错误 1005 (HY000): 无法创建表(错误:150"外键约束格式不正确")



当我尝试创建最后一个表gatt_descriptors时,我得到

ERROR 1005 (HY000): Can't create table (errno: 150 "Foreign key constraint is incorrectly formed')

gatt_profiles (primary key 'version')
CREATE TABLE gatt_profiles(
version INT NOT NULL PRIMARY KEY, 
profile_name VARCHAR(25), 
value VARCHAR(25));
gatt_services (primary key 'service_name')
CREATE TABLE gatt_services(
service_name VARCHAR(25) NOT NULL,
uuid VARCHAR(36),
id VARCHAR(36),
declaration_type VARCHAR(10),
advertise BIT,
version INT NOT NULL, 
CONSTRAINT ver_val FOREIGN KEY (version) REFERENCES gatt_profiles(version) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT pk_verser PRIMARY KEY (service_name, version));
gatt_characteristics (primary key 'char_name')
CREATE TABLE gatt_characteristics(
char_name VARCHAR(25) PRIMARY KEY NOT NULL,
uuid VARCHAR(36),
id VARCHAR(36),
val_const BIT,
init_val VARCHAR(25),
var_length BIT,
val_length INT,
read_val VARCHAR(7),
write_val VARCHAR(7),
write_wo_response VARCHAR(7),
reliable_write VARCHAR(7),
notify VARCHAR(7),
indicate VARCHAR(7),
version INT NOT NULL, 
service_name VARCHAR(25) NOT NULL,
FOREIGN KEY (version) REFERENCES gatt_profiles (version) ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (service_name) REFERENCES gatt_services (service_name) ON DELETE RESTRICT ON UPDATE CASCADE);
gatt_descriptors (primary key 'descriptor_name')
CREATE TABLE gatt_descriptors(
descriptor_name VARCHAR(25) NOT NULL,
uuid VARCHAR(36),
id VARCHAR(36),
val_const BIT,
init_val VARCHAR(25),
var_length BIT,
val_length INT,
read_val VARCHAR(7),
write_val VARCHAR(7),
version INT NOT NULL, 
service_name VARCHAR(25) NOT NULL,
char_name VARCHAR(25), 
FOREIGN KEY (char_name, service_name, version) REFERENCES gatt_characteristics (char_name, service_name, version) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT pk_verserchardescr PRIMARY KEY (descriptor_name, char_name, service_name, version));

gatt_characteristics有一个由单列char_name组成的主键

gatt_descriptors中的外键是三列,第一列是char_name

当你定义一个外键时,它应该有与它引用的表的主键完全相同的列。

相关内容

  • 没有找到相关文章