SQL Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

“When you smile the things starts falling in place”

SELECT - extracts data from a database


UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index

Link for 30 sql questions:


https://www.edureka.co/blog/interview-questions/sql-query-interview-questions

SELECT * FROM Customers


WHERE Country='Mexico';

SELECT * FROM Customers


WHERE CustomerID=1;

<> means not equal to .... or else we can write !=

------Distinct-----------

- The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes you only want
to list the different (distinct) values.
syntax - :

SELECT DISTINCT column1, column2, ...


FROM table_name;

SELECT COUNT(DISTINCT Country) FROM Customers;

--------WHERE--------------------

- The WHERE clause is used to filter records.


It is used to extract only those records that fulfill a specified condition.

- Text Fields vs. Numeric Fields


SQL requires single quotes around text values (most database systems will also allow
double quotes).
However, numeric fields should not be enclosed in quotes:

SELECT * FROM Customers


WHERE City LIKE 's%';

SELECT * FROM Customers


WHERE City IN ('Paris','London'); ---- For getting results from multiple values

SELECT * FROM Customers


where NOT city = 'Berlin';

select * from Customers


where city = 'Berlin' or city = 'London';

-------------AND, OR and NOT--------------

- The WHERE clause can be combined with AND, OR, and NOT operators.

- The AND and OR operators are used to filter records based on more than one condition:

The AND operator displays a record if all the conditions separated by AND are TRUE.
The OR operator displays a record if any of the conditions separated by OR is TRUE.
The NOT operator displays a record if the condition(s) is NOT TRUE.
- AND Syntax :-

SELECT column1, column2, ...


FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

- OR Syntax:-

SELECT column1, column2, ...


FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

- NOT Syntax

SELECT column1, column2, ...


FROM table_name
WHERE NOT condition;

- Here use parenthesis if the conditions are complex

Q) The following SQL statement selects all fields from "Customers" where country is "Germany"
AND city must be "Berlin" OR "München"

SELECT * FROM Customers


WHERE Country='Germany' AND (City='Berlin' OR City='München');

SELECT * FROM Customers


WHERE NOT Country='Germany' AND NOT Country='USA';

-----------Order By------------

- The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records
in descending order, use the DESC keyword.

SELECT * FROM Customers


ORDER BY Country, CustomerName; ------ This means that it orders by Country, but if some
rows have the same Country, it orders them by CustomerName:

SELECT * FROM Customers


ORDER BY Country ASC, CustomerName DESC;

-------------INSERT INTO-------------

- The INSERT INTO statement is used to insert new records in a table.

- INSERT INTO Syntax

It is possible to write the INSERT INTO statement in two ways:

1. Specify both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);

2. If you are adding values for all the columns of the table, you do not need to specify the
column names in the SQL query. However, make sure the order of the values is in the same
order as the columns in the table. Here, the INSERT INTO syntax would be as follows:

INSERT INTO table_name


VALUES (value1, value2, value3, ...);

- The following SQL statement inserts a new record in the "Customers" table:

Example

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)


VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');

- It is also possible to only insert data in specific columns.

The following SQL statement will insert a new record, but only insert data in the
"CustomerName", "City", and "Country" columns (CustomerID will be updated automatically):

Example
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
-------------------NULL-----------------------

NULL Value - A field with null value is a field with no value


- If a field in a table is optional, it is possible to insert a new record or update a
record without adding a value to this field. Then, the field will be saved with a NULL value.

- Note: A NULL value is different from a zero value or a field that contains spaces. A field with a
NULL value is one that has been left blank during record creation!

- With null we can find out whether any particular coloumn is having any null (empty) values or
not

- It is not possible to test for NULL values with comparison operators, such as =, <, or <>.
We will have to use the IS NULL and IS NOT NULL operators instead.

SELECT CustomerName, ContactName, Address


FROM Customers
WHERE Address IS NULL;

SELECT CustomerName, ContactName, Address


FROM Customers
WHERE Address IS NOT NULL;

-----------------------UPDATE---------------------

Syntax :-

NOTE* - We use SET with Update

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

- Be careful when updating records. If you omit the WHERE clause, ALL records will be
updated!
-------------------DELETE--------------------------

Syntax:-

DELETE FROM table_name WHERE condition;

- Note: Be careful when deleting records in a table! Notice the WHERE clause in the DELETE
statement. The WHERE clause specifies which record(s) should be deleted. If you omit the
WHERE clause, all records in the table will be deleted!

- If we don't use where then all the records will be deleted

- Here we are using where to specify the change... If we don't write where then all the records
will be changed .

- To delete all rows from a table

---DELETE FROM Customers;

-------------------TOP OR LIMIT---------------------------

- The SELECT TOP clause is used to specify the number of records to return.

The SELECT TOP clause is useful on large tables with thousands of records. Returning a large
number of records can impact performance.

- Note: Not all database systems support the SELECT TOP clause. MySQL supports the LIMIT
clause to select a limited number of records, while Oracle uses FETCH FIRST n ROWS ONLY
and ROWNUM.

- SELECT TOP 3 * FROM Customers;


-> It will return the top 3 values from the table

FOR MYSQL

- SELECT * FROM Customers


LIMIT 3;

- adding a where claus will work well with it


SELECT * FROM Customers
WHERE Country='Germany'
LIMIT 3;

---------------------MIN and MAX------------------------

- The MIN() function returns the smallest value of the selected column.

- The MAX() function returns the largest value of the selected column.

SELECT MIN(Price) AS SmallestPrice


FROM Products;

- SELECT MAX(column_name)
FROM table_name
WHERE condition;

------------Count-----AVG--------SUM-----------

- The COUNT() function returns the number of rows that matches a specified criterion.

- SELECT COUNT(column_name)
FROM table_name
WHERE condition;

- The AVG() function returns the average value of a numeric column.

- SELECT AVG(column_name)
FROM table_name
WHERE condition;

- The SUM() function returns the total sum of a numeric column.

- SELECT SUM(column_name)
FROM table_name
WHERE condition;

- Note: NULL values are ignored.


-----------------------LIKE-------------------------------

***WILDCARDS

- The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

- There are two wildcards often used in conjunction with the LIKE operator:

- The percent sign (%) represents zero, one, or multiple characters


- The underscore sign (_) represents one, single character

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2
characters in length

WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least
3 characters in length

WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"

SELECT * FROM Customers


WHERE CustomerName NOT LIKE 'a%';

Q) The following SQL statement selects all customers with a ContactName that starts with "a"
and ends with "o":

SELECT * FROM Customers


WHERE ContactName LIKE 'a%o';

Q) Select all records where the value of the City column does NOT start with the letter "a".

SELECT * FROM Customers


where city not like "a%";

select distinct city from station


where
city like 'a%' or city like 'e%' or city like 'i%' or city like 'o%' or city like 'u%';
-------------------WILDCARDS--------------------------

- The following SQL statement selects all customers with a City starting with "b", "s", or "p":

SELECT * FROM Customers


WHERE City LIKE '[bsp]%';

- The following SQL statement selects all customers with a City starting with "a", "b", or "c":

SELECT * FROM Customers


WHERE City LIKE '[a-c]%';

- The two following SQL statements select all customers with a City NOT starting with "b", "s", or
"p":

SELECT * FROM Customers


WHERE City LIKE '[!bsp]%'; ---- IMP

OR

SELECT * FROM Customers


WHERE City NOT LIKE '[bsp]%';

Q) Select all records where the first letter of the City is an "a" or "c" or "s".

SELECT * FROM Customers


WHERE City LIKE '[acs]%';

Q) Select all records where the first letter of the City starts with anything from an "a" to an "f".

SELECT * FROM Customers


WHERE City LIKE '[a-f]%';

Q) Select all records where the first letter of the City is NOT an "a" or a "c" or an "f".

SELECT * FROM Customers


WHERE City LIKE '[!acf]%';

----------------------------IN-------------------------------

- The IN operator allows you to specify multiple values in a WHERE clause.

- The IN operator is a shorthand for multiple OR conditions.

- IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

or:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

SELECT * FROM Customers


WHERE Country IN ('Germany', 'France', 'UK');

- The following SQL statement selects all customers that are NOT located in "Germany",
"France" or "UK":

SELECT * FROM Customers


WHERE Country NOT IN ('Germany', 'France', 'UK');

---------------BETWEEN----------------------------

- The BETWEEN operator selects values within a given range. The values can be numbers,
text, or dates.

- The BETWEEN operator is inclusive: begin and end values are included.

SELECT * FROM Products


WHERE Price NOT BETWEEN 10 AND 20;
Q) The following SQL statement selects all products with a price between 10 and 20. In
addition; do not show products with a CategoryID of 1,2, or 3:

SELECT * FROM Products


WHERE Price BETWEEN 10 AND 20
AND CategoryID NOT IN (1,2,3);

Q) The following SQL statement selects all products with a ProductName between Carnarvon
Tigers and Chef Anton's Cajun Seasoning:

SELECT * FROM Products


WHERE ProductName BETWEEN "Carnarvon Tigers" AND "Chef Anton's Cajun Seasoning"
ORDER BY ProductName;

BETWEEN DATES EXAMPLE

SELECT * FROM Orders


WHERE OrderDate BETWEEN #07/01/1996# AND #07/31/1996#;

OR

SELECT * FROM Orders


WHERE OrderDate BETWEEN '1996-07-01' AND '1996-07-31';

-------------------ALIASES------------------------------------

- SQL aliases are used to give a table, or a column in a table, a temporary name.

- Aliases are often used to make column names more readable.

- An alias only exists for the duration of that query.

- An alias is created with the AS keyword.

***NOTE****** IF the Alias name contains space then use double quotes or square brackets

The following SQL statement creates an alias named "Address" that combine four columns
(Address, PostalCode, City and Country):
EG:
SELECT CustomerName AS Customer, ContactName AS [Contact Person]
FROM Customers;

SELECT CustomerName, Address + ', ' + PostalCode + ' ' + City + ', ' + Country AS Address
FROM Customers; -----------> V.IMP

----------------------------JOINS--------------------------------------

- A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.

- (INNER) JOIN: Returns records that have matching values in both tables

- LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from
the right table

- RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from
the left table

- FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate


FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

------------------------------INNER JOIN-----------------------------------

Syntax:

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
--Inner Join on three tables

SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName


FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);

- The RIGHT JOIN keyword returns all records from the right table (Employees), even if there
are no matches in the left table (Orders).

- The Left JOIN keyword returns all records from the Join table (Employees), even if there are
no matches in the right table (Orders).

- The FULL OUTER JOIN keyword returns all matching records from both tables whether the
other table matches or not. So, if there are rows in "Customers" that do not have matches in
"Orders", or if there are rows in "Orders" that do not have matches in "Customers", those rows
will be listed as well.

-----------------SELF Join----------------------------

- A self join is a regular join, but the table is joined with itself.

- Self Join Syntax:

SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;

SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2,


A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;
--------------------------Union------------------------------

- The UNION operator is used to combine the result-set of two or more SELECT statements.

- Every SELECT statement within UNION must have the same number of columns
- The columns must also have similar data types
- The columns in every SELECT statement must also be in the same order

- Syntax:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

EX:

SELECT column_name(s) FROM table1


UNION ALL
SELECT column_name(s) FROM table2;

- The UNION operator selects only distinct values by default. To allow duplicate values, use
UNION ALL:

- Syntax for Union All:


SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

EX:

SELECT City FROM Customers


UNION ALL
SELECT City FROM Suppliers
ORDER BY City;

Union with where

SELECT City, Country FROM Customers


WHERE Country='Germany'
UNION ALL
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;

SELECT 'Customer' AS Type, ContactName, City, Country


FROM Customers
UNION
SELECT 'Supplier', ContactName, City, Country
FROM Suppliers;

-----------------------------GROUP By---------------------------

- The GROUP BY statement groups rows that have the same values into summary rows, like
"find the number of customers in each country".

- The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(),
SUM(), AVG()) to group the result-set by one or more columns.

- Syntax :-

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

Eg - The following SQL statement lists the number of customers in each country, sorted high to
low:

SELECT COUNT(CustomerID), Country


FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;

Eg - With Join ( The following SQL statement lists the number of orders sent by each shipper )

SELECT Shippers.ShipperName, COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders


LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
GROUP BY ShipperName;
--------------------------------HAVING-------------------------

- The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions.

Syntax :-

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

eg - The following SQL statement lists the number of customers in each country. Only include
countries with more than 5 customers:

SELECT COUNT(CustomerID), Country


FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

eg - with Join

- The following SQL statement lists the employees that have registered more than 10 orders:

SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders


FROM (Orders
INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID)
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 10;

- The following SQL statement lists if the employees "Davolio" or "Fuller" have registered more
than 25 orders:

SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders


FROM Orders
INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
WHERE LastName = 'Davolio' OR LastName = 'Fuller'
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 25;

------------------------EXIST AND NOT EXIST---------------------------


SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID =
Suppliers.supplierID AND Price < 20);

-----------------------CASE-----------------------------------------

- The CASE statement goes through conditions and returns a value when the first condition is
met (like an if-then-else statement). So, once a condition is true, it will stop reading and return
the result. If no conditions are true, it returns the value in the ELSE clause.

- If there is no ELSE part and no conditions are true, it returns NULL.

CASE Syntax
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;

EG :

SELECT OrderID, Quantity,


CASE
WHEN Quantity > 30 THEN 'The quantity is greater than 30'
WHEN Quantity = 30 THEN 'The quantity is 30'
ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;
Here END as is used beacasue it peovides the name for the result column as "Quantity text"

EG: The following SQL will order the customers by City. However, if City is NULL, then order by
Country:

SELECT CustomerName, City, Country


FROM Customers
ORDER BY
(CASE
WHEN City IS NULL THEN Country
ELSE City
END);

--------------------------------------------------------------------SQL NULL
Functions---------------------------------------------------

--------SQL IFNULL(), ISNULL(), COALESCE(), and NVL() Functions--------

1. IFNULL() - Return the specified value IF the expression is NULL, otherwise return the
expression:

Syntax:

- IFNULL(expression, alt_value)

eg: SELECT IFNULL(NULL, "W3Schools.com");

2. COALESCE() - Return the first non-null value in a list:

eg: SELECT COALESCE(NULL, NULL, NULL, 'W3Schools.com', NULL, 'Example.com'); -> the
ans is 'W3Schools.com'

3. ISNULL() - Return the specified value IF the expression is NULL, otherwise return the
expression:

eg: SELECT ISNULL(NULL, 'W3Schools.com');

NOTE -> In Oracle the ISNULL() is same as NVL()


-------------------------------------------Replace()-----------------------------------

- Replace "T" with "M":

eg - SELECT REPLACE('SQL Tutorial', 'T', 'M'); -> The output is 'SQL MuMorial'

eg - SELECT REPLACE('SQL Tutorial', 'SQL', 'HTML'); -> The output is 'HTML Tutorial'

eg - SELECT REPLACE('ABC ABC ABC', 'a', 'c'); -> The output is 'cBC cBC cBC'

Watch from here :-

https://www.youtube.com/watch?v=VxiF_MgePL8

- Diff between truncate and round?

- if the ans is 78.98 for avg(city) the by rounding off as Round(Avg(city)) it will be 78

*****NOTE*****

- If using GROUP BY claus

eg: Select ___ from emp Group By deptname

- if we write this then the value in blank will be either deptname or any value with
aggregate function

eg: select deptname, count(dept) from emp by deptname

- Group by claus always works with having and not with where to know wht see video 58 of gate
smashers

- Cartesian product means CROSS JOIN

- SQL Server FLOOR() Function - The FLOOR() function returns the largest integer value that is
smaller than or equal to a number. (Basically removes decimal values from number)
eg - SELECT FLOOR(25.75) AS FloorValue; answer is 25

- SELECT FLOOR(-13.5) AS FloorValue; the answer is -14

- Ceil(), floor() and replace() see from

Round and Floor definition

- Upper() -> converts the text to upper case

------------------------------------- LOCATE() ------------------------------------------------------

- The LOCATE() function returns the position of the first occurrence of a substring in a string.

- If the substring is not found within the original string, this function returns 0.

- This function performs a case-insensitive search.

eg - SELECT LOCATE("3", "W3Schools.com") AS MatchPosition; -> the ans is 2

---------------------------------------MID()--------------------------------------------------------------

- Extract a substring from a string (start at position 5, extract 3 characters):

eg - SELECT MID("SQL Tutorial", 5, 3) AS ExtractString; -> TUT

select round(sqrt(power(max(lat_n) - min(lat_n),2) + power(max(long_w) - min(long_w),2)),4)


from station;

------------------------------------------------SQUARE()-------------------------------SQRT()-----------------------
---------------------

--------------------------trim()----------------------ltrim()----------------------------rtrim()--------------------------
- Following MySQL query returns the current date and time:

SELECT NOW();

Regex
Syntax: Select col1, col2 from table where columname regexp ‘pattern’

Eg. select city from station where city regexp "^[aeiou]";

Note: be careful while writing patterns, if we write regexp ‘xyz ’ then it will also search for space,
here spacing is provided just for clarity. Do not put space in real life

1. [abc] -> a, b or c (any character listed in square brackets)

2. [^abc] -> except a, b, c

3. [a-z] -> anything between a to z

4. [A-Z] -> anything between A to Z

5. [a-z A-Z] -> a to z, A to Z

6. [0-9] -> 0 to 9

7. ^ -> starting

8. $ -> ending

9. | -> it is like or operation for multiple regex

10. ‘kite$ | poor’ -> ending with kite or has poor together anywhere

11. ^[abc] -> starting from either a or b or c

12. [a-h]e -> search for ae or be or ce …. He


13. abc | def | ghi -> matches pattern of either abc or def or ghi from the word

14. ^ta -> starting with ta

15. an$ -> ending with an

16. [ ca ]h -> search for either ch or ah

17. h [ca] -> search for hc or ha

18. . ->

19. * ->

Quantifiers

[ ]{n} -> any character or number inside the square brackets that occurs n number of times
where n is any whole number here.

[ ]{y,z} -> any character or number inside the square brackets that occurs at least y times but
less than z times

Hacker Rank REGEXP Queries:

1. Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION.
Your result cannot contain duplicates.

select city from station where city regexp "^[aeiou]";

2. Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your
result cannot contain duplicates.

select distinct(city) from station where city regexp "[aeiou]$"

3. Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as
both their first and last characters. Your result cannot contain duplicates.

select distinct(city) from station where city regexp "^[aeiou]" and city regexp
"[aeiou]$";

4. Query the list of CITY names from STATION that do not start with vowels. Your result
cannot contain duplicates.
select distinct(city) from station where city regexp "^[^AEIOU]";

5. Query the list of CITY names from STATION that do not end with vowels. Your result
cannot contain duplicates.

SELECT DISTINCT (CITY) FROM STATION WHERE CITY REGEXP "[^aeiou]$";


6. Query the list of CITY names from STATION that do not start with vowels and do not
end with vowels. Your result cannot contain duplicates.

select distinct(city) from station where city regexp "^[^aeiou]" and city regexp
"[^aieou]$";

DOUBT

(Solve using regexp)


Q-18. Write an SQL query to print details of the Workers whose
FIRST_NAME ends with ‘h’ and contains six alphabets.
Ans.

The required query is:

Select * from Worker where FIRST_NAME like '_____h';

How to change the format of date in sql

Q-25. Write an SQL query to fetch duplicate records having matching data
in some fields of a table.

Ans.

The required query is:

SELECT WORKER_TITLE, AFFECTED_FROM, COUNT(*)


FROM Title
GROUP BY WORKER_TITLE, AFFECTED_FROM

HAVING COUNT(*) > 1;


Theory

Candidate Key -> Candidate key is a single key or a group of multiple keys that
uniquely identify rows in a table.

- Candidate key = Unique values + can be null values

- Among many candidate key we make one Primary Key

- Apart from the primary key rest are called Alternative Key

Primary Key -> Unique values + Not Null

- There should always be one primary key in a table and can't be more
than one.
Foreign Key - It is an attribute or set of attributes that references to
primary key of same table or another table

- It’s not necessary that the name of foreign key attribute should have
the same name as the primary key

- The table with the foreign key is called the child table, and the table with
the primary key is called the referenced or parent table.

- Can be one or more foreign key in a table

- Referential integrity (Imp Concept) -> It means that the foreign key
takes reference from primary key

- The table that has primary key is called Referenced Table


And
The table that has foreign key is called Referencing table
Lecture 11 or video 11 please refer from video or Ananya please
update here

Super Key -> We can define a super key as a set of those keys that identify
a row or a tuple uniquely. The word super denotes the superiority of a key.
Thus, a super key is the superset of a key known as a Candidate key
(discussed in the next section). It means a candidate key is obtained from a
super key only.
Note: Two or more attributes in a table can together identify a table uniquely, so the
combination of such attributes is nothing but a super key only.

ER Model

Entity - A thing or object which has characteristics or attributes defined


(Denoted by Rectangle).E.g. Student

Attributes- Characteristic of entity (denoted by Oval)


Relationship- Association between 2 entities (By rhombus). Attributes of
relationship table is called descriptive attribute.
TYPES OF ATTRIBUTES-
1. Single- e.g Roll No (single eclipse)
2. Multivalued- e.g Mobile no (double eclipse)
3. Simple- Which cannot be broken further e.g Age
4. Composite/Compound- which can be divided further( e.g Name-
fname, midname,lname)
5. Stored- Which is fixed and cannot be derived with any hint
6. Derived- e.g Age- can be derived from DOB (denoted by dotted
eclipse)
7. Key attribute- It is unique ( denoted by underline words in eclipse)
8. Non Key- normal attributes other than key
9. Required- Mandatory fields e.g student name
10.Optional
11. Complex- Composite+multivalue

According to my understanding ER model is like

- Flow chart that is given to us and accordingly we create the table

Basically scene ye hai ki hum architect hai jisse customer ne apne ghar ka
bule - print diya hai and ab usse mujhe construct karni hai
Ab obvious sa baat hai ki tumhe blueprint design kar ke dene ka bhi ek
pattern hoga aisa nahi ki customer ne bola mere ko garage terris pe
chahiye toh tum bana doge… yahi scene ER mein bhi hai har ek type of
attribute ke liye ek pattern set kiya gaya hai and you have to follow it!

Degree of relationship- is called Cardinality

One to One Relationship

Here the association table( here “has”) has 2 columns definitely( P.K of both
the other linking tables) and these act as foreign key for other tables. And
since there is no repetition (being 1 to 1), any column among these 2 can
be primary key.

- There will be three tables 1. Person


2. Has
3. Passport

- Tables can be reduced here ( e.g has table can be merged with either
of the other 2 table)
- We can take Primary Key in Has table

One To Many Relationship


- The Table can be merged only from MANY side (since only this side will
be unique)

- Here in relationship table - primary key will be from MANY side(as it is not
repetitive) which is also the primary key for Many side table.

Table EXAMPLE is CUSTOMER -> Gives -> Orders

Many to Many-

Here Primary key in association table - combination of P.Ks of the two


tables i.e. composite key

No reduction of table possible

EXAMPLE is STUDENT -> STUDY -> COURSE


NORMALIZATION

Technique to remove or reduce redundancy from table


1. Row level duplicacy - removed by primary key
2. Column level duplicacy- Causes 3 anomalies or problems (
INSERTION, DELETION, UPDATION)

1NF-

Rules
EF CODD says- Table should not contain a multivalued attribute
A relation is in first normal form if every attribute in that relation is a single
valued attribute.

Conversion to 1NF-
- You must define the data items.
- The next step is ensuring that there are no repeating groups of data.
- The final rule of the first normal form, create a primary key for each table.

CLOSURE METHOD

- It is used to find the no of Candidate Keys in a Table

- Prime Attribute means the attribute required to form the Candidate Key

- Non Prime Attribute means the attribute that are not required to form the
Candidate Key

Q1) R (A B C D)

FD { A -> B, B -> C, C -> D }

(Read as A closure)
A+ = A B C D

B+ = B C D

C+ = C D

D+ = D

Candidate Key can only be A as it is the one who is determining the rest of the
attributes
CK { A }

Prime Attribute -> A

Non Prime Attribute -> B, C, D


***NOTE*** if we add anything with Candidate Key then it is called Super Key
Eg: (AB)+ -> Super Key

Q2) R (A B C D)

FD { A -> B, B -> C, C -> D, D -> A }

A+ = A B C D

B+ = B C D A

C+ = C D A B

D+ = A B C D

CK { A, B, C, D } (Because all are determining all the attributes )

Prime Attribute = A, B, C, D

Non Prime Attribute = NULL

Q3) R ( A B C D E )

FD { A -> B, BC -> D, E -> C, D -> A }

*NOTE* Here see the right side value there E is not present, hence in order to put on
right side we have to put in on left side for every attribute
AE+ = A B E C D A

BE+ = B E C D A

CE+ = C E

DE+ = D E C A B

CK { AE, BE, DE}

Primary Attribute- A,D,B,E


Non-Prime- C
FUNCTIONAL DEPENDENCIES

(Determinant) X -> Y (Dependent)

Here we can say

- X determines Y
- Y is determined by X

Functional Dependencies are of two types

a) Trivial

- X -> Y

Where Y is the subset of X


Eg- Sid -> Sid
1. The Trivial values are always true

2. LHS intersects RHS is never null

Eg: Sid, Sname -> Sid

b) Non - Trivial

Here, LHS intersects RHS is always NULL

Eg. Sid -> Sname or Sid -> Semester

Properties of functional dependencies

1. Reflexivity

If Y is subset of X, then X -> Y

2. Augmentation

If X -> Y, then XZ -> YZ

3. Transitive**

If X -> Y and Y -> Z, then X -> Z

4. Union
If X -> Y and X -> Z, then X -> YZ

5. Decomposition

If X -> YZ , then X -> Y and X -> Z

6. Pseudo Transitivity

If X -> Y and W Y -> Z, then WX -> Z

7. Composition

If X -> Y and Z -> W, then XZ -> YW

Validity and Invalidity is always checked in Non functional dependencies


because trivial is kind of reflexive which is always valid

2NF-

Rules-
1.Table should be in 1NF

3. There should be no partial dependency in the relation


(Non prime attribute should be fully functional dependent on Candidate
key)- condition to check partial dependency

EXTRA:

Eg:

R (A B C D E F )

FD { C -> F, E -> A, EC -> D, A -> B }

Find CK - { EC }
Find Prime Attributes - {E, C }
Find Non Prime Attributes - { A, B, D, F }

In above Partial Dependency we have to check one by one


HOW TO KNOW PARTIAL DEPENDENCY IS PRESENT?

Let’s say for C -> F

1. Check whether the left (means C here) is a proper subset of CK or not


-> Yes C is proper subset ok CK

2. Check whether the right (Means F here) is a non prime attribute


-> Yes F is a non prime attribute

by the above it is proven that the table has Partial Dependency, Hence the
table is not in 2NF form, we have to check for all the FD here

**NOTE*** Agar ek bhi humme FD se Partial Dependency mil jaaye then


koi sense nahi banta hai dusre ko check karne ka.. And wahi pe declare
karo ki table 2NF ke form mein nahi hai

3NF

- Table should not contain Transitive Dependency

- Non Prime Attribute cannot determine Non Prime Attribute

- Prime Attribute can determine Prime Attribute no problem in this case

Rules
all Functional Dependencies
LHS of (FD) is CK or Super Key (SK)

OR

For all FD RHS is a Prime Attribute


NOTE
Also Check all the CK of the FD do not Stop after finding one CK

Q1) R ( A B C D )

FD { AB -> C, C -> D }

AB+ (Remember here we have taken B because B is not present on the


RHS of FD ) = A B C D

CK is { AB }

Prime Attribute { A, B }

Non Prime Attribute { C, D }


Solving: here 1st rule is failed and 2nd Failed

Hence the table is not in 3 NF Form

Q2) R ( A B C D )

FD { AB -> CD, D -> A }

AB+ = A B C D

DB+ (Here we replaced A from D because we can see that D is not present
on RHS) = D B A C

CK { AB, DB }
Prime Attributes { A, B, D }

Non Prime Attributes { C }

Solve: 1st rule doesn't satisfy here as AB is CK but D is not CK DB is CK

2nd rule satisfy here as A is a Prime Attribute

Hence the table is in 3NF form

BCNF ( Boyce Codd Normal Form )

Rules

- Should be in 3 NF

LHS of each FD should be a CK or Super Key (SK)

Q) Student ( Roll_no, Name, Voter_id, age )

FD
(
Roll_no -> Name
Roll_no -> Voter_id
Voter_id -> age
Voter_id -> Roll_no
)

Solve -

CK { Roll_no, Voter_id }

1st FD is valid Roll_no is CK


2nd FD is valid Roll_no is CK
3rd FD is valid Voter_id is CK
4th FD is valid Voter_id is CK

Hence the Table is BCNF

BCNF always ensures Dependency Preserving Decomposition?

- Third normal form always ensures “Dependency Preserving


Decomposition” but in BCNF it may or may not (depends on the
question)

- Both third and BCNF ensures lossless decomposition (infact all the
normal forms)

- Questn

R {AB->CD, D->A}
CK - {AB, BD}
PA- {A,B,D}
Non PA- {C}

This relation is in 3NF but not in BCNF

To check dependency preservation- After decomposing the table,


the common attribute in both table should either be the CK of any one
decomposed table or the original table. Now refer the below example

Lossless and Lossy Decomposition-

Spurious tuples- extra tuples which occur after decomposition +


joining the tables. This leads to data inconsistency and hence lossy
decomposition. So to avoid this we follow some rules
Criteria for taking common attribute before decomposition so
that lossless decomposition occure

Common attribute should be CK or SK of either R1 or R2 or both

Rules for lossless join decomposition-


1. R1 U R2 = R
2. R1 ∩ R2 != phi (or empty)
3. Common attribute should be CK or SK of either R1 or R2 or
both

Example of lossy decomposition-


Original table Decomposing into R1 R2 without rules

Joining the decomposed tables with some condition. Here extra tuples appear
which are not in R. This is lossy decomposition.
Quick Revision
Question For Minimal Cover
If any Dependency is in BCNF then it’s clear that it will be in 3rd, 2nd
and 1st Normal Form

To check Highest Normal Form of any table check first BCNF, then
3rd and so on…

Steps to find highest normal form in a question:

1. Find all the CKs of the relation.


2. Write all prime attributes.
3. Write all non-prime attributes.
4. Check fr BCNF- 3NF - 2NF and so on

Equivalence of Functional dependency

1. Check X covers Y - Take left hand side of Y and write its closure from
X
2. Check Y covers X - Take left hand side of X and write its closure from
FDs of Y.
3. If both are true , then X and Y are equivalent

Q1)

Checking whether X is covering Y or not


Checking whether Y is covering X or not

Q2)
Checking X covers Y or not

Checking whether Y covers X or not

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy