How to store file paths on phpmyadmin? - mysql

I am using XAMPP and I want to store image paths on my phpmyadmin database and I saw here: Database design : preferred field length for file paths that the best options to store file paths should be VARCHAR(MAX) or NVARCHAR(MAX).
As I cannot set VARCHAR(MAX) or NVARCHAR(MAX) on my phpmyadmin,
How can I store these paths on my database properly? I mean, what is the best option to store them on my database?
Edit: I have seen the question that has been marked as possible duplicate. I know that I can "simulate" the VARCHAR(MAX) on phpmyadmin but, as it does not exist by default, it would be better to use another length instead of MAX?
I think that if you set a path of length 100 and you have your MAX as the limit is not worth so VARCHAR(suitable number) would be better than VARCHAR(MAX) (or at least I think it would be better). I am new with MySQL so I do not know about the entrails of it (by the moment).
I think it can be problems of performance or maybe memory but I cannot find anything on the official documentation so please if you find something related post here.
Is there a suitable length for store paths on phpmyadmin? A number of length that it is usually used (and why).
Thanks in advance!

the best way to do this is to create a VARCHAR(255) column.
255 because normally standard url should not exceed this size
If you can't do it directly you can try :
1) create your column as VARCHAR
2) in the structure part you can set the size that will be the max size of your column

Related

VARCHAR(MAX) vs TEXT vs .txt file for use in MySQL database

I tried to google this, but any results I found were related to importing data from a txt file to populate the database as opposed to storing data.
To me, it seems strange that the contents of a file should be stored in a database. We're working on building an eCommerce site, and each item has a description. I assumed the standard would be to store the description in a txt file and the URL in the database, and not to store the huge contents in the database to keep the file size low and speeds high. When you need to store images in a database, you reference it using a URL instead of storing all the pixel data - why would text be any different?
That's what I thought, but everyone seems to be arguing about VARCHAR vs TEXT, so what really is the best way to store text data up to 1000 characters or so?
Thanks!
Whether you store long text data or image data in a database or in external files has no right or wrong answer. There are pros and cons on both sides—despite many developers making unequivocal claims that you should store images outside the database.
Consider you might want the text data to:
Allow changes to be rolled back.
Support transaction isolation.
Enforce SQL access privileges to the data.
Be searchable in SQL when you create a fulltext index.
Support the NOT NULL constraint, so your text is required to contain something.
Automatically be included when you create a database backup (and the version of the text is the correct version, assuring consistency with other data).
Automatically transfer the text to replica database instances.
For all of the above, you would need the text to be stored in the database. If the text is outside the database, those features won't work.
With respect to the VARCHAR vs. TEXT, I can answer for MySQL (though you mentioned VARCHAR(MAX) so you might be referring to Microsoft SQL Server).
In MySQL, both VARCHAR and TEXT max out at 64KB in bytes. If you use
a multibyte character set, the max number of characters is lower.
Both VARCHAR and TEXT have a character set and collation.
VARCHAR allows a DEFAULT, but TEXT does not.
Internally the InnoDB storage engine, VARCHAR and TEXT are stored identically (as well as VARBINARY and BLOB and all their cousins). See https://www.percona.com/blog/2010/02/09/blob-storage-in-innodb/

Why does imported MySQL dump show some column value as BLOB?

I imported a MySQL table dump from my server to my local system. I am using phpMyAdmin to view my local MySQL databases. The column that held emails shows as BLOB - instead of actual email.
When I press on "edit" it shows correct emails, but not on listing.
I am totally confused as why this happens. Can anyone suggest solution?
BLOB data type is meant to store arbitrary binary data (Binary Large OBject = BLOB), for example an image or another document. It does not make sense to show the value as-is. It would look the same as viewing an image in a text editor.
You have several options, depending on the version of phpMyAdmin you are using - which I unfortunately do not know.
Make phpMyAdmin show BLOB values by default.
Show BLOB values for a complete result set.
These two possibilities are covered by an already asked question.
But basically, this is just fighting symptoms instead of curing the disease. The question is: Why did you chose the email field to be a BLOB? Basically, a VARCHAR is enough. I do not know the MySQL version you are running, but since MySQL 5.0.3, a VARCHAR can be as large as 65k byte.
ALTER TABLE
`table`
CHANGE
`email` `email` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;
The query above changes the field email to a VARCHAR(255). Pay attention to the length you like to use, and to the character set and collation. But UTF-8 should be very fine in this case.
It shows the text of "Blob" as a placeholder as the blob datatype (which is the datatype that your emails are stored in) is vastly variable in size. For large Blob sizes, if the data were displayed, it could take up 100s + screens to display.

When to use the BIN flag in MySQL Workbench?

I came to know that MySQL Workbench allows a BIN flag on a column for storing data as binary strings here.
If that's the case, what's the difference between BIN flag and the datatype BINARY(n)? Besides, it even allows me to set this flag on columns with datatypes VARCHAR(n), CHAR(n), etc. which seems to be conflicting.
When should I exactly use this flag?
You can see the manual entry here. The most obvious use for this is where you have an ID rather than text where you may want an ID of, say, "A107652B" to be distinct from "a107652b". Most textual types would treat these as the same.

How can I insert a file into a table in SQL?

I haven't seen any data type that can store a file in SQL. Is there something like that? What I'm particularly talking about is that I want to insert into my table a source code. What is the best method to do it? It can be either stored in my database as a nicely formatted text, or better (what I actually want) to store it as a single file. Please note that I'm using MySQL.
It is best not to store a file in your SQL database but to store a path to the file in the server or any other UNC path that your application can retrieve by itself and do with it what ever is unnecessary.
see this: https://softwareengineering.stackexchange.com/questions/150669/is-it-a-bad-practice-to-store-large-files-10-mb-in-a-database
and this:
Better way to store large files in a MySQL database?
and if you still want to store the file on the DB.. here is an example:
http://mirificampress.com/permalink/saving_a_file_into_mysql
If you can serialized the file you can store it as binary and then deserialize when needed
http://dev.mysql.com/doc/refman/5.0/en/binary-varbinary.html
You can also use a BLOB (http://dev.mysql.com/doc/refman/5.0/en/blob.html) which has some differences. Normally I just store the file in the filesystem and a pointer in the DB, which makes serving it back via something like HTTP a bit easier and doesn't bloat up the Database.
Storing the file in a table only makes sense if you need to do searches in that code. In other cases, you should only store a file's URL.
If you want to store a text file, use the TEXT datatype. Since it is a source code, you may consider using the ASCII character set to save space - but be aware that this will cause character set conversions during your queries, and this affects performances. Also, if it is ASCII you can use REGEXP for searches (that operator doesnt work with multi-byte charsets).
To load the file, if the file is on the same server as MySQL, you can use the FILE() function within an INSERT.

MySQL 5.5 LONGBLOB Column Data too long for column 'x' at row 1

I have a column in my MySQL database that stores images as a byte array.
I am trying to update a row to insert a new image. The new image is 163K, and when I convert it to a byte[], the number of elements in the array is 167092. When I run the stored procedure that does the update, I get an error "Data too long for column 'x' at row 1. I already have an existing image in the database that has 8844 byte[] elements when converted.
The column datatype is LONGBLOB. From my understanding, I should have appox 4Gb to work with.
I have tried updating my my.ini file to make the MAX_ALLOWED_PACKETS=16M and I even tried 100M.
I am using the MySQL .NET Connector libraries to execute my stored procedures.
Does anyone have any ideas on how to fix this issue? I know I could store the image paths instead of storing the images directly into the database. But I would like to know how to solve my current issue and still store the images in the database first before trying to change my approach.
I've had exactly the same problem...
In my case I was passing the LONGBLOB via a TEXT parameter since I wanted to use CONCAT inside the stored procedure in order to create dynamic SQL.
The solution was simply to change TEXT into LONGTEXT. That's it :)
That really took some time to figure out...
Hope I could help even after almost three years.