2013-11-01 91 views
1

我有一個表horse和一個視圖view_horsehorse表中選擇除主鍵(主鍵是自動遞增整數),然後將其呈現給用戶的每一列,我想插入數據到該視圖的基礎表自然期望主鍵自動生成。但是當我嘗試向其中插入任何數據時,我總是收到一個SQL異常,聲明「視圖view_horse底層沒有默認值」。插入到帶有自動增量主鍵的視圖中?

編輯 -

CREATE TABLE IF NOT EXISTS `TRC`.`horse` (
`horse_id` INT NOT NULL AUTO_INCREMENT, 
`registered_name` VARCHAR(20) NOT NULL, 
`stable_name` VARCHAR(20) NOT NULL, 
`horse_birth_year` DATE NOT NULL, 
`horse_height` DECIMAL(3,1) NOT NULL, 
`horse_location` VARCHAR(50) NOT NULL DEFAULT 'TRC', 
`arrival_date` DATE NOT NULL, 
`passport_no` MEDIUMTEXT NULL, 
`is_deceased` TINYINT(1) NOT NULL, 
`arrival_weight` DECIMAL NOT NULL, 
`horse_sex` VARCHAR(8) NOT NULL, 
`microchip_no` VARCHAR(15) NULL, 
`date_of_death` DATE NULL, 
PRIMARY KEY (`horse_id`), 
INDEX `fk_Horses_SexLookup1_idx` (`horse_sex` ASC), 
CONSTRAINT `fk_Horses_SexLookup1` 
FOREIGN KEY (`horse_sex`) 
REFERENCES `TRC`.`lookup_sex` (`sex`) 
ON DELETE NO ACTION 
ON UPDATE NO ACTION) 
ENGINE = InnoDB 

USE `TRC`; 
CREATE OR REPLACE VIEW `TRC`.`view_horse` AS SELECT 
registered_name AS 'Registered Name', 
stable_name AS 'Stable Name', 
horse_birth_year AS 'Age', 
horse_height AS 'Height', 
arrival_weight AS 'Weight on Arrival', 
horse_sex AS 'Sex', 
horse_location AS 'Location', 
arrival_date AS 'Date of Arrival', 
passport_no AS 'Passport no.', 
microchip_no AS 'Microchip no.', 
is_deceased AS 'Alive?' 
FROM `horse`; 

如果我插入的觀點沒有具體說明實際完成確定的列。但不是當我給視圖中指定的列。

+0

你能張貼了一份sqlfiddle? – Strawberry

回答

0

你不是試圖INSERT到實際的VIEW是你嗎?插入到TABLE中,並從VIEW中選擇。

編輯。算了吧;感謝更正草莓。

+1

您可以插入到視圖中。這很好 - (好吧,有很多警告!);-) – Strawberry

+0

@Strawberry我明白你可以但你爲什麼要? – bendataclear

+0

嗯,這是一個公平的問題...... – Strawberry

相關問題