I'm trying to connect the accountID
field in the accounts
table to the foreign key in the customers
table. This is my code so far:
CREATE TABLE IF NOT EXISTS `items` (
`id` int(6) unsigned NOT NULL,
`name` varchar(200) NOT NULL,
PRIMARY KEY (`id`) ) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `accounts` (
`accountID` int(6) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(200) NOT NULL,
`passwordHash` varchar(200) NOT NULL,
PRIMARY KEY (`accountID`) ) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `customers` (
`customerID` int(6) unsigned NOT NULL,
`firstName` varchar(200) NOT NULL,
`lastName` varchar(200) NOT NULL,
`address` varchar(200) NOT NULL,
`accountID` int(6) unsigned NOT NULL,
PRIMARY KEY (`customerID`),
CONSTRAINT `FK_accounts_accountID` FOREIGN KEY (`accountID`)
REFERENCES `accounts` (`accountID`) );
CREATE TABLE IF NOT EXISTS `orders` (
`id` int(6) unsigned NOT NULL,
`date` DATE NOT NULL,
`customerID` int(6) unsigned NOT NULL,
`paymentAmmount` float(6) unsigned NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_customers_customerID` FOREIGN KEY (`customerID`)
REFERENCES `customers` (`customerID`) );
CREATE TABLE IF NOT EXISTS `items-orders` (
`id` int(6) unsigned NOT NULL,
`itemId` int(6) unsigned NOT NULL,
`orderId` int(6) unsigned NOT NULL,
`itemQuantity` int(1) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_items_itemId` (`itemId`),
CONSTRAINT `FK_items_itemId` FOREIGN KEY (`itemId`) REFERENCES `items`
(`id`) ) DEFAULT CHARSET=utf8;
INSERT INTO `items` (`id`, `name`) VALUES
('1', 'super cool huge bell'),
('2', 'iron 20p D&D cube'),
('3', 'Dumbledore wand'),
('4', 'super cool small bell');
INSERT INTO `accounts` (`accountID`, `email`, `passwordHash`) VALUES
('1', 'emailAddress', 'lfadaw'),
('2', 'anotherAddress', 'dafaegdwa'),
('3', 'anotherOne', 'gaeeaw');
INSERT INTO `customers`
(`customerID`, `firstName`, `lastName`, `address`) VALUES
('1', 'moshe', 'rubin', 'haifa'),
('2', 'tanya', 'shelomayev', 'netanya'),
('3', 'itamar', 'ofer', 'haifa');
INSERT INTO `orders` (`id`, `date`, `customerID`, `paymentAmmount`) VALUES
('1', '2019-02-01', '1', '40'),
('2', '2019-02-01', '3', '361'),
('3', '2018-10-14', '2', '10');
INSERT INTO `items-orders` (`id`,`itemId`,`orderId`,`itemQuantity`) VALUES
('1', '1', '3', '2'),
('2', '4', '3', '10'),
('3', '3', '1', '1'),
('4', '2', '2', '1');
I added AUTO_INCREMENT
to the accountID
field, but I still get this error message:
Field 'accountID' doesn't have a default value
.
When I linked the customerID
field between the tables orders
and customers
I didn't get an error.
I'm sure this is a question that was answered before, but all I found was the tip of adding the AUTO_INCREMENT to the field, which didn't work for me. Any help would be appriciated ^^
This is because your customer
table contains an accountID
column but constraint NOT NULL
and without default value.
Therefore, actually it is this statement causing the problem:
INSERT INTO `customers`
(`customerID`, `firstName`, `lastName`, `address`) VALUES
('1', 'moshe', 'rubin', 'haifa'),
('2', 'tanya', 'shelomayev', 'netanya'),
('3', 'itamar', 'ofer', 'haifa');