Suppose we decided to implement a site with ads and want to allow our users to create ads. We already know how to output a page with a form to add an ad. We need to generate HTML using the templating tool. But what do we do after the form data arrives at the server? Where do we store them? We'll get to the bottom of this question in this lesson.
What is a DBMS?
The easiest way to look at it is by using files as an example. We can write all events to a file and read from it when output. This approach has many drawbacks and is very inconvenient to work with. The correct way to work with data is with a database. It is stored in files too, but this process is 100% controlled by a DBMS (database management system). It is the one you need to install before you start work.
The most common systems on the web are PostgreSQL and MySQL. We won't discuss it here because configuring and installing a DBMS isn't as trivial as installing an interpreter. You can find many ready-made instructions on DBMS installation on your operating system.
DBMSs start as separate programs and have a life of their own. These systems can serve many different databases. Usually, one site uses one database, but we can store many databases from several sites in one DBMS. You can interact with a particular database in two ways:
- Run the DBMS command shell, which allows you to interactively work with commands, write and read data, add a user to the system, and so on. When connecting or after connecting, you need to select the database you want to interact with:
- Connect to the DBMS from your program using a driver — a library that communicates with the DBMS. It has a relatively simple interface for performing queries and getting results. Below we'll see how this approach works in practice.
Relationships within the database
PostgreSQL and MySQL belong to the class of relational databases because these DBMSs work based on relational algebra. Without going into theory, we'll say that the data in relational databases store their data in the form of tables. This format is familiar to everyone who has used Microsoft Excel or Google Sheets.
Each table in such a database has its name and a set of named columns. We can refer to columns in a database as fields. For example, we can create a table with ads, give it the name
ads and place the following data:
|1||132453||Selling a car|
|2||342341||Buying a yacht|
|3||908324||Renting a tent|
Each row in the table is independent and represents a complete data set. It is a single declaration in our case. Programmers commonly refer to lines as records.