Hello everyone, this is going to be the assignment of Databases 101. In here, you have to perform CRUD operations in JavaScript or Python.
You have to perform CRUD operations in the given sequence according to this. You are given a list of instructions, follow them to setup a local database on your computer system and then start working on the project. Once done, you can run a SQL query(given below) to populate dummy data in the database. Once there is data in your local database, you are good to go with the rest of the project given below.
-
Download and Install MySQL:
- Visit the official MySQL website and download the MySQL Community Server appropriate for your operating system.
- Follow the installation wizard to install MySQL on your computer.
-
Download and Install MySQL Workbench:
- Visit the MySQL Workbench download page and install the latest version.
- Use MySQL Workbench to manage your databases, run SQL queries, and visualize the database schema.
-
You can also refer to this video for installation of both: https://youtu.be/If_racf2ocM?si=3G4tqjWNlgtlKUKn
- Create a New Database:
- Open MySQL Workbench and connect to your local MySQL server.
- Execute the following command to create a new database (replace
your_database_namewith databases):CREATE DATABASE databases;
- Create a Table for Dummy Data:
- Switch to your newly created database:
USE databases;
- Switch to your newly created database:
- Create a users table
CREATE TABLE IF NOT EXISTS users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, email VARCHAR(100) NOT NULL UNIQUE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
- Run the following SQL query in MySQL Workbench to insert dummy data into the
userstable:INSERT INTO users (name, email) VALUES ('David Miller', 'david.miller@example.com'), ('Emily Clark', 'emily.clark@example.com'), ('Frank Peterson', 'frank.peterson@example.com'), ('Grace Lee', 'grace.lee@example.com'), ('Hannah Scott', 'hannah.scott@example.com'), ('Ian Turner', 'ian.turner@example.com'), ('Jane Doe', 'jane.doe@example.com');
- Now you are ready to start with the project
- Set Up Your Node.js Environment:
- Ensure that Node.js is installed on your system.
- Create a new Node.js project and initialize it:
mkdir my_project cd my_project npm init -y
- Install MySQL Package:
- Install the MySQL package (or mysql2 for promise support):
npm install mysql
- Connect to Your Database and Implement CRUD:
- Create a JavaScript file (e.g.,
app.js) and include the following sample code to write the code in the given stub.
- Create a JavaScript file (e.g.,
- Set Up Your Python Environment:
- Ensure that Python is installed on your system.
- Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows, use venv\Scripts\activate
- Install the MySQL Connector:
- Install the
mysql-connector-pythonpackage:pip install mysql-connector-python
- Install the
- Connect to Your Database and Implement CRUD:
- Create a Python script (e.g.,
app.py) with the code stub given.
- Create a Python script (e.g.,
You need to perform CRUD operations in the following order:
- Create Operation: Add a new record with the name Anurag Kumar and the email kumar.anurag@gmail.com.
- Update Operation: Modify the email address of the user David Miller.
- Delete Operation: Remove the record of the user Emily Clark.
- Operation:
Add a new user record.- SQL Query:
INSERT INTO users (name, email) VALUES ('Anurag Kumar', 'kumar.anurag@gmail.com');
- SQL Query:
- Operation:
Update the email address of David Miller.- SQL Query:
(Assuming the new email isdavid.miller_updated@example.com.) - Expected Outcome:
The email field for the record where the name is David Miller is updated.
- SQL Query:
- Operation:
Delete the record for Emily Clark.- Expected Outcome:
The record with the name Emily Clark is removed from theuserstable.
- Expected Outcome:
- Final Verification
After executing the above operations sequentially, perform a final read operation to verify the state of the database:
SELECT * FROM users
- It should look something like this. Note that the id and timestamp may differ.
| id | name | created_at | |
|---|---|---|---|
| 1 | Alice Johnson | alice.johnson@example.com | 2025-02-13 08:45:00 |
| 5 | David Miller | david.miller_updated@example.com | 2025-02-14 15:20:00 |
| 10 | Anurag Kumar | kumar.anurag@gmail.com | 2025-02-15 10:05:00 |
You have to create an index of user's name and their email so that we can fetch the email of the user just by having their name. You will have to implement it from scratch or in the code stub provided. Make sure its not GPTed, we follow patterns in code.