Partial preview of the text
Download Data base management and more Thesis Database Management Systems (DBMS) in PDF only on Docsity!
Assignment Number - 1 AIM: Introduction to Database Creation Using MySQL/PostgreSQL Title: Objective: Set up and create a database using MySQL/PostgreSQL. Task: Entities and Relationships: « Student (StudentID, Name, Email, Age, Address) * Course (CourseID, CourseName, Credits, InstructorID) + Instructor (InstructorID, Name, Email, Department) ¢ Enrollment (EnrollmentID, StudentID, CoursD, EnrollmentDate) Relationships: ¢ A student can enroll in multiple courses. « A course can have multiple students. + An instructor teaches one or more courses. Objective: To understand the fundamentals of database creation by designing and implementing a relational database using MySQL or PostgreSQL, based on given entities and their relationships. Outcome: After completing this lab, the student will be able to: Understand the concept of databases and relational models Identify entities, attributes, and relationships Create tables using SQL Define primary keys and foreign keys Implement relationships between tables Insert and retrieve data from a database Software Required: e@ MySQL Server / PostgreSQL Server @ MySQL Workbench / pgAdmin e@ Operating System: Windows / Linux / macOS Theory: Introduction to MySQL MySQL is an open-source Relational Database Management System (RDBMS) used to store, organize, and manage large amounts of structured data efficiently. It uses Structured Query Language (SQL) as the standard language for performing database operations such as creating databases, inserting data, updating records, and retrieving information. In MySQL, data is stored in the form of tables, which consist of rows (records) and columns (attributes). Each table represents an entity, and relationships between tables are established using primary keys and foreign keys. This relational structure helps maintain data accuracy and reduces redundancy. MySQL follows a client-server architecture. The MySQL server manages the databases and processes SQL queries, while clients such as MySQL Workbench or command-line tools send queries to the server and display results. This architecture allows multiple users to access and work with the database simultaneously. One of the key strengths of MySQL is its performance and scalability. It can handle large databases and high traffic efficiently, making it suitable for web-based and enterprise applications. MySQL also provides security features such as user authentication, access control, and data encryption to protect sensitive information. MySQL supports multiple storage engines like InnoDB and MyISAM. InnoDB is commonly used because it supports transactions, foreign keys, and data integrity, which are essential for reliable database operations. Due to its simplicity, reliability, and wide community support, MySQL is extensively used in applications such as student management systems, e-commerce platforms, banking systems, and content management systems. 2. Installation of MySQL MySQL can be installed on various operating systems, including Windows, macOS, and Linux. Here are the general steps to install MySQL: Windows: 1. Download the MySQL installer from the official website. 2. https://dev.mysql.com/downloads/installer/ 3. Run the installer and follow the on-screen instructions. 4. Choose the installation type (Typical, Complete, or Custom). Recommended Custom. 5. Seta root password for the MySQL server. 3. MySQL Installation in Ubuntu MySQL can be easily installed on Ubuntu Linux using the APT (Advanced Package Tool) package manager. Ubuntu provides MySQL packages in its official repositories, which sudo mysql 6. Verify Installation Inside the MySQL prompt, check the version: SELECT VERSION(); If the version is displayed, MySQL is installed successfully. 4. Database A database is an organized collection of data that allows easy access, management, and updating of information. Relational databases store data in tables consisting of rows and columns. 5. Entities and Attributes An entity represents a real-world object. Each entity has attributes that describe it. Student e StudentID (Primary Key) Name Email Age Address eeege Instructor InstructorID (Primary Key) e Name e Email e Department Course CourseID (Primary Key) e@ CourseName e Credits e InstructorID (Foreign Key) Enrollment e EnrollmentID (Primary Key) e StudentID (Foreign Key) e CourseID (Foreign Key) e EnrollmentDate Relationships e A student can enroll in multiple courses (One-to-Many) e Acourse can have multiple students (One-to-Many) e An instructor teaches one or more courses (One-to-Many) The Enrollment table is used to manage the many-to-many relationship between Student and Course. SQL Table Creation CREATE TABLE Student ( StudentID INT PRIMARY KEY, Name VARCHAR(50), Email VARCHAR(50), Age INT, Address VARCHAR(100) )s CREATE TABLE Instructor ( InstructorID INT PRIMARY KEY, Name VARCHAR(S50), Email VARCHAR(50), Department VARCHAR(50) ) CREATE TABLE Course ( CourseID INT PRIMARY KEY, CourseName VARCHAR(50), Credits INT, InstructorID INT, FOREIGN KEY (InstructorID) REFERENCES Instructor(InstructorID) ) CREATE TABLE Enrollment ( EnrollmentID INT PRIMARY KEY, StudentID INT, Assignment Number - 2 AIM: Views and Indexing — Optimizing a Student Course Management System for Faster Query Performance. Problem Statement: Views and Indexing Optimizing a Student Course Management System with Views and Indexing for Faster Query Performance. Objective: Understand views and indexing for performance optimization. Task: * Create views for easy reporting on students and course details. * Update and delete records using views where applicable. * Use indexing to speed up searches and measure query performance improvements. Objective: To understand the concept of Views and Indexing in a database and apply them to optimize query performance in a Student Course Management System. Outcome e Views were created to simplify student and course reporting. e Records were updated and deleted using updatable views. e Indexes improved search speed and query performance. e Overall database efficiency was enhanced using views and indexing. Software Requirements e@ Database: MySQL / PostgreSQL / Oracle e@ SQL Client: MySQL Workbench / pgAdmin / SQL*Plus Theory: Views: A View is a virtual table created using a SQL query. It does not store data physically but displays data from one or more tables.A VIEW is a virtual table based on the result-set of an SQL query. It contains rows and columns, just like a real table, but it doesn't store data itself. The data in a view is derived from the base tables at the time the view is accessed. Why use Views? e Simplicity: Simplify complex queries (e.g., hiding multi-table joins) into a single, easy- to-query virtual table. e Security: Grant users access to specific data through a view, without giving them direct access to the underlying tables. You can expose only certain columns or rows. e Data Abstraction: Present data in a way that is logical to the user, regardless of how the data is physically stored. e Consistency: Provide a consistent view of data, even if the underlying schema changes (as long as the view's definition is updated). Basic Syntax: CREATE VIEW ViewName AS SELECT column], column?, ... FROM table_name WHERE condition; Example: Create a view to see only the FirstName, LastName, and Major of students, hiding their age or other sensitive info. CREATE VIEW Student_Majors_View AS SELECT FirstName, LastName, Major FROM Students Example: Create an index on the LastName column of the Students table to speed up searches by last name. CREATE INDEX IX_Students_LastName ON Students (LastName); You can also create a UNIQUE INDEX to ensure all values in the indexed column(s) are unique: CREATE UNIQUE INDEX UQ Students_Email ON Students (Email); -- If you had an Email column When to Index? Columns frequently used in WHERE clauses. Columns frequently used in JOIN conditions. Columns frequently used in ORDER BY clauses. Columns used in aggregate functions where GROUP BY is applied. Foreign key columns. eeeee Advantages of Indexing: e Faster search operations e Efficient filtering and sorting e Reduced query execution time Drawbacks: e Slower INSERT, UPDATE, DELETE: Maintaining indexes adds overhead to data modification operations because the index itself must be updated. e Disk Space: Indexes consume disk space Step to solve the given problem e Create Database and Use It e@ Table creation e Sample data insertion e@ Views creation e Indexing creation e Example queries with performance check o Sample Examples: 10 m Query 1: Get all courses a student is enrolled in = Query 2: Get courses with more than 1 student m Update and Delete Examples m= Check updated views Conclusion The experiment demonstrated that: Views simplify complex queries and improve data accessibility. Updatable views allow safe modification of records. Indexing enhances query performance and reduces execution time. Thus, Views and Indexing play a crucial role in database optimization. Oral Questions: 1. What is a View? 2. Can you update data through a view? 3. 4 5 What is an Index? . Benefits of Indexing? . Difference between a Table and a View? 11 e Understand relational database concepts and SQL query optimization. Software Requirements e Database Management System (DBMS): MySQL / MariaDB / PostgreSQL / Oracle / SQL Server e SQL Client / IDE (Optional but Recommended): MySQL Workbench, pgAdmin, DBeaver, SQL Server Management Studio (SSMS) e@ Operating System: Windows 10 or later, macOS, or Linux Hardware Requirements (Minimal): e Processor: Intel i3 or equivalent @ RAM: 4 GB or more e Storage: 2 GB free space for DBMS installation © Display: 1024x768 resolution or higher Theory: 1. Introduction to SQL Structured Query Language (SQL) is the standard language used to communicate with relational databases. It allows users to store, retrieve, update, and delete data efficiently. A relational database organizes data into tables (also called relations), where each table consists of rows (records) and columns (fields). SQL is essential for managing this data and performing operations like filtering, sorting, aggregating, and combining data from multiple tables. 2. Basic SQL Commands SQL commands are generally divided into the following categories: 1. Data Definition Language (DDL): Used to define and modify database structure Examples: CREATE TABLE, ALTER TABLE, DROP TABLE 13 2. Data Manipulation Language (DML): Used to manipulate the data stored in tables Examples: SELECT, INSERT, UPDATE, DELETE 3. Data Control Language (DCL): Used to control access to data Examples: GRANT, REVOKE 4. Transaction Control Language (TCL): Used to manage transactions Examples: COMMIT, ROLLBACK 3. filtering and sorting Database filtering and sorting are two distinct operations used to manipulate and retrieve data from a database, but they serve different purposes. Filtering is the process of selecting a subset of records from a database based on specific conditions or criteria. It reduces the number of records returned by querying only those that match the given rules. Filtering is typically done using clauses like WHERE in SQL. Example: Suppose you have a database table of employees with columns like Name, Department, and Salary. If you want to retrieve only employees from the "Sales" department, you would use a filter: SELECT * FROM Employees WHERE Department = 'Sales'; This query returns only the rows where the Department is "Sales," excluding all other departments. Sorting, on the other hand, arranges the retrieved records in a specific order (ascending or descending) based on one or more columns. It does not reduce the number of records but organizes them for better readability or analysis. Sorting is done using clauses like ORDER BY in SQL. Example: If you want to retrieve all employees but display them in order of their salary (from highest to lowest), you would use: SELECT * FROM Employees ORDER BY Salary DESC; 14 Example 2: List courses by credits from highest to lowest SELECT course_name, credits FROM Courses ORDER BY credits DESC; Key Points: e@ ORDER BY is applied after all filtering (WHERE) and joins. e Useful for reports, leaderboards, or sorted lists. GROUP BY: The GROUP BY clause is used to group rows that have the same value in one or more columns and often works with aggregate functions like COUNTQ, AVG(), SUMQ, MAX(), MINO. Syntax: SELECT columnl, AGG_FUNC(column2) FROM table_name GROUP BY column]; Example 1: Count the number of students in each major SELECT major, COUNT(student_id) AS student_count FROM Students GROUP BY major; Key Points: e GROUP BY reduces multiple rows into summary rows. e Can be combined with HAVING to filter groups: SELECT major, COUNT(student_id) AS student_count FROM Students GROUP BY major HAVING COUNT(student_id) > 1; -- only majors with more than 1 student How to Solve Questions Step by Step: 1. Understand the Question — Determine whether it requires filtering, sorting, aggregation, or subqueries. 2. Identify Tables — Locate the relevant tables containing required information. 16 3. Choose SQL Clauses - Use WHERE, ORDER BY, GROUP BY, or subqueries as needed. 4. Write the Query Stepwise o Start simple with SELECT o Apply filters with WHERE o Sort using ORDER BY o Aggregate using GROUP BY © Use nested queries if results depend on another query 5. Test and Verify — Run the query and check the results for correctness. Conclusion: e SQL allows for efficient data retrieval using filters, sorting, aggregation, and nested subqueries. e Stepwise query building ensures accuracy and clarity. e@ Mastering these techniques is essential for managing relational databases and generating meaningful reports. Oral Questions: What is the difference between WHERE and HAVING clauses? How does ORDER BY affect performance? Explain GROUP BY with an example. What is a nested subquery and why use it? Can a subquery return multiple values? How do you handle it? How can grades (A, B, C) be converted to numeric values in SQL? Difference between INNER JOIN and LEFT JOIN. mayvwr YP YP BS 17 e Apply joins in real-world database applications like order management systems. © Understand relational integrity and query optimization concepts. Software Requirements: e Database Management System (DBMS): MySQL / MariaDB / PostgreSQL / Oracle / SQL Server e SQL Client / IDE (Optional but Recommended): MySQL Workbench, pgAdmin, DBeaver, SQL Server Management Studio (SSMS) © Operating System: Windows 10 or later, macOS, or Linux Hardware Requirements (Minimal): e Processor: Intel i3 or equivalent @ RAM: 4 GB or more e Storage: 2 GB free space for DBMS installation @ Display: 1024 x 768 resolution or higher Theory: 1. Introduction to SQL Joins e In relational databases, data is stored across multiple tables to reduce redundancy and maintain data integrity. SQL JOIN operations are used to combine rows from two or more tables based on a related column, usually a primary key—foreign key relationship. e Ina Customer Order Management System, customer details are stored in one table, while order details are stored in another. Joins help retrieve meaningful information such as which customer placed which order. 2. Types of SQL Joins SQL Joins are used to combine data from two or more tables based on a related column, usually a primary key—foreign key relationship. Different types of joins return different result sets depending on how matching and non-matching records are handled. 19 1. INNER JOIN An INNER JOIN returns only those records where there is a matching value in both tables involved in the join. Records that do not have a match in either table are excluded from the result set. This type of join is commonly used when we want only related data from both tables. Example Use Case: Retrieve customers who have placed at least one order. Key Characteristics: Displays only matching records from both tables Excludes all unmatched rows Most commonly used join in SQL 2. LEFT JOIN (LEFT OUTER JOIN) A LEFT JOIN returns all records from the left table and the matching records from the right table. If there is no matching record in the right table, the result will contain NULL values for the tight table’s columns. This join is useful when it is important to include all records from the main table, regardless of whether related data exists. Example Use Case: Retrieve all customers, including those who have not placed any orders. Key Characteristics: All records from the left table are included Matching records from the right table are shown Non-matching rows from the right table appear as NULL 3. RIGHT JOIN (RIGHT OUTER JOIN) A RIGHT JOIN returns all records from the right table and the matching records from the left table. If there is no matching record in the left table, NULL values are displayed for the left table’s columns. This join ensures that all records from the secondary table are included in the result. Example Use Case: Retrieve all orders, including those placed by customers whose details are missing from the Customers table. Key Characteristics: e Allrecords from the right table are included e Matching records from the left table are displayed 20