Mango db create a table 1
23.10.24
Here's how you can create a SQL table based on the given data, and I'll provide the expected output.
SQL Code to Create the Table and Insert Data:
Ex.Code
--------------------------------------------------------------------------------------
-- Drop the table if it already exists
DROP TABLE IF EXISTS students_info;
-- Create the table
CREATE TABLE students_info (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER NOT NULL,
country TEXT NOT NULL
);
-- Insert data into the table
INSERT INTO students_info (id, name, age, country) VALUES (1, 'Vidya Sagar', 30, 'India');
INSERT INTO students_info (id, name, age, country) VALUES (2, 'Pinky', 28, 'Australia');
-- Select all data from the table
SELECT * FROM students_info;
--------------------------------------------------------------------------------------
Explanation of SQL Code:
- DROP TABLE IF EXISTS: Ensures that the table is deleted if it exists, to avoid conflicts.
- CREATE TABLE: Creates a new table
students_infowith four columns:id,name,age, andcountry. - INSERT INTO: Adds the specified data (names, ages, and countries) into the table.
- SELECT: Retrieves and displays all data from the
students_infotable.
Expected Final Output:
This output shows the table with two entries for Vidya Sagar and Pinky, along with their corresponding age and country details. Let me know if you need further assistance!
To create the same table-like structure in MongoDB, we need to understand that MongoDB uses collections and documents instead of tables and rows (as in SQL). In MongoDB, data is stored in a JSON-like format called BSON (Binary JSON).
Here’s how you can create a similar structure in MongoDB:
Steps to Create and Insert Data in MongoDB:
Install MongoDB:
- Ensure MongoDB is installed on your system. You can download it from mongodb.com.
Open MongoDB Shell (or use MongoDB Compass):
- You can interact with MongoDB either via MongoDB Shell or using a GUI tool like MongoDB Compass.
Create a Database:
- In MongoDB, you first switch to a database. If the database does not exist, MongoDB will create it.
Ex.Code
--------------------------------------------------------------------------------------
use studentsDatabase
--------------------------------------------------------------------------------------
4. Create a Collection:
- Collections in MongoDB are like tables in SQL. We will create a collection named
studentsInfo.
Ex.Code
--------------------------------------------------------------------------------------
db.createCollection("studentsInfo")
--------------------------------------------------------------------------------------
5. Insert Data into the Collection:
- Instead of inserting rows, you insert documents (JSON-like objects) into a collection.
Ex.Code
--------------------------------------------------------------------------------------
db.studentsInfo.insertMany([
{ id: 1, name: "Vidya Sagar", age: 30, country: "India" },
{ id: 2, name: "Pinky", age: 28, country: "Australia" }
])
--------------------------------------------------------------------------------------
6. Retrieve the Data:
- To retrieve all the data from the
studentsInfocollection, use the following command:
Ex.Code
--------------------------------------------------------------------------------------
db.studentsInfo.find().pretty()
--------------------------------------------------------------------------------------
Explanation of MongoDB Commands:
use studentsDatabase: Switches to thestudentsDatabasedatabase. If it doesn’t exist, MongoDB will create it.db.createCollection("studentsInfo"): Creates a new collection calledstudentsInfo. In MongoDB, you don't necessarily need to create a collection explicitly; it will be created when you insert data.db.studentsInfo.insertMany([...]): Inserts multiple documents (similar to rows) into thestudentsInfocollection.db.studentsInfo.find(): Retrieves all documents from thestudentsInfocollection..pretty(): Formats the output to make it easier to read.
Expected Output in MongoDB:
The inserted data in MongoDB will look like this when you run the find() command:
Ex.Code
--------------------------------------------------------------------------------------
[
{
"_id": ObjectId("..."), // MongoDB automatically adds a unique _id field
"id": 1,
"name": "Vidya Sagar",
"age": 30,
"country": "India"
},
{
"_id": ObjectId("..."),
"id": 2,
"name": "Pinky",
"age": 28,
"country": "Australia"
}
]
--------------------------------------------------------------------------------------
Key Differences from SQL:
- No Tables: MongoDB stores data in collections instead of tables.
- No Schema: MongoDB is schema-less, meaning you don’t need to define the structure beforehand.
- Documents (BSON): Data is stored as documents (like JSON), making it flexible to handle different types of data.
Let me know if you need more explanation or help with the commands!
Ex.Code
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
Ex.Code
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
Ex.Code
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
Ex.Code
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
Ex.Code
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------

Comments
Post a Comment