-- Books table
CREATE TABLE Books (
book_id INT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author_id INT,
genre VARCHAR(50),
price DECIMAL(10, 2),
published_date DATE,
FOREIGN KEY (author_id) REFERENCES Authors(author_id)
);
-- Authors table
CREATE TABLE Authors (
author_id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
bio TEXT
);
-- Customers table
CREATE TABLE Customers (
customer_id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE,
phone VARCHAR(20),
address TEXT
);
-- Orders table
CREATE TABLE Orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
total_amount DECIMAL(10, 2),
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);
-- Order_Items table
CREATE TABLE Order_Items (
order_item_id INT PRIMARY KEY,
order_id INT,
book_id INT,
quantity INT,
price DECIMAL(10, 2),
FOREIGN KEY (order_id) REFERENCES Orders(order_id),
FOREIGN KEY (book_id) REFERENCES Books(book_id)
);
-- Sample Data
INSERT INTO Authors (author_id, name, bio) VALUES (1, 'Author A', 'Bio of Author A');
INSERT INTO Books (book_id, title, author_id, genre, price, published_date) VALUES (1, 'Book A', 1, 'Fiction', 19.99,
'2023-01-01');
INSERT INTO Customers (customer_id, name, email, phone, address) VALUES (1, 'Customer A',
'[email protected]', '123-456-7890', '123 Street, City, Country');
INSERT INTO Orders (order_id, customer_id, order_date, total_amount) VALUES (1, 1, '2024-08-01', 19.99);
INSERT INTO Order_Items (order_item_id, order_id, book_id, quantity, price) VALUES (1, 1, 1, 1, 19.99);
3. Normalization to Third Normal Form (3NF)
Unnormalized Table (Customer Orders):
OrderID CustomerName CustomerEmail BookTitle AuthorName Price OrderDate
1 Alice [email protected] Book A Author A 10.00 2024-08-01
2 Bob [email protected] Book B Author B 15.00 2024-08-02
3 Alice [email protected] Book C Author A 20.00 2024-08-03
Step 1: 1NF (First Normal Form)
Ensure that the table has no repeating groups. The unnormalized table already meets this requirement.
Step 2: 2NF (Second Normal Form)
Remove partial dependencies. We separate the data into multiple tables to ensure each non-key attribute is fully
functionally dependent on the primary key.
Customers Table:
CustomerID CustomerName CustomerEmail
1 Alice [email protected]
2 Bob [email protected]
Orders Table:
OrderID CustomerID OrderDate
1 1 2024-08-01
2 2 2024-08-02
3 1 2024-08-03
Books Table:
BookID BookTitle AuthorID Price
1 Book A 1 10.00
2 Book B 2 15.00
3 Book C 1 20.00
Authors Table:
AuthorID AuthorName
1 Author A
2 Author B
Step 3: 3NF (Third Normal Form)
Remove transitive dependencies. Each non-key attribute should depend only on the primary key.
The current schema is in 3NF as there are no transitive dependencies.
5. SQL Joins
INNER JOIN: Returns only the rows where there is a match in both tables.
sql
Copy code
SELECT Customers.name, Orders.order_date, Books.title
FROM Customers
INNER JOIN Orders ON Customers.customer_id = Orders.customer_id
INNER JOIN Order_Items ON Orders.order_id = Order_Items.order_id
INNER JOIN Books ON Order_Items.book_id = Books.book_id;
LEFT JOIN: Returns all rows from the left table and matched rows from the right table. If no match, NULL
values are returned.
sql
Copy code
SELECT Customers.name, Orders.order_date, Books.title
FROM Customers
LEFT JOIN Orders ON Customers.customer_id = Orders.customer_id
LEFT JOIN Order_Items ON Orders.order_id = Order_Items.order_id
LEFT JOIN Books ON Order_Items.book_id = Books.book_id;
RIGHT JOIN: Returns all rows from the right table and matched rows from the left table. If no match, NULL
values are returned.
sql
Copy code
SELECT Customers.name, Orders.order_date, Books.title
FROM Customers
RIGHT JOIN Orders ON Customers.customer_id = Orders.customer_id
RIGHT JOIN Order_Items ON Orders.order_id = Order_Items.order_id
RIGHT JOIN Books ON Order_Items.book_id = Books.book_id;
Differences:
INNER JOIN excludes records without a match in both tables.
LEFT JOIN includes all records from the left table, with NULL for unmatched records in the right table.
RIGHT JOIN includes all records from the right table, with NULL for unmatched records in the left table.
7. ERD for a Student Management System
Entities:
Students: student_id (PK), name, email, dob
Courses: course_id (PK), course_name, description, credits
Instructors: instructor_id (PK), name, email, department
Enrollments: enrollment_id (PK), student_id (FK), course_id (FK), enrollment_date
Relationships:
Students enroll in Courses.
Courses are taught by Instructors.
9. High-Level Architecture of a CRM System
User Interface: The frontend through which users interact with the CRM, including web and mobile
applications.
Application Server: Handles business logic, processes user requests, and communicates with the database.
May include components like authentication, API gateway, and service layer.
Database Server: Stores and manages customer data, interactions, and transactional records. It can include
relational databases, data warehouses, and NoSQL databases for different data types.
11. Stored Procedure for Total Sales in a Month
Stored Procedure:
sql
Copy code
CREATE PROCEDURE GetTotalSalesByMonth(IN year INT, IN month INT, OUT total_sales DECIMAL(10, 2))
BEGIN
SELECT SUM(total_amount)
INTO total_sales
FROM Orders
WHERE YEAR(order_date) = year AND MONTH(order_date) = month;
END;
Executing the Procedure:
sql
Copy code
CALL GetTotalSalesByMonth(2024, 8, @total_sales);
SELECT @total_sales AS 'Total Sales';
13. Simple Form and Report
Form for New Customer Details:
A form can be created in the database management system (DBMS) or a web application with fields for Name, Email,
Phone, and Address.
Report to Display All Customers:
sql
Copy code
SELECT customer_id, name, email, phone, address
FROM Customers;
15. User Roles and Permissions in a DBMS
Example:
Admin: Full access to all tables and operations.
Editor: Can insert, update, and delete records but cannot alter table structures.
Viewer: Read-only access to view records.
Implementation:
sql
Copy code
-- Create roles
CREATE ROLE admin;
CREATE ROLE editor;
CREATE ROLE viewer;
-- Assign privileges
GRANT ALL PRIVILEGES ON database_name.* TO 'admin'@'localhost';
GRANT SELECT, INSERT, UPDATE, DELETE ON database_name.* TO 'editor'@'localhost';
GRANT SELECT ON database_name.* TO 'viewer'@'localhost';
-- Assign roles to users
GRANT admin TO 'admin_user'@'localhost';
GRANT editor TO 'editor_user'@'localhost';
GRANT viewer TO 'viewer_user'@'localhost';
17. Indexing in SQL
Example of Creating an Index:
sql
Copy code
CREATE INDEX idx_customer_email ON Customers(email);
Explanation: Indexing speeds up query performance by allowing the database to find records faster. It works similarly
to an index in a book. However, indexes consume additional disk space and may slow down INSERT, UPDATE, and
DELETE operations due to the overhead of maintaining the index.
19. Case Study: Implementing a New MIS
Key Challenges:
Integration with existing systems
Data migration
User training
Change management
Benefits:
Improved data accuracy and consistency
Streamlined business processes
Enhanced decision-making capabilities
Better customer service and communication
20. MIS Implementation Plan
Phases:
1. Planning: Define objectives, scope, and requirements. Conduct feasibility studies and risk assessments.
2. Design: Develop system architecture, design database schema, and define interfaces.
3. Development: Code the system, build interfaces, and integrate components.
4. Testing: Perform unit, integration, and user acceptance testing to ensure system functionality and reliability.
5. Deployment: Implement the system in the production environment, provide training, and roll out the system
to users.