r/AskComputerScience • u/BrookieYT • Nov 29 '20
Are databases basically data stored in .txt files?
So I was learning File Handling the other day, and a question sparked into my mind. Is the things that we do in File Handling basically the same thing as storing data in databases? I know I am asking ahead of my knowledge and have a long way to go before I study databases, but are these data basically stored in .txt files. Also do all these big companies just simply store their data in .txt files or do they have a different way of storing their data?
Sorry for my noob question! Also I am asking in terms of C++, if that's something required for my question. Thanks for your time!!!
5
u/claytonkb Nov 29 '20 edited Nov 29 '20
Is the things that we do in File Handling basically the same thing as storing data in databases?
There are in-memory databases, so file handling is a separate topic from databases themselves.
I know I am asking ahead of my knowledge and have a long way to go before I study databases, but are these data basically stored in .txt files.
The short answer is No. The slightly more correct answer is that it depends on how you choose to think about text, binary data, databases, and so on.
For example, there is nothing stopping you from creating a sequential database of random records and writing this to file, in ASCII (.txt) format. The problem, however, is that this is like storing a library catalog in random order. Every time you want to access a record from your database, you will need to start at the beginning of the drawer (file) and thumb through each record until you find the one you're looking for. If you have N records, this is O(N/2) average time-complexity. If you are performing M queries, the total time-complexity is O(MxN). If you only have 5 records and you're only performing 10 queries, ever, then maybe that's acceptable. But we can do a lot better than that, so no one does it this way except perhaps in some throwaway script used to perform a one-off task.
The first improvement that might occur to us is to sort the records by some key. This helps access time (since we can use binary search to find a record in O(log(N)) steps) but it slows down write time (since we have to rearrange the whole database every time a record is inserted). A better solution is to store the keys as an array of record indices, and then just sort the key array. That way, we can append records in random order, then quickly insert the key into the key array. Lookup is just as fast as before.
While this solution is dramatically faster than the very bad solution we started with, it is (perhaps surprisingly) very far from the best we can do. We can use a hash-table to store the keys and bring the time-complexity of record lookup and insert down to O(1). At this point, we've achieved a "reasonably usable" database, even if it is just a humble concatenation of string data stored in a flat ASCII file. But we are still very far from anything resembling a commercial database and the topic becomes non-trivial from this point on.
Also do all these big companies just simply store their data in .txt files or do they have a different way of storing their data?
I don't think there are any commercial database that store data in ASCII. One problem with ASCII is that you have to encode binary data before writing it out, which blows up the size of the file considerably. Using the best encoding (Base-85), you're talking about a 67% increase in file size for every binary file stored in ASCII. That's terrible, so no one is going to do that for nothing in return. Just open the file in binary mode instead of text mode. Now you can write binary data or ASCII data.
You are right to suspect that there is a link between file-systems and databases, however. A file-system is a hierarchical, tree-like data structure that stores information about its contents in a directory (index). A database can be arranged in a large number of ways but it, too, will have at least one index (but it can have many, depending on how it is structured, how it is accessed, and so on).
SQLite is an open-source database so, unlike proprietary, closed-source databases, you can read all about how it works internally. I recommend you check out the documentation and other online resources that explain the basics of SQLite.
3
u/CaffeinatedMancubus Nov 29 '20
Files are simply sequences of bytes stored in a storage media. When you say .txt files, I take it that you are referring to files that can be opened in a text editor and contain human readable text. These are simply files that happen to store bytes that encode text, but not all files need to follow this. When you don't need your file to be opened in a text editor and be readable by humans, it can be more efficient to store data in a format that doesn't encode text. Both text files and binary files are ultimately just sequences of bytes stored on disk, but the difference is in what the bytes represent. (look up "ascii" and "unicode" if you're more interested in learning how text files work).
Databases are softwares that accept instructions in a language such as SQL and allow you to store and retrieve information. This information could be text, or images or something else, and it may be stored as-is or in some more more efficient, compressed format. That is upto the implementation of that particular database. The software may store that in a single file, or divide it into multiple files, or even store it in RAM.
You are right to a some extent, however - most databases do store the data in a file ultimately, since that's basically how you store stuff that you want to persist after a reboot. But these files are almost never "text" files, since that won't be very efficient. The database software needs to be able to jump directly to the relevant place in the file without having to go through it sequentially. You are also right to think that the database software does file handling in a way similar to what you learnt, since when you get down to the low level implementation, it ultimately all boils down to asking the OS to open a file identified by a particular path on the disk and returning bytes at a particular offset from the start.
3
Nov 29 '20
For the sake of your understanding, yes, but in reality, no.
The difference is that there is a lot more involved than just reading a file (for a sophisticated database technology), but in layman’s terms it is essentially just reading from files on the disk. It’s just not usually readable text files, they are in binary. They also use certain algorithms to read and search that file, it also allows for multiple people to access that file at the “same” time.
On top of all that, it depends what kind of database it is.
But for your basic understanding, you could make a database that all works from a JSON or text file in a format that allows your program to access it and that would technically be a database.
But if you’re creating an application for a production environment where users need to rely on that data and need to access, search and modify it quickly, you would not want to create your own because it would take years to create something that works as well as SQL or NoSQL or any of the many tried and true database technologies.
2
35
Nov 29 '20
[removed] — view removed comment
3
u/shiningmatcha Nov 29 '20
How is SQL stored?
29
u/Rangsk Nov 29 '20
SQL is a language, not an implementation. The comment you replied to gave two examples of database implementations (PostgreSQL and SQLite) which support SQL queries, but there are many more.
15
u/teraflop Nov 29 '20
SQL is a language for describing queries -- that is, computations or updates -- on the data in a relational database. It has nothing to do with the format that is used to store the data.
(You can export a database as SQL, by generating a text file that contains the CREATE TABLE and INSERT statements that would allow it to be recreated. But that's not how the database itself is normally stored, because it doesn't allow for efficient modification or retrieval. It's more useful for backups and debugging.)
1
u/shiningmatcha Nov 29 '20
Then how is a database normally stored?
12
u/teraflop Nov 29 '20
It's complicated, but this comment has a good summary and links to some detailed descriptions.
3
38
u/CavemanKnuckles Nov 29 '20
A couple of things to note:
there's no such thing as a .txt file. As in, there is no recognized file format for .txt files, other than the bytes only containing human readable ASCII for the most part (even that is up for debate, if you want an umlaut or emojis in your text file).
not all database persist to disk. One example is redis, an in memory database.
not all databases persist to a single file
However, there's the realization that (most) databases are simply applications, and their data storage is dependent on the operating system's file system. That's a pretty interesting thing to think about!