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

js81xvg6  于 2023-05-05  发布在  Mysql
关注(0)|答案(1)|浏览(187)

当我尝试创建最后一个表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));
xt0899hw

xt0899hw1#

gatt_characteristics的主键由单列char_name组成。
gatt_descriptors中的外键有三列,第一列是char_name
定义外键时,它应该与它引用的表的主键具有完全相同的列。

相关问题