Note 1-4

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 10

Class-1: Theory

---------------
student(SID, S_Name, S_Age, S_Address, S_Contact, S_Gender)
1, abc, 10, india, 123, M
2, lily, 20, US, 456, F

RegisteredCourse(C_ID, C_Name, S_Name)


100, SQL, abc
200, AE, xyz

person(ID, Name, Age)


1, Priya, 25
2, Trimala, 26
3, Mahesh, 26

ID --> Age
2 --> 25/26/27 = 26
3 --> 25/26/27 = 26

create
alter
drop

insert --> add data in the table


update --> modify the existing data from the table
delete --> remove data from the table

user1 can access the table student --> grant


user2 can not access the table student --> revoke

-- what is stale data?


-- What is client server architecture?
-- What is data consistency?
-- how to recover data?
-- What is SSMS(SQL Server Management Studio) and its role?
-- What is RDBMS?

-- 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)

Facebook_User(F_UserName, Password, FirstName, Lastname, MobileNumber,


emailaddress, Age)
(abc, 123, Debu, Ghosh, 99999, [email protected], 36)

personaldetails(ID, firstname, lastname, age, country, gender, DOB, Language,


Interest, hobby, emailaddress)
(1, Debu, Ghosh, 36, India, Male, 21-09-1987, English, CS, Music,
[email protected])

(firstname, lastname, age, country, gender, DOB)


(firstname, lastname, age, country, gender, DOB, Language)
(firstname, lastname, age, country, gender, DOB, Language, Interest)
(firstname, lastname, age, country, gender, DOB, Language, Interest, hobby)

LMS(books, subjects, members, admins, booklocations, reports, pentalty, infra,


etc.)

Order(oid, cid, cName, item, quantity, price)


(1, 1, Debabrata Ghosh, rice, 1, 100)

Customer(cid, cname, address, age, mobile)


(1, Debabrata Ghosh, Kolkata, 35, 12345)

problem --> design database --> design tables --> designing procedures -->
designing features --> (Populating data into database)

(roll_number, course_id) --> non-prime attribute


(Roll_number, Course_id, Name) --> non-prime attribute

Roll --> name, pin, state, city, district


Roll --> pin
pin --> state, city, district
roll --> state, city, district (TFD)

-- Funtional dependency:
A cell value in a row can uniquely determine the other cell values of the same row

-- Super Key: A superkey is a combination of columns that uniquely identifies any


row within a table.
-- Candidate Key: A Candidate key is a subset of Super key without any unnecessary
attributes for uniquely identifying rows.
-- Prime attribute: An attribute that is a part of one of the candidate keys is
known as prime attribute.
-- Non Prime attribute: An attribute that is not part of the candidate keys is
known as non prime attribute.

-- Partial Functional dependency:


When a non prime attribute is functionally dependent on part of candidate key.
ID --> Name, Age, City
ID, Name --> Age, City

-- Full Funtional dependency:


When a non prime attribute is functionally dependent only on the candidate key.

-- Transative Functional Depedency: (Special case)


A functional dependency is transative if it is formed by two funtional depedency.
(A,B,C)

A-->B
B-->C
TFD: A-->C

-- Multivalued Functional Depedency: (Special case)


When there are more than one independent multivalued attribute in a table.
Example:
Model --> --> Year

why normalization?
anomalies without nornalization?
Dependencies

(E_ID, Certification, Hobby)


1, java, books
1, SQL, books
1, Python, books
1, C#, books
1, ML, books

2, SQL, books
2, SQL, cricket
2, SQL, Football
2, SQL, Planting
2, SQL, Dogs

E_ID --> --> Hobby


E_ID --> --> Certification

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

Roll --> State, City, District

Roll --> Pin


Pin --> State, City, District

Class-2: Theory & Practicals


---------------------------------
Decomposition, Normalization
ER Model
SSMS overview
Service Overview

All_details(s_id, s_name, s_age, Trainer_name, Trainer_Age, Trainer_Id)

s_id --> s_name, s_age


Trainer_Id --> Trainer_Name, Trainer_Age

student_details(s_id, s_name, s_age) s_id --> s_name, s_age


trainer_details(Trainer_name, Trainer_Age, Trainer_Id) Trainer_Id --> Trainer_Name,
Trainer_Age

T1(s_id, s_name, s_age, Trainer_name) s_id --> s_name, s_age


T2(Trainer_Age, Trainer_Id) Trainer_Id --> Trainer_Name, Trainer_Age

Table(ID, name, age, gender, address, fingerprint, retina)

Table1(ID, name, age, gender)


Table2(ID, address, fingerprint, retina)

Table = Table1 & Table2

Table(A_Num, name, age, gender, PIN, State, City, District, Locality)


A_Num --> name, age, gender
PIN --> State, City, District, Locality

Table1(A_Num, name, age, gender)


Table2(PIN, State, City, District, Locality)

user(id, name, mobile1, mobile2)


(1, Deb, 123, 456)

Student(Roll, Name, PIN, State, City, District)

Roll -> (Name, PIN, State, City, District) --> FD1


PIN -> (State, City, District) --> FD2

Roll --> PIN


PIN --> State/City/District

Roll --> State/City/District --> TFD

---------------------------Normalization---------------------------------
-- What is normalization?
Technique to minimize data duplication and increase logical consistency.

-- Why to perform normalization?


Remove redundancy from database
Maintain consistency and integrity

-- First Normal Form:


Rule-1: Single valued column
Rule-2: same kind of data in each column
Rule-3: All column name should be unique
Rule-4: FYI - Row order does not matter

-- Second Normal Form:


Rule-1: Table should follow first normal form
Rule-2: No partial depedency in a table

-- Third Normal Form:


Rule-1: Table should follow second normal form
Rule-2: No transative dependency in the table

-- BCNF: Boyce - Codd Normal Form (Extension of third Normal Form)


Rule-1: Table should follow third normal form
Rule-2: For X->Y, X should be the super key of the table

-- Forth Normal Form:


Rule-1: Table should follow BCNF
Rule-2: Table should not have multi valued depedency.

indipendent columns/ unrelated columns, then we should remove them


We should be having only related columns in a table

Server --> SQL Server Service


Client --> SSMS (SQL Server Management Studio)

Client --> Instance Name(Service Name) --> Server

Instance Name: Hostname\ServiceName (mylaptop\hermes)

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

Orders(O_ID, O_Item, O_Status, O_Amount, C_ID)


1, rice, preparing, 50, 4

Create Table Person


(
PID INT NOT NULL,
Name varchar(32) NOT NULL,
Address varchar(64) NOT NULL,
DOB Date NULL,
Primary Key(PID)
)

insert into person (pid, name, address, dob)


values(1,'Mahesh', 'Pune', '2000-01-15')
insert into person
values(2,'Asma', 'Mumbai', '2002-02-10')
insert into person
values
(3,'Nadaf', 'Kolkata', '2005-02-10'),
(4,'Amit', 'Goa', NULL),
(5,'Ekwulu', 'Ranchi', '2002-02-10'),
(6,'Asma', 'Pune', NULL)
insert into person (pid, name, address)
values(7,'Asma', 'Pune')

select distinct BloodGroup from dbo.Person

select * from dbo.Person where Address = 'Kolkata'


select * from dbo.Person where BloodGroup = 'A+'
select * from dbo.Person where PID = 10
select * from dbo.Person where Address = 'Kolkata' AND BloodGroup = 'O+'
select * from dbo.Person where Address = 'Kolkata' OR BloodGroup = 'A+'
select * from dbo.Person where BloodGroup <> 'A+'
select * from dbo.Person where BloodGroup != 'A+'
select * from Person where DOB IS NULL
select * from Person where DOB IS NOT NULL
select * from Person where BloodGroup IS NULL
select * from Person where BloodGroup IS NOT NULL
select * from Person where DOB IS NULL AND BloodGroup IS NULL

select * from dbo.Person where name LIKE 'a%'


select * from dbo.Person where BloodGroup like 'a%'
select * from dbo.Person where Name like '%a'
select * from dbo.Person where Name like '%a%'
select * from dbo.Person where DOB like '1995-%'
select * from dbo.Person where MobileNumber like '%4_'
select * from dbo.Person where Name like '''%'
-- to use ' as a string charecter, we need to use ''
select * from dbo.Person where PID between 2 and 5 -- 2<= PID <= 5
select * from dbo.Person where BloodGroup between 'a+' and 'B+'
select * from dbo.Person where PID >= 2 AND PID <= 5
select * from dbo.Person where BloodGroup IN ('AB+', 'O+', 'B+')
select * from dbo.Person where BloodGroup NOT IN ('AB+', 'O+', 'B+')
select * from dbo.Person where BloodGroup='AB+' OR BloodGroup='O+' OR
BloodGroup='B+'
select * from
dbo.employee as t1
INNER JOIN
dbo.Department as t2
ON t1.dept = t2.name
GO

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

-- T-SQL (Transact SQL)


/* write anything
Commented out */
-- T-SQL programming language is case insensetive

char(5) --> can hold charecter of maximum length of 5


varchar(5) --> can hold charecter of maximum length of 5
-- Delete
delete from dbo.employee where id = 4
delete from dbo.employee where gender = 'M'

-- 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 all the employee records where department location is Kolkata


select *
from dbo.employee as t1
INNER JOIN
dbo.Department as t2
ON t1.dept = t2.name
where t2.Location = 'Kolkata'
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

-- Alter table command


ALTER TABLE dbo.Employee
ADD Experience INT NULL
GO
ALTER TABLE dbo.Employee
DROP COLUMN Experience
GO
ALTER TABLE dbo.Employee
ALTER COLUMN Name varchar(64) NOT NULL
GO

-- Add primary key


ALTER TABLE Person1
ADD PRIMARY KEY CLUSTERED
(
pid ASC
)
-- drop primary key
ALTER TABLE Person1
DROP CONSTRAINT [PK__Person1__DD37D91A6442C810]

-- Temp table OR Hash table


-- use of temptable (store table data temporarily)
-- scope of temptable (only within the session)
-- we can not see the temptable from any other session
-- operation is same as normal table
-- When the temptable will be dropped (As soon as we close the session)

CREATE TABLE #TestTable


(
ProductID INT,
ProductName VARCHAR(50),
Price DECIMAL(9,2)
)
GO
insert into #TestTable values
(1, 'abc', 10.2)
GO
select * from #TestTable
GO

-- Global Temp table


-- Existance of global temp table is across sessins
-- Once source session is termineted, the global temp table is also dropped
CREATE TABLE ##TestTable
(
ProductID INT,
ProductName VARCHAR(50),
Price DECIMAL(9,2)
)
GO
insert into ##TestTable values
(1, 'abc', 10.2)
GO
select * from ##TestTable
GO

You might also like