multiple table joins
Entity Creation:
create table product(
pid int identity,
p_name varchar(200),
CONSTRAINT product_pk PRIMARY KEY (pid)
)
create table purchase(
pid int primary key identity,
p_quantity varchar(200),
CONSTRAINT purchase_pk PRIMARY KEY (pid)
)
create table sales(
pid int primary key identity,
p_amount decimal(18,2),
CONSTRAINT sales_pk PRIMARY KEY (pid)
)
Foreign Key Relation:
use SalesDb
alter table purchage add
constraint purchase_fk foreign key(pid) REFERENCES product(pid);
alter table sales add
constraint sales_fk foreign key (pid) REFERENCES product(pid);
joins the tables:
select product.pid,product.p_name,sales.p_amount,purchase.p_quantity from product
inner join purchase on product.pid = purchage.pid
inner join sales on product.pid = sales.pid
view creation:
create view joinsview as
select product.pid,product.p_name,sales.p_amount,purchage.p_quantity from product
inner join purchage on product.pid = purchage.pid
inner join sales on product.pid = sales.pid
select *from joinsview
create table product(
pid int identity,
p_name varchar(200),
CONSTRAINT product_pk PRIMARY KEY (pid)
)
create table purchase(
pid int primary key identity,
p_quantity varchar(200),
CONSTRAINT purchase_pk PRIMARY KEY (pid)
)
create table sales(
pid int primary key identity,
p_amount decimal(18,2),
CONSTRAINT sales_pk PRIMARY KEY (pid)
)
Foreign Key Relation:
use SalesDb
alter table purchage add
constraint purchase_fk foreign key(pid) REFERENCES product(pid);
alter table sales add
constraint sales_fk foreign key (pid) REFERENCES product(pid);
joins the tables:
select product.pid,product.p_name,sales.p_amount,purchase.p_quantity from product
inner join purchase on product.pid = purchage.pid
inner join sales on product.pid = sales.pid
view creation:
create view joinsview as
select product.pid,product.p_name,sales.p_amount,purchage.p_quantity from product
inner join purchage on product.pid = purchage.pid
inner join sales on product.pid = sales.pid
select *from joinsview
Comments
Post a Comment