0

I am trying to set up a constraint so that when INSERT a record into the Item table - a record is inserted into the itemIndustryCodesLookup assignment table.

Is this possible in MySql (InnoDB) Database?

How would I go about setting it up?

This is the insert I would like to be able to make for example. itemPrice can be null because it defaults to 0.00, I want itemIndustryCodesLookupId to actually be the next available itemIndustryCodesLookupId value in the itemIndustryCodesLookup table. Column isEnabled is a Bit column and has a default value of 0.

INSERT INTO `nextcart`.`item`
(`itemId`,
`itemName`,
`itemSku`,
`itemPrice`,
`itemIndustryCodesLookupId`,
`isEnabled`)
VALUES
(
NULL,
'Max Extract All-Terrain Carpet Washer',
'F7452900',
NULL,
NULL,
NULL
);

This is the Alter Statement I ran on the Item table.

ALTER TABLE `item` ADD CONSTRAINT industry_codes_refs FOREIGN KEY (`itemIndustryCodesLookupId`) REFERENCES `itemindustrycodeslookup` (`itemIndustryCodesLookupId`);

This is the error I get: Error Code: 1048. Column 'itemIndustryCodesLookupId' cannot be null

I guess my alternative question is, am I setting this up correctly?

Thanks!

4

2 に答える 2

1

1) "I am trying to set up a constraint so that when INSERT a record into the Item table - a record is inserted into a second assignment table.

Is this possible in MySql (InnoDB) Database?"

To achieve that functionality you must use triggers.

2) You con't use column which contains NULL-s as foreign key which tells you error.

于 2012-12-23T21:14:11.790 に答える
1

I would recommend using triggers. There you can specify the query that will execute once row is inserted to the first table

于 2012-12-23T21:14:26.983 に答える