diff --git a/ABOUT.md b/ABOUT.md new file mode 100644 index 0000000..ceee1d6 --- /dev/null +++ b/ABOUT.md @@ -0,0 +1,15 @@ +As developers, we all know the importance of automating repetitive tasks. Not only does it save time and effort, but it also eliminates the possibility of human error. One area where automation can be particularly useful is in database management. + +Recently, we explored how to automate various database tasks using Python and MySQL. We started by checking for the existence of a database named "spj", and if it existed, we dropped it and then created a new one with the same name. + +We then inserted multiple rows of data into the tables named "supplier", "shipment", "project", and "part" using the mysql-connector-python package and the INSERT INTO statement. The data was taken from text files saved in the same directory, with rows separated by a new line and column data separated by whitespace in the text files. + +However, when we tried to truncate the parent table, we encountered an issue as the "shipment" table had foreign key constraints referencing the primary keys of the other tables. To solve this, we deleted the foreign key constraints referencing the primary keys of the other tables and then truncated the "shipment" table. + +In addition to the basic CRUD operations, we have also seen how to handle the foreign key constraints while truncating the tables and how to handle the case where the database does not exist. + +By using tools like Python and MySQL, as well as libraries such as sqlalchemy, developers can easily create, modify, and query databases, as well as insert and manipulate data, and handle various database related tasks. + +In order to be an effective developer, it's important to have a good understanding of database management and the tools available. By staying up-to-date with the latest developments in the field and experimenting with different tools and techniques, developers can continue to improve their skills and become more efficient at managing databases. + +Don't wait, start automating your database tasks today, and see the difference for yourself! \ No newline at end of file diff --git a/commands.sql b/Databases/commands.sql similarity index 99% rename from commands.sql rename to Databases/commands.sql index d14087d..dcf247c 100644 --- a/commands.sql +++ b/Databases/commands.sql @@ -32,4 +32,4 @@ create table shipment ( foreign key (SNO) references supplier(SNO), foreign key (PNO) references part(PNO), foreign key (JNO) references project(JNO) -); \ No newline at end of file +); diff --git a/part.txt b/Databases/part.txt similarity index 100% rename from part.txt rename to Databases/part.txt diff --git a/project.txt b/Databases/project.txt similarity index 100% rename from project.txt rename to Databases/project.txt diff --git a/shipment.txt b/Databases/shipment.txt similarity index 100% rename from shipment.txt rename to Databases/shipment.txt diff --git a/supplier.txt b/Databases/supplier.txt similarity index 59% rename from supplier.txt rename to Databases/supplier.txt index 6d7e283..73121d9 100644 --- a/supplier.txt +++ b/Databases/supplier.txt @@ -1,5 +1,5 @@ S1 SMITH 20 LONDON -S2 JONES 10 PARRIS -S3 BLAKE 30 PARRIS +S2 JONES 10 PARIS +S3 BLAKE 30 PARIS S4 CLARK 20 LONDON S5 ADAMS 30 ATHENS \ No newline at end of file diff --git a/README.md b/README.md index ceee1d6..753b055 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,21 @@ -As developers, we all know the importance of automating repetitive tasks. Not only does it save time and effort, but it also eliminates the possibility of human error. One area where automation can be particularly useful is in database management. +# Supplier-SQL -Recently, we explored how to automate various database tasks using Python and MySQL. We started by checking for the existence of a database named "spj", and if it existed, we dropped it and then created a new one with the same name. +Read about this repository [here](ABOUT.md). -We then inserted multiple rows of data into the tables named "supplier", "shipment", "project", and "part" using the mysql-connector-python package and the INSERT INTO statement. The data was taken from text files saved in the same directory, with rows separated by a new line and column data separated by whitespace in the text files. +### Start working with the project +1. Clone this repository by `git clone https://github.com/huzefamehidpurwala/Supplier-Python-SQL.git`. +2. After successfully cloning the repository change directory by `cd Supplier-Python-SQL`. +3. (Optional) Create virtual environment for the project by `python3 -m venv Supplier-Python-SQL_venv`. + - If using linux and get any error then install the python3-venv package `sudo apt install -y python3.-venv` here, `` should be replaced by the current version of python installed in the system. For ex: `sudo apt install -y python3.8-venv`. Now re-execute the above command. +4. (Necessary if used the above 3rd point) Activate the environment: + - for windows `Supplier-Python-SQL_venv\Scripts\activate` + - for linux `Supplier-Python-SQL_venv/bin/activate` +5. Install necessary python modules by `pip install mysql-connector-python`. +6. Open `main.py` in editing mode: + - Windows `notepad main.py` + - Linux `nano main.py` + + Enter the "username" and "password" of the mysql having major rights for editing the tables and databases accordingly and save the file. +7. Now execute the _python script_ by `python main.py`. -However, when we tried to truncate the parent table, we encountered an issue as the "shipment" table had foreign key constraints referencing the primary keys of the other tables. To solve this, we deleted the foreign key constraints referencing the primary keys of the other tables and then truncated the "shipment" table. - -In addition to the basic CRUD operations, we have also seen how to handle the foreign key constraints while truncating the tables and how to handle the case where the database does not exist. - -By using tools like Python and MySQL, as well as libraries such as sqlalchemy, developers can easily create, modify, and query databases, as well as insert and manipulate data, and handle various database related tasks. - -In order to be an effective developer, it's important to have a good understanding of database management and the tools available. By staying up-to-date with the latest developments in the field and experimenting with different tools and techniques, developers can continue to improve their skills and become more efficient at managing databases. - -Don't wait, start automating your database tasks today, and see the difference for yourself! \ No newline at end of file +***Now you have your database and tables ready to solve SQL Challenge!*** ‍🎓 diff --git a/main.py b/main.py index 93003e0..4b9a54f 100644 --- a/main.py +++ b/main.py @@ -11,14 +11,24 @@ # Create a cursor object cursor = db.cursor() +# Read the SQL file +with open("path/to/file.sql", 'r') as f: + sql = f.read() + +# Execute the SQL commands +cursor.execute(sql) + +# Commit the changes to the database +db.commit() + # Read data from text files -with open("supplier.txt") as f: +with open("Databases/supplier.txt") as f: supplier_data = f.readlines() -with open("shipment.txt") as f: +with open("Databases/shipment.txt") as f: shipment_data = f.readlines() -with open("project.txt") as f: +with open("Databases/project.txt") as f: project_data = f.readlines() -with open("part.txt") as f: +with open("Databases/part.txt") as f: part_data = f.readlines() # Iterate over each line of data and insert it into the corresponding table diff --git a/questions-solutions.sql b/questions-solutions.sql new file mode 100644 index 0000000..2be0260 --- /dev/null +++ b/questions-solutions.sql @@ -0,0 +1,122 @@ +-- Comments: "-- " and "/*___*/" + +USE spj; -- "use" is used to select database. +-- ==== Query questions ==== -- + +-- 1. Get Full details of all projects. +SELECT * FROM project; -- "select" query is used to select or fetch data from the particular database.table + +-- 2. Get Full details of all projects in London. +SELECT * FROM project + WHERE CITY="LONDON"; -- "where" is used to filter records returned by select. + +-- 3. Get supplier numbers for suppliers who supply projects J1. +SELECT SNO FROM shipment + WHERE JNO="J1"; + +-- 4. Get all shipments where the quantity is in the range 300 to 750 inclusive. +SELECT * FROM shipment + WHERE QTY BETWEEN 300 AND 750; -- "between" operator selects values within a given range. + -- AND, OR, and NOT are logical operators + +/* 5. Get all part-color/part-city pairs. + Note : Here and subsequently, the terms “all” means “all currently represented in the database, “ not “all possible” +*/ +SELECT CONCAT_WS("-", PNAME, COLOR) AS "part-color", + CONCAT_WS("-", PNAME, CITY) AS "part-city" FROM part; +-- or +SELECT CONCAT_WS("\t", + CONCAT(PNAME, "-", COLOR), + CONCAT(PNAME, "-", CITY) + ) AS columnName FROM part; +-- "concat" function is used concatenate strings (outputs) +-- "concat_ws" function is used to concatenate strings (outputs) with a specified character. +-- "as" is used to give alias (a temporary name) + +/* 6 Get all supplier -number/part- number/project- number triples such that the indicated supplier, part and + project are all collocated (i.e. all in the same city). output: https://prnt.sc/RZkLL_qFr9_U */ +-- for above we have to specify a inner join as we want the same/common data from different tables. +SELECT CONCAT_WS("->", s.SNO, p.PNO, j.JNO) AS columnName, s.CITY +FROM supplier AS s + JOIN part AS p + JOIN project AS j ON p.CITY = j.CITY AND j.CITY = s.CITY; +-- OR +SELECT CONCAT_WS("->", s.SNO, p.PNO, j.JNO) AS columnName, + s.CITY AS supplierCity, + p.CITY AS partCity, + j.CITY AS projectCity +FROM supplier AS s, + part AS p, + project AS j +WHERE s.CITY=p.CITY AND p.CITY=j.CITY AND s.CITY = j.CITY; +-- A JOIN clause is used to combine rows from two or more tables, based on a related column between them. + +/* 7. Get all supplier -number/part- number/project- number triples such that the indicated supplier, part +and project are not all collocated. output: https://prnt.sc/FVrRIegH5gLh */ +SELECT CONCAT_WS("->", s.SNO, p.PNO, j.JNO) AS columnName, + s.CITY AS supplierCity, + p.CITY AS partCity, + j.CITY AS projectCity +FROM supplier AS s, + part AS p, + project AS j +WHERE NOT (s.CITY=p.CITY AND p.CITY=j.CITY AND s.CITY = j.CITY); +-- OR +SELECT CONCAT_WS(" -> ", s.SNO, p.PNO, j.JNO) AS columnName, + s.CITY AS supplierCity, + p.CITY AS partCity, + j.CITY AS projectCity +FROM supplier AS s + JOIN part AS p + JOIN project AS j ON NOT (p.CITY = j.CITY AND j.CITY = s.CITY AND s.CITY = j.CITY); + +/* 8. Get all supplier -number/part- number/project- number triples such that no two of the indicated +supplier, part and project are collocated. */ +SELECT CONCAT_WS(" -> ", s.SNO, p.PNO, j.JNO) AS columnName, + s.CITY AS supplierCity, + p.CITY AS partCity, + j.CITY AS projectCity +FROM supplier AS s + JOIN part AS p + JOIN project j ON NOT s.CITY = p.CITY AND NOT p.CITY = j.CITY AND NOT s.CITY = j.CITY; +-- OR +SELECT CONCAT_WS(" -> ", s.SNO, p.PNO, j.JNO) AS columnName, + s.CITY AS supplierCity, + p.CITY AS partCity, + j.CITY AS projectCity +FROM supplier AS s, + part AS p, + project j +WHERE NOT s.CITY = p.CITY AND NOT p.CITY = j.CITY AND NOT s.CITY = j.CITY; + +-- 9. Get full details for parts supplied by the supplier in the London. +SELECT * FROM part WHERE CITY="London"; + +-- 10. Get part numbers for parts supplied by a supplier in London to a project in London. +SELECT DISTINCT part.PNO-- , part.CITY, p.CITY +FROM part +LEFT JOIN project p on part.CITY = p.CITY WHERE p.CITY="LONDON" +ORDER BY part.PNO; +-- DISTINCT keyword is used to filter the repeated data rows +-- ORDER BY is used sort data in ascending or descending with respect to specified column. default is ASC, can use DESC. + +-- 11. Get all pairs of city names such that a supplier in the first city supplies a project in the second city. + + +-- 15. Get the total number of projects supplied by supplier S1. +SELECT SNO, COUNT(JNO) AS totalNumProjects +FROM shipment +WHERE SNO="S1" +GROUP BY SNO; +-- COUNT() function returns the number of records returned by a select query +-- The GROUP BY statement groups rows that have the same values into summary rows, often used with aggregate functions +-- to group the result-set by one or more columns. + +-- 16. Get the total quantity of part P1 supplied by supplier S1. +SELECT SNO, PNO, SUM(QTY) totalQuantity +FROM shipment +WHERE SNO="S1" AND PNO="P1" +GROUP BY SNO; +-- SUM function is used sum the values of select query + +-- diff --git a/questions.txt b/questions.txt new file mode 100644 index 0000000..75a9333 --- /dev/null +++ b/questions.txt @@ -0,0 +1,88 @@ +Write SQL Queries for the above database: +1 Get Full details of all projects. + +2 Get Full details of all projects in London. + +3 Get supplier numbers for suppliers who supply projects J1. + +4 Get all shipments where the quantity is in the range 300 to 750 inclusive. + +5 Get all part-color/part-city pairs. Note : Here and subsequently, the terms “all” means “all currently +represented in the database, “ not “all possible”. + +6 Get all supplier -number/part- number/project- number triples such that the indicated supplier, part and +project are all collocated (i.e. all in the same city). + +7 Get all supplier -number/part- number/project- number triples such that the indicated supplier, part +and project are not all collocated. + +8 Get all supplier -number/part- number/project- number triples such that no two of the indicated +supplier, part and project are collocated. + +9 Get full details for parts supplied by the supplier in the London. + +10 Get part numbers for parts supplied by a supplier in London to a project in London. + +11 Get all pairs of city names such that a supplier in the first city supplies a project in the second city. + +12 Get part numbers for parts supplied to any project by a supplier in the same city as that project. + +13 Get project numbers for projects supplied by at least one supplier not in the same city. + +14 Get all pairs of part numbers such that some supplier supplies both the indicated parts. + +15 Get the total number of projects supplied by supplier S1. + +16 Get the total quantity of part P1 supplied by supplier S1. + +17 For each part being supplied to a project, get the part number, the project number, and the +corresponding total quantity. + +18 Get part numbers of parts supplied to some project in an average quantity of more than 350. + +19 Get project names for projects supplied by supplier S1. + +20 Get colors of parts supplied by supplier S1. + +21 Get part numbers for parts supplied to any project in London. + +22 Get project numbers for projects using at least one part available from supplier S1. + +23 Get supplier numbers for suppliers supplying at least one part supplied by at least one supplier who +supplies at least one red part. + +24 Get supplier numbers for suppliers with a status lower than that of supplier S1. + +25 Get project numbers for projects whose city is first in the alphabetic list of such cities + +26 Get project numbers for projects supplied with part P1 in an average quantity greater than the greatest +quantity in which any part is supplied to project J1 + +27 Get supplier numbers for suppliers supplying some project with part P1 in a quantity greater than the +average shipment quantity of part P1 for that project. + +28 Get project numbers for project not supplied with any red part by any London supplier. + +29 Get project numbers for projects supplied entirely by supplier SI. + +30 Get part numbers for parts supplied to all projects in London. + +31 Get supplier numbers for suppliers who supply the same part to all projects. + +32 Get project numbers for projects supplied with at least all parts available from +supplier SI. + +33 Get all cities in which at least one supplier. Part. Or project is located. + +34 Get part numbers for parts that are supplied either by London supplier or to a London +project. + +35 Get supplier-number/part-number pairs such that the indicated supplier does not +supply the indicated part. + +36 Get all pairs of supplier numbers, Sx and Sy say. Such that Sx and Sy supply exactly +the same set of parts each. Note: For simplicity, you might want to use the original +suppliers-and-part data-base for this exercise, instead of the expanded suppliers-partprojects database. + +37 Get a “grouped” version of all shipment showing, for each suppliers-number/partnumber pair, the corresponding project numbers and quantities in the form of a binary +relation.