2012-02-14 56 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