Look into flask-boilerplate's wiki for more Flask/SQLAlchemy specific database interactions as well as database troubleshooting.
What is a database?
- A database is a collection of organized information. It stores data in some structured format and supports some sort of interface to access and mutate your data (some flavor of SQL or NoSQL). A database generally has many different tables. (ex. A database might have a Users table and an Events table)
- A table is a group of entries. (ex. a Users table would store all users)
- Each table should have a different set of columns.
- Tables are a way of encoding logical separation of entries in your database.
- A row is an entry in the table of a database. (ex. a row in a Users table would be a specific user and all their attributes)
- A column can be thought of as a property/attribute of an entry. (ex. a User would have columns such as age, profile picture, etc.)
Download TablePlus for Mac or SQLelectron for a gui client view of SQL databases.
SQL and NoSQL
SQL
- First, we need to know what SQL, or Structured Query Language is. As the name suggests, it's a language that allows us to communicate with a database and perform tons of operations (select, update, delete, insert, create a record, alter a record, create a table, alter a table, drop a table, etc.)
- MySQL, SQLite, and PostgreSQL are all different implementations of the same SQL way of interacting with a database. A SQL Database is a database that accepts SQL ****queries.
- SQL ****at the core, requires that every row in a table must store the exact same attributes (have the same columns). SQL tables can truly be compared to a table in a spreadsheet. Each column must contain values of the same data type, and only supports certain primitive data types (no lists, and no nesting
- These requirements may seem very restrictive, but they make SQL databases extremely fast, and give developers a clear set of requirements they can expect from their data.
NoSQL
- While most SQL implementations have relatively similar syntax and support the same rules in terms of structuring data, NoSQL implementations can widely vary.
- MongoDB for example can be thought of as a blob of json. While there are schemas for different types of documents (rows), a document may not contain all the rows, or may contain more rows than the schema defines. The attributes can be nested objects or lists. This gives the developer more freedom, but can come at the cost of performance, especially if not used well.
- Other examples are neo4j, a graph database, that can be thought of sort of as a network.
What is SQLAlchemy?
- SQLAlchemy uses an ORM, or Object Relational Mapping, which allows us to think about database entries as object instances, columns as properties of objects, and tables as data structures that store object instances. Plain SQL queries tend to look something like
SELECT *
FROM User
WHERE id > 5;