Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2 views27 pages

Access Create Tables and Insert Data With Tabl1

The document outlines the creation of several database tables including customer, category, publisher, author, book, order_master, order_item, and payment, along with their respective fields and constraints. It also includes SQL queries for data retrieval, aggregation, and analysis across these tables, demonstrating various SQL functionalities such as joins, subqueries, and conditional statements. Additionally, unique indexes are created for certain fields to ensure data integrity and optimize query performance.

Uploaded by

eramkader38
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)
2 views27 pages

Access Create Tables and Insert Data With Tabl1

The document outlines the creation of several database tables including customer, category, publisher, author, book, order_master, order_item, and payment, along with their respective fields and constraints. It also includes SQL queries for data retrieval, aggregation, and analysis across these tables, demonstrating various SQL functionalities such as joins, subqueries, and conditional statements. Additionally, unique indexes are created for certain fields to ensure data integrity and optimize query performance.

Uploaded by

eramkader38
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
You are on page 1/ 27

CREATE.TABLES.AND.INSERT.DATA.WITH.TABLE"S.

RELATIONS
‗‗.Table.7¿.Customer.table
CREATE TABLE customer (
customer_id AUTOINCREMENT PRIMARY KEY,
first_name TEXT(50) NOT NULL,
last_name TEXT(50) NOT NULL,
email TEXT(100) NOT NULL,
phone TEXT(15) NOT NULL,
city TEXT(50) NOT NULL,
registration_date DATETIME DEFAULT Now()
);

‗‗.Create.unique.index.for.email.(Access.doesn҂t.support.UNIQUE.constraint.in.CREATE.
TABLE)
CREATE UNIQUE INDEX idx_customer_email ON customer (email);

‗‗.Table.8¿.Category.table
CREATE TABLE category (
category_id AUTOINCREMENT PRIMARY KEY,
category_name TEXT(100) NOT NULL,
category_description MEMO,
parent_category_id LONG,
created_date DATETIME DEFAULT Now()
);

‗‗.Create.unique.index.for.category( name
CREATE UNIQUE INDEX idx_category_name ON category (category_name);

‗‗.Table.9¿.Publisher.table
CREATE TABLE publisher (
publisher_id AUTOINCREMENT PRIMARY KEY,
publisher_name TEXT(150) NOT NULL,
city TEXT(50),
country TEXT(50) DEFAULT 'Bangladesh',
phone TEXT(15),
email TEXT(100)
);

‗‗.Table.0¿.Author.table
CREATE TABLE author (
author_id AUTOINCREMENT PRIMARY KEY,
first_name TEXT(50) NOT NULL,
last_name TEXT(50) NOT NULL,
email TEXT(100),
phone TEXT(15),
created_date DATETIME DEFAULT Now()
);

‗‗.Table.❶¿.Book.table
CREATE TABLE book (
book_id AUTOINCREMENT PRIMARY KEY,
title TEXT(200) NOT NULL,
isbn TEXT(20),
publication_date DATETIME,
language TEXT(50) DEFAULT 'Bengali',
price CURRENCY NOT NULL,
stock_quantity LONG DEFAULT 0,
description MEMO,
category_id LONG NOT NULL,
publisher_id LONG NOT NULL,
created_date DATETIME DEFAULT Now(),
updated_date DATETIME DEFAULT Now()
);

‗‗.Create.unique.index.for.ISBN
CREATE UNIQUE INDEX idx_book_isbn ON book (isbn);

‗‗.Table.❷¿.Book( Author.junction.table
CREATE TABLE book_author (
book_id LONG NOT NULL,
author_id LONG NOT NULL,
author_role TEXT(50) DEFAULT 'Author'
);

‗‗.Create.composite.primary.key.using.index
CREATE UNIQUE INDEX idx_book_author_pk ON book_author (book_id, author_id);

‗‗.Table.❸¿.order_master table
CREATE TABLE order_master (

order_id AUTOINCREMENT PRIMARY KEY,

customer_id LONG NOT NULL,

order_date DATETIME DEFAULT Now(),

total_amount CURRENCY NOT NULL,

shipping_cost CURRENCY DEFAULT 0,

final_amount CURRENCY NOT NULL,


order_status TEXT(20) DEFAULT "Pending",

delivery_date DATETIME

);

‗‗.Table.❹¿.order_item table
CREATE TABLE order_item (

order_item_id AUTOINCREMENT PRIMARY KEY,

order_id LONG NOT NULL,

book_id LONG NOT NULL,

quantity LONG NOT NULL DEFAULT 1,

unit_price CURRENCY NOT NULL,

total_price CURRENCY NOT NULL,

created_date DATETIME DEFAULT Now()

);

‗‗.Table.❺¿.payment table
CREATE TABLE payment (

payment_id AUTOINCREMENT PRIMARY KEY,

order_id LONG NOT NULL,

payment_method TEXT(30) NOT NULL,

payment_status TEXT(20) DEFAULT "Pending",

payment_amount CURRENCY NOT NULL,

transaction_id TEXT(100),

payment_date DATETIME DEFAULT Now(),

payment_gateway TEXT(50),

gateway_transaction_id TEXT(100),

created_date DATETIME DEFAULT


CREATE 25 QUESTIONS WITH OUTPUT

1. SELECT with WHERE

SELECT book_id, title, price, stock_quantity

FROM book

WHERE price BETWEEN 200 AND 1000;


2. COUNT with GROUP BY
3. SUM and AVG

4. MAX and MIN functions


SELECT title, price,
IIF(price = (SELECT MAX(price) FROM book), "Highest", "Lowest") AS
price_category
FROM book
WHERE price = (SELECT MAX(price) FROM book) OR price = (SELECT MIN(price)
FROM book);
5. INNER JOIN with multiple tables
SELECT b.title, a.first_name & " " & a.last_name AS author_name,
c.category_name, b.price
FROM ((book b
INNER JOIN book_author ba ON b.book_id = ba.book_id)
INNER JOIN author a ON ba.author_id = a.author_id)
INNER JOIN category c ON b.category_id = c.category_id;
6. LEFT JOIN to show all customers
SELECT c.first_name + " " + c.last_name AS customer_name,
c.email, COUNT(o.order_id) AS order_count
FROM customer c
LEFT JOIN order_master o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name, c.email;
7. Conditional statements
SELECT title, stock_quantity,
IIF(stock_quantity > 50, "High Stock",
IIF(stock_quantity > 20, "Medium Stock", "Low Stock")) AS stock_status
FROM book
ORDER BY stock_quantity DESC;
8. filtering

SELECT order_id, customer_id, order_date, final_amount

FROM order_master

WHERE order_date >= DateAdd("d", -30, Date())

ORDER BY order_date DESC;


9. Subquery
SELECT customer_id, first_name + " " + last_name AS customer_name, email
FROM customer c
WHERE EXISTS (SELECT 1 FROM order_master o WHERE o.customer_id =
c.customer_id);
10. NOT EXISTS for customers
SELECT customer_id, first_name + " " + last_name AS customer_name, email, city
FROM customer c
WHERE NOT EXISTS (SELECT 1 FROM order_master o WHERE o.customer_id =
c.customer_id);
11. HAVING clause
SELECT c.category_name, COUNT(b.book_id) AS book_count
FROM category c
INNER JOIN book b ON c.category_id = b.category_id
GROUP BY c.category_name
HAVING COUNT(b.book_id) > 5;
12. Top record
SELECT TOP 10 title, price, stock_quantity, publication_date
FROM book
ORDER BY price DESC;
13. String functions categories
SELECT DISTINCT b.title, a.first_name & " " & a.last_name AS author_name,
c.category_name, b.price
FROM ((book b
INNER JOIN book_author ba ON b.book_id = ba.book_id)
INNER JOIN author a ON ba.author_id = a.author_id)
INNER JOIN category c ON b.category_id = c.category_id
WHERE b.title LIKE "*Bangladesh*" OR a.last_name LIKE "*Rahman*";
14. String
SELECT DISTINCT b.title, a.first_name & " " & a.last_name AS author_name, b.price
FROM (book b
INNER JOIN book_author ba ON b.book_id = ba.book_id)
INNER JOIN author a ON ba.author_id = a.author_id
WHERE b.title LIKE "*Bangladesh*" OR a.last_name LIKE "*Rahman*";
15. Calculate total sales
SELECT b.title, SUM(oi.quantity) AS total_sold,
SUM(oi.total_price) AS total_revenue,
AVG(oi.unit_price) AS avg_selling_price
FROM book b
INNER JOIN order_item oi ON b.book_id = oi.book_id
GROUP BY b.book_id, b.title
ORDER BY total_revenue DESC;
16. Customer spending
SELECT TOP 10 c.first_name + " " + c.last_name AS customer_name,
c.city, COUNT(o.order_id) AS total_orders,
SUM(o.final_amount) AS total_spent
FROM customer c
INNER JOIN order_master o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name, c.city
ORDER BY total_spent DESC;
17. Inventory value
SELECT c.category_name,
COUNT(b.book_id) AS total_books,
SUM(b.stock_quantity) AS total_stock,
SUM(b.price * b.stock_quantity) AS inventory_value
FROM category c
INNER JOIN book b ON c.category_id = b.category_id
GROUP BY c.category_id, c.category_name
ORDER BY inventory_value DESC;
18. Payment method
SELECT payment_method,
COUNT(payment_id) AS transaction_count,
SUM(payment_amount) AS total_amount,
AVG(payment_amount) AS avg_transaction_value
FROM payment
WHERE payment_status = 'Completed'
GROUP BY payment_method
ORDER BY total_amount DESC;
19.
SELECT b.book_id, b.title, b.price, b.stock_quantity, c.category_name
FROM book b
INNER JOIN category c ON b.category_id = c.category_id
WHERE NOT EXISTS (SELECT 1 FROM order_item oi WHERE oi.book_id = b.book_id);
20. Publisher
SELECT p.publisher_name, p.city,
COUNT(b.book_id) AS books_published,
COUNT(oi.order_item_id) AS total_items_sold,
SUM(oi.total_price) AS total_sales_revenue
FROM (publisher p
INNER JOIN book b ON p.publisher_id = b.publisher_id)
LEFT JOIN order_item oi ON b.book_id = oi.book_id
GROUP BY p.publisher_id, p.publisher_name, p.city
ORDER BY total_sales_revenue DESC;
21. Performance analysis
SELECT p.publisher_name, p.city,
(SELECT COUNT(*) FROM book b2 WHERE b2.publisher_id = p.publisher_id) AS
books_published,
COUNT(oi.order_item_id) AS total_items_sold,
SUM(oi.total_price) AS total_sales_revenue
FROM (publisher p
LEFT JOIN book b ON p.publisher_id = b.publisher_id)
LEFT JOIN order_item oi ON b.book_id = oi.book_id
GROUP BY p.publisher_id, p.publisher_name, p.city
ORDER BY total_sales_revenue DESC;
22. Order status
SELECT order_status,
COUNT(order_id) AS order_count,
SUM(final_amount) AS total_value,
(COUNT(order_id) * 100.0 / (SELECT COUNT(*) FROM order_master)) AS
percentage
FROM order_master
GROUP BY order_status
ORDER BY order_count DESC;
23. Authors with multiple books
SELECT a.first_name & " " & a.last_name AS author_name,
COUNT(ba.book_id) AS books_written,
AVG(b.price) AS avg_book_price
FROM (author a
INNER JOIN book_author ba ON a.author_id = ba.author_id)
INNER JOIN book b ON ba.book_id = b.book_id
GROUP BY a.author_id, a.first_name, a.last_name
HAVING COUNT(ba.book_id) > 1
ORDER BY books_written DESC;

You might also like