0

我是 mysql 的新手,并且有随附的代码来生成数据库和表。前 2 个表生成良好,但出现上述错误(在主题行中)。你能帮忙吗?

CREATE DATABASE IF NOT EXISTS dbwms DEFAULT CHARACTER SET `utf8`;

USE dbwms;

CREATE TABLE IF NOT EXISTS t_truck_types (
f_pk_ma_truck_type SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
f_sa_truck_type CHAR(4) NOT NULL UNIQUE, INDEX(f_sa_truck_type),
f_truck_length TINYINT
) ENGINE=InnoDB DEFAULT CHARACTER SET `utf8`;

CREATE TABLE IF NOT EXISTS t_truck_vendors (
f_pk_ma_truck_vendor_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
f_sa_truck_vendor_id SMALLINT UNSIGNED NOT NULL UNIQUE, INDEX(f_sa_truck_vendor_id),    
f_truck_vendor_nickname VARCHAR(12) NOT NULL UNIQUE, INDEX(f_truck_vendor_nickname)
) ENGINE=InnoDB DEFAULT CHARACTER SET `utf8`;

CREATE TABLE IF NOT EXISTS t_trucks (
f_pk_ma_truck_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
f_truck_license_plate VARCHAR(15) NOT NULL UNIQUE, INDEX(f_truck_license_plate),
f_fk_ma_truck_type SMALLINT UNSIGNED NOT NULL,
f_fk_ma_truck_vendor_id SMALLINT UNSIGNED NOT NULL,
FOREIGN KEY (f_fk_ma_truck_type) REFERENCES t_truck_types(f_pk_ma_truck_type),
FOREIGN KEY (f_fk_ma_truck_vendor_id) REFERENCES t_truck_vendors(f_pk_ma_truck_vendor_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET `utf8`;

在此先感谢您的帮助。

斯贝特

4

1 回答 1

1

那是因为f_fk_ma_truck_typeint_trucks定义为UNSIGNED. 主键和外键都应该是同一类型。你可以unsigned删除t_trucks

CREATE TABLE IF NOT EXISTS t_trucks (
f_pk_ma_truck_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
f_truck_license_plate VARCHAR(15) NOT NULL UNIQUE, INDEX(f_truck_license_plate),
f_fk_ma_truck_type SMALLINT NOT NULL,
f_fk_ma_truck_vendor_id SMALLINT UNSIGNED NOT NULL,
FOREIGN KEY (f_fk_ma_truck_type) REFERENCES t_truck_types(f_pk_ma_truck_type),
FOREIGN KEY (f_fk_ma_truck_vendor_id) REFERENCES t_truck_vendors(f_pk_ma_truck_vendor_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET `utf8`;

或在t_truck_types声明中添加

于 2012-06-01T05:27:16.097 回答