-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sale.py
48 lines (40 loc) · 1.6 KB
/
Sale.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import mysql.connector
from faker import Faker
def create_sale(db):
cursor = db.cursor()
fake = Faker()
# Create Sale table if not exists
cursor.execute("""
CREATE TABLE IF NOT EXISTS Sale (
SaleID INT AUTO_INCREMENT PRIMARY KEY,
CustomerID INT,
CarID INT,
SalespersonID INT,
PaymentID INT,
SaleDate DATE,
SalePrice INT,
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (CarID) REFERENCES Car(CarID)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (SalespersonID) REFERENCES SalesPerson(SalesPersonID)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (PaymentID) REFERENCES Payment(PaymentID)
ON UPDATE CASCADE
ON DELETE CASCADE
)
""")
# Populate Sale table
sale_data = []
for i in range(1, 100):
sale_data.append((None, fake.random_int(min=1, max=50), fake.random_int(min=1, max=100),
fake.random_int(min=1, max=20), fake.random_int(min=1, max=100),
fake.future_date(end_date='+1y'), fake.random_int(min = 500000,max = 2000000)))
insert_sale_query = "INSERT INTO Sale (SaleID, CustomerID, CarID, SalespersonID, PaymentID, SaleDate, SalePrice) VALUES (%s, %s, %s, %s, %s, %s, %s)"
cursor.executemany(insert_sale_query, sale_data)
db.commit()
cursor.close()
print("Sale table created and populated successfully.")