Notes
Notes - notes.io |
create database SQLtest;
use SQLtest;
create table Address(address_id int primary key,
client_id int, street varchar(50),number int,zip_code int,
state varchar(50),country_id int);
create table Client(client_id int primary key,
first_name varchar(50), last_name varchar(50),
age int);
create table Country(country_id int primary key,
name varchar(50));
create table Phone(phone_id int primary key,
client_id int,country_code int,phone varchar(50));
create table [Order](order_id int primary key,
client_id int, [date] date);
create table item(item_id int primary key,
order_id int,description varchar(50),value int,amount int);
-- foreign key constraints
Alter table Address
add constraint fk1 foreign key(client_id) references Client(client_id);
Alter table Address
add constraint fk2 foreign key(country_id) references Country(country_id);
Alter table Phone
add constraint fk3 foreign key(client_id) references Client(client_id);
Alter table [Order]
add constraint fk4 foreign key(client_id) references Client(client_id);
Alter table Item
add constraint fk5 foreign key(order_id) references [Order](order_id);
--Inserting data
insert into Client values(1,'Lebron','James',38)
insert into Client values(2,'James','Harden',31)
insert into Client values(3,'Donovan','Mitchel',24)
insert into Client values(4,'Jason','Tatum',22)
insert into Client values(5,'Stephen','Curry',30)
insert into Client values(6,'Klay','Thompson',29)
insert into Client values(7,'Kevin','Durant',32)
insert into Client values(8,'Kyrie','Irving',30)
insert into Client values(9,'Joel','Embwid',27)
insert into Client values(10,'Micheal','Jordon',50)
insert into Country values(1,'India')
insert into Country values(2,'Russia')
insert into Country values(3,'China')
insert into Country values(4,'Japan')
insert into Country values(5,'USA')
insert into Country values(6,'Brazil')
insert into Country values(7,'Pakistan')
insert into Country values(8,'Saudi Arabia')
insert into Country values(9,'Bangladesh')
insert into Country values(10,'Bhutan')
insert into Address values(1,1,'park street',20,77777,'West Bengal',1)
insert into Address values(2,2,'bbu street',25,10677,'Minnesota',5)
insert into Address values(3,3,'Kfc street',55,10077,'San Francisco',5)
insert into Address values(4,4,'Gandhi Marg',3,89898,'New Delhi',1)
insert into Address values(5,5,'alaung street',09,8777,'Dhaka',9)
insert into Address values(6,6,'51st street',51,17777,'Washington DC',5)
insert into Address values(7,7,'VSS marg',05,76777,'Odisha',1)
insert into Address values(8,8,'Ali street',20,79977,'Karachi',7)
insert into Address values(9,9,'RUshmancgah',01,90777,'Moscow',2)
insert into Address values(10,10,'jflidhj',33,57777,'Xiangjdk',3)
insert into Phone values(1,1,91,'9876545678')
insert into Phone values(2,2,67,'8806545678')
insert into Phone values(3,3,55,'76545678')
insert into Phone values(4,4,91,'876545678')
insert into Phone values(5,5,88,'7867866678')
insert into Phone values(6,6,51,'987678')
insert into Phone values(7,7,91,'7876545678')
insert into Phone values(8,8,45,'676545678')
insert into Phone values(9,9,87,'45678')
insert into Phone values(10,10,99,'876545678')
insert into [Order] values(1,1,'09-12-22')
insert into [Order] values(2,2,'05-18-22') -- done
insert into [Order] values(3,1,'09-12-22')
insert into [Order] values(4,3,'10-12-22')
insert into [Order] values(5,4,'08-11-22')
insert into [Order] values(6,5,'12-12-22')
insert into [Order] values(7,5,'12-12-22')
insert into [Order] values(8,6,'01-01-23')
insert into [Order] values(9,1,'09-12-22')
insert into [Order] values(10,8,'12-18-22')
insert into item values(1,1,'Maggie',12,500)
insert into item values(2,2,'Yippie',10,500)
insert into item values(3,3,'Parle G',5,300)
insert into item values(4,4,'Tiger biscuit',5,500)
insert into item values(5,5,'Surf Excel',24,100)
insert into item values(6,6,'Wheel',10,350)
insert into item values(7,7,'Dove shampoo',40,50)
insert into item values(8,8,'Pantene shampoo',36,20)
insert into item values(9,9,'Conditioner',60,50)
insert into item values(10,10,'Dabur Red',20,200)
--Update
Update Country
set name='Afghanistan' where country_id=4;
Update item
set value=7 where description='Parle G'
-- delete
delete from Phone
where phone_id=10
-- Select
select order_id, first_name as Client_First_Name , last_name as Client_Last_Name,zip_code,state from Address A
inner join (select C.client_id,C.first_name,C.last_name,O.order_id from Client C inner join [Order] O on C.client_id=O.client_id ) X on
A.client_id= X.client_id
select client_id,name as Country_Name from Address inner join Country on Address.country_id=Country.country_id
--Store Procedure by Joining Order, Address, Client
create procedure spAddOrderClient
as
begin
select order_id, first_name as Client_First_Name , last_name as Client_Last_Name,zip_code,state from Address A
inner join (select C.client_id,C.first_name,C.last_name,O.order_id from Client C inner join [Order] O on C.client_id=O.client_id ) X on
A.client_id= X.client_id
end
exec spAddOrderClient
-- parameterized stored procedure
-- Stored procedure takes client id as input and returns the country name of the client as output
create procedure spCountrynameofClientId
@clientId int,
@Countryname varchar(50) output
as
begin
select @Countryname=name from Address A inner join Country C on A.country_id=C.country_id
where A.client_id=@clientId
end
declare @CountryNme varchar(50)
exec spCountrynameofClientId 5,@CountryNme out
print @CountryNme
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team