I want to create one to optional many relation between following two table.
'FK_transactionmember' FOREGIN KEY ('MemberID') REFERENCES 'member' ('ID')
I create foreign key constrain witch is one-to-many relation(Above). In this case member ID must required for transaction, but member is optional.
How to control this optional situation?
Memebr:
CREATE TABLE `member` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`FirstName` varchar(30) DEFAULT NULL,
`LastName` varchar(30) DEFAULT NULL,
`PermanentAddress` varchar(100) DEFAULT NULL,
`TemporaryAddress` varchar(100) DEFAULT NULL,
`Zip` varchar(30) DEFAULT NULL,
`City` varchar(30) DEFAULT NULL,
`LastVisit` datetime DEFAULT NULL,
`TotalVisit` datetime DEFAULT NULL,
`Active` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Transaction
CREATE TABLE `transaction` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`MemberID` int(11) DEFAULT '0',
`UserID` int(11) DEFAULT NULL,
`Total` float DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
There are (at last) two ways create table like below:
and set
MemberIDtonullwhenever you can’t connect it with member. It’s fine in my opinion but it doesn’t meet BNF requirements or use different table to link both tables like below:and then
transactiontable withoutMemberIDcolumn.Added similar question: Nullable Foreign Key bad practice?.