0% found this document useful (0 votes)
4 views

SQL User story

The document outlines SQL commands for creating and managing a database named 'StarProtect' with tables for 'Underwriter' and 'Insurance'. It includes commands for inserting data into these tables, querying insurance details, and deleting underwriter records. Additionally, it demonstrates how to calculate insurance premiums and manage date fields within the database structure.

Uploaded by

swathin2020csbs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL User story

The document outlines SQL commands for creating and managing a database named 'StarProtect' with tables for 'Underwriter' and 'Insurance'. It includes commands for inserting data into these tables, querying insurance details, and deleting underwriter records. Additionally, it demonstrates how to calculate insurance premiums and manage date fields within the database structure.

Uploaded by

swathin2020csbs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

-- Switch to the database

USE StarProtect; X

-- Create the Underwriter table

CREATE TABLE Underwriter (

UnderwriterId INT AUTO_INCREMENT PRIMARY KEY,

Name VARCHAR(255) NOT NULL,

DOB DATE NOT NULL,

JoiningDate DATE NOT NULL,

DefaultPassword VARCHAR(255) NOT NULL

);

2.

INSERT INTO Underwriter (Name, DOB, JoiningDate, DefaultPassword)

VALUES

('John Doe', '1990-05-15', '2023-01-10', 'default123'),

('Jane Smith', '1985-10-25', '2023-02-20', 'password456');

3.

-- Switch to the database

USE StarProtect;

-- Create the Insurance table

CREATE TABLE Insurance (

PolicyNo INT AUTO_INCREMENT PRIMARY KEY,

VehicleNo VARCHAR(50) NOT NULL, -- Adjust length based on expected alphanumeric value

VehicleType VARCHAR(50) NOT NULL, -- For example, '2-wheeler', '4-wheeler'

CustomerName VARCHAR(255) NOT NULL,

EngineNo INT NOT NULL,

ChasisNo INT NOT NULL,

PhoneNo BIGINT NOT NULL CHECK (LENGTH(PhoneNo) = 10), -- Ensures 10 digits


Type VARCHAR(50) NOT NULL, -- For example, 'Full Insurance', 'Third Party'

PremiumAmt DECIMAL(10, 2) GENERATED ALWAYS AS (

CASE

WHEN Type = 'Full Insurance' THEN 5000.00

WHEN Type = 'Third Party' THEN 2500.00

ELSE NULL

END

) STORED, -- Automatically calculates based on Type

FromDate DATE NOT NULL,

ToDate DATE GENERATED ALWAYS AS (DATE_ADD(FromDate, INTERVAL 365 DAY)) STORED, -- Adds
365 days to FromDate

UnderwriterId INT NOT NULL -- Captured from system, assumed to relate to Underwriter table

);

4.

INSERT INTO Insurance (VehicleNo, VehicleType, CustomerName, EngineNo, ChasisNo, PhoneNo,


Type, FromDate, UnderwriterId)

VALUES

-- Records for Underwriter with ID 1

('ABC1234', '4-wheeler', 'John Doe', 12345678, 87654321, 9876543210, 'Full Insurance', '2024-01-
01', 1),

('XYZ5678', '2-wheeler', 'Jane Smith', 11223344, 44332211, 9876501234, 'Third Party', '2024-02-
01', 1),

-- Records for Underwriter with ID 2

('LMN9012', '4-wheeler', 'Alice Brown', 55667788, 88776655, 9876547890, 'Full Insurance', '2024-
03-01', 2),

('PQR3456', '2-wheeler', 'Bob White', 66778899, 99887766, 9876567890, 'Third Party', '2024-04-
01', 2);

5.

SELECT

VehicleNo,
VehicleType,

CustomerName,

EngineNo,

ChasisNo,

PhoneNo,

Type,

PremiumAmt,

FromDate,

ToDate,

UnderwriterId

FROM Insurance

WHERE PolicyNo = <YourPolicyNo>;

6.

SELECT

UnderwriterId,

COUNT(*) AS NumberOfVehicles,

GROUP_CONCAT(PolicyNo) AS PolicyNumbers,

GROUP_CONCAT(VehicleNo) AS VehicleNumbers,

GROUP_CONCAT(VehicleType) AS VehicleTypes,

GROUP_CONCAT(CustomerName) AS CustomerNames,

GROUP_CONCAT(EngineNo) AS EngineNumbers,

GROUP_CONCAT(ChasisNo) AS ChasisNumbers,

GROUP_CONCAT(PhoneNo) AS PhoneNumbers,

GROUP_CONCAT(Type) AS InsuranceTypes,

GROUP_CONCAT(PremiumAmt) AS PremiumAmounts,

GROUP_CONCAT(FromDate) AS InsuranceStartDates,

GROUP_CONCAT(ToDate) AS InsuranceEndDates

FROM Insurance

GROUP BY UnderwriterId;

(-----OR----)
SELECT

UnderwriterId,

COUNT(*) AS NumberOfVehicles,

GROUP_CONCAT(VehicleNo SEPARATOR ', ') AS VehicleNumbers

FROM Insurance

GROUP BY UnderwriterId;

7.

SELECT

PolicyNo,

VehicleNo,

VehicleType,

CustomerName,

EngineNo,

ChasisNo,

PhoneNo,

Type,

PremiumAmt,

FromDate,

ToDate,

UnderwriterId

FROM Insurance

WHERE ToDate < CURRENT_DATE;

8.

DELETE FROM Underwriter

WHERE UnderwriterId = <UnderwriterId>;

You might also like