Aneesh
Aneesh

Reputation: 1733

How do I create tables for the following schema ?

I'm a beginner and am having trouble with respect to the relations. Here I have two tables with 1:infinity relation. I'd appreciate it if someone helps me understand how to create the tables for them.

A has id, name attribute
B has id , email password attribute

A:B = 1:infinity.

How to create this?

Also if A has infinity relation on itself , how will that work out ?

Upvotes: 0

Views: 61

Answers (1)

peterm
peterm

Reputation: 92795

Are you looking for something like this?

CREATE TABLE users
(
  id INT NOT NULL PRIMARY KEY, 
  name VARCHAR(64),
  user_id INT,
  CONSTRAINT fk_users_user_id FOREIGN KEY (user_id) REFERENCES users (id)
);

CREATE TABLE accounts
(
  id INT NOT NULL PRIMARY KEY, 
  user_id INT NOT NULL, 
  email VARCHAR(64), 
  password VARCHAR(32),
  CONSTRAINT fk_accounts_user_id FOREIGN KEY (user_id) REFERENCES users (id)
);

Here is SQLFiddle demo

Upvotes: 1

Related Questions