CREATE SCHEMA IF NOT EXISTS `utftest` DEFAULT CHARACTER SET utf16;
CREATE TABLE IF NOT EXISTS `metadata_labels` (`metadata_id` INT NOT NULL , `label` VARCHAR(256) NOT NULL , PRIMARY KEY (`metadata_id`, `label`));
however I get the following error msg:
Specified key was too long; max key length is 767 bytes
Please advise
UTF 16 uses 32 bits per character (4 bytes) in MySQL. 4 x 256 > 767.
If possible, I would recommend using something other than UTF16 VARCHAR for your key.
In UTF8, it would require 3 x 256 + 4 = 772 bytes. UTF16 would take another 25% more.
You shouldn't use a primary key that's so wide; for an index to be efficient, the storage for each index should be kept to a minimum.
If you need to prevent duplicates, I would recommend adding a calculated field that contains a hash of the contents (e.g. sha1) and create a unique constraint on that instead.
Alternatively, use latin1 as the character encoding for the label field to reduce the number of bytes to 256 + 4 = 300.
If Unicode is a must and hashes are out of the picture you should reduce the column to either UTF8 (250 chars) or UTF16 (190 chars)
Related
I created this table:
CREATE TABLE Hospital_MedicalRecord(
recNo CHAR(5),
patient CHAR(9),
doctor CHAR(9),
enteredOn DATETIME NOT NULL,
diagnosis LONGTEXT NOT NULL,
treatment TEXT(1000),
PRIMARY KEY (recNo, patient),
CONSTRAINT FK_patient FOREIGN KEY (patient) REFERENCES Hospital_Patient(NINumber),
CONSTRAINT FK_doctor FOREIGN KEY (doctor) REFERENCES Hospital_Doctor(NINumber)
ON DELETE CASCADE
);
How can one make diagnosis contain some long text but never more than 2^24 bytes? I've looked into LONGTEXT but I couldn't find a way to limit it since it can go up to 2^34 I believe?
Use MEDIUMTEXT.
https://dev.mysql.com/doc/refman/8.0/en/string-type-overview.html
MEDIUMTEXT [CHARACTER SET charset_name] [COLLATE collation_name]
A TEXT column with a maximum length of 16,777,215 (224 − 1) characters. The effective maximum length is less if the value contains multibyte characters. Each MEDIUMTEXT value is stored using a 3-byte length prefix that indicates the number of bytes in the value.
The wording is a little strange. The length limit is really on bytes, not characters.
LONGTEXT is limited to the maximum that the filesystem permits for a 32 bit system it is limited to 2^32 that is approximately 4.000.000.000 characters (if you do not use multi-byte characters), I have not calculated exactly.
Then you have MEDIUMTEXT with 2^24 characters .. around 16.000.000 characters. TEXT has a limit at 2^16 character that is much smaller, about 64.000 characters (if you do not have multibyte).
What You need is called MEDIUMTEXT
I haven't coded PHP for a while now. My website, that I wrote in PHP some time ago, that is running from a shared hosting company, started giving me the following error in the log when the script was executing the following query via mysql_query:
CREATE TABLE tbl (idx TINYTEXT, PRIMARY KEY(idx(255)))
and the error itself:
MySQL #1071 'Specified key was too long; max key length is 255 bytes'
It worked for many years. So what's the problem there now?
If the maximum key length is 255 bytes and you are using UTF8 encoding, then each character can be up to 4 bytes.
Hence, the maximum is 63 bytes. This works:
CREATE TABLE tbl (
idx TINYTEXT,
PRIMARY KEY (idx(63))
);
You can query the catalog in particular the information_schema.character_sets table to get the maximum length a character can have in a given character set. From that you can calculate the maximum number of characters not exceeding 255 bytes.
SELECT floor(255 / maxlen) maxcharlen
FROM information_schema.character_sets
WHERE character_set_name = ##character_set_database;
You can then amend your CREATE statement based on that value.
I have a problem to create i18n table for CakePHP 3 Translate Behavior. So I have my database in phpmyadmin and when I want to execute this piece of code from the official cookbook :
CREATE TABLE i18n (
id int NOT NULL auto_increment,
locale varchar(6) NOT NULL,
model varchar(255) NOT NULL,
foreign_key int(10) NOT NULL,
field varchar(255) NOT NULL,
content text,
PRIMARY KEY (id),
UNIQUE INDEX I18N_LOCALE_FIELD(locale, model, foreign_key, field),
INDEX I18N_FIELD(model, foreign_key, field)
);
PhpMyAdmin say :
1071 - Specified key was too long; max key length is 767 bytes
I'm in uft8_unicode_ci. Should I go for utf8_general_ci?
Thanks for your help.
There is no difference in size requirements between utf8_unicode and utf8_general, they only differ with regards to sorting.
By default the index (key prefix) limit is 767 bytes for InnoDB tables (and 1000 bytes for MyISAM), if applicable enable the innodb_large_prefix option (it is enabled by default as of MySQL 5.7) which raises the limit to 3072 bytes, or make the VARCHAR columns smaller, and/or change their collation, the locale column (which holds ISO locale/country codes) surely doesn't use unicode characters, and chances are that your model and column/field names also only use ASCII characters, and that their names are way below 255 characters in length.
With an ASCII collation the VARCHAR columns require only 1 byte per char, unlike with UTF-8, which can require up to 3 bytes (or 4 bytes for the mb4 variants), which alone already causes the index size limit to be exceeded (3 * 255 * 2 = 1530).
See also
MySQL 5.7 Manual > Character Sets and Collations
MySQL 5.7 Manual > Limits on InnoDB Tables > Maximums and Minimums
MySQL 5.7 Manual > InnoDB Startup Options and System Variables > innodb_large_prefix
I have limited my request with :
model varchar(85) NOT NULL,
field varchar(85) NOT NULL,
model and field at 85, I think it's enought, I mysql accept it.
Hope that will help someone.
This question already has answers here:
#1071 - Specified key was too long; max key length is 1000 bytes
(13 answers)
Closed 6 years ago.
I get following error when I Import the Sqlbackup i have:
SQL-Befehl:
CREATE TABLE jos_hikashop_zone_link (
zone_parent_namekey VARCHAR(255) NOT NULL,
zone_child_namekey VARCHAR(255) NOT NULL,
PRIMARY KEY (zone_parent_namekey, zone_child_namekey)
)
ENGINE = MYISAM
AVG_ROW_LENGTH = 49
CHARACTER SET utf8
COLLATE utf8_general_ci
MySQL meldet: Dokumentation
1071 - Specified key was too long; max key length is 1000 bytes
I have tried to
"set GLOBAL storage_engine='InnoDb'" before I Import the file.
I can`t get a new SQLImportfile.
I am using XAMPP on a local machine.
Because your primary key is a multi-column primary key on both UTF8 VARCHAR(255) columns, the index size is the size of both columns, added together.
Indexes on UTF8 columns automatically allocate all possible space, where each character could take up to 3 bytes. Therefore, your index size is 255 characters * 3 bytes * 2 columns = 1530 bytes.
MyISAM has a limit of 1000 bytes for indexes. InnoDB has an even smaller limit (767 bytes) unless you're on MySQL 5.7.7+, in which case the limit is 3072 bytes by default.
Try to reduce your index size. Typically, the smaller your index size, the better your seek performance will be.
By definition:
VARCHAR: The range of Length is 1 to 255 characters. VARCHAR values are sorted and compared in case-insensitive fashion unless the BINARY keyword is given. x+1 bytes
TINYBLOB, TINYTEXT: A BLOB or TEXT column with a maximum length of 255 (2^8 - 1) characters x+1 bytes
So based on this, I creaate the following table:
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255),
`lastname` tinytext,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
Or is it better to create a varchar or tinytext and why?
Is it the same for:
VARCHAR: The range of Length is > 255 characters. VARCHAR values are sorted and compared in case-insensitive fashion unless the BINARY keyword is given. x+2 bytes
BLOB, TEXT A BLOB or TEXT column with a maximum length of 65535 (2^16 - 1) characters x+2 bytes
In this case varchar is better.
Note that varchar can be from 1 to 65535 chars.
Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions. The effective maximum length of a VARCHAR in MySQL 5.0.3 and later is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. See Section E.7.4, “Table Column-Count and Row-Size Limits”.
Blobs are saved in a separate section of the file.
They require an extra fileread to include in the data.
For this reason varchar is fetched much faster.
If you have a large blob that you access infrequently, than a blob makes more sense.
Storing the blob data in a separate (part of the) file allows your core data file to be smaller and thus be fetched quicker.