MySQL's support of the BINARY and VARBINARY data type is good, and the BINARY and VARBINARY data types are good things. And now the details. What applies here for MySQL applies for MariaDB as well.
Who supports VARBINARY
There's an SQL:2008 standard optional feature T021 "BINARY and VARBINARY data types". Our book has a bigger description, but here is one that is more up to date:
DBMS | Standard-ish? | Maximum length |
---|---|---|
DB2 for LUW | No. Try CHAR FOR BIT DATA. | 254 for fixed-length, 32672 for variable-length |
DB2 for z/OS | Yes. | 255 for fixed-length, 32704 for variable-length |
Informix | No. Try CHAR. | 32767 |
MySQL | Yes. | constrained by maximum row length = 65535 |
Oracle | No. Try RAW. | 2000 sometimes; 32767 sometimes |
PostgreSQL | No. Try BYTEA. | theoretically 2**32 - 1 |
SQL Server | Yes. | 8000 for fixed-length. 2**31 -1 for variable length |
Standard Conformance
Provided that sql_mode=strict_all_tables, MySQL does the right (standard) thing most of the time, with two slight exceptions and one exception that isn't there.
The first exception:
CREATE TABLE t1 (s1 VARBINARY(2));INSERT INTO t1 VALUES (X'010200');
... This causes an error. MySQL is non-conformant. If one tries to store three bytes into a two-byte target, but the third byte is X'00', there should be no error.
The second exception:
SET sql_mode='traditional,pipes_as_concat';CREATE TABLE t2 (s1 VARBINARY(50000));CREATE TABLE t3 AS SELECT s1 || s1 FROM t2;
... This does not cause an error. MySQL is non-conformant. If a concatenation results in a value that's longer than the maximum length of VARBINARY, which is less than 65535, then there should be an error.
The exception that isn't there:
The MySQL manual makesan odd claimthat, for certain cases when there's a UNIQUE index, "For example, if a table contains 'a', an attempt to store 'a/0' causes a duplicate-key error." Ignore the manual. Attempting to insert 'a/0' will only cause a duplicate-key error if the table's unique-key column contains 'a/0'.
The poemAntigonishdesccribed a similar case:
Yesterday, upon the stair,
I met a man who wasn't there.
He wasn't there again today,
I wish, I wish he'd go away..."
The BINARY trap
Since BINARY columns are fixed-length, there has to be a padding rule. For example, suppose somebody enters zero bytes into a BINARY(2) target:
CREATE TABLE t4 (s1 BINARY(2));INSERT INTO t4 VALUES (X'');SELECT HEX(s1) FROM t4;
... The result is '0000' -- the padding byte for BINARY is X'00' (0x00), not X'20' (space).
There also has to be a rule about what to do for comparisons if comparands end with padding bytes.
CREATE TABLE t5 (s1 VARBINARY(2));INSERT INTO t5 VALUES (X'0102');SELECT * FROM t5 WHERE s1 = X'010200';
... This returns zero rows. It's implementation-defined whether MySQL should
ignore trailing X'00' during comparisons, so there was no chance of getting
it wrong.
The behaviour difference between BINARY and VARBINARY can cause fun:
CREATE TABLE t7 (s1 VARBINARY(2) PRIMARY KEY);CREATE TABLE t8 (s1 BINARY(2), FOREIGN KEY (s1) REFERENCES t7 (s1));INSERT INTO t7 VALUES (0x01);INSERT INTO t8 SELECT s1 FROM t7;
... which fails on a foreign-key constraint error! It looks bizarre
that a value which is coming from the primary-key row can't be put
in the foreign-key row, doesn't it? But the zero-padding rule, combined
with the no-ignore-zero rule, means this is inevitable.
BINARY(x) is a fine data type whenever it's certain that all the valueswill be exactly x bytes long, and otherwise it's a troublemaker.
When to use VARBINARY
VARBINARY is better than TINYBLOB or MEDIUMBLOB because it has a definite
maximum size, and that makes life easier for client programs that want to
know: how wide can the display be? In most DBMSs it's more important that BLOBs can be stored separately from the rest of the row.
VARBINARY is better than VARCHAR if there should be no validity checking.For example, if the default character set is UTF8 then this is illegal:
CREATE TABLE t9 (s1 VARCHAR(5));INSERT INTO t9 VALUES (0xF4808283);
... but this is legal because character set doesn't matter:
CREATE TABLE t10 (s1 VARBINARY(5));INSERT INTO t10 VALUES (0xF4808283);
(I ran into this example on aSQL Server forumwhere the participants display woeful ignorance of Unicode).
And finally converting everything to VARBINARY is one way to avoid the annoying message "Invalid mix of collations". In fact the wikimedia folks appear to havechanged all VARCHARs to VARBINARYsback in 2011 just to avoid that error. I opine that the less drastic solution is to use collations consistently, but I wasn't there.

MySQL中去重主要使用DISTINCT和GROUPBY。 1.DISTINCT用於返回唯一值,如SELECTDISTINCTname,ageFROMusers。 2.GROUPBY通過分組實現去重並可進行聚合操作,如SELECTid,name,MAX(created_at)aslatest_dateFROMusersGROUPBYname。

在MySQL中查看所有數據庫的信息可以通過以下兩種方法實現:1.使用SHOWDATABASES;命令,可以快速列出所有數據庫名稱。 2.查詢INFORMATION_SCHEMA.SCHEMATA表,可以獲取更詳細的信息,如數據庫的創建時間和字符集設置。

在MySQL中,可以通過以下步驟添加和刪除唯一約束:1.在創建表時添加唯一約束,使用CREATETABLE語句;2.在已存在的表上添加唯一約束,使用ALTERTABLE和ADDCONSTRAINT語句;3.刪除唯一約束,使用ALTERTABLE和DROPINDEX語句。唯一約束確保表中某列或多列的值唯一,防止數據重複,維護數據完整性。

在MySQL中復製表結構和數據的方法包括:1.使用CREATETABLE...LIKE複製表結構;2.使用INSERTINTO...SELECT複製數據。通過這些步驟,可以高效地在不同場景下進行數據備份和遷移。

從MySQL表中隨機獲取數據可以使用RAND()函數。 1.基本用法:SELECTFROMusersORDERBYRAND()LIMIT5;2.高級用法:SELECTFROMusersWHEREid>=(SELECTFLOOR(RAND()*(SELECTMAX(id)FROMusers)))LIMIT5;優化策略包括使用索引和分頁查詢。

MySQL表的索引优化策略包括:1.为经常查询的列创建索引;2.使用联合索引提高多列查询效率;3.定期检查和优化索引,避免滥用和失效;4.选择合适的索引类型和列,监控和优化索引,编写高效查询语句。通过这些方法,可以显著提升MySQL查询性能。

在MySQL中優化數據更新和刪除操作可以通過以下步驟實現:1.使用索引,如CREATEINDEXidx_last_order_dateONcustomers(last_order_date);2.進行批量操作,減少鎖定時間;3.避免全表掃描,使用合適的索引和WHERE子句;4.使用事務提高性能和原子性;5.監控和優化,利用慢查詢日誌識別性能瓶頸。

修改MySQL默認端口號的方法是:1.打開配置文件sudonano/etc/my.cnf;2.在[mysqld]部分添加或修改port=3307;3.保存並退出編輯器;4.重啟MySQL服務sudosystemctlrestartmysql,這樣可以提高數據庫的安全性並解決端口衝突問題。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

禪工作室 13.0.1
強大的PHP整合開發環境