Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

WRITE THE CODE OF MYSQL WITH PYTHON PLEASE DO THE FUNCTIONS OF THE QUESTION BY CREATING TABLE AND APPLY THE FUNCTIONS TO THE GIVEN CODE

WRITE THE CODE OF MYSQL WITH PYTHON

PLEASE DO THE FUNCTIONS OF THE QUESTION BY CREATING TABLE

AND APPLY THE FUNCTIONS TO THE GIVEN CODE BELOW!!!

import mysql.connector

# Connect to the MySQL server

cnx = mysql.connector.connect(host="localhost", user="root",

password="Yurtdisi4")

cursor = cnx.cursor()

# Create the 'Market' database

cursor.execute('CREATE DATABASE Market;')

# Use the 'Market' database

cursor.execute('USE Market;')

cursor = cnx.cursor(buffered=True)

# Create the 'Vendor' table

cursor.execute('CREATE TABLE Vendor (vendor_id INTEGER PRIMARY KEY, vendor_name VARCHAR(255), license_start DATE, license_end DATE);')

# Create the 'Item' table

cursor.execute('CREATE TABLE Item (item_id INTEGER PRIMARY KEY, item_name VARCHAR(255), item_type VARCHAR(255), previous_price FLOAT, last_sold_date DATE);')

# Create the 'Auction' table

cursor.execute('CREATE TABLE Auction (auction_id INTEGER PRIMARY KEY, vendor_id INTEGER, item_id INTEGER, price FLOAT, FOREIGN KEY (vendor_id) REFERENCES Vendor(vendor_id), FOREIGN KEY (item_id) REFERENCES Item(item_id));')

sql1 = "INSERT INTO Vendor (vendor_id, vendor_name, license_start, license_end) VALUES (%s, %s, %s, %s)"

val1 = [(1, 'John Smith', '2021-01-01', '2022-12-31'),

(2, 'Melinda Jones', '2021-02-01', '2022-11-30'),

(3, 'Tom Wilson', '2021-03-01', '2022-10-31'),

(4, 'Rachel Williams', '2021-05-01', '2022-09-30'),

(5, 'Adam Johnson', '2021-06-01', '2022-08-31')]

cursor.executemany(sql1, val1)

cnx.commit()

print(cursor.rowcount, "was inserted.")

# We have inserted the tables here

sql2 = "INSERT INTO Item (item_id, item_name, item_type, previous_price, last_sold_date) VALUES (%s, %s, %s, %s, %s)"

val2 = [(11, 'Ball', 'Bronze', 15.50, '2022-12-31'),

(22, 'Jewelery', 'Diamond', 30000.0, '2020-01-23'),

(33, 'Watch', 'Gold', 200.0, '2021-11-30'),

(44, 'Medal', 'Silver', 120.0, '2012-10-06'),

(55, 'Wire', 'Copper', 5.0, '2022-12-15')]

cursor.executemany(sql2, val2)

cnx.commit()

print(cursor.rowcount, "was inserted.")

sql3 = "INSERT INTO Auction (auction_id, vendor_id, item_id, price) VALUES (%s, %s, %s, %s)"

val3 = [(111, 1, 11, 10.0),

(222, 2, 22, 20.0),

(333, 3, 33, 30.0),

(444, 4, 44, 40.0),

(555, 5, 55, 50.0)]

cursor.executemany(sql3, val3)

cnx.commit()

print(cursor.rowcount, "was inserted.")

cursor.execute("SHOW TABLES")

#We apply the function that are required

expensive = ('SELECT previous_price FROM Item WHERE previous_price > 25000');

cursor.execute(expensive)

popular = ('SELECT last_sold_date FROM Item WHERE last_sold_date > 2010-01-01');

cursor.execute(popular)

result1 = ('SELECT item_name, previous_price FROM expensive WHERE item_name, previous_price');

#cursor.execute(result1)

result2 = ('SELECT item_name, previous_price FROM popular, expensive WHERE item_name, previous_price');

#cursor.execute(result2)

result3 = ('SELECT Vendor, Auction FROM Vendor, Auction WHERE Vendor * Auction');

#cursor.execute(result3)

result4 = ('SELECT item_id FROM Popular, Auction WHERE (item_id = item_id)');

#cursor.execute(expensive)

#expensive = cursor.fetchall()

myresult = cursor.fetchall()

#We show their tables

cursor.execute('SELECT * FROM Vendor');

cursor.execute('SELECT vendor_id FROM Auction');

#PRICES

cursor.execute('SELECT price FROM Auction');

cursor.execute('SELECT * FROM Item');

cursor.execute('SELECT * FROM Auction');

#It gives the info

for x in myresult:

print(x)

# Close the connection to the MySQL server

cnx.close()image text in transcribed

Apply the functions given below and print the results - EXPENSIVE \( \leftarrow \sigma_{\text {previous_price }>25000}( \) ITEM ) - POPULAR \( \leftarrow \sigma_{\text {last_sold_date }>2010.01 .01}( \) ITEM) - \( R E S U L T 1 \leftarrow \pi_{\text {item_name,previous_price }}(E X P E N S I V E) \) - \( R E S U L T 2 \leftarrow\left(\pi_{\text {item_name } \text { previous_price }}(P O P U L A R) \cup ight. \) \( \left.\pi_{\text {item_name,previous_price }}(E X P E N S I V E) ight) \) - RESULT3(VENDORAUCTION) - \( R E S U L T 4 \leftarrow \pi\left(P O P U L A R \bowtie_{\text {item_id=item_id }} A U C T I O N ight) \)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions