This project is used to experiment with SQL.
Added and updated critical files.
Add/Add .gitignore The .gitignore file tells Git files to ignore when committing changes. Review/copy the example .gitignore file, you might be able to use it without modification. Add/Update requirements.txt The requirements.txt file lists the packages used in the project. Review/copy the example requirements.txt file, you might be able to use it without modification. You may not need all the listed packages - and may want to add others. Modify the requirements.txt as needed. Update README.md Edit and customize your README.md to provide an overview of the project and instructions for running it. Git add-commit-push After adding .gitignore (or any other key file), run git add, commit, and push to commit your changes to GitHub.
On Mac/Linux, Use zsh or bash (or PowerShell): python3 -m venv .venv
Do you want to select it for the workspace folder? Click Yes.
source .venv/bin/activate
Design a schema with at least two related tables, including foreign key constraints. Document the schema design in your README.md.
sql_create folder:
01_drop_tables.sql - drop tables to restart 02_create_tables.sql - create your database schema using sql 03_insert_records.sql - insert at least 10 additional records into each table. db01_setup.py:
Create a Python script that demonstrates the ability to create a database, define a schema, and insert records. Make it easy to re-run by dropping the tables first.
Implement SQL statements and queries to perform additional operations and use Python to execute your SQL statements. You might create an additional table, insert new records, and perform data querying (with filters, sorting, and joining tables), data aggregation, and record update and deletion.
sql_features folder:
update_records.sql - update 1 or more records in a table. delete_records.sql - delete 1 or more records from a table. db02_features.py
Create a Python script that demonstrates the ability to run sql scripts to interact with fields, update records, delete records, and maybe add additional columns.
Implement SQL statements and queries to perform aggregations and queries.
sql_queries folder:
query_aggregation.sql - use aggregation functions including COUNT, AVG, SUM. query_filter.sql - use WHERE to filter data based on conditions. query_sorting.sql - use ORDER BY to sort data. query_group_by.sql - use GROUP BY clause (and optionally with aggregation) query_join.sql - use INNER JOIN operation and optionally include LEFT JOIN, RIGHT JOIN, etc. Use Python to execute the SQL queries and maybe chart, illustrate, and/or summarize your findings:
db03_queries.py
This project uses two related tables to store information about books and authors. The schema includes:
-
authors: A table containing information about authors. It includes:
author_id(Primary Key)first_namelast_nameyear_born
-
books: A table containing information about books. It includes:
book_id(Primary Key)titleyear_publishedauthor_id(Foreign Key referencingauthors)
01_drop_tables.sql: Drops theauthorsandbookstables if they exist. This allows us to re-run the script for development or testing purposes.02_create_tables.sql: Creates theauthorsandbookstables with the necessary schema and relationships.03_insert_records.sql: Inserts 10 records into theauthorsandbookstables.
db01_setup.py: This Python script demonstrates how to create the SQLite database, define the schema, and insert the records. It automatically drops existing tables to ensure a clean setup and then re-creates the schema and inserts the data.
- Ensure the SQL scripts (
01_drop_tables.sql,02_create_tables.sql,03_insert_records.sql) are located in thesql_createfolder. - Run the
db01_setup.pyPython script. It will create a SQLite database (books_authors.db), define the tables, and insert the records. - The database file
books_authors.dbwill be created in the project directory.
- The schema uses a foreign key relationship between the
booksandauthorstables. - The Python script is designed to be re-runnable: it first drops any existing tables before re-creating and populating them.
- Update Records (update_records.sql) This file contains SQL statements to update one or more records in the database. For example, updating the publication year of a book or changing an author's last name.
- Delete Records (delete_records.sql) This file contains SQL statements to delete records from the database. You can delete books or authors based on specific conditions.
SQL File Execution: The Python script now executes SQL files from the sql_queries folder. Query Execution:
- Aggregation: Queries like counting the total number of books and calculating the average publication year are executed.
- Filtering: Books published after 2015 are filtered.
- Sorting: Books are sorted by publication year.
- Grouping: Books are grouped by author, showing how many books each author has published.
- Join: The script uses INNER JOIN and LEFT JOIN to fetch books with their respective authors. Displaying Results: The results of each query are printed to the console. Optional: If needed, you can further enhance this script to create visualizations using matplotlib.
Books Published Per Year: A bar chart displays the total number of books published each year. Books Published Per Author: A horizontal bar chart shows how many books each author has published.