SQL and NF - Google Docs
SQL and NF - Google Docs
Example:
●
EmployeeID
: An integer that serves as the primary key.
●
FirstName
: A variable character string up to 50 characters, cannot be null.
●
LastName
: A variable character string up to 50 characters, cannot be null.
●
Email
: A variable character string up to 100 characters, must be unique.
●
HireDate
: A date field.
Syntax:
Example:
Stafftable.
Dropping the
● IF EXISTS: To prevent errors if the table does not exist, you can use the
IF
EXISTSclause
(supported in many DBMS).
3/ INSERT
INSERTSyntax
1/ Basic
he
T INSERT
INTOstatement adds a new record toatable.Thesimplestforminvolvesspecifyingthe
table name and providing values for all columns in the order they appear in the table.
Syntax:
INSERT INTO table_name
VALUES (value1, value2, ...);
●
table_name
: The name of the table where the record will be inserted.
●
VALUES
: Specifies the data to insert.
●
value1, value2, ...
: The actual data corresponding to each column in the table.
Example:
●
EmployeeID(INT, Primary Key)
●
FirstName(VARCHAR(50), NOT NULL)
●
LastName(VARCHAR(50), NOT NULL)
●
Email(VARCHAR(100), UNIQUE)
●
HireDate(DATE)
VALUES
...;
Example:
VALUES
Syntax:
Example:
HireDatehasadefaultvalueofthecurrentdate,andyouwanttoinsertanewemployee
Supposethe
HireDate
without specifying the :
Syntax:
INSERT INTO table_name (column1, column2, ...)
SELECT columnA, columnB, ...
FROM another_table
WHERE condition;
Example:
DELETESyntax
Basic
DELETEstatementremovesallrecordsfromatable.However,it'scrucialtouse
Thesimplestformofthe
WHEREclause to specify which records to delete; otherwise,all records will be removed.
the
Syntax:
DELETE FROM table_name
WHERE condition;
●
table_name
: The name of the table from which you want to delete records.
●
WHERE condition
: Specifies the criteria that determine which records to delete.
Example:
Employees
Suppose you have a table named :
EmployeeID1:
To delete the record of the employee with
Syntax:
Example:
WHEREClause
Key Points about the
1. Purpose:
○ Filtering Data: Restricts the result set to only those rows that meet the specified
conditions.
○ UsedWith:Commonlyusedin SELECT UPDATE
, DELETE
, INSERT(withconditions)
,and
statements.
Basic Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Common Operators:
xamples:
E
Selecting Specific Records:
● Retrieves first and last names of employees in Sales hired after January 1, 2022.
LIKE
Pattern Matching with :
Best Practices:
● A
lways Use Precise Conditions: To avoid unintended data manipulation, ensure that your
WHEREclause accurately targets the desired records.
SELECTFirst:Beforeperforming
Testwith UPDATEor
DELETEoperations,runa
SELECTquerywiththe
WHEREconditions to verify which records will be affected.
same
●
se Parentheses for Complex Conditions: When combining multiple logical operators, use
U
parentheses to clarify the order of evaluation.
●
WHERE
(Department
=
'Sales'
OR
Department
=
'Marketing')
AND
HireDate
>
'2022-01-01';
AGGrigate
ORDER BYClause
1.
Purpose:
Syntax:
Example:
SELECT FirstName, LastName, HireDate
FROM Employees
ORDER BY HireDate DESC;
COUNTFunction
2.
Purpose:
eturns the number of rows that match a specified condition. Useful for countingrecordsinatableor
R
grouped data.
Syntax:
Example:
SELECT COUNT(EmployeeID) AS TotalEmployees
FROM Employees;
MAXFunction
3.
Purpose:
Syntax:
Example:
MINFunction
4.
Purpose:
Syntax:
SUMFunction
5.
Purpose:
Syntax:
SELECT SUM(column_name) AS AliasName
FROM table_name
WHERE condition;
Example:
AVGFunction
6.
Purpose:
Syntax:
DISTINCTKeyword
7.
Purpose:
Eliminates duplicate values in the result set, returning only unique records.
Syntax:
Example:
Provides temporary names to columns or tables within a query for better readability or convenience.
Syntax:
olumn Alias:
C
SELECT column_name AS AliasName
FROM table_name;
Table Alias:
Example:
FirstNameto
Renames Nameand
Salaryto
EmployeeSalaryin the result set.
BETWEENOperator
9.
Purpose:
Filters the result set to include values within a specified range (inclusive).
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Example:
Selects employees hired between January 1, 2022, and December 31, 2023.
ANDOperator
10.
Purpose:
WHEREclause.Allconditionsseparatedby
ombinesmultipleconditionsina
C ANDmustbetrueforarow
to be included in the result set.
Syntax:
Example:
SELECT
Department AS Dept,
COUNT(EmployeeID) AS NumEmployees,
MAX(Salary) AS HighestSalary,
AVG(Salary) AS AvgSalary
FROM Employees
WHERE Salary BETWEEN 30000 AND 120000
GROUP BY Department
ORDER BY NumEmployees DESC;
Explanation:
●
DISTINCT Alternative: If you wanted to count unique departments,
COUNT(DISTINCT
Department)could be used.
Departmentis aliased as
● Aliases: Dept
, and aggregate results are given meaningful aliases.
●
BETWEEN
:Filters salaries between 30,000 and 120,000.
●
AND
:Not used directly here but can be added for additional conditions.
●
GROUP BY
:Groups results by department.
●
ORDER BY
:Sorts the results by the number of employees in descending order.
Summary
● O RDER BYClause:Sortsqueryresultsbasedonspecifiedcolumnsinascendingordescending
order.
● AggregateFunctions( C
OUNT MAX
, MIN
, SUM
, AVG
, ):Performcalculationsonasetofvaluesto
return a single value.
● DISTINCTKeyword:Ensures that only unique values are returned in the result set.
● MySQL Aliases:Temporary names for columns or tables to enhance readability.
● BETWEENOperator:Filters records within a specific range.
●
ANDOperator:Combines multiple conditions that must all be true for records to be selected.
y mastering these SQL components, you can perform more precise and efficient data retrieval and
B
analysis in your databases.
What is Normalization?
ormalization is theprocessoforganizingtheattributesandtablesofarelationaldatabasetominimize
N
data redundancy and improve data integrity. It involves decomposing tables into smaller, more
manageable pieces without losing information, ensuring that the database remains efficient and scalable.
Benefits of Normalization
. E
1 liminates Redundancy:Reduces duplicate data, saving storage space.
2. Prevents Anomalies: Avoids insertion, deletion, and update anomalies that can lead to
inconsistent data.
3. Enhances Data Integrity:Ensures that data dependencies make sense and are logical.
4. Improves Query Performance:Streamlined tables can lead to faster query processing.
5. Facilitates Maintenance:Easier to update and maintain the database structure.
Let's delve into each normal form with definitions and examples.
1. A tomicity:Eachcolumncontainsonlyatomic(indivisible)values;therearenorepeatinggroups
or arrays.
2. Uniqueness:Each row is unique, typically enforced by a primary key.
Example
Unnormalized Table:
Issues:
Converting to 1NF
o convertto1NF,ensurethateachfieldcontainsonlyonevalue.Thisofteninvolvescreatingseparate
T
rows for each product in an order.
Explanation:
. It is in 1NF.
1
2. FullFunctionalDependency:Allnon-keyattributesarefullyfunctionallydependentontheentire
primary key (i.e., no partial dependency on a part of the composite primary key).
Key Concepts
F
● unctional Dependency:A relationship where one attribute uniquely determines another.
● Partial Dependency:A non-key attribute depends only on part of a composite primary key.
Example
Issues:
Converting to 2NF
. O
1 rders Table:Contains information about orders.
2. OrderDetails Table:Contains information about products in each order.
Orders Table:
OrderDetails Table:
OrderID Product
1 Laptop
1 Mouse
2 Smartphone
3 Tablet
3 Keyboard
3 Charger
Explanation:
● Orders Table:
OrderIDis the primary key.
CustomerNameand
CustomerAddressdepend
OrderID
fully on .
● O rderDetails Table: The composite primary key is (
O
rderID
,
Product
). No partial
dependencies exist as all attributes depend on the entire primary key.
. It is in 2NF.
1
2. No Transitive Dependencies:No non-key attribute depends on another non-key attribute.
Key Concepts
● T ransitiveDependency:Anon-keyattributedependsonanothernon-keyattribute,whichinturn
depends on the primary key.
Example
1 Laptop
1 Mouse
2 Smartphone
3 Tablet
3 Keyboard
3 Charger
CustomerNameand
Here, CustomerAddressdependon
CustomerID OrderIDorthe
,notdirectlyon
composite key (
OrderID Product
, ), creating transitivedependencies.
Converting to 3NF
OrderID Product
1 Laptop
1 Mouse
2 Smartphone
3 Tablet
3 Keyboard
3 Charger
Explanation:
. It is in 3NF.
1
2. Every determinant is a candidate key.
Key Concepts
D
● eterminant:An attribute or a set of attributes that uniquely determines another attribute.
● Candidate Key:A minimal set of attributes necessary to uniquely identify a row.
venifatableisin3NF,itmightstillhaveanomaliesiftherearedependencieswherenon-candidatekeys
E
determine other attributes. BCNF ensures that all functional dependencies are based on candidate keys.
Example
Table: InstructorCourses
Assumptions:
Issues:
● T CourseID→
hedependency RoomviolatesBCNFbecause
CourseIDisnotacandidatekey
for the table.
Converting to BCNF
To achieve BCNF, decompose the table to ensure that every determinant is a candidate key.
Decomposed Tables:
CourseID Room
C101 R1
C102 R2
C103 R3
InstructorID CourseID
I001 C101
I001 C102
I002 C101
I003 C103
Explanation:
Result:
Both tables are now in BCNF, eliminating the anomaly caused by the original table.
Scenario
Business Context:
small bookstore maintains information about book sales, including details about sales transactions,
A
books, authors, and publishers.
Unnormalized Table:
ransactionI
T ookTitl
B uthorNam
A ublisherNam P
P ublisherAddre uantit
Q aleDat
S
D e e e ss y e
T001 QL
S John Smith TechBooks 100 Tech Ave 3 023-01
2
Basics -15
T002 dvance
A Jane Doe TechBooks 100 Tech Ave 2 023-01
2
d SQL -16
T003 ooking
C ary
M HomePress 200 Home St 5 023-01
2
101 Johnson -17
T004 ardenin
G ary
M HomePress 200 Home St 1 023-01
2
g Tips Johnson -18
T005 QL
S John Smith TechBooks 100 Tech Ave 4 023-01
2
Basics -19
Issues:
R
● PublisherNameand
edundancy: PublisherAddressare repeated for each book.
● Non-Atomicity:All data appears atomic, so 1NF is satisfied.
● Functional Dependencies:
○
TransactionID → BookTitle
, AuthorName
, PublisherName
,
PublisherAddress
Quantity
, SaleDate
,
○
BookTitle→
AuthorName PublisherName
, PublisherAddress
,
○
PublisherName→
PublisherAddress
hetablealreadysatisfies1NFasallcolumnscontainatomicvaluesandeachrowisunique(assuming
T
TransactionIDis unique).
●
BookTitle→
AuthorName PublisherName
, PublisherAddress
,
●
PublisherName→
PublisherAddress
PublisherName PublisherAddress
Explanation:
BookTitle
● Transactions Table:Focuses solely on transactions, linking to .
B
● ooks Table:Contains book-specific information, linking each book to its author and publisher.
PublisherAddressis
● PublishersTable:Containspublisher-specificinformation,ensuringthat
PublisherNamealone.
determined by
Result:
All tables are now inBCNF, eliminating any remaininganomalies and ensuring optimal structure.