2012-02-14 55 views
1

我的应用程序需要5种不同类别的车辆,每种车辆都有一些共同的领域。所以我所做的就是为车辆的5个类别vehicle1,vehicle2,vehicle3,vehicle4,,vehicle5 的每一个创建5个表格,然后创建第6个表格“车辆”来存储每个车辆通用的字段。现在,无论何时输入与特定车辆相关的信息(该车辆是INSERT INTO该特定车辆的类别表),都会执行一个触发器,将公共字段插入vehicle表中。所以触发这个样子mysql触发器部分工作

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但对于vehicle表中的categ字段,总是插入0categ字段的类型是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

你能证明你的表的输出中'递减vehicle' – 2012-02-14 07:49:50

+0

@NaveenKumar做 – lovesh 2012-02-14 08:03:59

+0

究竟是什么问题。 – 2012-02-14 08:27:57

回答

0

它是正确的,你的任何代码样本中的触发器具有相同的名称?我会建议检查原始代码中的拼写错误。

+0

如果触发器名称有问题,那么为什么所有其他字段将被输入 – lovesh 2012-02-14 09:15:24

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; 
+0

顺便说一句:我注意到你有2个领域与本质上相同的信息:主要和次要驱动程序。当然我明白你需要一个“主要”保险,但是如果一辆车有两个以上的司机呢?你最终会添加新的列,一些空的驱动程序少的车辆,一些溢出。使用关系数据库的优势是能够对数据进行规范化处理,而不会遇到这种问题。 – 2012-02-14 09:08:46

+0

只有次要的驱动程序可以是多个,所以它是一个varchar,所以我将存储次要驱动程序的ID在由分隔符分隔 – lovesh 2012-02-14 09:13:51

+0

tinyint(1)需要1个字节而不是位 – lovesh 2012-02-14 09:15:12