-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.py
175 lines (154 loc) · 5.29 KB
/
query.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import mysql.connector
# Step 1: Connect to MySQL
def create_connection():
connection = mysql.connector.connect(
host="localhost", # Change this to your MySQL server's host
user="your_username", # Replace with your MySQL username
password="your_password", # Replace with your MySQL password
database="your_database" # Replace with your database name
)
return connection
# Step 2: Create Tables
def create_tables(connection):
cursor = connection.cursor()
# Creating the STUDENT table
cursor.execute("""
CREATE TABLE IF NOT EXISTS STUDENT (
STUDENT_ID INT PRIMARY KEY,
STUDENT_NAME VARCHAR(100),
AGE INT,
HOBBY VARCHAR(100),
DOB DATE,
DOOR_NO VARCHAR(10),
STREET VARCHAR(100),
CITY VARCHAR(100),
STATE VARCHAR(100),
PIN VARCHAR(10)
);
""")
# Creating the LECTURER table
cursor.execute("""
CREATE TABLE IF NOT EXISTS LECTURER (
LECTURER_ID INT PRIMARY KEY,
LECTURER_NAME VARCHAR(100)
);
""")
# Creating the COURSE table
cursor.execute("""
CREATE TABLE IF NOT EXISTS COURSE (
COURSE_ID INT PRIMARY KEY,
COURSE_NAME VARCHAR(100)
);
""")
# Creating the SUBJECTS table
cursor.execute("""
CREATE TABLE IF NOT EXISTS SUBJECTS (
SUBJECT_ID INT PRIMARY KEY,
SUBJECT_NAME VARCHAR(100)
);
""")
# Creating the STUDENT_COURSE table (junction table)
cursor.execute("""
CREATE TABLE IF NOT EXISTS STUDENT_COURSE (
STUDENT_ID INT,
COURSE_ID INT,
PRIMARY KEY (STUDENT_ID, COURSE_ID),
FOREIGN KEY (STUDENT_ID) REFERENCES STUDENT(STUDENT_ID),
FOREIGN KEY (COURSE_ID) REFERENCES COURSE(COURSE_ID)
);
""")
# Creating the LECTURER_COURSE table (junction table)
cursor.execute("""
CREATE TABLE IF NOT EXISTS LECTURER_COURSE (
LECTURER_ID INT,
COURSE_ID INT,
PRIMARY KEY (LECTURER_ID, COURSE_ID),
FOREIGN KEY (LECTURER_ID) REFERENCES LECTURER(LECTURER_ID),
FOREIGN KEY (COURSE_ID) REFERENCES COURSE(COURSE_ID)
);
""")
# Creating the COURSE_SUBJECT table (junction table)
cursor.execute("""
CREATE TABLE IF NOT EXISTS COURSE_SUBJECT (
COURSE_ID INT,
SUBJECT_ID INT,
PRIMARY KEY (COURSE_ID, SUBJECT_ID),
FOREIGN KEY (COURSE_ID) REFERENCES COURSE(COURSE_ID),
FOREIGN KEY (SUBJECT_ID) REFERENCES SUBJECTS(SUBJECT_ID)
);
""")
connection.commit()
print("Tables created successfully.")
# Step 3: Insert Data into Tables
def insert_data(connection):
cursor = connection.cursor()
# Inserting into STUDENT table
cursor.executemany("""
INSERT INTO STUDENT (STUDENT_ID, STUDENT_NAME, AGE, HOBBY, DOB, DOOR_NO, STREET, CITY, STATE, PIN)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
""", [
(1, 'John Doe', 20, 'Reading', '2003-05-15', '12A', 'Maple Street', 'New York', 'NY', '10001'),
(2, 'Jane Smith', 22, 'Cycling', '2001-03-22', '25B', 'Pine Avenue', 'San Francisco', 'CA', '94102')
])
# Inserting into LECTURER table
cursor.executemany("""
INSERT INTO LECTURER (LECTURER_ID, LECTURER_NAME)
VALUES (%s, %s);
""", [
(1, 'Dr. Emily Brown'),
(2, 'Dr. Robert Wilson')
])
# Inserting into COURSE table
cursor.executemany("""
INSERT INTO COURSE (COURSE_ID, COURSE_NAME)
VALUES (%s, %s);
""", [
(101, 'Computer Science 101'),
(102, 'Mathematics 101')
])
# Inserting into SUBJECTS table
cursor.executemany("""
INSERT INTO SUBJECTS (SUBJECT_ID, SUBJECT_NAME)
VALUES (%s, %s);
""", [
(1, 'Data Structures'),
(2, 'Algorithms'),
(3, 'Calculus'),
(4, 'Linear Algebra')
])
# Inserting into STUDENT_COURSE table (junction)
cursor.executemany("""
INSERT INTO STUDENT_COURSE (STUDENT_ID, COURSE_ID)
VALUES (%s, %s);
""", [
(1, 101), # John Doe attends Computer Science 101
(2, 102) # Jane Smith attends Mathematics 101
])
# Inserting into LECTURER_COURSE table (junction)
cursor.executemany("""
INSERT INTO LECTURER_COURSE (LECTURER_ID, COURSE_ID)
VALUES (%s, %s);
""", [
(1, 101), # Dr. Emily Brown teaches Computer Science 101
(2, 102) # Dr. Robert Wilson teaches Mathematics 101
])
# Inserting into COURSE_SUBJECT table (junction)
cursor.executemany("""
INSERT INTO COURSE_SUBJECT (COURSE_ID, SUBJECT_ID)
VALUES (%s, %s);
""", [
(101, 1), # Computer Science 101 has the subject Data Structures
(101, 2), # Computer Science 101 has the subject Algorithms
(102, 3), # Mathematics 101 has the subject Calculus
(102, 4) # Mathematics 101 has the subject Linear Algebra
])
connection.commit()
print("Data inserted successfully.")
# Step 4: Main Execution
def main():
connection = create_connection()
create_tables(connection)
insert_data(connection)
connection.close()
if __name__ == "__main__":
main()