I have created two tables user and contact. The user is the parent and contact is the child table. I refer userId as the foreign key in contact table. I have gone through the following query.
CREATE TABLE user(
userId INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NULL,
phone VARCHAR(50) NULL,
email VARCHAR(50) NULL,
address VARCHAR(150) NULL,
loginName VARCHAR(45) NOT NULL,
password VARCHAR(50) NOT NULL,
role INT(1) NOT NULL DEFAULT 2,
loginStatus INT(1) NOT NULL DEFAULT 1,
PRIMARY KEY(userId),
);
=====Second Table=========
CREATE TABLE contact(
contactId INT NOT NULL AUTO_INCREMENT,
userId INT NULL,
name VARCHAR(50) NULL,
phone VARCHAR(50) NULL,
email VARCHAR(50) NULL,
address VARCHAR(150) NULL,
remark VARCHAR(150) NULL,
PRIMARY KEY(contactId),
CONSTRAINT fk_con_userId FOREIGN KEY (userId)
REFERENCES user (userId)
ON DELETE CASCADE ON UPDATE NO ACTION
);
And to see the column information I write the following query in H2 database.
But my table does not show the userId as the foreign key.