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!