Skip to content

Editings #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ABOUT.md
Original file line number Diff line number Diff line change
@@ -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!
22 changes: 10 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -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!
***Still work going on...! Stay tuned!*** 🎁
2 changes: 1 addition & 1 deletion commands.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
);
50 changes: 50 additions & 0 deletions questions-solutions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-- 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;


4 changes: 2 additions & 2 deletions supplier.txt
Original file line number Diff line number Diff line change
@@ -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