Ism Lab File 1
Ism Lab File 1
On
Introduction Of SQL
Submitted in partial fulfilment of the requirement of Bachelors of Business Administration
(BBA)
Session 2023-24
48/4, Knowledge Park III, Greater Noida, Knowledge Park III, Noida, Uttar
Pradesh 201306
1
DECLARATION
I hereby declare that this ‘ISM LAB FILE (307)’ submitted by me to JEMTEC, Greater Noida is a
Bonafide work undertaken by me and has not been submitted to any other University or Institution for
the award of any degree diploma /certificate or published any time before.
Date: 14/12/2023
2
TO WHOM SO EVER IT MAY CONCERN
This is to certify that MR. Adarsh Srivastava S/O Abhay Kumar Srivastava Batch-
2021-2024, Enrollment No. 01525501721 student of BBA, Vth Semester at JIMS
Engineering Management Technical Campus, Greater Noida affiliated to Guru
Gobind Singh Indraprastha University, New Delhi, has prepared and submitted
his/her SUMMER TRAINING REPORT on the topic,
I certify that the report is the original work done in the partial fulfillment of awarding
Degree of Bachelor of Business Administration (BBA).
Date
3
ACKNOWLEDGEMENT
I offer my sincere thanks and humble regards to JEMTEC, Greater Noida for imparting us very
valuable professional training in BBA. I pay my gratitude and sincere regards to Ms. Charu Singh, my
project Guide, for giving me the cream of his knowledge. I am thankful to her as she has been a
constant source of advice, motivation and inspiration. I am also thankful to her for giving his
suggestions and encouragement throughout the project work.
I take the opportunity to express my gratitude and thanks to our computer Lab staff and library staff
for providing me the opportunity to utilize their resources for the completion of the project.
I am also thankful to my family and friends for constantly motivating me to complete the project and
providing me an environment, which enhanced my knowledge.
Date: 13/12/2023
4
INDEX
S.NO. Particulars Page Signature
No.
1. Introduction Of SQL 1
4. Key Constrains-Normalization 15
Create table
Alter table
Drop Table
6. Implementation of DML 21
commands of SQL with suitable
examples
Insert
5
Update
Delete
7. Implementation of different types 26
of function with suitable
examples
Number function
Aggregate Function
Character Function
Conversion Function
Date Function
8. Implementation of different types 31
of operators in SQL
Arithmetic Operators
Logical Operators
Comparison Operator
Special Operator
Set Operation
6
9. Consider the following schema 36
for a Library Database
Due Date)
SALESMAN(Salesman id Name,
City, Commission)
CUSTOMER(Customer_id Cust
Name, City, Grade, Salesman_id)
8
ORDERS Ord No, Purchase Amt,
Ord Date, Customer_id
Salesman_id)
11
xii
CHAPTER 1-
INTRODUCTION TO
SQL
1
Definition : SQL, which stands for Structured Query Language, is a domain-specific language used
for managing and manipulating relational databases. It provides a standardized way to interact with
relational database management systems (RDBMS) by defining, querying, and manipulating the data
stored in these databases. Here's a detailed breakdown of SQL:
It is a declarative language, meaning that users specify what they want to achieve, and
the database management system (DBMS) determines the most efficient way to carry
out the request.
Data Definition Language (DDL): Used to define and manage the structure of the
database.
CREATE: Used to create database objects like tables, indexes, and views.
Data Manipulation Language (DML): Used to manipulate the data stored in the
database.
Data Control Language (DCL): Manages access to data within the database.
2
COMMIT: Saves all the changes made during the current transaction.
Views: Virtual tables that are based on the result of a SELECT query.
4. SQL Syntax:
5. SQL Queries:
A SQL query is a request for information from a database. The SELECT statement is
commonly used to retrieve data.
6. Normalization:
7. Relationships:
8. ACID Properties:
Examples include MySQL, PostgreSQL, Oracle Database, Microsoft SQL Server, and
SQLite.
3
In summary, SQL is a powerful language for managing relational databases, providing a standardized
and efficient way to define database structures, manipulate data, and control access to the data. Its
widespread adoption has made it a fundamental skill for database administrators, developers, and data
analysts.
Models of SQ
1. SQL Standards:
3. SQL Variants:
4
Some database systems may implement their own variations or extensions of SQL. For
example, MySQL has its own specific syntax for certain operations, and Transact-SQL
(T-SQL) used by Microsoft SQL Server includes additional features beyond standard
SQL.
While traditional relational databases use SQL, NoSQL databases (like MongoDB,
Cassandra, or CouchDB) often have their own query languages and models. They may
not strictly adhere to the relational model or SQL standards.
5
CHAPTER 2- ROADWAY
TRAVELS ER DIAGRAM
6
Definition of ER Diagram & symbols in ER Diagram
1. Entity:
At the core of ER diagrams are entities, which are objects or concepts in the real world
that can be identified and distinguished. Entities are represented by rectangles in the
diagram and often correspond to tables in a relational database. For instance, in a
library database, entities could include "Book," "Author," and "Borrower."
2. Attribute:
3. Key Attribute:
A key attribute is an attribute that uniquely identifies each instance of an entity within
the entity set. It is denoted by underlining the attribute name. In the "Author" entity, for
instance, "AuthorID" could be a key attribute.
4. Relationship:
7
5. Cardinality:
Cardinality describes the number of instances of one entity that can be related to the
number of instances of another entity. It is often expressed as "one" (1), "many" (*), or
specific numeric values. Cardinality helps in understanding the nature of relationships,
such as whether it's a one-to-one, one-to-many, or many-to-many relationship.
6. Weak Entity:
A weak entity is an entity that cannot be uniquely identified by its attributes alone and
depends on the existence of another related entity. It is represented by a double
rectangle. For example, in a university database, a "Room" entity might be weak if it
depends on another entity, such as "Building."
7. Associative Entity:
8. Recursive Relationship:
A recursive relationship occurs when an entity is related to itself. This could represent
hierarchical structures or relationships where an entity is connected to other entities of
the same type. A common example is an "Employee" entity with a recursive
relationship for a manager-subordinate hierarchy.
9. Relationship Attribute:
1. Rectangle:
8
Represents an entity. Each entity in the ER diagram corresponds to a table in the
database.
2. Ellipse (Oval):
Represents an attribute. Attributes provide details about entities and contribute to the
overall understanding of the data.
3. Diamond:
4. Lines:
Connect entities with relationships and attributes. Lines show the connections and
associations between different components in the ER diagram.
5. Underline:
Indicates a key attribute. Key attributes are crucial for uniquely identifying instances of
an entity.
A common notation for representing cardinality. The crow's foot at the end of a line
indicates the "many" side of the relationship.
7. Double Rectangle:
Represents a weak entity. Weak entities depend on another entity for identification.
8. Double Diamond:
9. Loop:
9
ER Diagram of ROADWAY TRAVELS
The attributes identified for entities in “Roadway Travels” are:
Each BUS is identified by a unique identification bus number that acts as a Primary
Key.
Each Ticket issued is identified by Unique Identification ticket number and it is considered as a
Primary Key.
10
o CREATION OF CANCELLATION ENTITY SET:
ER DIAGRAM:
11
CHAPTER 3- VARIOUS
DATA TYPES
12
VARIOUS DATA TYPES:
1. Integer: Integers are whole numbers, either positive or negative, without any decimal places.
They are commonly used for counting and indexing, such as 42 or -17 in mathematical and
programming contexts.
2. Float (Floating-point): Floats represent real numbers and include decimal points. They are
suitable for more precise mathematical calculations, like 3.14 or -0.05 in scientific and
engineering applications.
3. String: Strings are sequences of characters, either letters, numbers, or symbols, enclosed in
single or double quotes. Examples include "Hello, World!" and '123', and they are fundamental
for handling textual data.
4. Boolean: Booleans have only two possible values: True or False. They are crucial for logical
operations and decision-making in programming, providing a basis for conditional statements.
5. List: Lists are ordered collections of items, enclosed in square brackets. They are mutable,
allowing for dynamic changes in size and content. For instance, [1, 2, 3] represents a list of
integers.
6. Tuple: Tuples are similar to lists but immutable, meaning their size and content cannot be
changed after creation. Tuples use parentheses for notation, like (1, 2, 3), making them
suitable for fixed data structures.
7. Dictionary: Dictionaries store data in key-value pairs, enclosed in curly braces. They are
unordered and provide efficient data retrieval based on keys. For example, {'key': 'value'}
represents a dictionary entry with a specific key-value association.
8. Set: Sets are unordered collections of unique elements, denoted by curly braces. They are
useful for mathematical operations like union and intersection. For instance, {1, 2, 3}
represents a set containing distinct integers.
9. Complex: Complex numbers consist of a real part and an imaginary part, expressed as a sum
of the form a+bi, where a and b are real numbers, and i is the imaginary unit. These are
employed in various mathematical and engineering applications.
10. None Type: None is a special object in Python, representing the absence of a value or a null
value. It is commonly used to denote that a variable or a function returns no meaningful result.
13
11. Bytes and Bytearray: Bytes represent a sequence of immutable 8-bit values, often used for
binary data, while bytearray is a mutable counterpart. They are essential for handling raw data,
such as encoding and decoding files or network communication.
12. Range: Range represents an immutable sequence of numbers, commonly used in loops for
iterating a specific number of times. It is memory-efficient as it generates values on the fly,
saving resources compared to creating a list of numbers.
13. Frozen set: Similar to a set, a frozen set is an immutable collection of unique elements. Once
created, its content cannot be modified, making it suitable for situations where immutability is
desired.
14. Decimal: The decimal data type represents floating-point numbers with fixed precision,
helping to avoid issues with floating-point arithmetic. It is useful in financial and decimal-
based applications where precision is critical.
15. Enum: Enums (enumerations) provide a way to create named constant values, improving code
readability by assigning meaningful names to specific values. They are often used to define
sets of related constants or options.
14
CHAPTER4-KEY
CONSTRAINT
NORMALIZATION
15
KEY CONSTRAINTS & NORMALIZATION
Key Constraints:
1. Primary Key: A primary key uniquely identifies each record in a database table.
It must be unique for each row and cannot contain null values.
2. Foreign Key: A foreign key establishes a link between two tables. It refers to
the primary key in another table, creating a relationship. This ensures referential
integrity.
3. Unique Key: Similar to a primary key, a unique key ensures that each value in a
column is unique. However, unlike a primary key, a table can have multiple
unique keys.
Normalization:
1. First Normal Form (1NF): Ensures that data in a table is organized, with no
repeating groups or arrays. Each column contains atomic values, and each cell
has a single value.
2. Second Normal Form (2NF): Builds on 1NF by ensuring that no partial
dependencies exist. All non-key attributes are fully functionally dependent on
the primary key.
3. Third Normal Form (3NF): Extends normalization by eliminating transitive
dependencies. No non-key attribute depends on another non-key attribute.
4. Boyce-Codd Normal Form (BCNF): A more stringent form of 3NF, ensuring
that for every non-trivial functional dependency, the determinant is a super key.
5. Fourth Normal Form (4NF): Deals with multi-valued dependencies. It ensures
that no non-trivial multi-valued dependency exists between candidate keys and
non-prime attributes.
Normalization aims to reduce redundancy, improve data integrity, and maintain a
clear structure in a relational database. It involves breaking down large tables
into smaller, related tables and establishing relationships between them.
16
PART A
17
IMPLEMENTATION OF DDL COMMANDS OF SQL WITH
SUITABLE EXAMPLES:
a) Create Table:
Definition: The CREATE TABLE statement is used in SQL to define and create a new table in
a database. It specifies the table name, along with the names and data types of each column the
table will contain.
SQL Query:
18
b) Alter Table:
Definition: The ALTER TABLE statement in SQL is used to modify an existing table
structure. It can add, modify, or drop columns, as well as alter constraints associated with the
table.
SQL Query:
19
c) Drop Table:
Definition: The DROP TABLE statement is used in SQL to remove an existing table and all
its data from the database. It permanently deletes the table structure and its associated data.
SQL Query:
SQL Query:
b). UPDATE:
Definition: The UPDATE statement in SQL is a DML command used to modify existing
records in a table based on specified conditions.
SQL Query:
-- Update the price of a product with ProductID 1
UPDATE Products
SET Price = 12.99
-- Before UPDATE
22
-- After UPDATE
23
c). DELETE:
Definition: The DELETE statement in SQL is a DML command used to remove records from
a table based on specified conditions.
SQL Query:
24
-- Before DELETE
-- After DELETE
25
IMPLEMENTATION OF DIFFERENT TYPES OF FUNCTIONS
WITH SUITABLE EXAMPLES
a). Number Function:
Explanation: Number functions in SQL perform operations on numeric data types. In this
example, we use the ABS() function to get the absolute value of an employee's salary and
SQRT() to calculate the square root of the salary.
26
b). Aggregate Function:
Explanation: Aggregate functions operate on sets of values and return a single result. The
AVG() function calculates the average salary, and MAX() finds the maximum salary in the
'employees' table.
27
c). Character Function:
Explanation: Character functions manipulate string data in SQL. The CONCAT() function
combines the employee's name and salary into a sentence, while UPPER() converts the
employee's name to uppercase.
28
d). Conversion Function:
Explanation: Conversion functions change the data type of a value. In this example, we use
CAST() to convert the salary to an integer and CONVERT() to transform the employee ID
into a string.
29
e). Date Function:
Explanation: Date functions manipulate date and time values in SQL. The CURRENT_DATE
function retrieves the current date, and DATEADD() adds three months to the current date for
the employee with ID 4.
-- Select data from the Products table and include the current date
SELECT ProductID, ProductName, Category, Price, GETDATE() AS Today
FROM Products;
30
IMPLEMENTATION OF DIFFERENT TYPES OF
OPERATORS IN SQL
a). Arithmetic Operators:
Explanation: Perform mathematical operations on values.
UPDATE Products
SET Date = GETDATE()
WHERE ProductID = 1;
31
b). Comparison Operators:
32
c). Logical Operators:
-- Select products that are either in 'Electronics' category or have a price greater than $50.00
SELECT *
FROM Products
WHERE Category = 'Electronics' OR Price < 50.00;
33
d). SPECIAL OPERATOR:
SELECT *
FROM Products
WHERE ProductName LIKE 'Product1%';
34
5. SET OPERATOR:
UNION
SELECT *
FROM Products
35
BOOK_COPIES (Book_id, Branch_id, No-of_Copies)
BOOK_LENDING (Book_id, Branch_id, Card_No, Date_Out, Due_Date)
LIBRARY_BRANCH (Branch_id, Branch_Name, Address)
Write SQL queries to
1. Retrieve details of all books in the library – id, title, name of publisher,
authors, number of copies in
2. Get the particulars of borrowers who have borrowed more than 3 books, but
from Jan 2023 to June
2023
3. Delete a book in BOOK table. Update the contents of other tables to reflect
this data manipulation
operation.
5. Create a view of all books and its number of copies that are currently
available in the library.
36
SOL:
ER DIAGRAM:
37
Table Creation:
I. PUBLISHER
SQL Query
Insert Values:
38
II. BOOK
SQL Query
39
Insert Values:
BOOK AUTHORS
SQL Query:
Insert Values:
41
Insert Values:
INSERT INTO LIBRARY_BRANCH VALUES
(1, 'City Library', '123 Main St, Cityville'),
(2, 'Town Library', '456 Oak St, Townsville'),
(3, 'Village Library', '789 Pine St, Villageland'),
(4, 'Countryside Library', '101 Cedar St, Countryside');
42
BOOK COPIES
SQL Query:
Insert Values :
V. BOOK LENDING
SQL Query:
Branch_id INT,
Insert Values:
INSERT
VI. INTO BOOK_LENDING VALUES
(1, 1, 101, '2023-02-15', '2023-03-15'),
(2, 1, 101, '2023-04-20', '2023-05-20'),
(3, 2, 103, '2023-06-10', '2023-07-10'),
(4, 2, 104, '2023-01-05', '2023-02-05'),
(5, 3, 105, '2023-03-01', '2023-04-01'),
(6, 3, 101, '2023-05-15', '2023-06-15'),
(7, 4, 101, '2023-01-20', '2023-02-20'),
(8, 4, 103, '2023-04-05', '2023-05-05');
CARD
SQL Query:
-- CARD
CREATE TABLE CARD (
Card_No INT PRIMARY KEY
);
INSERT INTO CARD VALUES
(101),
(102),
(103),
(104),
(105);
Queries:
44
1. Retrieve details of all books in the library – id, title, name of publisher,
authors, number of copies in each branch, etc.
Sol :
2. Get the particulars of borrowers who have borrowed more than 3 books,
45
but from Jan 2023 to June 2023.
Sol:
46
3. Delete a book in BOOK table. Update the contents of other tables to reflect
this data manipulation operation.
Sol:
47
48
49
4. Partition the BOOK table based on year of publication. Demonstrate its
working with a simple query.
Sol:
-- Create range partitions for BOOK table based on the year of publication
CREATE PARTITION FUNCTION YearPartitionFunction (INT)
AS RANGE RIGHT FOR VALUES (1950, 1960, 1970, 1980, 1990, 2000, 2010, 2020);
50
5. Create a view of all books and its number of copies that are currently
available in the library.
Sol:
2. Find the name and numbers of all salesmen who had more than one customer.
3. List all salesmen and indicate those who have and don’t have customers in their
cities
51
4. Create a view that finds the salesman who has the customer with the highest
order of a
Day
5.Demonstrate the DELETE operation by removing salesman with id 1000. All his
orders must also be deleted.
ER DIAGRAM:
52
53
Table Creation:
1. SALESMAN
Sql Query:
54
55
2. CUSTOMER
Sql Query:
Insert Values:
56
57
3. ORDERS
Sql Query:
Queries:
58
SELECT COUNT(*) AS AboveAvgCustomers
FROM CUSTOMER
WHERE Grade > (SELECT AVG(Grade) FROM CUSTOMER WHERE City = 'Bangalore');
2. . Find the name and numbers of all salesmen who had more than one
customer.
Sol:
61
Q11. CONSIDER THE FOLLOWING SCHEMA FOR MOVIE DATABASE:
62
V. RATING(Mov_id, Rev_Stars)
2. Find the movie names where one or more actors acted in two or more movies.
3. List all actors who acted in a movie before 2000 and in a movie after 2015 (use
JOIN operation).
4. Find the title of movies and number of stars for each movie that has at least one
rating and find the
highest number of stars that movie received. Sort the result by movie title.
ER DIAGRAM:
63
Table Creation
64
1. ACTOR
Sql Query:
Insert Values:
2. DIRECTOR
Sql Query:
65
Insert Values:
3. MOVIES
Sql Query:
66
4. MOVIE_CAST
Sql Query:
Act_id INT,
Insert Values:
5. RATING
Sql Query:
Insert Values:
68
69
Queries:
SELECT Mov_Title
FROM MOVIES
WHERE Dir_id = (SELECT Dir_id FROM DIRECTOR WHERE Dir_Name = 'Anurag
Kashyap');
70
2. Find the movie names where one or more actors acted in one or more
movies.
Sol:
SELECT Mov_Title
FROM MOVIES
WHERE Mov_id IN (
SELECT Mov_id
FROM MOVIE_CAST
GROUP BY Mov_id
HAVING COUNT(Act_id) >= 1
);
3. List all actors who acted in a movie before 2000 and also in a movie after
2015 (use JOIN operation).
Sol:
4. Find the title of movies and number of stars for each movie that has at least
one rating and find the highest number of stars that movie received. Sort
the result by movie title.
Sol:
UPDATE RATING
SET Rev_Stars = 5
WHERE Mov_id IN (SELECT Mov_id FROM MOVIES WHERE Dir_id = (SELECT
Dir_id FROM DIRECTOR WHERE Dir_Name = 'Steven Spielberg'));
72
PART – B
MINI PROJECT
1. Entities:
PRODUCTS:
73
Attributes: ProductID (Primary Key), ProductName, SupplierID (Foreign
Key), Price, StockQuantity
SUPPLIERS:
Attributes: SupplierID (Primary Key), SupplierName, ContactPerson,
ContactNumber
CUSTOMERS:
Attributes: CustomerID (Primary Key), CustomerName, ContactNumber,
Address
ORDERS:
Attributes: OrderID (Primary Key), CustomerID (Foreign Key),
OrderDate
ORDER_DETAILS:
Attributes: OrderID (Foreign Key), ProductID (Foreign Key), Quantity
(Primary Key)
2. Relationships:
SUPPLIERS - PRODUCTS:
One-to-Many relationship: A supplier can supply multiple products, but
each product is supplied by one supplier.
PRODUCTS - ORDERS:
Many-to-Many relationship: A product can be part of multiple orders, and
an order can contain multiple products. This relationship is resolved by the
ORDER_DETAILS table.
SUPPLIERS - ORDERS:
Many-to-Many relationship: A supplier can be associated with multiple
orders, and an order can have multiple suppliers through the products it
contains.
CUSTOMERS - ORDERS:
One-to-Many relationship: A customer can place multiple orders, but each
order is placed by one customer.
ORDERS - ORDER_DETAILS:
One-to-Many relationship: An order can have multiple order details, but
each order detail is associated with one order.
3. Cardinalities:
PRODUCTS - SUPPLIERS:
74
Supplier (1) ----< Supplies >---- (Many) Products
PRODUCTS - ORDERS:
Order (Many) ----< Contains >---- (Many) Products
SUPPLIERS - ORDERS:
Supplier (Many) ----< Supplies >---- (Many) Orders
CUSTOMERS - ORDERS:
Customer (1) ----< Places >---- (Many) Orders
ORDERS - ORDER_DETAILS:
Order (1) ----< Contains >---- (Many) Order Details
1. Suppliers:
75
CREATE TABLE SUPPLIERS (
SupplierID INT PRIMARY KEY,
SupplierName VARCHAR(50),
ContactPerson VARCHAR(50),
ContactNumber VARCHAR(15)
);
2.. Customers
CREATE TABLE CUSTOMERS (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
ContactNumber VARCHAR(15),
Address VARCHAR(100)
);
76
3. ORDER DETAILS
77
4.orders
CREATE TABLE ORDERS (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
FOREIGN KEY (CustomerID) REFERENCES CUSTOMERS(CustomerID)
);
78
5.products
CREATE TABLE PRODUCTS (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(50),
SupplierID INT,
Price DECIMAL(10, 2),
StockQuantity INT,
FOREIGN KEY (SupplierID) REFERENCES SUPPLIERS(SupplierID)
);
79
80
Application Development Process:
4. Choose a programming language (e.g., Python, Java) and a database management system
(e.g., MySQL, PostgreSQL).
5. Establish a connection between the application and the database.
6. Implement functions or methods to interact with the database, such as adding new products,
processing orders, etc.
7. Develop a user interface to input data and display information.
8. Test the application to ensure proper functionality.
9. Deploy the application for use in a real-world setting.
81