-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_mysql_tables.py
76 lines (68 loc) · 2.29 KB
/
create_mysql_tables.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import mysql.connector
# define tables to be created
tables = []
tables.append('''
CREATE TABLE IF NOT EXISTS users (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(255),
email VARCHAR(255),
phone_number VARCHAR(22),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
''')
tables.append('''
CREATE TABLE IF NOT EXISTS vendors (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(255),
state VARCHAR(14),
zip VARCHAR(5),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
''')
tables.append('''
CREATE TABLE IF NOT EXISTS products (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
vendor_id INT,
CONSTRAINT fk_vendor FOREIGN KEY (vendor_id) REFERENCES vendors(id),
name VARCHAR(255),
description TEXT,
price DECIMAL(10, 2),
stock_quantity INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
''')
tables.append('''
CREATE TABLE IF NOT EXISTS orders (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
user_id INT,
CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id),
product_id INT,
CONSTRAINT fk_product FOREIGN KEY (product_id) REFERENCES products(id),
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
total_product INT,
shipping_address VARCHAR(255),
payment_method VARCHAR(50),
status VARCHAR(50) DEFAULT 'Pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
''')
payment_methods = ['Credit Card', 'Mailed Check', 'Paypal', 'Electronic Check']
statuses = ['Pending', 'Shipped', 'Cancelled', 'Delivered']
# Connect to PostgreSQL database
conn = mysql.connector.connect(
host="localhost",
user="benchmark_user",
password="password",
database="benchmark_db"
)
cursor = conn.cursor()
# Create tables
for table_def in tables:
cursor.execute(table_def)
# Commit the changes and close the connection
conn.commit()
conn.close()