0% found this document useful (0 votes)
8 views42 pages

Chapter 6 - Copy - Copy

The document provides an overview of SQL and NoSQL databases, highlighting the differences in structure and flexibility. It discusses the three levels of database architecture and outlines the principles of normalization, including 1NF, 2NF, and 3NF. Additionally, it categorizes SQL commands into DDL, DML, and DCL, providing examples of each type and demonstrating how to manipulate data using various SQL queries.

Uploaded by

csprogrammer19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views42 pages

Chapter 6 - Copy - Copy

The document provides an overview of SQL and NoSQL databases, highlighting the differences in structure and flexibility. It discusses the three levels of database architecture and outlines the principles of normalization, including 1NF, 2NF, and 3NF. Additionally, it categorizes SQL commands into DDL, DML, and DCL, providing examples of each type and demonstrating how to manipulate data using various SQL queries.

Uploaded by

csprogrammer19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL Language

• Chapter 6
SQL VS non SQL database
• SQL databases are relational databases. They store
data in tables with rows and columns, and
relationships between tables are defined using keys.
SQL databases use a structured schema and support
powerful querying with SQL.
• NoSQL databases are non-relational and can store
data in various formats such as key-value pairs,
documents, graphs, or wide-columns. They are more
flexible with their schema and are designed for
scalability and handling large volumes of unstructured
or semi-structured data.
{ "_id": 1,
"name":"Alice",
"email": "alice@[Link]" }
3-level architecture
• External Level (view Level)
• Conceptual Level (logical Level)
• Internal Level (Physical Level)
First Normal Form (1NF)
• 1. All columns contain atomic (indivisible)
values.
• Each column contains values of the same
type.
• There are no repeating groups or arrays.
2NF (Second Normal Form)
• A table is in Second Normal Form if:
• It is already in First Normal Form (1NF)
• Every non-key column depends on the full primary key, not
just part of it.
• This matters only when your primary key is composite (i.e.,
made of two or more columns).

StudentID CourseID StudentName CourseName


1 C1 Ali Math
1 C2 Ali English
2 C1 Sara Math
Third Normal Form (3NF)
two conditions:
• It is already in Second Normal Form (2NF).
• No non-key column is transitively dependent
on the primary key. In other words, all non-key
attributes must depend directly on the
primary key.
EmployeeID EmployeeName DepartmentID DepartmentN DepartmentLoc
1 John Smith 101 Sales New York
2 Alice Johnson 102 Marketing Los Angeles
3 Bob Williams 101 Sales New York
4 Eve Brown 102 Marketing Los Angeles
SQL commands can be classified into
three types.
• Data definition language (DDL)
• Commands used to define a database,
including those for creating, altering, and
dropping tables and establishing constraints.
• Data manipulation language (DML)
• Commands used to maintain and query a
database, including those for updating,
inserting, modifying, and querying data.
SQL commands cont..
• Data control language (DCL)
• Commands used to control a database,
including those for administering privileges
and committing (saving) data.

Category Purpose Common Commands


DDL Define/modify database schema CREATE, ALTER, DROP, TRUNCATE
DML Manipulate data SELECT, INSERT, UPDATE, DELETE
DCL Control access/permissions GRANT, REVOKE
Query:
• Create/drop/backup database
• Drop/Insert/delete/update
• Select UserType as Roll
• Select distinct addresses
• Select all Lecturer
• Select users whose name ends with 'A‘
• Select all Lecturer or Hod
• Select all users, ordered by name ascending
• Select top 2 users, ordered by name descending
• Find the total no of users, ignore duplicates
• Delete a Single Column
• Find the number of products where Price is higher than 20
• lowest price in the Price column
Data definition language
• CREATE
– CREATE TABLE tbl_customer(
Cust_ID int not null,
cust_Name char(25)
constraint PK_cust_id primary key(Cust_ID),
)
• ALTER
– ALTER TABLE tbl_customer ALTER column cust_name varchar(30);
– ALTER TABLE tbl_customer ADD house_number varchar(5)
• DROP
– DROP TABLE tbl_customer
– ALTER TABLE tbl_customer DROP column house_number
– Alter table tbl_order drop FK_cust_id
DDL cont…
• To rename the column name:
• exec sp_rename
‘[Link]',‘NewName'
• EXEC sp_rename
'[Link]',
'NewColumnName', 'COLUMN';
• Insert
• Insert into tbl_book values(‘DB’,’12345’)
• Delete
• Delete from customer where customer_id=2
• Update
• Update customer set name=‘ali’ where
cust_id=5
DDL cont..

• If you want to add a column with a not null


restriction in a table, and there is already data
available in the table then SQL server will not allow
you to add new column with not null restriction. So
you have to set default value for the previous
entries.
• ALTER TABLE tbl_product ADD price DECIMAL(6,2)
NOT NULL DEFAULT 30
• ALTER TABLE tbl_order
ADD CONSTRAINT def_cust_id
DEFAULT FOR cust_id
DML.
• Processing single table
– Select
– From
– Where
– Alias: Other name use for table
• SELECT colunmName AS OtherName FROM tableName
• SELECT price AS ProductPrice FROM tbl_product
DML cont..
• Arithmetic Operations
• Multiply one column with another
column or value
• SELECT price, price*10 AS
increased_price FROM tbl_product
• SELECT price, price*price AS
doubled_price FROM tbl_product
DML cont..
• Using functions
– Mathematical
• MIN, MAX, COUNT, SUM
• SUM, AVG only apply on numbers. But
MIN and MAX can be apply on text field
also.
• SELECT AVG(price) AS avaragePrice FROM
tbl_product
• SELECT MIN(price) AS MiniPrice FROM
tbl_product
• SELECT COUNT(price) AS NumberOfEntry
FROM tbl_product
• SELECT SUM(price) AS SumPrice FROM
tbl_product
DML cont..
• SELECT COUNT (*) FROM tbl_product WHERE
price = 30;
• How many different product were listed of
price 30, and what are they?
• SELECT product_name, COUNT (*) FROM
tbl_product WHERE price = 30; //Error
• SELECT productName, COUNT (*) FROM
Product WHERE UnitPrice = 30000 group by
productName
DML cont..
– String
• LOWER (to change to all lower case), UPPER (to change
to all capital letters), CONCAT (to concatenate)
• SELECT CONCAT(‘This is’,product_description ) AS
ProductName FROM tbl_product.
• SELECT LOWER(product_description ) AS ProductName
FROM tbl_product.
• SELECT UPPER(product_description ) AS ProductName
FROM tbl_product.
DML cont..
• Using Wildcards
• Like ‘% ‘ word match
– Name of the product starts with ‘this’
– SELECT * FROM tbl_product WHERE Product_description LIKE
‘this %'
– Name of the product ends with polish
– SELECT * FROM tbl_product WHERE Product_description LIKE '%
polish‘
• Like ‘_’ for single character
– name of three letters product ends with ‘il’
– SELECT * FROM tbl_product WHERE Product_description
LIKE ‘_il’
DML..
• Comparison Operators
DML.
• Boolean Operators
– AND Joins two or more conditions and returns
results only when all conditions are true.
– OR Joins two or more conditions and returns
results when any conditions are true.
– NOT Negates an expression.
DML…
• Ranges for Qualification
– Which products in the Product table have a
standard price between $200 and $300?
– SELECT ProductDescription, Price FROM
tbl_product WHERE Price > 199 AND Price < 301;
– SELECT ProductDescription, Price FROM
tbl_product WHERE Price BETWEEN 200 AND 300;
DML..
• Using IN and NOT IN with Lists
– SELECT * from tbl_cutomer WHERE
CustomerState IN (‘Kohat’, ‘banu’, ‘peshawar’)
DML..
• Group by clause
– GROUP BY is particularly useful when paired with
aggregate functions, such as SUM or COUNT.
GROUP BY divides a table into subsets (by groups);
then an aggregate function can be used to provide
summary information for that group.
DML.
• Group by
– Group the product according to their price and
show that how many times each price is exits?
– SELECT price, count (price) FROM tbl_product
GROUP BY price
– SELECT product_description, price, COUNT (price)
FROM tbl_product GROUP BY
product_description,price

You might also like