Puru
Puru

Reputation: 9083

File handling in MySQL database?

I don't know this concept is there or not. Can we store the Files and related information in MySQL database ? If the answer is Yes then how ?

Upvotes: 0

Views: 2351

Answers (3)

Your Common Sense
Your Common Sense

Reputation: 157895

If the answer is Yes then why?
Filesystem itself is already a database. A very good one. To deal with files, keyed by filenames.
While relational databases, like mysql, intended to deal with data contents - order it, filter it, make relations with it. Nothing of this can be done to binary file content. You have to have strong reasons to put files into relational database. Not "why not?" one.

Upvotes: 0

Brenton Alker
Brenton Alker

Reputation: 9072

Yes, but make sure you really want to.

MySQL has a "BLOB" (and related) column type, which can be used for storing arbitrary binary data, much the same way you would store a string (it is just a string of bits instead of a string a characters) value.

The problem is serving files from the database is quite inefficient (the filesystem is much faster), as well as putting unnecessary strain on your DB. So if you can, you are often better of using real files and only storing metadata in the database. Or, at a minimum caching the file to the filesystem.

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838246

You can store the contents of a file in a BLOB but it is often better to just store the location of the file as a varchar and store the actual contents of the file in the filesystem.

Upvotes: 1

Related Questions