Ass 3
Ass 3
ASSIGNMENT 3
Project Data Insertion:
CREATE TABLE Customer (
CustomerID INT PRIMARY KEY,
Name VARCHAR(255),
Gender VARCHAR(10),
DateOfBirth DATE,
ContactNumber VARCHAR(15),
Email VARCHAR(100),
RegistrationDate DATE,
PurchaseHistory DATE,
Address VARCHAR(255),
CustomerStatus VARCHAR(255)
);
Python faker:
total_customers = 0
online_customers = 0
while total_customers < 200: # Adjust the total number of customers as needed
customer_id = None
name = fake.name()
email = fake.email()
query_customer = f'''
'''
cursor.execute(query_customer)
total_customers += 1
username = fake.email()
password = fake.password()
query_online_customer = f'''
'''
cursor.execute(query_online_customer)
online_customers += 1
courierId = 1001
for _ in range(200): # Adjust the loop range to 200
company_name ='TCS'
contact_information = fake.phone_number()
delivery_time_estimates = fake.random_element(elements=('Same Day', 'Next Day', '2-3
Days', '3-5 Days'))
query = f'''
INSERT INTO CourierCompany (CourierID, CompanyName, ContactInformation,
DeliveryTimeEstimates)
VALUES ({courierId}, '{company_name}', '{contact_information}',
'{delivery_time_estimates}')
'''
courierId += 1
Python faker:
fake = Faker()
manager_count = 0
cashier_count = 0
designer_count = 0
employee_id = None
name = fake.name()
email = fake.email()
if manager_count < 2:
position = 'Manager'
manager_count += 1
salary = commonsalarym
position = 'Cashier'
cashier_count += 1
position = 'Designer'
designer_count += 1
else:
position = 'Staff'
salary = commonsalaryst # Use common salary for staff
address = fake.address()
query = f'''
INSERT INTO Employee (EmployeeID, Name, Gender, DateOfBirth, ContactNumber, Email, Position,
Salary, HireDate, Address)
);
Python faker:
if not valid_product_ids:
print("No valid ProductID found in the Products table. Please insert some values
first.")
else:
# Create a Faker instance
fake = Faker()
try:
cursor.execute(insert_purchase_query)
conn.commit()
except pyodbc.Error as e:
print(f"Error inserting purchase data: {e}")
conn.rollback()
for _ in range(total_purchases):
insert_fake_purchase()
Python faker:
def generate_valid_price_id():
while True:
fake_price_id = fake.random_int(min=1000, max=9999)
cursor.execute(f"SELECT COUNT(*) FROM Price WHERE PriceID =
{fake_price_id}")
count = cursor.fetchone()[0]
if count == 0:
return fake_price_id
if purchase_price_row:
purchase_price = purchase_price_row.purchase_price
fake_sale_price = Decimal(str(round(float(purchase_price) * 1.20,
2))) # Convert to Decimal
try:
cursor.execute(insert_price_query)
conn.commit()
except pyodbc.Error as e:
print(f"Error inserting price data: {e}")
conn.rollback()
else:
print(f"No PurchasePrice found for ProductID {fake_product_id}.
Skipping insertion.")
else:
print("No more ProductIDs available. Skipping insertion.")
for _ in range(total_prices):
insert_fake_price()
Python faker:
cursor = conn.cursor()
# Set the maximum value of the random quantity to the remaining quantity
fake_quantity = random.randint(1, min(remaining_quantity, 7))
# Check if remaining quantity will be less than 0 after the order
if remaining_quantity - fake_quantity < 0:
print(f"Not enough stock for ProductID {fake_product_id} to fulfill
OrderID {fake_order_id}")
return
# Subtract the quantity from the remaining quantity in the Purchase table
cursor.execute(f"UPDATE Purchase SET qty_rem = qty_rem - {fake_quantity}
WHERE ProductID = {fake_product_id}")
try:
cursor.execute(insert_order_query)
conn.commit()
print(f"Successfully inserted OrderID {fake_order_id} with Quantity
{fake_quantity}")
except pyodbc.Error as e:
print(f"Error inserting order data: {e}")
conn.rollback()
else:
print("No available products for order.")
for _ in range(total_orders):
insert_fake_order()
Python faker:
cursor = conn.cursor()
total_products = cursor.fetchone()[0]
total_customers = cursor.fetchone()[0]
total_employees = cursor.fetchone()[0]
print("Please insert some values into Products, Customer, and Employee tables first.")
else:
fake = Faker()
def generate_valid_sale_id():
while True:
if count == 0:
return fake_sale_id
def insert_fake_sale():
fake_sale_id = generate_valid_sale_id()
purchase_data = cursor.fetchone()
# Set the maximum value of the random quantity to the remaining quantity
return
# Subtract the quantity from the remaining quantity in the Purchase table
fake_customer_id = cursor.fetchone().CustomerID
fake_employee_id = cursor.fetchone().EmployeeID
# Fetch an existing SalePrice from the Price table
sale_price = cursor.fetchone().sale_price
insert_sale_query = f"""
'{fake.date_time_between(start_date='-1d', end_date='now')}',
{fake_customer_id}, {fake_employee_id}
"""
try:
cursor.execute(insert_sale_query)
conn.commit()
# Check if the remaining quantity in the Purchase table has reached zero
if remaining_quantity - fake_quantity == 0:
except pyodbc.Error as e:
conn.rollback()
else: