DATA MANIPULATION
--Populate table manually
INSERT into users (user_name, description) values ('olaola', 'ola');
---Fetch data from table in a database
SELECT sum(quantity) AS sum_of_qty, pizza_id
FROM pizzas
WHERE pizza_id in ('%s')
ORDER BY sum_of_qty DESC
0r
-- Sum of quantity of pizza ordered per pizza_id for all pizzas_id starting with the letter 's'
SELECT sum(quantity) as sum_of_qty, pizza_id
FROM order_details
WHERE pizza_id like ('s%')
GROUP BY pizza_id
ORDER BY sum_of_qty DESC;
--SELECT with DISTINCT
SELECT DISTINCT category FROM pizza_types;
-- SELECT with aliasing
SELECT
pizza_id AS Pizza_number,
quantity as Number_purchases
FROM
order_details
WHERE
quantity > 1;
--SELCT with WHere clause (In, between, like)
SELECT
*
from
order_details
where quantity<>3 or quantity <> 4;
select
*
from
order_details
where
quantity in (1,4);
SELECT
*
from
order_details
where
quantity between 1 and 3;
---- Using wildcards (_,%)
SELECT
*
from
order_details
where
pizza_id like 'm_x%m';
--Import csv file (use this in PSQL tool) (Data integration)
\COPY users FROM 'path' delimiter ',' csv header;
---Exporting Data to CSV:
COPY table_name TO '/path/to/export.csv' DELIMITER ',' CSV HEADER;
---Data Migration from one database to another
--- Insertion of Bulk record
BULK INSERT Products
FROM 'C:\path\to\new_products.csv'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n');
--Edit an existing recod
UPDATE your_table
SET status = 'active'
WHERE id = 1;
--Change table column data type
ALTER TABLE users
ALTER COLUMN new_description TYPE integer;
--Change table column name
ALTER TABLE users
RENAME COLUMN description to user_phone_number;
--Add a new column
ALTER TABLE users
Add Column email_address varchar;
--Delete column
ALTER TABLE users
Drop Column description;
--Delete row(s)
DELETE FROM users
WHERE email_address = '[email protected]';
--Add row (Use insert into)
--Delete table
DROP TABLE users;
--Convert data type of an existing column
ALTER TABLE users
ADD new_user_phone_number INTEGER;
UPDATE users
SET new_user_phone_number = CAST(user_phone_number AS INTEGER)
WHERE user_phone_number IS NOT NULL AND user_phone_number ~ '^[0-9]+$';
Note: The ~ '^[0-9]+$' condition checks whether the VARCHAR value contains only digits.
-- Cerate View for the profit and recaluculate from there
CREATE VIEW profit_table AS (
SELECT
product_category,
CAST(REPLACE(product_price, '$', '') AS numeric) - CAST(REPLACE(product_cost, '$', '') AS
numeric) AS profit,
sales.units*(CAST(REPLACE(product_price, '$', '') - CAST(REPLACE(product_cost, '$', '') AS
total_sales
FROM products
LEFT JOIN
sales
ON
products.product_id=sales.product_id
);
--- Perform aggregation (Sum, Avg, Max, Min, count etc)
SELECT
COUNT (*)
FROM
ORDER_DETAILS;
SELECT
AVG(QUANTITY) AS AVERAG_QUANITY,
SUM(QUANTITY) AS TOTAL_QUANTITY,
MAX(QUANTITY) AS MAXIMUM_QUANTITY,
MIN(QUANTITY) AS MINIMUM_QUANTITY
FROM
ORDER_DETAILS
WHERE
PIZZA_ID = 'THAI_CKN_1';
-- Perform UNION - use union to put distinct values together
SELECT * from january_class
UNION
SELECT * from february_class;
-- UNION ALL - use union to put all values together
SELECT * from january_class
UNION ALL
SELECT * from february_class;
-- Note that the above worked because the tables contain same columns with the same data types
and in the same order
DATABASE MANAGEMENT
Table Creation: Creating new tables to store data with CREATE TABLE statements.
Indexing: Adding indexes to columns for improved query performance.
Data Integrity: Defining constraints like primary keys, foreign keys, and unique constraints to ensure
data quality and consistency.
Backup and Restore: Regularly backing up databases to prevent data loss and restoring them if needed.
Database Security: Managing user access rights, permissions, and roles to ensure data security.
Database Optimization: Analyzing and optimizing queries and database design for better performance.
---Create database
CREATE DATABASE IF NOT EXISTS Restaurant;
---Create table
CREATE TABLE IF NOT EXISTS customer (
cus_id serial PRIMARY KEY NOT NULL,
Customer_name VARCHAR NOT NULL
cus_address VARCHAR NOT NULL);