- 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:
Room 10: Farah ,Banan , Abdullah, Dana
1-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
2-Embedding involves nesting one document within another, it can simplify queries and improve read performance, especially for cases
where related data is always accessed together. It eliminates the need for additional queries and provides better performance.
Referencing involves storing references to other documents within a document, it allows for more flexible and scalable data management
as it avoids duplication of data. It also enables better performance for updating documents as changes in referenced documents don't
require updating every document that has a reference to them.
3-referencing and embedding are two different strategies for modeling relationships between documents.
Embedded documents are an efficient and clean way to store related data, especially data that’s regularly accessed together. In general,
when designing schemas for MongoDB, you should prefer embedding by default, and use references and application-side or database-side
joins only when they’re worthwhile.
4-The related data is small and won't change much.
There's a one-to-few or one-to-many relationship.
You want faster read performance, as you can retrieve all data in one query.
5-An ORM is simply a library that makes it easier to communicate with a database in code. It is basically a technique to query or perform
CRUD (Create, Read, Update, Delete) operations to the database, mainly RDBMS (Relational Databases), using an object-oriented paradigm.
With the help of ORM, you don`t actually need to use SQL at all. Why we use ORM:
Abstraction: ORM abstracts away the complexities of SQL queries and database interactions, allowing developers to work with objects in
their code instead of raw database tables.
Portability: ORM makes it easier to switch between different database systems since the underlying SQL is abstracted by the framework.
Productivity: ORM can reduce the amount of boilerplate code needed for database operations, making development faster and more
maintainable.
Assuming you have a table named 'users'
SELECT id, username, email FROM users WHERE age > 25; # Assuming you have a User class mapped to a 'users' table
from sqlalchemy import create_engine, Column, Integer, String, Sequence
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
class User(Base):
tablename = 'users'
id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
username = Column(String(50))
email = Column(String(50))
age = Column(Integer)
Creating an SQLalchemy engine and session
engine = create_engine('sqlite:///:memory:')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
Using ORM to query the database
users_over_25 = session.query(User.id, User.username, User.email).filter(User.age > 25).all()
6-A table and a collection are both used to organize and store data, the main difference is that a table is structured and schema-driven data
storage unit used in SQL databases, while a collection is a more flexible and schema-less data container used in NoSQL databases.