当我尝试创建最后一个表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));
1条答案
按热度按时间xt0899hw1#
gatt_characteristics
的主键由单列char_name
组成。gatt_descriptors
中的外键有三列,第一列是char_name
。定义外键时,它应该与它引用的表的主键具有完全相同的列。