From 87475042baf13933ce41f4ca0411539b434d22fb Mon Sep 17 00:00:00 2001 From: huzefamehidpurwala Date: Tue, 17 Jan 2023 12:05:23 +0530 Subject: [PATCH 01/10] New Commit --- commands.sql | 2 +- questions-solutions.sql | 48 +++++++++++++++++++++++++++++++++++++++++ supplier.txt | 4 ++-- 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 questions-solutions.sql diff --git a/commands.sql b/commands.sql index d14087d..dcf247c 100644 --- a/commands.sql +++ b/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/questions-solutions.sql b/questions-solutions.sql new file mode 100644 index 0000000..d85982f --- /dev/null +++ b/questions-solutions.sql @@ -0,0 +1,48 @@ +-- 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). */ +-- 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 ON s.CITY = p.CITY + JOIN project AS j ON p.CITY = j.CITY; +-- OR +SELECT CONCAT_WS("->", s.SNO, p.PNO, j.JNO) AS columnName, s.CITY +FROM supplier AS s, + part AS p, + project AS j +WHERE s.CITY=p.CITY AND p.CITY=j.CITY; diff --git a/supplier.txt b/supplier.txt index 6d7e283..73121d9 100644 --- a/supplier.txt +++ b/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 From ec9db35f41f9344226239e1885ad6c661ac1735c Mon Sep 17 00:00:00 2001 From: huzefamehidpurwala Date: Tue, 17 Jan 2023 12:07:31 +0530 Subject: [PATCH 02/10] New Commit --- ABOUT.md | 15 +++++++++++++++ README.md | 22 ++++++++++------------ 2 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 ABOUT.md 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/README.md b/README.md index ceee1d6..4e80f15 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,13 @@ -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. Create virtual environment for the project by `python3 -m venv Supplier-Python-SQL_venv`. +4. Activate the environment: + - for windows `Supplier-Python-SQL_venv\Scripts\activate` + - for linux `Supplier-Python-SQL_venv/bin/activate` -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 +***Still work going on...! Stay tuned!*** 🎁 From 97baf57e0656d527f6193c539d002164b9a1c571 Mon Sep 17 00:00:00 2001 From: huzefamehidpurwala Date: Tue, 17 Jan 2023 12:25:00 +0530 Subject: [PATCH 03/10] New Commit --- questions-solutions.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/questions-solutions.sql b/questions-solutions.sql index d85982f..73e001f 100644 --- a/questions-solutions.sql +++ b/questions-solutions.sql @@ -46,3 +46,5 @@ FROM supplier AS s, part AS p, project AS j WHERE s.CITY=p.CITY AND p.CITY=j.CITY; + + From 5c405f02b653c866bbe74c59dbfc23d3230ef8e6 Mon Sep 17 00:00:00 2001 From: huzefamehidpurwala Date: Tue, 17 Jan 2023 13:07:13 +0530 Subject: [PATCH 04/10] questions-solutions.sql Changed --- questions-solutions.sql | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/questions-solutions.sql b/questions-solutions.sql index 73e001f..4e24643 100644 --- a/questions-solutions.sql +++ b/questions-solutions.sql @@ -34,7 +34,7 @@ SELECT CONCAT_WS("\t", -- "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). */ + 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 @@ -46,5 +46,4 @@ FROM supplier AS s, part AS p, project AS j WHERE s.CITY=p.CITY AND p.CITY=j.CITY; - - +-- A JOIN clause is used to combine rows from two or more tables, based on a related column between them. From 73b926e7070d5da3aeb2421431caa9b8b0f72c39 Mon Sep 17 00:00:00 2001 From: huzefamehidpurwala Date: Wed, 18 Jan 2023 11:23:52 +0530 Subject: [PATCH 05/10] Working Going on! --- commands.sql => Databases/commands.sql | 0 part.txt => Databases/part.txt | 0 project.txt => Databases/project.txt | 0 shipment.txt => Databases/shipment.txt | 0 supplier.txt => Databases/supplier.txt | 0 README.md | 4 +- main.py | 18 ++++-- questions-solutions.sql | 52 +++++++++++++-- questions.txt | 88 ++++++++++++++++++++++++++ 9 files changed, 153 insertions(+), 9 deletions(-) rename commands.sql => Databases/commands.sql (100%) rename part.txt => Databases/part.txt (100%) rename project.txt => Databases/project.txt (100%) rename shipment.txt => Databases/shipment.txt (100%) rename supplier.txt => Databases/supplier.txt (100%) create mode 100644 questions.txt diff --git a/commands.sql b/Databases/commands.sql similarity index 100% rename from commands.sql rename to Databases/commands.sql 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 100% rename from supplier.txt rename to Databases/supplier.txt diff --git a/README.md b/README.md index 4e80f15..d39668b 100644 --- a/README.md +++ b/README.md @@ -9,5 +9,7 @@ Read about this repository [here](ABOUT.md). 4. 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. Now execute the _python script_ by `python main.py`. -***Still work going on...! Stay tuned!*** 🎁 +***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 index 4e24643..5e32a4d 100644 --- a/questions-solutions.sql +++ b/questions-solutions.sql @@ -38,12 +38,56 @@ SELECT CONCAT_WS("\t", -- 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 ON s.CITY = p.CITY - JOIN project AS j ON p.CITY = j.CITY; + 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 +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; +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. + 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. From 25bb42401e8463a0c19cbacd175bce645577d4e9 Mon Sep 17 00:00:00 2001 From: huzefamehidpurwala Date: Thu, 19 Jan 2023 12:48:42 +0530 Subject: [PATCH 06/10] Solving Questions --- questions-solutions.sql | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/questions-solutions.sql b/questions-solutions.sql index 5e32a4d..0e553d2 100644 --- a/questions-solutions.sql +++ b/questions-solutions.sql @@ -90,4 +90,33 @@ FROM supplier AS s, 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 + From 39afc9b46a15427f4483c1853bf445e4154a20e7 Mon Sep 17 00:00:00 2001 From: huzefamehidpurwala Date: Thu, 19 Jan 2023 13:01:21 +0530 Subject: [PATCH 07/10] Update README.md --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d39668b..02e72dc 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,11 @@ Read about this repository [here](ABOUT.md). - 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. Now execute the _python script_ by `python main.py`. +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`. ***Now you have your database and tables ready to solve SQL Challenge!*** ‍🎓 From 64fbf5d19c55d252d488f50f9d31b18fd567618c Mon Sep 17 00:00:00 2001 From: huzefamehidpurwala Date: Fri, 20 Jan 2023 22:42:28 +0530 Subject: [PATCH 08/10] Update commands.sql --- Databases/commands.sql | 13 +++++++++++-- README.md | 5 +++-- questions-solutions.sql | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Databases/commands.sql b/Databases/commands.sql index dcf247c..15fa090 100644 --- a/Databases/commands.sql +++ b/Databases/commands.sql @@ -1,5 +1,14 @@ -DROP DATABASE IF EXISTS spj; - CREATE DATABASE spj; +-- DROP DATABASE IF EXISTS spj; +-- CREATE DATABASE spj; + +USE mysql; +SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'spj'; + +IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'spj') THEN + DROP DATABASE spj; +END IF; + +CREATE DATABASE spj; USE spj; diff --git a/README.md b/README.md index 02e72dc..753b055 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,9 @@ Read about this repository [here](ABOUT.md). ### 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. Create virtual environment for the project by `python3 -m venv Supplier-Python-SQL_venv`. -4. Activate the environment: +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`. diff --git a/questions-solutions.sql b/questions-solutions.sql index 0e553d2..2be0260 100644 --- a/questions-solutions.sql +++ b/questions-solutions.sql @@ -119,4 +119,4 @@ WHERE SNO="S1" AND PNO="P1" GROUP BY SNO; -- SUM function is used sum the values of select query - +-- From 10066fd902f135800d32cceb704fa1d69f81fbe9 Mon Sep 17 00:00:00 2001 From: huzefamehidpurwala Date: Fri, 20 Jan 2023 22:48:52 +0530 Subject: [PATCH 09/10] Update commands.sql --- Databases/commands.sql | 44 ++++++------------------------------------ 1 file changed, 6 insertions(+), 38 deletions(-) diff --git a/Databases/commands.sql b/Databases/commands.sql index 15fa090..18418fc 100644 --- a/Databases/commands.sql +++ b/Databases/commands.sql @@ -1,44 +1,12 @@ --- DROP DATABASE IF EXISTS spj; --- CREATE DATABASE spj; - -USE mysql; -SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'spj'; - -IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'spj') THEN - DROP DATABASE spj; -END IF; - -CREATE DATABASE spj; +DROP DATABASE IF EXISTS spj; + CREATE DATABASE spj; USE spj; -create table supplier ( - SNO varchar(5) Primary key, - SNAME varchar(25) NOT NULL, - STATUS int NOT NULL, - CITY varchar(20) NOT NULL -); +create table supplier (SNO varchar(5) Primary key, SNAME varchar(25) NOT NULL, STATUS int NOT NULL, CITY varchar(20) NOT NULL); -create table part ( - PNO varchar(6) Primary key, - PNAME varchar(25) NOT NULL, - COLOR varchar(10) NOT NULL, - WEIGHT numeric(5, 1) NOT NULL, - CITY varchar(20) NOT NULL -); +create table part (PNO varchar(6) Primary key, PNAME varchar(25) NOT NULL, COLOR varchar(10) NOT NULL, WEIGHT numeric(5, 1) NOT NULL, CITY varchar(20) NOT NULL); -create table project ( - JNO varchar(6) Primary key, - JNAME varchar(25) NOT NULL, - CITY varchar(20) NOT NULL -); +create table project (JNO varchar(6) Primary key, JNAME varchar(25) NOT NULL, CITY varchar(20) NOT NULL); -create table shipment ( - SNO varchar(6) NOT NULL, - PNO varchar(6) NOT NULL, - JNO varchar(6) NOT NULL, - QTY int NOT NULL, - foreign key (SNO) references supplier(SNO), - foreign key (PNO) references part(PNO), - foreign key (JNO) references project(JNO) -); +create table shipment (SNO varchar(6) NOT NULL, PNO varchar(6) NOT NULL, JNO varchar(6) NOT NULL, QTY int NOT NULL, foreign key (SNO) references supplier(SNO), foreign key (PNO) references part(PNO), foreign key (JNO) references project(JNO)); From 0717ec9d1947466547022747e2aa08c3196fe954 Mon Sep 17 00:00:00 2001 From: huzefamehidpurwala Date: Fri, 20 Jan 2023 23:46:28 +0530 Subject: [PATCH 10/10] Update commands.sql --- Databases/commands.sql | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/Databases/commands.sql b/Databases/commands.sql index 18418fc..dcf247c 100644 --- a/Databases/commands.sql +++ b/Databases/commands.sql @@ -3,10 +3,33 @@ DROP DATABASE IF EXISTS spj; USE spj; -create table supplier (SNO varchar(5) Primary key, SNAME varchar(25) NOT NULL, STATUS int NOT NULL, CITY varchar(20) NOT NULL); +create table supplier ( + SNO varchar(5) Primary key, + SNAME varchar(25) NOT NULL, + STATUS int NOT NULL, + CITY varchar(20) NOT NULL +); -create table part (PNO varchar(6) Primary key, PNAME varchar(25) NOT NULL, COLOR varchar(10) NOT NULL, WEIGHT numeric(5, 1) NOT NULL, CITY varchar(20) NOT NULL); +create table part ( + PNO varchar(6) Primary key, + PNAME varchar(25) NOT NULL, + COLOR varchar(10) NOT NULL, + WEIGHT numeric(5, 1) NOT NULL, + CITY varchar(20) NOT NULL +); -create table project (JNO varchar(6) Primary key, JNAME varchar(25) NOT NULL, CITY varchar(20) NOT NULL); +create table project ( + JNO varchar(6) Primary key, + JNAME varchar(25) NOT NULL, + CITY varchar(20) NOT NULL +); -create table shipment (SNO varchar(6) NOT NULL, PNO varchar(6) NOT NULL, JNO varchar(6) NOT NULL, QTY int NOT NULL, foreign key (SNO) references supplier(SNO), foreign key (PNO) references part(PNO), foreign key (JNO) references project(JNO)); +create table shipment ( + SNO varchar(6) NOT NULL, + PNO varchar(6) NOT NULL, + JNO varchar(6) NOT NULL, + QTY int NOT NULL, + foreign key (SNO) references supplier(SNO), + foreign key (PNO) references part(PNO), + foreign key (JNO) references project(JNO) +);