0% found this document useful (0 votes)
6 views

SQL_Keys_Normalization_DBA

Uploaded by

mousomray02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL_Keys_Normalization_DBA

Uploaded by

mousomray02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

SQL

• A database is a systematic collection of data and it


supports electronic storage and manipulation of data.
Databases make data management easier.
• Relational databases define database relationships in the
form of tables. It is also called Relational DBMS
(RDBMS). Example - MySQL, Oracle, and Microsoft SQL
Server database etc.
• SQL is Structured Query Language. SQL is the standard
language for dealing with Relational Databases. SQL can be
used to create, insert, search, update, and delete database
records.

1
KEYS in DBMS
• Primary key
• Alternate key
• Candidate key
• Foreign key
• Composite key
• Surrogate key

2
Primary Key

COURSE First LastNam


StudID Course Email
CODE Name e
abc@gm
1 11 PYTHON Tom Price
ail.com
xyz@gm
2 09 C Nick Wright
ail.com
mno@ya
3 11 PYTHON Dana Natan
hoo.com

3
Alternate Key

StudID Roll No Email

1 11 [email protected] Roll No, Email


becomes the
2 12 [email protected] alternative
keys here.
3 13 [email protected]

4
Candidate Key

StudID Roll No First Name LastName Email

1 11 Tom Price [email protected]

2 12 Nick Wright [email protected]

3 13 Dana Natan [email protected]

Stud ID, Roll No, and email are candidate keys which altogether help us
to uniquely identify the student record in the table.

5
Foreign Key

Table: department Table: teacher


DeptCode DeptName Teacher ID Fname Lname DeptCode
001 Physics B002 David Warner 005
002 English B017 Sara Joseph 001
005 Computer B009 Mike Brunton 002

Using this foreign key we can join these tables.


Teacher ID Fname Lname DeptCode DeptName
B002 David Warner 005 Computer
B017 Sara Joseph 001 Physics
B009 Mike Brunton 002 English

6
Composite Key

Course First LastNam


StudID Course Email
code Name e
abc@gm
100 11 PYTHON Tom Price
ail.com
abc@gm
100 12 C Tom Price
ail.com
mno@ya
101 11 JAVA Dana Natan
hoo.com

Here course code and StudID are not showing uniqueness but course
code, studID altogether shows uniqueness to table data. So, these
two attributes are a set of composite key
7
Surrogate Key

8
Joins
• Joins help retrieving data from two or more database tables into a single
result set.
• The tables are mutually related using primary and foreign keys.
• Inner join - The inner JOIN is used to return rows from both tables that satisfy
the given condition.
• Left Join - The LEFT JOIN returns all the rows from the table on the left even
if no matching rows have been found in the table on the right. Where no
matches have been found in the table on the right, NULL is returned.
• Right Join - RIGHT JOIN is obviously the opposite of LEFT JOIN. The RIGHT
JOIN returns all the columns from the table on the right even if no matching
rows have been found in the table on the left. Where no matches have been
found in the table on the left, NULL is returned.
• Outer Join - Outer JOINs return all records matching from both tables .

9
Firstname Lastname Start Time End Time
Anne Smith 09:00 18:00
Jack Francis 08:00 17:00
Anna McLean 11:00 20:00
Shown Willam 14:00 23:00

Id Firstname Lastname Start Time End Time


0001 Anne Smith 09:00 18:00
0002 Jack Francis 08:00 17:00
0003 Anna McLean 11:00 20:00
0004 Shown Willam 14:00 23:00

Here Id is the surrogate key


10
What is normalization of data?
• Data normalization is the technique of organizing data in the
database.
• It is a systematic approach of decomposing tables to eliminate
data redundancy (inconsistent duplicates of the same entry).
• It is multi-step process to put data into tabular form by deleting
duplicate data from its relational table
• It improves data integrity

11
Normalization of data
Why do we normalize data?
• Eliminate repeated data
• To ensure data dependencies make some logical
sense

What happens if we don’t normalize data?


• Consumes more memory
• Difficulties to update database and to handle data
from database

12
Consider this is the dataset :
Cust Item Shipping Supplier Supplier Price
Name Address Phone
Alan Xbox 35, Palm St, Microso (800) BUY- 250
Smith One Miami ft XBOX
Roger PlayStat 47 Campus Sony (800) BUY- 300
Banks ion 4 Rd, Boston SONY

Evan Xbox 28 Rock Av, Wholesa Toll Free 450


Wilson One, PS Denver le
Vita
Alan Playstat 47 Campus Sony (800) BUY- 300
Smith ion 4 Rd, Boston SONY

13
1st Normal Form
• A single cell can not hold multiple values

14
2nd Normal Form
•1st NF
•All attributes should depend on key

15
3rd Normal Form

• 2nd normal form


• No non prime attributes should be dependent on any
non-prime attribute which depends on other non-prime
attributes

• Item depends on supplier (vice-versa)


• supplier depends on supplier phone (vice-versa)

16
3rd Normal Form
17
BCNF (Boyce–Codd normal form)
• 3NF
• For every Functional Dependency, Left Hand Side attribute is super
key.

Not in BCNF

18
In BCNF
4th Normal Form

• A relation will be in 4NF if it is in Boyce Codd normal


form and has no multi-valued dependency.
• For a dependency A → B, if for a single value of A,
multiple values of B exists, then the relation will be a
multi-valued dependency.

19
20
Resources :

https://www.freecodecamp.org/news/learn-sql-queries-database-
query-tutorial-for-beginners/ - SQL queries

https://www.guru99.com/joins.html - joins

https://www.guru99.com/where-clause.html - where clause

https://www.youtube.com/watch?v=ABwD8IYByfk - normalization

21

You might also like