Note 1-4
Note 1-4
Note 1-4
---------------
student(SID, S_Name, S_Age, S_Address, S_Contact, S_Gender)
1, abc, 10, india, 123, M
2, lily, 20, US, 456, F
ID --> Age
2 --> 25/26/27 = 26
3 --> 25/26/27 = 26
create
alter
drop
-- Command types:
DQL, DDL, DML, DCL
-- Higherarchy
DBMS
Database
Table
Data
(1, CSE301) --> A
(1, CSE306) --> C
Facebook_User(UserName, Password, FirstName, Lastname, MobileNumber, emailaddress,
Age)
(FB_Usr1, 123, Debabrata, Ghosh, 12345, dg@gmail, 35)
(FB_Usr2, a123b, Risha, Halder, 94321, rh@gmail, 24)
(FB_xyz, abcd, Edmund, Anazodo, 98765, ea@yahoo, 28)
problem --> design database --> design tables --> designing procedures -->
designing features --> (Populating data into database)
-- Funtional dependency:
A cell value in a row can uniquely determine the other cell values of the same row
A-->B
B-->C
TFD: A-->C
why normalization?
anomalies without nornalization?
Dependencies
2, SQL, books
2, SQL, cricket
2, SQL, Football
2, SQL, Planting
2, SQL, Dogs
T1(E_ID, Certification)
T2(E_ID, Hobby)
1, Java
1, SQL
1, Python
1, C#
1, ML
2, SQL
1, books
2, books
2, cricket
2, Football
2, Planting
2, Dogs
Roll --> Name, Pin, State, City, District
---------------------------Normalization---------------------------------
-- What is normalization?
Technique to minimize data duplication and increase logical consistency.
Class-3: Practicals
-------------------
-- Zomato
Customer(C_ID, C_Name, C_Address, C_Phone)
1, abc, xyz, 123
2, def, ynx, 456
3, ghi, pko, 789
Class-4: Practicals
-----------------------------
select
t1.Name as EmployeeName,
t1.Salary as EmployeeSalary,
t1.Age as EmplyeeAge,
t2.Location as EmployeeLocation
from
employee as t1
INNER JOIN
Department as t2
ON t1.dept = t2.name
where t2.Location = 'New York'
select * from
dbo.employee as t1
LEFT JOIN
dbo.Department as t2
ON t1.dept = t2.name
GO
select * from
dbo.employee as t1
RIGHT JOIN
dbo.Department as t2
ON t1.dept = t2.name
GO
select * from
dbo.employee as t1
FULL JOIN
dbo.Department as t2
ON t1.dept = t2.name
GO
-- Update
update dbo.employee
set salary = 2500
where id = 5
GO
update employee
set salary = salary + 500
where id = 5
GO
update person
set DOB = '2000-10-20', Address = 'Patna'
where pid in (2,5)
GO
-- give a hike of 100 in salary where employee department location is New York
select *
from dbo.employee as t1
INNER JOIN
dbo.Department as t2
ON t1.dept = t2.name
where t2.Location = 'New York'
GO
update t1
set t1.salary = t1.salary + 100
from dbo.employee as t1
INNER JOIN
dbo.Department as t2
ON t1.dept = t2.name
where t2.Location = 'New York'
GO
delete t1
from dbo.employee as t1
INNER JOIN
dbo.Department as t2
ON t1.dept = t2.name
where t2.Location = 'Kolkata'
GO
-- Merge
Runs insert, update, or delete operations on a target table from the results of a
join with a source table. For example, synchronize two tables by inserting,
updating, or deleting rows in one table based on differences found in the other
table.
-- Create environment
CREATE TABLE SourceProducts(
ProductID INT,
ProductName VARCHAR(50),
Price DECIMAL(9,2)
)
INSERT INTO SourceProducts(ProductID,ProductName, Price) VALUES(1,'Table',100)
INSERT INTO SourceProducts(ProductID,ProductName, Price) VALUES(2,'Desk',80)
INSERT INTO SourceProducts(ProductID,ProductName, Price) VALUES(3,'Chair',50)
INSERT INTO SourceProducts(ProductID,ProductName, Price) VALUES(4,'Computer',300)
GO
CREATE TABLE TargetProducts(
ProductID INT,
ProductName VARCHAR(50),
Price DECIMAL(9,2)
)
INSERT INTO TargetProducts(ProductID,ProductName, Price) VALUES(1,'Table',100)
INSERT INTO TargetProducts(ProductID,ProductName, Price) VALUES(2,'Desk',180)
INSERT INTO TargetProducts(ProductID,ProductName, Price) VALUES(5,'Bed',50)
INSERT INTO TargetProducts(ProductID,ProductName, Price) VALUES(6,'Cupboard',300)
GO
SELECT * FROM SourceProducts
SELECT * FROM TargetProducts
GO
-- Examples:
MERGE TargetProducts AS Target
USING SourceProducts AS Source
ON Source.ProductID = Target.ProductID
-- For Inserts
WHEN NOT MATCHED BY Target THEN
INSERT (ProductID,ProductName, Price)
VALUES (Source.ProductID,Source.ProductName, Source.Price)
-- For Updates
WHEN MATCHED THEN
UPDATE SET
Target.ProductName = Source.ProductName,
Target.Price = Source.Price
-- For Deletes
WHEN NOT MATCHED BY Source THEN
DELETE;
GO