Assignment15Utkarsh
Assignment15Utkarsh
Question 1
Answer:
A database is a collection of organized data that is stored and managed on a computer system. It allows for
efficient storage, retrieval, and manipulation of large amounts of data. A database system typically consists of
software that manages the data, a database server that stores the data, and one or more applications that
access the data.
SQL (Structured Query Language) and NoSQL (Not only SQL) are two different types of database management
systems that use different methods for storing and retrieving data. Here are some key differences between the
two:
Usage Complex queries and transactions Fast and flexible data processing
1. SQL Databases:
mySQL
Oracle
PostgreSQL
Microsoft SQL Server
SQLite
2. NoSQL Databases:
MongoDB (document-oriented)
Cassandra (column-family)
Redis (key-value)
Neo4j (graph)
Amazon DynamoDB (document-oriented)
It's worth noting that there are many different types of NoSQL databases, each with their own strengths and
weaknesses. For example, document-oriented databases like MongoDB are great for storing unstructured data
such as JSON documents, while graph databases like Neo4j are ideal for modeling complex relationships
between data points.
Similarly, there are many different types of SQL databases, each with their own features and benefits. For
example, MySQL is a popular choice for web applications due to its fast performance and scalability, while
PostgreSQL is often used for data warehousing and business intelligence applications due to its advanced query
optimization capabilities.
Ultimately, the choice between SQL and NoSQL databases will depend on the specific requirements of your
application and the type of data you need to store and process.
Question 2
Using !pip install to install above package through this jupyter notebook
In [1]:
!pip install sqlalchemy==1.3.9 ipython-sql
In [2]:
%load_ext sql
In [3]:
import csv, sqlite3
con = sqlite3.connect("test.db")
cur = con.cursor()
In [4]:
%sql sqlite:///test.db
With above a connection is now established to test.db and we can directly write a query
with %%sql magic command using above
Note: The %%sql command is a Jupyter Notebook magic command that allows you to execute SQL queries
directly in a notebook cell. Provided that database connection is established
In [5]:
%%sql
CREATE TABLE if not exists users
(id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL);
* sqlite:///test.db
Done.
Out[5]:
[]
In [6]:
# Viewing Above table
%sql SELECT * FROM users;
* sqlite:///test.db
Done.
Done.
Out[6]:
1. DROP: The DROP command is used to delete an existing database object. For example, to delete the
"users" table created in the previous example, you would use the following SQL statement:
In [7]:
# DROP example to delete tables
%sql DROP TABLE users;
* sqlite:///test.db
Done.
Out[7]:
[]
In [8]:
%sql SELECT * FROM users
#Below output shows that users table is deleted (no such table: users)
* sqlite:///test.db
(sqlite3.OperationalError) no such table: users
[SQL: SELECT * FROM users]
(Background on this error at: http://sqlalche.me/e/e3q8)
1. ALTER: The ALTER command is used to modify an existing database object, such as a table, index, or view.
For example, to add a new column to the "users" table to track a user's login status.
In [9]:
%%sql
CREATE TABLE if not exists users
(id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL);
* sqlite:///test.db
Done.
Out[9]:
[]
In [10]:
%sql SELECT * FROM users;
* sqlite:///test.db
Done.
Out[10]:
In [11]:
# ALTER Example to change column name and schema
%sql ALTER TABLE users ADD COLUMN login_status BOOLEAN;
* sqlite:///test.db
Done.
Out[11]:
[]
[]
In [12]:
%sql SELECT * FROM users;
* sqlite:///test.db
Done.
Out[12]:
1. TRUNCATE: The TRUNCATE command is used to delete all the rows in a table, while keeping the table
structure intact. For example, to delete all the data in the "users" table but keep the table structure, you
would use the following SQL statement:
Please note that SQLite has command : DELETE FROM table_name instead of TRUNCATE from mySQL
In [13]:
%sql select * from users;
* sqlite:///test.db
Done.
Out[13]:
In [14]:
%%sql
INSERT INTO users VALUES (1,'John Doe','[email protected]','123#413s',False);
INSERT INTO users VALUES (2, 'Jane Doe','[email protected]','21wdds2',True);
* sqlite:///test.db
1 rows affected.
1 rows affected.
Out[14]:
[]
In [15]:
%sql SELECT * FROM users;
* sqlite:///test.db
Done.
Out[15]:
In [16]:
# Truncate Example SQLITE Has DELETE command instead of TRUNCATE
%sql DELETE FROM users;
* sqlite:///test.db
2 rows affected.
Out[16]:
[]
In [17]:
%sql SELECT * FROM users;
* sqlite:///test.db
Done.
Out[17]:
Question 3
In [18]:
%%sql
CREATE TABLE if not exists students
(name VARCHAR(50) ,
age INT ,
grade VARCHAR(2));
* sqlite:///test.db
Done.
Out[18]:
[]
In [19]:
# Viewing the students table
%sql SELECT * FROM students
* sqlite:///test.db
Done.
Out[19]:
1. INSERT - The INSERT command is used to add new data to a database table.
In [20]:
#INSERT Command
%sql INSERT INTO students VALUES ('Utkarsh Gaikwad', 28, 'B');
* sqlite:///test.db
1 rows affected.
Out[20]:
[]
In [21]:
# Viewing the table now
%sql SELECT * FROM students
* sqlite:///test.db
Done.
Out[21]:
Utkarsh Gaikwad 28 B
1. UPDATE: The UPDATE command is used to modify existing data in a database table
In [22]:
# UPDATE example
%sql UPDATE students SET grade = 'A+' WHERE name = 'Utkarsh Gaikwad';
* sqlite:///test.db
1 rows affected.
Out[22]:
[]
In [23]:
#View Updated table
%sql SELECT * FROM students;
* sqlite:///test.db
Done.
Out[23]:
Utkarsh Gaikwad 28 A+
1. DELETE: The DELETE command is used to remove data from a database table.
In [24]:
%sql DELETE FROM students WHERE name='Utkarsh Gaikwad';
* sqlite:///test.db
1 rows affected.
Out[24]:
[]
In [25]:
%sql SELECT * FROM students;
* sqlite:///test.db
Done.
Out[25]:
In [27]:
%sql SELECt * FROM students
* sqlite:///test.db
Done.
Out[27]:
In [28]:
%%sql
INSERT INTO students VALUES ('Utkarsh', 28, 'A');
INSERT INTO students VALUES ('John', 35, 'B');
INSERT INTO students VALUES ('Krish', 40, 'A');
INSERT INTO students VALUES ('Sudh', 35, 'A');
INSERT INTO students VALUES ('Jane', 24, 'C');
INSERT INTO students VALUES ('Aditi', 22, 'B');
INSERT INTO students VALUES ('Kritika', 38, 'C')
* sqlite:///test.db
1 rows affected.
1 rows affected.
1 rows affected.
1 rows affected.
1 rows affected.
1 rows affected.
1 rows affected.
Out[28]:
[]
In [29]:
# Selecting Entire students table
%sql SELECT * from students;
* sqlite:///test.db
Done.
Out[29]:
name age grade
Utkarsh 28 A
John 35 B
Krish 40 A
Sudh 35 A
Jane 24 C
Aditi 22 B
Kritika 38 C
In [30]:
# Example 2 : Show only name and age of students
%sql SELECT name, age FROM students;
* sqlite:///test.db
Done.
Out[30]:
name age
Utkarsh 28
John 35
Krish 40
Sudh 35
Jane 24
Aditi 22
Kritika 38
In [32]:
# Example 3: Selecting student with A Grade
%sql SELECT * FROM students WHERE grade='A';
* sqlite:///test.db
Done.
Out[32]:
Utkarsh 28 A
Krish 40 A
Sudh 35 A
Question 5
Answer:
1. Primary Key: In a database table, a primary key is a column or a set of columns that uniquely identifies each
row in the table. The primary key is used to ensure that each row in the table is unique, and it is often used
row in the table. The primary key is used to ensure that each row in the table is unique, and it is often used
as a reference by other tables. The primary key is also used to enforce data integrity, which means that it
ensures that there are no duplicate or null values in the key column(s).
Creating a table named "customers" with columns "customer_id", "first_name", "last_name", and "email". The
"customer_id" column is defined as the primary key.
In [33]:
%%sql
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100));
* sqlite:///test.db
Done.
Out[33]:
[]
In [34]:
%sql SELECT * FROM customers
* sqlite:///test.db
Done.
Out[34]:
1. Foreign Key: A foreign key is a column or a set of columns in a table that refers to the primary key of another
table. The foreign key is used to establish a relationship between two tables, and it ensures that the data in
the foreign key column(s) of one table matches the data in the primary key column(s) of the other table.
Creating a table named "orders" with columns "order_id", "order_date", "customer_id", and "total_amount". The
"order_id" column is defined as the primary key, and the "customer_id" column is defined as a foreign key that
references the "customer_id" column of the "customers" table. This establishes a relationship between the
"orders" table and the "customers" table. The "total_amount" column is used to store the total amount of the
order.
In [35]:
%%sql
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_date DATE,
customer_id INT,
total_amount DECIMAL(10,2),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id));
* sqlite:///test.db
Done.
Out[35]:
[]
In [36]:
%sql SELECT * FROM orders
* sqlite:///test.db
Done.
Out[36]:
order_id order_date customer_id total_amount
In [42]:
con.close()
Question 6
Answer:
In [37]:
Collecting mysql-connector-python
Downloading mysql_connector_python-8.0.32-cp310-cp310-win_amd64.whl (7.9 MB)
---------------------------------------- 0.0/7.9 MB ? eta -:--:--
---------------------------------------- 0.0/7.9 MB 960.0 kB/s eta 0:00:09
--------------------------------------- 0.1/7.9 MB 1.3 MB/s eta 0:00:06
- -------------------------------------- 0.2/7.9 MB 1.8 MB/s eta 0:00:05
-- ------------------------------------- 0.5/7.9 MB 2.9 MB/s eta 0:00:03
----- ---------------------------------- 1.0/7.9 MB 4.6 MB/s eta 0:00:02
--------- ------------------------------ 1.8/7.9 MB 6.4 MB/s eta 0:00:01
------------- -------------------------- 2.6/7.9 MB 8.0 MB/s eta 0:00:01
------------------ --------------------- 3.7/7.9 MB 9.9 MB/s eta 0:00:01
-------------------------- ------------- 5.3/7.9 MB 13.1 MB/s eta 0:00:01
------------------------------ --------- 6.1/7.9 MB 13.4 MB/s eta 0:00:01
--------------------------------- ------ 6.7/7.9 MB 13.0 MB/s eta 0:00:01
---------------------------------- ----- 6.7/7.9 MB 12.3 MB/s eta 0:00:01
------------------------------------ --- 7.3/7.9 MB 12.6 MB/s eta 0:00:01
--------------------------------------- 7.7/7.9 MB 12.0 MB/s eta 0:00:01
---------------------------------------- 7.9/7.9 MB 12.0 MB/s eta 0:00:00
Collecting protobuf<=3.20.3,>=3.11.0
Using cached protobuf-3.20.3-cp310-cp310-win_amd64.whl (904 kB)
Installing collected packages: protobuf, mysql-connector-python
Successfully installed mysql-connector-python-8.0.32 protobuf-3.20.3
In [41]:
import mysql.connector
# import mysql.connector
#create user 'user'@'%' identified by 'password'
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="Secure@1994"
)
print(mydb)
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES")
for x in mycursor:
print(x)
mydb.close()
1. In above code, we first establish a connection to a MySQL database using the mysql.connector.connect()
method, which takes the host, user, password as arguments.
2. Next, we create a cursor object using the cursor() method of the connection object. The cursor object allows
us to execute queries and fetch results.
3. We then execute a SQL query using the execute() method of the cursor object, which takes the SQL query as
an argument.
Question 7