我的申请需要5种不同类别的车辆,每种类型的车辆都有一些共同的领域。所以我所做的是为5类车辆vehicle1
,vehicle2
,vehicle3
,vehicle4
,vehicle5
中的每一类创建了5个表格
然后创建了第6个表“车辆”,用于存储每辆车共用的区域。现在,每当我输入与特定车辆相关的信息时,该特定车辆是INSERT INTO
特定车辆的类别表,执行将公共字段插入vehicle
表的触发器。所以触发器看起来像这样
CREATE TRIGGER `tr_vehicle1_info` AFTER INSERT ON `vehicle1`
FOR EACH ROW insert into vehicle(categ,year,make,model,vin,user_id,principal_driver) values (1,new.year,new.make,new.model,new.vin,new.user_id,new.principal_driver)
CREATE TRIGGER `tr_vehicle1_info` AFTER INSERT ON `vehicle2`
FOR EACH ROW insert into vehicle(categ,year,make,model,vin,user_id,principal_driver) values (2,new.year,new.make,new.model,new.vin,new.user_id,new.principal_driver)
CREATE TRIGGER `tr_vehicle1_info` AFTER INSERT ON `vehicle3`
FOR EACH ROW insert into vehicle(categ,year,make,model,vin,user_id,principal_driver) values (3,new.year,new.make,new.model,new.vin,new.user_id,new.principal_driver)
依旧.....
现在的问题是,当我为车辆插入信息时,触发器会执行并且值会插入到表vehicle
中,但对于categ
表中的vehicle
字段,已插入0
。 categ
字段的类型为tinyint(1)
。
我不明白什么是错的。救命?
更新
车辆架构
CREATE TABLE IF NOT EXISTS `vehicle` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`categ` tinyint(1) NOT NULL,
`year` char(4) NOT NULL,
`make` varchar(30) NOT NULL,
`model` varchar(50) NOT NULL,
`vin` varchar(25) NOT NULL,
`user_id` int(11) NOT NULL,
`principal_driver` int(11) DEFAULT NULL,
`secondary_driver` varchar(30) NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `vin` (`vin`,`user_id`)
) ENGINE=InnoDB;
答案 0 :(得分:1)
您的类被定义为单个位:“TINYINT(1)”分配1位来存储整数。因此,您只能存储0或1。 (编辑:我错误的存储分配,我误解了文档。)但老实说,我不明白你为什么要“向后”输入信息。如果你想避免一堆空条目,我会将信息输入到主车辆表中,然后链接记录以打开具有特定于车辆类别的列的表 - 通常我只是根据信息类型重新调整空字段的用途节省空间(如果我不打算通过那些信息搜索,如果有的话),只检索我需要的东西。但我不知道你想要完成什么,所以我不能肯定地说。
编辑:它是否有效,如果没有,你有什么问题?这是我可能会做的(注意:不完整,也没检查): CREATE TABLE IF NOT EXISTS `logistics`.`vehicle` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT ,
`category` TINYINT(4) NOT NULL COMMENT '(4) Allows for 7 vehicle Categories' ,
`v_year` YEAR NOT NULL ,
`v_make` VARCHAR(30) NOT NULL ,
`created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ,
`modified` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `logistics`.`driver` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT ,
`first_name` VARCHAR(45) NOT NULL ,
`middle_name` VARCHAR(45) NULL COMMENT 'helpful in cases of 2 drivers with the exact same first and last' ,
`sir_name` VARCHAR(45) NOT NULL ,
`suffix_name` VARCHAR(45) NULL COMMENT 'rather than \"pollute\" your sir name with a suffix' ,
`license_num` VARCHAR(45) NOT NULL COMMENT 'Always handy in case of claims, reporting, and checking with the DMV, etc.' ,
`license_expiration` DATE NOT NULL COMMENT 'Allows status of driver\'s license report to be run and alert staff of needed to verify updated license' ,
`license_class` CHAR(1) NULL COMMENT 'From what I know classes are \'A\' through \'D\' and usually a single letter. Helpful if needing to assign drivers to vehicles.' ,
`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`modified` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `logistics`.`driver_vehicle` (
`vehicle_id` INT(11) UNSIGNED NOT NULL ,
`driver_id` INT(11) UNSIGNED NOT NULL ,
`principal_driver` TINYINT(1) NOT NULL DEFAULT 'FALSE' COMMENT 'if not specified it will be assumed the driver is not a primary.' ,
`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`modified` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP ,
`admin_id` INT(11) UNSIGNED NOT NULL ,
PRIMARY KEY (`vehicle_id`, `driver_id`) ,
INDEX `fk_driver_vehicle_driver1` (`driver_id` ASC) ,
CONSTRAINT `fk_driver_vehicle_vehicle`
FOREIGN KEY (`vehicle_id` )
REFERENCES `mydb`.`vehicle` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_driver_vehicle_driver1`
FOREIGN KEY (`driver_id` )
REFERENCES `mydb`.`driver` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `logistics`.`vehicle_options` (
`vehicle_id` INT(11) UNSIGNED NOT NULL ,
`option_type` VARCHAR(45) NOT NULL COMMENT 'if certain options are common you could pull by type of option i.e. cosmetic, cargo, hp, weight_capacity, max_speed, etc.' ,
`option_value` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`vehicle_id`, `option_type`) ,
CONSTRAINT `fk_vehicle_options_vehicle1`
FOREIGN KEY (`vehicle_id` )
REFERENCES `mydb`.`vehicle` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
答案 1 :(得分:0)
代码示例中的任何触发器都具有相同的名称是否正确?我建议在原始代码中检查拼写错误。