Da2 21bce1325 Iwp
Da2 21bce1325 Iwp
I have done this assignment in my friends Macbook as my laptop didn’t support xampp software.But I have
created my own database and codes.
Creating The Table
Create table “Employee” and insert at least 10-20 records. Set Empid as primary
key.
Empid:
EmpId Employee’s unique ID. Max. 4 characters should be numeric (Primary Key)
Department Max. 15 characters (Not Null) (Only HR, Testing, Development and
Accounts)
Sol:
➢ PHP Script
➢ IN this script we will create a table named Revanth_21BCE5214 and will
insert some random regards for testing further questions
➢ How To run the PHP
➢ <?php
➢ // Define MySQL connection parameters
➢ $servername = "localhost"; // or the IP address of your MySQL server
➢ $username = "root"; // MySQL username
➢ $password = "poojit@1325"; // MySQL password
➢ $dbname = "Poojit_1325"; // Your database name
➢
➢ // Create connection
➢ $conn = mysqli_connect($servername, $username, $password, $dbname);
➢
➢ // Check connection
➢ if (!$conn) {
➢ die("Connection failed: " . mysqli_connect_error());
➢ }
➢
➢ // SQL to create Employee table
➢ $sql = "CREATE TABLE IF NOT EXISTS Revanth_21BCE5214 (
➢ EmpId INT(4) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
➢ Name VARCHAR(15) NOT NULL,
➢ Department VARCHAR(15) NOT NULL CHECK (Department IN ('HR', 'Testing', 'Development',
'Accounts')),
➢ Grade VARCHAR(3) CHECK (Grade IN ('A', 'B', 'C')),
➢ BasicPay DECIMAL(10,2),
➢ SalaryPay DECIMAL(10,2),
➢ BonusPoints DECIMAL(5,2),
➢ HireDate DATE
➢ )";
➢
➢ if (mysqli_query($conn, $sql)) {
➢ echo "Table Employee created successfully\n";
➢ } else {
➢ echo "Error creating table: " . mysqli_error($conn) . "\n";
➢ }
➢
➢ // Insert records into Employee table
➢ $insert_sql = "INSERT INTO Poojit_21BCE1325 (Name, Department, Grade, BasicPay, SalaryPay,
BonusPoints, HireDate) VALUES ";
➢
➢ // Sample records (you can add more records as needed)
➢ $insert_sql .= "('John Doe', 'HR', 'A', 5000.00, 6000.00, 100.00, '2023-01-
15'), ";
➢ $insert_sql .= "('Jane Smith', 'Testing', 'B', 4500.00, 5500.00, 80.00, '2023-02-20'), ";
➢ $insert_sql .= "('Michael Johnson', 'Development', 'C', 4800.00, 5800.00, 120.00, '2023-03-10'), ";
➢ $insert_sql .= "('Emily Brown', 'Accounts', 'A', 5200.00, 6200.00, 90.00, '2023-04-05')";
➢
➢ // Execute insert query
➢ if (mysqli_query($conn, $insert_sql)) {
➢ echo "Records inserted successfully\n";
➢ } else {
➢ echo "Error inserting records: " . mysqli_error($conn) . "\n";
➢ }
➢
➢ // Close connection
➢ mysqli_close($conn);
➢ ?>
➢
➢ Php Script
<?php
// Define MySQL connection parameters
$servername = "localhost"; // or the IP address of your MySQL server
$username = "root"; // MySQL username
$password = "poojit@1325"; // MySQL password
$dbname = "Poojit_1325"; // Your database name
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Close connection
mysqli_close($conn);
?>
➢ Output Results
2Q. List the number of employees in each department. Only include department
with more than 3 employees. (Use having clause)
<?php
// Define MySQL connection parameters
$servername = "localhost"; // or the IP address of your MySQL server
$username = "root"; // MySQL username
$password = "poojit@1325"; // MySQL password
$dbname = "Poojit_1325"; // Your database name
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Query to list the number of employees in each department with more than 3
employees
$sql = "SELECT Department, COUNT(*) AS NumEmployees FROM Poojit_21BCE1325 GROUP BY
Department HAVING COUNT(*) > 3";
3Q. Lists the number of employees in each department and group by their
departments (Use Group by)
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Query to list the number of employees in each department and group by department
$sql = "SELECT Department, COUNT(*) AS NumEmployees FROM Poojit_21BCE1325 GROUP BY
Department";
// Close connection
mysqli_close($conn);
?>
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Close connection
mysqli_close($conn);
?>
5Q. How many employees earn salary in the range between 30k and 40k
<?php
// Define MySQL connection parameters
$servername = "localhost"; // or the IP address of your MySQL server
$username = "root"; // MySQL username
$password = "poojit@1325"; // MySQL password
$dbname = "Poojit_1325"; // Your database name
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Query to count employees earning salary in the range between 30k and 40k
$sql = "SELECT COUNT(*) AS NumEmployees FROM Poojit_21BCE1325 WHERE SalaryPay
BETWEEN 30000 AND 40000";
// Close connection
mysqli_close($conn);
?>
➢ Results of php
6Q. Find the rounded value of the bonus points. (Differentiate using CEIL,
FLOOR, TRUNC, ROUND)
➢ Php
<?php
// Define MySQL connection parameters
$servername = "localhost"; // or the IP address of your MySQL server
$username = "root"; // MySQL username
$password = "poojit@1325"; // MySQL password
$dbname = "Poojit_1325"; // Your database name
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Query to find the rounded value of the bonus points using different rounding
methods
$sql = "SELECT
BonusPoints AS OriginalBonusPoints,
CEIL(BonusPoints) AS RoundedUpBonusPoints,
FLOOR(BonusPoints) AS RoundedDownBonusPoints,
TRUNCATE(BonusPoints, 0) AS TruncatedBonusPoints,
ROUND(BonusPoints) AS RoundedBonusPoints
FROM
Poojit_21BCE1325";
// Close connection
mysqli_close($conn);
?>
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection if
(!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Query to find the employee details with the minimum bonus points
$sql = "SELECT * FROM Poojit_21BCE1325 WHERE BonusPoints = (SELECT MIN(BonusPoints) FROM Poojit_21BCE1325)";
// Close connection
mysqli_close($conn);
?>
➢ Running the php scripts and results
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Close connection
mysqli_close($conn);
?>
➢ Running the php and results
<?php
// Define MySQL connection parameters
$servername = "localhost"; // or the IP address of your MySQL server
$username = "root"; // MySQL username
$password = "poojit@1325"; // MySQL password
$dbname = "Poojit_1325"; // Your database name
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Close connection
mysqli_close($conn);
?>
➢ PHP script
<?php
// Define MySQL connection parameters
$servername = "localhost"; // or the IP address of your MySQL server
$username = "root"; // MySQL username
$password = "poojit@1325"; // MySQL password
$dbname = "Poojit_1325"; // Your database name
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Close connection
mysqli_close($conn);
?>