yllzon
yllzon

Reputation: 51

How to create a weak entity in mySql?

I want to know how can I create weak entities in mySql by Creating tables, I have the code like this:

CREATE TABLE users(
    user_Id int AUTO_INCREMENT,
    full_Name varchar(60),
    email varchar(30),
    password varchar(30),
    reg_Date timestamp
);

CREATE TABLE personal_Infos(

   ...
);

These are the tables, all I want to Know is how can I Connect to each-other with foreign key and should I create another primary key at the second table? (Second table has some more informations for the first table)

Upvotes: 4

Views: 12327

Answers (1)

Yousaf
Yousaf

Reputation: 29282

should I create another primary key at the second table?

Yes you do need to create a primary key in your second table personal_Infos table and that primary key will become foreign key in your users table.

how can I Connect to each-other with foreign key ?

Suppose your primary key in personal_Infos table is P_id, then you can add it as foreign key in your users table like shown below

CREATE TABLE users(
    user_Id int AUTO_INCREMENT,
    full_Name varchar(60),
    email varchar(30),
    password varchar(30),
    reg_Date timestamp,
    p_id int not null,
FOREIGN KEY (P_Id) REFERENCES personal_Infos(P_Id)
);

Upvotes: 1

Related Questions