- What is the difference between SQL and NoSQL?
- What is referencing and what is embedding in MongoDB?
- Why should we embed more than referencing when we can in MongoDB?
- When should we prefer referencing over nesting in MongoDB?
- What are ORMs? Why we use them? Give an example of an SQL request with and without using ORM.
- What is the difference between a table and a collection?
Some helpful resources:
team members: jafar bino, Dana Maraqa , lin Daaboul, Ahmed shalash
Q1:
SQL databases are relational, and NoSQL databases are non-relational.
SQL databases use structured query language (SQL) and have a predefined schema. NoSQL databases have dynamic schemas for unstructured data.
SQL databases are vertically scalable, while NoSQL databases are horizontally scalable.
SQL databases are table-based, while NoSQL databases are document, key-value, graph, or wide-column stores.
SQL databases are better for multi-row transactions, while NoSQL is better for unstructured data like documents or JSON.
Q2:
Referencing in MongoDB : is when we use a unique Identifier -to reference once document in another separate document, the reference is then used to $lookup the data when needed , usually used in frequently updated DB or when the data is considered large
Embedding in MongDB : mainly means that within the same document a reference is needed it could be done by adding a sub-document or document inside a parent document.
Embedding is usually used when the data doesn't change frequently and a document needs to be fetched on one go
Q3:
embedding allows for faster reads since all the relevant data is stored in one document. Referencing needs additional queries to get related data.
Q4
referencing should be preferred over nesting when the related data can grow large and lead to overly large documents that exceed the 16MB document size limit or subject to frequent updates also when the data needs to be accessed and managed independently of the parent doc
Q5:
ORMs (Object-Relational Mappers) it is a technique for converting data between database (which use tables and SQL queries) and other oop lang (like Python or Java).in simle words it connernt between these types of programing langueges.
why to use it ?
Simplicity: ORMs let you work with databases using code that's easier to understand, write, and maintain.
Avoidance of Raw SQL: Instead of writing SQL queries directly, you can use the programming language's syntax to interact with the database.
ex:
Fetch user by ID (Raw SQL):
String sqlQuery = "SELECT * FROM users WHERE id = 1;";
with ORM:
@entity
@table(name = "users")
public class User {
@id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(String[] args) {
// Hibernate setup
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(User.class)
.buildSessionFactory();
}
Q6:
Table (Relational Database):
Structure: A table is a structured set of data organized in rows and columns.
Schema: In relational databases, tables have a predefined schema, meaning the structure, data types, and constraints for each column are fixed and defined when creating the table.
Relationships: Relational databases emphasize relationships between tables. Data integrity is maintained using foreign key constraints to establish connections between different tables.
Collection (NoSQL Database):
Structure: A collection, in the context of NoSQL databases, is a container for storing data. It can store various forms of data structures such as documents, key-value pairs, or wide-column structures.
Schema: NoSQL databases often offer schema flexibility. Some variations allow collections to store different types of documents or entities with different structures within the same collection.
Relationships: While some NoSQL databases support relationships, they often handle relationships differently compared to relational databases. Some NoSQL databases emphasize denormalized data storage, where related data can be embedded within a single document or stored separately without explicit foreign keys.
Differences:
Schema Flexibility: Tables in relational databases have a fixed schema, whereas collections in NoSQL databases can have a flexible or even schema-less structure.
Data Structure: Tables consist of rows and columns, whereas collections in NoSQL databases can contain various forms of data structures based on the database type.
Relationship Handling: Relational databases use explicit relationships between tables (foreign keys), while NoSQL databases handle relationships differently, often by embedding related data within documents or using alternative mechanisms.