6 Â Ñèíòàêñèñ îïåðàòîðîâ SQL

реклама
6
Ñèíòàêñèñ
îïåðàòîðîâ SQL
Â
ɧɚɫɬɨɹɳɟɣ ɝɥɚɜɟ ɨɩɢɫɚɧ ɫɢɧɬɚɤɫɢɫ ɨɩɟɪɚɬɨɪɨɜ SQL, ɤɨɬɨɪɵɣ ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ ɜ
MySQL.
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
6.1.1. Ñèíòàêñèñ DELETE
Ɉɞɧɨɬɚɛɥɢɱɧɵɣ ɫɢɧɬɚɤɫɢɫ:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM ɢɦɹ_ɬɚɛɥɢɰɵ
[WHERE ɨɩɪɟɞɟɥɟɧɢɟ_where]
[ORDER BY ...]
[LIMIT ɤɨɥɢɱɟɫɬɜɨ_ɫɬɪɨɤ]
Ɇɧɨɝɨɬɚɛɥɢɱɧɵɣ ɫɢɧɬɚɤɫɢɫ:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
ɢɦɹ_ɬɚɛɥɢɰɵ[.*] [, ɢɦɹ_ɬɚɛɥɢɰɵ[.*] ...]
FROM ɬɚɛɥɢɱɧɵɟ_ɫɫɵɥɤɢ
[WHERE ɨɩɪɟɞɟɥɟɧɢɟ_where]
ɂɥɢ:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
FROM ɢɦɹ_ɬɚɛɥɢɰɵ[.*] [, ɢɦɹ_ɬɚɛɥɢɰɵ[.*] ...]
USING ɬɚɛɥɢɱɧɵɟ_ɫɫɵɥɤɢ
[WHERE ɨɩɪɟɞɟɥɟɧɢɟ_where]
DELETE ɭɞɚɥɹɟɬ ɫɬɪɨɤɢ ɢɡ ɬɚɛɥɢɰɵ ɢɦɹ_ɬɚɛɥɢɰɵ, ɭɞɨɜɥɟɬɜɨɪɹɸɳɢɟ ɭɫɥɨɜɢɸ ɨɩɪɟɞɟɥɟɧɢɟ_where ɢ ɜɨɡɜɪɚɳɚɟɬ ɤɨɥɢɱɟɫɬɜɨ ɭɞɚɥɟɧɧɵɯ ɫɬɪɨɤ.
ȼ ɫɥɭɱɚɟ ɜɵɞɚɱɢ ɨɩɟɪɚɬɨɪɚ DELETE ɛɟɡ ɤɨɧɫɬɪɭɤɰɢɢ WHERE ɭɞɚɥɹɸɬɫɹ ɜɫɟ ɫɬɪɨɤɢ. ȿɫ-
ɥɢ ɜɚɦ ɧɟ ɧɭɠɧɨ ɡɧɚɬɶ ɤɨɥɢɱɟɫɬɜɨ ɭɞɚɥɟɧɧɵɯ ɫɬɪɨɤ, ɬɨ ɷɬɨ ɦɨɠɧɨ ɫɞɟɥɚɬɶ ɛɵɫɬɪɟɟ ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ TRUNCATE TABLE. ɋɦ. ɪɚɡɞɟɥ 6.1.9.
ȼ MySQL 3.23 ɨɩɟɪɚɬɨɪ DELETE ɛɟɡ ɤɨɧɫɬɪɭɤɰɢɢ WHERE ɜɨɡɜɪɚɳɚɟɬ ɧɨɥɶ.
ȼ ɜɟɪɫɢɢ MySQL 3.23, ɟɫɥɢ ɜɵ ɞɟɣɫɬɜɢɬɟɥɶɧɨ ɯɨɬɢɬɟ ɡɧɚɬɶ ɤɨɥɢɱɟɫɬɜɨ ɭɞɚɥɟɧɧɵɯ
ɡɚɩɢɫɟɣ, ɢ ɫɨɝɥɚɫɧɵ ɧɚ ɧɟɤɨɬɨɪɨɟ ɫɧɢɠɟɧɢɟ ɩɪɨɢɡɜɨɞɢɬɟɥɶɧɨɫɬɢ, ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ
DELETE ɫ ɤɨɧɫɬɪɭɤɰɢɟɣ WHERE, ɭɫɥɨɜɢɟ ɤɨɬɨɪɨɣ ɹɜɥɹɟɬɫɹ ɢɫɬɢɧɧɵɦ ɞɥɹ ɤɚɠɞɨɣ ɡɚɩɢɫɢ.
214
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɇɚɩɪɢɦɟɪ:
mysql> DELETE FROM ɢɦɹ_ɬɚɛɥɢɰɵ WHERE 1>0;
ɗɬɨ ɝɨɪɚɡɞɨ ɦɟɞɥɟɧɧɟɟ, ɱɟɦ DELETE FROM ɢɦɹ_ɬɚɛɥɢɰɵ ɛɟɡ ɤɨɧɫɬɪɭɤɰɢɢ WHERE, ɩɨɫɤɨɥɶɤɭ ɫɬɪɨɤɢ ɭɞɚɥɹɸɬɫɹ ɩɨ ɨɞɧɨɣ.
ȿɫɥɢ ɜɵ ɭɞɚɥɹɟɬɟ ɫɬɪɨɤɭ, ɫɨɞɟɪɠɚɳɭɸ ɦɚɤɫɢɦɚɥɶɧɨɟ ɡɧɚɱɟɧɢɟ ɫɬɨɥɛɰɚ
AUTO_INCREMENT, ɷɬɨ ɡɧɚɱɟɧɢɟ ɛɭɞɟɬ ɩɨɜɬɨɪɧɨ ɢɫɩɨɥɶɡɨɜɚɧɨ ɜ ɬɚɛɥɢɰɚɯ ISAM ɢ BDB, ɧɨ ɧɟ
ɜ ɬɚɛɥɢɰɚɯ MyISAM ɢɥɢ InnoDB. ȿɫɥɢ ɜɵ ɭɞɚɥɹɟɬɟ ɜɫɟ ɫɬɪɨɤɢ ɬɚɛɥɢɰɵ ɨɩɟɪɚɬɨɪɨɦ DELETE
FROM ɢɦɹ_ɬɚɛɥɢɰɵ (ɛɟɡ ɤɨɧɫɬɪɭɤɰɢɢ WHERE) ɜ ɪɟɠɢɦɟ AUTOCOMMIT, ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ
ɫɬɚɪɬɭɟɬ ɞɥɹ ɜɫɟɯ ɬɢɩɨɜ ɬɚɛɥɢɰ, ɤɪɨɦɟ InnoDB ɢ (ɧɚɱɢɧɚɹ ɫ MySQL 4.0) MyISAM. ɋɭɳɟɫɬɜɭɸɬ ɧɟɤɨɬɨɪɵɟ ɢɫɤɥɸɱɟɧɢɹ ɭɩɨɦɹɧɭɬɨɝɨ ɩɨɜɟɞɟɧɢɹ, ɤɨɬɨɪɵɟ ɩɨɞɪɨɛɧɨ ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ ɜ ɝɥɚɜɟ, ɩɨɫɜɹɳɟɧɧɨɣ InnoDB, ɤɧɢɝɢ MySQL. Ɋɭɤɨɜɨɞɫɬɜɨ ɚɞɦɢɧɢɫɬɪɚɬɨɪɚ (Ɇ. :
ɂɡɞɚɬɟɥɶɫɤɢɣ ɞɨɦ “ȼɢɥɶɹɦɫ”, 2005, ISBN 5-8459-0805-1).
Ⱦɥɹ ɬɚɛɥɢɰ MyISAM ɢ BDB ɦɨɠɧɨ ɨɛɴɹɜɢɬɶ ɜɬɨɪɢɱɧɵɣ ɫɬɨɥɛɟɰ AUTO_INCREMENT ɜ ɫɨɫɬɚɜɧɨɦ ɤɥɸɱɟ. ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɩɨɜɬɨɪɧɨɟ ɢɫɩɨɥɶɡɨɜɚɧɢɟ ɡɧɚɱɟɧɢɣ, ɭɞɚɥɟɧɧɵɯ ɫ ɜɟɪɲɢɧɵ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ, ɩɪɨɢɫɯɨɞɢɬ ɞɚɠɟ ɞɥɹ ɬɚɛɥɢɰ MyISAM.
Ɉɩɟɪɚɬɨɪ DELETE ɩɨɞɞɟɪɠɢɜɚɟɬ ɫɥɟɞɭɸɳɢɟ ɦɨɞɢɮɢɤɚɬɨɪɵ:
ɭɤɚɡɚɬɶ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ LOW_PRIORITY, ɜɵɩɨɥɧɟɧɢɟ DELETE ɨɬɤɥɚɞɵɜɚɟɬɫɹ ɞɨ
ɬɟɯ ɩɨɪ, ɩɨɤɚ ɞɪɭɝɢɟ ɤɥɢɟɧɬɵ ɧɟ ɡɚɜɟɪɲɚɬ ɱɬɟɧɢɟ ɬɚɛɥɢɰɵ.
„ȿɫɥɢ
ɬɚɛɥɢɰ MyISAM, ɟɫɥɢ ɭɤɚɡɚɧɨ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ QUICK, ɦɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ ɧɟ
ɨɛɴɟɞɢɧɹɟɬ ɥɢɫɬɶɹ ɢɧɞɟɤɫɚ ɜ ɩɪɨɰɟɫɫɟ ɭɞɚɥɟɧɢɹ, ɱɬɨ ɭɫɤɨɪɹɟɬ ɧɟɤɨɬɨɪɵɟ ɬɢɩɵ
ɨɩɟɪɚɰɢɣ DELETE.
„Ⱦɥɹ
ɫɥɨɜɨ IGNORE ɡɚɫɬɚɜɥɹɟɬ MySQL ɢɝɧɨɪɢɪɨɜɚɬɶ ɜɫɟ ɨɲɢɛɤɢ ɜ ɩɪɨɰɟɫɫɟ
ɭɞɚɥɟɧɢɹ ɫɬɪɨɤ. (Ɉɲɢɛɤɢ, ɨɛɧɚɪɭɠɟɧɧɵɟ ɧɚ ɫɬɚɞɢɢ ɫɢɧɬɚɤɫɢɱɟɫɤɨɝɨ ɚɧɚɥɢɡɚ, ɨɛɪɚɛɚɬɵɜɚɸɬɫɹ ɨɛɵɱɧɵɦ ɨɛɪɚɡɨɦ.) Ɉɛ ɨɲɢɛɤɚɯ, ɤɨɬɨɪɵɟ ɢɝɧɨɪɢɪɭɸɬɫɹ ɜɫɥɟɞɫɬɜɢɟ
ɩɪɢɦɟɧɟɧɢɹ ɷɬɨɣ ɨɩɰɢɢ, ɫɨɨɛɳɚɟɬɫɹ ɜ ɜɢɞɟ ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ. ɗɬɚ ɨɩɰɢɹ ɩɨɹɜɢɥɚɫɶ ɜ MySQL 4.1.1.
„Ʉɥɸɱɟɜɨɟ
ɇɚ ɫɤɨɪɨɫɬɶ ɜɵɩɨɥɧɟɧɢɹ ɨɩɟɪɚɰɢɢ ɭɞɚɥɟɧɢɹ ɦɨɝɭɬ ɬɚɤɠɟ ɩɨɜɥɢɹɬɶ ɮɚɤɬɨɪɵ, ɤɨɬɨɪɵɟ
ɨɛɫɭɠɞɚɸɬɫɹ ɜ ɝɥɚɜɟ, ɩɨɫɜɹɳɟɧɧɨɣ ɨɩɬɢɦɢɡɚɰɢɢ, ɤɧɢɝɢ MySQL. Ɋɭɤɨɜɨɞɫɬɜɨ ɚɞɦɢɧɢɫɬɪɚɬɨɪɚ.
ȼ ɬɚɛɥɢɰɚɯ MyISAM ɭɞɚɥɟɧɧɵɟ ɡɚɩɢɫɢ ɩɨɦɟɳɚɸɬɫɹ ɜ ɫɜɹɡɧɵɣ ɫɩɢɫɨɤ, ɢ ɩɨɫɥɟɞɭɸɳɢɣ
ɨɩɟɪɚɬɨɪ INSERT ɩɨɜɬɨɪɧɨ ɢɫɩɨɥɶɡɭɟɬ ɫɬɚɪɵɟ ɩɨɡɢɰɢɢ ɡɚɩɢɫɟɣ. ɑɬɨɛɵ ɜɟɪɧɭɬɶ ɧɟɢɫɩɨɥɶɡɭɟɦɨɟ ɩɪɨɫɬɪɚɧɫɬɜɨ ɢ ɭɦɟɧɶɲɢɬɶ ɪɚɡɦɟɪɵ ɮɚɣɥɨɜ, ɩɪɢɦɟɧɹɣɬɟ ɨɩɟɪɚɬɨɪ OPTIMIZE
TABLE ɢɥɢ ɭɬɢɥɢɬɭ myisamchk ɞɥɹ ɪɟɨɪɝɚɧɢɡɚɰɢɢ ɬɚɛɥɢɰ. OPTIMIZE TABLE ɩɪɨɳɟ, ɧɨ
myisamchk ɛɵɫɬɪɟɟ. ɋɦ. ɪɚɡɞɟɥ 6.5.2.5.
ɋɩɟɰɢɮɢɱɧɚɹ ɞɥɹ MySQL ɨɩɰɢɹ LIMIT ɤɨɥɢɱɟɫɬɜɨ_ɫɬɪɨɤ ɨɩɟɪɚɬɨɪɚ DELETE ɫɨɨɛɳɚɟɬ
ɫɟɪɜɟɪɭ ɦɚɤɫɢɦɚɥɶɧɨɟ ɱɢɫɥɨ ɭɞɚɥɹɟɦɵɯ ɫɬɪɨɤ ɩɟɪɟɞ ɜɨɡɜɪɚɬɨɦ ɭɩɪɚɜɥɟɧɢɹ ɤɥɢɟɧɬɭ. ɗɬɨ
ɩɪɢɦɟɧɹɟɬɫɹ ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɝɚɪɚɧɬɢɪɨɜɚɬɶ, ɱɬɨ ɜɵɩɨɥɧɟɧɢɟ ɤɚɤɨɝɨ-ɬɨ ɨɫɨɛɟɧɧɨɝɨ ɨɩɟɪɚɬɨɪɚ DELETE ɧɟ ɡɚɣɦɟɬ ɫɥɢɲɤɨɦ ɦɧɨɝɨ ɜɪɟɦɟɧɢ. ȼɵ ɦɨɠɟɬɟ ɩɪɨɫɬɨ ɩɨɜɬɨɪɹɬɶ ɷɬɨɬ
ɨɩɟɪɚɬɨɪ DELETE ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɤɨɥɢɱɟɫɬɜɨ ɭɞɚɥɟɧɧɵɯ ɫɬɪɨɤ ɧɟ ɨɤɚɠɟɬɫɹ ɦɟɧɶɲɟ, ɱɟɦ
ɭɤɚɡɚɧɨ ɜ ɡɧɚɱɟɧɢɢ LIMIT.
ȿɫɥɢ ɨɩɟɪɚɬɨɪ DELETE ɜɤɥɸɱɚɟɬ ɤɨɧɫɬɪɭɤɰɢɸ ORDER BY, ɬɨ ɫɬɪɨɤɢ ɭɞɚɥɹɸɬɫɹ ɜ ɭɤɚɡɚɧɧɨɦ ɩɨɪɹɞɤɟ. ɗɬɨ ɞɟɣɫɬɜɢɬɟɥɶɧɨ ɭɞɨɛɧɨ ɬɨɥɶɤɨ ɜ ɫɨɱɟɬɚɧɢɢ ɫ ɨɩɰɢɟɣ LIMIT. ɇɚɩɪɢɦɟɪ, ɫɥɟɞɭɸɳɢɣ ɨɩɟɪɚɬɨɪ ɧɚɯɨɞɢɬ ɫɬɪɨɤɢ, ɭɞɨɜɥɟɬɜɨɪɹɸɳɢɟ ɭɫɥɨɜɢɸ WHERE, ɫɨɪɬɢɪɭɟɬ
ɢɯ ɩɨ ɩɨɪɹɞɤɭ timestamp ɢ ɭɞɚɥɹɟɬ ɩɟɪɜɭɸ (ɧɚɢɛɨɥɟɟ ɫɬɚɪɭɸ) ɢɡ ɧɢɯ:
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
215
DELETE FROM somelog
WHERE user = 'jcole'
ORDER BY timestamp
LIMIT 1
ORDER BY ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɜɦɟɫɬɟ ɫ DELETE, ɧɚɱɢɧɚɹ ɫ MySQL 4.0.0.
ɇɚɱɢɧɚɹ ɫ MySQL 4.0, ɜ ɨɩɟɪɚɬɨɪɟ DELETE ɦɨɠɧɨ ɭɤɚɡɵɜɚɬɶ ɦɧɨɠɟɫɬɜɨ ɬɚɛɥɢɰ, ɱɬɨɛɵ ɭɞɚɥɹɬɶ ɫɬɪɨɤɢ ɢɡ ɨɞɧɨɣ ɢɥɢ ɛɨɥɟɟ ɬɚɛɥɢɰ, ɜ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɨɩɪɟɞɟɥɟɧɧɨɝɨ ɭɫɥɨɜɢɹ
ɜɨ ɦɧɨɠɟɫɬɜɟ ɬɚɛɥɢɰ. Ɉɞɧɚɤɨ ɜ ɦɧɨɝɨɬɚɛɥɢɱɧɨɦ DELETE ɧɟɥɶɡɹ ɭɤɚɡɵɜɚɬɶ ORDER BY ɢɥɢ
LIMIT.
ɉɟɪɜɵɣ ɦɧɨɝɨɬɚɛɥɢɱɧɵɣ ɫɢɧɬɚɤɫɢɫ DELETE ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ, ɧɚɱɢɧɚɹ ɫ MySQL 4.0.0.
ȼɬɨɪɨɣ ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ, ɧɚɱɢɧɚɹ ɫ MySQL 4.0.2. ɑɚɫɬɶ ɬɚɛɥɢɱɧɵɟ_ɫɫɵɥɤɢ ɩɟɪɟɱɢɫɥɹɟɬ
ɬɚɛɥɢɰɵ, ɭɱɚɫɬɜɭɸɳɢɟ ɜ ɫɨɟɞɢɧɟɧɢɢ. ɗɬɨɬ ɫɢɧɬɚɤɫɢɫ ɩɨɞɪɨɛɧɨ ɨɩɢɫɚɧ ɜ ɪɚɡɞɟɥɟ 6.1.7.1.
ɉɪɢ ɢɫɩɨɥɶɡɨɜɚɧɢɢ ɩɟɪɜɨɝɨ ɫɢɧɬɚɤɫɢɫɚ ɭɞɚɥɹɸɬɫɹ ɬɨɥɶɤɨ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɫɬɪɨɤɢ
ɢɡ ɬɚɛɥɢɰ, ɩɟɪɟɱɢɫɥɟɧɧɵɯ ɩɟɪɟɞ ɤɨɧɫɬɪɭɤɰɢɟɣ FROM. ɉɪɢ ɜɬɨɪɨɦ ɫɢɧɬɚɤɫɢɫɟ ɭɞɚɥɹɸɬɫɹ
ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɫɬɪɨɤɢ ɢɡ ɬɚɛɥɢɰ, ɩɟɪɟɱɢɫɥɟɧɧɵɯ ɜ ɤɨɧɫɬɪɭɤɰɢɢ FROM (ɩɟɪɟɞ USING).
ɗɮɮɟɤɬ ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɬɨɦ, ɱɬɨ ɜɵ ɦɨɠɟɬɟ ɭɞɚɥɹɬɶ ɫɬɪɨɤɢ ɢɡ ɦɧɨɝɢɯ ɬɚɛɥɢɰ ɨɞɧɨɜɪɟɦɟɧɧɨ ɢ ɬɚɤɠɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɞɨɩɨɥɧɢɬɟɥɶɧɵɟ ɬɚɛɥɢɰɵ ɞɥɹ ɩɨɢɫɤɚ:
DELETE t1,t2 FROM t1,t2,t3 WHERE t1.id=t2.id AND t2.id=t3.id;
ɢɥɢ
DELETE FROM t1,t2 USING t1,t2,t3 WHERE t1.id=t2.id AND t2.id=t3.id;
ɗɬɢ ɨɩɟɪɚɬɨɪɵ ɢɫɩɨɥɶɡɭɸɬ ɬɪɢ ɬɚɛɥɢɰɵ ɞɥɹ ɩɨɢɫɤɚ ɫɬɪɨɤ, ɩɨɞɥɟɠɚɳɢɯ ɭɞɚɥɟɧɢɸ, ɧɨ
ɭɞɚɥɹɸɬ ɩɨɞɯɨɞɹɳɢɟ ɫɬɪɨɤɢ ɬɨɥɶɤɨ ɜ ɞɜɭɯ ɬɚɛɥɢɰɚɯ – t1 ɢ t2.
ɉɪɢɦɟɪɵ ɩɨɤɚɡɵɜɚɸɬ ɜɥɨɠɟɧɧɵɟ ɫɨɟɞɢɧɟɧɢɹ, ɢɫɩɨɥɶɡɭɹ ɨɩɟɪɚɰɢɸ ɡɚɩɹɬɨɣ, ɧɨ ɦɧɨɝɨɬɚɛɥɢɱɧɵɟ ɨɩɟɪɚɬɨɪɵ DELETE ɦɨɝɭɬ ɬɚɤɠɟ ɩɪɢɦɟɧɹɬɶ ɥɸɛɨɣ ɬɢɩ ɫɨɟɞɢɧɟɧɢɹ, ɞɨɩɭɫɬɢɦɵɣ ɞɥɹ ɨɩɟɪɚɬɨɪɨɜ SELECT, ɬɚɤɨɣ, ɧɚɩɪɢɦɟɪ, ɤɚɤ LEFT JOIN.
ɋɢɧɬɚɤɫɢɫ ɞɨɩɭɫɤɚɟɬ '.*' ɩɨɫɥɟ ɢɦɟɧ ɬɚɛɥɢɰ ɞɥɹ ɞɨɫɬɢɠɟɧɢɹ ɫɨɜɦɟɫɬɢɦɨɫɬɢ ɫ Access.
ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ɦɧɨɝɨɬɚɛɥɢɱɧɵɣ ɨɩɟɪɚɬɨɪ DELETE ɫ ɬɚɛɥɢɰɚɦɢ InnoDB, ɭ ɤɨɬɨɪɵɯ ɟɫɬɶ ɨɝɪɚɧɢɱɟɧɢɹ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ, ɨɩɬɢɦɢɡɚɬɨɪ MySQL ɦɨɠɟɬ ɨɛɪɚɛɚɬɵɜɚɬɶ ɬɚɛɥɢɰɵ ɜ ɩɨɪɹɞɤɟ, ɨɬɥɢɱɚɸɳɟɦɫɹ ɨɬ ɡɚɞɚɧɧɨɝɨ ɢɯ ɨɬɧɨɲɟɧɢɟɦ ɪɨɞɢɬɟɥɶɫɤɢɣ/ɞɨɱɟɪɧɢɣ. ȼ
ɷɬɨɦ ɫɥɭɱɚɟ ɨɩɟɪɚɬɨɪ ɡɚɜɟɪɲɚɟɬɫɹ ɨɲɢɛɤɨɣ ɢ ɬɪɚɧɡɚɤɰɢɹ ɨɬɤɚɬɵɜɚɟɬɫɹ. ȼɦɟɫɬɨ ɷɬɨɝɨ
ɭɞɚɥɢɬɟ ɧɭɠɧɵɟ ɡɚɩɢɫɢ ɢɡ ɨɞɧɨɣ ɬɚɛɥɢɰɵ, ɢ ɪɚɫɫɱɢɬɵɜɚɣɬɟ ɧɚ ɜɨɡɦɨɠɧɨɫɬɶ ON DELETE,
ɩɪɟɞɨɫɬɚɜɥɹɟɦɭɸ InnoDB, ɱɬɨɛɵ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɦ ɨɛɪɚɡɨɦ ɦɨɞɢɮɢɰɢɪɨɜɚɬɶ ɡɚɜɢɫɢɦɵɟ
ɬɚɛɥɢɰɵ.
oË ÑËÕÏÛµÂ!
ȼ MySQL 4.0 ɞɥɹ ɭɞɚɥɟɧɢɹ ɡɚɩɢɫɟɣ ɜɵ ɞɨɥɠɧɵ ɨɛɪɚɳɚɬɶɫɹ ɤ ɬɚɛɥɢɰɚɦ ɩɨ ɢɯ ɪɟɚɥɶɧɵɦ ɢɦɟɧɚɦ.
ȼ MySQL 4.1 ɞɥɹ ɨɛɪɚɳɟɧɢɹ ɤ ɢɦɟɧɚɦ ɬɚɛɥɢɰ ɧɟɨɛɯɨɞɢɦɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɩɫɟɜɞɨɧɢɦɵ (ɟɫɥɢ ɨɧɢ
ɭɤɚɡɚɧɵ):
ȼ MySQL 4.0:
DELETE test FROM test AS t1, test2 WHERE ...
ȼ MySQL 4.1:
DELETE t1 FROM test AS t1, test2 WHERE ...
ɉɪɢɱɢɧɚ ɬɨɝɨ, ɱɬɨ ɷɬɨ ɧɟ ɛɵɥɨ ɪɟɚɥɢɡɨɜɚɧɨ ɜ 4.0, ɫɨɫɬɨɢɬ ɜ ɬɨɦ, ɱɬɨ ɦɵ ɧɟ ɯɨɬɟɥɢ ɪɚɡɪɭɲɚɬɶ
ɩɪɢɥɨɠɟɧɢɹ MySQL 4.0, ɤɨɬɨɪɵɟ ɢɫɩɨɥɶɡɨɜɚɥɢ ɫɬɚɪɵɣ ɫɢɧɬɚɤɫɢɫ.
216
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
6.1.2. Ñèíòàêñèñ DO
DO ɜɵɪɚɠɟɧɢɟ [, ɜɵɪɚɠɟɧɢɟ] ...
Ɉɩɟɪɚɬɨɪ DO ɜɵɩɨɥɧɹɟɬ ɜɵɪɚɠɟɧɢɹ, ɧɨ ɧɟ ɜɨɡɜɪɚɳɚɟɬ ɧɢɤɚɤɢɯ ɪɟɡɭɥɶɬɚɬɨɜ. ɗɬɨ ɫɨɤɪɚɳɟɧɢɟ ɞɥɹ SELECT ɜɵɪɚɠɟɧɢɟ, ..., ɤɨɬɨɪɨɟ ɨɛɥɚɞɚɟɬ ɬɟɦ ɩɪɟɢɦɭɳɟɫɬɜɨɦ, ɱɬɨ ɪɚɛɨɬɚɟɬ ɧɟɦɧɨɝɨ ɛɵɫɬɪɟɟ, ɟɫɥɢ ɜɚɫ ɧɟ ɢɧɬɟɪɟɫɭɟɬ ɪɟɡɭɥɶɬɚɬ.
DO ɭɞɨɛɟɧ ɜ ɨɫɧɨɜɧɨɦ ɞɥɹ ɮɭɧɤɰɢɣ, ɤɨɬɨɪɵɟ ɢɦɟɸɬ ɫɬɨɪɨɧɧɢɣ ɷɮɮɟɤɬ, ɬɚɤɢɯ ɤɚɤ
RELEASE_LOCK().
6.1.3. Ñèíòàêñèñ HANDLER
HANDLER ɢɦɹ_ɬɚɛɥɢɰɵ OPEN [ AS ɩɫɟɜɞɨɧɢɦ ]
HANDLER ɢɦɹ_ɬɚɛɥɢɰɵ READ ɢɦɹ_ɢɧɞɟɤɫɚ { = | >= | <= | < }
(ɡɧɚɱɟɧɢɟ1,ɡɧɚɱɟɧɢɟ2,...)
[ WHERE ɭɫɥɨɜɢɟ_where ] [LIMIT ... ]
HANDLER ɢɦɹ_ɬɚɛɥɢɰɵ READ ɢɦɹ_ɢɧɞɟɤɫɚ { FIRST | NEXT | PREV | LAST }
[ WHERE ɭɫɥɨɜɢɟ_where ] [LIMIT ... ]
HANDLER ɢɦɹ_ɬɚɛɥɢɰɵ READ { FIRST | NEXT }
[ WHERE ɭɫɥɨɜɢɟ_where ] [LIMIT ... ]
HANDLER ɢɦɹ_ɬɚɛɥɢɰɵ CLOSE
Ɉɩɟɪɚɬɨɪ HANDLER ɩɪɟɞɨɫɬɚɜɥɹɟɬ ɩɪɹɦɨɣ ɞɨɫɬɭɩ ɤ ɢɧɬɟɪɮɟɣɫɚɦ ɦɟɯɚɧɢɡɦɚ ɯɪɚɧɟɧɢɹ
ɬɚɛɥɢɰ. Ɉɧ ɞɨɫɬɭɩɟɧ ɞɥɹ ɬɚɛɥɢɰ MyISAM, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.0.0, ɢ ɞɥɹ ɬɚɛɥɢɰ
InnoDB, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.0.3.
Ɉɩɟɪɚɬɨɪ HANDLER...OPEN ɨɬɤɪɵɜɚɟɬ ɬɚɛɥɢɰɭ, ɞɟɥɚɹ ɟɟ ɞɨɫɬɭɩɧɨɣ ɞɥɹ ɩɨɫɥɟɞɭɸɳɢɯ
ɨɩɟɪɚɬɨɪɨɜ HANDLER...READ. ɗɬɨɬ ɬɚɛɥɢɱɧɵɣ ɨɛɴɟɤɬ ɧɟ ɪɚɡɞɟɥɹɟɬɫɹ ɞɪɭɝɢɦɢ ɩɨɬɨɤɚɦɢ
ɫɟɪɜɟɪɚ ɢ ɧɟ ɡɚɤɪɵɜɚɟɬɫɹ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɧɟ ɛɭɞɟɬ ɜɵɡɜɚɧ HANDLER...CLOSE, ɥɢɛɨ ɧɟ
ɛɭɞɟɬ ɩɪɟɪɜɚɧɨ ɜɵɩɨɥɧɟɧɢɟ ɩɨɬɨɤɚ. ȿɫɥɢ ɜɵ ɨɬɤɪɵɜɚɟɬɟ ɬɚɛɥɢɰɭ, ɢɫɩɨɥɶɡɭɹ ɩɫɟɜɞɨɧɢɦ,
ɩɨɫɥɟɞɭɸɳɢɟ ɨɛɪɚɳɟɧɢɹ ɤ ɧɟɣ ɨɩɟɪɚɬɨɪɨɦ HANDLER ɬɚɤɠɟ ɞɨɥɠɧɵ ɢɫɩɨɥɶɡɨɜɚɬɶ ɩɫɟɜɞɨɧɢɦ ɜɦɟɫɬɨ ɪɟɚɥɶɧɨɝɨ ɢɦɟɧɢ ɬɚɛɥɢɰɵ.
ɉɟɪɜɵɣ ɫɢɧɬɚɤɫɢɫ HANDLER...READ ɢɡɜɥɟɤɚɟɬ ɫɬɪɨɤɭ, ɜ ɤɨɬɨɪɨɣ ɭɤɚɡɚɧɧɵɣ ɢɧɞɟɤɫ
ɭɞɨɜɥɟɬɜɨɪɹɟɬ ɡɚɞɚɧɧɵɦ ɡɧɚɱɟɧɢɹɦ ɢ ɜɵɩɨɥɧɹɟɬɫɹ ɭɫɥɨɜɢɟ WHERE. ȿɫɥɢ ɭ ɜɚɫ ɟɫɬɶ ɢɧɞɟɤɫ ɫ ɫɨɫɬɚɜɧɵɦ ɤɥɸɱɨɦ, ɭɤɚɡɵɜɚɣɬɟ ɡɧɚɱɟɧɢɹ ɢɧɞɟɤɫɧɵɯ ɫɬɨɥɛɰɨɜ ɜ ɜɢɞɟ ɫɩɢɫɤɚ ɫ
ɪɚɡɞɟɥɢɬɟɥɹɦɢ-ɡɚɩɹɬɵɦɢ. Ɇɨɠɧɨ ɭɤɚɡɵɜɚɬɶ ɥɢɛɨ ɜɫɟ ɫɬɨɥɛɰɵ ɢɧɞɟɤɫɚ, ɥɢɛɨ ɥɟɜɵɣ (ɧɚɱɚɥɶɧɵɣ) ɩɪɟɮɢɤɫ ɫɩɢɫɤɚ ɢɧɞɟɤɫɧɵɯ ɫɬɨɥɛɰɨɜ. ɉɪɟɞɩɨɥɨɠɢɦ, ɱɬɨ ɢɧɞɟɤɫ ɜɤɥɸɱɚɟɬ ɬɪɢ
ɫɬɨɥɛɰɚ ɫ ɢɦɟɧɚɦɢ col_a, col_b ɢ col_c, ɩɪɢɱɟɦ ɢɦɟɧɧɨ ɜ ɬɚɤɨɦ ɩɨɪɹɞɤɟ. ȼ ɨɩɟɪɚɬɨɪɟ
HANDLER ɦɨɠɧɨ ɫɩɟɰɢɮɢɰɢɪɨɜɚɬɶ ɡɧɚɱɟɧɢɹ ɞɥɹ ɬɪɟɯ ɫɬɨɥɛɰɨɜ ɢɧɞɟɤɫɚ ɥɢɛɨ ɞɥɹ ɢɯ ɥɟɜɨɝɨ ɩɨɞɦɧɨɠɟɫɬɜɚ, ɧɚɩɪɢɦɟɪ:
HANDLER ... ɢɦɹ_ɢɧɞɟɤɫɚ = (ɡɧɚɱɟɧɢɟ_col_a,ɡɧɚɱɟɧɢɟ_col_b,ɡɧɚɱɟɧɢɟ_col_c) ...
HANDLER ... ɢɦɹ_ɢɧɞɟɤɫɚ = (ɡɧɚɱɟɧɢɟ_col_a,ɡɧɚɱɟɧɢɟ_col_b) ...
HANDLER ... ɢɦɹ_ɢɧɞɟɤɫɚ = (ɡɧɚɱɟɧɢɟ_col_a) ...
ȼɬɨɪɨɣ ɫɢɧɬɚɤɫɢɫ HANDLER...READ ɢɡɜɥɟɤɚɟɬ ɫɬɪɨɤɭ ɢɡ ɬɚɛɥɢɰɵ, ɭɞɨɜɥɟɬɜɨɪɹɸɳɭɸ
ɭɫɥɨɜɢɸ WHERE, ɜ ɩɨɪɹɞɤɟ, ɡɚɞɚɧɧɨɦ ɢɧɞɟɤɫɨɦ.
Ɍɪɟɬɢɣ ɫɢɧɬɚɤɫɢɫ HANDLER...READ ɢɡɜɥɟɤɚɟɬ ɫɬɪɨɤɭ ɢɡ ɬɚɛɥɢɰɵ, ɭɞɨɜɥɟɬɜɨɪɹɸɳɭɸ
ɭɫɥɨɜɢɸ WHERE, ɜ ɧɚɬɭɪɚɥɶɧɨɦ ɩɨɪɹɞɤɟ. ɇɚɬɭɪɚɥɶɧɵɣ ɩɨɪɹɞɨɤ – ɷɬɨ ɬɨɬ ɩɨɪɹɞɨɤ, ɜ ɤɨɬɨɪɨɦ ɫɬɪɨɤɢ ɯɪɚɧɹɬɫɹ ɜ ɮɚɣɥɟ ɬɚɛɥɢɱɧɵɯ ɞɚɧɧɵɯ MyISAM. ɗɬɢ ɨɩɟɪɚɬɨɪɵ ɬɚɤɠɟ ɪɚɛɨɬɚɸɬ
ɢ ɫ ɬɚɛɥɢɰɚɦɢ InnoDB, ɧɨ ɬɚɦ ɧɟɬ ɬɚɤɨɣ ɤɨɧɰɟɩɰɢɢ, ɩɨɫɤɨɥɶɤɭ ɧɟɬ ɨɬɞɟɥɶɧɨɝɨ ɮɚɣɥɚ
ɞɚɧɧɵɯ ɬɚɛɥɢɰɵ.
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
217
Ȼɟɡ ɤɨɧɫɬɪɭɤɰɢɢ LIMIT ɜɫɟ ɮɨɪɦɵ ɨɩɟɪɚɬɨɪɚ HANDLER ... READ ɢɡɜɥɟɤɚɸɬ ɨɞɧɭ ɫɬɪɨɤɭ, ɟɫɥɢ ɨɧɚ ɞɨɫɬɭɩɧɚ. ɑɬɨɛɵ ɜɟɪɧɭɬɶ ɨɩɪɟɞɟɥɟɧɧɨɟ ɱɢɫɥɨ ɫɬɪɨɤ, ɜɤɥɸɱɢɬɟ ɤɨɧɫɬɪɭɤɰɢɸ
LIMIT. Ɉɧɚ ɢɦɟɟɬ ɬɨɬ ɠɟ ɫɢɧɬɚɤɫɢɫ, ɱɬɨ ɢ ɜ ɨɩɟɪɚɬɨɪɟ SELECT. ɋɦ. ɪɚɡɞɟɥ 6.1.7.
HANDLER...CLOSE ɡɚɤɪɵɜɚɟɬ ɬɚɛɥɢɰɭ, ɤɨɬɨɪɚɹ ɛɵɥɚ ɨɬɤɪɵɬɚ HANDLER...OPEN.
oË ÑËÕÏÛµÂ!
ɑɬɨɛɵ ɢɫɩɨɥɶɡɨɜɚɬɶ ɢɧɬɟɪɮɟɣɫ HANDLER ɞɥɹ ɨɛɪɚɳɟɧɢɹ ɤ ɩɟɪɜɢɱɧɨɦɭ ɤɥɸɱɭ (PRIMARY KEY)
ɬɚɛɥɢɰɵ, ɭɤɚɡɵɜɚɣɬɟ ɢɞɟɧɬɢɮɢɤɚɬɨɪ `PRIMARY` ɜ ɤɚɜɵɱɤɚɯ:
HANDLER ɢɦɹ_ɬɚɛɥɢɰɵ READ `PRIMARY` > (...);
ȼ ɨɩɪɟɞɟɥɟɧɧɨɦ ɫɦɵɫɥɟ HANDLER ɹɜɥɹɟɬɫɹ ɨɩɟɪɚɬɨɪɨɦ ɧɢɡɤɨɝɨ ɭɪɨɜɧɹ. ɇɚɩɪɢɦɟɪ, ɨɧ
ɧɟ ɨɛɟɫɩɟɱɢɜɚɟɬ ɰɟɥɨɫɬɧɨɫɬɢ. Ɍɨ ɟɫɬɶ, HANDLER...OPEN ɧɟ ɞɟɥɚɟɬ ɫɧɢɦɤɚ ɬɚɛɥɢɰɵ ɢ ɧɟ
ɛɥɨɤɢɪɭɟɬ ɬɚɛɥɢɰɭ. ɗɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɩɨɫɥɟ ɬɨɝɨ, ɤɚɤ ɷɬɨɬ ɨɩɟɪɚɬɨɪ ɨɬɩɪɚɜɥɟɧ ɫɟɪɜɟɪɭ,
ɞɚɧɧɵɟ ɬɚɛɥɢɰɵ ɦɨɝɭɬ ɛɵɬɶ ɦɨɞɢɮɢɰɢɪɨɜɚɧɵ (ɷɬɢɦ ɠɟ ɢɥɢ ɥɸɛɵɦ ɞɪɭɝɢɦ ɩɨɬɨɤɨɦ) ɢ
ɬɚɤɢɟ ɦɨɞɢɮɢɤɚɰɢɢ ɦɨɝɭɬ ɛɵɬɶ ɨɬɪɚɠɟɧɵ ɥɢɲɶ ɱɚɫɬɢɱɧɨ ɩɪɢ ɫɤɚɧɢɪɨɜɚɧɢɢ ɫ ɩɨɦɨɳɶɸ
HANDLER...NEXT ɢ HANDLER...PREV.
ȿɫɬɶ ɧɟɫɤɨɥɶɤɨ ɩɪɢɱɢɧ, ɩɨ ɤɨɬɨɪɵɦ ɫɬɨɢɬ ɢɫɩɨɥɶɡɨɜɚɬɶ ɢɧɬɟɪɮɟɣɫ HANDLER ɜɦɟɫɬɨ
ɨɛɵɱɧɨɝɨ ɨɩɟɪɚɬɨɪɚ SELECT:
ɛɵɫɬɪɟɟ, ɱɟɦ SELECT.
x ɇɚɡɧɚɱɟɧɧɵɣ handler-ɨɛɴɟɤɬ ɜɵɞɟɥɹɟɬɫɹ ɦɟɯɚɧɢɡɦɭ ɯɪɚɧɟɧɢɹ ɩɪɢ ɜɵɡɨɜɟ
HANDLER...OPEN, ɢ ɢɫɩɨɥɶɡɭɟɬɫɹ ɩɨɜɬɨɪɧɨ ɩɪɢ ɩɨɫɥɟɞɭɸɳɢɯ ɜɵɡɨɜɚɯ HANDLER
ɞɥɹ ɬɚɛɥɢɰɵ; ɟɝɨ ɧɟ ɧɭɠɧɨ ɩɨɜɬɨɪɧɨ ɢɧɢɰɢɚɥɢɡɢɪɨɜɚɬɶ ɤɚɠɞɵɣ ɪɚɡ.
x Ɇɟɧɶɲɟ ɪɚɛɨɬɵ ɩɨ ɫɢɧɬɚɤɫɢɱɟɫɤɨɦɭ ɚɧɚɥɢɡɭ.
x ɇɢɤɚɤɨɣ ɞɨɩɨɥɧɢɬɟɥɶɧɨɣ ɧɚɝɪɭɡɤɢ, ɫɜɹɡɚɧɧɨɣ ɫ ɩɪɨɜɟɪɤɨɣ ɡɚɩɪɨɫɨɜ ɢɥɢ
ɨɩɬɢɦɢɡɚɰɢɟɣ.
x Ɍɚɛɥɢɰɭ ɧɟ ɧɚɞɨ ɛɥɨɤɢɪɨɜɚɬɶ ɦɟɠɞɭ ɞɜɭɦɹ ɜɵɡɨɜɚɦɢ HANDLER.
x ɂɧɬɟɪɮɟɣɫ HANDLER ɧɟ ɞɨɥɠɟɧ ɨɛɟɫɩɟɱɢɜɚɬɶ ɰɟɥɨɫɬɧɨɫɬɶ ɞɚɧɧɵɯ (ɧɚɩɪɢɦɟɪ,
ɞɨɩɭɫɤɚɸɬɫɹ ɧɟɞɟɣɫɬɜɢɬɟɥɶɧɵɟ ɱɬɟɧɢɹ), ɩɨɷɬɨɦɭ ɦɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ ɦɨɠɟɬ
ɢɫɩɨɥɶɡɨɜɚɬɶ ɨɩɬɢɦɢɡɚɰɢɸ, ɤɨɬɨɪɭɸ ɨɛɵɱɧɨ ɧɟ ɩɨɡɜɨɥɹɟɬ SELECT.
„HANDLER
„HANDLER
ɡɧɚɱɢɬɟɥɶɧɨ ɭɩɪɨɳɚɟɬ ɩɟɪɟɧɨɫ ɜ MySQL ɩɪɢɥɨɠɟɧɢɣ, ɢɫɩɨɥɶɡɭɸɳɢɯ
ISAM-ɩɨɞɨɛɧɵɣ ɢɧɬɟɪɮɟɣɫ.
ɩɨɡɜɨɥɹɟɬ ɩɟɪɟɫɟɤɚɬɶ ɛɚɡɭ ɞɚɧɧɵɯ ɫɩɨɫɨɛɨɦ, ɤɨɬɨɪɵɣ ɧɟɩɪɨɫɬɨ (ɢɥɢ ɞɚɠɟ
ɧɟɜɨɡɦɨɠɧɨ) ɪɟɚɥɢɡɨɜɚɬɶ ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ SELECT. ɂɧɬɟɪɮɟɣɫ HANDLER – ɷɬɨ
ɛɨɥɟɟ ɟɫɬɟɫɬɜɟɧɧɵɣ ɫɩɨɫɨɛ ɞɨɫɬɭɩɚ ɤ ɞɚɧɧɵɦ ɢɡ ɩɪɢɥɨɠɟɧɢɣ, ɩɪɟɞɫɬɚɜɥɹɸɳɢɯ
ɢɧɬɟɪɚɤɬɢɜɧɵɣ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɢɣ ɢɧɬɟɪɮɟɣɫ ɞɨɫɬɭɩɚ ɤ ɛɚɡɟ ɞɚɧɧɵɯ.
„HANDLER
6.1.4. Ñèíòàêñèñ INSERT
INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] ɢɦɹ_ɬɚɛɥɢɰɵ [(ɢɦɹ_ɫɬɨɥɛɰɚ,...)]
VALUES ({ɜɵɪɚɠɟɧɢɟ | DEFAULT},...),(...),...
[ ON DUPLICATE KEY UPDATE ɢɦɹ_ɫɬɨɥɛɰɚ=ɜɵɪɚɠɟɧɢɟ, ... ]
ɢɥɢ:
INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] ɢɦɹ_ɬɚɛɥɢɰɵ
SET ɢɦɹ_ɫɬɨɥɛɰɚ={ɜɵɪɚɠɟɧɢɟ | DEFAULT}, ...
[ ON DUPLICATE KEY UPDATE ɢɦɹ_ɫɬɨɥɛɰɚ=ɜɵɪɚɠɟɧɢɟ, ... ]
218
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɢɥɢ:
INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] ɢɦɹ_ɬɚɛɥɢɰɵ [(ɢɦɹ_ɫɬɨɥɛɰɚ,...)]
SELECT ...
Ɉɩɟɪɚɬɨɪ INSERT ɜɫɬɚɜɥɹɟɬ ɧɨɜɵɟ ɫɬɪɨɤɢ ɜ ɫɭɳɟɫɬɜɭɸɳɭɸ ɬɚɛɥɢɰɭ. Ɏɨɪɦɵ
INSERT...VALUES ɢ INSERT...SET ɷɬɨɝɨ ɨɩɟɪɚɬɨɪɚ ɜɫɬɚɜɥɹɸɬ ɫɬɪɨɤɢ ɧɚ ɨɫɧɨɜɚɧɢɢ ɹɜɧɨ
ɭɤɚɡɚɧɧɵɯ ɡɧɚɱɟɧɢɣ. Ɏɨɪɦɚ INSERT...SELECT ɜɫɬɚɜɥɹɟɬ ɫɬɪɨɤɢ, ɜɵɛɢɪɚɹ ɢɯ ɢɡ ɞɪɭɝɨɣ
ɬɚɛɥɢɰɵ ɢɥɢ ɬɚɛɥɢɰ. Ɏɨɪɦɚ INSERT...VALUES ɫɨ ɦɧɨɝɢɦɢ ɫɩɢɫɤɚɦɢ ɡɧɚɱɟɧɢɣ ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ MySQL 3.22.10 ɢ ɛɨɥɟɟ ɩɨɡɞɧɢɦɢ ɜɟɪɫɢɹɦɢ. INSERT...SELECT ɨɩɢɫɵɜɚɟɬɫɹ ɞɚɥɟɟ
ɜ ɪɚɡɞɟɥɟ 6.1.4.1.
ɢɦɹ_ɬɚɛɥɢɰɵ – ɷɬɨ ɢɦɹ ɬɚɛɥɢɰɵ, ɜ ɤɨɬɨɪɭɸ ɧɭɠɧɨ ɜɫɬɚɜɢɬɶ ɫɬɪɨɤɢ. ɋɬɨɥɛɰɵ, ɤɨɬɨɪɵɦ ɨɩɟɪɚɬɨɪ ɩɪɢɫɜɚɢɜɚɟɬ ɡɧɚɱɟɧɢɹ, ɦɨɝɭɬ ɛɵɬɶ ɭɤɚɡɚɧɵ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
„ɋɩɢɫɨɤ ɢɦɟɧ ɫɬɨɥɛɰɨɜ ɜ ɤɨɧɫɬɪɭɤɰɢɢ SET ɡɚɞɚɟɬ ɢɯ ɹɜɧɨ.
ɫɩɢɫɨɤ ɫɬɨɥɛɰɨɜ ɧɟ ɭɤɚɡɚɧ ɞɥɹ INSERT...VALUES ɢɥɢ INSERT...SELECT, ɬɨ
ɡɧɚɱɟɧɢɹ ɞɥɹ ɜɫɟɯ ɫɬɨɥɛɰɨɜ ɬɚɛɥɢɰɵ ɞɨɥɠɧɵ ɛɵɬɶ ɩɪɟɞɫɬɚɜɥɟɧɵ ɜ ɫɩɢɫɤɟ
VALUES() ɢɥɢ ɫ ɩɨɦɨɳɶɸ SELECT. ȿɫɥɢ ɜɵ ɧɟ ɡɧɚɟɬɟ ɩɨɪɹɞɤɚ ɫɥɟɞɨɜɚɧɢɹ ɫɬɨɥɛɰɨɜ
ɜ ɬɚɛɥɢɰɟ, ɜɨɫɩɨɥɶɡɭɣɬɟɫɶ DESCRIBE ɢɦɹ_ɬɚɛɥɢɰɵ ɞɥɹ ɟɝɨ ɩɨɥɭɱɟɧɢɹ.
„ȿɫɥɢ
ɋɩɢɫɤɢ ɡɧɚɱɟɧɢɣ ɦɨɝɭɬ ɛɵɬɶ ɭɤɚɡɚɧɵ ɧɟɫɤɨɥɶɤɢɦɢ ɫɩɨɫɨɛɚɦɢ:
„Ʌɸɛɨɦɭ ɫɬɨɥɛɰɭ, ɞɥɹ ɤɨɬɨɪɨɝɨ ɧɟ ɭɤɚɡɚɧɨ ɡɧɚɱɟɧɢɟ ɹɜɧɨ, ɩɪɢɫɜɚɢɜɚɟɬɫɹ ɡɧɚɱɟɧɢɟ
ɩɨ ɭɦɨɥɱɚɧɢɸ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɩɪɢɜɟɞɟɧ ɫɩɢɫɨɤ ɫɬɨɥɛɰɨɜ, ɤɨɬɨɪɵɣ ɧɟ ɜɤɥɸɱɚɟɬ ɜ
ɫɟɛɹ ɜɫɟ ɫɬɨɥɛɰɵ ɬɚɛɥɢɰɵ, ɧɟɧɚɡɜɚɧɧɵɟ ɫɬɨɥɛɰɵ ɩɨɥɭɱɚɸɬ ɫɜɨɢ ɡɧɚɱɟɧɢɹ ɩɨ
ɭɦɨɥɱɚɧɢɸ. ɉɪɢɫɜɨɟɧɢɟ ɡɧɚɱɟɧɢɣ ɩɨ ɭɦɨɥɱɚɧɢɸ ɨɩɢɫɚɧɨ ɜ ɪɚɡɞɟɥɟ 6.2.5.
MySQL ɜɫɟɝɞɚ ɢɦɟɟɬ ɡɧɚɱɟɧɢɹ ɩɨ ɭɦɨɥɱɚɧɢɸ ɞɥɹ ɜɫɟɯ ɫɬɨɥɛɰɨɜ. ɗɬɨ ɩɪɨɞɢɤɬɨɜɚɧɨ ɧɟɨɛɯɨɞɢɦɨɫɬɶɸ ɞɥɹ MySQL ɪɚɛɨɬɚɬɶ ɤɚɤ ɫ ɬɪɚɧɡɚɤɰɢɨɧɧɵɦɢ, ɬɚɤ ɢ ɫ ɧɟɬɪɚɧɡɚɤɰɢɨɧɧɵɦɢ ɬɚɛɥɢɰɚɦɢ.
ɋ ɧɚɲɟɣ ɬɨɱɤɢ ɡɪɟɧɢɹ ɩɪɨɜɟɪɤɚ ɤɨɧɬɟɤɫɬɚ ɫɬɨɥɛɰɨɜ ɞɨɥɠɧɚ ɜɵɩɨɥɧɹɬɶɫɹ ɜ ɩɪɢɥɨɠɟɧɢɢ, ɚ ɧɟ ɧɚ ɫɟɪɜɟɪɟ ɛɚɡɵ ɞɚɧɧɵɯ.
oË ÑËÕÏÛµÂ!
ȿɫɥɢ ɜɵ ɯɨɬɢɬɟ ɡɚɫɬɚɜɢɬɶ ɨɩɟɪɚɬɨɪ INSERT ɝɟɧɟɪɢɪɨɜɚɬɶ ɨɲɢɛɤɭ, ɤɨɝɞɚ ɧɟ ɭɤɚɡɚɧɵ ɹɜɧɨ ɡɧɚɱɟɧɢɹ ɞɥɹ ɜɫɟɯ ɫɬɨɥɛɰɨɜ ɬɚɛɥɢɰɵ, ɬɪɟɛɭɸɳɢɯ ɧɟ-NULL ɡɧɚɱɟɧɢɣ, ɦɨɠɟɬɟ ɫɤɨɧɮɢɝɭɪɢɪɨɜɚɬɶ MySQL
ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɨɩɰɢɢ DONT_USE_DEFAULT_USE. ɗɬɨ ɜɨɡɦɨɠɧɨ, ɬɨɥɶɤɨ ɩɪɢ ɤɨɦɩɢɥɹɰɢɢ
MySQL ɢɡ ɢɫɯɨɞɧɵɯ ɬɟɤɫɬɨɜ.
ɦɨɠɟɬɟ ɜɨɫɩɨɥɶɡɨɜɚɬɶɫɹ ɤɥɸɱɟɜɵɦ ɫɥɨɜɨɦ DEFAULT, ɱɬɨɛɵ ɹɜɧɨ ɩɪɢɫɜɨɢɬɶ
ɫɬɨɥɛɰɭ ɡɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ (ɧɨɜɚɹ ɜɨɡɦɨɠɧɨɫɬɶ ɜ MySQL 4.0.3). ɗɬɨ ɭɩɪɨɳɚɟɬ ɧɚɩɢɫɚɧɢɟ ɨɩɟɪɚɬɨɪɨɜ INSERT, ɤɨɬɨɪɵɟ ɩɪɢɫɜɚɢɜɚɸɬ ɡɧɚɱɟɧɢɹ ɜɫɟɦ ɫɬɨɥɛɰɚɦ, ɤɪɨɦɟ ɧɟɤɨɬɨɪɵɯ, ɩɨɫɤɨɥɶɤɭ ɩɨɡɜɨɥɹɟɬ ɢɡɛɟɠɚɬɶ ɧɚɩɢɫɚɧɢɹ ɧɟɩɨɥɧɵɯ ɫɩɢɫɤɨɜ VALUES, ɤɨɬɨɪɵɟ ɧɟ ɜɤɥɸɱɚɸɬ ɜɫɟɯ ɫɬɨɥɛɰɨɜ ɬɚɛɥɢɰɵ. ɂɧɚɱɟ ɜɚɦ ɩɪɢɲɥɨɫɶ
ɛɵ ɩɢɫɚɬɶ ɫɩɢɫɨɤ ɢɦɟɧ ɫɬɨɥɛɰɨɜ, ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɯ ɤɚɠɞɨɦɭ ɷɥɟɦɟɧɬɭ ɜ ɫɩɢɫɤɟ
VALUES.
„ȼɵ
ɢ ɫɩɢɫɨɤ ɫɬɨɥɛɰɨɜ, ɢ ɫɩɢɫɨɤ ɡɧɚɱɟɧɢɣ VALUES ɩɭɫɬɵ, ɨɩɟɪɚɬɨɪ INSERT ɫɨɡɞɚɟɬ ɫɬɪɨɤɭ, ɜ ɤɨɬɨɪɨɣ ɜɫɟ ɫɬɨɥɛɰɵ ɩɪɢɧɢɦɚɸɬ ɡɧɚɱɟɧɢɹ ɩɨ ɭɦɨɥɱɚɧɢɸ.
„ȿɫɥɢ
mysql> INSERT INTO ɢɦɹ_ɬɚɛɥɢɰɵ () VALUES();
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
219
„ȼɵɪɚɠɟɧɢɟ ɜɵɪɚɠɟɧɢɟ
ɦɨɠɟɬ ɫɫɵɥɚɬɶɫɹ ɧɚ ɥɸɛɨɣ ɫɬɨɥɛɟɰ, ɤɨɬɨɪɵɣ ɭɩɨɦɹɧɭɬ ɪɚɧɟɟ ɜ ɫɩɢɫɤɟ ɡɧɚɱɟɧɢɣ. ɇɚɩɪɢɦɟɪ, ɜɵ ɦɨɠɟɬɟ ɞɟɥɚɬɶ ɷɬɨ ɩɨɬɨɦɭ, ɱɬɨ ɡɧɚɱɟɧɢɟ ɞɥɹ
col2 ɫɫɵɥɚɟɬɫɹ ɧɚ col1, ɡɧɚɱɟɧɢɟ ɤɨɬɨɪɨɝɨ ɭɠɟ ɭɫɬɚɧɨɜɥɟɧɨ:
mysql> INSERT INTO ɢɦɹ_ɬɚɛɥɢɰɵ (col1,col2) VALUES(15,col1*2);
Ɉɞɧɚɤɨ ɫɥɟɞɭɸɳɢɣ ɨɩɟɪɚɬɨɪ ɜɵɩɨɥɧɢɬɶ ɧɟɜɨɡɦɨɠɧɨ, ɬɚɤ ɤɚɤ col1 ɫɫɵɥɚɟɬɫɹ ɧɚ
col2, ɡɧɚɱɟɧɢɟ ɤɨɬɨɪɨɝɨ ɩɪɢɫɜɚɢɜɚɟɬɫɹ ɩɨɫɥɟ col1:
mysql> INSERT INTO ɢɦɹ_ɬɚɛɥɢɰɵ (col1,col2) VALUES(col2*2,15);
Ɉɩɟɪɚɬɨɪ INSERT ɩɨɞɞɟɪɠɢɜɚɟɬ ɫɥɟɞɭɸɳɢɟ ɦɨɞɢɮɢɤɚɬɨɪɵ:
ɜɵ ɭɤɚɡɵɜɚɟɬɟ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ DELAYED, ɫɟɪɜɟɪ ɩɨɦɟɳɚɟɬ ɫɬɪɨɤɭ ɢɥɢ ɫɬɪɨɤɢ,
ɤɨɬɨɪɵɟ ɩɨɞɥɟɠɚɬ ɜɫɬɚɜɤɟ, ɜ ɛɭɮɟɪ, ɢ ɤɥɢɟɧɬ, ɩɪɢɫɥɚɜɲɢɣ ɷɬɨɬ ɨɩɟɪɚɬɨɪ, ɦɨɠɟɬ
ɩɪɨɞɨɥɠɚɬɶ ɫɜɨɸ ɪɚɛɨɬɭ. ȿɫɥɢ ɬɚɛɥɢɰɚ ɡɚɧɹɬɚ, ɫɟɪɜɟɪ ɭɞɟɪɠɢɜɚɟɬ ɫɬɪɨɤɢ. Ʉɨɝɞɚ
ɬɚɛɥɢɰɚ ɨɫɜɨɛɨɞɢɬɫɹ, ɨɧ ɧɚɱɧɟɬ ɢɯ ɜɫɬɚɜɤɭ, ɩɪɨɜɟɪɹɹ ɩɟɪɢɨɞɢɱɟɫɤɢ, ɧɟɬ ɥɢ ɧɨɜɵɯ
ɡɚɩɪɨɫɨɜ ɧɚ ɱɬɟɧɢɟ ɷɬɨɣ ɬɚɛɥɢɰɵ. ȿɫɥɢ ɨɧɢ ɟɫɬɶ, ɨɛɫɥɭɠɢɜɚɧɢɟ ɨɱɟɪɟɞɢ ɨɬɥɨɠɟɧɧɵɯ ɫɬɪɨɤ ɞɥɹ ɜɫɬɚɜɤɢ ɩɪɢɨɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɬɚɛɥɢɰɚ ɧɟ ɨɫɜɨɛɨɞɢɬɫɹ ɜɧɨɜɶ. ɋɦ. ɪɚɡɞɟɥ 6.1.4.2.
„ȿɫɥɢ ɭɤɚɡɚɧɨ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ LOW_PRIORITY, ɜɵɩɨɥɧɟɧɢɟ ɜɫɬɚɜɤɢ ɨɬɤɥɚɞɵɜɚɟɬɫɹ
ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɜɫɟ ɞɪɭɝɢɟ ɤɥɢɟɧɬɵ ɧɟ ɡɚɜɟɪɲɚɬ ɱɬɟɧɢɟ ɬɚɛɥɢɰɵ. ɗɬɨ ɤɚɫɚɟɬɫɹ
ɬɚɤɠɟ ɤɥɢɟɧɬɨɜ, ɤɨɬɨɪɵɟ ɧɚɱɚɥɢ ɱɬɟɧɢɟ ɜ ɬɨ ɜɪɟɦɹ, ɤɨɝɞɚ ɫɭɳɟɫɬɜɭɸɳɢɟ ɤɥɢɟɧɬɵ
ɭɠɟ ɱɢɬɚɥɢ, ɢ ɬɟɯ, ɱɬɨ ɨɛɪɚɬɢɥɢɫɶ ɤ ɬɚɛɥɢɰɟ ɜɨ ɜɪɟɦɹ ɨɠɢɞɚɧɢɹ ɨɩɟɪɚɬɨɪɚ INSERT
LOW_PRIORITY. Ɍɚɤɢɦ ɨɛɪɚɡɨɦ, ɟɫɬɶ ɜɟɪɨɹɬɧɨɫɬɶ, ɱɬɨ ɤɥɢɟɧɬ, ɩɪɢɫɥɚɜɲɢɣ ɡɚɩɪɨɫ
INSERT LOW_PRIORITY, ɛɭɞɟɬ ɨɠɢɞɚɬɶ ɜɟɫɶɦɚ ɞɥɢɬɟɥɶɧɨɟ ɜɪɟɦɹ (ɢɥɢ ɞɚɠɟ ɛɟɫɤɨɧɟɱɧɨ ɞɨɥɝɨ) ɜ ɫɪɟɞɟ ɫ ɜɵɫɨɤɨɣ ɧɚɝɪɭɡɤɨɣ ɩɨ ɱɬɟɧɢɸ. (ɗɬɨ ɨɬɥɢɱɚɟɬɫɹ ɨɬ ɨɩɟɪɚɬɨɪɚ INSERT DELAYED, ɤɨɬɨɪɵɣ ɩɨɡɜɨɥɹɟɬ ɤɥɢɟɧɬɭ ɩɪɨɞɨɥɠɚɬɶ ɪɚɛɨɬɭ.) ɋɦ. ɪɚɡɞɟɥ
6.1.4.2. ɂɦɟɣɬɟ ɜ ɜɢɞɭ, ɱɬɨ LOW_PRIORITY ɨɛɵɱɧɨ ɧɟ ɞɨɥɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɫ ɬɚɛɥɢɰɚɦɢ MyISAM, ɩɨɫɤɨɥɶɤɭ ɷɬɨ ɩɪɟɩɹɬɫɬɜɭɟɬ ɩɚɪɚɥɥɟɥɶɧɵɦ ɜɫɬɚɜɤɚɦ.
„ȿɫɥɢ
ɭɤɚɡɚɧɨ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ IGNORE ɜ ɨɩɟɪɚɬɨɪɟ INSERT ɫɨ ɦɧɨɝɢɦɢ ɫɬɪɨɤɚɦɢ,
ɥɸɛɚɹ ɫɬɪɨɤɚ, ɜ ɤɨɬɨɪɨɣ ɞɭɛɥɢɪɭɟɬɫɹ ɡɧɚɱɟɧɢɟ ɫɬɨɥɛɰɨɜ ɭɧɢɤɚɥɶɧɨɝɨ ɢɧɞɟɤɫɚ ɢɥɢ
ɩɟɪɜɢɱɧɨɝɨ ɤɥɸɱɚ, ɢɝɧɨɪɢɪɭɟɬɫɹ ɢ ɧɟ ɜɫɬɚɜɥɹɟɬɫɹ. ȿɫɥɢ ɜɵ ɧɟ ɭɤɚɡɵɜɚɟɬɟ IGNORE,
ɨɩɟɪɚɰɢɹ ɜɫɬɚɜɤɢ ɩɪɟɪɵɜɚɟɬɫɹ ɩɪɢ ɨɛɧɚɪɭɠɟɧɢɢ ɞɭɛɥɢɪɨɜɚɧɧɵɯ ɫɬɪɨɤ. Ʉɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ, ɜɫɬɚɜɥɟɧɧɵɯ ɜ ɬɚɛɥɢɰɭ, ɦɨɠɧɨ ɨɩɪɟɞɟɥɢɬɶ ɜɵɡɨɜɨɦ ɮɭɧɤɰɢɢ C API
mysql_info().
„ȿɫɥɢ
ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ɤɨɧɫɬɪɭɤɰɢɸ ON DUPLICATE KEY UPDATE (ɧɨɜɚɹ ɜ MySQL 4.1.0),
ɢ ɜɫɬɚɜɥɹɟɬɫɹ ɫɬɪɨɤɚ ɫ ɞɭɛɥɢɪɨɜɚɧɧɵɦ ɡɧɚɱɟɧɢɟɦ ɤɥɸɱɚ ɭɧɢɤɚɥɶɧɨɝɨ ɢɧɞɟɤɫɚ ɢɥɢ ɩɟɪɜɢɱɧɨɝɨ ɤɥɸɱɚ, ɬɨ ɜɵɩɨɥɧɹɟɬɫɹ ɨɩɟɪɚɰɢɹ UPDATE ɫɬɚɪɨɣ ɫɬɪɨɤɢ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɫɬɨɥɛɟɰ
a ɨɛɴɹɜɥɟɧ ɤɚɤ UNIQUE ɢ ɭɠɟ ɫɨɞɟɪɠɢɬ ɡɧɚɱɟɧɢɟ 1, ɬɨ ɞɜɚ ɫɥɟɞɭɸɳɢɯ ɨɩɟɪɚɬɨɪɚ ɞɚɞɭɬ
ɨɞɢɧ ɢ ɬɨɬ ɠɟ ɷɮɮɟɤɬ:
mysql> INSERT INTO table (a,b,c) VALUES (1,2,3)
-> ON DUPLICATE KEY UPDATE c=c+1;
mysql> UPDATE table SET c=c+1 WHERE a=1;
oË ÑËÕÏÛµÂ!
ȿɫɥɢ ɫɬɨɥɛɟɰ b ɬɨɠɟ ɭɧɢɤɚɥɶɧɵɣ, ɬɨ INSERT ɛɭɞɟɬ ɷɤɜɢɜɚɥɟɧɬɟɧ ɬɚɤɨɦɭ ɨɩɟɪɚɬɨɪɭ UPDATE:
mysql> UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;
220
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ȿɫɥɢ ɭɫɥɨɜɢɸ a=1 OR b=2 ɫɨɨɬɜɟɬɫɬɜɭɸɬ ɧɟɫɤɨɥɶɤɨ ɫɬɪɨɤ, ɬɨ ɨɛɧɨɜɥɹɟɬɫɹ ɬɨɥɶɤɨ ɨɞɧɚ ɫɬɪɨɤɚ. ȼɨɨɛɳɟ ɜɚɦ ɫɥɟɞɭɟɬ ɢɡɛɟɝɚɬɶ ɩɪɢɦɟɧɟɧɢɹ ɤɨɧɫɬɪɭɤɰɢɢ ON DUPLICATE KEY ɫ
ɬɚɛɥɢɰɚɦɢ, ɭ ɤɨɬɨɪɵɯ ɧɟɫɤɨɥɶɤɨ ɭɧɢɤɚɥɶɧɵɯ ɤɥɸɱɟɣ.
ɇɚɱɢɧɚɹ ɫ MySQL 4.1.1, ɜ ɤɨɧɫɬɪɭɤɰɢɢ UPDATE ɜɵ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɮɭɧɤɰɢɸ
VALUES(ɢɦɹ_ɫɬɨɥɛɰɚ), ɱɬɨɛɵ ɫɨɫɥɚɬɶɫɹ ɧɚ ɡɧɚɱɟɧɢɹ ɫɬɨɥɛɰɨɜ ɢɡ ɱɚɫɬɢ INSERT ɨɩɟɪɚɬɨɪɚ
INSERT...UPDATE. Ⱦɪɭɝɢɦɢ ɫɥɨɜɚɦɢ, VALUES (ɢɦɹ_ɫɬɨɥɛɰɚ) ɜ ɤɨɧɫɬɪɭɤɰɢɢ UPDATE ɫɫɵɥɚɟɬɫɹ ɧɚ ɡɧɚɱɟɧɢɟ ɢɦɹ_ɫɬɨɥɛɰɚ, ɤɨɬɨɪɨɟ ɞɨɥɠɧɨ ɛɵɬɶ ɜɫɬɚɜɥɟɧɨ, ɟɫɥɢ ɧɟ ɨɛɧɚɪɭɠɢɬɫɹ
ɧɢɤɚɤɢɯ ɤɨɧɮɥɢɤɬɨɜ ɞɭɛɥɢɪɨɜɚɧɢɹ ɤɥɸɱɟɣ. ɗɬɚ ɮɭɧɤɰɢɹ ɨɫɨɛɟɧɧɨ ɭɞɨɛɧɚ ɩɪɢ ɦɧɨɝɨɫɬɪɨɱɧɵɯ ɜɫɬɚɜɤɚɯ. Ɏɭɧɤɰɢɹ VALUES ɢɦɟɟɬ ɫɦɵɫɥ ɬɨɥɶɤɨ ɜ ɨɩɟɪɚɬɨɪɟ INSERT...UPDATE, ɢ
ɜɨɡɜɪɚɳɚɟɬ NULL ɜɨ ɜɫɟɯ ɨɫɬɚɥɶɧɵɯ ɫɥɭɱɚɹɯ.
ɇɢɠɟ ɩɪɟɞɫɬɚɜɥɟɧ ɩɪɢɦɟɪ:
mysql> INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
-> ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);
ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɢɞɟɧɬɢɱɟɧ ɫɥɟɞɭɸɳɢɦ ɞɜɭɦ:
mysql>
->
mysql>
->
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=3;
INSERT INTO table (a,b,c) VALUES (4,5,6)
ON DUPLICATE KEY UPDATE c=9;
Ʉɨɝɞɚ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ON DUPLICATE KEY UPDATE, ɨɩɰɢɹ DELAYED ɢɝɧɨɪɢɪɭɟɬɫɹ.
ȼɵ ɦɨɠɟɬɟ ɩɨɥɭɱɢɬɶ ɡɧɚɱɟɧɢɟ, ɢɫɩɨɥɶɡɨɜɚɧɧɨɟ ɞɥɹ ɩɪɢɫɜɨɟɧɢɹ ɫɬɨɥɛɰɭ ɫ ɚɬɪɢɛɭɬɨɦ
AUTO_INCREMENT, ɨɛɪɚɬɢɜɲɢɫɶ ɤ ɮɭɧɤɰɢɢ LAST_INSERT_ID(). ȼ ɩɪɨɝɪɚɦɦɧɨɦ ɢɧɬɟɪɮɟɣɫɟ
C API ɞɥɹ ɷɬɨɝɨ ɩɪɟɞɭɫɦɨɬɪɟɧɚ ɮɭɧɤɰɢɹ mysql_insert_id(). Ɉɞɧɚɤɨ, ɩɨɦɧɢɬɟ, ɱɬɨ ɷɬɢ
ɞɜɟ ɮɭɧɤɰɢɢ ɜɟɞɭɬ ɫɟɛɹ ɧɟ ɜɨ ɜɫɟɯ ɫɥɭɱɚɹɯ ɨɞɢɧɚɤɨɜɨ. ɉɨɜɟɞɟɧɢɟ ɨɩɟɪɚɬɨɪɨɜ INSERT ɫɨ
ɫɬɨɥɛɰɚɦɢ AUTO_INCREMENT ɨɛɫɭɠɞɚɟɬɫɹ ɞɚɥɟɟ ɜ ɪɚɡɞɟɥɟ 5.8.3.
ȿɫɥɢ ɜɵ ɩɪɢɦɟɧɹɟɬɟ ɨɩɟɪɚɬɨɪ INSERT...VALUES ɫ ɦɧɨɠɟɫɬɜɟɧɧɵɦɢ ɫɩɢɫɤɚɦɢ ɡɧɚɱɟɧɢɣ ɢɥɢ INSERT...SELECT, ɷɬɨɬ ɨɩɟɪɚɬɨɪ ɜɨɡɜɪɚɳɚɟɬ ɢɧɮɨɪɦɚɰɢɨɧɧɭɸ ɫɬɪɨɤɭ ɜ ɫɥɟɞɭɸɳɟɦ ɮɨɪɦɚɬɟ:
Records: 100 Duplicates: 0 Warnings: 0
Records (ɡɚɩɢɫɢ) ɨɛɨɡɧɚɱɚɟɬ ɤɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ, ɨɛɪɚɛɨɬɚɧɧɵɯ ɨɩɟɪɚɬɨɪɨɦ. (ɗɬɨ ɧɟ
ɨɛɹɡɚɬɟɥɶɧɨ ɛɭɞɟɬ ɤɨɥɢɱɟɫɬɜɨ ɞɟɣɫɬɜɢɬɟɥɶɧɨ ɜɫɬɚɜɥɟɧɧɵɯ ɫɬɪɨɤ. Duplicates ɦɨɠɟɬ
ɛɵɬɶ ɧɟɧɭɥɟɜɵɦ.) Duplicates (ɞɭɛɥɢɤɚɬɵ) ɨɡɧɚɱɚɟɬ ɤɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ, ɤɨɬɨɪɵɟ ɧɟ ɦɨɝɭɬ
ɛɵɬɶ ɜɫɬɚɜɥɟɧɵ ɢɡ-ɡɚ ɬɨɝɨ, ɱɬɨ ɨɧɢ ɞɭɛɥɢɪɭɸɬ ɤɚɤɢɟ-ɬɨ ɭɧɢɤɚɥɶɧɵɟ ɡɧɚɱɟɧɢɹ ɢɧɞɟɤɫɨɜ.
Warnings (ɩɪɟɞɭɩɪɟɠɞɟɧɢɹ) ɨɡɧɚɱɚɟɬ ɤɨɥɢɱɟɫɬɜɨ ɩɨɩɵɬɨɤ ɜɫɬɚɜɤɢ ɡɧɚɱɟɧɢɣ ɫɬɨɥɛɰɨɜ,
ɤɨɬɨɪɵɟ ɨɤɚɡɚɥɢɫɶ ɩɨ ɤɚɤɢɦ-ɬɨ ɩɪɢɱɢɧɚɦ ɩɪɨɛɥɟɦɚɬɢɱɧɵɦɢ. ɉɪɟɞɭɩɪɟɠɞɟɧɢɹ ɦɨɝɭɬ
ɜɨɡɧɢɤɚɬɶ ɩɪɢ ɫɥɟɞɭɸɳɢɯ ɭɫɥɨɜɢɹɯ:
„ɉɪɢ ɩɨɩɵɬɤɟ ɜɫɬɚɜɢɬɶ NULL ɜ ɫɬɨɥɛɟɰ, ɨɛɴɹɜɥɟɧɧɵɣ ɤɚɤ NOT NULL. Ⱦɥɹ ɦɧɨɝɨɫɬɪɨɱɧɵɯ ɨɩɟɪɚɬɨɪɨɜ INSERT ɢɥɢ INSERT...SELECT ɬɚɤɢɦ ɫɬɨɥɛɰɚɦ ɩɪɢɫɜɚɢɜɚɸɬɫɹ ɡɧɚɱɟɧɢɹ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɜ ɫɨɨɬɜɟɬɫɬɜɢɢ ɫ ɢɯ ɬɢɩɨɦ. ɗɬɨ 0 ɞɥɹ ɱɢɫɥɨɜɵɯ ɬɢɩɨɜ, ɩɭɫɬɚɹ
ɫɬɪɨɤɚ ('') ɞɥɹ ɫɬɪɨɤɨɜɵɯ ɬɢɩɨɜ ɢ “ɧɭɥɟɜɵɟ” ɡɧɚɱɟɧɢɹ ɞɥɹ ɬɢɩɨɜ ɜɪɟɦɟɧɢ ɢ ɞɚɬɵ.
„ɉɪɢ ɩɪɢɫɜɨɟɧɢɢ ɱɢɫɥɨɜɨɦɭ ɫɬɨɥɛɰɭ ɡɧɚɱɟɧɢɹ, ɥɟɠɚɳɟɝɨ ɜɧɟ ɩɪɟɞɟɥɨɜ ɞɨɩɭɫɬɢɦɨɝɨ ɞɢɚɩɚɡɨɧɚ. Ɍɚɤɢɟ ɡɧɚɱɟɧɢɹ ɫɦɟɳɚɸɬɫɹ ɤ ɛɥɢɠɚɣɲɢɦ ɝɪɚɧɢɰɚɦ ɜ ɪɚɦɤɚɯ ɞɨɩɭɫɬɢɦɨɝɨ ɞɢɚɩɚɡɨɧɚ.
„ɉɪɢ ɩɪɢɫɜɨɟɧɢɢ ɡɧɚɱɟɧɢɹ ɜɪɨɞɟ '10.34 a' ɱɢɫɥɨɜɵɦ ɫɬɨɥɛɰɚɦ. Ɂɚɜɟɪɲɚɸɳɢɟ
ɧɟɰɢɮɪɨɜɵɟ ɫɢɦɜɨɥɵ ɨɬɛɪɚɫɵɜɚɸɬɫɹ, ɚ ɨɫɬɚɜɲɚɹɫɹ ɰɢɮɪɨɜɚɹ ɱɚɫɬɶ ɜɫɬɚɜɥɹɟɬɫɹ.
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
221
ȿɫɥɢ ɫɬɪɨɱɧɨɟ ɡɧɚɱɟɧɢɟ ɧɟ ɢɦɟɟɬ ɜɟɞɭɳɢɯ ɰɢɮɪɨɜɵɯ ɫɢɦɜɨɥɨɜ, ɬɨ ɫɬɨɥɛɰɭ ɩɪɢɫɜɚɢɜɚɟɬɫɹ 0.
ɜɫɬɚɜɤɟ ɫɢɦɜɨɥɶɧɵɯ ɡɧɚɱɟɧɢɣ ɜ ɫɬɪɨɤɨɜɵɟ ɫɬɨɥɛɰɵ (CHAR, VARCHAR, TEXT ɢɥɢ
BLOB), ɤɨɬɨɪɵɟ ɩɪɟɜɵɲɚɸɬ ɦɚɤɫɢɦɚɥɶɧɭɸ ɞɥɢɧɭ ɫɬɨɥɛɰɚ. Ɍɚɤɢɟ ɡɧɚɱɟɧɢɹ ɭɫɟɤɚ-
„ɉɪɢ
ɸɬɫɹ ɞɨ ɦɚɤɫɢɦɚɥɶɧɨɣ ɞɥɢɧɵ ɫɬɨɥɛɰɚ.
„ɉɪɢ ɜɫɬɚɜɤɟ ɜ ɫɬɨɥɛɟɰ ɞɚɬɵ ɢɥɢ ɜɪɟɦɟɧɢ ɡɧɚɱɟɧɢɹ, ɤɨɬɨɪɨɟ ɧɟɞɨɩɭɫɬɢɦɨ ɞɥɹ ɞɚɧɧɨɝɨ ɬɢɩɚ ɫɬɨɥɛɰɚ. ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɜ ɫɬɨɥɛɟɰ ɡɚɩɢɫɵɜɚɟɬɫɹ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɟ ɷɬɨɦɭ
ɬɢɩɭ ɧɭɥɟɜɨɟ ɡɧɚɱɟɧɢɟ.
ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ɩɪɨɝɪɚɦɦɧɵɣ ɢɧɬɟɪɮɟɣɫ C API, ɢɧɮɨɪɦɚɰɢɨɧɧɚɹ ɫɬɪɨɤɚ ɦɨɠɟɬ
ɛɵɬɶ ɩɨɥɭɱɟɧɚ ɫ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɢ mysql_info().
6.1.4.1. Ñèíòàêñèñ INSERT…SELECT
INSERT [LOW_PRIORITY] [IGNORE] [INTO] ɢɦɹ_ɬɚɛɥɢɰɵ [(ɫɩɢɫɨɤ_ɫɬɨɥɛɰɨɜ)]
SELECT ...
ɋ ɩɨɦɨɳɶɸ INSERT ... SELECT ɦɨɠɧɨ ɛɵɫɬɪɨ ɜɫɬɚɜɢɬɶ ɦɧɨɠɟɫɬɜɨ ɫɬɪɨɤ ɜ ɨɞɧɭ ɬɚɛɥɢɰɭ ɢɡ ɞɪɭɝɨɣ ɢɥɢ ɞɪɭɝɢɯ.
ɇɚɩɪɢɦɟɪ:
INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
ȼ ɨɬɧɨɲɟɧɢɢ ɨɩɟɪɚɬɨɪɚ INSERT...SELECT ɫɨɛɥɸɞɚɸɬɫɹ ɫɥɟɞɭɸɳɢɟ ɭɫɥɨɜɢɹ:
ɜɟɪɫɢɢ MySQL 4.0.1 INSERT...SELECT ɧɟɹɜɧɨ ɪɚɛɨɬɚɥ ɜ ɪɟɠɢɦɟ IGNORE. ɇɚɱɢɧɚɹ ɫ MySQL 4.0.1, ɫɥɟɞɭɟɬ ɹɜɧɨ ɭɤɚɡɵɜɚɬɶ IGNORE, ɱɬɨɛɵ ɢɝɧɨɪɢɪɨɜɚɬɶ ɫɬɪɨɤɢ,
ɤɨɬɨɪɵɟ ɧɚɪɭɲɚɸɬ ɭɫɥɨɜɢɹ ɭɧɢɤɚɥɶɧɨɫɬɢ ɤɥɸɱɟɣ.
„ɇɟ ɢɫɩɨɥɶɡɨɜɚɬɶ DELAYED ɫ INSERT...SELECT.
„Ⱦɨ
MySQL 4.0.14 ɰɟɥɟɜɚɹ ɬɚɛɥɢɰɚ ɨɩɟɪɚɬɨɪɚ INSERT ɧɟ ɦɨɝɥɚ ɜɫɬɪɟɱɚɬɶɫɹ ɜ ɤɨɧɫɬɪɭɤɰɢɢ FROM ɱɚɫɬɢ SELECT. ɗɬɨ ɨɝɪɚɧɢɱɟɧɢɟ ɫɧɹɬɨ ɜ ɜɟɪɫɢɢ 4.0.14.
„Ⱦɨ
„ɋɬɨɥɛɰɵ AUTO_INCREMENT
ɪɚɛɨɬɚɸɬ ɤɚɤ ɨɛɵɱɧɨ.
ɝɚɪɚɧɬɢɪɨɜɚɬɶ, ɱɬɨ ɛɢɧɚɪɧɵɣ ɠɭɪɧɚɥ ɫɦɨɠɟɬ ɛɵɬɶ ɢɫɩɨɥɶɡɨɜɚɧ ɞɥɹ ɩɟɪɟɫɨɡɞɚɧɢɹ ɨɪɢɝɢɧɚɥɶɧɵɯ ɬɚɛɥɢɰ, MySQL ɧɟ ɪɚɡɪɟɲɚɟɬ ɩɚɪɚɥɥɟɥɶɧɵɟ ɜɫɬɚɜɤɢ ɜɨ
ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ INSERT...SELECT.
„ɑɬɨɛɵ
ȼɵ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ REPLACE ɜɦɟɫɬɨ INSERT, ɱɬɨɛɵ ɩɟɪɟɡɚɩɢɫɵɜɚɬɶ ɫɬɚɪɵɟ ɫɬɪɨɤɢ. REPLACE – ɷɬɨ ɞɨɩɨɥɧɟɧɢɟ ɤ INSERT IGNORE ɩɪɢ ɨɛɪɚɛɨɬɤɟ ɧɨɜɵɯ ɫɬɪɨɤ, ɤɨɬɨɪɵɟ ɫɨɞɟɪɠɚɬ ɡɧɚɱɟɧɢɹ ɭɧɢɤɚɥɶɧɵɯ ɤɥɸɱɟɣ, ɞɭɛɥɢɪɭɸɳɢɯ ɫɬɚɪɵɟ ɫɬɪɨɤɢ. ɇɨɜɵɟ ɫɬɪɨɤɢ ɢɫɩɨɥɶɡɭɸɬɫɹ ɞɥɹ ɡɚɦɟɧɵ ɫɬɚɪɵɯ ɜɦɟɫɬɨ ɬɨɝɨ, ɱɬɨɛɵ ɩɪɨɫɬɨ ɨɬɜɟɪɝɚɬɶɫɹ.
6.1.4.2. Ñèíòàêñèñ INSERT DELAYED
INSERT DELAYED ...
Ɉɩɰɢɹ DELAYED ɨɩɟɪɚɬɨɪɚ INSERT – ɷɬɨ ɪɚɫɲɢɪɟɧɢɟ MySQL ɫɬɚɧɞɚɪɬɚ SQL, ɤɨɬɨɪɨɟ
ɨɱɟɧɶ ɭɞɨɛɧɨ, ɟɫɥɢ ɭ ɜɚɫ ɟɫɬɶ ɤɥɢɟɧɬɵ, ɤɨɬɨɪɵɟ ɧɟ ɦɨɝɭɬ ɠɞɚɬɶ ɡɚɜɟɪɲɟɧɢɹ ɜɵɩɨɥɧɟɧɢɹ
INSERT. ɗɬɨ ɨɛɳɚɹ ɩɪɨɛɥɟɦɚ, ɤɨɝɞɚ MySQL ɢɫɩɨɥɶɡɭɟɬɫɹ ɞɥɹ ɪɟɝɢɫɬɪɚɰɢɢ ɜ ɠɭɪɧɚɥɟ ɫ
ɩɚɪɚɥɥɟɥɶɧɵɦ ɩɟɪɢɨɞɢɱɟɫɤɢɦ ɜɵɩɨɥɧɟɧɢɟɦ ɨɩɟɪɚɬɨɪɨɜ SELECT ɢ UPDATE, ɤɨɬɨɪɵɟ ɪɚɛɨɬɚɸɬ ɩɨɞɨɥɝɭ. DELAYED ɛɵɥɨ ɩɪɟɞɫɬɚɜɥɟɧɨ ɜ MySQL 3.22.15.
222
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
Ʉɨɝɞɚ ɤɥɢɟɧɬ ɩɪɢɦɟɧɹɟɬ INSERT DELAYED, ɨɧ ɩɨɥɭɱɚɟɬ ɨɬ ɫɟɪɜɟɪɚ ɩɨɞɬɜɟɪɠɞɟɧɢɟ ɫɪɚɡɭ, ɚ ɜɫɬɚɜɥɹɟɦɚɹ ɫɬɪɨɤɚ ɫɬɚɧɨɜɢɬɫɹ ɜ ɨɱɟɪɟɞɶ ɫ ɬɟɦ, ɱɬɨɛɵ ɪɟɚɥɶɧɨ ɞɨɛɚɜɢɬɶɫɹ ɜ ɬɚɛɥɢɰɭ, ɤɨɝɞɚ ɨɧɚ ɧɟ ɛɭɞɟɬ ɡɚɧɹɬɚ ɞɪɭɝɢɦɢ ɩɨɬɨɤɚɦɢ.
Ⱦɪɭɝɚɹ ɫɭɳɟɫɬɜɟɧɧɚɹ ɜɵɝɨɞɚ ɢɫɩɨɥɶɡɨɜɚɧɢɹ INSERT DELAYED ɫɨɫɬɨɢɬ ɜ ɬɨɦ, ɱɬɨ
ɜɫɬɚɜɤɢ ɨɬ ɦɧɨɝɢɯ ɤɥɢɟɧɬɨɜ ɫɜɹɡɵɜɚɸɬɫɹ ɜɦɟɫɬɟ ɢ ɡɚɩɢɫɵɜɚɸɬɫɹ ɜ ɨɞɢɧ ɛɥɨɤ. ɗɬɨ ɧɚɦɧɨɝɨ ɛɵɫɬɪɟɟ, ɱɟɦ ɜɵɩɨɥɧɹɬɶ ɦɧɨɠɟɫɬɜɨ ɨɬɞɟɥɶɧɵɯ ɜɫɬɚɜɨɤ.
ɋɭɳɟɫɬɜɭɸɬ ɧɟɤɨɬɨɪɵɟ ɨɝɪɚɧɢɱɟɧɢɹ ɩɪɢ ɢɫɩɨɥɶɡɨɜɚɧɢɢ DELAYED:
ɪɚɛɨɬɚɟɬ ɬɨɥɶɤɨ ɫ ɬɚɛɥɢɰɚɦɢ MyISAM ɢ ISAM. Ⱦɥɹ ɬɚɛɥɢɰ MyISAM
ɟɫɥɢ ɧɟɬ ɫɜɨɛɨɞɧɵɯ ɛɥɨɤɨɜ ɜ ɫɟɪɟɞɢɧɟ ɮɚɣɥɚ ɞɚɧɧɵɯ, ɬɨ ɩɨɞɞɟɪɠɢɜɚɸɬɫɹ ɩɚɪɚɥɥɟɥɶɧɵɟ ɨɩɟɪɚɬɨɪɵ INSERT ɢ SELECT. ȼ ɬɚɤɢɯ ɭɫɥɨɜɢɹɯ ɜɚɦ ɨɱɟɧɶ ɪɟɞɤɨ ɩɨɧɚɞɨɛɢɬɫɹ ɢɫɩɨɥɶɡɨɜɚɬɶ INSERT DELAYED ɫ ɬɚɛɥɢɰɚɦɢ MyISAM.
„INSERT DELAYED
ɞɨɥɠɟɧ ɩɪɢɦɟɧɹɬɶɫɹ ɬɨɥɶɤɨ ɫ ɨɩɟɪɚɬɨɪɚɦɢ INSERT, ɜ ɤɨɬɨɪɵɯ
ɭɤɚɡɚɧɵ ɫɩɢɫɤɢ ɡɧɚɱɟɧɢɣ. ɗɬɨ ɬɪɟɛɨɜɚɧɢɟ ɜɜɟɞɟɧɨ ɜ MySQL 4.0.18. ɋɟɪɜɟɪ ɢɝɧɨɪɢɪɭɟɬ DELAYED ɞɥɹ INSERT DELAYED...SELECT.
„INSERT DELAYED
„ɋɟɪɜɟɪ ɢɝɧɨɪɢɪɭɟɬ DELAYED ɜ ɨɩɟɪɚɬɨɪɚɯ INSERT DELAYED...ON DUPLICATE UPDATES.
„ɉɨɫɤɨɥɶɤɭ
ɨɩɟɪɚɬɨɪ ɜɨɡɜɪɚɳɚɟɬ ɭɩɪɚɜɥɟɧɢɟ ɧɟɦɟɞɥɟɧɧɨ, ɞɨ ɬɨɝɨ ɤɚɤ ɫɬɪɨɤɢ ɮɚɤɬɢɱɟɫɤɢ ɛɭɞɭɬ ɜɫɬɚɜɥɟɧɵ, ɜɵ ɧɟ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ LAST_INSERT_ID() ɞɥɹ ɩɨɥɭɱɟɧɢɹ ɩɨɫɥɟɞɧɟɝɨ ɡɧɚɱɟɧɢɹ AUTO_INCREMENT, ɤɨɬɨɪɨɟ ɦɨɠɟɬ ɛɵɬɶ ɫɝɟɧɟɪɢɪɨɜɚɧɨ
ɨɩɟɪɚɬɨɪɨɦ.
„ɋɬɪɨɤɢ DELAYED ɧɟɜɢɞɢɦɵ ɞɥɹ SELECT ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɨɧɢ ɞɟɣɫɬɜɢɬɟɥɶɧɨ ɧɟ ɛɭɞɭɬ ɜɫɬɚɜɥɟɧɵ ɜ ɬɚɛɥɢɰɭ.
ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɩɨɫɬɚɜɥɟɧɧɵɟ ɜ ɨɱɟɪɟɞɶ ɡɚɩɢɫɢ ɯɪɚɧɹɬɫɹ ɬɨɥɶɤɨ ɜ ɩɚɦɹɬɢ ɞɨ
ɬɨɝɨ, ɤɚɤ ɛɭɞɭɬ ɜɫɬɚɜɥɟɧɵ ɜ ɬɚɛɥɢɰɭ. ɗɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɟɫɥɢ ɜɵ ɩɪɟɪɜɟɬɟ ɪɚɛɨɬɭ mysqld
ɩɪɢɧɭɞɢɬɟɥɶɧɨ (ɧɚɩɪɢɦɟɪ, ɤɨɦɚɧɞɨɣ kill -9), ɥɢɛɨ ɨɧ ɡɚɜɟɪɲɢɬ ɪɚɛɨɬɭ ɚɜɚɪɢɣɧɨ, ɬɨ
ɜɫɟ ɡɚɩɢɫɢ, ɩɨɫɬɚɜɥɟɧɧɵɟ ɜ ɨɱɟɪɟɞɶ, ɛɭɞɭɬ ɭɬɟɪɹɧɵ!
ɇɢɠɟ ɩɪɢɜɟɞɟɧɨ ɞɟɬɚɥɶɧɨɟ ɨɩɢɫɚɧɢɟ ɬɨɝɨ, ɱɬɨ ɩɪɨɢɫɯɨɞɢɬ, ɟɫɥɢ ɩɪɢɦɟɧɹɬɶ ɨɩɰɢɸ
DELAYED ɤ ɨɩɟɪɚɬɨɪɚɦ INSERT ɢɥɢ REPLACE. ȼ ɷɬɨɦ ɨɩɢɫɚɧɢɢ ɩɨɞ “ɩɨɬɨɤɨɦ” ɢɦɟɟɬɫɹ ɜ
ɜɢɞɭ ɩɨɬɨɤ ɫɟɪɜɟɪɚ, ɩɪɢɧɢɦɚɸɳɢɣ ɨɩɟɪɚɬɨɪ INSERT DELAYED, ɚ “ɨɛɪɚɛɨɬɱɢɤ” (handler) –
ɷɬɨ ɩɨɬɨɤ, ɤɨɬɨɪɵɣ ɨɛɪɚɛɚɬɵɜɚɟɬ ɜɫɟ ɨɩɟɪɚɬɨɪɵ INSERT DELAYED ɞɥɹ ɨɬɞɟɥɶɧɨɣ ɬɚɛɥɢɰɵ.
ɩɨɬɨɤ ɜɵɩɨɥɧɹɟɬ DELAYED-ɨɩɟɪɚɬɨɪ ɧɚ ɤɨɧɤɪɟɬɧɨɣ ɬɚɛɥɢɰɟ, ɫɨɡɞɚɟɬɫɹ ɩɨɬɨɤ
ɨɛɪɚɛɨɬɱɢɤɚ ɞɥɹ ɜɵɩɨɥɧɟɧɢɹ ɜɫɟɯ ɷɬɢɯ ɨɬɥɨɠɟɧɧɵɯ ɨɩɟɪɚɬɨɪɨɜ ɞɥɹ ɬɚɛɥɢɰɵ, ɟɫɥɢ ɬɨɥɶɤɨ ɨɧɚ ɟɳɟ ɧɟ ɫɭɳɟɫɬɜɨɜɚɥɚ ɧɚ ɷɬɨɬ ɦɨɦɟɧɬ.
„ɉɨɬɨɤ ɩɪɨɜɟɪɹɟɬ, ɩɨɥɭɱɢɥ ɥɢ ɨɛɪɚɛɨɬɱɢɤ DELAYED-ɛɥɨɤɢɪɨɜɤɭ. ȿɫɥɢ ɧɟɬ, ɨɧ ɞɚɟɬ
ɤɨɦɚɧɞɭ ɩɨɬɨɤɭ ɨɛɪɚɛɨɬɱɢɤɚ ɫɞɟɥɚɬɶ ɷɬɨ. DELAYED-ɛɥɨɤɢɪɨɜɤɚ ɦɨɠɟɬ ɛɵɬɶ ɩɨɥɭɱɟɧɚ, ɞɚɠɟ ɟɫɥɢ ɞɪɭɝɢɟ ɩɨɬɨɤɢ ɢɦɟɸɬ ɧɚ ɷɬɨɣ ɬɚɛɥɢɰɟ ɛɥɨɤɢɪɨɜɤɭ ɱɬɟɧɢɹ ɢɥɢ ɡɚɩɢɫɢ. Ɉɞɧɚɤɨ ɨɛɪɚɛɨɬɱɢɤ ɛɭɞɟɬ ɨɠɢɞɚɬɶ ɛɥɨɤɢɪɨɜɨɤ ALTER TABLE ɢɥɢ FLUSH
TABLES, ɱɬɨɛɵ ɝɚɪɚɧɬɢɪɨɜɚɬɶ, ɱɬɨ ɫɬɪɭɤɬɭɪɚ ɬɚɛɥɢɰɵ ɫɢɧɯɪɨɧɢɡɢɪɨɜɚɧɚ.
„Ʉɨɝɞɚ
ɜɵɩɨɥɧɹɟɬ ɨɩɟɪɚɬɨɪ INSERT, ɧɨ ɜɦɟɫɬɨ ɡɚɩɢɫɢ ɫɬɪɨɤɢ ɜ ɬɚɛɥɢɰɭ ɩɨɦɟɳɚɟɬ ɟɟ
ɤɨɩɢɸ ɜ ɨɱɟɪɟɞɶ ɜ ɩɚɦɹɬɢ, ɤɨɬɨɪɚɹ ɭɩɪɚɜɥɹɟɬɫɹ ɩɨɬɨɤɨɦ ɨɛɪɚɛɨɬɱɢɤɚ. Ʌɸɛɵɟ ɫɢɧɬɚɤɫɢɱɟɫɤɢɟ ɨɲɢɛɤɢ ɩɨɬɨɤɨɦ ɨɬɦɟɱɚɸɬɫɹ ɢ ɫɨɨɛɳɚɸɬɫɹ ɤɥɢɟɧɬɫɤɨɣ ɩɪɨɝɪɚɦɦɟ.
„Ʉɥɢɟɧɬ ɧɟ ɦɨɠɟɬ ɩɨɥɭɱɢɬɶ ɨɬ ɫɟɪɜɟɪɚ ɤɨɥɢɱɟɫɬɜɨ ɞɭɛɥɢɪɨɜɚɧɧɵɯ ɫɬɪɨɤ ɢɥɢ ɡɧɚɱɟɧɢɟ AUTO_INCREMENT ɪɟɡɭɥɶɬɢɪɭɸɳɟɣ ɫɬɪɨɤɢ, ɩɨɬɨɦɭ ɱɬɨ INSERT ɜɨɡɜɪɚɳɚɟɬ
ɭɩɪɚɜɥɟɧɢɟ ɩɪɟɠɞɟ, ɱɟɦ ɜɫɬɚɜɤɚ ɫɬɪɨɤɢ ɛɭɞɟɬ ɜɵɩɨɥɧɟɧɚ. (ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ
„ɉɨɬɨɤ
223
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
ɩɪɨɝɪɚɦɦɧɵɣ ɢɧɬɟɪɮɟɣɫ C API, ɩɨ ɬɨɣ ɠɟ ɩɪɢɱɢɧɟ ɮɭɧɤɰɢɹ mysql_info() ɧɟ ɜɟɪɧɟɬ ɧɢɱɟɝɨ ɜɪɚɡɭɦɢɬɟɥɶɧɨɝɨ.)
„Ȼɢɧɚɪɧɵɣ ɠɭɪɧɚɥ ɨɛɧɨɜɥɹɟɬɫɹ ɩɨɬɨɤɨɦ ɨɛɪɚɛɨɬɱɢɤɚ, ɤɨɝɞɚ ɫɬɪɨɤɚ ɜɫɬɚɜɥɹɟɬɫɹ ɜ
ɬɚɛɥɢɰɭ. ȼ ɫɥɭɱɚɟ ɦɧɨɝɨɫɬɪɨɱɧɨɣ ɜɫɬɚɜɤɢ ɛɢɧɚɪɧɵɣ ɠɭɪɧɚɥ ɨɛɧɨɜɥɹɟɬɫɹ, ɤɨɝɞɚ
ɜɫɬɚɜɥɹɟɬɫɹ ɩɟɪɜɚɹ ɫɬɪɨɤɚ.
„ɉɨɫɥɟ ɬɨɝɨ, ɤɚɤ delayed_insert_limit ɫɬɪɨɤ ɜɫɬɚɜɥɟɧɨ, ɨɛɪɚɛɨɬɱɢɤ ɩɪɨɜɟɪɹɟɬ,
ɧɟɬ ɥɢ ɨɠɢɞɚɸɳɢɯ ɨɩɟɪɚɬɨɪɨɜ SELECT. ȿɫɥɢ ɟɫɬɶ, ɨɧ ɩɨɡɜɨɥɹɟɬ ɢɦ ɜɵɩɨɥɧɢɬɶɫɹ,
ɩɪɟɠɞɟ ɱɟɦ ɩɪɨɞɨɥɠɢɬ ɫɜɨɸ ɪɚɛɨɬɭ.
„Ʉɨɝɞɚ ɨɛɪɚɛɨɬɱɢɤ ɛɨɥɶɲɟ ɧɟ ɢɦɟɟɬ ɫɬɪɨɤ ɜ ɨɱɟɪɟɞɢ, ɛɥɨɤɢɪɨɜɤɚ ɫ ɬɚɛɥɢɰɵ ɫɧɢɦɚɟɬɫɹ. ȿɫɥɢ ɛɨɥɶɲɟ ɧɢɤɚɤɢɯ ɧɨɜɵɯ ɨɩɟɪɚɬɨɪɨɜ INSERT DELAYED ɧɟ ɩɨɥɭɱɟɧɨ ɜ ɬɟɱɟɧɢɟ delayed_insert_timeout ɫɟɤɭɧɞ, ɬɨ ɩɨɬɨɤ ɨɛɪɚɛɨɬɱɢɤɚ ɩɪɟɪɵɜɚɟɬɫɹ.
ɜ ɤɨɧɤɪɟɬɧɨɣ ɨɱɟɪɟɞɢ ɧɚɤɚɩɥɢɜɚɟɬɫɹ ɛɨɥɟɟ delayed_queue_size ɫɬɪɨɤ, ɬɨ ɩɨɬɨɤ, ɤɨɬɨɪɵɣ ɡɚɩɪɚɲɢɜɚɟɬ ɜɵɩɨɥɧɟɧɢɟ ɨɱɟɪɟɞɧɨɝɨ ɨɩɟɪɚɬɨɪɚ INSERT DELAYED, ɨɠɢɞɚɟɬ, ɩɨɤɚ ɧɟ ɨɫɜɨɛɨɞɢɬɫɹ ɦɟɫɬɨ ɜ ɨɱɟɪɟɞɢ. ɗɬɨ ɞɟɥɚɟɬɫɹ ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɝɚɪɚɧɬɢɪɨɜɚɬɶ, ɱɬɨ ɫɟɪɜɟɪ mysqld ɧɟ ɡɚɯɜɚɬɢɬ ɜɫɸ ɫɜɨɛɨɞɧɭɸ ɩɚɦɹɬɶ ɩɨɞ ɷɬɭ ɨɱɟɪɟɞɶ.
„ɉɨɬɨɤ ɨɛɪɚɛɨɬɱɢɤɚ ɩɨɤɚɡɵɜɚɟɬɫɹ ɜ ɫɩɢɫɤɟ ɩɪɨɰɟɫɫɨɜ MySQL ɫɨ ɡɧɚɱɟɧɢɟɦ ɫɬɨɥɛɰɚ Command, ɪɚɜɧɵɦ delayed_insert. Ɉɧ ɛɭɞɟɬ ɩɪɟɪɜɚɧ, ɟɫɥɢ ɜɵɩɨɥɧɢɬɶ ɨɩɟɪɚɬɨɪ
FLUSH TABLES ɢɥɢ ɩɪɟɪɜɚɬɶ ɟɝɨ ɩɨɫɪɟɞɫɬɜɨɦ KILL ɢɞɟɧɬɢɮɢɤɚɬɨɪ_ɩɨɬɨɤɚ. Ɉɞɧɚɤɨ
ɩɟɪɟɞ ɜɵɯɨɞɨɦ ɩɨɬɨɤ ɫɧɚɱɚɥɚ ɡɚɩɢɲɟɬ ɜɫɟ ɫɬɪɨɤɢ, ɧɚɯɨɞɹɳɢɟɫɹ ɜ ɨɱɟɪɟɞɢ, ɜ ɬɚɛɥɢɰɭ. ȼ ɷɬɨ ɜɪɟɦɹ ɨɧ ɧɟ ɛɭɞɟɬ ɩɪɢɧɢɦɚɬɶ ɧɢɤɚɤɢɯ ɨɩɟɪɚɬɨɪɨɜ INSERT ɨɬ ɞɪɭɝɢɯ
ɩɨɬɨɤɨɜ. ȿɫɥɢ ɜɵɞɚɬɶ ɨɩɟɪɚɬɨɪ INSERT DELAYED ɩɨɫɥɟ ɷɬɨɝɨ, ɛɭɞɟɬ ɫɨɡɞɚɧ ɧɨɜɵɣ
ɩɨɬɨɤ ɨɛɪɚɛɨɬɱɢɤɚ.
ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɷɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɨɩɟɪɚɬɨɪɵ INSERT DELAYED ɢɦɟɸɬ ɛɨɥɟɟ
ɜɵɫɨɤɢɣ ɩɪɢɨɪɢɬɟɬ, ɱɟɦ ɧɨɪɦɚɥɶɧɵɟ ɨɩɟɪɚɬɨɪɵ INSERT, ɟɫɥɢ ɫɭɳɟɫɬɜɭɟɬ ɪɚɛɨɬɚɸɳɢɣ ɩɨɬɨɤ ɨɛɪɚɛɨɬɱɢɤɚ INSERT DELAYED. ȼɫɟ ɨɫɬɚɥɶɧɵɟ ɨɩɟɪɚɬɨɪɵ ɨɛɧɨɜɥɟɧɢɹ ɞɚɧɧɵɯ ɞɨɥɠɧɵ ɛɭɞɭɬ ɨɠɢɞɚɬɶ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɨɱɟɪɟɞɶ INSERT DELAYED ɧɟ
ɨɱɢɫɬɢɬɫɹ, ɥɢɛɨ ɤɬɨ-ɧɢɛɭɞɶ ɧɟ ɩɪɟɪɜɟɬ ɩɨɬɨɤ ɨɛɪɚɛɨɬɱɢɤɚ (ɫ ɩɨɦɨɳɶɸ kill
ɢɞɟɧɬɢɮɢɤɚɬɨɪ_ɩɨɬɨɤɚ), ɥɢɛɨ ɠɟ ɛɭɞɟɬ ɜɵɩɨɥɧɟɧ ɨɩɟɪɚɬɨɪ FLUSH TABLES.
„ȿɫɥɢ
„ɋɥɟɞɭɸɳɢɟ
ɩɟɪɟɦɟɧɧɵɟ ɫɨɫɬɨɹɧɢɹ ɩɪɟɞɨɫɬɚɜɥɹɸɬ ɢɧɮɨɪɦɚɰɢɸ ɨɛ ɨɩɟɪɚɬɨɪɚɯ
INSERT DELAYED:
ɉɟɪɟɦɟɧɧɚɹ ɫɨɫɬɨɹɧɢɹ
Ɂɧɚɱɟɧɢɟ
Delayed_insert_threads
Ʉɨɥɢɱɟɫɬɜɨ ɩɨɬɨɤɨɜ ɨɛɪɚɛɨɬɱɢɤɨɜ.
Delayed_writes
Ʉɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ, ɡɚɩɢɫɚɧɧɵɯ INSERT DELAYED.
Not_flushed_delayed_rows
Ʉɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ, ɨɠɢɞɚɸɳɢɯ ɡɚɩɢɫɢ.
ȼɵ ɦɨɠɟɬɟ ɩɪɨɫɦɨɬɪɟɬɶ ɷɬɢ ɩɟɪɟɦɟɧɧɵɟ, ɜɵɩɨɥɧɢɜ ɨɩɟɪɚɬɨɪ SHOW STATUS ɥɢɛɨ
ɤɨɦɚɧɞɭ mysqladmin extend-status.
Ɉɩɟɪɚɬɨɪ INSERT DELAYED ɦɟɞɥɟɧɧɟɟ, ɱɟɦ ɨɛɵɱɧɵɣ INSERT, ɟɫɥɢ ɬɚɛɥɢɰɚ ɧɟ ɢɫɩɨɥɶɡɭɟɬɫɹ. ɉɨɞɞɟɪɠɢɜɚɬɶ ɨɬɞɟɥɶɧɵɣ ɩɨɬɨɤ ɞɥɹ ɤɚɠɞɨɣ ɬɚɛɥɢɰɵ, ɞɥɹ ɤɨɬɨɪɨɣ ɫɭɳɟɫɬɜɭɸɬ
ɨɬɥɨɠɟɧɧɵɟ ɨɩɟɪɚɬɨɪɵ ɜɫɬɚɜɤɢ – ɷɬɨ ɫɨɡɞɚɟɬ ɞɨɩɨɥɧɢɬɟɥɶɧɭɸ ɧɚɝɪɭɡɤɭ ɧɚ ɫɟɪɜɟɪ. ɗɬɨ
ɡɧɚɱɢɬ, ɱɬɨ ɜɵ ɞɨɥɠɧɵ ɩɪɢɦɟɧɹɬɶ INSERT DELAYED ɬɨɥɶɤɨ ɜ ɫɥɭɱɚɹɯ, ɤɨɝɞɚ ɜɵ ɞɟɣɫɬɜɢɬɟɥɶɧɨ ɭɜɟɪɟɧɵ, ɱɬɨ ɷɬɨ ɧɟɨɛɯɨɞɢɦɨ!
224
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
6.1.5. Ñèíòàêñèñ LOAD DATA INFILE
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'ɢɦɹ_ɮɚɣɥɚ.txt'
[REPLACE | IGNORE]
INTO TABLE ɢɦɹ_ɬɚɛɥɢɰɵ
[FIELDS
[TERMINATED BY '\t']
[[OPTIONALLY] ENCLOSED BY '']
[ESCAPED BY '\\']
]
[LINES
[STARTING BY '']
[TERMINATED BY '\n']
]
[IGNORE ɤɨɥɢɱɟɫɬɜɨ LINES]
[(ɢɦɹ_ɫɬɨɥɛɰɚ,...)]
Ɉɩɟɪɚɬɨɪ LOAD DATA INFILE ɱɢɬɚɟɬ ɫɬɪɨɤɢ ɢɡ ɬɟɤɫɬɨɜɨɝɨ ɮɚɣɥɚ ɢ ɡɚɝɪɭɠɚɟɬ ɢɯ ɜ ɬɚɛɥɢɰɭ ɧɚ ɨɱɟɧɶ ɜɵɫɨɤɨɣ ɫɤɨɪɨɫɬɢ.
ȼɵ ɦɨɠɟɬɟ ɬɚɤɠɟ ɡɚɝɪɭɠɚɬɶ ɮɚɣɥɵ ɞɚɧɧɵɯ ɫ ɩɨɦɨɳɶɸ ɭɬɢɥɢɬɵ mysqlimport. Ɉɧɚ
ɪɚɛɨɬɚɟɬ, ɩɨɫɵɥɚɹ ɧɚ ɫɟɪɜɟɪ ɨɩɟɪɚɬɨɪ LOAD DATA INFILE. Ɉɩɰɢɹ --local ɡɚɫɬɚɜɥɹɟɬ ɭɬɢɥɢɬɭ mysqlimport ɱɢɬɚɬɶ ɮɚɣɥ ɞɚɧɧɵɯ ɫ ɤɥɢɟɧɬɫɤɨɝɨ ɯɨɫɬɚ. ȼɵ ɦɨɠɟɬɟ ɭɤɚɡɚɬɶ ɨɩɰɢɸ
--compress ɞɥɹ ɩɨɜɵɲɟɧɢɹ ɩɪɨɢɡɜɨɞɢɬɟɥɶɧɨɫɬɢ ɜ ɦɟɞɥɟɧɧɵɯ ɫɟɬɹɯ, ɟɫɥɢ ɤɥɢɟɧɬ ɢ ɫɟɪɜɟɪ ɩɨɞɞɟɪɠɢɜɚɸɬ ɫɠɚɬɵɣ ɩɪɨɬɨɤɨɥ.
ȿɫɥɢ ɭɤɚɡɚɧɨ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ LOW_PRIORITY, ɜɵɩɨɥɧɟɧɢɟ ɨɩɟɪɚɬɨɪɚ LOAD DATA ɨɬɤɥɚɞɵɜɚɟɬɫɹ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɜɫɟ ɨɫɬɚɥɶɧɵɟ ɤɥɢɟɧɬɵ ɧɟ ɡɚɜɟɪɲɚɬ ɱɬɟɧɢɟ.
ȿɫɥɢ ɭɤɚɡɚɧɨ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ CONCURRENT ɫ ɬɚɛɥɢɰɟɣ MyISAM, ɤɨɬɨɪɚɹ ɭɞɨɜɥɟɬɜɨɪɹɟɬ
ɭɫɥɨɜɢɸ ɩɚɪɚɥɥɟɥɶɧɵɯ ɜɫɬɚɜɨɤ (ɬɨ ɟɫɬɶ ɧɟ ɢɦɟɟɬ ɫɜɨɛɨɞɧɵɯ ɛɥɨɤɨɜ ɜ ɫɟɪɟɞɢɧɟ ɮɚɣɥɚ),
ɬɨ ɞɪɭɝɢɟ ɩɨɬɨɤɢ ɫɦɨɝɭɬ ɢɡɜɥɟɤɚɬɶ ɞɚɧɧɵɟ ɢɡ ɬɚɛɥɢɰɵ ɨɞɧɨɜɪɟɦɟɧɧɨ ɫ ɜɵɩɨɥɧɟɧɢɟɦ
LOAD DATA. ɉɪɢɦɟɧɟɧɢɟ ɷɬɨɣ ɨɩɰɢɢ ɧɟɦɧɨɝɨ ɫɤɚɡɵɜɚɟɬɫɹ ɧɚ ɩɪɨɢɡɜɨɞɢɬɟɥɶɧɨɫɬɢ LOAD
DATA, ɞɚɠɟ ɟɫɥɢ ɧɢ ɨɞɢɧ ɞɪɭɝɨɣ ɩɨɬɨɤ ɫ ɷɬɨɣ ɬɚɛɥɢɰɟɣ ɧɟ ɪɚɛɨɬɚɟɬ.
ȿɫɥɢ ɭɤɚɡɚɧɨ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ LOCAL, ɨɧɨ ɤɚɫɚɟɬɫɹ ɤɥɢɟɧɬɫɤɨɣ ɱɚɫɬɢ ɫɨɟɞɢɧɟɧɢɹ.
ɫɥɨɜɨ LOCAL ɭɤɚɡɚɧɨ, ɮɚɣɥ ɱɢɬɚɟɬɫɹ ɤɥɢɟɧɬɫɤɨɣ ɩɪɨɝɪɚɦɦɨɣ ɧɚ ɯɨɫɬɟ ɤɥɢɟɧɬɚ ɢ ɨɬɩɪɚɜɥɹɟɬɫɹ ɧɚ ɫɟɪɜɟɪ.
„ȿɫɥɢ ɫɥɨɜɨ LOCAL ɧɟ ɭɤɚɡɚɧɨ, ɡɚɝɪɭɠɚɟɦɵɣ ɮɚɣɥ ɞɨɥɠɟɧ ɧɚɯɨɞɢɬɶɫɹ ɧɚ ɯɨɫɬɟ ɫɟɪɜɟɪɚ, ɢ ɱɢɬɚɟɬɫɹ ɫɟɪɜɟɪɨɦ ɧɟɩɨɫɪɟɞɫɬɜɟɧɧɨ.
LOCAL ɞɨɫɬɭɩɧɨ ɜ MySQL 3.22.6 ɢ ɛɨɥɟɟ ɩɨɡɞɧɢɯ ɜɟɪɫɢɹɯ.
ɂɡ ɫɨɨɛɪɚɠɟɧɢɣ ɛɟɡɨɩɚɫɧɨɫɬɢ ɩɪɢ ɱɬɟɧɢɢ ɬɟɤɫɬɨɜɵɯ ɮɚɣɥɨɜ, ɪɚɫɩɨɥɨɠɟɧɧɵɯ ɧɚ ɫɟɪɜɟɪɟ, ɮɚɣɥɵ ɞɨɥɠɧɵ ɥɢɛɨ ɧɚɯɨɞɢɬɶɫɹ ɜ ɤɚɬɚɥɨɝɟ ɞɚɧɧɵɯ, ɥɢɛɨ ɛɵɬɶ ɞɨɫɬɭɩɧɵɦɢ ɜɫɟɦ ɩɨ
ɱɬɟɧɢɸ. Ʉɪɨɦɟ ɬɨɝɨ, ɱɬɨɛɵ ɢɫɩɨɥɶɡɨɜɚɬɶ LOAD DATA ɫ ɫɟɪɜɟɪɧɵɦɢ ɮɚɣɥɚɦɢ, ɜɵ ɞɨɥɠɧɵ
ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ FILE.
Ɂɚɝɪɭɡɤɚ ɫ ɨɩɰɢɟɣ LOCAL ɢɞɟɬ ɧɟɫɤɨɥɶɤɨ ɦɟɞɥɟɧɧɟɟ, ɱɟɦ ɤɨɝɞɚ ɜɵ ɞɚɟɬɟ ɫɟɪɜɟɪɭ ɜɨɡɦɨɠɧɨɫɬɶ ɧɟɩɨɫɪɟɞɫɬɜɟɧɧɨɝɨ ɞɨɫɬɭɩɚ ɤ ɡɚɝɪɭɠɚɟɦɵɦ ɮɚɣɥɚɦ, ɩɨɬɨɦɭ ɱɬɨ ɜ ɷɬɨɦ ɫɥɭɱɚɟ
ɫɨɞɟɪɠɢɦɨɟ ɮɚɣɥɨɜ ɩɟɪɟɞɚɟɬɫɹ ɩɨ ɫɟɬɢ ɱɟɪɟɡ ɤɥɢɟɧɬ-ɫɟɪɜɟɪɧɨɟ ɫɨɟɞɢɧɟɧɢɟ. ɋ ɞɪɭɝɨɣ
ɫɬɨɪɨɧɵ, ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɜɚɦ ɧɟ ɧɭɠɧɵ ɩɪɢɜɢɥɟɝɢɢ FILE.
ɇɚɱɢɧɚɹ ɫ ɜɟɪɫɢɣ MySQL 3.23.49 ɢ MySQL 4.0.2 (4.0.13 ɞɥɹ Windows), LOCAL ɪɚɛɨɬɚɟɬ, ɬɨɥɶɤɨ ɟɫɥɢ ɢ ɤɥɢɟɧɬ, ɢ ɫɟɪɜɟɪ ɪɚɡɪɟɲɚɸɬ ɷɬɨ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ mysqld ɡɚɩɭɳɟɧ ɫ ɨɩɰɢɟɣ --local-infile=0, ɬɨ LOCAL ɪɚɛɨɬɚɬɶ ɧɟ ɛɭɞɟɬ.
„ȿɫɥɢ
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
225
ȿɫɥɢ ɜɚɦ ɧɭɠɧɨ ɫ ɩɨɦɨɳɶɸ LOAD DATA ɱɢɬɚɬɶ ɢɡ ɩɪɨɝɪɚɦɦɧɨɝɨ ɤɚɧɚɥɚ, ɜɵ ɦɨɠɟɬɟ
ɩɪɢɦɟɧɢɬɶ ɫɥɟɞɭɸɳɭɸ ɬɟɯɧɢɤɭ:
mkfifo /mysql/db/x/x
chmod 666 /mysql/db/x/x
cat < /dev/tcp/10.1.1.12/4711 > /mysql/db/x/x
mysql -e "LOAD DATA INFILE 'x' INTO TABLE x" x
ȿɫɥɢ ɜɵ ɪɚɛɨɬɚɟɬɟ ɫ ɜɟɪɫɢɟɣ MySQL, ɩɪɟɞɲɟɫɬɜɭɸɳɟɣ 3.23.25, ɬɨ ɷɬɭ ɬɟɯɧɢɤɭ ɦɨɠɧɨ ɩɪɢɦɟɧɹɬɶ ɬɨɥɶɤɨ ɫ LOAD DATA LOCAL INFILE.
ȿɫɥɢ ɭ ɜɚɫ ɜɟɪɫɢɹ MySQL, ɩɪɟɞɲɟɫɬɜɭɸɳɚɹ 3.23.24, ɬɨ ɜɵ ɧɟ ɫɦɨɠɟɬɟ ɱɢɬɚɬɶ ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ LOAD DATA INFILE ɢɡ FIFO. ȿɫɥɢ ɜɚɦ ɧɭɠɧɨ ɱɢɬɚɬɶ ɢɡ FIFO (ɧɚɩɪɢɦɟɪ, ɢɡ ɜɵɯɨɞɧɨɝɨ ɩɨɬɨɤɚ gunzip), ɩɪɢɦɟɧɹɣɬɟ ɜɦɟɫɬɨ ɷɬɨɝɨ LOAD DATA LOCAL INFILE.
ɉɪɢ ɩɨɢɫɤɟ ɮɚɣɥɚ ɜ ɫɜɨɟɣ ɮɚɣɥɨɜɨɣ ɫɢɫɬɟɦɟ ɫɟɪɜɟɪ ɪɭɤɨɜɨɞɫɬɜɭɟɬɫɹ ɫɥɟɞɭɸɳɢɦɢ
ɩɪɚɜɢɥɚɦɢ:
„ȿɫɥɢ ɡɚɞɚɧ ɚɛɫɨɥɸɬɧɵɣ ɩɭɬɶ, ɫɟɪɜɟɪ ɟɝɨ ɢɫɩɨɥɶɡɭɟɬ, ɤɚɤ ɟɫɬɶ.
„ȿɫɥɢ ɡɚɞɚɧ ɨɬɧɨɫɢɬɟɥɶɧɵɣ ɩɭɬɶ ɫ ɨɞɧɢɦ ɢɥɢ ɛɨɥɟɟ ɜɟɞɭɳɢɦɢ ɤɨɦɩɨɧɟɧɬɚɦɢ, ɫɟɪɜɟɪ ɢɳɟɬ ɮɚɣɥɵ ɨɬɧɨɫɢɬɟɥɶɧɨ ɫɜɨɟɝɨ ɤɚɬɚɥɨɝɚ ɞɚɧɧɵɯ.
„ȿɫɥɢ ɡɚɞɚɧɨ ɢɦɹ ɮɚɣɥɚ ɛɟɡ ɜɟɞɭɳɢɯ ɤɨɦɩɨɧɟɧɬɨɜ ɩɭɬɢ, ɫɟɪɜɟɪ ɢɳɟɬ ɮɚɣɥ ɜ ɤɚɬɚɥɨɝɟ ɞɚɧɧɵɯ ɛɚɡɵ ɞɚɧɧɵɯ ɩɨ ɭɦɨɥɱɚɧɢɸ.
Ɉɬɦɟɬɢɦ, ɱɬɨ ɢɡ ɷɬɢɯ ɩɪɚɜɢɥ ɫɥɟɞɭɟɬ, ɱɬɨ ɮɚɣɥ ɫ ɢɦɟɧɟɦ ./myfile.txt ɱɢɬɚɟɬɫɹ ɢɡ
ɤɚɬɚɥɨɝɚ ɞɚɧɧɵɯ ɫɟɪɜɟɪɚ, ɜ ɬɨ ɜɪɟɦɹ ɤɚɤ ɮɚɣɥ ɫ ɢɦɟɧɟɦ myfile.txt ɱɢɬɚɟɬɫɹ ɢɡ ɤɚɬɚɥɨɝɚ
ɞɚɧɧɵɯ ɛɚɡɵ ɞɚɧɧɵɯ ɩɨ ɭɦɨɥɱɚɧɢɸ. ɇɚɩɪɢɦɟɪ, ɫɥɟɞɭɸɳɢɣ ɨɩɟɪɚɬɨɪ LOAD DATA INFILE
ɱɢɬɚɟɬ ɮɚɣɥ data.txt ɢɡ ɤɚɬɚɥɨɝɚ ɞɚɧɧɵɯ ɛɚɡɵ db1, ɩɨɬɨɦɭ ɱɬɨ db1 – ɬɟɤɭɳɚɹ ɛɚɡɚ ɞɚɧɧɵɯ, ɧɟɫɦɨɬɪɹ ɧɚ ɬɨ, ɱɬɨ ɨɩɟɪɚɬɨɪ ɡɚɝɪɭɠɚɟɬ ɞɚɧɧɵɟ ɜ ɛɚɡɭ ɞɚɧɧɵɯ db2:
mysql> USE db1;
mysql> LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;
Ʉɥɸɱɟɜɵɟ ɫɥɨɜɚ REPLACE ɢ IGNORE ɭɩɪɚɜɥɹɸɬ ɪɚɛɨɬɨɣ ɫ ɜɯɨɞɧɵɦɢ ɫɬɪɨɤɚɦɢ, ɤɨɬɨɪɵɟ
ɞɭɛɥɢɪɭɸɬ ɫɭɳɟɫɬɜɭɸɳɢɟ ɩɨ ɡɧɚɱɟɧɢɸ ɭɧɢɤɚɥɶɧɵɯ ɤɥɸɱɟɣ.
ȿɫɥɢ ɭɤɚɡɚɧɨ REPLACE, ɜɯɨɞɧɵɟ ɫɬɪɨɤɢ ɡɚɦɟɧɹɸɬ ɫɭɳɟɫɬɜɭɸɳɢɟ ɫɬɪɨɤɢ (ɞɪɭɝɢɦɢ
ɫɥɨɜɚɦɢ, ɫɬɪɨɤɢ, ɤɨɬɨɪɵɟ ɢɦɟɸɬ ɬɟ ɠɟ ɡɧɚɱɟɧɢɹ ɩɟɪɜɢɱɧɵɯ ɢɥɢ ɭɧɢɤɚɥɶɧɵɯ ɤɥɸɱɟɣ, ɤɚɤ
ɢ ɫɭɳɟɫɬɜɭɸɳɢɟ ɜ ɬɚɛɥɢɰɟ ɫɬɪɨɤɢ). ɋɦ. ɪɚɡɞɟɥ 6.1.6.
ȿɫɥɢ ɭɤɚɡɚɧɨ IGNORE, ɬɨ ɜɯɨɞɧɵɟ ɫɬɪɨɤɢ, ɤɨɬɨɪɵɟ ɞɭɛɥɢɪɭɸɬ ɫɭɳɟɫɬɜɭɸɳɢɟ ɫɬɪɨɤɢ ɫ
ɬɟɦɢ ɠɟ ɡɧɚɱɟɧɢɹɦɢ ɩɟɪɜɢɱɧɵɯ ɢɥɢ ɭɧɢɤɚɥɶɧɵɯ ɤɥɸɱɟɣ, ɩɪɨɩɭɫɤɚɸɬɫɹ. ȿɫɥɢ ɧɟ ɭɤɚɡɚɧɚ
ɧɢ ɨɞɧɚ, ɧɢ ɞɪɭɝɚɹ ɨɩɰɢɢ, ɬɨ ɩɨɜɟɞɟɧɢɟ ɡɚɜɢɫɢɬ ɨɬ ɬɨɝɨ, ɭɤɚɡɚɧɨ ɥɢ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ LOCAL.
ɉɪɢ ɨɬɫɭɬɫɬɜɢɢ LOCAL, ɜ ɫɥɭɱɚɟ ɨɛɧɚɪɭɠɟɧɢɹ ɞɭɛɥɢɪɨɜɚɧɢɹ ɤɥɸɱɚ ɝɟɧɟɪɢɪɭɟɬɫɹ ɨɲɢɛɤɚ, ɚ
ɨɫɬɚɬɨɤ ɬɟɤɫɬɨɜɨɝɨ ɮɚɣɥɚ ɢɝɧɨɪɢɪɭɟɬɫɹ. ɉɪɢ ɧɚɥɢɱɢɢ LOCAL, ɩɨɜɟɞɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ
ɛɭɞɟɬ ɬɚɤɢɦ ɠɟ, ɤɚɤ ɟɫɥɢ ɛɵ ɛɵɥɨ ɭɤɚɡɚɧɨ IGNORE. ɗɬɨ ɨɛɴɹɫɧɹɟɬɫɹ ɬɟɦ, ɱɬɨ ɫɟɪɜɟɪ ɧɟ ɜ
ɫɨɫɬɨɹɧɢɢ ɨɫɬɚɧɨɜɢɬɶ ɩɟɪɟɞɚɱɭ ɮɚɣɥɚ ɜ ɩɪɨɰɟɫɫɟ ɜɵɩɨɥɧɟɧɢɹ ɷɬɨɣ ɨɩɟɪɚɰɢɢ.
ȿɫɥɢ ɜɵ ɯɨɬɢɬɟ ɢɝɧɨɪɢɪɨɜɚɬɶ ɨɝɪɚɧɢɱɟɧɢɹ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ ɜ ɩɪɨɰɟɫɫɟ ɨɩɟɪɚɰɢɢ ɡɚɝɪɭɡɤɢ ɞɚɧɧɵɯ, ɜɵ ɦɨɠɟɬɟ ɜɵɩɨɥɧɢɬɶ ɨɩɟɪɚɬɨɪ SET FOREIGN_KEY_CHECKS=0 ɩɟɪɟɞ ɡɚɩɭɫɤɨɦ LOAD DATA.
ȿɫɥɢ ɜɵ ɡɚɩɭɫɤɚɟɬɟ LOAD DATA ɞɥɹ ɩɭɫɬɨɣ ɬɚɛɥɢɰɵ MyISAM, ɜɫɟ ɧɟɭɧɢɤɚɥɶɧɵɟ ɢɧɞɟɤɫɵ ɫɨɡɞɚɸɬɫɹ ɜ ɨɬɞɟɥɶɧɨɦ ɡɚɞɚɧɢɢ (ɤɚɤ ɞɥɹ REPAIR TABLE). Ɉɛɵɱɧɨ ɷɬɨ ɩɪɢɜɨɞɢɬ ɤ ɬɨɦɭ,
ɱɬɨ ɩɪɢ ɧɚɥɢɱɢɢ ɦɧɨɝɢɯ ɢɧɞɟɤɫɨɜ LOAD DATA ɜɵɩɨɥɧɹɟɬɫɹ ɝɨɪɚɡɞɨ ɛɵɫɬɪɟɟ. Ʉɚɤ ɩɪɚɜɢɥɨ,
ɷɬɨ ɪɚɛɨɬɚɟɬ ɨɱɟɧɶ ɛɵɫɬɪɨ, ɧɨ ɜ ɧɟɤɨɬɨɪɵɯ ɨɫɨɛɵɯ ɫɥɭɱɚɹɯ ɜɵ ɦɨɠɟɬɟ ɫɨɡɞɚɬɶ ɢɧɞɟɤɫɵ
ɞɚɠɟ ɟɳɟ ɛɵɫɬɪɟɟ, ɜɵɤɥɸɱɢɜ ɢɯ ɱɟɪɟɡ ALTER TABLE...DISABLE KEYS ɩɟɪɟɞ ɡɚɝɪɭɡɤɨɣ
226
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɮɚɣɥɚ ɜ ɬɚɛɥɢɰɭ, ɩɟɪɟɫɨɡɞɚɜ ɢɧɞɟɤɫɵ ɢ ɜɤɥɸɱɢɜ ɢɯ ɫ ɩɨɦɨɳɶɸ ALTER TABLE...ENABLE
KEYS ɩɨɫɥɟ ɨɤɨɧɱɚɧɢɹ ɡɚɝɪɭɡɤɢ.
LOAD DATA INFILE – ɷɬɨ ɞɨɩɨɥɧɟɧɢɟ ɞɥɹ SELECT...INTO OUTFILE. ɋɦ. ɪɚɡɞɟɥ 6.1.7.
Ⱦɥɹ ɡɚɩɢɫɢ ɞɚɧɧɵɯ ɢɡ ɬɚɛɥɢɰɵ ɜ ɮɚɣɥ ɩɨɥɶɡɭɣɬɟɫɶ SELECT...INTO OUTFILE. ɑɬɨɛɵ ɩɪɨɱɢɬɚɬɶ ɞɚɧɧɵɟ ɨɛɪɚɬɧɨ ɢɡ ɮɚɣɥɚ ɜ ɬɚɛɥɢɰɭ, ɜɨɫɩɨɥɶɡɭɣɬɟɫɶ LOAD DATA INFILE. ɋɢɧɬɚɤɫɢɫ ɤɨɧɫɬɪɭɤɰɢɣ FIELDS ɢ LINES ɨɞɢɧɚɤɨɜ ɞɥɹ ɨɛɨɢɯ ɨɩɟɪɚɬɨɪɨɜ. Ɉɛɟ ɷɬɢ ɤɨɧɫɬɪɭɤɰɢɢ
ɧɟ ɨɛɹɡɚɬɟɥɶɧɵ, ɧɨ FIELDS ɞɨɥɠɧɚ ɩɪɟɞɲɟɫɬɜɨɜɚɬɶ LINES, ɟɫɥɢ ɭɤɚɡɚɧɵ ɨɛɟ.
ȿɫɥɢ ɭɤɚɡɚɧɚ ɤɨɧɫɬɪɭɤɰɢɹ FIELDS, ɬɨ ɜɫɟ ɟɟ ɩɚɪɚɦɟɬɪɵ (TERMINATED BY, [OPTIONALLY]
ENCLOSED BY ɢ ESCAPED BY) ɬɚɤɠɟ ɧɟ ɨɛɹɡɚɬɟɥɶɧɵ, ɡɚ ɢɫɤɥɸɱɟɧɢɟɦ ɬɪɟɛɨɜɚɧɢɹ, ɱɬɨ ɨɛɹɡɚɬɟɥɶɧɨ ɞɨɥɠɟɧ ɩɪɢɫɭɬɫɬɜɨɜɚɬɶ ɯɨɬɹ ɛɵ ɨɞɢɧ ɩɚɪɚɦɟɬɪ.
ȿɫɥɢ ɤɨɧɫɬɪɭɤɰɢɹ FIELDS ɧɟ ɭɤɚɡɚɧɚ, ɩɨ ɭɦɨɥɱɚɧɢɸ ɩɪɢɧɢɦɚɟɬɫɹ ɫɥɟɞɭɸɳɢɣ ɜɢɞ:
FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\\'
ȿɫɥɢ ɧɟ ɭɤɚɡɚɧɚ ɤɨɧɫɬɪɭɤɰɢɹ LINES, ɩɨ ɭɦɨɥɱɚɧɢɸ ɩɪɢɧɢɦɚɟɬɫɹ ɬɚɤɨɣ ɜɚɪɢɚɧɬ:
LINES TERMINATED BY '\n' STARTING BY ''
Ⱦɪɭɝɢɦɢ ɫɥɨɜɚɦɢ, ɩɨɜɟɞɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ LOAD DATA INFILE ɩɪɢ ɱɬɟɧɢɢ ɜɜɨɞɚ ɬɚɤɨɜɨ:
„ɂɫɤɚɬɶ ɪɚɡɞɟɥɢɬɟɥɢ ɫɬɪɨɤ ɜ ɧɚɱɚɥɟ ɫɬɪɨɤ.
„ɇɟ ɩɪɨɩɭɫɤɚɬɶ ɧɢɤɚɤɢɯ ɩɪɟɮɢɤɫɨɜ ɫɬɪɨɤɢ.
„Ɋɚɡɛɢɜɚɬɶ ɫɬɪɨɤɭ ɧɚ ɩɨɥɹ ɩɨ ɡɧɚɤɚɦ ɬɚɛɭɥɹɰɢɢ.
„ɇɟ ɨɠɢɞɚɬɶ, ɱɬɨ ɩɨɥɹ ɛɭɞɭɬ ɡɚɤɥɸɱɟɧɵ ɜ ɤɚɜɵɱɤɢ.
„ɂɧɬɟɪɩɪɟɬɢɪɨɜɚɬɶ ɩɨɹɜɥɟɧɢɟ ɡɧɚɤɚ ɬɚɛɭɥɹɰɢɢ, ɩɟɪɟɜɨɞɚ ɫɬɪɨɤɢ ɢɥɢ ɫɢɦɜɨɥɚ ‘\’,
ɤɨɬɨɪɵɦ ɩɪɟɞɲɟɫɬɜɭɟɬ ‘\’, ɤɚɤ ɥɢɬɟɪɚɥɶɧɵɟ ɫɢɦɜɨɥɵ, ɹɜɥɹɸɳɢɟɫɹ ɱɚɫɬɶɸ ɡɧɚɱɟɧɢɹ ɩɨɥɹ.
ɂ ɧɚɨɛɨɪɨɬ, SELECT...INTO OUTFILE ɩɨ ɭɦɨɥɱɚɧɢɸ ɜɟɞɟɬ ɫɟɛɹ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
„ɉɢɲɟɬ ɡɧɚɤɢ ɬɚɛɭɥɹɰɢɢ ɦɟɠɞɭ ɩɨɥɹɦɢ.
„ɇɟ ɨɤɪɭɠɚɟɬ ɡɧɚɱɟɧɢɹ ɩɨɥɟɣ ɤɚɜɵɱɤɚɦɢ.
„ɂɫɩɨɥɶɡɭɟɬ ‘\’ ɞɥɹ ɜɵɞɟɥɟɧɢɹ ɡɧɚɤɨɜ ɬɚɛɭɥɹɰɢɢ, ɩɟɪɟɜɨɞɚ ɫɬɪɨɤ ɢɥɢ ‘\’, ɜɫɬɪɟɱɚɸɳɢɯɫɹ ɜɧɭɬɪɢ ɡɧɚɱɟɧɢɣ ɩɨɥɟɣ.
„ɉɢɲɟɬ ɫɢɦɜɨɥ ɩɟɪɟɜɨɞɚ ɫɬɪɨɤɢ ɜ ɤɨɧɰɟ ɫɬɪɨɤ.
ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɞɥɹ ɧɚɩɢɫɚɧɢɹ FIELDS ESCAPED BY '\\' ɩɨɬɪɟɛɭɟɬɫɹ ɭɤɚɡɚɬɶ
ɞɜɚ ɡɧɚɤɚ ɨɛɪɚɬɧɨɣ ɤɨɫɨɣ ɱɟɪɬɵ ɞɥɹ ɡɧɚɱɟɧɢɣ, ɜ ɤɨɬɨɪɵɯ ɧɭɠɧɨ ɱɢɬɚɬɶ ɨɞɧɭ ɨɛɪɚɬɧɭɸ
ɤɨɫɭɸ ɱɟɪɬɭ.
oË ÑËÕÏÛµÂ!
ȿɫɥɢ ɜɵ ɫɝɟɧɟɪɢɪɨɜɚɥɢ ɬɟɤɫɬɨɜɵɣ ɮɚɣɥ ɜ ɫɢɫɬɟɦɟ Windows, ɜɨɡɦɨɠɧɨ, ɜɚɦ ɩɨɧɚɞɨɛɢɬɫɹ ɡɚɞɚɬɶ
LINES TERMINATED BY '\r\n', ɱɬɨɛɵ ɩɪɚɜɢɥɶɧɨ ɩɪɨɱɢɬɚɬɶ ɮɚɣɥ, ɩɨɫɤɨɥɶɤɭ ɩɪɨɝɪɚɦɦɵ
Windows ɨɛɵɱɧɨ ɢɫɩɨɥɶɡɭɸɬ ɷɬɢ ɞɜɚ ɫɢɦɜɨɥɚ ɜ ɤɚɱɟɫɬɜɟ ɪɚɡɞɟɥɢɬɟɥɹ ɫɬɪɨɤ. ɇɟɤɨɬɨɪɵɟ ɩɪɨɝɪɚɦɦɵ, ɩɨɞɨɛɧɵɟ WordPad, ɩɪɢ ɡɚɩɢɫɢ ɮɚɣɥɨɜ ɦɨɝɭɬ ɢɫɩɨɥɶɡɨɜɚɬɶ ɫɢɦɜɨɥ '\r' ɜ ɤɚɱɟɫɬɜɟ
ɪɚɡɞɟɥɢɬɟɥɹ ɫɬɪɨɤ. ɑɬɨɛɵ ɱɢɬɚɬɶ ɬɚɤɢɟ ɮɚɣɥɵ, ɢɫɩɨɥɶɡɭɣɬɟ LINES TERMINATED BY '\r'.
ȿɫɥɢ ɜɫɟ ɫɬɪɨɤɢ ɱɢɬɚɟɦɨɝɨ ɮɚɣɥɚ ɢɦɟɸɬ ɨɛɳɢɣ ɩɪɟɮɢɤɫ, ɤɨɬɨɪɵɣ ɜɵ ɯɨɬɢɬɟ
ɢɝɧɨɪɢɪɨɜɚɬɶ, ɢɫɩɨɥɶɡɭɣɬɟ LINES STARTING BY 'ɫɬɪɨɤɚ_ɩɪɟɮɢɤɫɚ' ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ
ɩɪɨɩɭɫɤɚɬɶ ɷɬɨɬ ɩɪɟɮɢɤɫ. ȿɫɥɢ ɫɬɪɨɤɚ ɧɟ ɫɨɞɟɪɠɢɬ ɩɪɟɮɢɤɫɚ, ɨɧɚ ɩɪɨɩɭɫɤɚɟɬɫɹ ɜɫɹ
ɰɟɥɢɤɨɦ.
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
227
Ɉɩɰɢɹ IGNORE ɤɨɥɢɱɟɫɬɜɨ LINES ɫɥɭɠɢɬ ɞɥɹ ɢɝɧɨɪɢɪɨɜɚɧɢɹ ɡɚɞɚɧɧɨɝɨ ɤɨɥɢɱɟɫɬɜɚ
ɫɬɪɨɤ ɜ ɧɚɱɚɥɟ ɮɚɣɥɚ. ɇɚɩɪɢɦɟɪ, ɜɵ ɦɨɠɟɬɟ ɜɨɫɩɨɥɶɡɨɜɚɬɶɫɹ IGNORE 1 LINES, ɱɬɨɛɵ
ɩɪɨɩɭɫɬɢɬɶ ɧɚɱɚɥɶɧɭɸ ɫɬɪɨɤɭ, ɫɨɞɟɪɠɚɳɭɸ ɢɦɟɧɚ ɫɬɨɥɛɰɨɜ:
mysql> LOAD DATA INFILE '/tmp/test.txt'
-> INTO TABLE test IGNORE 1 LINES;
Ʉɨɝɞɚ ɜɵ ɩɪɢɦɟɧɹɟɬɟ SELECT...INTO OUTFILE ɜ ɫɜɹɡɤɟ ɫ LOAD DATA INFILE ɞɥɹ ɡɚɩɢɫɢ
ɞɚɧɧɵɯ ɢɡ ɛɚɡɵ ɜ ɮɚɣɥ ɢ ɩɨɫɥɟɞɭɸɳɟɝɨ ɟɝɨ ɱɬɟɧɢɹ ɢ ɡɚɝɪɭɡɤɢ ɨɛɪɚɬɧɨ ɜ ɛɚɡɭ, ɨɩɰɢɢ
ɭɩɪɚɜɥɟɧɢɹ ɫɬɪɨɤɚɦɢ ɢ ɩɨɥɹɦɢ ɞɥɹ ɨɛɨɢɯ ɨɩɟɪɚɬɨɪɨɜ ɞɨɥɠɧɵ ɫɨɜɩɚɞɚɬɶ. ȼ ɩɪɨɬɢɜɧɨɦ
ɫɥɭɱɚɟ LOAD DATA INFILE ɧɟ ɫɦɨɠɟɬ ɩɪɚɜɢɥɶɧɨ ɢɧɬɟɪɩɪɟɬɢɪɨɜɚɬɶ ɫɨɞɟɪɠɢɦɨɟ ɬɟɤɫɬɨɜɨɝɨ ɮɚɣɥɚ. ɉɪɟɞɩɨɥɨɠɢɦ, ɱɬɨ ɜɵ ɫ ɩɨɦɨɳɶɸ SELECT...INTO OUTFILE ɜɵɜɟɥɢ ɞɚɧɧɵɟ ɜ
ɬɟɤɫɬɨɜɵɣ ɮɚɣɥ, ɪɚɡɞɟɥɹɹ ɩɨɥɹ ɡɚɩɹɬɵɦɢ:
mysql> SELECT * INTO OUTFILE 'data.txt'
-> FIELDS TERMINATED BY ','
-> FROM table2;
ɑɬɨɛɵ ɩɪɨɱɢɬɚɬɶ ɪɚɡɞɟɥɟɧɧɵɣ ɡɚɩɹɬɵɦɢ ɮɚɣɥ ɨɛɪɚɬɧɨ, ɩɪɚɜɢɥɶɧɨ ɛɭɞɟɬ ɩɨɫɬɭɩɢɬɶ ɬɚɤ:
mysql> LOAD DATA INFILE 'data.txt' INTO TABLE table2
-> FIELDS TERMINATED BY ',';
ȿɫɥɢ ɜɦɟɫɬɨ ɷɬɨɝɨ ɜɵ ɩɨɩɵɬɚɟɬɟɫɶ ɩɪɨɱɢɬɚɬɶ ɟɝɨ ɨɩɟɪɚɬɨɪɨɦ, ɩɪɢɜɟɞɟɧɧɵɦ ɧɢɠɟ,
ɷɬɨ ɧɟ ɫɪɚɛɨɬɚɟɬ, ɩɨɬɨɦɭ ɱɬɨ LOAD DATA INFILE ɛɭɞɟɬ ɢɫɤɚɬɶ ɫɢɦɜɨɥɵ ɬɚɛɭɥɹɰɢɢ ɦɟɠɞɭ
ɡɧɚɱɟɧɢɹɦɢ ɩɨɥɟɣ:
mysql> LOAD DATA INFILE 'data.txt' INTO TABLE table2
-> FIELDS TERMINATED BY '\t';
ɇɚɢɛɨɥɟɟ ɜɟɪɨɹɬɧɵɦ ɪɟɡɭɥɶɬɚɬɨɦ ɛɭɞɟɬ ɢɧɬɟɪɩɪɟɬɚɰɢɹ ɜɯɨɞɧɨɣ ɫɬɪɨɤɢ ɤɚɤ ɟɞɢɧɫɬɜɟɧɧɨɝɨ ɩɨɥɹ.
LOAD DATA INFILE ɬɚɤɠɟ ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɞɥɹ ɱɬɟɧɢɹ ɮɚɣɥɨɜ ɢɡ ɜɧɟɲɧɢɯ ɢɫɬɨɱɧɢɤɨɜ. ɇɚɩɪɢɦɟɪ, ɧɟɤɨɬɨɪɵɣ ɮɚɣɥ ɦɨɠɟɬ ɢɦɟɬɶ ɩɨɥɹ, ɪɚɡɞɟɥɟɧɧɵɟ ɡɚɩɹɬɵɦɢ ɢ ɡɚɤɥɸɱɟɧɧɵɟ ɜ ɞɜɨɣɧɵɟ ɤɚɜɵɱɤɢ. ȿɫɥɢ ɫɬɪɨɤɢ ɜ ɮɚɣɥɟ ɪɚɡɞɟɥɟɧɵ ɫɢɦɜɨɥɨɦ ɧɨɜɨɣ ɫɬɪɨɤɢ,
ɩɪɢɜɟɞɟɧɧɵɣ ɧɢɠɟ ɩɪɢɦɟɪ ɢɥɥɸɫɬɪɢɪɭɟɬ, ɤɚɤɢɟ ɞɨɥɠɧɵ ɛɵɬɶ ɭɫɬɚɧɨɜɥɟɧɵ ɨɩɰɢɢ ɪɚɡɞɟɥɢɬɟɥɟɣ ɫɬɪɨɤ ɢ ɫɬɨɥɛɰɨɜ ɞɥɹ ɡɚɝɪɭɡɤɢ ɮɚɣɥɚ:
mysql> LOAD DATA INFILE 'data.txt' INTO TABLE ɢɦɹ_ɬɚɛɥɢɰɵ
-> FIELDS TERMINATED BY ',' ENCLOSED BY '"'
-> LINES TERMINATED BY '\n';
Ʌɸɛɵɦ ɨɩɰɢɹɦ, ɡɚɞɚɸɳɢɦ ɨɝɪɚɧɢɱɢɬɟɥɢ ɫɬɪɨɤ ɢ ɫɬɨɥɛɰɨɜ, ɦɨɠɧɨ ɭɤɚɡɵɜɚɬɶ ɜ ɤɚɱɟɫɬɜɟ ɚɪɝɭɦɟɧɬɨɜ ɩɭɫɬɵɟ ɫɬɪɨɤɢ (''). ȿɫɥɢ ɠɟ ɚɪɝɭɦɟɧɬɵ – ɧɟ ɩɭɫɬɵɟ ɫɬɪɨɤɢ, ɬɨ ɡɧɚɱɟɧɢɹ
ɞɥɹ FIELDS [OPTIONALLY] ENCLOSED BY ɢ FIELDS ESCAPED BY ɞɨɥɠɧɵ ɛɵɬɶ ɨɞɧɨɫɢɦɜɨɥɶɧɵɦɢ. Ⱥɪɝɭɦɟɧɬɵ ɨɩɰɢɣ FIELDS TERMINATED BY, LINES STARTING BY ɢ LINES
TERMINATED BY ɦɨɝɭɬ ɢɦɟɬɶ ɞɥɢɧɭ ɛɨɥɟɟ ɨɞɧɨɝɨ ɫɢɦɜɨɥɚ. ɇɚɩɪɢɦɟɪ, ɱɬɨɛɵ ɩɢɫɚɬɶ ɫɬɪɨɤɢ, ɪɚɡɞɟɥɟɧɧɵɟ ɫɢɦɜɨɥɚɦɢ ɜɨɡɜɪɚɬ ɤɚɪɟɬɤɢ/ɩɟɪɟɜɨɞ ɫɬɪɨɤɢ, ɥɢɛɨ ɱɬɨɛɵ ɱɢɬɚɬɶ ɮɚɣɥɵ,
ɫɨɞɟɪɠɚɳɢɟ ɬɚɤɢɟ ɫɬɪɨɤɢ, ɭɤɚɡɵɜɚɣɬɟ ɤɨɧɫɬɪɭɤɰɢɸ LINES TERMINATED BY '\r\n'.
ɑɬɨɛɵ ɩɪɨɱɢɬɚɬɶ ɮɚɣɥ, ɪɚɡɞɟɥɟɧɧɵɣ ɩɨ ɫɬɪɨɤɚɦ ɫɢɦɜɨɥɚɦɢ %%, ɦɨɠɧɨ ɩɨɫɬɭɩɢɬɶ
ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
mysql> CREATE TABLE jokes
-> (a INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> joke TEXT NOT NULL);
228
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
mysql> LOAD DATA INFILE '/tmp/jokes.txt' INTO TABLE jokes
-> FIELDS TERMINATED BY ''
-> LINES TERMINATED BY '\n%%\n' (joke);
FIELDS [OPTIONALLY] ENCLOSED BY ɭɩɪɚɜɥɹɟɬ ɨɝɪɚɧɢɱɢɬɟɥɹɦɢ (ɤɚɜɵɱɤɚɦɢ) ɩɨɥɟɣ.
ɉɪɢ ɜɵɜɨɞɟ (SELECT...INTO OUTFILE), ɟɫɥɢ ɩɪɨɩɭɫɬɢɬɶ ɫɥɨɜɨ OPTIONALLY, ɜɫɟ ɩɨɥɹ ɛɭɞɭɬ ɨɤɪɭɠɟɧɵ ɫɢɦɜɨɥɨɦ, ɭɤɚɡɚɧɧɵɦ ɜ ENCLOSED BY. ɉɪɢɦɟɪ ɬɚɤɨɝɨ ɜɵɜɨɞɚ (ɫ ɢɫɩɨɥɶɡɨ-
ɜɚɧɢɟɦ ɡɚɩɹɬɨɣ ɜ ɤɚɱɟɫɬɜɟ ɪɚɡɞɟɥɢɬɟɥɹ ɩɨɥɟɣ) ɩɨɤɚɡɚɧ ɧɢɠɟ:
"1","a
"2","a
"3","a
"4","a
string","100.20"
string containing a , comma","102.20"
string containing a \" quote","102.20"
string containing a \", quote and comma","102.20"
ȿɫɥɢ ɜɵ ɭɤɚɡɵɜɚɟɬɟ OPTIONALLY, ɬɨ ɫɢɦɜɨɥ ENCLOSED BY ɩɪɢɦɟɧɹɟɬɫɹ ɬɨɥɶɤɨ ɞɥɹ ɡɚɤɥɸɱɟɧɢɹ ɜ ɤɚɜɵɱɤɢ ɩɨɥɟɣ ɬɢɩɚ CHAR ɢ VARCHAR:
1,"a
2,"a
3,"a
4,"a
string",100.20
string containing a , comma",102.20
string containing a \" quote",102.20
string containing a \", quote and comma",102.20
Ɉɛɪɚɬɢɬɟ ɜɧɢɦɚɧɢɟ, ɱɬɨ ɜɯɨɠɞɟɧɢɹ ɫɢɦɜɨɥɚ, ɭɤɚɡɚɧɧɨɝɨ ɜ ENCLOSED BY, ɜɧɭɬɪɢ ɡɧɚɱɟɧɢɹ ɩɨɥɹ ɩɪɟɞɜɚɪɹɟɬɫɹ ɫɢɦɜɨɥɨɦ, ɡɚɞɚɧɧɵɦ ɜ ESCAPED BY. Ʉɪɨɦɟ ɬɨɝɨ, ɟɫɥɢ ɭɤɚɡɚɬɶ
ɩɭɫɬɨɟ ɡɧɚɱɟɧɢɟ ɞɥɹ ESCAPED BY, ɜɨɡɦɨɠɧɨ, ɱɬɨ ɛɭɞɟɬ ɫɝɟɧɟɪɢɪɨɜɚɧ ɮɚɣɥ, ɤɨɬɨɪɵɣ LOAD
DATA INFILE ɧɟ ɫɭɦɟɟɬ ɩɪɚɜɢɥɶɧɨ ɡɚɝɪɭɡɢɬɶ.
ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɫɢɦɜɨɥ ɨɬɦɟɧɵ ɨɫɬɚɜɢɬɶ ɩɭɫɬɵɦ, ɩɪɢɜɟɞɟɧɧɵɣ ɜɵɲɟ ɜɵɜɨɞ ɛɭɞɟɬ
ɜɵɝɥɹɞɟɬɶ ɬɚɤ, ɤɚɤ ɩɨɤɚɡɚɧɨ ɧɢɠɟ. ɇɟɫɥɨɠɧɨ ɡɚɦɟɬɢɬɶ, ɱɬɨ ɜɬɨɪɨɟ ɩɨɥɟ ɜ ɱɟɬɜɟɪɬɨɣ
ɫɬɪɨɤɟ ɫɨɞɟɪɠɢɬ ɡɚɩɹɬɭɸ, ɫɥɟɞɭɸɳɭɸ ɡɚ ɤɚɜɵɱɤɨɣ, ɤɨɬɨɪɚɹ (ɨɲɢɛɨɱɧɨ) ɛɭɞɟɬ ɜɵɝɥɹɞɟɬɶ ɤɚɤ ɪɚɡɞɟɥɢɬɟɥɶ ɩɨɥɟɣ.
1,"a
2,"a
3,"a
4,"a
string",100.20
string containing a , comma",102.20
string containing a " quote",102.20
string containing a ", quote and comma",102.20
ɉɪɢ ɜɜɨɞɟ ɫɢɦɜɨɥ ENCLOSED BY, ɟɫɥɢ ɨɧ ɟɫɬɶ, ɭɞɚɥɹɟɬɫɹ ɢɡ ɤɨɧɰɚ ɡɧɚɱɟɧɢɹ ɩɨɥɟɣ. (ɗɬɨ
ɜɟɪɧɨ ɜɧɟ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɬɨɝɨ, ɭɤɚɡɚɧɨ ɢɥɢ ɧɟɬ ɫɥɨɜɨ OPTIONALLY. Ⱦɚɧɧɨɟ ɫɥɨɜɨ ɧɟ ɢɦɟɟɬ
ɧɢɤɚɤɨɝɨ ɷɮɮɟɤɬɚ ɩɪɢ ɢɧɬɟɪɩɪɟɬɚɰɢɢ ɜɜɨɞɚ.) ɉɨɹɜɥɟɧɢɟ ɫɢɦɜɨɥɨɜ ENCLOSED BY ɫ ɩɪɟɞɲɟɫɬɜɭɸɳɢɦ ɫɢɦɜɨɥɨɦ ESCAPED BY ɢɧɬɟɪɩɪɟɬɢɪɭɟɬɫɹ ɤɚɤ ɱɚɫɬɶ ɬɟɤɭɳɟɝɨ ɡɧɚɱɟɧɢɹ ɩɨɥɹ.
ȿɫɥɢ ɩɨɥɟ ɧɚɱɢɧɚɟɬɫɹ ɫ ɫɢɦɜɨɥɚ ENCLOSED BY, ɷɤɡɟɦɩɥɹɪɵ ɷɬɨɝɨ ɫɢɦɜɨɥɚ ɢɧɬɟɪɩɪɟɬɢɪɭɸɬɫɹ ɤɚɤ ɡɚɜɟɪɲɟɧɢɟ ɡɧɚɱɟɧɢɹ ɩɨɥɹ, ɬɨɥɶɤɨ ɟɫɥɢ ɡɚ ɧɢɦɢ ɫɥɟɞɭɟɬ ɩɨɥɟ ɢɥɢ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ TERMINATED BY. ɑɬɨɛɵ ɢɡɛɟɠɚɬɶ ɧɟɨɞɧɨɡɧɚɱɧɨɫɬɢ ɩɪɢ ɩɨɹɜɥɟɧɢɢ ɫɢɦɜɨɥɚ
ENCLOSED BY ɜɧɭɬɪɢ ɡɧɚɱɟɧɢɹ ɩɨɥɹ, ɷɬɨɬ ɫɢɦɜɨɥ ɦɨɠɟɬ ɛɵɬɶ ɩɪɨɞɭɛɥɢɪɨɜɚɧ, ɢ ɛɭɞɟɬ
ɢɧɬɟɪɩɪɟɬɢɪɨɜɚɬɶɫɹ ɤɚɤ ɟɞɢɧɫɬɜɟɧɧɵɣ ɷɤɡɟɦɩɥɹɪ ɫɢɦɜɨɥɚ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɡɚɞɚɟɬɫɹ
ENCLOSED BY '"', ɤɚɜɵɱɤɢ ɨɛɪɚɛɚɬɵɜɚɸɬɫɹ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
"The ""BIG"" boss" -> The "BIG" boss
The "BIG" boss -> The "BIG" boss
The ""BIG"" boss -> The ""BIG"" boss
FIELDS ESCAPED BY ɭɩɪɚɜɥɹɟɬ ɱɬɟɧɢɟɦ ɢɥɢ ɡɚɩɢɫɶɸ ɫɩɟɰɢɚɥɶɧɵɯ ɫɢɦɜɨɥɨɜ. ȿɫɥɢ ɚɪɝɭɦɟɧɬ FIELDS ESCAPED BY ɧɟ ɩɭɫɬɨɣ, ɨɧ ɢɫɩɨɥɶɡɭɟɬɫɹ ɜ ɤɚɱɟɫɬɜɟ ɩɪɟɮɢɤɫɚ ɞɥɹ ɫɥɟɞɭɸɳɢɯ ɫɢɦɜɨɥɨɜ ɜ ɜɵɜɨɞɟ:
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
229
„ɋɢɦɜɨɥɚ FIELDS ESCAPED BY.
„ɋɢɦɜɨɥɚ FIELDS [OPTIONALLY] ENCLOSED BY.
„ɉɟɪɜɨɝɨ ɫɢɦɜɨɥɚ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɟɣ FIELDS TERMINATED BY ɢ LINES TERMINATED BY.
„ASCII 0
(ɤɨɬɨɪɵɣ ɩɢɲɟɬɫɹ ɜɫɥɟɞ ɡɚ ɫɢɦɜɨɥɨɦ ɨɬɦɟɧɵ ɤɚɤ ASCII ‘0’, ɚ ɧɟ ɧɭɥɟɜɨɣ
ɛɚɣɬ).
ȿɫɥɢ ɫɢɦɜɨɥ FIELDS ESCAPED BY ɩɭɫɬ, ɧɢɤɚɤɢɟ ɫɢɦɜɨɥɵ ɧɟ ɩɪɟɞɜɚɪɹɸɬɫɹ ɫɢɦɜɨɥɚɦɢ
ɨɬɦɟɧɵ, ɢ NULL ɜɵɜɨɞɢɬɫɹ ɤɚɤ NULL, ɚ ɧɟ \N. ȼɟɪɨɹɬɧɨ, ɷɬɨ ɧɟ ɨɱɟɧɶ ɯɨɪɨɲɚɹ ɦɵɫɥɶ –
ɨɫɬɚɜɥɹɬɶ ɩɭɫɬɵɦ ɚɪɝɭɦɟɧɬ FIELDS ESCAPED BY, ɨɫɨɛɟɧɧɨ, ɟɫɥɢ ɡɧɚɱɟɧɢɹ ɩɨɥɟɣ ɜɚɲɢɯ
ɞɚɧɧɵɯ ɫɨɞɟɪɠɚɬ ɥɸɛɨɣ ɢɡ ɭɩɨɦɹɧɭɬɵɯ ɫɢɦɜɨɥɨɜ.
ɉɪɢ ɜɜɨɞɟ, ɟɫɥɢ FIELDS ESCAPED BY ɧɟ ɩɭɫɬ, ɬɨ ɩɪɢ ɩɨɹɜɥɟɧɢɢ ɷɬɨɝɨ ɫɢɦɜɨɥɚ ɜ ɫɬɪɨɤɟ
ɡɧɚɱɟɧɢɹ ɨɧ ɭɞɚɥɹɟɬɫɹ, ɚ ɫɥɟɞɭɸɳɢɣ ɡɚ ɧɢɦ ɫɢɦɜɨɥ ɱɢɬɚɟɬɫɹ ɥɢɬɟɪɚɥɶɧɨ, ɤɚɤ ɱɚɫɬɶ ɡɧɚɱɟɧɢɹ ɩɨɥɹ. ɂɫɤɥɸɱɟɧɢɹɦɢ ɹɜɥɹɸɬɫɹ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ‘0’ ɢɥɢ ‘N’ (\0 ɢɥɢ \N, ɟɫɥɢ
ɫɢɦɜɨɥɨɦ ɨɬɦɟɧɵ ɜɵɛɪɚɧ ‘\’). ɗɬɢ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɢɧɬɟɪɩɪɟɬɢɪɭɸɬɫɹ, ɫɨɨɬɜɟɬɫɬɜɟɧɧɨ, ɤɚɤ ASCII NUL (ɧɭɥɟɜɨɣ ɛɚɣɬ) ɢ NULL. ɉɪɚɜɢɥɚ ɨɛɪɚɳɟɧɢɹ ɫ NULL ɨɩɢɫɚɧɵ ɧɢɠɟ ɜ
ɧɚɫɬɨɹɳɟɦ ɪɚɡɞɟɥɟ.
Ȼɨɥɟɟ ɩɨɞɪɨɛɧɭɸ ɢɧɮɨɪɦɚɰɢɸ ɨ ɫɢɧɬɚɤɫɢɫɟ ɨɬɦɟɧɵ ‘\’ ɦɨɠɧɨ ɧɚɣɬɢ ɜ ɪɚɡɞɟɥɟ 2.1.
ȼ ɧɟɤɨɬɨɪɵɯ ɫɥɭɱɚɹɯ ɨɩɰɢɢ, ɭɩɪɚɜɥɹɸɳɢɟ ɩɨɥɹɦɢ ɢ ɫɬɪɨɤɚɦɢ, ɜɡɚɢɦɨɞɟɣɫɬɜɭɸɬ ɦɟɠɞɭ ɫɨɛɨɣ:
„ȿɫɥɢ ɭɤɚɡɚɧɚ ɩɭɫɬɚɹ ɫɬɪɨɤɚ ɞɥɹ LINES TERMINATED BY, ɚ FIELDS TERMINATED BY ɧɟ
ɩɭɫɬ, ɬɨ ɪɚɡɞɟɥɢɬɟɥɟɦ ɫɬɪɨɤ ɬɚɤɠɟ ɫɥɭɠɢɬ LINES TERMINATED BY.
ɩɭɫɬɵ ɢ FIELDS TERMINATED BY ɢ FIELDS ENCLOSED BY, ɢɫɩɨɥɶɡɭɟɬɫɹ ɮɨɪɦɚɬ
ɮɢɤɫɢɪɨɜɚɧɧɨɣ ɫɬɪɨɤɢ (ɛɟɡ ɪɚɡɞɟɥɢɬɟɥɟɣ). ȼ ɷɬɨɦ ɮɨɪɦɚɬɟ ɧɟ ɩɪɢɦɟɧɹɟɬɫɹ ɧɢɤɚɤɢɯ ɪɚɡɞɟɥɢɬɟɥɟɣ ɦɟɠɞɭ ɩɨɥɹɦɢ (ɧɨ ɦɨɠɧɨ ɢɦɟɬɶ ɪɚɡɞɟɥɢɬɟɥɶ ɫɬɪɨɤ). ȼɦɟɫɬɨ ɷɬɨɝɨ ɡɧɚɱɟɧɢɹ ɫɬɨɥɛɰɨɜ ɩɢɲɭɬɫɹ ɢ ɱɢɬɚɸɬɫɹ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɲɢɪɢɧɵ ɨɬɨɛɪɚɠɟɧɢɹ ɫɬɨɥɛɰɨɜ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɫɬɨɥɛɟɰ ɨɛɴɹɜɥɟɧ ɤɚɤ INT(7), ɡɧɚɱɟɧɢɹ ɫɬɨɥɛɰɚ ɡɚɩɢɫɵɜɚɸɬɫɹ ɜ ɫɟɦɢɫɢɦɜɨɥɶɧɨɟ ɩɨɥɟ. ɉɪɢ ɜɜɨɞɟ ɡɧɚɱɟɧɢɹ ɫɬɨɥɛɰɚ ɢɡɜɥɟɤɚɸɬɫɹ
ɱɬɟɧɢɟɦ ɫɟɦɢ ɫɢɦɜɨɥɨɜ.
LINES TERMINATED BY ɩɨ-ɩɪɟɠɧɟɦɭ ɢɫɩɨɥɶɡɭɟɬɫɹ ɞɥɹ ɪɚɡɞɟɥɟɧɢɹ ɫɬɪɨɤ. ȿɫɥɢ ɫɬɪɨɤɚ ɧɟ ɫɨɞɟɪɠɢɬ ɜɫɟɯ ɩɨɥɟɣ, ɨɫɬɚɥɶɧɵɦ ɫɬɨɥɛɰɚɦ ɩɪɢɫɜɚɢɜɚɸɬɫɹ ɢɯ ɡɧɚɱɟɧɢɹ ɩɨ
ɭɦɨɥɱɚɧɢɸ. ȿɫɥɢ ɭ ɜɚɫ ɧɟɬ ɬɟɪɦɢɧɚɬɨɪɚ ɫɬɪɨɤɢ, ɟɝɨ ɡɧɚɱɟɧɢɟ ɧɭɠɧɨ ɭɫɬɚɧɨɜɢɬɶ ɜ
''. ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɬɟɤɫɬɨɜɵɣ ɮɚɣɥ ɞɨɥɠɟɧ ɫɨɞɟɪɠɚɬɶ ɜɫɟ ɩɨɥɹ ɜ ɤɚɠɞɨɣ ɫɬɪɨɤɟ.
Ɏɨɪɦɚɬ ɫ ɮɢɤɫɢɪɨɜɚɧɧɨɣ ɞɥɢɧɨɣ ɫɬɪɨɤɢ ɬɚɤɠɟ ɤɚɫɚɟɬɫɹ ɪɚɛɨɬɵ ɫɨ ɡɧɚɱɟɧɢɹɦɢ
NULL, ɤɚɤ ɨɩɢɫɚɧɨ ɧɢɠɟ. ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɮɨɪɦɚɬ ɮɢɤɫɢɪɨɜɚɧɧɨɣ ɞɥɢɧɵ ɧɟ
ɪɚɛɨɬɚɟɬ, ɟɫɥɢ ɢɫɩɨɥɶɡɭɟɬɫɹ ɦɧɨɝɨɛɚɣɬɧɵɣ ɧɚɛɨɪ ɫɢɦɜɨɥɨɜ (ɧɚɩɪɢɦɟɪ, Unicode).
„ȿɫɥɢ
Ɉɛɪɚɛɨɬɤɚ ɡɧɚɱɟɧɢɣ NULL ɜɚɪɶɢɪɭɟɬɫɹ ɜ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɩɪɢɦɟɧɹɟɦɵɯ ɨɩɰɢɣ FIELDS
ɢ LINES:
ɡɧɚɱɟɧɢɹɯ FIELDS ɢ LINES ɩɨ ɭɦɨɥɱɚɧɢɸ NULL ɩɢɲɟɬɫɹ ɤɚɤ ɡɧɚɱɟɧɢɟ ɩɨɥɹ ɜ
ɜɢɞɟ \N ɞɥɹ ɜɵɜɨɞɚ ɢ ɷɬɨ ɠɟ ɡɧɚɱɟɧɢɟ \N ɱɢɬɚɟɬɫɹ ɤɚɤ NULL ɩɪɢ ɜɜɨɞɟ (ɩɪɟɞɩɨɥɚɝɚɹ, ɱɬɨ ɫɢɦɜɨɥ ESCAPED BY ɭɫɬɚɧɨɜɥɟɧ ɜ ‘\’).
„ɉɪɢ
ɧɟ ɩɭɫɬɨɣ, ɬɨ ɩɨɥɟ, ɫɨɞɟɪɠɚɳɟɟ ɥɢɬɟɪɚɥɶɧɨɟ ɫɥɨɜɨ NULL,
ɱɢɬɚɟɬɫɹ ɤɚɤ ɡɧɚɱɟɧɢɟ NULL. ɗɬɨ ɨɬɥɢɱɚɟɬɫɹ ɨɬ ɫɥɭɱɚɹ, ɤɨɝɞɚ ɫɥɨɜɨ NULL ɨɝɪɚɧɢɱɟɧɨ
ɫɢɦɜɨɥɚɦɢ FIELDS ENCLOSED BY, ɤɨɝɞɚ ɡɧɚɱɟɧɢɟ ɱɢɬɚɟɬɫɹ, ɤɚɤ ɫɬɪɨɤɚ 'NULL'.
„ȿɫɥɢ FIELDS ENCLOSED BY
„ȿɫɥɢ FIELDS ESCAPED BY
ɩɭɫɬɨɟ, NULL ɩɢɲɟɬɫɹ ɤɚɤ ɫɥɨɜɨ NULL.
230
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɮɨɪɦɚɬɟ ɫ ɮɢɤɫɢɪɨɜɚɧɧɨɣ ɞɥɢɧɨɣ ɫɬɪɨɤɢ (ɱɬɨ ɫɥɭɱɚɟɬɫɹ, ɤɨɝɞɚ ɢ FIELDS
TERMINATED BY, ɢ FIELDS ENCLOSED BY ɩɭɫɬɵɟ) NULL ɡɚɩɢɫɵɜɚɟɬɫɹ ɤɚɤ ɩɭɫɬɚɹ ɫɬɪɨɤɚ. Ɉɬɦɟɬɢɦ, ɱɬɨ ɷɬɨ ɩɪɢɜɨɞɢɬ ɤ ɬɨɦɭ, ɱɬɨ ɡɧɚɱɟɧɢɹ NULL ɢ ɩɭɫɬɵɟ ɫɬɪɨɤɢ ɜ ɬɚɛ-
„ɉɪɢ
ɥɢɰɟ ɫɬɚɧɨɜɹɬɫɹ ɧɟɪɚɡɥɢɱɢɦɵ ɩɪɢ ɡɚɩɢɫɢ ɜ ɮɚɣɥ, ɩɨɫɤɨɥɶɤɭ ɜ ɨɛɨɢɯ ɫɥɭɱɚɹɯ ɩɢɲɭɬɫɹ ɩɭɫɬɵɟ ɫɬɪɨɤɢ. ȿɫɥɢ ɜɚɦ ɧɟɨɛɯɨɞɢɦɨ ɞɟɥɚɬɶ ɪɚɡɥɢɱɢɟ ɦɟɠɞɭ ɧɢɦɢ, ɢɡɛɟɝɚɣɬɟ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɮɨɪɦɚɬɚ ɫ ɮɢɤɫɢɪɨɜɚɧɧɨɣ ɞɥɢɧɨɣ ɫɬɪɨɤɢ.
ɇɢɠɟ ɩɪɟɞɫɬɚɜɥɟɧɵ ɧɟɤɨɬɨɪɵɟ ɫɥɭɱɚɢ, ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɦɵɟ LOAD DATA INFILE:
ɮɢɤɫɢɪɨɜɚɧɧɨɣ ɞɥɢɧɵ (FIELDS TERMINATED BY ɢ FIELDS ENCLOSED BY ɩɭɫɬɵɟ) ɩɪɢ ɧɚɥɢɱɢɢ ɫɬɨɥɛɰɨɜ ɬɢɪɚ TEXT ɢɥɢ BLOB.
„ɋɬɪɨɤɢ
ɜɵ ɭɤɚɡɵɜɚɟɬɟ ɪɚɡɞɟɥɢɬɟɥɶ, ɤɨɬɨɪɵɣ ɫɨɜɩɚɞɚɟɬ ɫ ɩɪɟɮɢɤɫɨɦ ɞɪɭɝɨɝɨ, LOAD
DATA INFILE ɧɟ ɦɨɠɟɬ ɩɪɚɜɢɥɶɧɨ ɢɧɬɟɪɩɪɟɬɢɪɨɜɚɬɶ ɜɯɨɞɧɨɣ ɩɨɬɨɤ. ɇɚɩɪɢɦɟɪ,
„ȿɫɥɢ
ɫɥɟɞɭɸɳɢɣ ɜɚɪɢɚɧɬ ɩɪɢɜɟɞɟɬ ɤ ɩɪɨɛɥɟɦɚɦ:
FIELDS TERMINATED BY '"' ENCLOSED BY '"'
„ȿɫɥɢ FIELDS ESCAPED BY
ɩɭɫɬ, ɡɧɚɱɟɧɢɹ ɩɨɥɟɣ, ɤɨɬɨɪɵɟ ɜɤɥɸɱɚɸɬ ɜ ɫɟɛɹ ɫɢɦɜɨɥɵ
FIELDS ENCLOSED BY ɢɥɢ LINES TERMINATED BY ɫ ɩɨɫɥɟɞɭɸɳɢɦ ɫɢɦɜɨɥɨɦ LINES
TERMINATED BY, ɡɚɫɬɚɜɹɬ LOAD DATA INFILE ɫɥɢɲɤɨɦ ɪɚɧɨ ɩɪɟɤɪɚɬɢɬɶ ɱɬɟɧɢɟ ɮɚɣɥɚ ɢɥɢ ɫɬɪɨɤɢ. ɗɬɨ ɩɪɨɢɡɨɣɞɟɬ ɩɨɬɨɦɭ, ɱɬɨ LOAD DATA INFILE ɧɟ ɦɨɠɟɬ ɩɪɚɜɢɥɶɧɨ
ɨɩɪɟɞɟɥɢɬɶ, ɝɞɟ ɡɚɜɟɪɲɚɟɬɫɹ ɡɧɚɱɟɧɢɟ ɩɨɥɹ ɢɥɢ ɫɬɪɨɤɢ.
ɋɥɟɞɭɸɳɢɣ ɩɪɢɦɟɪ ɡɚɝɪɭɠɚɟɬ ɜɫɟ ɫɬɨɥɛɰɵ ɬɚɛɥɢɰɵ persondata:
mysql> LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata;
ɉɨ ɭɦɨɥɱɚɧɢɸ, ɟɫɥɢ ɜ ɤɨɧɰɟ ɨɩɟɪɚɬɨɪɚ LOAD DATA INFILE ɧɟ ɩɪɢɜɟɞɟɧ ɫɩɢɫɨɤ ɫɬɨɥɛɰɨɜ, ɨɠɢɞɚɟɬɫɹ, ɱɬɨ ɜɨ ɜɯɨɞɹɳɟɣ ɫɬɪɨɤɟ ɫɨɞɟɪɠɚɬɫɹ ɩɨɥɹ ɞɥɹ ɤɚɠɞɨɝɨ ɫɬɨɥɛɰɚ ɬɚɛɥɢɰɵ.
ȿɫɥɢ ɜɵ ɯɨɬɢɬɟ ɡɚɝɪɭɡɢɬɶ ɬɨɥɶɤɨ ɧɟɤɨɬɨɪɵɟ ɢɡ ɫɬɨɥɛɰɨɜ ɬɚɛɥɢɰɵ, ɭɤɚɡɵɜɚɣɬɟ ɫɩɢɫɨɤ
ɫɬɨɥɛɰɨɜ:
mysql> LOAD DATA INFILE 'persondata.txt'
-> INTO TABLE persondata (col1,col2,...);
ȼɵ ɬɚɤɠɟ ɞɨɥɠɧɵ ɭɤɚɡɵɜɚɬɶ ɫɩɢɫɨɤ ɫɬɨɥɛɰɨɜ, ɟɫɥɢ ɩɨɪɹɞɨɤ ɩɨɥɟɣ ɜɨ ɜɯɨɞɧɨɦ ɮɚɣɥɟ
ɨɬɥɢɱɚɟɬɫɹ ɨɬ ɩɨɪɹɞɤɚ ɫɬɨɥɛɰɨɜ ɜ ɬɚɛɥɢɰɟ. ȼ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ MySQL ɧɟ ɫɦɨɠɟɬ ɭɫɬɚɧɨɜɢɬɶ ɫɨɨɬɜɟɬɫɬɜɢɟ ɦɟɠɞɭ ɜɯɨɞɧɵɦɢ ɩɨɥɹɦɢ ɢ ɫɬɨɥɛɰɚɦɢ ɬɚɛɥɢɰ.
ȿɫɥɢ ɜɯɨɞɧɨɣ ɮɚɣɥ ɢɦɟɟɬ ɫɥɢɲɤɨɦ ɦɚɥɨ ɩɨɥɟɣ ɜ ɫɬɪɨɤɚɯ, ɬɨ ɧɟɞɨɫɬɚɸɳɢɦ ɫɬɨɥɛɰɚɦ
ɛɭɞɭɬ ɩɪɢɫɜɨɟɧɵ ɡɧɚɱɟɧɢɹ ɩɨ ɭɦɨɥɱɚɧɢɸ. ɉɪɢɫɜɨɟɧɢɟ ɡɧɚɱɟɧɢɣ ɩɨ ɭɦɨɥɱɚɧɢɸ ɨɩɢɫɚɧɨ
ɜ ɪɚɡɞɟɥɟ 6.2.5.
ɉɭɫɬɵɟ ɡɧɚɱɟɧɢɹ ɩɨɥɟɣ ɢɧɬɟɪɩɪɟɬɢɪɭɸɬɫɹ ɢɧɚɱɟ, ɱɟɦ ɩɪɨɩɭɳɟɧɧɵɟ:
„Ⱦɥɹ ɫɬɪɨɤɨɜɵɯ ɬɢɩɨɜ – ɫɬɨɥɛɰɭ ɩɪɢɫɜɚɢɜɚɟɬɫɹ ɩɭɫɬɚɹ ɫɬɪɨɤɚ.
„Ⱦɥɹ ɱɢɫɥɨɜɵɯ ɬɢɩɨɜ – ɫɬɨɥɛɰɭ ɩɪɢɫɜɚɢɜɚɟɬɫɹ 0.
„Ⱦɥɹ ɬɢɩɨɜ ɞɚɬɵ ɢ ɜɪɟɦɟɧɢ – ɫɬɨɥɛɟɰ ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɜ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɟ ɬɢɩɭ
“ɧɭɥɟɜɨɟ” ɡɧɚɱɟɧɢɟ. ɋɦ. ɪɚɡɞɟɥ 4.3.
ɗɬɨ ɬɟ ɠɟ ɡɧɚɱɟɧɢɹ, ɱɬɨ ɩɨɥɭɱɚɸɬɫɹ ɜ ɪɟɡɭɥɶɬɚɬɟ ɹɜɧɨɝɨ ɩɪɢɫɜɨɟɧɢɹ ɩɭɫɬɨɣ ɫɬɪɨɤɢ
ɫɬɨɥɛɰɚɦ ɷɬɢɯ ɬɢɩɨɜ ɜ ɨɩɟɪɚɬɨɪɚɯ INSERT ɢɥɢ UPDATE.
Ɂɧɚɱɟɧɢɹ ɫɬɨɥɛɰɨɜ ɬɢɩɚ TIMESTAMP ɭɫɬɚɧɚɜɥɢɜɚɸɬɫɹ ɜ ɬɟɤɭɳɭɸ ɞɚɬɭ ɢ ɜɪɟɦɹ, ɬɨɥɶɤɨ
ɟɫɥɢ ɢɦ ɩɪɢɫɜɚɢɜɚɟɬɫɹ ɡɧɚɱɟɧɢɟ NULL (ɬɨ ɟɫɬɶ, \N), ɥɢɛɨ ɟɫɥɢ ɫɬɨɥɛɟɰ ɷɬɨɝɨ ɬɢɩɚ ɩɪɨɩɭɳɟɧ ɜ ɫɩɢɫɤɟ ɩɨɥɟɣ, ɟɫɥɢ ɫɩɢɫɨɤ ɩɨɥɟɣ ɩɪɢɜɟɞɟɧ.
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
231
LOAD DATA INFILE ɪɚɫɫɦɚɬɪɢɜɚɟɬ ɜɟɫɶ ɜɜɨɞ, ɤɚɤ ɫɬɪɨɤɨɜɵɣ, ɩɨɷɬɨɦɭ ɜɵ ɧɟ ɦɨɠɟɬɟ
ɢɫɩɨɥɶɡɨɜɚɬɶ ɱɢɫɥɨɜɵɟ ɡɧɚɱɟɧɢɹ ɞɥɹ ɫɬɨɥɛɰɨɜ ɬɢɩɚ ENUM ɢɥɢ SET, ɤɚɤ ɷɬɨ ɞɨɩɭɫɤɚɟɬɫɹ ɜ
ɨɩɟɪɚɬɨɪɚɯ INSERT. ȼɫɟ ɡɧɚɱɟɧɢɹ ENUM ɢɥɢ SET ɞɨɥɠɧɵ ɭɤɚɡɵɜɚɬɶɫɹ ɤɚɤ ɫɬɪɨɤɢ!
Ʉɨɝɞɚ ɨɩɟɪɚɬɨɪ LOAD DATA INFILE ɡɚɜɟɪɲɚɟɬ ɪɚɛɨɬɭ, ɨɧ ɜɨɡɜɪɚɳɚɟɬ ɢɧɮɨɪɦɚɰɢɨɧɧɭɸ ɫɬɪɨɤɭ ɜ ɫɥɟɞɭɸɳɟɦ ɮɨɪɦɚɬɟ:
Records: 1 Deleted: 0 Skipped: 0 Warnings: 0
ȿɫɥɢ ɜɵ ɪɚɛɨɬɚɟɬɟ ɫ ɩɪɨɝɪɚɦɦɧɵɦ ɢɧɬɟɪɮɟɣɫɨɦ C API, ɬɨ ɦɨɠɟɬɟ ɩɨɥɭɱɢɬɶ ɢɧɮɨɪɦɚɰɢɸ ɨɛ ɷɬɨɦ ɨɩɟɪɚɬɨɪɟ, ɨɛɪɚɬɢɜɲɢɫɶ ɤ ɮɭɧɤɰɢɢ mysql_info().
ɉɪɟɞɭɩɪɟɠɞɟɧɢɹ, ɤɨɬɨɪɵɟ ɩɨɹɜɥɹɸɬɫɹ ɩɪɢ ɧɟɤɨɬɨɪɵɯ ɭɫɥɨɜɢɹɯ, ɬɚɤɢɟ ɠɟ, ɤɚɤ ɩɪɢ
ɜɫɬɚɜɤɟ ɡɧɚɱɟɧɢɣ ɨɩɟɪɚɬɨɪɨɦ INSERT (ɫɦ. ɪɚɡɞɟɥ 6.1.4), ɡɚ ɢɫɤɥɸɱɟɧɢɟɦ ɬɨɝɨ, ɱɬɨ LOAD
DATA INFILE ɤɪɨɦɟ ɧɢɯ ɝɟɧɟɪɢɪɭɟɬ ɩɪɟɞɭɩɪɟɠɞɟɧɢɹ ɨ ɬɨɦ, ɱɬɨ ɜɨ ɜɯɨɞɧɨɦ ɮɚɣɥɟ ɫɥɢɲɤɨɦ ɦɚɥɨ ɢɥɢ ɫɥɢɲɤɨɦ ɦɧɨɝɨ ɩɨɥɟɣ. ɉɪɟɞɭɩɪɟɠɞɟɧɢɹ ɧɢɝɞɟ ɧɟ ɫɨɯɪɚɧɹɸɬɫɹ, ɤɨɥɢɱɟɫɬɜɨ
ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ ɦɨɠɟɬ ɛɵɬɶ ɢɫɩɨɥɶɡɨɜɚɧɨ ɬɨɥɶɤɨ ɤɚɤ ɩɪɢɡɧɚɤ ɬɨɝɨ, ɱɬɨ ɜɫɟ ɩɪɨɲɥɨ ɭɫɩɟɲɧɨ.
ɇɚɱɢɧɚɹ ɫ MySQL 4.1.1, ɜɵ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ SHOW WARNINGS ɞɥɹ ɩɨɥɭɱɟɧɢɹ ɫɩɢɫɤɚ ɩɟɪɜɵɯ max_error_count ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ ɜ ɤɚɱɟɫɬɜɟ ɢɧɮɨɪɦɚɰɢɢ ɨ ɬɨɦ, ɱɬɨ ɩɪɢ
ɡɚɝɪɭɡɤɟ ɩɪɨɲɥɨ ɧɟ ɬɚɤ, ɤɚɤ ɧɚɞɨ. ɋɦ. ɪɚɡɞɟɥ 6.5.3.20.
Ⱦɨ MySQL 4.1.1 ɬɨɥɶɤɨ ɤɨɥɢɱɟɫɬɜɨ ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ ɛɵɥɨ ɩɪɢɡɧɚɤɨɦ ɬɨɝɨ, ɱɬɨ ɡɚɝɪɭɡɤɚ ɩɪɨɲɥɚ ɧɟ ɝɥɚɞɤɨ. ȿɫɥɢ ɜɵ ɩɨɥɭɱɚɟɬɟ ɩɪɟɞɭɩɪɟɠɞɟɧɢɟ ɢ ɯɨɬɢɬɟ ɡɧɚɬɶ ɬɨɱɧɨ,
ɩɨɱɟɦɭ ɨɧɨ ɩɨɹɜɢɥɢɫɶ, ɟɞɢɧɫɬɜɟɧɧɵɣ ɩɭɬɶ ɫɞɟɥɚɬɶ ɷɬɨ – ɫ ɩɨɦɨɳɶɸ SELECT...INTO
OUTFILE ɜɵɝɪɭɡɢɬɶ ɞɚɦɩ ɬɚɛɥɢɰɵ ɜ ɞɪɭɝɨɣ ɮɚɣɥ ɢ ɫɪɚɜɧɢɬɶ ɟɝɨ ɫ ɨɪɢɝɢɧɚɥɶɧɵɦ ɜɯɨɞɧɵɦ ɮɚɣɥɨɦ.
6.1.6. Ñèíòàêñèñ REPLACE
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] ɢɦɹ_ɬɚɛɥɢɰɵ [(ɢɦɹ_ɫɬɨɥɛɰɚ,...)]
VALUES ({ɜɵɪɚɠɟɧɢɟ | DEFAULT},...),(...),...
ɢɥɢ:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] ɢɦɹ_ɬɚɛɥɢɰɵ
SET ɢɦɹ_ɫɬɨɥɛɰɚ={ɜɵɪɚɠɟɧɢɟ | DEFAULT}, ...
ɢɥɢ:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] ɢɦɹ_ɬɚɛɥɢɰɵ [(ɢɦɹ_ɫɬɨɥɛɰɚ,...)]
SELECT ...
REPLACE ɪɚɛɨɬɚɟɬ ɬɨɱɧɨ ɬɚɤ ɠɟ, ɤɚɤ INSERT, ɡɚ ɢɫɤɥɸɱɟɧɢɟɦ ɬɨɝɨ, ɱɬɨ ɟɫɥɢ ɫɬɪɨɤɚ ɫ
ɬɟɦ ɠɟ ɡɧɚɱɟɧɢɟɦ ɩɟɪɜɢɱɧɨɝɨ ɢɥɢ ɭɧɢɤɚɥɶɧɨɝɨ ɤɥɸɱɚ ɜ ɬɚɛɥɢɰɟ ɫɭɳɟɫɬɜɭɟɬ, ɬɨ ɫɬɚɪɚɹ
ɫɬɪɨɤɚ ɭɞɚɥɹɟɬɫɹ ɩɟɪɟɞ ɜɫɬɚɜɤɨɣ ɧɨɜɨɣ. ɋɦ. ɪɚɡɞɟɥ 6.1.4.
ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɢɫɩɨɥɶɡɨɜɚɧɢɟ REPLACE ɧɟ ɢɦɟɟɬ ɫɦɵɫɥɚ, ɟɫɥɢ ɬɨɥɶɤɨ ɬɚɛɥɢɰɚ
ɧɟ ɢɦɟɟɬ ɢɧɞɟɤɫɨɜ PRIMARY KEY ɢɥɢ UNIQUE. ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɨɩɟɪɚɬɨɪ ɩɨɥɧɨɫɬɶɸ ɷɤɜɢɜɚɥɟɧɬɟɧ INSERT. ȼɵ ɦɨɠɟɬɟ ɨɛɪɚɳɚɬɶɫɹ ɤ ɡɧɚɱɟɧɢɹɦ ɜ ɫɬɚɪɨɣ ɫɬɪɨɤɟ ɢ ɢɫɩɨɥɶɡɨɜɚɬɶ ɢɯ ɜ
ɧɨɜɨɣ. Ɇɨɠɟɬ ɩɨɤɚɡɚɬɶɫɹ, ɱɬɨ ɷɬɨ ɦɨɠɧɨ ɛɵɥɨ ɞɟɥɚɬɶ ɢ ɜ ɧɟɤɨɬɨɪɵɯ ɫɬɚɪɵɯ ɜɟɪɫɢɹɯ
MySQL, ɨɞɧɚɤɨ ɬɚɦ ɫɨɞɟɪɠɚɥɚɫɶ ɨɲɢɛɤɚ, ɤɨɬɨɪɚɹ ɩɨɡɠɟ ɛɵɥɚ ɢɫɩɪɚɜɥɟɧɚ.
Ⱦɥɹ ɢɫɩɨɥɶɡɨɜɚɧɢɹ REPLACE ɧɟɨɛɯɨɞɢɦɨ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɢ INSERT ɢ DELETE ɞɥɹ ɬɚɛɥɢɰɵ.
232
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
Ɉɩɟɪɚɬɨɪ REPLACE ɜɨɡɜɪɚɳɚɟɬ ɤɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ, ɤɨɬɨɪɵɟ ɛɭɞɭɬ ɨɛɪɚɛɨɬɚɧɵ. ɗɬɨ ɤɨɥɢɱɟɫɬɜɨ ɪɚɜɧɨ ɫɭɦɦɟ ɭɞɚɥɟɧɧɵɯ ɢ ɜɫɬɚɜɥɟɧɧɵɯ ɫɬɪɨɤ. ȿɫɥɢ ɤɨɥɢɱɟɫɬɜɨ ɛɨɥɶɲɟ 1 ɞɥɹ
ɨɞɧɨɫɬɪɨɱɧɨɝɨ ɨɩɟɪɚɬɨɪɚ REPLACE, ɷɬɨ ɡɧɚɱɢɬ, ɱɬɨ ɫɬɪɨɤɚ ɛɵɥɚ ɜɫɬɚɜɥɟɧɚ ɢ ɧɢ ɨɞɧɨɣ
ɫɬɪɨɤɢ ɧɟ ɛɵɥɨ ɭɞɚɥɟɧɨ ɩɟɪɟɞ ɜɫɬɚɜɤɨɣ. ɋɭɳɟɫɬɜɭɟɬ ɜɨɡɦɨɠɧɨɫɬɶ, ɱɬɨ ɨɞɧɚ ɧɨɜɚɹ ɫɬɪɨɤɚ ɡɚɦɟɧɢɬ ɛɨɥɟɟ ɨɞɧɨɣ ɫɬɚɪɨɣ, ɟɫɥɢ ɭ ɬɚɛɥɢɰɵ ɟɫɬɶ ɧɟɫɤɨɥɶɤɨ ɭɧɢɤɚɥɶɧɵɯ ɢɧɞɟɤɫɨɜ ɢ
ɧɨɜɚɹ ɫɬɪɨɤɚ ɩɨ ɡɧɚɱɟɧɢɸ ɢɧɞɟɤɫɧɵɯ ɤɥɸɱɟɣ ɪɚɡɧɵɯ ɭɧɢɤɚɥɶɧɵɯ ɢɧɞɟɤɫɨɜ ɞɭɛɥɢɪɭɟɬ
ɛɨɥɟɟ ɱɟɦ ɨɞɧɭ ɫɬɚɪɭɸ ɫɬɪɨɤɭ.
ɋɱɟɬɱɢɤ ɨɛɪɚɛɨɬɚɧɧɵɯ ɫɬɪɨɤ ɩɨɡɜɨɥɹɟɬ ɥɟɝɤɨ ɨɩɪɟɞɟɥɢɬɶ, ɜɵɩɨɥɧɢɥ ɥɢ ɨɩɟɪɚɬɨɪ
REPLACE ɬɨɥɶɤɨ ɞɨɛɚɜɥɟɧɢɟ ɧɨɜɨɣ ɫɬɪɨɤɢ, ɢɥɢ ɩɪɨɢɡɜɨɞɢɥ ɬɚɤɠɟ ɡɚɦɟɧɭ. ɉɪɢ ɡɧɚɱɟɧɢɢ 1
ɜɵɩɨɥɧɹɥɚɫɶ ɬɨɥɶɤɨ ɜɫɬɚɜɤɚ, ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ – ɡɚɦɟɧɚ.
ȿɫɥɢ ɜɵ ɩɨɥɶɡɭɟɬɟɫɶ ɩɪɨɝɪɚɦɦɧɵɦ ɢɧɬɟɪɮɟɣɫɨɦ C API, ɫɱɟɬɱɢɤ ɨɛɪɚɛɨɬɚɧɧɵɯ ɫɬɪɨɤ
ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɜɵɡɨɜɨɦ ɮɭɧɤɰɢɢ mysql_affected_rows().
ɇɢɠɟ ɩɪɢɜɟɞɟɧɨ ɛɨɥɟɟ ɞɟɬɚɥɶɧɨɟ ɨɩɢɫɚɧɢɟ ɩɪɢɦɟɧɹɟɦɨɝɨ ɚɥɝɨɪɢɬɦɚ (ɷɬɨ ɤɚɫɚɟɬɫɹ
ɬɚɤɠɟ ɢ LOAD DATA...REPLACE):
1. ȼɵɩɨɥɧɹɟɬɫɹ ɩɨɩɵɬɤɚ ɜɫɬɚɜɤɢ ɧɨɜɨɣ ɫɬɪɨɤɢ ɜ ɬɚɛɥɢɰɭ.
2. ȿɫɥɢ ɜɫɬɚɜɤɚ ɧɟ ɭɞɚɟɬɫɹ ɢɡ-ɡɚ ɞɭɛɥɢɪɨɜɚɧɢɹ ɩɟɪɜɢɱɧɨɝɨ ɢɥɢ ɭɧɢɤɚɥɶɧɵɯ ɤɥɸɱɟɣ, ɬɨ:
ɚ) ɤɨɧɮɥɢɤɬɭɸɳɢɟ ɫɬɪɨɤɢ ɭɞɚɥɹɸɬɫɹ ɢɡ ɬɚɛɥɢɰɵ;
ɛ) ɜɵɩɨɥɧɹɟɬɫɹ ɧɨɜɚɹ ɩɨɩɵɬɤɚ ɜɫɬɚɜɤɢ ɫɬɪɨɤɢ.
6.1.7. Ñèíòàêñèñ SELECT
SELECT
[ALL | DISTINCT | DISTINCTROW]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] ɜɵɪɚɠɟɧɢɟ_select,...
[INTO OUTFILE 'ɢɦɹ_ɮɚɣɥɚ' ɨɩɰɢɢ_ɷɤɫɩɨɪɬɚ
| INTO DUMPFILE 'ɢɦɹ_ɮɚɣɥɚ']
[FROM ɬɚɛɥɢɱɧɵɟ_ɫɫɵɥɤɢ
[WHERE ɨɩɪɟɞɟɥɟɧɢɟ_where]
[GROUP BY {ɢɦɹ_ɫɬɨɥɛɰɚ | ɜɵɪɚɠɟɧɢɟ | ɩɨɡɢɰɢɹ}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING ɨɩɪɟɞɟɥɟɧɢɟ_where]
[ORDER BY {ɢɦɹ_ɫɬɨɥɛɰɚ | ɜɵɪɚɠɟɧɢɟ | ɩɨɡɢɰɢɹ}
[ASC | DESC] ,...]
[LIMIT [ɫɦɟɳɟɧɢɟ,{] ɤɨɥɢɱɟɫɬɜɨ_ɫɬɪɨɤ | ɤɨɥɢɱɟɫɬɜɨ_ɫɬɪɨɤ OFFSET ɫɦɟɳɟɧɢɟ}]
[PROCEDURE ɢɦɹ_ɩɪɨɰɟɞɭɪɵ(ɫɩɢɫɨɤ_ɚɪɝɭɦɟɧɬɨɜ)]
[FOR UPDATE | LOCK IN SHARE MODE]]
SELECT ɩɪɢɦɟɧɹɟɬɫɹ ɞɥɹ ɢɡɜɥɟɱɟɧɢɹ ɫɬɪɨɤ ɢɡ ɨɞɧɨɣ ɢɥɢ ɛɨɥɟɟ ɬɚɛɥɢɰ. ɉɨɞɞɟɪɠɤɚ
ɨɩɟɪɚɬɨɪɨɜ UNION ɢ ɩɨɞɡɚɩɪɨɫɨɜ ɞɨɫɬɭɩɧɚ, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɣ MySQL 4.0 ɢ 4.1, ɫɨɨɬɜɟɬɫɬɜɟɧɧɨ. ɋɦ. ɪɚɡɞɟɥɵ 6.1.7.2 ɢ 6.1.8.
„Ʉɚɠɞɨɟ ɜɵɪɚɠɟɧɢɟ ɜɵɪɚɠɟɧɢɟ_select ɭɤɚɡɵɜɚɟɬ ɫɬɨɥɛɟɰ, ɤɨɬɨɪɵɣ ɧɟɨɛɯɨɞɢɦɨ
ɢɡɜɥɟɱɶ.
„ɬɚɛɥɢɱɧɵɟ_ɫɫɵɥɤɢ ɭɤɚɡɵɜɚɸɬ ɬɚɛɥɢɰɭ ɢɥɢ ɬɚɛɥɢɰɵ, ɢɡ ɤɨɬɨɪɵɯ ɢɡɜɥɟɤɚɸɬɫɹ
ɫɬɪɨɤɢ. ɋɨɨɬɜɟɬɫɬɜɭɸɳɢɣ ɫɢɧɬɚɤɫɢɫ ɨɩɢɫɚɧ ɜ ɪɚɡɞɟɥɟ 6.1.8.
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
233
ɡɚɞɚɟɬ ɥɸɛɵɟ ɭɫɥɨɜɢɹ, ɤɨɬɨɪɵɦ ɞɨɥɠɧɵ ɭɞɨɜɥɟɬɜɨɪɹɬɶ ɜɵɛɪɚɧɧɵɟ ɫɬɪɨɤɢ.
SELECT ɬɚɤɠɟ ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɞɥɹ ɢɡɜɥɟɱɟɧɢɹ ɜɵɱɢɫɥɹɟɦɵɯ ɫɬɪɨɤ ɛɟɡ ɨɛɪɚɳɟɧɢɹ ɤ ɤɚɤɨɣ-ɥɢɛɨ ɬɚɛɥɢɰɟ.
ɇɚɩɪɢɦɟɪ:
„ɨɩɪɟɞɟɥɟɧɢɟ_where
mysql> SELECT 1 + 1;
-> 2
ȼɫɟ ɢɫɩɨɥɶɡɭɟɦɵɟ ɜ ɨɩɟɪɚɬɨɪɟ ɤɨɧɫɬɪɭɤɰɢɢ ɞɚɸɬɫɹ ɬɨɱɧɨ ɜ ɬɨɦ ɩɨɪɹɞɤɟ, ɤɚɤ ɩɪɢɜɟɞɟɧɨ ɜ ɨɩɢɫɚɧɢɢ ɫɢɧɬɚɤɫɢɫɚ. ɇɚɩɪɢɦɟɪ, ɤɨɧɫɬɪɭɤɰɢɹ HAVING ɞɨɥɠɧɚ ɫɥɟɞɨɜɚɬɶ ɡɚ ɤɨɧɫɬɪɭɤɰɢɟɣ GROUP BY ɢ ɩɟɪɟɞ ORDER BY.
ɦɨɠɟɬ ɛɵɬɶ ɞɚɧ ɩɫɟɜɞɨɧɢɦ AS ɢɦɹ_ɩɫɟɜɞɨɧɢɦɚ.
ɉɫɟɜɞɨɧɢɦ ɢɫɩɨɥɶɡɭɟɬɫɹ ɤɚɤ ɢɦɹ ɫɬɨɥɛɰɚ, ɡɚɞɚɧɧɨɝɨ ɜɵɪɚɠɟɧɢɟɦ, ɢ ɦɨɠɟɬ ɩɪɢɫɭɬɫɬɜɨɜɚɬɶ ɜ ɤɨɧɫɬɪɭɤɰɢɹɯ GROUP BY, ORDER BY ɢ HAVING, ɧɚɩɪɢɦɟɪ:
„ȼɵɪɚɠɟɧɢɸ ɜɵɪɚɠɟɧɢɟ_select
mysql> SELECT CONCAT(last_name,', ',first_name) AS full_name
-> FROM mytable ORDER BY full_name;
ɉɪɢ ɩɪɢɫɜɨɟɧɢɢ ɩɫɟɜɞɨɧɢɦɚ ɫɬɨɥɛɰɭ ɭɤɚɡɵɜɚɬɶ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ AS ɧɟ ɨɛɹɡɚɬɟɥɶɧɨ. ɉɪɟɞɵɞɭɳɢɣ ɩɪɢɦɟɪ ɦɨɠɟɬ ɛɵɬɶ ɩɟɪɟɩɢɫɚɧ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
mysql> SELECT CONCAT(last_name,', ',first_name) full_name
-> FROM mytable ORDER BY full_name;
ɉɨɫɤɨɥɶɤɭ AS ɧɟɨɛɹɡɚɬɟɥɶɧɨ, ɡɞɟɫɶ ɦɨɠɟɬ ɜɨɡɧɢɤɧɭɬɶ ɨɞɧɚ ɬɨɧɤɚɹ ɩɪɨɛɥɟɦɚ, ɟɫɥɢ
ɜɵ ɡɚɛɭɞɟɬɟ ɩɨɫɬɚɜɢɬɶ ɡɚɩɹɬɭɸ ɦɟɠɞɭ ɞɜɭɦɹ ɜɵɪɚɠɟɧɢɹɦɢ SELECT: MySQL ɢɧɬɟɪɩɪɟɬɢɪɭɟɬ ɜɬɨɪɨɟ ɤɚɤ ɩɫɟɜɞɨɧɢɦ. ɇɚɩɪɢɦɟɪ, ɜ ɫɥɟɞɭɸɳɟɦ ɨɩɟɪɚɬɨɪɟ columnb
ɪɚɫɫɦɚɬɪɢɜɚɟɬɫɹ ɤɚɤ ɩɫɟɜɞɨɧɢɦ:
mysql> SELECT columna columnb FROM mytable;
ɞɨɩɭɫɤɚɟɬɫɹ ɭɤɚɡɚɧɢɟ ɩɫɟɜɞɨɧɢɦɚ ɫɬɨɥɛɰɚ ɜ ɤɨɧɫɬɪɭɤɰɢɢ WHERE, ɩɨɫɤɨɥɶɤɭ
ɡɧɚɱɟɧɢɟ ɫɬɨɥɛɰɚ ɟɳɟ ɦɨɠɟɬ ɛɵɬɶ ɧɟɨɩɪɟɞɟɥɟɧɧɵɦ, ɤɨɝɞɚ ɜɵɩɨɥɧɹɟɬɫɹ ɤɨɧɫɬɪɭɤɰɢɹ WHERE. ɋɦ. ɪɚɡɞɟɥ Ⱥ.1.4.
„ɇɟ
„Ʉɨɧɫɬɪɭɤɰɢɹ FROM ɬɚɛɥɢɱɧɵɟ_ɫɫɵɥɤɢ
ɩɟɪɟɱɢɫɥɹɟɬ ɬɚɛɥɢɰɵ, ɢɡ ɤɨɬɨɪɵɯ ɢɡɜɥɟɤɚɸɬɫɹ ɫɬɪɨɤɢ. ȿɫɥɢ ɜɵ ɭɤɚɡɵɜɚɟɬɟ ɛɨɥɟɟ ɱɟɦ ɨɞɧɭ ɬɚɛɥɢɰɭ, ɜɵɩɨɥɧɹɟɬɫɹ ɫɨɟɞɢɧɟɧɢɟ. ɂɧɮɨɪɦɚɰɢɸ ɨ ɫɢɧɬɚɤɫɢɫɟ ɫɨɟɞɢɧɟɧɢɣ ɦɨɠɧɨ ɧɚɣɬɢ ɜ ɪɚɡɞɟɥɟ 6.1.7.1. Ⱦɥɹ
ɤɚɠɞɨɣ ɢɡ ɩɟɪɟɱɢɫɥɟɧɧɵɯ ɬɚɛɥɢɰ ɦɨɠɧɨ ɭɤɚɡɚɬɶ ɧɟɨɛɹɡɚɬɟɥɶɧɵɣ ɩɫɟɜɞɨɧɢɦ.
ɢɦɹ_ɬɚɛɥɢɰɵ [[AS] ɩɫɟɜɞɨɧɢɦ]
[[USE INDEX (ɫɩɢɫɨɤ_ɤɥɸɱɟɣ)]
| [IGNORE INDEX (ɫɩɢɫɨɤ_ɤɥɸɱɟɣ)]
| [FORCE INDEX (ɫɩɢɫɨɤ_ɤɥɸɱɟɣ)]]
ɉɪɢɦɟɧɟɧɢɟ USE INDEX, IGNORE INDEX, FORCE INDEX ɞɥɹ ɭɤɚɡɚɧɢɹ ɩɨɞɫɤɚɡɨɤ ɨɩɬɢɦɢɡɚɬɨɪɭ ɩɨ ɢɫɩɨɥɶɡɨɜɚɧɢɸ ɢɧɞɟɤɫɨɜ ɨɩɢɫɚɧɨ ɜ ɪɚɡɞɟɥɟ 6.1.7.1.
ɇɚɱɢɧɚɹ ɫ MySQL 4.0.14, ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ SET max_seeks_for_key=ɡɧɚɱɟɧɢɟ,
ɤɚɤ ɚɥɶɬɟɪɧɚɬɢɜɧɵɣ ɫɩɨɫɨɛ ɡɚɫɬɚɜɢɬɶ MySQL ɩɪɟɞɩɨɱɟɫɬɶ ɩɨɢɫɤ ɩɨ ɤɥɸɱɭ ɜɦɟɫɬɨ
ɫɤɚɧɢɪɨɜɚɧɢɹ ɬɚɛɥɢɰɵ.
„Ʉ ɬɚɛɥɢɰɟ ɜɧɭɬɪɢ ɬɟɤɭɳɟɣ ɛɚɡɵ ɞɚɧɧɵɯ ɦɨɠɧɨ ɨɛɪɚɳɚɬɶɫɹ ɤɚɤ ɢɦɹ_ɬɚɛɥɢɰɵ,
ɥɢɛɨ ɤɚɤ ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.ɢɦɹ_ɬɚɛɥɢɰɵ ɞɥɹ ɹɜɧɨɝɨ ɭɤɚɡɚɧɢɹ ɛɚɡɵ ɞɚɧɧɵɯ. ȼɵ
ɦɨɠɟɬɟ ɫɨɫɥɚɬɶɫɹ ɧɚ ɫɬɨɥɛɟɰ ɤɚɤ ɢɦɹ_ɫɬɨɥɛɰɚ, ɢɦɹ_ɬɚɛɥɢɰɵ.ɢɦɹ_ɫɬɨɥɛɰɚ ɢɥɢ
234
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.ɢɦɹ_ɬɚɛɥɢɰɵ.ɢɦɹ_ɫɬɨɥɛɰɚ. ɉɪɟɮɢɤɫɵ ɫɬɨɥɛɰɨɜ ɢɦɹ_ɬɚɛɥɢɰɵ ɢɥɢ
ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.ɢɦɹ_ɬɚɛɥɢɰɵ ɭɤɚɡɵɜɚɬɶ ɧɟɨɛɹɡɚɬɟɥɶɧɨ, ɟɫɥɢ ɬɨɥɶɤɨ ɫɫɵɥɤɚ ɧɚ
ɫɬɨɥɛɟɰ ɧɟ ɹɜɥɹɟɬɫɹ ɧɟɨɞɧɨɡɧɚɱɧɨɣ. ȼ ɪɚɡɞɟɥɟ 2.2 ɩɪɟɞɫɬɚɜɥɟɧɵ ɩɪɢɦɟɪɵ ɧɟɨɞɧɨɡɧɚɱɧɵɯ ɫɫɵɥɨɤ, ɤɨɬɨɪɵɟ ɬɪɟɛɭɸɬ ɛɨɥɟɟ ɹɜɧɵɯ ɫɩɟɰɢɮɢɤɚɰɢɣ.
„ɇɚɱɢɧɚɹ ɫ MySQL 4.1.0, ɞɨɩɭɫɤɚɟɬɫɹ ɭɤɚɡɵɜɚɬɶ DUAL ɤɚɤ ɢɦɹ ɩɫɟɜɞɨɬɚɛɥɢɰɵ ɜ ɫɢɬɭɚɰɢɹɯ, ɤɨɝɞɚ ɧɟ ɧɭɠɧɵ ɫɫɵɥɤɢ ɧɚ ɪɟɚɥɶɧɵɟ ɬɚɛɥɢɰɵ:
mysql> SELECT 1 + 1 FROM DUAL;
-> 2
DUAL ɜɜɟɞɟɧɨ ɢɫɤɥɸɱɢɬɟɥɶɧɨ ɫɨɜɦɟɫɬɢɦɨɫɬɢ ɪɚɞɢ. ɇɟɤɨɬɨɪɵɟ ɞɪɭɝɢɟ ɫɟɪɜɟɪɵ ɛɚɡ
ɞɚɧɧɵɯ ɬɪɟɛɭɸɬ ɬɚɤɨɝɨ ɫɢɧɬɚɤɫɢɫɚ.
„ɋɫɵɥɤɚ ɧɚ ɬɚɛɥɢɰɭ ɦɨɠɟɬ ɛɵɬɶ ɡɚɦɟɧɟɧɚ ɩɫɟɜɞɨɧɢɦɨɦ: ɢɦɹ_ɬɚɛɥɢɰɵ [AS]
ɢɦɹ_ɩɫɟɜɞɨɧɢɦɚ.
mysql> SELECT t1.name, t2.salary FROM employee AS t1, info AS t2
-> WHERE t1.name = t2.name;
mysql> SELECT t1.name, t2.salary FROM employee t1, info t2
-> WHERE t1.name = t2.name;
ɤɨɧɫɬɪɭɤɰɢɢ WHERE ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɥɸɛɵɟ ɮɭɧɤɰɢɢ, ɩɨɞɞɟɪɠɢɜɚɟɦɵɟ
MySQL, ɡɚ ɢɫɤɥɸɱɟɧɢɟɦ ɚɝɪɟɝɚɬɧɵɯ (ɢɬɨɝɨɜɵɯ) ɮɭɧɤɰɢɣ. ɋɦ. ɝɥɚɜɭ 5.
„ɇɚ ɫɬɨɥɛɰɵ, ɜɵɛɪɚɧɧɵɟ ɞɥɹ ɜɵɜɨɞɚ, ɦɨɠɧɨ ɫɫɵɥɚɬɶɫɹ ɜ ɤɨɧɫɬɪɭɤɰɢɹɯ ORDER BY ɢ
GROUP BY, ɢɫɩɨɥɶɡɭɹ ɩɪɢ ɷɬɨɦ ɢɦɟɧɚ ɫɬɨɥɛɰɨɜ, ɩɫɟɜɞɨɧɢɦɵ ɫɬɨɥɛɰɨɜ ɥɢɛɨ ɢɯ ɧɨɦɟɪɚ ɩɨɡɢɰɢɣ. ɉɨɡɢɰɢɢ ɫɬɨɥɛɰɨɜ ɭɤɚɡɵɜɚɸɬɫɹ ɰɟɥɵɦɢ ɱɢɫɥɚɦɢ, ɧɚɱɢɧɚɹ ɫ 1:
„ȼ
mysql> SELECT college, region, seed FROM tournament
-> ORDER BY region, seed;
mysql> SELECT college, region AS r, seed AS s FROM tournament
-> ORDER BY r, s;
mysql> SELECT college, region, seed FROM tournament
-> ORDER BY 2, 3;
ɑɬɨɛɵ ɫɨɪɬɢɪɨɜɚɬɶ ɜ ɨɛɪɚɬɧɨɦ ɩɨɪɹɞɤɟ, ɜ ɤɨɧɫɬɪɭɤɰɢɢ ORDER BY ɩɨɬɪɟɛɭɟɬɫɹ ɞɨɛɚɜɢɬɶ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ DESC ɤ ɢɦɟɧɢ ɫɬɨɥɛɰɚ, ɩɨ ɤɨɬɨɪɨɦɭ ɜɵɩɨɥɧɹɟɬɫɹ ɫɨɪɬɢɪɨɜɤɚ. ɉɨ ɭɦɨɥɱɚɧɢɸ ɩɪɢɦɟɧɹɟɬɫɹ ɩɨɪɹɞɨɤ ɫɨɪɬɢɪɨɜɤɢ ɩɨ ɜɨɡɪɚɫɬɚɧɢɸ. ɗɬɨ
ɦɨɠɧɨ ɭɤɚɡɚɬɶ ɹɜɧɨ ɤɥɸɱɟɜɵɦ ɫɥɨɜɨɦ ASC.
ɉɪɢɦɟɧɟɧɢɟ ɧɨɦɟɪɨɜ ɩɨɡɢɰɢɣ ɫɬɨɥɛɰɨɜ ɫɱɢɬɚɟɬɫɹ ɭɫɬɚɪɟɜɲɢɦ, ɩɨɫɤɨɥɶɤɭ ɷɬɨɬ
ɫɢɧɬɚɤɫɢɫ ɢɫɤɥɸɱɟɧ ɢɡ ɫɬɚɧɞɚɪɬɚ SQL.
„ȿɫɥɢ ɢɫɩɨɥɶɡɭɟɬɫɹ GROUP BY, ɜɵɯɨɞɧɵɟ ɫɬɪɨɤɢ ɫɨɪɬɢɪɭɸɬɫɹ ɜ ɫɨɨɬɜɟɬɫɬɜɢɢ ɫɨ
ɫɬɨɥɛɰɚɦɢ, ɭɤɚɡɚɧɧɵɦɢ ɜ GROUP BY, ɤɚɤ ɟɫɥɢ ɛɵ ɛɵɥɨ ɭɤɚɡɚɧɨ ORDER BY ɞɥɹ ɬɟɯ ɠɟ
ɫɬɨɥɛɰɨɜ. MySQL ɢɦɟɟɬ ɪɚɫɲɢɪɟɧɧɵɣ ɜɚɪɢɚɧɬ ɤɨɧɫɬɪɭɤɰɢɢ GROUP BY, ɧɚɱɢɧɚɹ ɫ
ɜɟɪɫɢɢ ɫɟɪɜɟɪɚ 3.23.34, ɩɨɡɜɨɥɹɸɳɢɣ ɫɩɟɰɢɮɢɰɢɪɨɜɚɬɶ ASC ɢ DESC ɩɨɫɥɟ ɫɬɨɥɛɰɨɜ, ɧɚɡɜɚɧɧɵɯ ɜ ɤɨɧɫɬɪɭɤɰɢɢ:
SELECT a, COUNT(b) FROM test_table GROUP BY a DESC
„
MySQL ɪɚɫɲɢɪɹɟɬ ɩɪɢɦɟɧɟɧɢɟ GROUP BY, ɩɨɡɜɨɥɹɹ ɜɵɛɢɪɚɬɶ ɩɨɥɹ, ɧɟ ɩɟɪɟɱɢɫɥɟɧɧɵɟ ɜ ɤɨɧɫɬɪɭɤɰɢɢ GROUP BY. ȿɫɥɢ ɜɵ ɧɟ ɩɨɥɭɱɚɟɬɟ ɨɠɢɞɚɟɦɨɝɨ ɪɟɡɭɥɶɬɚɬɚ ɧɚ
ɡɚɩɪɨɫ, ɩɪɨɱɬɢɬɟ ɨɩɢɫɚɧɢɟ ɢɫɩɨɥɶɡɨɜɚɧɢɹ GROUP BY ɜ ɪɚɡɞɟɥɟ 5.9.
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
235
ɫ MySQL 4.1.1, GROUP BY ɞɨɩɭɫɤɚɟɬ ɦɨɞɢɮɢɤɚɬɨɪ WITH ROLLUP. ɋɦ. ɪɚɡɞɟɥ 5.9.
„Ʉɨɧɫɬɪɭɤɰɢɹ HAVING ɦɨɠɟɬ ɫɫɵɥɚɬɶɫɹ ɧɚ ɥɸɛɨɣ ɫɬɨɥɛɟɰ ɢɥɢ ɩɫɟɜɞɨɧɢɦ ɢɡ ɜɵɪɚɠɟɧɢɟ_select. Ɉɧɨ ɭɤɚɡɵɜɚɟɬɫɹ ɩɨɱɬɢ ɜ ɤɨɧɰɟ, ɧɟɩɨɫɪɟɞɫɬɜɟɧɧɨ ɩɟɪɟɞ ɨɬɩɪɚɜɤɨɣ
ɪɟɡɭɥɶɬɚɬɚ ɤɥɢɟɧɬɭ, ɛɟɡ ɨɩɬɢɦɢɡɚɰɢɢ (ɬɨɥɶɤɨ LIMIT ɧɚɯɨɞɢɬɫɹ ɩɨɫɥɟ HAVING).
„ɇɚɱɢɧɚɹ
ɢɫɩɨɥɶɡɭɣɬɟ HAVING ɞɥɹ ɷɥɟɦɟɧɬɨɜ, ɤɨɬɨɪɵɟ ɞɨɥɠɧɵ ɛɵɬɶ ɜ ɤɨɧɫɬɪɭɤɰɢɢ
WHERE. ɇɚɩɪɢɦɟɪ, ɧɟ ɞɟɥɚɣɬɟ ɬɚɤ:
„ɇɟ
mysql> SELECT ɢɦɹ_ɫɬɨɥɛɰɚ FROM ɢɦɹ_ɬɚɛɥɢɰɵ HAVING ɢɦɹ_ɫɬɨɥɛɰɚ > 0;
ȼɦɟɫɬɨ ɷɬɨɝɨ ɥɭɱɲɟ ɧɚɩɢɫɚɬɶ:
mysql> SELECT ɢɦɹ_ɫɬɨɥɛɰɚ FROM ɢɦɹ_ɬɚɛɥɢɰɵ WHERE ɢɦɹ_ɫɬɨɥɛɰɚ > 0;
„Ʉɨɧɫɬɪɭɤɰɢɹ HAVING
ɦɨɠɟɬ ɫɫɵɥɚɬɶɫɹ ɧɚ ɚɝɪɟɝɚɬɧɵɟ ɮɭɧɤɰɢɢ, ɜ ɬɨ ɜɪɟɦɹ ɤɚɤ
WHERE – ɧɟɬ:
mysql> SELECT user, MAX(salary) FROM users
-> GROUP BY user HAVING MAX(salary)>10;
Ɉɞɧɚɤɨ ɷɬɨ ɧɟ ɪɚɛɨɬɚɟɬ ɜ ɫɬɚɪɵɯ ɜɟɪɫɢɹɯ MySQL (ɞɨ 3.22.5). ȼɦɟɫɬɨ ɷɬɨɝɨ ɜɵ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɩɫɟɜɞɨɧɢɦ ɜ ɫɩɢɫɤɟ ɜɵɛɢɪɚɟɦɵɯ ɫɬɨɥɛɰɨɜ, ɱɬɨɛɵ ɫɨɫɥɚɬɶɫɹ ɧɚ
ɧɟɝɨ ɜ ɤɨɧɫɬɪɭɤɰɢɢ HAVING:
mysql> SELECT user, MAX(salary) AS max_salary FROM users
-> GROUP BY user HAVING max_salary>10;
„Ʉɨɧɫɬɪɭɤɰɢɹ LIMIT
ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɞɥɹ ɨɝɪɚɧɢɱɟɧɢɹ ɤɨɥɢɱɟɫɬɜɚ ɫɬɪɨɤ, ɜɨɡɜɪɚɳɚɟɦɵɯ ɨɩɟɪɚɬɨɪɨɦ SELECT. LIMIT ɩɪɢɧɢɦɚɟɬ ɨɞɢɧ ɢɥɢ ɞɜɚ ɱɢɫɥɨɜɵɯ ɚɪɝɭɦɟɧɬɚ, ɤɨɬɨɪɵɟ ɞɨɥɠɧɵ ɛɵɬɶ ɰɟɥɨɱɢɫɥɟɧɧɵɦɢ ɤɨɧɫɬɚɧɬɚɦɢ. Ʉɨɝɞɚ ɭɤɚɡɵɜɚɸɬɫɹ ɞɜɚ
ɚɪɝɭɦɟɧɬɚ, ɩɟɪɜɵɣ ɨɡɧɚɱɚɟɬ ɫɦɟɳɟɧɢɟ ɜ ɪɟɡɭɥɶɬɢɪɭɸɳɟɦ ɫɩɢɫɤɟ ɩɟɪɜɨɣ ɫɬɪɨɤɢ,
ɤɨɬɨɪɭɸ ɧɭɠɧɨ ɜɟɪɧɭɬɶ, ɚ ɜɬɨɪɨɣ – ɦɚɤɫɢɦɚɥɶɧɨɟ ɤɨɥɢɱɟɫɬɜɨ ɜɨɡɜɪɚɳɚɟɦɵɯ
ɫɬɪɨɤ. ɋɦɟɳɟɧɢɟ ɧɚɱɚɥɶɧɨɣ ɫɬɪɨɤɢ ɪɚɜɧɨ 0 (ɧɟ 1):
mysql> SELECT * FROM table LIMIT 5,10; # ɂɡɜɥɟɱɟɧɢɟ ɫɬɪɨɤ 6-15
Ⱦɥɹ ɫɨɜɦɟɫɬɢɦɨɫɬɢ ɫ PostgreSQL ɫɢɫɬɟɦɚ MySQL ɬɚɤɠɟ ɩɨɞɞɟɪɠɢɜɚɟɬ ɫɢɧɬɚɤɫɢɫ
LIMIT ɤɨɥɢɱɟɫɬɜɨ_ɫɬɪɨɤ OFFSET ɫɦɟɳɟɧɢɟ.
ɑɬɨɛɵ ɢɡɜɥɟɱɶ ɜɫɟ ɫɬɪɨɤɢ, ɧɚɱɢɧɚɹ ɫ ɨɩɪɟɞɟɥɟɧɧɨɝɨ ɫɦɟɳɟɧɢɹ ɢ ɞɨ ɤɨɧɰɚ ɪɟɡɭɥɶɬɢɪɭɸɳɟɝɨ ɧɚɛɨɪɚ, ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɤɚɤɨɟ-ɧɢɛɭɞɶ ɛɨɥɶɲɨɟ ɱɢɫɥɨ ɜɨ ɜɬɨɪɨɦ
ɩɚɪɚɦɟɬɪɟ. ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɢɡɜɥɟɤɚɟɬ ɜɫɟ ɫɬɪɨɤɢ, ɧɚɱɢɧɚɹ ɫ 96-ɣ ɢ ɞɨ ɩɨɫɥɟɞɧɟɣ:
mysql> SELECT * FROM table LIMIT 95,18446744073709551615;
ȿɫɥɢ ɭɤɚɡɚɧ ɨɞɢɧ ɚɪɝɭɦɟɧɬ, ɬɨ ɨɧ ɨɛɨɡɧɚɱɚɟɬ ɤɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ, ɤɨɬɨɪɵɟ ɧɭɠɧɨ
ɜɟɪɧɭɬɶ ɨɬ ɧɚɱɚɥɚ ɪɟɡɭɥɶɬɢɪɭɸɳɟɝɨ ɧɚɛɨɪɚ:
mysql> SELECT * FROM table LIMIT 5; # ɂɡɜɥɟɱɶ ɩɟɪɜɵɟ 5 ɫɬɪɨɤ
Ⱦɪɭɝɢɦɢ ɫɥɨɜɚɦɢ, LIMIT n ɷɤɜɢɜɚɥɟɧɬɧɨ LIMIT 0,n.
„ɋɢɧɬɚɤɫɢɫ SELECT...INTO
OUTFILE 'ɢɦɹ_ɮɚɣɥɚ' ɩɢɲɟɬ ɢɡɜɥɟɤɚɟɦɵɟ ɫɬɪɨɤɢ ɜ
ɮɚɣɥ. Ɏɚɣɥ ɫɨɡɞɚɟɬɫɹ ɧɚ ɯɨɫɬɟ ɫɟɪɜɟɪɚ, ɩɨɷɬɨɦɭ ɜɵ ɞɨɥɠɧɵ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ
FILE, ɱɬɨɛɵ ɢɫɩɨɥɶɡɨɜɚɬɶ ɷɬɭ ɮɨɪɦɭ SELECT. Ɏɚɣɥ ɧɟ ɞɨɥɠɟɧ ɫɭɳɟɫɬɜɨɜɚɬɶ ɧɚ
ɦɨɦɟɧɬ ɜɵɩɨɥɧɟɧɢɹ ɨɩɟɪɚɬɨɪɚ, ɱɬɨ ɩɪɟɞɨɬɜɪɚɳɚɟɬ ɫɥɭɱɚɣɧɨɟ ɪɚɡɪɭɲɟɧɢɟ ɜɚɠɧɵɯ
ɫɢɫɬɟɦɧɵɯ ɮɚɣɥɨɜ, ɜɪɨɞɟ /etc/passwd ɢɥɢ ɮɚɣɥɨɜ ɬɚɛɥɢɰ.
236
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
Ɉɩɟɪɚɬɨɪ SELECT...INTO OUTFILE ɩɪɟɞɧɚɡɧɚɱɟɧ ɝɥɚɜɧɵɦ ɨɛɪɚɡɨɦ ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ
ɩɨɡɜɨɥɹɬɶ ɛɵɫɬɪɨ ɜɵɝɪɭɠɚɬɶ ɞɚɦɩ ɬɚɛɥɢɰɵ ɧɚ ɦɚɲɢɧɟ ɫɟɪɜɟɪɚ. ȿɫɥɢ ɜɵ ɯɨɬɢɬɟ
ɫɨɡɞɚɬɶ ɪɟɡɭɥɶɬɢɪɭɸɳɢɣ ɮɚɣɥ ɧɚ ɯɨɫɬɟ ɤɥɢɟɧɬɚ, ɜɵ ɧɟ ɦɨɠɟɬɟ ɩɪɢɦɟɧɢɬɶ ɞɥɹ
ɷɬɨɝɨ SELECT...INTO OUTFILE. ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɜɵ ɞɨɥɠɧɵ ɜɦɟɫɬɨ ɷɬɨɝɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɧɚ ɤɥɢɟɧɬɫɤɨɣ ɦɚɲɢɧɟ ɱɬɨ-ɬɨ ɜɪɨɞɟ mysql -e "SELECT..." > ɢɦɹ_ɮɚɣɥɚ ɞɥɹ
ɝɟɧɟɪɚɰɢɢ ɮɚɣɥɚ.
SELECT...INTO OUTFILE – ɷɬɨ ɞɨɩɨɥɧɟɧɢɟ LOAD DATA INFILE. ɋɢɧɬɚɤɫɢɫ ɱɚɫɬɢ
ɨɩɰɢɢ_ɷɤɫɩɨɪɬɚ ɷɬɨɝɨ ɨɩɟɪɚɬɨɪɚ ɫɨɫɬɨɢɬ ɢɡ ɬɟɯ ɠɟ ɩɪɟɞɥɨɠɟɧɢɣ FIELDS ɢ LINES,
ɤɨɬɨɪɵɟ ɩɪɢɦɟɧɹɸɬɫɹ ɜ LOAD DATA INFILE. ɋɦ. ɪɚɡɞɟɥ 6.1.5.
FIELDS ESCAPED BY ɭɩɪɚɜɥɹɟɬ ɡɚɩɢɫɶɸ ɫɩɟɰɢɚɥɶɧɵɯ ɫɢɦɜɨɥɨɜ. ȿɫɥɢ ɚɪɝɭɦɟɧɬ
FIELDS ESCAPED BY ɧɟ ɩɭɫɬ, ɨɧ ɢɫɩɨɥɶɡɭɟɬɫɹ ɜ ɤɚɱɟɫɬɜɟ ɩɪɟɮɢɤɫɚ ɩɪɢ ɜɵɜɨɞɟ ɫɥɟɞɭɸɳɢɯ ɫɢɦɜɨɥɨɜ:
x ɋɚɦɨɝɨ ɫɢɦɜɨɥɚ FIELDS ESCAPED BY.
x ɋɢɦɜɨɥɚ FIELDS [OPTIONALLY] ENCLOSED BY.
x ɉɟɪɜɨɝɨ ɫɢɦɜɨɥɚ ɡɧɚɱɟɧɢɣ FIELDS TERMINATED BY ɢ LINES TERMINATED BY.
x ASCII 0 (ɤɨɬɨɪɵɣ ɩɢɲɟɬɫɹ ɜɫɥɟɞ ɡɚ ɫɢɦɜɨɥɨɦ ɨɬɦɟɧɵ ɤɚɤ ASCII ‘0’, ɚ ɧɟ ɧɭɥɟɜɨɣ ɛɚɣɬ).
ȿɫɥɢ FIELDS ESCAPED BY ɩɭɫɬ, ɧɢɤɚɤɢɟ ɫɢɦɜɨɥɵ ɧɟ ɩɪɟɞɜɚɪɹɸɬɫɹ ɫɢɦɜɨɥɚɦɢ ɨɬɦɟɧɵ, ɢ NULL ɜɵɜɨɞɢɬɫɹ ɤɚɤ NULL, ɚ ɧɟ \N. ȼɟɪɨɹɬɧɨ, ɷɬɨ ɧɟ ɨɱɟɧɶ ɯɨɪɨɲɚɹ ɢɞɟɹ –
ɭɤɚɡɵɜɚɬɶ ɩɭɫɬɨɣ ɫɢɦɜɨɥ ɨɬɦɟɧɵ, ɨɫɨɛɟɧɧɨ, ɟɫɥɢ ɡɧɚɱɟɧɢɹ ɩɨɥɟɣ ɜ ɜɚɲɢɯ ɞɚɧɧɵɯ
ɫɨɞɟɪɠɚɬ ɥɸɛɵɟ ɢɡ ɩɟɪɟɱɢɫɥɟɧɧɵɯ ɜɵɲɟ ɫɢɦɜɨɥɨɜ.
ɉɪɢɱɢɧɚ ɷɬɨɝɨ ɫɨɫɬɨɢɬ ɜ ɬɨɦ, ɱɬɨ ɜɵ ɨɛɹɡɚɧɵ ɩɪɟɞɜɚɪɹɬɶ ɫɢɦɜɨɥɚɦɢ ɨɬɦɟɧɵ
ɥɸɛɵɟ ɫɢɦɜɨɥɵ ɢɡ FIELDS TERMINATED BY, ENCLOSED BY, ESCAPED BY ɢ LINES
TERMINATED BY, ɱɬɨɛɵ ɝɚɪɚɧɬɢɪɨɜɚɧɧɨ ɢɦɟɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɩɪɨɱɟɫɬɶ ɮɚɣɥ ɜ ɛɭɞɭɳɟɦ. ASCII NUL ɩɪɟɞɜɚɪɹɟɬɫɹ ɫɢɦɜɨɥɨɦ ɨɬɦɟɧɵ ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɭɩɪɨɫɬɢɬɶ ɩɪɨɫɦɨɬɪ ɮɚɣɥɚ ɧɟɤɨɬɨɪɵɦɢ ɩɪɨɝɪɚɦɦɚɦɢ ɩɨɫɬɪɚɧɢɱɧɨɝɨ ɜɵɜɨɞɚ.
Ɋɟɡɭɥɶɬɢɪɭɸɳɢɣ ɮɚɣɥ ɧɟ ɞɨɥɠɟɧ ɫɨɨɬɜɟɬɫɬɜɨɜɚɬɶ SQL-ɫɢɧɬɚɤɫɢɫɭ, ɩɨɷɬɨɦɭ ɧɢɱɟɝɨ ɞɪɭɝɨɝɨ ɛɨɥɟɟ ɧɟ ɞɨɥɠɧɨ ɩɪɟɞɜɚɪɹɬɶɫɹ ɫɢɦɜɨɥɚɦɢ ɨɬɦɟɧɵ.
ɇɢɠɟ ɩɪɢɜɟɞɟɧ ɩɪɢɦɟɪ, ɤɨɬɨɪɵɣ ɝɟɧɟɪɢɪɭɟɬ ɮɚɣɥ ɜ ɮɨɪɦɚɬɟ ɫ ɡɚɩɹɬɨɣ ɜ ɤɚɱɟɫɬɜɟ
ɪɚɡɞɟɥɢɬɟɥɹ ɩɨɥɟɣ, ɤɨɬɨɪɵɣ ɢɫɩɨɥɶɡɭɟɬɫɹ ɦɧɨɝɢɦɢ ɩɪɨɝɪɚɦɦɚɦɢ:
SELECT a,b,a+b INTO OUTFILE '/tmp/result.text'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;
ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ INTO DUMPFILE ɜɦɟɫɬɨ INTO OUTFILE, MySQL ɩɢɲɟɬ ɬɨɥɶɤɨ
ɨɞɧɭ ɫɬɪɨɤɭ ɜ ɮɚɣɥ, ɛɟɡ ɤɚɤɢɯ-ɥɢɛɨ ɨɝɪɚɧɢɱɢɬɟɥɟɣ ɫɬɪɨɤ ɢɥɢ ɫɬɨɥɛɰɨɜ, ɛɟɡ ɤɚɤɨɣ
ɛɵ ɬɨ ɧɢ ɛɵɥɨ ɨɛɪɚɛɨɬɤɢ ɫ ɩɨɦɨɳɶɸ ɫɢɦɜɨɥɨɜ ɨɬɦɟɧɵ. ɗɬɨ ɭɞɨɛɧɨ, ɟɫɥɢ ɧɭɠɧɨ
ɩɨɦɟɫɬɢɬɶ ɜ ɮɚɣɥ ɡɧɚɱɟɧɢɹ ɬɢɩɚ BLOB.
„ȿɫɥɢ
ɨɬɦɟɬɢɬɶ, ɱɬɨ ɥɸɛɨɣ ɮɚɣɥ, ɫɨɡɞɚɧɧɵɣ INTO DUMPFILE ɢɥɢ INTO OUTFILE,
ɞɨɫɬɭɩɟɧ ɩɨ ɡɚɩɢɫɢ ɜɫɟɦ ɩɨɥɶɡɨɜɚɬɟɥɹɦ ɯɨɫɬɚ ɫɟɪɜɟɪɚ. ɉɪɢɱɢɧɚ ɷɬɨɝɨ ɜ ɬɨɦ, ɱɬɨ
ɫɟɪɜɟɪ MySQL ɧɟ ɦɨɠɟɬ ɫɨɡɞɚɬɶ ɮɚɣɥ, ɩɪɢɧɚɞɥɟɠɚɳɢɣ ɤɨɦɭ-ɧɢɛɭɞɶ ɞɪɭɝɨɦɭ, ɚ
ɧɟ ɩɨɥɶɡɨɜɚɬɟɥɸ, ɨɬ ɢɦɟɧɢ ɤɨɬɨɪɨɝɨ ɨɧ ɡɚɩɭɳɟɧ (ɧɢɤɨɝɞɚ ɧɟ ɡɚɩɭɫɤɚɣɬɟ mysqld ɨɬ
ɢɦɟɧɢ root). ɉɨɷɬɨɦɭ ɮɚɣɥ ɞɨɥɠɟɧ ɛɵɬɶ ɞɨɫɬɭɩɧɵɦ ɜɫɟɦ ɩɨ ɡɚɩɢɫɢ, ɱɬɨɛɵ ɜɵ
ɦɨɝɥɢ ɦɚɧɢɩɭɥɢɪɨɜɚɬɶ ɟɝɨ ɫɨɞɟɪɠɢɦɵɦ.
„ɋɥɟɞɭɟɬ
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
237
„Ʉɨɧɫɬɪɭɤɰɢɹ PROCEDURE
ɨɩɪɟɞɟɥɹɟɬ ɩɪɨɰɟɞɭɪɭ, ɤɨɬɨɪɚɹ ɞɨɥɠɧɚ ɨɛɪɚɛɚɬɵɜɚɬɶ
ɞɚɧɧɵɟ ɜ ɪɟɡɭɥɶɬɢɪɭɸɳɟɦ ɧɚɛɨɪɟ.
„ȿɫɥɢ ɜɵ ɩɪɢɦɟɧɹɟɬɟ FOR UPDATE ɫ ɦɟɯɚɧɢɡɦɨɦ ɯɪɚɧɟɧɢɹ, ɤɨɬɨɪɵɣ ɢɫɩɨɥɶɡɭɟɬ
ɛɥɨɤɢɪɨɜɤɢ ɫɬɪɚɧɢɰ ɢɥɢ ɫɬɪɨɤ, ɬɨ ɫɬɪɨɤɢ, ɩɪɨɜɟɪɹɟɦɵɟ ɡɚɩɪɨɫɨɦ, ɛɥɨɤɢɪɭɸɬɫɹ ɩɨ
ɡɚɩɢɫɢ ɞɨ ɡɚɜɟɪɲɟɧɢɹ ɬɟɤɭɳɟɣ ɬɪɚɧɡɚɤɰɢɢ.
ɉɨɫɥɟ ɤɥɸɱɟɜɨɝɨ ɫɥɨɜɚ SELECT ɦɨɠɧɨ ɭɤɚɡɚɬɶ ɦɧɨɠɟɫɬɜɨ ɨɩɰɢɣ, ɜɥɢɹɸɳɢɯ ɧɚ ɜɵɩɨɥɧɟɧɢɟ ɨɩɟɪɚɬɨɪɚ.
Ɉɩɰɢɢ ALL, DISTINCT ɢ DISTINCTROW ɨɩɪɟɞɟɥɹɸɬ, ɞɨɥɠɧɵ ɥɢ ɜɨɡɜɪɚɳɚɬɶɫɹ ɞɭɛɥɢɪɨɜɚɧɧɵɟ ɫɬɪɨɤɢ. ȿɫɥɢ ɧɢ ɨɞɧɚ ɢɡ ɷɬɢɯ ɨɩɰɢɣ ɧɟ ɭɤɚɡɚɧɚ, ɩɨ ɭɦɨɥɱɚɧɢɸ ɩɪɢɧɢɦɚɟɬɫɹ ALL
(ɜɨɡɜɪɚɳɚɸɬɫɹ ɜɫɟ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɫɬɪɨɤɢ). DISTINCT ɢ DISTINCTROW – ɫɢɧɨɧɢɦɵ; ɨɧɢ
ɭɤɚɡɵɜɚɸɬ, ɱɬɨ ɞɭɛɥɢɪɨɜɚɧɧɵɟ ɫɬɪɨɤɢ ɜ ɪɟɡɭɥɶɬɢɪɭɸɳɟɦ ɧɚɛɨɪɟ ɢɫɤɥɸɱɚɸɬɫɹ.
HIGH_PRIORITY, STRAIGHT_JOIN ɢ ɨɩɰɢɢ, ɧɚɱɢɧɚɸɳɢɟɫɹ ɫ SQL_, ɹɜɥɹɸɬɫɹ ɪɚɫɲɢɪɟɧɢɹɦɢ MySQL ɫɬɚɧɞɚɪɬɚ SQL.
„HIGH_PRIORITY ɧɚɡɧɚɱɚɟɬ ɨɩɟɪɚɬɨɪɭ SELECT ɛɨɥɟɟ ɜɵɫɨɤɢɣ ɩɪɢɨɪɢɬɟɬ, ɱɟɦ ɨɩɟɪɚɬɨɪɚɦ ɨɛɧɨɜɥɟɧɢɹ ɬɚɛɥɢɰɵ. ȼɵ ɞɨɥɠɧɵ ɢɫɩɨɥɶɡɨɜɚɬɶ ɷɬɨ ɬɨɥɶɤɨ ɞɥɹ ɡɚɩɪɨɫɨɜ,
ɤɨɬɨɪɵɟ ɜɵɩɨɥɧɹɸɬɫɹ ɨɞɧɨɤɪɚɬɧɨ ɢ ɨɱɟɧɶ ɛɵɫɬɪɨ. Ɂɚɩɪɨɫ SELECT HIGH_PRIORITY,
ɨɬɩɪɚɜɥɟɧɧɵɣ, ɤɨɝɞɚ ɬɚɛɥɢɰɚ ɡɚɛɥɨɤɢɪɨɜɚɧɚ ɩɨ ɱɬɟɧɢɸ, ɛɭɞɟɬ ɜɵɩɨɥɧɹɬɶɫɹ, ɞɚɠɟ
ɟɫɥɢ ɟɫɬɶ ɨɠɢɞɚɸɳɢɣ ɨɫɜɨɛɨɠɞɟɧɢɹ ɬɚɛɥɢɰɵ ɨɩɟɪɚɬɨɪ ɨɛɧɨɜɥɟɧɢɹ ɞɚɧɧɵɯ.
HIGH_PRIORITY ɧɟ ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɫ ɨɩɟɪɚɬɨɪɨɦ SELECT, ɹɜɥɹɸɳɢɦɫɹ ɱɚɫɬɶɸ UNION.
„STRAIGHT_JOIN
ɡɚɫɬɚɜɥɹɟɬ ɨɩɬɢɦɢɡɚɬɨɪ ɨɛɴɟɞɢɧɹɬɶ ɬɚɛɥɢɰɵ ɜ ɬɨɦ ɩɨɪɹɞɤɟ, ɜ ɤɨɬɨɪɨɦ ɨɧɢ ɩɟɪɟɱɢɫɥɟɧɵ ɜ ɤɨɧɫɬɪɭɤɰɢɢ FROM. ɗɬɨ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɞɥɹ ɭɫɤɨɪɟɧɢɹ ɡɚɩɪɨɫɨɜ, ɤɨɝɞɚ ɨɩɬɢɦɢɡɚɬɨɪ ɨɛɴɟɞɢɧɹɟɬ ɬɚɛɥɢɰɵ ɧɟ ɜ ɨɩɬɢɦɚɥɶɧɨɦ ɩɨɪɹɞɤɟ.
STRAIGHT_JOIN ɬɚɤɠɟ ɦɨɠɟɬ ɩɪɢɦɟɧɹɬɶɫɹ ɜ ɫɩɢɫɤɟ ɬɚɛɥɢɱɧɵɟ_ɫɫɵɥɤɢ. ɋɦ. ɪɚɡɞɟɥ
6.1.7.1.
„SQL_BIG_RESULT ɦɨɠɟɬ ɩɪɢɦɟɧɹɬɶɫɹ ɫ GROUP BY ɢɥɢ DISTINCT, ɱɬɨɛɵ ɫɨɨɛɳɢɬɶ ɨɩɬɢɦɢɡɚɬɨɪɭ, ɱɬɨ ɪɟɡɭɥɶɬɢɪɭɸɳɢɣ ɧɚɛɨɪ ɛɭɞɟɬ ɢɦɟɬɶ ɦɧɨɝɨ ɫɬɪɨɤ. ȼ ɷɬɨɦ ɫɥɭɱɚɟ
MySQL ɛɭɞɟɬ ɧɟɩɨɫɪɟɞɫɬɜɟɧɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɩɪɢ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɜɪɟɦɟɧɧɵɟ ɬɚɛɥɢɰɵ ɧɚ ɞɢɫɤɟ. Ⱥ ɬɚɤɠɟ ɜ ɷɬɨɦ ɫɥɭɱɚɟ MySQL ɩɪɟɞɩɨɱɬɟɬ ɫɨɪɬɢɪɨɜɤɭ ɩɨ ɤɥɸɱɭ
ɷɥɟɦɟɧɬɨɜ GROUP BY ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɜɪɟɦɟɧɧɵɯ ɬɚɛɥɢɰ.
„SQL_BUFFER_RESULT
ɩɪɢɧɭɞɢɬɟɥɶɧɨ ɩɨɦɟɳɚɟɬ ɪɟɡɭɥɶɬɚɬ ɜɨ ɜɪɟɦɟɧɧɵɣ ɮɚɣɥ. ɗɬɨ
ɩɨɦɨɝɚɟɬ MySQL ɩɨɪɚɧɶɲɟ ɨɫɜɨɛɨɞɢɬɶ ɬɚɛɥɢɱɧɵɟ ɛɥɨɤɢɪɨɜɤɢ ɢ ɨɤɚɡɵɜɚɟɬɫɹ ɩɨɥɟɡɧɵɦ ɜ ɫɥɭɱɚɹɯ, ɤɨɝɞɚ ɧɚ ɨɬɩɪɚɜɤɭ ɪɟɡɭɥɶɬɢɪɭɸɳɟɝɨ ɧɚɛɨɪɚ ɤɥɢɟɧɬɭ ɬɪɚɬɢɬɫɹ
ɦɧɨɝɨ ɜɪɟɦɟɧɢ.
„SQL_SMALL_RESULT ɦɨɠɟɬ ɩɪɢɦɟɧɹɬɶɫɹ ɜɦɟɫɬɟ ɫ GROUP BY ɢɥɢ DISTINCT, ɱɬɨɛɵ ɫɨɨɛɳɢɬɶ ɨɩɬɢɦɢɡɚɬɨɪɭ, ɱɬɨ ɪɟɡɭɥɶɬɢɪɭɸɳɢɣ ɧɚɛɨɪ ɛɭɞɟɬ ɦɚɥɟɧɶɤɢɦ. ȼ ɷɬɨɦ ɫɥɭɱɚɟ MySQL ɢɫɩɨɥɶɡɭɟɬ ɛɵɫɬɪɵɟ ɜɪɟɦɟɧɧɵɟ ɬɚɛɥɢɰɵ, ɱɬɨɛɵ ɯɪɚɧɢɬɶ ɪɟɡɭɥɶɬɢɪɭɸɳɭɸ ɬɚɛɥɢɰɭ ɜɦɟɫɬɨ ɩɪɢɦɟɧɟɧɢɹ ɫɨɪɬɢɪɨɜɤɢ. ȼ MySQL 3.23 ɢ ɜɵɲɟ ɷɬɨ
ɨɛɵɱɧɨ ɧɟ ɬɪɟɛɭɟɬɫɹ.
„SQL_CALC_FOUND_ROWS (ɞɨɫɬɭɩɧɨ ɜ MySQL 4.0.0 ɢ ɜɵɲɟ) ɫɨɨɛɳɚɟɬ ɫɟɪɜɟɪɭ
MySQL, ɱɬɨ ɧɭɠɧɨ ɩɨɫɱɢɬɚɬɶ ɤɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ ɜ ɪɟɡɭɥɶɬɢɪɭɸɳɟɦ ɧɚɛɨɪɟ, ɧɟɡɚɜɢɫɢɦɨ ɨɬ ɤɨɧɫɬɪɭɤɰɢɢ LIMIT. Ʉɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ ɡɚɬɟɦ ɦɨɠɟɬ ɛɵɬɶ ɢɡɜɥɟɱɟɧɨ ɫ
ɩɨɦɨɳɶɸ SELECT FOUND_ROWS(). ɋɦ. ɪɚɡɞɟɥ 5.8.3.
238
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
Ⱦɨ MySQL 4.1.0 ɷɬɚ ɨɩɰɢɹ ɧɟ ɪɚɛɨɬɚɥɚ ɫ LIMIT 0, ɱɬɨ ɛɵɥɨ ɨɩɬɢɦɢɡɢɪɨɜɚɧɨ ɞɥɹ
ɧɟɦɟɞɥɟɧɧɨɝɨ ɜɨɡɜɪɚɬɚ (ɫɨ ɡɧɚɱɟɧɢɟɦ ɫɱɟɬɱɢɤɚ ɫɬɪɨɤ, ɪɚɜɧɵɦ 0).
ɡɚɫɬɚɜɥɹɟɬ MySQL ɫɨɯɪɚɧɹɬɶ ɪɟɡɭɥɶɬɚɬ ɜ ɤɷɲɟ ɡɚɩɪɨɫɨɜ, ɟɫɥɢ ɢɫɩɨɥɶɡɭɟɬɫɹ ɡɧɚɱɟɧɢɟ query_cache_type, ɪɚɜɧɨɟ 2, ɢɥɢ DEMAND. Ⱦɥɹ ɡɚɩɪɨɫɨɜ, ɢɫɩɨɥɶɡɭɸɳɢɯ UNION ɢɥɢ ɩɨɞɡɚɩɪɨɫɵ, ɷɬɚ ɨɩɰɢɹ ɜɥɢɹɟɬ ɧɚ ɜɫɟ ɨɩɟɪɚɬɨɪɵ SELECT ɜ ɡɚɩɪɨɫɟ.
„SQL_NO_CACHE ɫɨɨɛɳɚɟɬ MySQL, ɱɬɨ ɪɟɡɭɥɶɬɚɬɵ ɡɚɩɪɨɫɨɜ ɧɟ ɧɭɠɧɨ ɫɨɯɪɚɧɹɬɶ ɜ
ɤɷɲɟ. Ⱦɥɹ ɡɚɩɪɨɫɨɜ, ɤɨɬɨɪɵɟ ɢɫɩɨɥɶɡɭɸɬ UNION ɢɥɢ ɩɨɞɡɚɩɪɨɫɵ, ɷɬɚ ɨɩɰɢɹ ɜɥɢɹɟɬ
ɧɚ ɜɫɟ ɨɩɟɪɚɬɨɪɵ SELECT ɜ ɡɚɩɪɨɫɟ.
„SQL_CACHE
6.1.7.1. Ñèíòàêñèñ JOIN
MySQL ɩɨɞɞɟɪɠɢɜɚɟɬ ɫɥɟɞɭɸɳɢɟ ɜɚɪɢɚɧɬɵ ɫɢɧɬɚɤɫɢɫɚ JOIN ɜ ɱɚɫɬɢ ɬɚɛɥɢɱɧɵɟ_ɫɫɵɥɤɢ ɨɩɟɪɚɬɨɪɨɜ SELECT, ɚ ɬɚɤɠɟ ɦɧɨɝɨɬɚɛɥɢɱɧɵɯ ɨɩɟɪɚɬɨɪɨɜ DELETE ɢ UPDATE:
ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ, ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ
ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ [INNER | CROSS] ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ [ɭɫɥɨɜɢɟ_ɫɨɟɞɢɧɟɧɢɹ]
ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ STRAIGHT_JOIN ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ
ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ LEFT [OUTER] JOIN ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ [ɭɫɥɨɜɢɟ_ɫɨɟɞɢɧɟɧɢɹ]
ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ NATURAL [LEFT [OUTER]] JOIN ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ
{ OJ ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ LEFT OUTER JOIN ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ ON ɭɫɥɨɜɧɨɟ_ɜɵɪɚɠɟɧɢɟ }
ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ RIGHT [OUTER] JOIN ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ [ɭɫɥɨɜɢɟ_ɫɨɟɞɢɧɟɧɢɹ]
ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ NATURAL [RIGHT [OUTER]] JOIN ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ
ɬɚɛɥɢɱɧɚɹ_ɫɫɵɥɤɚ ɨɩɪɟɞɟɥɟɧɨ ɤɚɤ:
ɢɦɹ_ɬɚɛɥɢɰɵ [[AS] ɩɫɟɜɞɨɧɢɦ]
[[USE INDEX (ɫɩɢɫɨɤ_ɤɥɸɱɟɣ)]
| [IGNORE INDEX (ɫɩɢɫɨɤ_ɤɥɸɱɟɣ)]
| [FORCE INDEX (ɫɩɢɫɨɤ_ɤɥɸɱɟɣ)]]
ɭɫɥɨɜɢɟ_ɫɨɟɞɢɧɟɧɢɹ ɨɩɪɟɞɟɥɟɧɨ ɤɚɤ:
ON ɭɫɥɨɜɧɨɟ_ɜɵɪɚɠɟɧɢɟ | USING (ɫɩɢɫɨɤ_ɫɬɨɥɛɰɨɜ)
Ɉɛɵɱɧɨ ɜɵ ɧɟ ɞɨɥɠɧɵ ɢɦɟɬɶ ɜ ɱɚɫɬɢ ON ɧɢɤɚɤɢɯ ɭɫɥɨɜɢɣ, ɨɝɪɚɧɢɱɢɜɚɸɳɢɯ ɫɬɪɨɤɢ
ɞɥɹ ɪɟɡɭɥɶɬɢɪɭɸɳɟɝɨ ɧɚɛɨɪɚ. ɗɬɢ ɭɫɥɨɜɢɹ ɭɤɚɡɵɜɚɸɬɫɹ ɜ ɤɨɧɫɬɪɭɤɰɢɢ WHERE.
Ɉɞɧɚɤɨ ɫɭɳɟɫɬɜɭɸɬ ɢɫɤɥɸɱɟɧɢɹ ɢɯ ɷɬɨɝɨ ɩɪɚɜɢɥɚ.
Ɉɬɦɟɬɢɦ, ɱɬɨ ɫɢɧɬɚɤɫɢɫ INNER JOIN ɩɪɟɞɭɫɦɚɬɪɢɜɚɟɬ ɭɫɥɨɜɢɟ_ɫɨɟɞɢɧɟɧɢɹ ɬɨɥɶɤɨ ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 3.23.17 ɢ ɜɵɲɟ. Ɍɨ ɠɟ ɫɚɦɨɟ ɜɟɪɧɨ ɢ ɞɥɹ JOIN ɢ CROSS JOIN, ɧɨ
ɬɨɥɶɤɨ ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.0.11.
ɋɢɧɬɚɤɫɢɫ {OJ...LEFT OUTER JOIN...}, ɩɪɟɞɫɬɚɜɥɟɧɧɵɣ ɜɵɲɟ, ɜɜɟɞɟɧ ɬɨɥɶɤɨ ɞɥɹ
ɫɨɜɦɟɫɬɢɦɨɫɬɢ ɫ ODBC.
„ɋɫɵɥɤɢ ɧɚ ɬɚɛɥɢɰɵ ɦɨɝɭɬ ɛɵɬɶ ɡɚɦɟɧɟɧɵ ɩɫɟɜɞɨɧɢɦɚɦɢ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ
ɢɦɹ_ɬɚɛɥɢɰɵ AS ɢɦɹ_ɩɫɟɜɞɨɧɢɦɚ ɢɥɢ ɢɦɹ_ɬɚɛɥɢɰɵ ɢɦɹ_ɩɫɟɜɞɨɧɢɦɚ:
mysql> SELECT t1.name, t2.salary FROM employee AS t1, info AS t2
-> WHERE t1.name = t2.name;
mysql> SELECT t1.name, t2.salary FROM employee t1, info t2
-> WHERE t1.name = t2.name;
– ɷɬɨ ɥɸɛɵɟ ɭɫɥɨɜɧɵɟ ɜɵɪɚɠɟɧɢɹ ɜ ɬɨɣ ɠɟ ɮɨɪɦɟ, ɱɬɨ ɩɪɢɦɟɧɹɸɬɫɹ ɜ
ɤɨɧɫɬɪɭɤɰɢɢ WHERE.
„ɍɫɥɨɜɢɹ ON
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
239
ɧɟ ɧɚɣɞɟɧɨ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɯ ɡɚɩɢɫɟɣ ɜ ɩɪɚɜɨɣ ɬɚɛɥɢɰɟ ɱɚɫɬɢ ON ɢɥɢ USING
ɤɨɧɫɬɪɭɤɰɢɢ LEFT JOIN, ɞɥɹ ɩɪɚɜɨɣ ɬɚɛɥɢɰɵ ɢɫɩɨɥɶɡɭɟɬɫɹ ɫɬɪɨɤɚ ɫɨ ɜɫɟɦɢ ɫɬɨɥɛɰɚɦɢ, ɭɫɬɚɧɨɜɥɟɧɧɵɦɢ ɜ NULL. ɗɬɢɦ ɮɚɤɬɨɦ ɦɨɠɧɨ ɜɨɫɩɨɥɶɡɨɜɚɬɶɫɹ ɞɥɹ ɩɨɢɫɤɚ
ɡɚɩɢɫɟɣ ɜ ɬɚɛɥɢɰɟ, ɞɥɹ ɤɨɬɨɪɨɣ ɧɟ ɫɭɳɟɫɬɜɭɟɬ ɞɨɩɨɥɧɟɧɢɣ ɜ ɞɪɭɝɨɣ ɬɚɛɥɢɰɟ:
„ȿɫɥɢ
mysql> SELECT table1.* FROM table1
-> LEFT JOIN table2 ON table1.id=table2.id
-> WHERE table2.id IS NULL;
ɗɬɨɬ ɩɪɢɦɟɪ ɧɚɯɨɞɢɬ ɜɫɟ ɫɬɪɨɤɢ ɬɚɛɥɢɰɵ table1 ɫɨ ɡɧɚɱɟɧɢɹɦɢ id, ɤɨɬɨɪɵɯ ɧɟɬ ɜ
ɬɚɛɥɢɰɟ table2 (ɬɨ ɟɫɬɶ, ɜɫɟɯ ɫɬɪɨɤ ɬɚɛɥɢɰɵ table1, ɞɥɹ ɤɨɬɨɪɵɯ ɧɟɬ ɫɜɹɡɚɧɧɵɯ
ɫɬɪɨɤ ɜ table2). ɉɪɟɞɩɨɥɚɝɚɟɬɫɹ, ɱɬɨ table2.id ɨɛɴɹɜɥɟɧ ɤɚɤ NOT NULL.
„Ʉɨɧɫɬɪɭɤɰɢɹ USING(ɫɩɢɫɨɤ_ɫɬɨɥɛɰɨɜ)
ɩɟɪɟɱɢɫɥɹɟɬ ɫɩɢɫɨɤ ɫɬɨɥɛɰɨɜ, ɤɨɬɨɪɵɟ
ɞɨɥɠɧɵ ɩɪɢɫɭɬɫɬɜɨɜɚɬɶ ɜ ɨɛɟɢɯ ɬɚɛɥɢɰɚɯ. ɋɥɟɞɭɸɳɢɟ ɞɜɟ ɤɨɧɫɬɪɭɤɰɢɢ ɫɟɦɚɧɬɢɱɟɫɤɢ ɢɞɟɧɬɢɱɧɵ:
a LEFT JOIN b USING (c1,c2,c3)
a LEFT JOIN b ON a.c1=b.c1 AND a.c2=b.c2 AND a.c3=b.c3
„NATURAL [LEFT] JOIN
ɞɥɹ ɞɜɭɯ ɬɚɛɥɢɰ ɨɩɪɟɞɟɥɟɧɨ ɤɚɤ ɫɟɦɚɧɬɢɱɟɫɤɢɣ ɷɤɜɢɜɚɥɟɧɬ
INNER JOIN ɢɥɢ LEFT JOIN ɫ ɤɨɧɫɬɪɭɤɰɢɟɣ USING, ɤɨɬɨɪɚɹ ɩɟɪɟɱɢɫɥɹɟɬ ɜɫɟ ɫɬɨɥɛ-
ɰɵ, ɩɪɢɫɭɬɫɬɜɭɸɳɢɟ ɜ ɨɛɟɢɯ ɬɚɛɥɢɰɚɯ.
ɢ , (ɡɚɩɹɬɚɹ) ɫɟɦɚɧɬɢɱɟɫɤɢ ɷɤɜɢɜɚɥɟɧɬɧɵ ɩɪɢ ɨɬɫɭɬɫɬɜɢɢ ɭɫɥɨɜɢɹ ɫɨɟɞɢɧɟɧɢɹ: ɨɛɚ ɜɵɩɨɥɧɹɸɬ ɞɟɤɚɪɬɨɜɨ ɩɪɨɢɡɜɟɞɟɧɢɟ ɭɤɚɡɚɧɧɵɯ ɬɚɛɥɢɰ (ɬɨ ɟɫɬɶ ɤɚɠɞɚɹ ɫɬɪɨɤɚ ɩɟɪɜɨɣ ɬɚɛɥɢɰɵ ɫɨɟɞɢɧɹɟɬɫɹ ɫɨ ɜɫɟɦɢ ɫɬɪɨɤɚɦɢ ɜɬɨɪɨɣ ɬɚɛɥɢɰɵ).
„RIGHT JOIN ɪɚɛɨɬɚɟɬ ɚɧɚɥɨɝɢɱɧɨ LEFT JOIN. ɑɬɨɛɵ ɫɨɯɪɚɧɹɬɶ ɤɨɞ ɩɟɪɟɧɨɫɢɦɵɦ
ɦɟɠɞɭ ɫɢɫɬɟɦɚɦɢ ɭɩɪɚɜɥɟɧɢɹ ɛɚɡɚɦɢ ɞɚɧɧɵɯ, ɪɟɤɨɦɟɧɞɭɟɬɫɹ ɢɫɩɨɥɶɡɨɜɚɬɶ LEFT
JOIN ɜɦɟɫɬɨ RIGHT JOIN.
„INNER JOIN
„STRAIGHT_JOIN
ɢɞɟɧɬɢɱɧɨ JOIN ɡɚ ɢɫɤɥɸɱɟɧɢɟɦ ɬɨɝɨ, ɱɬɨ ɥɟɜɚɹ ɬɚɛɥɢɰɚ ɜɫɟɝɞɚ ɱɢɬɚɟɬɫɹ ɪɚɧɶɲɟ, ɱɟɦ ɩɪɚɜɚɹ. ɗɬɨ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɜ ɬɟɯ (ɧɟɦɧɨɝɢɯ) ɫɥɭɱɚɹɯ, ɤɨɝɞɚ ɨɩɬɢɦɢɡɚɬɨɪ ɪɚɫɩɨɥɚɝɚɟɬ ɬɚɛɥɢɰɵ ɜ ɧɟɩɪɚɜɢɥɶɧɨɦ ɩɨɪɹɞɤɟ.
ɇɚɱɢɧɚɹ ɫ MySQL 3.23.12, ɜɵ ɦɨɠɟɬɟ ɭɤɚɡɵɜɚɬɶ ɩɨɞɫɤɚɡɤɢ (hints) ɨ ɬɨɦ, ɤɚɤ MySQL
ɞɨɥɠɟɧ ɢɫɩɨɥɶɡɨɜɚɬɶ ɢɧɞɟɤɫɵ ɩɪɢ ɢɡɜɥɟɱɟɧɢɢ ɞɚɧɧɵɯ ɢɡ ɬɚɛɥɢɰɵ. ɍɤɚɡɚɜ USE INDEX
(ɫɩɢɫɨɤ_ɤɥɸɱɟɣ), ɜɵ ɦɨɠɟɬɟ ɩɪɢɧɭɞɢɬɶ MySQL ɢɫɩɨɥɶɡɨɜɚɬɶ ɬɨɥɶɤɨ ɨɞɢɧ ɢɡ ɜɫɟɯ ɢɧɞɟɤɫɨɜ, ɱɬɨɛɵ ɢɫɤɚɬɶ ɫɬɪɨɤɢ ɜ ɬɚɛɥɢɰɟ. Ⱥɥɶɬɟɪɧɚɬɢɜɧɵɣ ɫɢɧɬɚɤɫɢɫ IGNORE INDEX (ɫɩɢɫɨɤ_ɤɥɸɱɟɣ) ɦɨɠɟɬ ɩɪɢɦɟɧɹɬɶɫɹ, ɱɬɨɛɵ ɡɚɩɪɟɬɢɬɶ MySQL ɢɫɩɨɥɶɡɨɜɚɬɶ ɤɚɤɨɣ-ɬɨ ɨɬɞɟɥɶɧɵɣ ɢɧɞɟɤɫ.
ɗɬɢ ɩɨɞɫɤɚɡɤɢ ɭɞɨɛɧɵ, ɟɫɥɢ EXPLAIN ɩɨɤɚɡɵɜɚɟɬ, ɱɬɨ MySQL ɢɫɩɨɥɶɡɭɟɬ ɧɟɩɪɚɜɢɥɶɧɵɟ ɢɧɞɟɤɫɵ ɢɡ ɫɩɢɫɤɚ ɜɨɡɦɨɠɧɵɯ.
ɇɚɱɢɧɚɹ ɫ MySQL 4.0.9, ɜɵ ɬɚɤɠɟ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ FORCE INDEX. ɗɬɨ ɪɚɛɨɬɚɟɬ
ɩɨɞɨɛɧɨ USE INDEX (ɫɩɢɫɨɤ_ɤɥɸɱɟɣ), ɧɨ ɫ ɬɟɦ ɨɬɥɢɱɢɟɦ, ɱɬɨ ɫɤɚɧɢɪɨɜɚɧɢɟ ɬɚɛɥɢɰ ɪɚɫɰɟɧɢɜɚɟɬɫɹ, ɤɚɤ ɨɱɟɧɶ ɞɨɪɨɝɚɹ ɨɩɟɪɚɰɢɹ. Ⱦɪɭɝɢɦɢ ɫɥɨɜɚɦɢ, ɫɤɚɧɢɪɨɜɚɧɢɟ ɬɚɛɥɢɰɵ ɞɨɩɭɫɤɚɟɬɫɹ ɬɨɥɶɤɨ ɜ ɬɨɦ ɫɥɭɱɚɟ, ɟɫɥɢ ɧɟɬ ɫɩɨɫɨɛɚ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɢɧɞɟɤɫɚ ɞɥɹ ɩɨɢɫɤɚ
ɫɬɪɨɤ ɜ ɬɚɛɥɢɰɟ.
USE KEY, IGNORE KEY ɢ FORCE KEY – ɫɢɧɨɧɢɦɵ ɞɥɹ USE INDEX, IGNORE INDEX ɢ FORCE
INDEX.
240
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
oË ÑËÕÏÛµÂ!
USE INDEX, IGNORE INDEX ɢ FORCE INDEX ɜɥɢɹɸɬ ɬɨɥɶɤɨ ɧɚ ɬɨ, ɤɚɤɢɟ ɢɧɞɟɤɫɵ ɛɭɞɭɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ, ɤɨɝɞɚ MySQL ɩɪɢɧɢɦɚɟɬ ɪɟɲɟɧɢɟ ɨ ɬɨɦ, ɤɚɤ ɢɫɤɚɬɶ ɫɬɪɨɤɢ ɜ ɬɚɛɥɢɰɟ ɢ ɤɚɤ ɜɵɩɨɥɧɹɬɶ
ɫɨɟɞɢɧɟɧɢɟ. Ɉɧɢ ɧɟ ɜɥɢɹɸɬ ɧɚ ɬɨ, ɤɚɤ ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɢɧɞɟɤɫ ɩɪɢ ɜɵɱɢɫɥɟɧɢɢ ORDER BY
ɢɥɢ GROUP BY.
ɇɢɠɟ ɩɪɟɞɫɬɚɜɥɟɧɵ ɩɪɢɦɟɪɵ ɫɨɟɞɢɧɟɧɢɣ:
mysql>
mysql>
mysql>
mysql>
->
mysql>
->
mysql>
->
SELECT
SELECT
SELECT
SELECT
*
*
*
*
FROM table1,table2 WHERE table1.id=table2.id;
FROM table1 LEFT JOIN table2 ON table1.id=table2.id;
FROM table1 LEFT JOIN table2 USING (id);
FROM table1 LEFT JOIN table2 ON table1.id=table2.id
LEFT JOIN table3 ON table2.id=table3.id;
SELECT * FROM table1 USE INDEX (key1,key2)
WHERE key1=1 AND key2=2 AND key3=3;
SELECT * FROM table1 IGNORE INDEX (key3)
WHERE key1=1 AND key2=2 AND key3=3;
6.1.7.2. Ñèíòàêñèñ UNION
SELECT ...
UNION [ALL | DISTINCT]
SELECT ...
[UNION [ALL | DISTINCT]
SELECT ...]
UNION ɩɪɢɦɟɧɹɟɬɫɹ ɞɥɹ ɤɨɦɛɢɧɢɪɨɜɚɧɢɹ ɪɟɡɭɥɶɬɚɬɨɜ ɦɧɨɠɟɫɬɜɚ ɨɩɟɪɚɬɨɪɨɜ SELECT ɜ
ɨɞɢɧ ɪɟɡɭɥɶɬɢɪɭɸɳɢɣ ɧɚɛɨɪ. Ɉɩɟɪɚɬɨɪ UNION ɞɨɫɬɭɩɟɧ, ɧɚɱɢɧɚɹ ɫ MySQL 4.0.0.
ȼɵɛɪɚɧɧɵɟ ɫɬɨɥɛɰɵ, ɩɟɪɟɱɢɫɥɟɧɧɵɟ ɜ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɯ ɩɨɡɢɰɢɹɯ ɤɚɠɞɨɝɨ ɨɩɟɪɚɬɨɪɚ SELECT, ɞɨɥɠɧɵ ɢɦɟɬɶ ɨɞɢɧɚɤɨɜɵɣ ɬɢɩ (ɧɚɩɪɢɦɟɪ, ɩɟɪɜɵɣ ɫɬɨɥɛɟɰ, ɜɵɛɢɪɚɟɦɵɣ ɩɟɪɜɵɦ ɨɩɟɪɚɬɨɪɨɦ, ɞɨɥɠɟɧ ɢɦɟɬɶ ɬɨɬ ɠɟ ɬɢɩ, ɱɬɨ ɢ ɩɟɪɜɵɣ ɫɬɨɥɛɟɰ, ɜɵɛɪɚɧɧɵɣ ɞɪɭɝɢɦɢ
ɨɩɟɪɚɬɨɪɚɦɢ). ɂɦɟɧɚ ɫɬɨɥɛɰɨɜ ɩɟɪɜɨɝɨ ɨɩɟɪɚɬɨɪɚ SELECT ɢɫɩɨɥɶɡɭɸɬɫɹ ɤɚɤ ɢɦɟɧɚ
ɫɬɨɥɛɰɨɜ ɞɥɹ ɜɫɟɯ ɜɨɡɜɪɚɳɚɟɦɵɯ ɪɟɡɭɥɶɬɚɬɨɜ.
Ɉɩɟɪɚɬɨɪɵ SELECT – ɷɬɨ ɨɛɵɱɧɵɟ ɨɩɟɪɚɬɨɪɵ ɢɡɜɥɟɱɟɧɢɹ ɫɬɪɨɤ, ɧɨ ɫɨ ɫɥɟɞɭɸɳɢɦɢ
ɨɝɪɚɧɢɱɟɧɢɹɦɢ:
„Ɍɨɥɶɤɨ ɨɞɢɧ ɢɡ ɨɩɟɪɚɬɨɪɨɜ SELECT ɦɨɠɟɬ ɢɦɟɬɶ INTO OUTFILE.
ɧɟ ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɫ ɨɩɟɪɚɬɨɪɚɦɢ SELECT, ɤɨɬɨɪɵɟ ɜɯɨɞɹɬ ɜ
UNION. ȿɫɥɢ ɜɵ ɭɤɚɡɵɜɚɟɬɟ ɟɝɨ ɜ ɩɟɪɜɨɦ ɨɩɟɪɚɬɨɪɟ SELECT, ɷɬɨ ɧɟ ɞɚɫɬ ɧɢɤɚɤɨɝɨ
ɷɮɮɟɤɬɚ. ȿɫɥɢ ɭɤɚɡɚɬɶ ɟɝɨ ɜ ɩɨɫɥɟɞɭɸɳɢɯ ɨɩɟɪɚɬɨɪɚɯ SELECT, ɪɟɡɭɥɶɬɚɬɨɦ ɛɭɞɟɬ
„HIGH_PRIORITY
ɫɨɨɛɳɟɧɢɟ ɨ ɫɢɧɬɚɤɫɢɱɟɫɤɨɣ ɨɲɢɛɤɟ.
ȿɫɥɢ ɜɵ ɧɟ ɢɫɩɨɥɶɡɭɟɬɟ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ ALL ɜ UNION, ɜɫɟ ɜɨɡɜɪɚɳɟɧɧɵɟ ɫɬɪɨɤɢ ɛɭɞɭɬ
ɭɧɢɤɚɥɶɧɵɦɢ, ɤɚɤ ɟɫɥɢ ɛɵ ɛɵɥ ɭɤɚɡɚɧ DISTINCT ɞɥɹ ɨɛɳɟɝɨ ɪɟɡɭɥɶɬɢɪɭɸɳɟɝɨ ɧɚɛɨɪɚ.
ȿɫɥɢ ALL ɛɭɞɟɬ ɭɤɚɡɚɧ, ɜɵ ɩɨɥɭɱɢɬɟ ɜɫɟ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɫɬɪɨɤɢ ɨɬ ɜɫɟɯ ɜɯɨɞɹɳɢɯ ɜ
UNION ɨɩɟɪɚɬɨɪɨɜ SELECT.
Ʉɥɸɱɟɜɨɟ ɫɥɨɜɨ DISTINCT ɹɜɥɹɟɬɫɹ ɧɟɨɛɹɡɚɬɟɥɶɧɵɦ (ɨɧɨ ɜɜɟɞɟɧɨ ɜ ɜɟɪɫɢɢ MySQL
4.0.17). Ɉɧɨ ɧɟ ɞɟɥɚɟɬ ɧɢɱɟɝɨ, ɧɨ ɞɨɛɚɜɥɟɧɨ ɞɥɹ ɨɛɟɫɩɟɱɟɧɢɹ ɫɨɨɬɜɟɬɫɬɜɢɹ ɫɢɧɬɚɤɫɢɫɚ
ɫɬɚɧɞɚɪɬɭ SQL.
oË ÑËÕÏÛµÂ!
ɇɟɥɶɡɹ ɫɦɟɲɢɜɚɬɶ UNION ALL ɢ UNION DISTINCT ɜ ɨɞɧɨɦ ɡɚɩɪɨɫɟ. ȿɫɥɢ ɭɤɚɡɚɧɨ ALL ɞɥɹ ɨɞɧɨɝɨ UNION, ɨɧɨ ɩɪɢɦɟɧɹɟɬɫɹ ɤɨ ɜɫɟɦ.
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
241
ȿɫɥɢ ɜɵ ɯɨɬɢɬɟ ɩɪɢɦɟɧɢɬɶ ORDER BY ɞɥɹ ɫɨɪɬɢɪɨɜɤɢ ɪɟɡɭɥɶɬɚɬɚ UNION, ɧɟɨɛɯɨɞɢɦɨ
ɢɫɩɨɥɶɡɨɜɚɬɶ ɫɤɨɛɤɢ:
(SELECT a FROM ɢɦɹ_ɬɚɛɥɢɰɵ WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM ɢɦɹ_ɬɚɛɥɢɰɵ WHERE a=11 AND B=2 ORDER BY a LIMIT 10)
ORDER BY a;
Ɍɢɩɵ ɢ ɞɥɢɧɚ ɫɬɨɥɛɰɨɜ ɜ ɪɟɡɭɥɶɬɢɪɭɸɳɟɦ ɧɚɛɨɪɟ UNION ɩɪɢɧɢɦɚɸɬ ɜɨ ɜɧɢɦɚɧɢɟ
ɡɧɚɱɟɧɢɹ, ɢɡɜɥɟɱɟɧɧɵɟ ɜɫɟɦɢ ɨɩɟɪɚɬɨɪɚɦɢ SELECT. Ⱦɨ MySQL 4.1.1 ɫɭɳɟɫɬɜɨɜɚɥɨ ɨɝɪɚɧɢɱɟɧɢɟ, ɡɚɤɥɸɱɚɸɳɟɟɫɹ ɜ ɬɨɦ, ɱɬɨ ɡɧɚɱɟɧɢɹ ɩɟɪɜɨɝɨ SELECT ɢɫɩɨɥɶɡɨɜɚɥɢɫɶ ɞɥɹ ɨɩɪɟɞɟɥɟɧɢɹ ɬɢɩɨɜ ɢ ɞɥɢɧ ɪɟɡɭɥɶɬɢɪɭɸɳɢɯ ɫɬɨɥɛɰɨɜ. ɗɬɨ ɦɨɝɥɨ ɩɪɢɜɨɞɢɬɶ ɤ ɭɫɟɱɟɧɢɸ ɡɧɚɱɟɧɢɣ, ɟɫɥɢ, ɧɚɩɪɢɦɟɪ, ɩɟɪɜɵɣ SELECT ɢɡɜɥɟɤɚɥ ɡɧɚɱɟɧɢɹ, ɛɨɥɟɟ ɤɨɪɨɬɤɢɟ, ɱɟɦ ɜɬɨɪɨɣ:
mysql> SELECT REPEAT('a',1) UNION SELECT REPEAT('b',10);
+--------------------+
| REPEAT('a',1)
|
+--------------------+
| a
|
| b
|
+--------------------+
ɗɬɨ ɨɝɪɚɧɢɱɟɧɢɟ ɛɵɥɨ ɫɧɹɬɨ ɜ MySQL 4.1.1:
mysql> SELECT REPEAT('a',1) UNION SELECT REPEAT('b',10);
+--------------------+
| REPEAT('a',1)
|
+--------------------+
| a
|
| bbbbbbbbbb
|
+--------------------+
6.1.8. Ñèíòàêñèñ ïîäçàïðîñîâ
ɉɨɞɡɚɩɪɨɫ – ɷɬɨ ɨɩɟɪɚɬɨɪ SELECT ɜɧɭɬɪɢ ɞɪɭɝɨɝɨ ɨɩɟɪɚɬɨɪɚ.
ɇɚɱɢɧɚɹ ɫ MySQL 4.1, ɩɨɞɞɟɪɠɢɜɚɸɬɫɹ ɜɫɟ ɮɨɪɦɵ ɩɨɞɡɚɩɪɨɫɨɜ, ɤɨɬɨɪɵɯ ɬɪɟɛɭɟɬ
ɫɬɚɧɞɚɪɬ SQL, ɪɚɜɧɨ ɤɚɤ ɢ ɧɟɤɨɬɨɪɵɟ ɫɪɟɞɫɬɜɚ, ɫɩɟɰɢɮɢɱɧɵɟ ɞɥɹ MySQL.
ȼ ɪɚɧɧɢɯ ɜɟɪɫɢɹɯ MySQL ɩɪɢɯɨɞɢɥɨɫɶ ɢɫɤɚɬɶ ɢɡɛɟɝɚɬɶ ɩɨɞɡɚɩɪɨɫɨɜ ɜɨɨɛɳɟ ɥɢɛɨ ɢɫɤɚɬɶ ɨɛɯɨɞɧɵɟ ɩɭɬɢ, ɧɨ ɪɚɡɪɚɛɨɬɱɢɤɢ, ɤɨɬɨɪɵɟ ɧɚɱɢɧɚɸɬ ɩɢɫɚɬɶ ɧɨɜɵɣ ɤɨɞ ɫɟɣɱɚɫ, ɨɛɧɚɪɭɠɚɬ, ɱɬɨ ɩɨɞɡɚɩɪɨɫɵ – ɨɱɟɧɶ ɭɞɨɛɧɚɹ ɱɚɫɬɶ ɢɧɫɬɪɭɦɟɧɬɚɥɶɧɨɝɨ ɧɚɛɨɪɚ MySQL.
ȼ MySQL ɞɨ 4.1 ɛɨɥɶɲɢɧɫɬɜɨ ɩɨɞɡɚɩɪɨɫɨɜ ɦɨɝɭɬ ɛɵɬɶ ɭɫɩɟɲɧɨ ɪɟɚɥɢɡɨɜɚɧɨ ɜ ɜɢɞɟ
ɫɨɟɞɢɧɟɧɢɣ ɢɥɢ ɞɪɭɝɢɦɢ ɫɩɨɫɨɛɚɦɢ. ɋɦ. ɪɚɡɞɟɥ 6.1.8.11.
ȼɨɬ ɩɪɢɦɟɪ ɩɨɞɡɚɩɪɨɫɚ:
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
ȼ ɷɬɨɦ ɩɪɢɦɟɪɟ SELECT * FROM t1 ɹɜɥɹɟɬɫɹ ɜɧɟɲɧɢɦ ɡɚɩɪɨɫɨɦ (ɢɥɢ ɜɧɟɲɧɢɦ ɨɩɟɪɚɬɨɪɨɦ), ɚ (SELECT column1 FROM t2) – ɩɨɞɡɚɩɪɨɫɨɦ. Ƚɨɜɨɪɹɬ, ɱɬɨ ɩɨɞɡɚɩɪɨɫ ɜɥɨɠɟɧ ɜɨ
ɜɧɟɲɧɢɣ ɡɚɩɪɨɫ. Ɏɚɤɬɢɱɟɫɤɢ, ɦɨɠɧɨ ɜɤɥɚɞɵɜɚɬɶ ɩɨɞɡɚɩɪɨɫɵ ɜ ɩɨɞɡɚɩɪɨɫɵ ɧɚ ɛɨɥɶɲɭɸ
ɝɥɭɛɢɧɭ. ɉɨɞɡɚɩɪɨɫ ɜɫɟɝɞɚ ɞɨɥɠɟɧ ɩɨɹɜɥɹɬɶɫɹ ɜ ɫɤɨɛɤɚɯ.
Ɉɫɧɨɜɧɵɟ ɩɪɟɢɦɭɳɟɫɬɜɚ ɩɨɞɡɚɩɪɨɫɨɜ:
„Ɉɧɢ ɩɨɡɜɨɥɹɸɬ ɩɢɫɚɬɶ ɫɬɪɭɤɬɭɪɢɪɨɜɚɧɧɵɟ ɡɚɩɪɨɫɵ ɬɚɤɢɦ ɨɛɪɚɡɨɦ, ɱɬɨ ɦɨɠɧɨ
ɢɡɨɥɢɪɨɜɚɬɶ ɱɚɫɬɢ ɨɩɟɪɚɬɨɪɚ.
242
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
„Ɉɧɢ
ɩɪɟɞɫɬɚɜɥɹɸɬ ɚɥɶɬɟɪɧɚɬɢɜɧɵɣ ɫɩɨɫɨɛ ɜɵɩɨɥɧɟɧɢɹ ɨɩɟɪɚɰɢɣ, ɤɨɬɨɪɵɟ ɬɪɟɛɭɸɬ ɩɪɢɦɟɧɟɧɢɹ ɫɥɨɠɧɵɯ ɫɨɟɞɢɧɟɧɢɣ ɢ ɫɥɢɹɧɢɣ (JOIN ɢ UNION).
„ɉɨ ɦɧɟɧɢɸ ɦɧɨɝɢɯ, ɨɧɢ ɛɨɥɟɟ ɱɢɬɚɛɟɥɶɧɵ. Ⱦɟɣɫɬɜɢɬɟɥɶɧɨ, ɧɨɜɢɡɧɚ ɩɨɞɡɚɩɪɨɫɨɜ ɜ
ɬɨɦ, ɱɬɨ ɨɧɢ ɧɚɝɥɹɞɧɨ ɩɪɟɞɫɬɚɜɥɹɸɬ ɥɸɞɹɦ ɢɫɯɨɞɧɭɸ ɢɞɟɸ SQL ɤɚɤ ɫɬɪɭɤɬɭɪɢɪɨɜɚɧɧɨɝɨ ɹɡɵɤɚ ɡɚɩɪɨɫɨɜ.
ɇɢɠɟ ɩɪɢɜɟɞɟɧ ɩɪɢɦɟɪ ɨɩɟɪɚɬɨɪɚ, ɤɨɬɨɪɵɣ ɞɟɦɨɧɫɬɪɢɪɭɟɬ ɨɫɧɨɜɧɵɟ ɩɨɧɹɬɢɹ ɨ ɫɢɧɬɚɤɫɢɫɟ ɩɨɞɡɚɩɪɨɫɨɜ, ɩɨɞɞɟɪɠɢɜɚɟɦɵɯ MySQL, ɤɚɤ ɬɨɝɨ ɬɪɟɛɭɟɬ ɫɬɚɧɞɚɪɬ SQL:
DELETE FROM t1
WHERE s11 > ANY
(SELECT COUNT(*) /* ɛɟɡ ɩɨɞɫɤɚɡɨɤ */ FROM t2
WHERE NOT EXISTS
(SELECT * FROM t3
WHERE ROW(5*t2.s1,77)=
(SELECT 50,11*s1 FROM t4 UNION SELECT 50,77 FROM
(SELECT * FROM t5) AS t5)));
6.1.8.1. Ïîäçàïðîñ, êàê ñêàëÿðíûé îïåðàíä
ȼ ɫɜɨɟɣ ɩɪɨɫɬɟɣɲɟɣ ɮɨɪɦɟ (ɫɤɚɥɹɪɧɵɣ ɩɨɞɡɚɩɪɨɫ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɨɛɨɣ ɩɪɨɬɢɜɨɩɨɥɨɠɧɨɫɬɶ ɫɬɪɨɱɧɵɦ ɢɥɢ ɬɚɛɥɢɱɧɵɦ ɩɨɞɡɚɩɪɨɫɚɦ, ɨɩɢɫɚɧɧɵɦ ɧɢɠɟ), ɩɨɞɡɚɩɪɨɫ – ɷɬɨ
ɩɪɨɫɬɨɣ ɨɩɟɪɚɧɞ. Ɍɨ ɟɫɬɶ ɜɵ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɟɝɨ ɜ ɥɸɛɨɦ ɦɟɫɬɟ, ɝɞɟ ɞɨɩɭɫɬɢɦɨ
ɡɧɚɱɟɧɢɟ ɫɬɨɥɛɰɚ ɢɥɢ ɥɢɬɟɪɚɥ, ɢ ɜɵ ɦɨɠɟɬɟ ɨɠɢɞɚɬɶ, ɱɬɨ ɨɧ ɢɦɟɟɬ ɬɟ ɠɟ ɯɚɪɚɤɬɟɪɢɫɬɢɤɢ,
ɤɨɬɨɪɵɟ ɢɦɟɸɬ ɜɫɟ ɨɩɟɪɚɧɞɵ: ɬɢɩ ɞɚɧɧɵɯ, ɞɥɢɧɭ, ɩɪɢɡɧɚɤ ɬɨɝɨ, ɦɨɠɟɬ ɥɢ ɨɧ ɩɪɢɧɢɦɚɬɶ
ɡɧɚɱɟɧɢɟ NULL, ɢ ɬɚɤ ɞɚɥɟɟ. ɇɚɩɪɢɦɟɪ:
CREATE TABLE t1 (s1 INT, s2 CHAR(5) NOT NULL);
SELECT (SELECT s2 FROM t1);
ɉɨɞɡɚɩɪɨɫ ɜ ɷɬɨɦ ɡɚɩɪɨɫɟ ɢɦɟɟɬ ɬɢɩ ɞɚɧɧɵɯ CHAR, ɞɥɢɧɭ 5, ɧɚɛɨɪ ɫɢɦɜɨɥɨɜ ɢ ɩɨɪɹɞɨɤ
ɫɨɩɨɫɬɚɜɥɟɧɢɹ ɩɨ ɭɦɨɥɱɚɧɢɸ ɬɚɤɢɟ, ɤɚɤ ɛɵɥɢ ɩɪɢ ɜɵɩɨɥɧɟɧɢɢ CREATE TABLE, ɢ ɩɪɢɡɧɚɤ
ɬɨɝɨ, ɱɬɨ ɡɧɚɱɟɧɢɟ ɫɬɨɥɛɰɚ ɞɨɩɭɫɤɚɟɬ ɡɧɚɱɟɧɢɟ NULL. Ɏɚɤɬɢɱɟɫɤɢ ɩɨɱɬɢ ɜɫɟ ɩɨɞɡɚɩɪɨɫɵ
ɦɨɝɭɬ ɛɵɬɶ NULL, ɩɨɫɤɨɥɶɤɭ ɟɫɥɢ ɬɚɛɥɢɰɚ ɩɭɫɬɚ, ɤɚɤ ɜ ɷɬɨɦ ɩɪɢɦɟɪɟ, ɡɧɚɱɟɧɢɟ ɩɨɞɡɚɩɪɨɫɚ ɛɭɞɟɬ ɪɚɜɧɨ NULL. ɋɭɳɟɫɬɜɭɟɬ ɧɟɫɤɨɥɶɤɨ ɨɝɪɚɧɢɱɟɧɢɣ.
„ȼɧɟɲɧɢɣ
ɨɩɟɪɚɬɨɪ ɩɨɞɡɚɩɪɨɫɚ ɦɨɠɟɬ ɛɵɬɶ ɨɞɧɢɦ ɢɡ ɫɥɟɞɭɸɳɢɯ: SELECT, INSERT,
UPDATE, DELETE, SET ɢɥɢ DO.
„ɉɨɞɡɚɩɪɨɫ
ɦɨɠɟɬ ɫɨɞɟɪɠɚɬɶ ɥɸɛɵɟ ɤɥɸɱɟɜɵɟ ɫɥɨɜɚ ɢ ɤɨɧɫɬɪɭɤɰɢɢ, ɤɨɬɨɪɵɟ ɞɨɩɭɫɬɢɦɵ ɜ ɨɛɵɱɧɨɦ ɨɩɟɪɚɬɨɪɟ SELECT: DISTINCT, GROUP BY, ORDER BY, LIMIT, ɤɨɧɫɬɪɭɤɰɢɢ JOIN, UNION, ɩɨɞɫɤɚɡɤɢ, ɤɨɦɦɟɧɬɚɪɢɢ, ɮɭɧɤɰɢɢ ɢ ɬɚɤ ɞɚɥɟɟ.
ɉɨɷɬɨɦɭ, ɤɨɝɞɚ ɜɵ ɜɢɞɢɬɟ ɩɪɢɦɟɪɵ, ɩɪɢɜɟɞɟɧɧɵɟ ɧɢɠɟ, ɤɨɬɨɪɵɟ ɜɤɥɸɱɚɸɬ ɞɨɜɨɥɶɧɨ
ɤɪɚɬɤɢɟ ɤɨɧɫɬɪɭɤɰɢɢ ɩɨɞɡɚɩɪɨɫɨɜ (SELECT column1 FROM t1), ɬɨ ɩɪɟɞɫɬɚɜɥɹɣɬɟ ɫɟɛɟ
ɫɜɨɣ ɫɨɛɫɬɜɟɧɧɵɣ ɤɨɞ, ɤɨɬɨɪɵɣ ɛɭɞɟɬ ɫɨɞɟɪɠɚɬɶ ɤɭɞɚ ɛɨɥɟɟ ɪɚɡɧɨɨɛɪɚɡɧɵɟ ɢ ɫɥɨɠɧɵɟ
ɤɨɧɫɬɪɭɤɰɢɢ.
ɇɚɩɪɢɦɟɪ, ɩɪɟɞɫɬɚɜɢɦ, ɱɬɨ ɫɨɡɞɚɧɵ ɞɜɟ ɬɚɛɥɢɰɵ:
CREATE
INSERT
CREATE
INSERT
TABLE t1 (s1 INT);
INTO t1 VALUES (1);
TABLE t2 (s1 INT);
INTO t2 VALUES (2);
Ɂɚɬɟɦ ɜɵɩɨɥɧɹɟɬɫɹ ɬɚɤɨɣ ɡɚɩɪɨɫ:
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
243
SELECT (SELECT s1 FROM t2) FROM t1;
Ɋɟɡɭɥɶɬɚɬɨɦ ɛɭɞɟɬ 2, ɩɨɬɨɦɭ ɱɬɨ ɫɭɳɟɫɬɜɭɟɬ ɫɬɪɨɤɚ ɜ t2, ɫɨɞɟɪɠɚɳɚɹ ɫɬɨɥɛɟɰ s1, ɤɨɬɨɪɵɣ ɢɦɟɟɬ ɡɧɚɱɟɧɢɟ 2.
ɉɨɞɡɚɩɪɨɫ ɦɨɠɟɬ ɛɵɬɶ ɱɚɫɬɶɸ ɜɵɪɚɠɟɧɢɹ. ȿɫɥɢ ɷɬɨ ɨɩɟɪɚɧɞ ɮɭɧɤɰɢɢ, ɧɟ ɡɚɛɭɞɶɬɟ
ɭɤɚɡɚɬɶ ɫɤɨɛɤɢ. ɇɚɩɪɢɦɟɪ:
SELECT UPPER((SELECT s1 FROM t1)) FROM t2;
6.1.8.2. Ñðàâíåíèÿ ñ èñïîëüçîâàíèåì ïîäçàïðîñîâ
ɇɚɢɛɨɥɟɟ ɱɚɫɬɨ ɩɨɞɡɚɩɪɨɫɵ ɩɪɢɦɟɧɹɸɬɫɹ ɜ ɬɚɤɨɣ ɮɨɪɦɟ:
ɨɩɟɪɚɧɞ_ɧɟ_ɩɨɞɡɚɩɪɨɫɚ ɨɩɟɪɚɰɢɹ_ɫɪɚɜɧɟɧɢɹ (ɩɨɞɡɚɩɪɨɫ)
Ɂɞɟɫɶ ɨɩɟɪɚɰɢɹ_ɫɪɚɜɧɟɧɢɹ – ɷɬɨ ɨɞɧɚ ɢɡ ɫɥɟɞɭɸɳɢɯ ɨɩɟɪɚɰɢɣ:
=
>
<
>=
<=
<>
ɇɚɩɪɢɦɟɪ:
... 'a' = (SELECT column1 FROM t1)
ȿɞɢɧɫɬɜɟɧɧɨɟ ɪɚɡɪɟɲɟɧɧɨɟ ɦɟɫɬɨ ɞɥɹ ɩɨɞɡɚɩɪɨɫɚ – ɜ ɩɪɚɜɨɣ ɱɚɫɬɢ ɜɵɪɚɠɟɧɢɹ ɫɪɚɜɧɟɧɢɹ, ɢ ɜɵ ɦɨɠɟɬɟ ɧɚɣɬɢ ɧɟɤɨɬɨɪɵɟ ɫɬɚɪɵɟ ɫɢɫɬɟɦɵ ɭɩɪɚɜɥɟɧɢɹ ɛɚɡɚɦɢ ɞɚɧɧɵɯ, ɤɨɬɨɪɵɟ ɧɚɫɬɚɢɜɚɸɬ ɧɚ ɷɬɨɦ.
ɇɢɠɟ ɩɪɢɜɟɞɟɧ ɩɪɢɦɟɪ ɨɛɳɟɣ ɮɨɪɦɵ ɫɪɚɜɧɟɧɢɹ ɫ ɩɨɞɡɚɩɪɨɫɨɦ, ɤɨɬɨɪɭɸ ɧɟɥɶɡɹ
ɩɪɢɦɟɧɹɬɶ ɜ ɫɨɟɞɢɧɟɧɢɢ. Ɉɧ ɧɚɣɞɟɬ ɜɫɟ ɡɧɚɱɟɧɢɹ ɬɚɛɥɢɰɵ t1, ɤɨɬɨɪɵɟ ɪɚɜɧɵ ɦɚɤɫɢɦɚɥɶɧɨɦɭ ɡɧɚɱɟɧɢɸ ɜ ɬɚɛɥɢɰɟ t2:
SELECT column1 FROM t1
WHERE column1 = (SELECT MAX(column2) FROM t2);
Ⱥ ɜɨɬ ɞɪɭɝɨɣ ɩɪɢɦɟɪ, ɬɚɤɠɟ ɧɟɜɨɡɦɨɠɧɵɣ ɜ ɜɢɞɟ ɫɨɟɞɢɧɟɧɢɹ, ɩɨɫɤɨɥɶɤɭ ɜɤɥɸɱɚɟɬ
ɚɝɪɟɝɚɬɧɭɸ ɮɭɧɤɰɢɸ ɜ ɨɞɧɨɣ ɢɡ ɬɚɛɥɢɰ. Ɉɧ ɧɚɣɞɟɬ ɜɫɟ ɫɬɪɨɤɢ ɬɚɛɥɢɰɵ t1, ɫɨɞɟɪɠɚɳɢɟ
ɡɧɚɱɟɧɢɟ, ɤɨɬɨɪɨɟ ɜɫɬɪɟɱɚɟɬɫɹ ɞɜɚɠɞɵ:
SELECT * FROM t1
WHERE 2 = (SELECT COUNT(column1) FROM t1);
6.1.8.3. Ïîäçàïðîñû ñ ANY, IN è SOME
ɋɢɧɬɚɤɫɢɫ:
ɨɩɟɪɚɧɞ ɨɩɟɪɚɰɢɹ_ɫɪɚɜɧɟɧɢɹ ANY (ɩɨɞɡɚɩɪɨɫ)
ɨɩɟɪɚɧɞ IN (ɩɨɞɡɚɩɪɨɫ)
ɨɩɟɪɚɧɞ ɨɩɟɪɚɰɢɹ_ɫɪɚɜɧɟɧɢɹ SOME (ɩɨɞɡɚɩɪɨɫ)
Ʉɥɸɱɟɜɨɟ ɫɥɨɜɨ ANY, ɤɨɬɨɪɨɟ ɞɨɥɠɧɨ ɫɥɟɞɨɜɚɬɶ ɡɚ ɨɩɟɪɚɰɢɟɣ ɫɪɚɜɧɟɧɢɹ, ɨɡɧɚɱɚɟɬ
“ɜɨɡɜɪɚɬɢɬɶ TRUE, ɟɫɥɢ ɫɪɚɜɧɟɧɢɟ ɞɚɟɬ TRUE ɞɥɹ ɅɘȻɈɃ ɢɡ ɫɬɪɨɤ, ɤɨɬɨɪɵɟ ɜɨɡɜɪɚɳɚɟɬ
ɩɨɞɡɚɩɪɨɫ”. ɇɚɩɪɢɦɟɪ:
SELECT s1 FROM t1 WHERE s1 > ANY (SELECT s1 FROM t2);
ɉɪɟɞɩɨɥɨɠɢɦ, ɱɬɨ ɜ ɬɚɛɥɢɰɟ t1 ɟɫɬɶ ɫɬɪɨɤɚ, ɤɨɬɨɪɚɹ ɫɨɞɟɪɠɢɬ (10). ȼɵɪɚɠɟɧɢɟ ɢɫɬɢɧɧɨ, ɟɫɥɢ ɬɚɛɥɢɰɚ t2 ɫɨɞɟɪɠɢɬ (21,14,7), ɩɨɫɤɨɥɶɤɭ ɜ t2 ɟɫɬɶ ɡɧɚɱɟɧɢɟ 7, ɤɨɬɨɪɨɟ
ɦɟɧɶɲɟ 10. ȼɵɪɚɠɟɧɢɟ ɥɨɠɧɨ, ɟɫɥɢ ɬɚɛɥɢɰɚ t2 ɫɨɞɟɪɠɢɬ (20,10), ɥɢɛɨ ɬɚɛɥɢɰɚ t2 ɩɭɫɬɚ. ȼɵɪɚɠɟɧɢɟ ɪɚɜɧɨ UNKNOWN, ɟɫɥɢ ɬɚɛɥɢɰɚ t2 ɫɨɞɟɪɠɢɬ ɡɧɚɱɟɧɢɹ (NULL,NULL,NULL).
ɋɥɨɜɨ IN – ɷɬɨ ɩɫɟɜɞɨɧɢɦ ɞɥɹ = ANY, ɩɨɷɬɨɦɭ ɫɥɟɞɭɸɳɢɟ ɞɜɚ ɨɩɟɪɚɬɨɪɚ ɨɞɢɧɚɤɨɜɵ:
244
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
SELECT s1 FROM t1 WHERE s1 = ANY (SELECT s1 FROM t2);
SELECT s1 FROM t1 WHERE s1 IN (SELECT s1 FROM t2);
ɋɥɨɜɨ SOME – ɷɬɨ ɩɫɟɜɞɨɧɢɦ ɞɥɹ ANY, ɩɨɷɬɨɦɭ ɩɨɤɚɡɚɧɧɵɟ ɧɢɠɟ ɞɜɚ ɨɩɟɪɚɬɨɪɚ ɨɞɢɧɚɤɨɜɵ:
SELECT s1 FROM t1 WHERE s1 <> ANY (SELECT s1 FROM t2);
SELECT s1 FROM t1 WHERE s1 <> SOME (SELECT s1 FROM t2);
ɋɥɨɜɨ SOME ɩɪɢɦɟɧɹɟɬɫɹ ɪɟɞɤɨ, ɧɨ ɩɪɟɞɵɞɭɳɢɣ ɩɪɢɦɟɪ ɩɨɤɚɡɵɜɚɟɬ, ɩɨɱɟɦɭ ɨɧɨ ɦɨɠɟɬ ɨɤɚɡɚɬɶɫɹ ɭɞɨɛɧɵɦ. Ⱦɥɹ ɫɥɭɯɚ ɛɨɥɶɲɢɧɫɬɜɚ ɥɸɞɟɣ ɚɧɝɥɢɣɫɤɚɹ ɮɪɚɡɚ “a is not equal
to any b” (“a ɧɟ ɪɚɜɧɨ ɥɸɛɨɦɭ b”) ɨɡɧɚɱɚɟɬ “there is no b which is equal to a” (“ɧɟɬ ɬɚɤɢɯ b,
ɤɨɬɨɪɵɟ ɪɚɜɧɵ a”), ɧɨ ɷɬɨ ɧɟ ɬɨ, ɱɬɨ ɩɨɞɪɚɡɭɦɟɜɚɟɬ ɫɢɧɬɚɤɫɢɫ SQL. ɉɪɢɦɟɧɟɧɢɟ <> SOME
ɩɨɦɨɝɚɟɬ ɜɫɟɦ ɩɪɚɜɢɥɶɧɨ ɩɨɧɢɦɚɬɶ ɞɟɣɫɬɜɢɬɟɥɶɧɵɣ ɫɦɵɫɥ ɡɚɩɪɨɫɚ.
6.1.8.4. Ïîäçàïðîñû ñ ALL
ɋɢɧɬɚɤɫɢɫ:
ɨɩɟɪɚɧɞ ɨɩɟɪɚɰɢɹ_ɫɪɚɜɧɟɧɢɹ ALL (ɩɨɞɡɚɩɪɨɫ)
ɋɥɨɜɨ ALL, ɤɨɬɨɪɨɟ ɞɨɥɠɧɨ ɫɥɟɞɨɜɚɬɶ ɡɚ ɨɩɟɪɚɰɢɟɣ ɫɪɚɜɧɟɧɢɹ, ɨɡɧɚɱɚɟɬ “ɜɨɡɜɪɚɬɢɬɶ
TRUE, ɟɫɥɢ ɫɪɚɜɧɟɧɢɟ ɞɚɟɬ TRUE ɞɥɹ ɜɫɟɯ ɫɬɪɨɤ, ɜɨɡɜɪɚɳɚɟɦɵɯ ɩɨɞɡɚɩɪɨɫɨɦ”. ɇɚɩɪɢɦɟɪ:
SELECT s1 FROM t1 WHERE s1 > ALL (SELECT s1 FROM t2);
ɉɪɟɞɩɨɥɨɠɢɦ, ɱɬɨ ɜ ɬɚɛɥɢɰɟ t1 ɟɫɬɶ ɫɬɪɨɤɚ, ɤɨɬɨɪɚɹ ɫɨɞɟɪɠɢɬ (10). ȼɵɪɚɠɟɧɢɟ ɢɫɬɢɧɧɨ, ɟɫɥɢ ɬɚɛɥɢɰɚ t2 ɫɨɞɟɪɠɢɬ (-5, 0, +5), ɩɨɬɨɦɭ ɱɬɨ 10 ɛɨɥɶɲɟ, ɱɟɦ ɜɫɟ ɬɪɢ ɡɧɚɱɟɧɢɹ ɢɡ ɬɚɛɥɢɰɵ t2. ȼɵɪɚɠɟɧɢɟ ɥɨɠɧɨ, ɟɫɥɢ ɬɚɛɥɢɰɚ t2 ɫɨɞɟɪɠɢɬ (12,6,NULL,-100), ɩɨɫɤɨɥɶɤɭ ɬɨɥɶɤɨ ɨɞɧɨ ɡɧɚɱɟɧɢɟ 12 ɜ ɬɚɛɥɢɰɟ t2 ɛɨɥɶɲɟ 10. ȼɵɪɚɠɟɧɢɟ ɧɟɨɩɪɟɞɟɥɟɧɧɨ
(UNKNOWN), ɟɫɥɢ ɬɚɛɥɢɰɚ t2 ɫɨɞɟɪɠɢɬ (0,NULL,1).
ɇɚɤɨɧɟɰ, ɟɫɥɢ ɬɚɛɥɢɰɚ t2 ɩɭɫɬɚ, ɪɟɡɭɥɶɬɚɬ ɪɚɜɟɧ TRUE. ȼɵ ɦɨɠɟɬɟ ɩɨɞɭɦɚɬɶ, ɱɬɨ ɪɟɡɭɥɶɬɚɬ ɛɭɞɟɬ UNKNOWN, ɧɨ, ɫɨɠɚɥɟɟɦ, ɨɧ ɛɭɞɟɬ ɢɦɟɧɧɨ TRUE. ɉɨɷɬɨɦɭ, ɤɚɤ ɧɢ ɫɬɪɚɧɧɨ,
ɫɥɟɞɭɸɳɢɣ ɨɩɟɪɚɬɨɪ ɜɟɪɧɟɬ TRUE, ɟɫɥɢ ɬɚɛɥɢɰɚ t2 ɩɭɫɬɚ:
SELECT * FROM t1 WHERE 1 > ALL (SELECT s1 FROM t2);
Ɉɞɧɚɤɨ ɫɥɟɞɭɸɳɢɣ ɨɩɟɪɚɬɨɪ ɜɟɪɧɟɬ UNKNOWN, ɟɫɥɢ ɬɚɛɥɢɰɚ t2 ɩɭɫɬɚ:
SELECT * FROM t1 WHERE 1 > (SELECT s1 FROM t2);
ɂ ɜɞɨɛɚɜɨɤ ɫɥɟɞɭɸɳɢɣ ɨɩɟɪɚɬɨɪ ɬɚɤɠɟ ɜɟɪɧɟɬ UNKNOWN, ɟɫɥɢ ɬɚɛɥɢɰɚ t2 ɩɭɫɬɚ:
SELECT * FROM t1 WHERE 1 > ALL (SELECT MAX(s1) FROM t2);
ȼ ɨɛɳɟɦ ɫɥɭɱɚɟ, ɬɚɛɥɢɰɵ ɫɨ ɡɧɚɱɟɧɢɹɦɢ NULL ɢ ɩɭɫɬɵɟ ɬɚɛɥɢɰɵ – ɷɬɨ ɤɪɚɣɧɢɟ ɫɥɭɱɚɢ. ɉɨɷɬɨɦɭ ɩɪɢ ɧɚɩɢɫɚɧɢɢ ɤɨɞɚ ɩɨɞɡɚɩɪɨɫɨɜ ɜɫɟɝɞɚ ɩɪɢɧɢɦɚɣɬɟ ɜɨ ɜɧɢɦɚɧɢɟ ɞɜɟ ɭɩɨɦɹɧɭɬɵɯ ɜɨɡɦɨɠɧɨɫɬɢ.
6.1.8.5. Êîððåëèðîâàííûå ïîäçàïðîñû
Ʉɨɪɪɟɥɢɪɨɜɚɧɧɵɣ ɩɨɞɡɚɩɪɨɫ – ɷɬɨ ɬɚɤɨɣ ɩɨɞɡɚɩɪɨɫ, ɤɨɬɨɪɵɣ ɫɨɞɟɪɠɢɬ ɫɫɵɥɤɭ ɧɚ
ɫɬɨɥɛɟɰ, ɤɨɬɨɪɵɣ ɟɫɬɶ ɜɨ ɜɧɟɲɧɟɦ ɡɚɩɪɨɫɟ. ɇɚɩɪɢɦɟɪ:
SELECT * FROM t1 WHERE column1 = ANY
(SELECT column1 FROM t2 WHERE t2.column2 = t1.column2);
Ɉɛɪɚɬɢɬɟ ɜɧɢɦɚɧɢɟ, ɱɬɨ ɩɨɞɡɚɩɪɨɫ ɫɨɞɟɪɠɢɬ ɫɫɵɥɤɭ ɧɚ ɫɬɨɥɛɟɰ ɬɚɛɥɢɰɵ t1, ɞɚɠɟ ɧɟɫɦɨɬɪɹ ɧɚ ɬɨ, ɱɬɨ ɤɨɧɫɬɪɭɤɰɢɹ FROM ɩɨɞɡɚɩɪɨɫɚ ɧɟ ɭɩɨɦɢɧɚɟɬ ɬɚɛɥɢɰɭ t1. Ɍɚɤɢɦ ɨɛɪɚ-
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
245
ɡɨɦ, MySQL ɜɵɩɨɥɧɹɟɬ ɩɪɨɫɦɨɬɪ ɡɚ ɩɪɟɞɟɥɚɦɢ ɩɨɞɡɚɩɪɨɫɚ ɢ ɧɚɯɨɞɢɬ t1 ɜɨ ɜɧɟɲɧɟɦ
ɡɚɩɪɨɫɟ.
ɉɪɟɞɩɨɥɨɠɢɦ, ɱɬɨ ɬɚɛɥɢɰɚ t1 ɫɨɞɟɪɠɢɬ ɫɬɪɨɤɭ, ɜ ɤɨɬɨɪɨɣ column1 = 5 ɢ column2 = 6,
ɜ ɬɨ ɠɟ ɜɪɟɦɹ ɬɚɛɥɢɰɚ t2 ɫɨɞɟɪɠɢɬ ɫɬɪɨɤɭ, ɜ ɤɨɬɨɪɨɣ column1 = 5 ɢ column2 = 7. ɉɪɨɫɬɨɟ ɜɵɪɚɠɟɧɢɟ ...WHERE column1=ANY(SELECT column1 FROM t2) ɛɭɞɟɬ TRUE, ɧɨ ɜ ɷɬɨɦ
ɩɪɢɦɟɪɟ ɤɨɧɫɬɪɭɤɰɢɹ WHERE ɜɧɭɬɪɢ ɩɨɞɡɚɩɪɨɫɚ ɞɚɫɬ FALSE (ɩɨɫɤɨɥɶɤɭ 7 ɧɟ ɪɚɜɧɨ 5), ɚ
ɩɨɷɬɨɦɭ ɢ ɜɟɫɶ ɩɨɞɡɚɩɪɨɫ ɜɟɪɧɟɬ FALSE.
ɉɪɚɜɢɥɨ ɜɢɞɢɦɨɫɬɢ: MySQL ɜɵɱɢɫɥɹɟɬ ɜɵɪɚɠɟɧɢɹ ɨɬ ɜɧɭɬɪɟɧɧɟɝɨ ɤ ɜɧɟɲɧɟɦɭ.
ɇɚɩɪɢɦɟɪ:
SELECT column1 FROM t1 AS x
WHERE x.column1 = (SELECT column1 FROM t2 AS x
WHERE x.column1 = (SELECT column1 FROM t3
WHERE x.column2 = t3.column1));
ȼ ɷɬɨɦ ɡɚɩɪɨɫɟ x.column2 ɞɨɥɠɟɧ ɛɵɬɶ ɫɬɨɥɛɰɨɦ ɬɚɛɥɢɰɵ t2, ɩɨɬɨɦɭ ɱɬɨ SELECT
column1 FROM t2 AS x ... ɩɟɪɟɢɦɟɧɨɜɵɜɚɟɬ t2. ɗɬɨ ɧɟ ɫɬɨɥɛɟɰ ɬɚɛɥɢɰɵ t1, ɬɚɤ ɤɚɤ
SELECT column1 FROM t1 ... – ɞɪɭɝɨɣ ɡɚɩɪɨɫ, ɤɨɬɨɪɵɣ ɧɚɯɨɞɢɬɫɹ ɜɨ ɜɧɟɲɧɟɦ ɤɨɧɬɟɤɫɬɟ.
Ⱦɥɹ ɩɨɞɡɚɩɪɨɫɨɜ, ɧɚɯɨɞɹɳɢɯɫɹ ɜ ɤɨɧɫɬɪɭɤɰɢɹɯ HAVING ɢɥɢ ORDER BY, MySQL ɬɚɤɠɟ
ɢɳɟɬ ɢɦɟɧɚ ɜ ɫɩɢɫɤɟ ɫɬɨɥɛɰɨɜ ɜɧɟɲɧɟɝɨ ɡɚɩɪɨɫɚ.
ȼ ɧɟɤɨɬɨɪɵɯ ɫɥɭɱɚɹɯ ɤɨɪɪɟɥɢɪɨɜɚɧɧɵɣ ɩɨɞɡɚɩɪɨɫ ɨɩɬɢɦɢɡɢɪɭɟɬɫɹ. ɇɚɩɪɢɦɟɪ:
ɡɧɚɱɟɧɢɟ IN (SELECT ɡɧɚɱɟɧɢɟ_ɤɥɸɱɚ FROM ɢɦɹ_ɬɚɛɥɢɰɵ
WHERE ɤɨɪɪɟɥɢɪɨɜɚɧɧɨɟ_ɭɫɥɨɜɢɟ)
ɂɧɚɱɟ ɨɧɢ ɦɨɝɭɬ ɨɤɚɡɚɬɶɫɹ ɧɟɷɮɮɟɤɬɢɜɧɵɦɢ ɢ, ɫɤɨɪɟɟ ɜɫɟɝɨ, ɦɟɞɥɟɧɧɵɦɢ. ȿɫɥɢ ɩɟɪɟɩɢɫɚɬɶ ɡɚɩɪɨɫ ɜ ɜɢɞɟ ɫɨɟɞɢɧɟɧɢɹ, ɷɬɨ ɦɨɠɟɬ ɭɜɟɥɢɱɢɬɶ ɩɪɨɢɡɜɨɞɢɬɟɥɶɧɨɫɬɶ.
6.1.8.6. EXISTS è NOT EXISTS
ȿɫɥɢ ɩɨɞɡɚɩɪɨɫ ɜɨɨɛɳɟ ɜɨɡɜɪɚɳɚɟɬ ɤɚɤɢɟ-ɧɢɛɭɞɶ ɡɧɚɱɟɧɢɹ, ɬɨ EXISTS ɩɨɞɡɚɩɪɨɫ
ɜɨɡɜɪɚɳɚɟɬ TRUE, ɚ NOT EXISTS ɩɨɞɡɚɩɪɨɫ – FALSE, ɧɚɩɪɢɦɟɪ:
SELECT column1 FROM t1 WHERE EXISTS (SELECT * FROM t2);
Ɍɪɚɞɢɰɢɨɧɧɨ ɩɨɞɡɚɩɪɨɫ ɜ EXISTS ɧɚɱɢɧɚɟɬɫɹ ɫ SELECT *, ɧɨ ɨɧ ɦɨɠɟɬ ɧɚɱɢɧɚɬɶɫɹ ɫ
SELECT 5 ɢɥɢ SELECT column1, ɥɢɛɨ ɫ ɟɳɟ ɱɟɝɨ-ɧɢɛɭɞɶ. MySQL ɢɝɧɨɪɢɪɭɟɬ ɫɩɢɫɨɤ
SELECT ɜ ɬɚɤɨɦ ɩɨɞɡɚɩɪɨɫɟ, ɩɨɬɨɦɭ ɷɬɨ ɧɟ ɜɚɠɧɨ.
Ⱦɥɹ ɩɪɟɞɵɞɭɳɟɝɨ ɩɪɢɦɟɪɚ, ɟɫɥɢ t2 ɫɨɞɟɪɠɢɬ ɥɸɛɵɟ ɫɬɪɨɤɢ, ɞɚɠɟ ɫɬɪɨɤɢ, ɜ ɤɨɬɨɪɵɯ
ɧɟɬ ɧɢɱɟɝɨ ɤɪɨɦɟ ɡɧɚɱɟɧɢɣ NULL, ɬɨ ɭɫɥɨɜɢɟ EXISTS ɢɫɬɢɧɧɨ. ȼɨɨɛɳɟ ɷɬɨ ɧɟɩɪɚɜɞɨɩɨɞɨɛɧɵɣ ɩɪɢɦɟɪ, ɩɨɫɤɨɥɶɤɭ ɩɨɱɬɢ ɜɫɟɝɞɚ ɩɨɞɡɚɩɪɨɫ [NOT] EXISTS ɫɨɞɟɪɠɢɬ ɤɨɪɪɟɥɹɰɢɸ.
ɇɢɠɟ ɩɪɟɞɫɬɚɜɥɟɧɵ ɛɨɥɟɟ ɪɟɚɥɢɫɬɢɱɧɵɟ ɩɪɢɦɟɪɵ:
„Ʉɚɤɢɟ ɬɢɩɵ ɦɚɝɚɡɢɧɨɜ ɟɫɬɶ ɜ ɨɞɧɨɦ ɢɥɢ ɛɨɥɟɟ ɝɨɪɨɞɨɜ?
SELECT DISTINCT store_type FROM Stores
WHERE EXISTS (SELECT * FROM Cities_Stores
WHERE Cities_Stores.store_type = Stores.store_type);
„Ʉɚɤɢɯ
ɬɢɩɨɜ ɦɚɝɚɡɢɧɨɜ ɧɟɬ ɧɢ ɜ ɨɞɧɨɦ ɝɨɪɨɞɟ?
SELECT DISTINCT store_type FROM Stores
WHERE NOT EXISTS (SELECT * FROM Cities_Stores
WHERE Cities_Stores.store_type = Stores.store_type);
„Ʉɚɤɨɣ
ɬɢɩ ɦɚɝɚɡɢɧɨɜ ɟɫɬɶ ɜɨ ɜɫɟɯ ɝɨɪɨɞɚɯ?
246
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
SELECT DISTINCT store_type FROM Stores S1
WHERE NOT EXISTS (
SELECT * FROM Cities WHERE NOT EXISTS (
SELECT * FROM Cities_Stores
WHERE Cities_Stores.city = Cities.city
AND Cities_Stores.store_type = Stores.store_type));
ȼ ɩɨɫɥɟɞɧɟɦ ɩɪɢɦɟɪɟ ɩɪɟɞɫɬɚɜɥɟɧ ɞɜɚɠɞɵ ɜɥɨɠɟɧɧɵɣ ɩɨɞɡɚɩɪɨɫ NOT EXISTS. Ɍɨ ɟɫɬɶ
ɤɨɧɫɬɪɭɤɰɢɹ NOT EXISTS ɫɨɞɟɪɠɢɬɫɹ ɜɧɭɬɪɢ ɞɪɭɝɨɣ ɤɨɧɫɬɪɭɤɰɢɢ NOT EXISTS. Ɏɨɪɦɚɥɶɧɨ ɨɧ ɨɬɜɟɱɚɟɬ ɧɚ ɜɨɩɪɨɫ “ɟɫɬɶ ɥɢ ɝɨɪɨɞ ɫ ɦɚɝɚɡɢɧɨɦ, ɤɨɬɨɪɨɝɨ ɧɟɬ ɜ Stores?”. ɇɨ ɩɪɨɳɟ
ɫɤɚɡɚɬɶ, ɱɬɨ ɜɥɨɠɟɧɧɵɣ NOT EXISTS ɨɬɜɟɱɚɟɬ ɧɚ ɜɨɩɪɨɫ “ɢɫɬɢɧɧɨ ɥɢ x ɞɥɹ ɜɫɟɯ y?”.
6.1.8.7. Ïîäçàïðîñû, âîçâðàùàþùèå ñòðîêó
Ⱦɨ ɫɢɯ ɩɨɪ ɦɵ ɨɛɫɭɠɞɚɥɢ ɩɨɞɡɚɩɪɨɫɵ, ɜɨɡɜɪɚɳɚɸɳɢɟ ɫɬɨɥɛɟɰ (ɫɤɚɥɹɪɧɵɟ), ɬɨ ɟɫɬɶ
ɩɨɞɡɚɩɪɨɫɵ, ɜɨɡɜɪɚɳɚɸɳɢɟ ɟɞɢɧɫɬɜɟɧɧɨɟ ɡɧɚɱɟɧɢɟ ɫɬɨɥɛɰɚ. ɋɬɪɨɱɧɵɟ ɩɨɞɡɚɩɪɨɫɵ –
ɷɬɨ ɜɚɪɢɚɧɬ ɩɨɞɡɚɩɪɨɫɨɜ, ɤɨɬɨɪɵɟ ɜɨɡɜɪɚɳɚɸɬ ɛɨɥɟɟ ɨɞɧɨɝɨ ɡɧɚɱɟɧɢɹ ɫɬɨɥɛɰɚ. ɇɢɠɟ
ɩɪɟɞɫɬɚɜɥɟɧɵ ɞɜɚ ɩɪɢɦɟɪɚ:
SELECT * FROM t1 WHERE (1,2) = (SELECT column1, column2 FROM t2);
SELECT * FROM t1 WHERE ROW(1,2) = (SELECT column1, column2 FROM t2);
Ɉɛɚ ɷɬɢ ɡɚɩɪɨɫɚ ɢɫɬɢɧɧɵ, ɟɫɥɢ ɜ ɬɚɛɥɢɰɟ t2 ɩɪɢɫɭɬɫɬɜɭɟɬ ɫɬɪɨɤɚ, ɜ ɤɨɬɨɪɨɣ
column1 = 1 ɢ column2 = 2.
ȼɵɪɚɠɟɧɢɹ (1,2) ɢ ROW(1,2) ɢɧɨɝɞɚ ɧɚɡɵɜɚɸɬ ɤɨɧɫɬɪɭɤɬɨɪɨɦ ɫɬɪɨɤɢ. ɗɬɢ ɞɜɚ ɜɵɪɚɠɟɧɢɹ ɷɤɜɢɜɚɥɟɧɬɧɵ. Ɉɧɢ ɜɩɨɥɧɟ ɤɨɪɪɟɤɬɧɵ ɢ ɜ ɞɪɭɝɢɯ ɤɨɧɬɟɤɫɬɚɯ. ɇɚɩɪɢɦɟɪ, ɫɥɟɞɭɸɳɢɟ ɞɜɚ ɨɩɟɪɚɬɨɪɚ ɫɟɦɚɧɬɢɱɟɫɤɢ ɷɤɜɢɜɚɥɟɧɬɧɵ (ɧɟɫɦɨɬɪɹ ɧɚ ɬɨ, ɱɬɨ ɬɨɥɶɤɨ ɜɬɨɪɨɣ
ɢɡ ɧɢɯ ɦɨɠɟɬ ɛɵɬɶ ɨɩɬɢɦɢɡɢɪɨɜɚɧ):
SELECT * FROM t1 WHERE (column1,column2) = (1,1);
SELECT * FROM t1 WHERE column1 = 1 AND column2 = 1;
Ʉɚɤ ɩɪɚɜɢɥɨ, ɤɨɧɫɬɪɭɤɬɨɪɵ ɫɬɪɨɤɢ ɢɫɩɨɥɶɡɭɸɬɫɹ ɞɥɹ ɫɪɚɜɧɟɧɢɹ ɫ ɩɨɞɡɚɩɪɨɫɚɦɢ, ɜɨɡɜɪɚɳɚɸɳɢɦɢ ɞɜɚ ɢɥɢ ɛɨɥɟɟ ɫɬɨɥɛɰɨɜ. ɇɚɩɪɢɦɟɪ, ɩɪɟɞɫɬɚɜɥɟɧɧɵɣ ɧɢɠɟ ɡɚɩɪɨɫ ɜɵɩɨɥɧɹɟɬ ɫɥɟɞɭɸɳɭɸ ɞɢɪɟɤɬɢɜɭ: “ɧɚɣɬɢ ɜɫɟ ɫɬɪɨɤɢ ɬɚɛɥɢɰɵ t1, ɤɨɬɨɪɵɟ ɟɫɬɶ ɬɚɤɠɟ ɢ ɜ ɬɚɛɥɢɰɟ t2”:
SELECT column1,column2,column3
FROM t1
WHERE (column1,column2,column3) IN
(SELECT column1,column2,column3 FROM t2);
6.1.8.8. Ïîäçàïðîñû â êîíñòðóêöèè FROM
ɉɨɞɡɚɩɪɨɫɵ ɪɚɡɪɟɲɟɧɵ ɢ ɜ ɤɨɧɫɬɪɭɤɰɢɢ FROM ɨɩɟɪɚɬɨɪɚ SELECT. ɂɯ ɫɢɧɬɚɤɫɢɫ ɜɵɝɥɹɞɢɬ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
SELECT ... FROM (ɩɨɞɡɚɩɪɨɫ) AS ɢɦɹ ...
Ʉɨɧɫɬɪɭɤɰɢɹ AS ɢɦɹ ɹɜɥɹɟɬɫɹ ɨɛɹɡɚɬɟɥɶɧɨɣ, ɩɨɫɤɨɥɶɤɭ ɤɚɠɞɚɹ ɬɚɛɥɢɰɚ ɜ ɤɨɧɫɬɪɭɤɰɢɢ FROM ɞɨɥɠɧɚ ɢɦɟɬɶ ɢɦɹ. ȼɫɟ ɫɬɨɥɛɰɵ ɜ ɫɩɢɫɤɟ ɩɨɞɡɚɩɪɨɫɚ ɩɨɞɡɚɩɪɨɫ ɬɚɤɠɟ ɞɨɥɠɧɵ
ɢɦɟɬɶ ɭɧɢɤɚɥɶɧɵɟ ɢɦɟɧɚ.
Ⱦɥɹ ɬɨɝɨ ɱɬɨɛɵ ɩɪɨɢɥɥɸɫɬɪɢɪɨɜɚɬɶ ɷɬɨ, ɩɪɟɞɩɨɥɨɠɢɦ, ɱɬɨ ɢɦɟɟɬɫɹ ɬɚɤɚɹ ɬɚɛɥɢɰɚ:
CREATE TABLE t1 (s1 INT, s2 CHAR(5), s3 FLOAT);
ȼɨɬ ɤɚɤ ɢɫɩɨɥɶɡɨɜɚɬɶ ɩɨɞɡɚɩɪɨɫɵ ɜ ɤɨɧɫɬɪɭɤɰɢɢ FROM ɞɥɹ ɞɚɧɧɨɝɨ ɩɪɢɦɟɪɚ ɬɚɛɥɢɰɵ:
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
247
INSERT INTO t1 VALUES (1,'1',1.0);
INSERT INTO t1 VALUES (2,'2',2.0);
SELECT sb1,sb2,sb3
FROM (SELECT s1 AS sb1, s2 AS sb2, s3*2 AS sb3 FROM t1) AS sb
WHERE sb1 > 1;
Ɋɟɡɭɥɶɬɚɬ: 2, '2', 4.0.
Ⱥ ɜɨɬ ɞɪɭɝɨɣ ɩɪɢɦɟɪ. ɉɪɟɞɩɨɥɨɠɢɦ, ɱɬɨ ɜɵ ɯɨɬɢɬɟ ɡɧɚɬɶ ɫɪɟɞɧɟɟ ɡɧɚɱɟɧɢɟ ɫɭɦɦ ɜ
ɫɝɪɭɩɩɢɪɨɜɚɧɧɨɣ ɬɚɛɥɢɰɟ. ɋɥɟɞɭɸɳɢɣ ɜɚɪɢɚɧɬ ɪɚɛɨɬɚɬɶ ɧɟ ɛɭɞɟɬ:
SELECT AVG(SUM(column1)) FROM t1 GROUP BY column1;
Ɉɞɧɚɤɨ ɩɪɢɜɟɞɟɧɧɵɣ ɧɢɠɟ ɡɚɩɪɨɫ ɜɵɞɚɫɬ ɧɭɠɧɭɸ ɢɧɮɨɪɦɚɰɢɸ:
SELECT AVG(sum_column1)
FROM (SELECT SUM(column1) AS sum_column1
FROM t1 GROUP BY column1) AS t1;
Ɉɬɦɟɬɢɦ, ɱɬɨ ɢɦɹ ɫɬɨɥɛɰɚ, ɢɫɩɨɥɶɡɭɟɦɨɟ ɜ ɩɨɞɡɚɩɪɨɫɟ (sum_column1) ɪɚɫɩɨɡɧɚɟɬɫɹ ɜɨ
ɜɧɟɲɧɟɦ ɡɚɩɪɨɫɟ.
ɇɚ ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɩɨɞɡɚɩɪɨɫɵ ɜ ɤɨɧɫɬɪɭɤɰɢɢ FROM ɧɟ ɦɨɝɭɬ ɛɵɬɶ ɤɨɪɪɟɥɢɪɨɜɚɧɧɵɦɢ
ɩɨɞɡɚɩɪɨɫɚɦɢ.
ɉɨɞɡɚɩɪɨɫ ɜ ɤɨɧɫɬɪɭɤɰɢɢ FROM ɛɭɞɟɬ ɜɵɩɨɥɧɟɧ (ɬɨ ɟɫɬɶ ɛɭɞɟɬ ɫɨɡɞɚɧɚ ɜɪɟɦɟɧɧɚɹ
ɬɚɛɥɢɰɚ) ɞɚɠɟ ɞɥɹ ɨɩɟɪɚɬɨɪɚ EXPLAIN, ɩɨɬɨɦɭ ɱɬɨ ɡɚɩɪɨɫɵ ɜɟɪɯɧɟɝɨ ɭɪɨɜɧɹ ɧɭɠɞɚɸɬɫɹ ɜ
ɢɧɮɨɪɦɚɰɢɢ ɨɛɨ ɜɫɟɯ ɬɚɛɥɢɰɚɯ ɧɚ ɫɬɚɞɢɢ ɨɩɬɢɦɢɡɚɰɢɢ.
6.1.8.9. Îøèáêè ïîäçàïðîñîâ
ɉɨɹɜɢɥɫɹ ɪɹɞ ɧɨɜɵɯ ɬɢɩɨɜ ɨɲɢɛɨɤ, ɤɨɬɨɪɵɟ ɢɦɟɸɬ ɨɬɧɨɲɟɧɢɟ ɤ ɩɨɞɡɚɩɪɨɫɚɦ. ȼ
ɷɬɨɦ ɪɚɡɞɟɥɟ ɨɧɢ ɫɝɪɭɩɩɢɪɨɜɚɧɵ ɜɦɟɫɬɟ, ɩɨɫɤɨɥɶɤɭ ɢɯ ɩɪɨɫɦɨɬɪ ɩɨɦɨɠɟɬ ɡɚɩɨɦɧɢɬɶ
ɧɟɤɨɬɨɪɵɟ ɜɚɠɧɵɟ ɦɨɦɟɧɬɵ.
„ɇɟɩɨɞɞɟɪɠɢɜɚɟɦɵɣ ɫɢɧɬɚɤɫɢɫ ɩɨɞɡɚɩɪɨɫɚ:
ERROR 1235 (ER_NOT_SUPPORTED_YET)
SQLSTATE = 42000
Message = "This version of MySQL doesn't yet support
'LIMIT & IN/ALL/ANY/SOME subquery'"
ɗɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɨɩɟɪɚɬɨɪɵ ɫɥɟɞɭɸɳɟɣ ɮɨɪɦɵ ɪɚɛɨɬɚɬɶ ɧɟ ɛɭɞɭɬ, ɯɨɬɹ ɷɬɨ ɢ ɫɥɭɱɚɟɬɫɹ ɬɨɥɶɤɨ ɜ ɪɚɧɧɢɯ ɜɟɪɫɢɹɯ, ɩɨɞɨɛɧɵɯ MySQL 4.1.1:
SELECT * FROM t1
WHERE s1 IN (SELECT s2 FROM t2 ORDER BY s1 LIMIT 1)
„ɇɟɜɟɪɧɨɟ
ɱɢɫɥɨ ɫɬɨɥɛɰɨɜ ɜ ɩɨɞɡɚɩɪɨɫɟ:
ERROR 1241 (ER_OPERAND_COLUMNF)
SQLSTATE = 21000
Message = "Operand should contain 1 column(s)"
ɗɬɚ ɨɲɢɛɤɚ ɜɨɡɧɢɤɚɟɬ ɜ ɫɥɭɱɚɹɯ ɧɚɩɨɞɨɛɢɟ ɫɥɟɞɭɸɳɟɝɨ:
SELECT (SELECT column1, column2 FROM t2) FROM t1;
Ⱦɨɩɭɫɬɢɦɨ ɩɪɢɦɟɧɹɬɶ ɩɨɞɡɚɩɪɨɫɵ, ɤɨɬɨɪɵɟ ɜɨɡɜɪɚɳɚɸɬ ɧɟɫɤɨɥɶɤɨ ɫɬɨɥɛɰɨɜ ɫ
ɰɟɥɶɸ ɫɪɚɜɧɟɧɢɹ. ɋɦ. ɪɚɡɞɟɥ 6.1.8.7. ɇɨ ɜ ɞɪɭɝɢɯ ɤɨɧɬɟɤɫɬɚɯ ɩɨɞɡɚɩɪɨɫ ɞɨɥɠɟɧ
ɛɵɬɶ ɫɤɚɥɹɪɧɵɦ ɨɩɟɪɚɧɞɨɦ.
„ɇɟɜɟɪɧɨɟ ɤɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ ɜ ɩɨɞɡɚɩɪɨɫɟ:
248
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ERROR 1242 (ER_SUBSELECT_NO_1_ROW)
SQLSTATE = 21000
Message = "Subquery returns more than 1 row"
ɗɬɚ ɨɲɢɛɤɚ ɩɪɨɢɫɯɨɞɢɬ ɜ ɨɩɟɪɚɬɨɪɚɯ ɜɪɨɞɟ ɩɪɢɜɟɞɟɧɧɨɝɨ ɧɢɠɟ, ɧɨ ɬɨɥɶɤɨ ɟɫɥɢ ɜ
ɬɚɛɥɢɰɟ t2 ɢɦɟɟɬɫɹ ɛɨɥɟɟ ɨɞɧɨɣ ɫɬɪɨɤɢ:
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
Ɍɚɤɚɹ ɨɲɢɛɤɚ ɦɨɠɟɬ ɜɞɪɭɝ ɨɛɧɚɪɭɠɢɬɶɫɹ ɜ ɤɨɞɟ, ɤɨɬɨɪɵɣ ɪɚɛɨɬɚɥ ɜ ɬɟɱɟɧɢɟ ɧɟɫɤɨɥɶɤɢɯ ɥɟɬ, ɩɨ ɬɨɣ ɩɪɢɱɢɧɟ, ɱɬɨ ɤɬɨ-ɬɨ ɜɧɟɫ ɦɨɞɢɮɢɤɚɰɢɸ, ɢɡɦɟɧɢɜɲɭɸ ɤɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ, ɜɨɡɜɪɚɳɚɟɦɵɯ ɩɨɞɡɚɩɪɨɫɨɦ. ɉɨɦɧɢɬɟ, ɱɬɨ ɟɫɥɢ ɧɭɠɧɨ ɧɚɣɬɢ ɥɸɛɨɟ
ɱɢɫɥɨ ɫɬɪɨɤ, ɚ ɧɟ ɬɨɥɶɤɨ ɨɞɧɭ, ɬɨ ɩɪɚɜɢɥɶɧɚɹ ɮɨɪɦɚ ɡɚɩɪɨɫɚ ɛɭɞɟɬ ɜɵɝɥɹɞɟɬɶ ɬɚɤ:
SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2);
„ɇɟɩɪɚɜɢɥɶɧɨ
ɢɫɩɨɥɶɡɭɟɬɫɹ ɬɚɛɥɢɰɚ ɜ ɩɨɞɡɚɩɪɨɫɟ:
Error 1093 (ER_UPDATE_TABLE_USED)
SQLSTATE = HY000
Message = "You can't specify target table 'x'
for update in FROM clause"
Ɍɚɤɚɹ ɨɲɢɛɤɚ ɩɪɨɢɫɯɨɞɢɬ ɜ ɫɥɭɱɚɟ ɡɚɩɪɨɫɚ, ɩɨɞɨɛɧɨɝɨ ɩɪɟɞɫɬɚɜɥɟɧɧɨɦɭ ɧɢɠɟ:
UPDATE t1 SET column2 = (SELECT MAX(column1) FROM t1);
ȼɩɨɥɧɟ ɤɨɪɪɟɤɬɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɩɨɞɡɚɩɪɨɫ ɞɥɹ ɩɪɢɫɜɨɟɧɢɹ ɜ ɨɩɟɪɚɬɨɪɟ UPDATE,
ɩɨɫɤɨɥɶɤɭ ɩɨɞɡɚɩɪɨɫɵ ɪɚɡɪɟɲɟɧɵ ɜ ɨɩɟɪɚɬɨɪɚɯ UPDATE ɢ DELETE, ɪɚɜɧɨ ɤɚɤ ɢ ɜ
ɨɩɟɪɚɬɨɪɟ SELECT. Ɉɞɧɚɤɨ ɜɵ ɧɟ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɨɞɧɭ ɢ ɬɭ ɠɟ ɬɚɛɥɢɰɭ – ɜ
ɞɚɧɧɨɦ ɫɥɭɱɚɟ t1 – ɢ ɜ ɤɨɧɫɬɪɭɤɰɢɢ FROM ɩɨɞɡɚɩɪɨɫɚ, ɢ ɜ ɤɚɱɟɫɬɜɟ ɨɛɴɟɤɬɚ ɞɥɹ
ɦɚɧɢɩɭɥɹɰɢɢ UPDATE.
Ɉɛɵɱɧɨ ɫɛɨɣ ɩɨɞɡɚɩɪɨɫɚ ɩɪɢɜɨɞɢɬ ɤ ɫɛɨɸ ɜɫɟɝɨ ɜɧɟɲɧɟɝɨ ɨɩɟɪɚɬɨɪɚ.
6.1.8.10. Îïòèìèçàöèÿ ïîäçàïðîñîâ
Ʉɨɝɞɚ ɢɞɟɬ ɪɚɡɪɚɛɨɬɤɚ, ɬɨ ɞɨɥɝɨɟ ɜɪɟɦɹ ɧɢɤɚɤɢɯ ɩɨɞɫɤɚɡɨɤ ɨɩɬɢɦɢɡɚɰɢɢ ɡɚɩɪɨɫɨɜ ɧɟ
ɩɪɢɦɟɧɹɟɬɫɹ. ɇɨ ɜɵ ɦɨɠɟɬɟ ɩɨɩɪɨɛɨɜɚɬɶ ɧɟɤɨɬɨɪɵɟ ɢɧɬɟɪɟɫɧɵɟ ɬɪɸɤɢ:
„ɂɫɩɨɥɶɡɨɜɚɬɶ ɤɨɧɫɬɪɭɤɰɢɢ ɩɨɞɡɚɩɪɨɫɨɜ, ɤɨɬɨɪɵɟ ɤɚɫɚɸɬɫɹ ɤɨɥɢɱɟɫɬɜɚ ɢɥɢ ɩɨɪɹɞɤɚ ɫɬɪɨɤ ɜ ɩɨɞɡɚɩɪɨɫɟ, ɧɚɩɪɢɦɟɪ:
ELECT * FROM t1 WHERE t1.column1 IN
(SELECT column1 FROM t2 ORDER BY column1);
SELECT * FROM t1 WHERE t1.column1 IN
(SELECT DISTINCT column1 FROM t2);
SELECT * FROM t1 WHERE EXISTS
(SELECT * FROM t2 LIMIT 1);
„Ɂɚɦɟɧɢɬɶ
ɫɨɟɞɢɧɟɧɢɟ ɩɨɞɡɚɩɪɨɫɨɦ. ɇɚɩɪɢɦɟɪ, ɢɫɩɨɥɶɡɭɣɬɟ ɬɚɤɨɣ ɡɚɩɪɨɫ:
SELECT DISTINCT column1 FROM t1 WHERE t1.column1 IN (
SELECT column1 FROM t2);
ɜɦɟɫɬɨ ɬɚɤɨɝɨ:
SELECT DISTINCT t1.column1 FROM t1, t2
WHERE t1.column1 = t2.column1;
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
249
„ɉɟɪɟɦɟɫɬɢɬɶ
ɤɨɧɫɬɪɭɤɰɢɢ ɢɡ ɜɧɟɲɧɟɝɨ ɡɚɩɪɨɫɚ ɜ ɩɨɞɡɚɩɪɨɫ. ɇɚɩɪɢɦɟɪ, ɢɫɩɨɥɶɡɭɣɬɟ ɬɚɤɨɣ ɡɚɩɪɨɫ:
SELECT * FROM t1
WHERE s1 IN (SELECT s1 FROM t1 UNION ALL SELECT s1 FROM t2);
ɜɦɟɫɬɨ ɬɚɤɨɝɨ:
SELECT * FROM t1
WHERE s1 IN (SELECT s1 FROM t1) OR s1 IN (SELECT s1 FROM t2);
ɂ ɞɪɭɝɨɣ ɩɪɢɦɟɪ; ɢɫɩɨɥɶɡɭɣɬɟ ɫɥɟɞɭɸɳɢɣ ɡɚɩɪɨɫ:
SELECT (SELECT column1 + 5 FROM t1) FROM t2;
ɜɦɟɫɬɨ ɬɚɤɨɝɨ:
SELECT (SELECT column1 FROM t1) + 5 FROM t2;
„ɉɪɢɦɟɧɹɬɶ
ɫɬɪɨɤɨɜɵɣ ɩɨɞɡɚɩɪɨɫ ɜɦɟɫɬɨ ɤɨɪɪɟɥɢɪɨɜɚɧɧɨɝɨ, ɧɚɩɪɢɦɟɪ, ɬɚɤɨɣ:
SELECT * FROM t1
WHERE (column1,column2) IN (SELECT column1,column2 FROM t2);
ɜɦɟɫɬɨ ɬɚɤɨɝɨ:
SELECT * FROM t1
WHERE EXISTS (SELECT * FROM t2 WHERE t2.column1=t1.column1
AND t2.column2=t1.column2);
ɜɦɟɫɬɨ a <> ALL (...).
ɜɦɟɫɬɨ x=1 OR x=2.
„ɂɫɩɨɥɶɡɨɜɚɬɶ = ANY ɜɦɟɫɬɨ EXISTS.
ɗɬɢ ɬɪɸɤɢ ɦɨɝɭɬ ɡɚɫɬɚɜɢɬɶ ɩɪɨɝɪɚɦɦɵ ɪɚɛɨɬɚɬɶ ɛɵɫɬɪɟɟ ɢɥɢ ɦɟɞɥɟɧɧɟɟ. ɂɫɩɨɥɶɡɭɹ
ɫɪɟɞɫɬɜɚ MySQL, ɩɨɞɨɛɧɵɟ ɮɭɧɤɰɢɢ BENCHMARK(), ɜɵ ɦɨɠɟɬɟ ɩɨɥɭɱɢɬɶ ɩɪɟɞɫɬɚɜɥɟɧɢɟ ɨ
ɬɨɦ, ɱɬɨ ɦɨɠɟɬ ɩɨɦɨɱɶ ɜ ɜɚɲɟɣ ɤɨɧɤɪɟɬɧɨɣ ɫɢɬɭɚɰɢɢ. ɇɟ ɛɟɫɩɨɤɨɣɬɟɫɶ ɫɥɢɲɤɨɦ ɨ
ɬɪɚɧɫɮɨɪɦɚɰɢɢ ɩɨɞɡɚɩɪɨɫɨɜ ɜ ɫɨɟɞɢɧɟɧɢɹ, ɟɫɥɢ ɬɨɥɶɤɨ ɜɚɦ ɧɟ ɧɭɠɧɨ ɨɛɟɫɩɟɱɢɬɶ ɫɨɜɦɟɫɬɢɦɨɫɬɶ ɫ ɜɟɪɫɢɹɦɢ MySQL ɞɨ 4.1, ɤɨɬɨɪɵɟ ɩɨɞɡɚɩɪɨɫɵ ɧɟ ɩɨɞɞɟɪɠɢɜɚɥɢ.
ɇɢɠɟ ɩɪɟɞɫɬɚɜɥɟɧɵ ɧɟɤɨɬɨɪɵɟ ɦɟɪɵ ɩɨ ɨɩɬɢɦɢɡɚɰɢɢ, ɤɨɬɨɪɵɟ ɩɪɢɧɢɦɚɟɬ ɫɚɦ
MySQL.
„MySQL ɜɵɩɨɥɧɹɟɬ ɧɟɤɨɪɪɟɥɢɪɨɜɚɧɧɵɟ ɩɨɞɡɚɩɪɨɫɵ ɬɨɥɶɤɨ ɨɞɢɧ ɪɚɡ. ɂɫɩɨɥɶɡɭɣɬɟ
EXPLAIN, ɱɬɨɛɵ ɭɛɟɞɢɬɶɫɹ, ɱɬɨ ɜɚɲ ɡɚɩɪɨɫ ɧɟɤɨɪɪɟɥɢɪɨɜɚɧɧɵɣ.
„ɉɪɢɦɟɧɹɬɶ NOT (a = ANY (...))
„ɉɪɢɦɟɧɹɬɶ x = ANY(ɬɚɛɥɢɰɚ_ɫɨɞɟɪɠɚɳɚɹ(1,2))
„MySQL
ɩɟɪɟɡɚɩɢɫɵɜɚɟɬ ɩɨɞɡɚɩɪɨɫɵ IN/ALL/ANY/SOME, ɩɵɬɚɹɫɶ ɩɨɥɭɱɢɬɶ ɜɵɢɝɪɵɲ ɨɬ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɢɧɞɟɤɫɨɜ.
„MySQL ɡɚɦɟɧɹɟɬ ɩɨɞɡɚɩɪɨɫɵ ɫɥɟɞɭɸɳɟɣ ɮɨɪɦɵ ɮɭɧɤɰɢɹɦɢ ɩɪɨɫɦɨɬɪɚ ɢɧɞɟɤɫɨɜ,
ɤɨɬɨɪɵɟ ɤɨɧɫɬɪɭɤɰɢɹ EXPLAIN ɛɭɞɟɬ ɨɩɢɫɵɜɚɬɶ ɤɚɤ ɫɩɟɰɢɚɥɶɧɵɣ ɬɢɩ ɫɨɟɞɢɧɟɧɢɹ:
... IN (SELECT ɢɧɞɟɤɫɢɪɨɜɚɧɧɵɣ_ɫɬɨɥɛɟɰ FROM ɨɞɢɧɨɱɧɚɹ_ɬɚɛɥɢɰɚ ...)
„MySQL
ɡɚɦɟɧɹɟɬ ɜɵɪɚɠɟɧɢɹ ɫɥɟɞɭɸɳɟɣ ɮɨɪɦɵ ɜɵɪɚɠɟɧɢɹɦɢ ɫ ɩɪɢɦɟɧɟɧɢɟɦ
MIN() ɢɥɢ MAX(), ɟɫɥɢ ɬɨɥɶɤɨ ɧɟ ɜɨɜɥɟɱɟɧɵ NULL-ɡɧɚɱɟɧɢɹ ɢ ɩɭɫɬɵɟ ɧɚɛɨɪɵ:
ɡɧɚɱɟɧɢɟ {ALL|ANY|SOME} {> | < | >= | <=} (ɧɟ-ɤɨɪɪɟɥɢɪɨɜɚɧɧɵɣ-ɩɨɞɡɚɩɪɨɫ)
ɧɚɩɪɢɦɟɪ, ɫɥɟɞɭɸɳɚɹ ɤɨɧɫɬɪɭɤɰɢɹ WHERE:
WHERE 5 > ALL (SELECT x FROM t)
ɦɨɠɟɬ ɛɵɬɶ ɩɪɟɨɛɪɚɡɨɜɚɧɚ ɨɩɬɢɦɢɡɚɬɨɪɨɦ ɜ ɬɚɤɭɸ:
WHERE 5 > (SELECT MAX(x) FROM t)
250
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ȼ ɪɭɤɨɜɨɞɫɬɜɟ ɩɨ ɜɧɭɬɪɟɧɧɟɦɭ ɭɫɬɪɨɣɫɬɜɭ MySQL (“MySQL Internals Manual”) ɢɦɟɟɬɫɹ ɝɥɚɜɚ “How MySQL Transforms Subqueries” (“Ʉɚɤ MySQL ɬɪɚɧɫɮɨɪɦɢɪɭɟɬ ɩɨɞɡɚɩɪɨɫɵ”). ȼɵ ɦɨɠɟɬɟ ɩɨɥɭɱɢɬɶ ɷɬɨɬ ɞɨɤɭɦɟɧɬ, ɜɵɝɪɭɡɢɜ ɢɫɯɨɞɧɵɣ ɞɢɫɬɪɢɛɭɬɢɜ MySQL ɢ
ɧɚɣɞɹ ɮɚɣɥ internals.text ɜ ɤɚɬɚɥɨɝɟ Docs.
6.1.8.11. Çàìåíà ïîäçàïðîñîâ ñîåäèíåíèÿìè äëÿ ðàííèõ âåðñèé MySQL
Ⱦɨ MySQL 4.1 ɩɨɞɞɟɪɠɢɜɚɥɢɫɶ ɬɨɥɶɤɨ ɜɥɨɠɟɧɧɵɟ ɡɚɩɪɨɫɵ ɞɥɹ ɮɨɪɦ INSERT...
SELECT... ɢ REPLACE...SELECT... Ʉɨɧɫɬɪɭɤɰɢɹ IN() ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɜ ɞɪɭɝɢɯ
ɤɨɧɬɟɤɫɬɚɯ ɞɥɹ ɩɪɨɜɟɪɤɢ ɜɯɨɠɞɟɧɢɹ ɜ ɦɧɨɠɟɫɬɜɨ ɡɧɚɱɟɧɢɣ.
ɑɚɫɬɨ ɦɨɠɧɨ ɩɟɪɟɩɢɫɚɬɶ ɡɚɩɪɨɫ ɛɟɡ ɩɨɞɡɚɩɪɨɫɚ:
SELECT * FROM t1 WHERE id IN (SELECT id FROM t2);
ɗɬɨ ɦɨɠɧɨ ɡɚɦɟɧɢɬɶ ɧɚ:
SELECT DISTINCT t1.* FROM t1,t2 WHERE t1.id=t2.id;
ɋɥɟɞɭɸɳɢɟ ɡɚɩɪɨɫɵ:
SELECT * FROM t1 WHERE id NOT IN (SELECT id FROM t2);
SELECT * FROM t1 WHERE NOT EXISTS (SELECT id FROM t2 WHERE t1.id=t2.id);
ɦɨɝɭɬ ɛɵɬɶ ɩɟɪɟɩɢɫɚɧɵ ɬɚɤ:
SELECT table1.* FROM table1 LEFT JOIN table2 ON table1.id=table2.id
WHERE table2.id IS NULL;
LEFT [OUTER] JOIN ɦɨɠɟɬ ɨɤɚɡɚɬɶɫɹ ɛɵɫɬɪɟɟ, ɱɟɦ ɷɤɜɢɜɚɥɟɧɬɧɵɣ ɩɨɞɡɚɩɪɨɫ, ɩɨɫɤɨɥɶɤɭ ɫɟɪɜɟɪ ɦɨɠɟɬ ɟɝɨ ɥɭɱɲɟ ɨɩɬɢɦɢɡɢɪɨɜɚɬɶ, ɢ ɷɬɨ ɤɚɫɚɟɬɫɹ ɧɟ ɬɨɥɶɤɨ ɨɞɧɨɝɨ
MySQL. Ⱦɨ ɜɵɯɨɞɚ ɫɬɚɧɞɚɪɬɚ SQL-92 ɜɧɟɲɧɢɟ ɫɨɟɞɢɧɟɧɢɹ ɧɟ ɞɨɩɭɫɤɚɥɢɫɶ, ɩɨɷɬɨɦɭ
ɩɨɞɡɚɩɪɨɫɵ ɛɵɥɢ ɟɞɢɧɫɬɜɟɧɧɵɦ ɫɩɨɫɨɛɨɦ ɞɟɥɚɬɶ ɨɩɪɟɞɟɥɟɧɧɵɟ ɜɟɳɢ ɜ ɷɬɢ ɩɪɨɲɟɞɲɢɟ
ɜɪɟɦɟɧɚ. ɋɟɝɨɞɧɹ ɫɟɪɜɟɪ MySQL ɢ ɦɧɨɝɢɟ ɞɪɭɝɢɟ ɫɨɜɪɟɦɟɧɧɵɟ ɫɢɫɬɟɦɵ ɛɚɡ ɞɚɧɧɵɯ
ɩɪɟɞɥɚɝɚɸɬ ɲɢɪɨɤɢɣ ɞɢɚɩɚɡɨɧ ɬɢɩɨɜ ɜɧɟɲɧɢɯ ɫɨɟɞɢɧɟɧɢɣ.
Ⱦɥɹ ɛɨɥɟɟ ɫɥɨɠɧɵɯ ɩɨɞɡɚɩɪɨɫɨɜ ɱɚɫɬɨ ɦɨɠɧɨ ɫɨɡɞɚɜɚɬɶ ɜɪɟɦɟɧɧɵɟ ɬɚɛɥɢɰɵ, ɜ ɤɨɬɨɪɵɯ ɯɪɚɧɹɬɫɹ ɩɪɨɦɟɠɭɬɨɱɧɵɟ ɪɟɡɭɥɶɬɚɬɵ. Ɉɞɧɚɤɨ ɜ ɧɟɤɨɬɨɪɵɯ ɫɥɭɱɚɹɯ ɬɚɤɨɣ ɫɩɨɫɨɛ
ɧɟɩɪɢɟɦɥɟɦ. ɇɚɢɛɨɥɟɟ ɱɚɫɬɨ ɷɬɨ ɫɥɭɱɚɟɬɫɹ ɫ ɨɩɟɪɚɬɨɪɚɦɢ DELETE, ɞɥɹ ɤɨɬɨɪɵɯ ɫɬɚɧɞɚɪɬ
SQL ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬ ɫɨɟɞɢɧɟɧɢɣ (ɤɪɨɦɟ ɤɚɤ ɜ ɩɨɞɡɚɩɪɨɫɚɯ). ȼ ɷɬɨɣ ɫɢɬɭɚɰɢɢ ɜɨɡɦɨɠɧɵ
ɬɪɢ ɩɭɬɢ:
„ɉɟɪɜɵɣ ɩɭɬɶ – ɨɛɧɨɜɢɬɶ ɫɟɪɜɟɪ ɞɨ ɜɟɪɫɢɢ MySQL 4.1, ɤɨɬɨɪɚɹ ɩɨɞɞɟɪɠɢɜɚɟɬ ɩɨɞɡɚɩɪɨɫɵ ɜ ɨɩɟɪɚɬɨɪɚɯ DELETE.
„ȼɬɨɪɨɣ ɩɭɬɶ – ɢɫɩɨɥɶɡɨɜɚɬɶ ɩɪɨɰɟɞɭɪɧɵɣ ɹɡɵɤ ɩɪɨɝɪɚɦɦɢɪɨɜɚɧɢɹ (ɬɚɤɨɣ ɤɚɤ Perl
ɢɥɢ PHP), ɱɬɨɛɵ ɜɵɩɨɥɧɢɬɶ ɡɚɩɪɨɫ SELECT ɞɥɹ ɩɨɥɭɱɟɧɢɹ ɡɧɚɱɟɧɢɣ ɩɟɪɜɢɱɧɵɯ
ɤɥɸɱɟɣ ɡɚɩɢɫɟɣ, ɩɨɞɥɟɠɚɳɢɯ ɭɞɚɥɟɧɢɸ, ɚ ɡɚɬɟɦ ɢɫɩɨɥɶɡɨɜɚɬɶ ɷɬɢ ɡɧɚɱɟɧɢɹ ɞɥɹ
ɩɨɫɬɪɨɟɧɢɹ ɨɩɟɪɚɬɨɪɚ DELETE (DELETE FROM ... WHERE ɤɥɸɱɟɜɨɣ_ɫɬɨɥɛɟɰ IN
(ɤɥɸɱ1,ɤɥɸɱ2,...)).
„Ɍɪɟɬɢɣ ɩɭɬɶ – ɢɫɩɨɥɶɡɨɜɚɬɶ ɢɧɬɟɪɚɤɬɢɜɧɵɣ SQL ɞɥɹ ɚɜɬɨɦɚɬɢɱɟɫɤɨɝɨ ɩɨɫɬɪɨɟɧɢɹ
ɧɚɛɨɪɚ ɨɩɟɪɚɬɨɪɨɜ DELETE ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ MySQL-ɪɚɫɲɢɪɟɧɢɹ CONCAT() (ɜɦɟɫɬɨ ɫɬɚɧɞɚɪɬɧɨɝɨ ɨɩɟɪɚɬɨɪɚ ||). ɇɚɩɪɢɦɟɪ:
SELECT
CONCAT('DELETE FROM table1 WHERE pkid = ', "'", table1.pkid, "'", ';')
FROM table1, table2
WHERE table1.column1 = table2.column2;
6.1. Îïåðàòîðû ìàíèïóëÿöèè äàííûìè
251
ɗɬɨɬ ɡɚɩɪɨɫ ɦɨɠɧɨ ɩɨɦɟɫɬɢɬɶ ɜ ɮɚɣɥ ɫɰɟɧɚɪɢɹ, ɭɤɚɡɚɬɶ ɟɝɨ ɜ ɤɚɱɟɫɬɜɟ ɜɯɨɞɧɨɝɨ
ɞɥɹ ɨɞɧɨɝɨ ɷɤɡɟɦɩɥɹɪɚ ɩɪɨɝɪɚɦɦɵ mysql ɢ ɩɟɪɟɧɚɩɪɚɜɢɬɶ ɟɝɨ ɜɵɯɨɞɧɨɣ ɩɨɬɨɤ ɧɚ
ɜɯɨɞ ɞɪɭɝɨɦɭ ɷɤɡɟɦɩɥɹɪɭ mysql:
shell> mysql --skip-column-names mydb < myscript.sql | mysql mydb
ɋɟɪɜɟɪ MySQL 4.0 ɩɨɞɞɟɪɠɢɜɚɟɬ ɦɧɨɝɨɬɚɛɥɢɱɧɵɟ ɨɩɟɪɚɬɨɪɵ DELETE, ɤɨɬɨɪɵɟ ɦɨɝɭɬ
ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɞɥɹ ɷɮɮɟɤɬɢɜɧɨɝɨ ɭɞɚɥɟɧɢɹ ɫɬɪɨɤ ɧɚ ɨɫɧɨɜɟ ɢɧɮɨɪɦɚɰɢɢ ɢɡ ɨɞɧɨɣ ɢɥɢ
ɞɚɠɟ ɦɧɨɝɢɯ ɬɚɛɥɢɰ ɨɞɧɨɜɪɟɦɟɧɧɨ. Ɇɧɨɝɨɬɚɛɥɢɱɧɵɟ ɨɩɟɪɚɬɨɪɵ UPDATE ɬɚɤɠɟ ɩɨɞɞɟɪɠɢɜɚɸɬɫɹ ɜ MySQL 4.0.
6.1.9. Ñèíòàêñèñ TRUNCATE
TRUNCATE TABLE ɢɦɹ_ɬɚɛɥɢɰɵ
TRUNCATE TABLE ɩɨɥɧɨɫɬɶɸ ɨɱɢɳɚɟɬ ɬɚɛɥɢɰɭ. Ʌɨɝɢɱɟɫɤɢ ɷɬɨ ɷɤɜɢɜɚɥɟɧɬ ɨɩɟɪɚɬɨɪɚ
DELETE, ɤɨɬɨɪɵɣ ɭɞɚɥɹɟɬ ɜɫɟ ɫɬɪɨɤɢ, ɧɨ ɜ ɧɟɤɨɬɨɪɵɯ ɫɥɭɱɚɹɯ ɢɦɟɸɬɫɹ ɨɩɪɟɞɟɥɟɧɧɵɟ
ɨɬɥɢɱɢɹ ɩɪɚɤɬɢɱɟɫɤɨɝɨ ɩɥɚɧɚ.
Ⱦɥɹ InnoDB ɨɩɟɪɚɬɨɪ TRUNCATE TABLE ɨɬɨɛɪɚɠɚɟɬɫɹ ɧɚ DELETE, ɩɨɬɨɦɭ ɡɞɟɫɶ ɪɚɡɧɢɰɵ
ɧɟɬ. Ⱦɥɹ ɞɪɭɝɢɯ ɦɟɯɚɧɢɡɦɨɜ ɯɪɚɧɟɧɢɹ TRUNCATE TABLE ɨɬɥɢɱɚɟɬɫɹ ɨɬ DELETE FROM... ɜ
MySQL 4.0 ɢ ɜɵɲɟ:
„Ɉɩɟɪɚɰɢɢ ɭɫɟɱɟɧɢɹ (truncate) ɭɞɚɥɹɸɬ ɢ ɩɟɪɟɫɨɡɞɚɸɬ ɬɚɛɥɢɰɵ, ɱɬɨ ɡɧɚɱɢɬɟɥɶɧɨ
ɛɵɫɬɪɟɟ, ɱɟɦ ɭɞɚɥɹɬɶ ɫɬɪɨɤɢ ɨɞɧɭ ɡɚ ɞɪɭɝɨɣ.
„Ɉɩɟɪɚɰɢɢ ɭɫɟɱɟɧɢɹ ɧɟ ɹɜɥɹɸɬɫɹ ɛɟɡɨɩɚɫɧɵɦɢ ɜ ɨɬɧɨɲɟɧɢɢ ɬɪɚɧɡɚɤɰɢɣ; ɜɵ ɩɨɥɭɱɢɬɟ ɨɲɢɛɤɭ, ɟɫɥɢ ɧɚ ɫɟɪɜɟɪɟ ɫɭɳɟɫɬɜɭɸɬ ɚɤɬɢɜɧɵɟ ɬɪɚɧɡɚɤɰɢɢ ɢɥɢ ɚɤɬɢɜɧɵɟ
ɛɥɨɤɢɪɨɜɤɢ ɬɚɛɥɢɰ.
„Ʉɨɥɢɱɟɫɬɜɨ ɭɞɚɥɟɧɧɵɯ ɫɬɪɨɤ ɧɟ ɜɨɡɜɪɚɳɚɟɬɫɹ.
„Ⱦɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɮɚɣɥ ɨɩɪɟɞɟɥɟɧɢɹ ɬɚɛɥɢɰɵ ɢɦɹ_ɬɚɛɥɢɰɵ.frm ɩɪɚɜɢɥɶɧɵɣ, ɬɚɛɥɢɰɚ ɦɨɠɟɬ ɛɵɬɶ ɫɨɡɞɚɧɚ ɩɨɜɬɨɪɧɨ ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ TRUNCATE TABLE ɤɚɤ ɩɭɫɬɚɹ, ɞɚɠɟ ɟɫɥɢ ɮɚɣɥ ɞɚɧɧɵɯ ɢɥɢ ɢɧɞɟɤɫɧɵɟ ɮɚɣɥɵ ɩɨɜɪɟɠɞɟɧɵ.
„Ⱦɟɫɤɪɢɩɬɨɪ ɬɚɛɥɢɰɵ ɧɟ ɡɚɩɨɦɢɧɚɟɬ ɩɨɫɥɟɞɧɟɝɨ ɢɫɩɨɥɶɡɨɜɚɧɧɨɝɨ ɡɧɚɱɟɧɢɹ
AUTO_INCREMENT, ɚ ɧɚɱɢɧɚɟɬ ɨɬɫɱɟɬ ɫɧɚɱɚɥɚ. ɗɬɨ ɜɟɪɧɨ ɞɚɠɟ ɞɥɹ ɦɟɯɚɧɢɡɦɚ
MyISAM, ɤɨɬɨɪɵɣ ɨɛɵɱɧɨ ɧɟ ɢɫɩɨɥɶɡɭɟɬ ɩɨɜɬɨɪɧɨ ɡɧɚɱɟɧɢɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ.
ȼ MySQL 3.23 ɨɩɟɪɚɬɨɪ TRUNCATE TABLE ɨɬɨɛɪɚɠɚɟɬɫɹ ɧɚ COMMIT; DELETE FROM
ɢɦɹ_ɬɚɛɥɢɰɵ, ɬɨ ɟɫɬɶ ɜɟɞɟɬ ɫɟɛɹ ɩɨɞɨɛɧɨ DELETE. ɋɦ. ɪɚɡɞɟɥ 6.1.1.
TRUNCATE TABLE – ɷɬɨ SQL-ɨɩɟɪɚɬɨɪ Oracle. Ɉɧ ɛɵɥ ɞɨɛɚɜɥɟɧ ɜ MySQL 3.23.28, ɯɨɬɹ
ɜ ɜɟɪɫɢɹɯ ɨɬ 3.23.28 ɞɨ 3.23.32 ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ TABLE ɦɨɠɟɬ ɩɪɨɩɭɫɤɚɬɶɫɹ.
6.1.10. Ñèíòàêñèñ UPDATE
Ɉɞɧɨɬɚɛɥɢɱɧɵɣ ɫɢɧɬɚɤɫɢɫ:
UPDATE [LOW_PRIORITY] [IGNORE] ɢɦɹ_ɬɚɛɥɢɰɵ
SET ɢɦɹ_ɫɬɨɥɛɰɚ1=ɜɵɪɚɠɟɧɢɟ1 [,ɢɦɹ_ɫɬɨɥɛɰɚ2=ɜɵɪɚɠɟɧɢɟ2 ...]
[WHERE ɨɩɪɟɞɟɥɟɧɢɟ_where]
[ORDER BY ...]
[LIMIT ɤɨɥɢɱɟɫɬɜɨ_ɫɬɪɨɤ]
Ɇɧɨɝɨɬɚɛɥɢɱɧɵɣ ɫɢɧɬɚɤɫɢɫ:
252
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
UPDATE [LOW_PRIORITY] [IGNORE] ɢɦɹ_ɬɚɛɥɢɰɵ [, ɢɦɹ_ɬɚɛɥɢɰɵ ...]
SET ɢɦɹ_ɫɬɨɥɛɰɚ1=ɜɵɪɚɠɟɧɢɟ1 [,ɢɦɹ_ɫɬɨɥɛɰɚ2=ɜɵɪɚɠɟɧɢɟ2 ...]
[WHERE ɨɩɪɟɞɟɥɟɧɢɟ_where]
Ɉɩɟɪɚɬɨɪ UPDATE ɨɛɧɨɜɥɹɟɬ ɫɬɨɥɛɰɵ ɫɭɳɟɫɬɜɭɸɳɢɯ ɫɬɪɨɤ ɬɚɛɥɢɰɵ ɧɨɜɵɦɢ ɡɧɚɱɟɧɢɹɦɢ. Ʉɨɧɫɬɪɭɤɰɢɹ SET ɩɟɪɟɱɢɫɥɹɟɬ ɫɬɨɥɛɰɵ, ɩɨɞɥɟɠɚɳɢɟ ɦɨɞɢɮɢɤɚɰɢɢ, ɢ ɡɧɚɱɟɧɢɹ,
ɤɨɬɨɪɵɟ ɢɦ ɩɪɢɫɜɚɢɜɚɸɬɫɹ. ȿɫɥɢ ɭɤɚɡɚɧɚ ɤɨɧɫɬɪɭɤɰɢɹ WHERE, ɨɧɚ ɡɚɞɚɟɬ, ɤɚɤɢɟ ɫɬɪɨɤɢ
ɞɨɥɠɧɵ ɛɵɬɶ ɨɛɧɨɜɥɟɧɵ. ȼ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ ɨɛɧɨɜɥɹɸɬɫɹ ɜɫɟ ɫɬɪɨɤɢ ɬɚɛɥɢɰɵ. ȿɫɥɢ
ɭɤɚɡɚɧɚ ɤɨɧɫɬɪɭɤɰɢɹ ORDER BY, ɫɬɪɨɤɢ ɛɭɞɭɬ ɨɛɧɨɜɥɟɧɵ ɜ ɡɚɞɚɧɧɨɦ ɩɨɪɹɞɤɟ. Ʉɨɧɫɬɪɭɤɰɢɹ LIMIT ɧɚɤɥɚɞɵɜɚɟɬ ɨɝɪɚɧɢɱɟɧɢɟ ɧɚ ɤɨɥɢɱɟɫɬɜɨ ɨɛɧɨɜɥɹɟɦɵɯ ɫɬɪɨɤ.
Ɉɩɟɪɚɬɨɪ UPDATE ɩɨɞɞɟɪɠɢɜɚɟɬ ɫɥɟɞɭɸɳɢɟ ɦɨɞɢɮɢɤɚɬɨɪɵ:
ɭɤɚɡɚɧɨ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ LOW_PRIORITY, ɜɵɩɨɥɧɟɧɢɟ UPDATE ɨɬɤɥɚɞɵɜɚɟɬɫɹ ɞɨ
ɬɟɯ ɩɨɪ, ɩɨɤɚ ɜɫɟ ɞɪɭɝɢɟ ɤɥɢɟɧɬɵ ɡɚɜɟɪɲɚɬ ɱɬɟɧɢɟ ɬɚɛɥɢɰɵ.
„ȿɫɥɢ ɭɤɚɡɚɧɨ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ IGNORE, ɨɩɟɪɚɰɢɹ ɨɛɧɨɜɥɟɧɢɹ ɧɟ ɛɭɞɟɬ ɩɪɟɪɜɚɧɚ,
ɞɚɠɟ ɟɫɥɢ ɜɨɡɧɢɤɧɭɬ ɨɲɢɛɤɢ ɞɭɛɥɢɪɨɜɚɧɢɹ ɤɥɸɱɚ. ɋɬɪɨɤɢ, ɤɨɬɨɪɵɟ ɩɪɢɜɨɞɹɬ ɤ
ɤɨɧɮɥɢɤɬɚɦ, ɨɛɧɨɜɥɟɧɵ ɧɟ ɛɭɞɭɬ.
ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ɫɬɨɥɛɰɵ ɢɡ ɬɚɛɥɢɰɵ ɢɦɹ_ɬɚɛɥɢɰɵ ɜ ɜɵɪɚɠɟɧɢɹɯ, UPDATE ɢɫɩɨɥɶɡɭɟɬ ɬɟɤɭɳɟɟ ɡɧɚɱɟɧɢɟ ɫɬɨɥɛɰɨɜ. ɇɚɩɪɢɦɟɪ, ɫɥɟɞɭɸɳɢɣ ɨɩɟɪɚɬɨɪ ɭɜɟɥɢɱɢɜɚɟɬ ɡɧɚɱɟɧɢɟ
ɫɬɨɥɛɰɚ age ɧɚ ɟɞɢɧɢɰɭ:
„ȿɫɥɢ
mysql> UPDATE persondata SET age=age+1;
ɉɪɢɫɜɨɟɧɢɹ ɜ UPDATE ɜɵɩɨɥɧɹɸɬɫɹ ɫɥɟɜɚ ɧɚɩɪɚɜɨ. ɇɚɩɪɢɦɟɪ, ɫɥɟɞɭɸɳɢɣ ɨɩɟɪɚɬɨɪ
ɭɞɜɚɢɜɚɟɬ ɡɧɚɱɟɧɢɟ ɫɬɨɥɛɰɚ age, ɚ ɡɚɬɟɦ ɭɜɟɥɢɱɢɜɚɟɬ ɧɚ ɟɞɢɧɢɰɭ:
mysql> UPDATE persondata SET age=age*2, age=age+1;
ȿɫɥɢ ɜɵ ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɡɧɚɱɟɧɢɟ ɫɬɨɥɛɰɚ ɜ ɬɨ, ɤɨɬɨɪɨɟ ɨɧ ɢɦɟɟɬ, MySQL ɨɛɧɚɪɭɠɢɜɚɟɬ ɷɬɨ ɢ ɧɟ ɜɵɩɨɥɧɹɟɬ ɨɛɧɨɜɥɟɧɢɟ.
ȿɫɥɢ ɜɵ ɨɛɧɨɜɥɹɟɬɟ ɫɬɨɥɛɟɰ, ɤɨɬɨɪɵɣ ɛɵɥɚ ɨɛɴɹɜɥɟɧ ɤɚɤ NOT NULL, ɩɪɢɫɜɚɢɜɚɹ ɟɦɭ
ɡɧɚɱɟɧɢɟ NULL, ɨɧ ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɜ ɡɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɟ ɤɨɧɤɪɟɬɧɨɦɭ ɬɢɩɭ ɞɚɧɧɵɯ ɢ ɭɜɟɥɢɱɢɜɚɟɬ ɫɱɟɬɱɢɤ ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ ɧɚ ɟɞɢɧɢɰɭ. Ɂɧɚɱɟɧɢɟ ɩɨ
ɭɦɨɥɱɚɧɢɸ ɪɚɜɧɨ 0 ɞɥɹ ɱɢɫɥɨɜɵɯ ɫɬɨɥɛɰɨɜ, ɩɭɫɬɚɹ ɫɬɪɨɤɚ ('') ɞɥɹ ɫɢɦɜɨɥɶɧɵɯ ɢ “ɧɭɥɟɜɨɟ” ɡɧɚɱɟɧɢɟ ɞɥɹ ɫɬɨɥɛɰɨɜ ɬɢɩɚ ɞɚɬɵ ɢ ɜɪɟɦɟɧɢ.
UPDATE ɜɨɡɜɪɚɳɚɟɬ ɤɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ, ɤɨɬɨɪɵɟ ɮɚɤɬɢɱɟɫɤɢ ɛɵɥɢ ɨɛɧɨɜɥɟɧɵ. ȼ
MySQL 3.22 ɢ ɛɨɥɟɟ ɩɨɡɞɧɢɯ ɜɟɪɫɢɹɯ ɮɭɧɤɰɢɹ mysql_info() ɩɪɨɝɪɚɦɦɧɨɝɨ ɢɧɬɟɪɮɟɣɫɚ
C API ɜɨɡɜɪɚɳɚɟɬ ɤɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ, ɤɨɬɨɪɵɟ ɫɨɨɬɜɟɬɫɬɜɨɜɚɥɢ ɡɚɩɪɨɫɭ ɢ ɛɵɥɢ ɨɛɧɨɜɥɟɧɵ, ɚ ɬɚɤɠɟ ɤɨɥɢɱɟɫɬɜɨ ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ, ɜɨɡɧɢɤɲɢɯ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ UPDATE.
ɇɚɱɢɧɚɹ ɫ MySQL 3.23, ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ LIMIT ɤɨɥɢɱɟɫɬɜɨ_ɫɬɪɨɤ ɞɥɹ ɨɝɪɚɧɢɱɟɧɢɹ ɨɛɥɚɫɬɢ ɞɟɣɫɬɜɢɹ UPDATE.
Ʉɨɧɫɬɪɭɤɰɢɹ LIMIT ɪɚɛɨɬɚɟɬ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
MySQL 4.0.13 LIMIT ɛɵɥɚ ɨɝɪɚɧɢɱɟɧɢɟɦ ɤɨɥɢɱɟɫɬɜɚ ɨɛɪɚɛɨɬɚɧɧɵɯ ɫɬɪɨɤ.
Ɉɩɟɪɚɬɨɪ ɡɚɜɟɪɲɚɥ ɪɚɛɨɬɭ, ɤɚɤ ɬɨɥɶɤɨ ɨɛɧɨɜɥɹɥ ɤɨɥɢɱɟɫɬɜɨ_ɫɬɪɨɤ ɫɬɪɨɤ, ɭɞɨɜɥɟɬɜɨɪɹɜɲɢɯ ɭɫɥɨɜɢɸ WHERE.
„Ⱦɨ
ɫ MySQL 4.0.13, LIMIT – ɨɝɪɚɧɢɱɟɧɢɟ ɫɨɨɬɜɟɬɫɬɜɢɹ ɫɬɪɨɤ. Ɉɩɟɪɚɬɨɪ ɡɚɜɟɪɲɚɟɬ ɪɚɛɨɬɭ, ɤɚɤ ɬɨɥɶɤɨ ɧɚɣɞɟɬ ɤɨɥɢɱɟɫɬɜɨ_ɫɬɪɨɤ ɫɬɪɨɤ, ɭɞɨɜɥɟɬɜɨɪɹɸɳɢɯ
ɭɫɥɨɜɢɸ WHERE, ɧɟɡɚɜɢɫɢɦɨ ɨɬ ɬɨɝɨ, ɛɵɥɢ ɥɢ ɨɧɢ ɞɟɣɫɬɜɢɬɟɥɶɧɨ ɨɛɧɨɜɥɟɧɵ.
„ɇɚɱɢɧɚɹ
6.2. Îïåðàòîðû îïðåäåëåíèÿ äàííûõ
253
ȿɫɥɢ ɨɩɟɪɚɬɨɪ UPDATE ɜɤɥɸɱɚɟɬ ɤɨɧɫɬɪɭɤɰɢɸ ORDER BY, ɬɨ ɫɬɪɨɤɢ ɨɛɧɨɜɥɹɸɬɫɹ ɜ ɩɨɪɹɞɤɟ, ɡɚɞɚɧɧɨɦ ɷɬɨɣ ɤɨɧɫɬɪɭɤɰɢɟɣ. ORDER BY ɦɨɠɟɬ ɩɪɢɦɟɧɹɬɶɫɹ, ɧɚɱɢɧɚɹ ɫ MySQL
4.0.0.
ɇɚɱɢɧɚɹ ɫ MySQL 4.0.0, ɬɚɤɠɟ ɦɨɠɧɨ ɜɵɩɨɥɧɹɬɶ ɨɩɟɪɚɰɢɢ UPDATE, ɤɨɬɨɪɵɟ ɪɚɛɨɬɚɸɬ
ɫ ɧɟɫɤɨɥɶɤɢɦɢ ɬɚɛɥɢɰɚɦɢ ɫɪɚɡɭ:
UPDATE items,month SET items.price=month.price
WHERE items.id=month.id;
ɗɬɨɬ ɩɪɢɦɟɪ ɞɟɦɨɧɫɬɪɢɪɭɟɬ ɜɧɭɬɪɟɧɧɟɟ ɨɛɴɟɞɢɧɟɧɢɟ, ɢɫɩɨɥɶɡɭɸɳɟɟ ɨɩɟɪɚɬɨɪ ɡɚɩɹɬɨɣ, ɧɨ ɦɧɨɝɨɬɚɛɥɢɱɧɵɟ UPDATE ɦɨɝɭɬ ɢɫɩɨɥɶɡɨɜɚɬɶ ɥɸɛɨɣ ɬɢɩ ɨɛɴɟɞɢɧɟɧɢɣ, ɞɨɩɭɫɬɢɦɵɣ ɜ ɨɩɟɪɚɬɨɪɟ SELECT, ɧɚɩɪɢɦɟɪ, LEFT JOIN.
oË ÑËÕÏÛµÂ!
ȼɵ ɧɟ ɦɨɠɟɬɟ ɩɪɢɦɟɧɹɬɶ ORDER BY ɢɥɢ LIMIT ɜ ɦɧɨɝɨɬɚɛɥɢɱɧɵɯ ɨɩɟɪɚɬɨɪɚɯ UPDATE.
Ⱦɨ ɜɟɪɫɢɢ MySQL 4.0.18 ɧɟɨɛɯɨɞɢɦɨ ɛɵɥɨ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ UPDATE ɞɥɹ ɜɫɟɯ ɬɚɛɥɢɰ, ɢɫɩɨɥɶɡɭɟɦɵɯ ɜ ɦɧɨɝɨɬɚɛɥɢɱɧɨɦ UPDATE, ɞɚɠɟ ɟɫɥɢ ɨɧɢ ɮɚɤɬɢɱɟɫɤɢ ɧɟ ɨɛɧɨɜɥɹɥɢɫɶ. ɇɚɱɢɧɚɹ ɫ MySQL 4.0.18, ɞɥɹ ɬɚɤɢɯ ɬɚɛɥɢɰ, ɱɶɢ ɫɬɨɥɛɰɵ ɬɨɥɶɤɨ ɱɢɬɚɸɬɫɹ, ɧɨ ɧɟ
ɨɛɧɨɜɥɹɸɬɫɹ, ɧɟɨɛɯɨɞɢɦɨ ɢɦɟɬɶ ɬɨɥɶɤɨ ɩɪɢɜɢɥɟɝɢɸ SELECT.
ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ɦɧɨɝɨɬɚɛɥɢɱɧɵɣ ɨɩɟɪɚɬɨɪ UPDATE ɜ ɨɬɧɨɲɟɧɢɢ ɬɚɛɥɢɰ InnoDB, ɭ
ɤɨɬɨɪɵɯ ɨɩɪɟɞɟɥɟɧɵ ɨɝɪɚɧɢɱɟɧɢɹ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ, ɨɩɬɢɦɢɡɚɬɨɪ MySQL ɦɨɠɟɬ ɨɛɪɚɛɚɬɵɜɚɬɶ ɢɯ ɜ ɩɨɪɹɞɤɟ, ɨɬɥɢɱɧɨɦ ɨɬ ɬɨɝɨ, ɤɨɬɨɪɵɣ ɡɚɞɚɟɬɫɹ ɢɯ ɨɬɧɨɲɟɧɢɹɦɢ “ɪɨɞɢɬɟɥɶɫɤɢɣ-ɞɨɱɟɪɧɢɣ”. ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɨɩɟɪɚɬɨɪ ɡɚɜɟɪɲɢɬɫɹ ɨɲɢɛɤɨɣ ɢ ɛɭɞɟɬ ɜɵɩɨɥɧɟɧ ɨɬɤɚɬ
ɬɪɚɧɡɚɤɰɢɢ. ȼɦɟɫɬɨ ɷɬɨɝɨ ɨɛɧɨɜɥɹɣɬɟ ɨɞɧɭ ɬɚɛɥɢɰɭ ɢ ɩɨɥɚɝɚɣɬɟɫɶ ɧɚ ɫɜɨɣɫɬɜɨ ON
UPDATE, ɤɨɬɨɪɨɟ ɩɪɟɞɨɫɬɚɜɥɹɟɬ ɦɟɯɚɧɢɡɦ InnoDB ɞɥɹ ɚɜɬɨɦɚɬɢɱɟɫɤɨɝɨ ɨɛɧɨɜɥɟɧɢɹ ɫɜɹɡɚɧɧɵɯ ɬɚɛɥɢɰ.
6.2. Îïåðàòîðû îïðåäåëåíèÿ äàííûõ
6.2.1. Ñèíòàêñèñ ALTER DATABASE
ALTER DATABASE ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ
ɫɩɟɰɢɮɢɤɚɰɢɹ_alter [, ɫɩɟɰɢɮɢɤɚɰɢɹ_alter] ...
ɫɩɟɰɢɮɢɤɚɰɢɹ_alter:
[DEFAULT] CHARACTER SET ɢɦɹ_ɧɚɛɨɪɚ_ɫɢɦɜɨɥɨɜ
| [DEFAULT] COLLATE ɢɦɹ_ɩɨɪɹɞɤɚ_ɫɨɩɨɫɬɚɜɥɟɧɢɹ
ALTER DATABASE ɩɨɡɜɨɥɹɟɬ ɢɡɦɟɧɹɬɶ ɨɛɳɢɟ ɯɚɪɚɤɬɟɪɢɫɬɢɤɢ ɛɚɡɵ ɞɚɧɧɵɯ. ɗɬɢ ɯɚɪɚɤɬɟɪɢɫɬɢɤɢ ɯɪɚɧɹɬɫɹ ɜ ɮɚɣɥɟ db.opt, ɧɚɯɨɞɹɳɟɦɫɹ ɜ ɤɚɬɚɥɨɝɟ ɞɚɧɧɵɯ. ɑɬɨɛɵ ɢɫɩɨɥɶɡɨɜɚɬɶ ALTER DATABASE, ɧɟɨɛɯɨɞɢɦɨ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ ALTER ɞɥɹ ɷɬɨɣ ɛɚɡɵ ɞɚɧɧɵɯ.
Ʉɨɧɫɬɪɭɤɰɢɹ CHARACTER SET ɢɡɦɟɧɹɟɬ ɧɚɛɨɪ ɫɢɦɜɨɥɨɜ ɩɨ ɭɦɨɥɱɚɧɢɸ ɞɥɹ ɞɚɧɧɨɣ ɛɚɡɵ ɞɚɧɧɵɯ. Ʉɨɧɫɬɪɭɤɰɢɹ COLLATE ɢɡɦɟɧɹɟɬ ɩɨɪɹɞɨɤ ɫɨɩɨɫɬɚɜɥɟɧɢɹ, ɢɫɩɨɥɶɡɭɟɦɵɣ ɩɨ
ɭɦɨɥɱɚɧɢɸ ɜ ɛɚɡɟ ɞɚɧɧɵɯ. ɇɚɛɨɪɵ ɫɢɦɜɨɥɨɜ ɢ ɩɨɪɹɞɤɢ ɫɨɩɨɫɬɚɜɥɟɧɢɹ ɨɛɫɭɠɞɚɸɬɫɹ ɜ
ɝɥɚɜɟ 3.
ALTER DATABASE ɩɨɹɜɢɥɫɹ ɜ ɜɟɪɫɢɢ MySQL 4.1.1.
6.2.2. Ñèíòàêñèñ ALTER TABLE
ALTER [IGNORE] TABLE ɢɦɹ_ɬɚɛɥɢɰɵ
ɫɩɟɰɢɮɢɤɚɰɢɹ_alter [, ɫɩɟɰɢɮɢɤɚɰɢɹ_alter] ...
254
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɫɩɟɰɢɮɢɤɚɰɢɹ_alter:
ADD [COLUMN] ɨɩɪɟɞɟɥɟɧɢɟ_ɫɬɨɥɛɰɚ [FIRST | AFTER ɢɦɹ_ɫɬɨɥɛɰɚ]
| ADD [COLUMN] (ɨɩɪɟɞɟɥɟɧɢɟ_ɫɬɨɥɛɰɚ,...)
| ADD INDEX [ɢɦɹ_ɢɧɞɟɤɫɚ] [ɬɢɩ_ɢɧɞɟɤɫɚ] (ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...)
| ADD [CONSTRAINT [ɫɢɦɜɨɥ]]
PRIMARY KEY [ɬɢɩ_ɢɧɞɟɤɫɚ] (ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...)
| ADD [CONSTRAINT [ɫɢɦɜɨɥ]]
UNIQUE [ɢɦɹ_ɢɧɞɟɤɫɚ] [ɬɢɩ_ɢɧɞɟɤɫɚ] (ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...)
| ADD [FULLTEXT|SPATIAL] [ɢɦɹ_ɢɧɞɟɤɫɚ] (ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...)
| ADD [CONSTRAINT [ɫɢɦɜɨɥ]]
FOREIGN KEY [ɢɦɹ_ɢɧɞɟɤɫɚ] (ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...)
[ɨɩɪɟɞɟɥɟɧɢɟ_ɫɫɵɥɤɢ]
| ALTER [COLUMN] ɢɦɹ_ɫɬɨɥɛɰɚ {SET DEFAULT ɥɢɬɟɪɚɥ | DROP DEFAULT}
| CHANGE [COLUMN] ɫɬɚɪɨɟ_ɢɦɹ_ɫɬɨɥɛɰɚ ɨɩɪɟɞɟɥɟɧɢɟ_ɫɬɨɥɛɰɚ
[FIRST|AFTER ɢɦɹ_ɫɬɨɥɛɰɚ]
| MODIFY [COLUMN] ɨɩɪɟɞɟɥɟɧɢɟ_ɫɬɨɥɛɰɚ [FIRST | AFTER ɢɦɹ_ɫɬɨɥɛɰɚ]
| DROP [COLUMN] ɢɦɹ_ɫɬɨɥɛɰɚ
| DROP PRIMARY KEY
| DROP INDEX ɢɦɹ_ɢɧɞɟɤɫɚ
| DROP FOREIGN KEY ɫɢɦɜɨɥ_fk
| DISABLE KEYS
| ENABLE KEYS
| RENAME [TO] ɧɨɜɨɟ_ɢɦɹ_ɬɚɛɥɢɰɵ
| ORDER BY ɢɦɹ_ɫɬɨɥɛɰɚ
| CONVERT TO CHARACTER SET ɢɦɹ_ɧɚɛɨɪɚ_ɫɢɦɜɨɥɨɜ
[COLLATE ɢɦɹ_ɩɨɪɹɞɤɚ_ɫɨɩɨɫɬɚɜɥɟɧɢɹ]
| [DEFAULT] CHARACTER SET ɢɦɹ_ɧɚɛɨɪɚ_ɫɢɦɜɨɥɨɜ
[COLLATE ɢɦɹ_ɩɨɪɹɞɤɚ_ɫɨɩɨɫɬɚɜɥɟɧɢɹ]
| DISCARD TABLESPACE
| IMPORT TABLESPACE
| ɨɩɰɢɢ_ɬɚɛɥɢɰɵ
ALTER TABLE ɩɨɡɜɨɥɹɟɬ ɢɡɦɟɧɢɬɶ ɫɬɪɭɤɬɭɪɭ ɫɭɳɟɫɬɜɭɸɳɟɣ ɬɚɛɥɢɰɵ. ɇɚɩɪɢɦɟɪ,
ɦɨɠɧɨ ɞɨɛɚɜɥɹɬɶ ɢɥɢ ɭɞɚɥɹɬɶ ɫɬɨɥɛɰɵ, ɫɨɡɞɚɜɚɬɶ ɢɥɢ ɭɧɢɱɬɨɠɚɬɶ ɢɧɞɟɤɫɵ, ɢɡɦɟɧɹɬɶ
ɬɢɩ ɫɭɳɟɫɬɜɭɸɳɢɯ ɫɬɨɥɛɰɨɜ ɢɥɢ ɩɟɪɟɢɦɟɧɨɜɵɜɚɬɶ ɫɬɨɥɛɰɵ ɢ ɫɚɦɢ ɬɚɛɥɢɰɵ. Ɇɨɠɧɨ
ɬɚɤɠɟ ɢɡɦɟɧɹɬɶ ɤɨɦɦɟɧɬɚɪɢɢ ɞɥɹ ɬɚɛɥɢɰ ɢ ɢɯ ɬɢɩ.
ɋɢɧɬɚɤɫɢɫ ɦɧɨɝɢɯ ɞɨɩɭɫɬɢɦɵɯ ɢɡɦɟɧɟɧɢɣ ɜ ɷɬɨɦ ɨɩɟɪɚɬɨɪɟ ɩɨɯɨɠ ɧɚ ɚɧɚɥɨɝɢɱɧɵɟ
ɤɨɧɫɬɪɭɤɰɢɢ ɨɩɟɪɚɬɨɪɚ CREATE TABLE. ɋɦ. ɪɚɡɞɟɥ 6.2.5.
ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɨɜɚɥɢ ALTER TABLE ɞɥɹ ɢɡɦɟɧɟɧɢɹ ɫɩɟɰɢɮɢɤɚɰɢɣ ɫɬɨɥɛɰɨɜ, ɧɨ
DESCRIBE ɢɦɹ_ɬɚɛɥɢɰɵ ɩɨɤɚɡɵɜɚɟɬ, ɱɬɨ ɜɚɲɢ ɫɬɨɥɛɰɵ ɧɟ ɢɡɦɟɧɢɥɢɫɶ, ɜɨɡɦɨɠɧɨ, MySQL
ɩɪɨɢɝɧɨɪɢɪɨɜɚɥ ɜɚɲɢ ɢɡɦɟɧɟɧɢɹ ɩɨ ɨɞɧɨɣ ɢɡ ɩɪɢɱɢɧ, ɨɩɢɫɚɧɧɵɯ ɜ ɪɚɡɞɟɥɟ 6.2.5.2. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɜɵ ɩɵɬɚɟɬɟɫɶ ɢɡɦɟɧɢɬɶ ɬɢɩ ɫɬɨɥɛɰɚ VARCHAR ɧɚ CHAR, MySQL ɛɭɞɟɬ ɩɨɩɪɟɠɧɟɦɭ ɢɫɩɨɥɶɡɨɜɚɬɶ VARCHAR, ɟɫɥɢ ɬɚɛɥɢɰɚ ɫɨɞɟɪɠɢɬ ɞɪɭɝɢɟ ɫɬɨɥɛɰɵ ɩɟɪɟɦɟɧɧɨɣ
ɞɥɢɧɵ.
ALTER TABLE ɪɚɛɨɬɚɟɬ, ɫɨɡɞɚɜɚɹ ɜɪɟɦɟɧɧɭɸ ɤɨɩɢɸ ɨɪɢɝɢɧɚɥɶɧɨɣ ɬɚɛɥɢɰɵ. ɂɡɦɟɧɟɧɢɹ ɩɪɨɢɡɜɨɞɢɬɫɹ ɧɚɞ ɤɨɩɢɟɣ, ɡɚɬɟɦ ɢɫɯɨɞɧɚɹ ɬɚɛɥɢɰɚ ɭɞɚɥɹɟɬɫɹ, ɚ ɧɨɜɚɹ ɩɟɪɟɢɦɟɧɨɜɵɜɚɟɬɫɹ. ȼ ɩɪɨɰɟɫɫɟ ɪɚɛɨɬɵ ALTER TABLE ɢɫɯɨɞɧɚɹ ɬɚɛɥɢɰɚ ɨɫɬɚɟɬɫɹ ɞɨɫɬɭɩɧɨɣ ɩɨ ɱɬɟɧɢɸ
ɞɪɭɝɢɦ ɤɥɢɟɧɬɚɦ. Ɉɛɧɨɜɥɟɧɢɹ ɢ ɡɚɩɢɫɶ ɜ ɷɬɭ ɬɚɛɥɢɰɭ ɩɪɢɨɫɬɚɧɚɜɥɢɜɚɸɬɫɹ ɞɨ ɬɟɯ ɩɨɪ,
ɩɨɤɚ ɧɨɜɚɹ ɬɚɛɥɢɰɚ ɧɟ ɛɭɞɟɬ ɝɨɬɨɜɚ, ɡɚɬɟɦ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɩɟɪɟɧɚɩɪɚɜɥɹɸɬɫɹ ɧɚ ɧɨɜɭɸ
ɬɚɛɥɢɰɭ, ɛɟɡ ɤɚɤɢɯ-ɥɢɛɨ ɩɨɬɟɪɶ.
6.2. Îïåðàòîðû îïðåäåëåíèÿ äàííûõ
255
ɉɨɦɧɢɬɟ, ɱɬɨ ɟɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ɥɸɛɵɟ ɨɩɰɢɢ ALTER TABLE ɡɚ ɢɫɤɥɸɱɟɧɢɟɦ
RENAME, MySQL ɜɫɟɝɞɚ ɫɨɡɞɚɟɬ ɜɪɟɦɟɧɧɭɸ ɬɚɛɥɢɰɭ, ɞɚɠɟ ɟɫɥɢ ɧɟɬ ɫɬɪɨɝɨɣ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɜ ɤɨɩɢɪɨɜɚɧɢɢ ɞɚɧɧɵɯ (ɧɚɩɪɢɦɟɪ, ɤɨɝɞɚ ɜɵ ɜɫɟɝɨ ɥɢɲɶ ɩɟɪɟɢɦɟɧɨɜɵɜɚɟɬɟ ɫɬɨɥɛɟɰ).
Ɇɵ ɩɥɚɧɢɪɭɟɦ ɢɫɩɪɚɜɢɬɶ ɷɬɨ ɜ ɛɭɞɭɳɟɦ, ɧɨ ɩɨɫɤɨɥɶɤɭ ALTER TABLE – ɨɩɟɪɚɬɨɪ, ɢɫɩɨɥɶɡɭɟɦɵɣ ɧɟɱɚɫɬɨ, ɷɬɚ ɡɚɞɚɱɚ ɧɟ ɢɦɟɟɬ ɜɵɫɨɤɨɝɨ ɩɪɢɨɪɢɬɟɬɚ ɜ ɧɚɲɢɯ ɩɥɚɧɚɯ. Ⱦɥɹ ɬɚɛɥɢɰ
MyISAM ɜɵ ɦɨɠɟɬɟ ɭɫɤɨɪɢɬɶ ɩɪɨɰɟɞɭɪɭ ɩɟɪɟɫɨɡɞɚɧɢɹ ɢɧɞɟɤɫɨɜ (ɱɬɨ ɹɜɥɹɟɬɫɹ ɫɚɦɨɣ ɦɟɞɥɟɧɧɨɣ ɱɚɫɬɶɸ ɜ ɩɪɨɰɟɫɫɟ ɜɵɩɨɥɧɟɧɢɹ ɷɬɨɝɨ ɨɩɟɪɚɬɨɪɚ), ɩɪɢɫɜɨɢɜ ɫɢɫɬɟɦɧɨɣ ɩɟɪɟɦɟɧɧɨɣ myisam_sort_buffer_size ɛɨɥɶɲɟɟ ɡɧɚɱɟɧɢɟ.
„Ⱦɥɹ
ɢɫɩɨɥɶɡɨɜɚɧɢɹ ALTER TABLE ɧɟɨɛɯɨɞɢɦɨ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɢ ALTER, INSERT ɢ
CREATE ɞɥɹ ɬɚɛɥɢɰɵ.
„IGNORE
– ɷɬɨ ɪɚɫɲɢɪɟɧɢɟ MySQL ɫɬɚɧɞɚɪɬɚ SQL. ɗɬɨ ɫɥɨɜɨ ɭɩɪɚɜɥɹɟɬ ɬɟɦ, ɤɚɤ
ALTER TABLE ɪɚɛɨɬɚɟɬ ɜ ɫɥɭɱɚɟ ɞɭɛɥɢɪɨɜɚɧɢɹ ɭɧɢɤɚɥɶɧɵɯ ɤɥɸɱɟɣ ɜ ɧɨɜɨɣ ɬɚɛɥɢɰɟ.
ȿɫɥɢ IGNORE ɧɟ ɭɤɚɡɚɧɨ, ɤɨɩɢɪɨɜɚɧɢɟ ɩɪɟɪɵɜɚɟɬɫɹ ɢ ɜɵɩɨɥɧɹɟɬɫɹ ɨɬɤɚɬ ɩɪɢ ɨɛɧɚɪɭɠɟɧɢɢ ɨɲɢɛɤɢ ɞɭɛɥɢɪɨɜɚɧɢɹ ɤɥɸɱɟɣ. ȿɫɥɢ ɠɟ IGNORE ɭɤɚɡɚɧɨ, ɬɨ ɢɡ ɫɬɪɨɤ, ɞɭɛ-
ɥɢɪɭɸɳɢɯ ɭɧɢɤɚɥɶɧɵɣ ɤɥɸɱ, ɢɫɩɨɥɶɡɭɟɬɫɹ ɬɨɥɶɤɨ ɩɟɪɜɚɹ. Ɉɫɬɚɥɶɧɵɟ ɭɞɚɥɹɸɬɫɹ.
„ȼɵ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɦɧɨɠɟɫɬɜɨ ɤɨɧɫɬɪɭɤɰɢɣ ADD, ALTER, DROP ɢ CHANGE ɜ ɩɪɟɞɟɥɚɯ ɨɞɧɨɝɨ ɨɩɟɪɚɬɨɪɚ ALTER TABLE. ɗɬɨ ɬɚɤɠɟ MySQL-ɪɚɫɲɢɪɟɧɢɟ ɫɬɚɧɞɚɪɬɚ
SQL, ɜ ɤɨɬɨɪɨɦ ɪɚɡɪɟɲɚɟɬɫɹ ɬɨɥɶɤɨ ɩɨ ɨɞɧɨɦɭ ɬɚɤɨɦɭ ɩɪɟɞɥɨɠɟɧɢɸ ɜ ɨɩɟɪɚɬɨɪɟ
ALTER TABLE.
„CHANGE ɢɦɹ_ɫɬɨɥɛɰɚ, DROP ɢɦɹ_ɫɬɨɥɛɰɚ
ɢ DROP INDEX – ɷɬɨ MySQL-ɪɚɫɲɢɪɟɧɢɹ
ɫɬɚɧɞɚɪɬɚ SQL.
„MODIFY – ɷɬɨ ɪɚɫɲɢɪɟɧɢɟ ALTER TABLE ɨɬ Oracle.
„ɋɥɨɜɨ COLUMN
– ɫɨɜɟɪɲɟɧɧɨ ɧɟɨɛɹɡɚɬɟɥɶɧɨɟ ɢ ɦɨɠɟɬ ɛɵɬɶ ɨɩɭɳɟɧɨ.
ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ALTER TABLE ɢɦɹ_ɬɚɛɥɢɰɵ RENAME TO ɧɨɜɨɟ_ɢɦɹ_ɬɚɛɥɢɰɵ
ɛɟɡ ɤɚɤɢɯ-ɥɢɛɨ ɨɩɰɢɣ, MySQL ɩɪɨɫɬɨ ɩɟɪɟɢɦɟɧɨɜɵɜɚɟɬ ɜɫɟ ɮɚɣɥɵ, ɢɦɟɸɳɢɟ ɨɬɧɨɲɟɧɢɟ ɤ ɬɚɛɥɢɰɟ ɢɦɹ_ɬɚɛɥɢɰɵ. ɇɟɬ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɫɨɡɞɚɜɚɬɶ ɜɪɟɦɟɧɧɭɸ ɬɚɛɥɢɰɭ. (ȼɵ ɬɚɤɠɟ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɨɩɟɪɚɬɨɪ RENAME TABLE ɞɥɹ ɩɟɪɟɢɦɟɧɨɜɚɧɢɹ
ɬɚɛɥɢɰ. ɋɦ. ɪɚɡɞɟɥ 6.2.9.)
„Ʉɨɧɫɬɪɭɤɰɢɢ ɨɩɪɟɞɟɥɟɧɢɟ_ɫɬɨɥɛɰɚ ɢɫɩɨɥɶɡɭɸɬ ɬɨɬ ɠɟ ɫɢɧɬɚɤɫɢɫ ɞɥɹ ADD ɢ
CHANGE, ɱɬɨ ɢ CREATE TABLE. ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɫɢɧɬɚɤɫɢɫ ɜɤɥɸɱɚɟɬ ɢɦɹ
ɫɬɨɥɛɰɚ, ɚ ɧɟ ɬɨɥɶɤɨ ɟɝɨ ɬɢɩ. ɋɦ. ɪɚɡɞɟɥ 6.2.5.
„ȼɵ ɦɨɠɟɬɟ ɩɟɪɟɢɦɟɧɨɜɵɜɚɬɶ ɫɬɨɥɛɰɵ, ɢɫɩɨɥɶɡɭɹ ɤɨɧɫɬɪɭɤɰɢɸ CHANGE ɫɬɚɪɨɟ_ɢɦɹ_ɫɬɨɥɛɰɚ ɨɩɪɟɞɟɥɟɧɢɟ_ɫɬɨɥɛɰɚ. ɑɬɨɛɵ ɫɞɟɥɚɬɶ ɷɬɨ, ɭɤɚɠɢɬɟ ɫɬɚɪɵɟ ɢ ɧɨɜɵɟ ɢɦɟɧɚ, ɚ ɬɚɤɠɟ ɬɢɩɵ ɫɬɨɥɛɰɨɜ. ɇɚɩɪɢɦɟɪ, ɱɬɨɛɵ ɩɟɪɟɢɦɟɧɨɜɚɬɶ ɫɬɨɥɛɟɰ
INTEGER ɢɡ a ɜ b, ɦɨɠɧɨ ɩɨɫɬɭɩɢɬɶ ɬɚɤ:
„ȿɫɥɢ
mysql> ALTER TABLE t1 CHANGE a b INTEGER;
ȿɫɥɢ ɜɵ ɯɨɬɢɬɟ ɢɡɦɟɧɢɬɶ ɬɨɥɶɤɨ ɬɢɩ ɫɬɨɥɛɰɚ, ɧɟ ɦɟɧɹɹ ɢɦɟɧɢ, ɫɢɧɬɚɤɫɢɫ CHANGE
ɜɫɟ ɪɚɜɧɨ ɬɪɟɛɭɟɬ ɭɤɚɡɚɧɢɹ ɫɬɚɪɨɝɨ ɢ ɧɨɜɨɝɨ ɢɦɟɧɢ ɫɬɨɥɛɰɚ, ɞɚɠɟ ɟɫɥɢ ɷɬɢ ɢɦɟɧɚ
ɨɞɢɧɚɤɨɜɵ. ɇɚɩɪɢɦɟɪ:
mysql> ALTER TABLE t1 CHANGE b b BIGINT NOT NULL;
Ɉɞɧɚɤɨ, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 3.22.16a, ɜɵ ɬɚɤɠɟ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ MODIFY
ɞɥɹ ɢɡɦɟɧɟɧɢɹ ɬɢɩɚ ɫɬɨɥɛɰɚ ɛɟɡ ɟɝɨ ɩɟɪɟɢɦɟɧɨɜɚɧɢɹ:
mysql> ALTER TABLE t1 MODIFY b BIGINT NOT NULL;
256
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ CHANGE ɢɥɢ MODIFY, ɱɬɨɛɵ ɭɤɨɪɨɬɢɬɶ ɫɬɨɥɛɟɰ, ɤɨɬɨɪɵɣ ɢɫɩɨɥɶɡɭɟɬɫɹ ɜ ɤɥɸɱɟ ɢɧɞɟɤɫɚ ɱɚɫɬɢɱɧɨ (ɧɚɩɪɢɦɟɪ, ɟɫɥɢ ɢɧɞɟɤɫ ɩɨɫɬɪɨɟɧ ɩɨ ɩɟɪɜɵɦ
10 ɫɢɦɜɨɥɚɦ ɫɬɨɥɛɰɚ VARCHAR), ɜɵ ɧɟ ɦɨɠɟɬɟ ɫɞɟɥɚɬɶ ɫɬɨɥɛɟɰ ɤɨɪɨɱɟ, ɱɟɦ ɤɨɥɢɱɟɫɬɜɨ ɫɢɦɜɨɥɨɜ, ɩɨ ɤɨɬɨɪɵɦ ɩɨɫɬɪɨɟɧ ɢɧɞɟɤɫ.
„ȼ MySQL 3.22 ɢ ɛɨɥɟɟ ɩɨɡɞɧɢɯ ɜɟɪɫɢɹɯ ɜɵ ɦɨɠɟɬɟ ɜɨɫɩɨɥɶɡɨɜɚɬɶɫɹ FIRST ɢɥɢ AFTER
ɢɦɹ_ɫɬɨɥɛɰɚ, ɱɬɨɛɵ ɞɨɛɚɜɢɬɶ ɧɨɜɵɣ ɫɬɨɥɛɟɰ ɜ ɭɤɚɡɚɧɧɨɟ ɦɟɫɬɨ ɫɬɪɨɤɢ ɬɚɛɥɢɰɵ. ɉɨ
ɭɦɨɥɱɚɧɢɸ ɫɬɨɥɛɰɵ ɞɨɛɚɜɥɹɸɬɫɹ ɜ ɤɨɧɟɰ ɫɬɪɭɤɬɭɪɵ. ɇɚɱɢɧɚɹ ɫ MySQL 4.0.1,
FIRST ɢɥɢ AFTER ɢɦɹ_ɫɬɨɥɛɰɚ ɦɨɠɧɨ ɬɚɤɠɟ ɩɪɢɦɟɧɹɬɶ ɜ ɨɩɟɪɚɰɢɹɯ CHANGE ɢ MODIFY.
„ȿɫɥɢ
„ALTER COLUMN
ɭɤɚɡɵɜɚɟɬ ɧɨɜɨɟ ɡɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ ɞɥɹ ɫɬɨɥɛɰɵ ɢɥɢ ɭɞɚɥɹɟɬ
ɫɬɚɪɨɟ. ȿɫɥɢ ɫɬɚɪɨɟ ɡɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ ɭɞɚɥɟɧɨ, ɢ ɫɬɨɥɛɟɰ ɦɨɠɟɬ ɩɪɢɧɢɦɚɬɶ
ɡɧɚɱɟɧɢɟ NULL, ɬɨ ɡɧɚɱɟɧɢɟɦ ɩɨ ɭɦɨɥɱɚɧɢɸ ɫɬɚɧɨɜɢɬɫɹ NULL. ȿɫɥɢ ɫɬɨɥɛɟɰ ɧɟ ɦɨɠɟɬ ɩɪɢɧɢɦɚɬɶ ɡɧɚɱɟɧɢɟ NULL, MySQL ɩɪɢɫɜɚɢɜɚɟɬ ɟɦɭ ɡɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ ɜ
ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɟɝɨ ɬɢɩɚ, ɤɚɤ ɨɩɢɫɚɧɨ ɜ ɪɚɡɞɟɥɟ 6.2.5.
„DROP INDEX ɭɞɚɥɹɟɬ ɢɧɞɟɤɫ. ɗɬɨ ɪɚɫɲɢɪɟɧɢɟ ɫɬɚɧɞɚɪɬɚ SQL ɨɬ MySQL. ɋɦ. ɪɚɡɞɟɥ 6.2.7.
„ȿɫɥɢ ɫɬɨɥɛɟɰ ɭɞɚɥɹɟɬɫɹ ɢɡ ɫɬɪɭɤɬɭɪɵ ɬɚɛɥɢɰɵ, ɫɬɨɥɛɟɰ ɬɚɤɠɟ ɭɞɚɥɹɸɬɫɹ ɢɡ ɜɫɟɯ
ɢɧɞɟɤɫɨɜ, ɱɚɫɬɶɸ ɤɥɸɱɚ ɤɨɬɨɪɵɯ ɨɧ ɛɵɥɚ. ȿɫɥɢ ɜɫɟ ɫɬɨɥɛɰɵ, ɫɨɫɬɚɜɥɹɸɳɢɟ ɤɥɸɱ
ɢɧɞɟɤɫɚ, ɭɞɚɥɟɧɵ, ɢɧɞɟɤɫ ɬɚɤɠɟ ɭɞɚɥɹɟɬɫɹ.
„ȿɫɥɢ ɬɚɛɥɢɰɚ ɫɨɞɟɪɠɢɬ ɬɨɥɶɤɨ ɨɞɢɧ ɫɬɨɥɛɟɰ, ɫɬɨɥɛɟɰ ɧɟ ɦɨɠɟɬ ɛɵɬɶ ɭɞɚɥɟɧ. ȿɫɥɢ
ɜɵ ɩɪɢ ɷɬɨɦ ɢɦɟɟɬɟ ɜ ɜɢɞɭ ɭɞɚɥɟɧɢɟ ɬɚɛɥɢɰɵ, ɢɫɩɨɥɶɡɭɣɬɟ ɜɦɟɫɬɨ ɷɬɨɝɨ DROP
TABLE.
ɭɧɢɱɬɨɠɚɟɬ ɢɧɞɟɤɫ ɩɟɪɜɢɱɧɨɝɨ ɤɥɸɱɚ. (Ⱦɨ ɜɟɪɫɢɢ MySQL
4.1.2, ɟɫɥɢ ɬɚɛɥɢɰɚ ɧɟ ɢɦɟɥɚ ɩɟɪɜɢɱɧɨɝɨ ɤɥɸɱɚ, ɨɩɟɪɚɬɨɪ DROP PRIMARY KEY ɭɧɢɱɬɨɠɚɥ ɩɟɪɜɵɣ ɭɧɢɤɚɥɶɧɵɣ ɢɧɞɟɤɫ ɬɚɛɥɢɰɵ. MySQL ɩɨɦɟɱɚɟɬ ɩɟɪɜɵɣ ɭɧɢɤɚɥɶɧɵɣ
ɤɥɸɱ ɤɚɤ ɩɟɪɜɢɱɧɵɣ (PRIMARY KEY), ɟɫɥɢ ɬɚɤɨɜɨɣ ɧɟ ɫɩɟɰɢɮɢɰɢɪɨɜɚɧ ɹɜɧɨ. ȿɫɥɢ
ɜɵ ɞɨɛɚɜɥɹɟɬɟ ɬɚɛɥɢɰɟ UNIQUE INDEX ɢɥɢ PRIMARY KEY, ɨɧ ɫɨɯɪɚɧɹɟɬɫɹ ɩɟɪɟɞ ɥɸɛɵɦ ɧɟɭɧɢɤɚɥɶɧɵɦ ɢɧɞɟɤɫɨɦ, ɱɬɨɛɵ MySQL ɦɨɝ ɨɛɧɚɪɭɠɢɜɚɬɶ ɫɥɭɱɚɢ ɞɭɛɥɢɪɨɜɚɧɢɹ ɤɥɸɱɚ ɤɚɤ ɦɨɠɧɨ ɪɚɧɶɲɟ.)
„ORDER BY ɩɨɡɜɨɥɹɟɬ ɫɨɡɞɚɜɚɬɶ ɧɨɜɭɸ ɬɚɛɥɢɰɭ ɫɨ ɫɬɪɨɤɚɦɢ ɜ ɨɩɪɟɞɟɥɟɧɧɨɦ ɩɨɪɹɞɤɟ.
ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɫɬɪɨɤ ɢɡɦɟɧɢɬɫɹ ɩɨɫɥɟ ɜɵɩɨɥɧɟɧɢɹ
ɜɫɬɚɜɨɤ ɢ ɭɞɚɥɟɧɢɣ. ɗɬɚ ɨɩɰɢɹ ɜ ɨɫɧɨɜɧɨɦ ɩɪɢɦɟɧɢɦɚ, ɤɨɝɞɚ ɜɵ ɡɧɚɟɬɟ, ɱɬɨ ɡɚɩɪɨɫɵ
ɛɭɞɭɬ ɜɵɩɨɥɧɹɬɶɫɹ ɜ ɨɫɧɨɜɧɨɦ ɬɚɤ, ɱɬɨɛɵ ɢɡɜɥɟɤɚɬɶ ɫɬɪɨɤɢ ɜ ɨɩɪɟɞɟɥɟɧɧɨɦ ɩɨɪɹɞɤɟ. ɂɫɩɨɥɶɡɭɹ ɷɬɭ ɨɩɰɢɸ ɩɨɫɥɟ ɫɭɳɟɫɬɜɟɧɧɵɯ ɢɡɦɟɧɟɧɢɣ ɜ ɬɚɛɥɢɰɟ, ɜɵ ɦɨɠɟɬɟ ɞɨɛɢɬɶɫɹ ɭɜɟɥɢɱɟɧɢɹ ɩɪɨɢɡɜɨɞɢɬɟɥɶɧɨɫɬɢ. ȼ ɧɟɤɨɬɨɪɵɯ ɫɥɭɱɚɹɯ ɦɨɠɧɨ ɨɛɥɟɝɱɢɬɶ
MySQL ɡɚɞɚɱɭ ɫɨɪɬɢɪɨɜɤɢ, ɟɫɥɢ ɞɚɧɧɵɟ ɜ ɬɚɛɥɢɰɟ ɛɭɞɭɬ ɪɚɫɩɨɥɨɠɟɧɵ ɜ ɨɩɪɟɞɟɥɟɧɧɨɦ ɩɨɪɹɞɤɟ ɩɨ ɡɧɚɱɟɧɢɸ ɫɬɨɥɛɰɚ, ɜ ɤɨɬɨɪɨɦ ɩɨɡɠɟ ɢɯ ɩɪɢɞɟɬɫɹ ɢɡɜɥɟɤɚɬɶ.
„ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ALTER TABLE ɞɥɹ ɬɚɛɥɢɰɵ MyISAM, ɜɫɟ ɧɟɭɧɢɤɚɥɶɧɵɟ ɢɧɞɟɤɫɵ
ɫɨɡɞɚɸɬɫɹ ɜ ɨɬɞɟɥɶɧɨɦ ɩɚɤɟɬɟ (ɤɚɤ ɩɪɢ ɜɵɩɨɥɧɟɧɢɢ REPAIR TABLE). ɗɬɨ ɦɨɠɟɬ
ɡɧɚɱɢɬɟɥɶɧɨ ɭɫɤɨɪɢɬɶ ɜɵɩɨɥɧɟɧɢɟ ALTER TABLE, ɟɫɥɢ ɢɧɞɟɤɫɨɜ ɦɧɨɝɨ.
ɇɚɱɢɧɚɹ ɫ MySQL 4.0, ɷɬɨ ɫɪɟɞɫɬɜɨ ɦɨɠɟɬ ɛɵɬɶ ɚɤɬɢɜɢɡɢɪɨɜɚɧɨ ɹɜɧɨ. ALTER
TABLE...DISABLE KEYS ɫɨɨɛɳɚɟɬ MySQL, ɱɬɨ ɧɭɠɧɨ ɩɪɟɤɪɚɬɢɬɶ ɨɛɧɨɜɥɟɧɢɟ ɧɟɭɧɢɤɚɥɶɧɵɯ ɢɧɞɟɤɫɨɜ ɜ ɬɚɛɥɢɰɟ MyISAM. ALTER TABLE...ENABLE KEYS ɡɚɬɟɦ ɦɨɠɟɬ
ɛɵɬɶ ɩɪɢɦɟɧɟɧ ɞɥɹ ɜɨɫɫɨɡɞɚɧɢɹ ɢɧɞɟɤɫɨɜ. MySQL ɞɟɥɚɟɬ ɷɬɨ ɩɨ ɫɩɟɰɢɚɥɶɧɨɦɭ
ɚɥɝɨɪɢɬɦɭ, ɤɨɬɨɪɵɣ ɝɨɪɚɡɞɨ ɛɵɫɬɪɟɟ, ɧɟɠɟɥɢ ɩɪɢ ɜɫɬɚɜɤɟ ɫɬɪɨɤ ɩɨ ɨɞɧɨɣ, ɩɨɷɬɨ„DROP PRIMARY KEY
6.2. Îïåðàòîðû îïðåäåëåíèÿ äàííûõ
257
ɦɭ ɨɬɤɥɸɱɟɧɢɟ ɢɧɞɟɤɫɨɜ ɩɟɪɟɞ ɨɩɟɪɚɰɢɟɣ ɩɚɤɟɬɧɨɣ ɜɫɬɚɜɤɢ ɞɨɥɠɧɨ ɞɚɬɶ ɨɳɭɬɢɦɨɟ ɭɫɤɨɪɟɧɢɟ.
„Ʉɨɧɫɬɪɭɤɰɢɢ FOREIGN KEY ɢ REFERENCES ɩɨɞɞɟɪɠɢɜɚɸɬɫɹ ɦɟɯɚɧɢɡɦɨɦ ɯɪɚɧɟɧɢɹ
InnoDB, ɤɨɬɨɪɵɣ ɪɟɚɥɢɡɭɟɬ ADD [CONSTRAINT [ɫɢɦɜɨɥ]] FOREIGN KEY (...)
REFERENCES... (...). Ⱦɥɹ ɞɪɭɝɢɯ ɦɟɯɚɧɢɡɦɨɜ ɯɪɚɧɟɧɢɹ ɷɬɢ ɤɨɧɫɬɪɭɤɰɢɢ ɩɪɢɧɢɦɚɸɬɫɹ, ɧɨ ɢɝɧɨɪɢɪɭɸɬɫɹ. Ʉɨɧɫɬɪɭɤɰɢɹ CHECK ɪɚɫɩɨɡɧɚɟɬɫɹ, ɧɨ ɢɝɧɨɪɢɪɭɟɬɫɹ
ɜɫɟɦɢ ɦɟɯɚɧɢɡɦɚɦɢ ɯɪɚɧɟɧɢɹ. ɋɦ. ɪɚɡɞɟɥ 6.2.5. ɉɪɢɱɢɧɚ, ɩɨ ɤɨɬɨɪɨɣ ɷɬɨɬ ɫɢɧɬɚɤɫɢɫ ɩɪɢɧɢɦɚɟɬɫɹ, ɧɨ ɢɝɧɨɪɢɪɭɟɬɫɹ, ɫɜɹɡɚɧɚ ɫ ɨɛɟɫɩɟɱɟɧɢɟɦ ɫɨɜɦɟɫɬɢɦɨɫɬɢ, ɭɩɪɨɳɟɧɢɟɦ ɩɟɪɟɧɨɫɚ ɤɨɞɚ ɫ ɞɪɭɝɢɯ SQL-ɫɟɪɜɟɪɨɜ, ɢ ɧɟɨɛɯɨɞɢɦɨɫɬɶɸ ɡɚɩɭɫɤɚ ɩɪɢɥɨɠɟɧɢɣ, ɤɨɬɨɪɵɟ ɫɨɡɞɚɸɬ ɬɚɛɥɢɰɵ ɫɨ ɫɫɵɥɤɚɦɢ. ɋɦ. ɪɚɡɞɟɥ 1.8.5.
„ɇɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.0.13, InnoDB ɩɨɞɞɟɪɠɢɜɚɟɬ ɩɪɢɦɟɧɟɧɢɟ ALTER TABLE
ɞɥɹ ɭɞɚɥɟɧɢɹ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ:
ALTER TABLE ɢɦɹ_ɬɚɛɥɢɰɵ DROP FOREIGN KEY ɫɢɦɜɨɥ_fk;
ɢɝɧɨɪɢɪɭɟɬ ɨɩɰɢɢ DATA DIRECTORY ɢ INDEX DIRECTORY.
„ɇɚɱɢɧɚɹ ɫ MySQL 4.1.2 ɢ ɩɨɫɥɟɞɭɸɳɢɯ ɜɟɪɫɢɣ, ɟɫɥɢ ɜɵ ɯɨɬɢɬɟ ɢɡɦɟɧɢɬɶ ɧɚɛɨɪ
ɫɢɦɜɨɥɨɜ ɞɥɹ ɜɫɟɯ ɫɢɦɜɨɥɶɧɵɯ ɫɬɨɥɛɰɨɜ (CHAR, VARCHAR, TEXT) ɧɚ ɞɪɭɝɨɣ, ɢɫɩɨɥɶɡɭɣɬɟ ɨɩɟɪɚɬɨɪ ɜɪɨɞɟ ɫɥɟɞɭɸɳɟɝɨ:
„ALTER TABLE
ALTER TABLE ɢɦɹ_ɬɚɛɥɢɰɵ CONVERT TO CHARACTER SET ɢɦɹ_ɧɚɛɨɪɚ_ɫɢɦɜɨɥɨɜ;
ɗɬɨ ɭɞɨɛɧɨ, ɧɚɩɪɢɦɟɪ, ɩɨɫɥɟ ɨɛɧɨɜɥɟɧɢɹ ɜɟɪɫɢɢ MySQL 4.0.x ɞɨ MySQL 4.1.x.
ɋɦ. ɪɚɡɞɟɥ 3.10.
dÖÒÕËÖÒÏ!
ɉɪɟɞɵɞɭɳɚɹ ɨɩɟɪɚɰɢɹ ɩɪɟɨɛɪɚɡɭɟɬ ɡɧɚɱɟɧɢɹ ɫɬɨɥɛɰɨɜ ɦɟɠɞɭ ɫɬɚɪɵɦ ɢ ɧɨɜɵɦ ɧɚɛɨɪɚɦɢ ɫɢɦɜɨɥɨɜ. ɗɬɨ ɧɟ ɬɨ, ɱɬɨ ɧɭɠɧɨ, ɟɫɥɢ ɭ ɜɚɫ ɟɫɬɶ ɫɬɨɥɛɟɰ ɜ ɨɞɧɨɦ ɧɚɛɨɪɟ ɫɢɦɜɨɥɨɜ (ɜɪɨɞɟ latin1), ɧɨ ɫɨɞɟɪɠɢɬ ɡɧɚɱɟɧɢɹ, ɜ ɞɟɣɫɬɜɢɬɟɥɶɧɨɫɬɢ ɢɫɩɨɥɶɡɭɸɳɢɟ ɤɚɤɨɣ-ɬɨ ɞɪɭɝɨɣ, ɧɟɫɨɜɦɟɫɬɢɦɵɣ ɧɚɛɨɪ
ɫɢɦɜɨɥɨɜ (ɜɪɨɞɟ utf8). ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɞɥɹ ɤɚɠɞɨɣ ɬɚɤɨɣ ɫɬɨɥɛɰɵ ɩɨɬɪɟɛɭɟɬɫɹ ɜɵɩɨɥɧɢɬɶ ɫɥɟɞɭɸɳɢɟ ɨɩɟɪɚɬɨɪɵ:
ALTER TABLE t1 CHANGE c1 c1 BLOB;
ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;
ɗɬɨ ɪɚɛɨɬɚɟɬ, ɩɨɫɤɨɥɶɤɭ ɧɟ ɩɪɨɢɫɯɨɞɢɬ ɧɢɤɚɤɨɝɨ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɫɬɨɥɛɰɨɜ ɬɢɩɚ
BLOB. Ⱦɥɹ ɢɡɦɟɧɟɧɢɹ ɬɨɥɶɤɨ ɧɚɛɨɪɚ ɫɢɦɜɨɥɨɜ ɩɨ ɭɦɨɥɱɚɧɢɸ ɞɥɹ ɬɚɛɥɢɰɵ ɜɨɫɩɨɥɶɡɭɣɬɟɫɶ ɫɥɟɞɭɸɳɢɦ ɨɩɟɪɚɬɨɪɨɦ:
ALTER TABLE ɢɦɹ_ɬɚɛɥɢɰɵ DEFAULT CHARACTER SET ɢɦɹ_ɧɚɛɨɪɚ_ɫɢɦɜɨɥɨɜ;
ɋɥɨɜɨ DEFAULT ɹɜɥɹɟɬɫɹ ɧɟɨɛɹɡɚɬɟɥɶɧɵɦ. ɇɚɛɨɪ ɫɢɦɜɨɥɨɜ ɩɨ ɭɦɨɥɱɚɧɢɸ – ɷɬɨ ɬɨɬ
ɧɚɛɨɪ, ɤɨɬɨɪɵɣ ɩɪɢɦɟɧɹɟɬɫɹ ɞɥɹ ɧɨɜɵɯ ɫɬɨɥɛɰɨɜ, ɞɨɛɚɜɥɹɟɦɵɯ ɤ ɬɚɛɥɢɰɟ, ɟɫɥɢ ɧɟ
ɭɤɚɡɵɜɚɟɬɫɹ ɹɜɧɨ (ɧɚɩɪɢɦɟɪ, ɜ ALTER TABLE...ADD column).
dÖÒÕËÖÒÏ!
ɇɚɱɢɧɚɹ ɫ MySQL 4.1.2 ɢ ɜɵɲɟ, ALTER TABLE...DEFAULT CHARACTER SET ɢ ALTER TABLE
... CHARACTER SET ɷɤɜɢɜɚɥɟɧɬɧɵ ɢ ɢɡɦɟɧɹɸɬ ɬɨɥɶɤɨ ɧɚɛɨɪ ɫɢɦɜɨɥɨɜ ɩɨ ɭɦɨɥɱɚɧɢɸ. ȼ ɜɵɩɭɫɤɚɯ MySQL 4.1, ɩɪɟɞɲɟɫɬɜɭɸɳɢɯ 4.1.2, ALTER TABLE...DEFAULT CHARACTER SET ɢɡɦɟɧɹɟɬ
ɧɚɛɨɪ ɫɢɦɜɨɥɨɜ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɚ ALTER TABLE...CHARACTER SET ɢɡɦɟɧɹɟɬ ɧɚɛɨɪ ɫɢɦɜɨɥɨɜ
ɩɨ ɭɦɨɥɱɚɧɢɸ ɢ ɩɪɟɨɛɪɚɡɭɟɬ ɜɫɟ ɫɬɨɥɛɰɵ.
258
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɬɚɛɥɢɰɵ InnoDB, ɤɨɬɨɪɚɹ ɛɵɥɚ ɫɨɡɞɚɧɚ ɜ ɫɜɨɟɦ ɫɨɛɫɬɜɟɧɧɨɦ ɬɚɛɥɢɱɧɨɦ ɩɪɨɫɬɪɚɧɫɬɜɟ ɜ ɮɚɣɥɟ .ibd, ɷɬɨɬ ɮɚɣɥ ɦɨɠɟɬ ɛɵɬɶ ɨɬɛɪɨɲɟɧ ɢ ɢɦɩɨɪɬɢɪɨɜɚɧ. ɑɬɨɛɵ
ɨɬɛɪɨɫɢɬɶ (discard) ɮɚɣɥ .ibd, ɜɨɫɩɨɥɶɡɭɣɬɟɫɶ ɫɥɟɞɭɸɳɢɦ ɨɩɟɪɚɬɨɪɨɦ:
„Ⱦɥɹ
ALTER TABLE ɢɦɹ_ɬɚɛɥɢɰɵ DISCARD TABLESPACE;
ɉɪɢɜɟɞɟɧɧɵɣ ɜɵɲɟ ɨɩɟɪɚɬɨɪ ɭɞɚɥɢɬ ɬɟɤɭɳɢɣ ɮɚɣɥ .ibd, ɩɨɷɬɨɦɭ ɫɧɚɱɚɥɚ ɭɛɟɞɢɬɟɫɶ, ɱɬɨ ɭ ɜɚɫ ɟɫɬɶ ɪɟɡɟɪɜɧɚɹ ɤɨɩɢɹ. ɉɨɩɵɬɤɢ ɨɛɪɚɬɢɬɶɫɹ ɤ ɬɚɛɥɢɰɟ, ɤɨɝɞɚ ɮɚɣɥ
ɬɚɛɥɢɱɧɨɝɨ ɩɪɨɫɬɪɚɧɫɬɜɚ ɨɬɛɪɨɲɟɧ, ɩɪɢɜɨɞɹɬ ɤ ɨɲɢɛɤɟ.
ɑɬɨɛɵ ɢɦɩɨɪɬɢɪɨɜɚɬɶ ɨɛɪɚɬɧɨ ɪɟɡɟɪɜɧɭɸ ɤɨɩɢɸ ɮɚɣɥɚ .ibd, ɫɤɨɩɢɪɭɣɬɟ ɟɝɨ ɜ ɤɚɬɚɥɨɝ ɞɚɧɧɵɯ ɢ ɜɵɩɨɥɧɢɬɟ ɫɥɟɞɭɸɳɢɣ ɨɩɟɪɚɬɨɪ:
ALTER TABLE ɢɦɹ_ɬɚɛɥɢɰɵ IMPORT TABLESPACE;
ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɢ C API mysql_info() ɦɨɠɧɨ ɨɩɪɟɞɟɥɢɬɶ, ɫɤɨɥɶɤɨ ɡɚɩɢɫɟɣ
ɛɵɥɨ ɫɤɨɩɢɪɨɜɚɧɨ ɢ (ɤɨɝɞɚ ɩɪɢɦɟɧɹɟɬɫɹ IGNORE) ɫɤɨɥɶɤɨ ɡɚɩɢɫɟɣ ɛɵɥɨ ɭɞɚɥɟɧɨ
ɢɡ-ɡɚ ɞɭɛɥɢɪɨɜɚɧɢɹ ɡɧɚɱɟɧɢɣ ɭɧɢɤɚɥɶɧɵɯ ɤɥɸɱɟɣ.
ɇɢɠɟ ɩɨɤɚɡɚɧɵ ɧɟɤɨɬɨɪɵɟ ɩɪɢɦɟɪɵ, ɞɟɦɨɧɫɬɪɢɪɭɸɳɢɟ ɩɪɢɦɟɧɟɧɢɟ ɨɩɟɪɚɬɨɪɚ ALTER
TABLE. ɇɚɱɧɟɦ ɫ ɬɚɛɥɢɰɵ t1, ɫɨɡɞɚɧɧɨɣ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
„ɋ
mysql> CREATE TABLE t1 (a INTEGER,b CHAR(10));
ɑɬɨɛɵ ɩɟɪɟɢɦɟɧɨɜɚɬɶ ɬɚɛɥɢɰɭ ɢɡ t1 ɜ t2, ɜɨɫɩɨɥɶɡɭɣɬɟɫɶ ɬɚɤɢɦ ɨɩɟɪɚɬɨɪɨɦ:
mysql> ALTER TABLE t1 RENAME t2;
ɑɬɨɛɵ ɢɡɦɟɧɢɬɶ ɬɢɩ ɫɬɨɥɛɰɚ ɫ INTEGER ɧɚ TINYINT NOT NULL (ɨɫɬɚɜɢɜ ɟɝɨ ɢɦɹ ɩɪɟɠɧɢɦ), ɢ ɱɬɨɛɵ ɢɡɦɟɧɢɬɶ ɬɢɩ ɫɬɨɥɛɰɚ b ɫ CHAR(10) ɧɚ CHAR(20) ɢ ɩɟɪɟɢɦɟɧɨɜɚɬɶ ɟɝɨ ɧɚ c,
ɜɨɫɩɨɥɶɡɭɣɬɟɫɶ ɬɚɤɢɦ ɨɩɟɪɚɬɨɪɨɦ:
mysql> ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);
ɑɬɨɛɵ ɞɨɛɚɜɢɬɶ ɧɨɜɵɣ ɫɬɨɥɛɟɰ ɬɢɩɚ TIMESTAMP ɫ ɢɦɟɧɟɦ d, ɜɵɩɨɥɧɢɬɟ ɫɥɟɞɭɸɳɢɣ
ɨɩɟɪɚɬɨɪ:
mysql> ALTER TABLE t2 ADD d TIMESTAMP;
ɑɬɨɛɵ ɞɨɛɚɜɢɬɶ ɢɧɞɟɤɫɵ ɩɨ ɫɬɨɥɛɰɭ d ɢ ɫɬɨɥɛɰɭ a, ɜɨɫɩɨɥɶɡɭɣɬɟɫɶ ɬɚɤɢɦ ɨɩɟɪɚɬɨɪɨɦ:
mysql> ALTER TABLE t2 ADD INDEX (d), ADD INDEX (a);
ɑɬɨɛɵ ɭɞɚɥɢɬɶ ɫɬɨɥɛɟɰ c, ɜɵɩɨɥɧɢɬɟ ɬɚɤɨɣ ɨɩɟɪɚɬɨɪ:
mysql> ALTER TABLE t2 DROP COLUMN c;
ɑɬɨɛɵ ɞɨɛɚɜɢɬɶ ɧɨɜɵɣ ɫɬɨɥɛɟɰ ɫ ɢɦɟɧɟɦ c ɰɟɥɨɝɨ ɬɢɩɚ, ɢɦɟɸɳɢɣ ɫɜɨɣɫɬɜɨ
AUTO_INCREMENT, ɜɨɫɩɨɥɶɡɭɣɬɟɫɶ ɫɥɟɞɭɸɳɢɦ ɨɩɟɪɚɬɨɪɨɦ:
mysql> ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,
-> ADD PRIMARY KEY (c);
ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɦɵ ɩɪɨɢɧɞɟɤɫɢɪɨɜɚɥɢ ɫɬɨɥɛɟɰ c (ɤɚɤ ɩɟɪɜɢɱɧɵɣ ɤɥɸɱ), ɩɨɫɤɨɥɶɤɭ ɫɬɨɥɛɰɵ AUTO_INCREMENT ɞɨɥɠɧɵ ɛɵɬɶ ɩɪɨɢɧɞɟɤɫɢɪɨɜɚɧɵ. Ʉɪɨɦɟ ɬɨɝɨ, ɫɬɨɥɛɟɰ
c ɨɛɴɹɜɥɟɧ ɤɚɤ NOT NULL, ɬɚɤ ɤɚɤ ɫɬɨɥɛɰɵ ɩɟɪɜɢɱɧɨɝɨ ɤɥɸɱɚ ɧɟ ɦɨɝɭɬ ɛɵɬɶ NULL.
Ʉɨɝɞɚ ɞɨɛɚɜɥɹɟɬɫɹ ɫɬɨɥɛɟɰ ɫ ɚɬɪɢɛɭɬɨɦ AUTO_INCREMENT, ɨɧ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɡɚɩɨɥɧɹɟɬɫɹ ɡɧɚɱɟɧɢɹɦɢ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɰɟɥɵɯ ɱɢɫɟɥ. Ⱦɥɹ ɬɚɛɥɢɰ MyISAM ɦɨɠɧɨ ɭɤɚɡɚɬɶ
ɩɟɪɜɨɟ ɱɢɫɥɨ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ, ɜɵɩɨɥɧɢɜ SET INSERT_ID=ɡɧɚɱɟɧɢɟ ɩɟɪɟɞ ɡɚɩɭɫɤɨɦ
ALTER TABLE, ɢɥɢ ɜɨɫɩɨɥɶɡɨɜɚɜɲɢɫɶ ɨɩɰɢɟɣ ɬɚɛɥɢɰɵ AUTO_INCREMENT=ɡɧɚɱɟɧɢɟ. ɋɦ.
ɪɚɡɞɟɥ 6.5.3.1.
6.2. Îïåðàòîðû îïðåäåëåíèÿ äàííûõ
259
Ⱦɥɹ ɬɚɛɥɢɰ MyISAM, ɟɫɥɢ ɜɵ ɧɟ ɢɡɦɟɧɹɟɬɟ ɫɬɨɥɛɟɰ AUTO_INCREMENT, ɱɢɫɥɨɜɚɹ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɧɟ ɛɭɞɟɬ ɡɚɬɪɨɧɭɬɚ. ȿɫɥɢ ɜɵ ɭɞɚɥɹɟɬɟ ɫɬɨɥɛɟɰ AUTO_INCREMENT, ɚ ɡɚɬɟɦ ɞɨɛɚɜɥɹɟɬɟ ɞɪɭɝɨɣ ɫɬɨɥɛɟɰ AUTO_INCREMENT, ɬɨ ɱɢɫɥɚ ɛɭɞɭɬ ɩɟɪɟɧɭɦɟɪɨɜɚɧɵ, ɧɚɱɢɧɚɹ ɫ 1.
ȼ MySQL 3.23.50 ɢ ɛɨɥɟɟ ɩɨɡɞɧɢɯ ɜɟɪɫɢɹɯ InnoDB ɪɚɡɪɟɲɚɟɬ ɞɨɛɚɜɥɹɬɶ ɧɨɜɵɟ ɨɝɪɚɧɢɱɟɧɢɹ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ ɤ ɬɚɛɥɢɰɟ ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ ALTER TABLE:
ALTER TABLE ɢɦɹ_ɬɚɛɥɢɰɵ
ADD [CONSTRAINT ɫɢɦɜɨɥ] FOREIGN KEY [ɢɞɟɧɬɢɮɢɤɚɬɨɪ] (ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...)
REFERENCES ɢɦɹ_ɬɚɛɥɢɰɵ (ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ, ...)
[ON DELETE {CASCADE | SET NULL | NO ACTION | RESTRICT}]
[ON UPDATE {CASCADE | SET NULL | NO ACTION | RESTRICT}]
ɇɟ ɡɚɛɭɞɶɬɟ ɫɧɚɱɚɥɚ ɫɨɡɞɚɬɶ ɜɫɟ ɧɟɨɛɯɨɞɢɦɵɟ ɢɧɞɟɤɫɵ. ɂɫɩɨɥɶɡɭɹ ALTER TABLE, ɜ
ɬɚɛɥɢɰɭ ɦɨɠɧɨ ɬɚɤɠɟ ɞɨɛɚɜɥɹɬɶ ɜɧɟɲɧɢɟ ɤɥɸɱɢ, ɫɫɵɥɚɸɳɢɟɫɹ ɧɚ ɷɬɭ ɠɟ ɬɚɛɥɢɰɭ.
ɇɚɱɢɧɚɹ ɫ MySQL 4.0.13, InnoDB ɩɨɞɞɟɪɠɢɜɚɟɬ ɩɪɢɦɟɧɟɧɢɟ ALTER TABLE ɞɥɹ ɭɞɚɥɟɧɢɹ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ:
ALTER TABLE ɢɦɹ_ɬɚɛɥɢɰɵ DROP FOREIGN KEY ɫɢɦɜɨɥ_fk;
ȿɫɥɢ ɤɨɧɫɬɪɭɤɰɢɹ FOREIGN KEY ɜɤɥɸɱɚɟɬ ɢɦɹ CONSTRAINT, ɤɨɝɞɚ ɫɨɡɞɚɟɬɫɹ ɜɧɟɲɧɢɣ
ɤɥɸɱ, ɬɨ ɜɵ ɦɨɠɟɬɟ ɫɫɵɥɚɬɶɫɹ ɧɚ ɧɟɝɨ, ɱɬɨɛɵ ɭɞɚɥɢɬɶ ɷɬɨɬ ɜɧɟɲɧɢɣ ɤɥɸɱ. (ɂɦɹ ɨɝɪɚɧɢɱɟɧɢɹ ɜɧɟɲɧɟɝɨ ɤɥɸɱɚ ɦɨɠɟɬ ɩɪɢɫɜɚɢɜɚɬɶɫɹ, ɧɚɱɢɧɚɹ ɫ MySQL 4.0.18.) ȼ ɩɪɨɬɢɜɧɨɦ
ɫɥɭɱɚɟ InnoDB ɝɟɧɟɪɢɪɭɟɬ ɜɧɭɬɪɟɧɧɟɟ ɡɧɚɱɟɧɢɟ ɫɢɦɜɨɥ_fk ɩɪɢ ɫɨɡɞɚɧɢɢ ɜɧɟɲɧɟɝɨ ɤɥɸɱɚ. ɑɬɨɛɵ ɭɡɧɚɬɶ ɷɬɨ ɡɧɚɱɟɧɢɟ, ɤɨɝɞɚ ɜɚɦ ɩɨɧɚɞɨɛɢɬɫɹ ɭɞɚɥɢɬɶ ɜɧɟɲɧɢɣ ɤɥɸɱ, ɜɨɫɩɨɥɶɡɭɣɬɟɫɶ ɨɩɟɪɚɬɨɪɨɦ SHOW CREATE TABLE, ɧɚɩɪɢɦɟɪ:
mysql> SHOW CREATE TABLE ibtest11c\G
*************************** 1. row ***************************
Table: ibtest11c
Create Table: CREATE TABLE `ibtest11c` (
`A` int(11) NOT NULL auto_increment,
`D` int(11) NOT NULL default '0',
`B` varchar(200) NOT NULL default '',
`C` varchar(175) default NULL,
PRIMARY KEY (`A`,`D`,`B`),
KEY `B` (`B`,`C`),
KEY `C` (`C`),
CONSTRAINT `0_38775` FOREIGN KEY (`A`, `D`)
REFERENCES `ibtest11a` (`A`, `D`)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `0_38776` FOREIGN KEY (`B`, `C`)
REFERENCES `ibtest11a` (`B`, `C`)
ON DELETE CASCADE ON UPDATE CASCADE
) TYPE=InnoDB CHARSET=latin1
1 row in set (0.01 sec)
mysql> ALTER TABLE ibtest11c DROP FOREIGN KEY 0_38775;
ȼ ɜɟɪɫɢɹɯ MySQL ɞɨ 3.23.50 ɨɩɟɪɚɬɨɪɵ ALTER TABLE ɢ CREATE INDEX ɧɟ ɞɨɥɠɧɵ
ɩɪɢɦɟɧɹɬɶɫɹ ɞɥɹ ɬɚɛɥɢɰ, ɤɨɬɨɪɵɟ ɢɦɟɸɬ ɨɝɪɚɧɢɱɟɧɢɹ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ, ɥɢɛɨ ɧɚ ɤɨɬɨɪɵɟ
ɫɫɵɥɚɸɬɫɹ ɜɧɟɲɧɢɟ ɤɥɸɱɢ ɞɪɭɝɢɯ ɬɚɛɥɢɰ. Ʌɸɛɨɣ ɨɩɟɪɚɬɨɪ ALTER TABLE ɭɞɚɥɹɟɬ ɜɫɟ
ɨɝɪɚɧɢɱɟɧɢɹ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ, ɨɩɪɟɞɟɥɟɧɧɵɟ ɜ ɬɚɛɥɢɰɟ. ɇɟɥɶɡɹ ɬɚɤɠɟ ɩɪɢɦɟɧɹɬɶ ALTER
TABLE ɤ ɬɚɛɥɢɰɚɦ, ɧɚ ɤɨɬɨɪɵɟ ɢɦɟɸɬɫɹ ɫɫɵɥɤɢ. ȼɦɟɫɬɨ ɷɬɨɝɨ ɧɭɠɧɨ ɩɨɥɶɡɨɜɚɬɶɫɹ DROP
260
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
TABLE ɢ CREATE TABLE ɞɥɹ ɦɨɞɢɮɢɤɚɰɢɢ ɫɯɟɦɵ. Ʉɨɝɞɚ ɫɟɪɜɟɪ MySQL ɜɵɩɨɥɧɹɟɬ ALTER
TABLE, ɨɧ ɦɨɠɟɬ ɫɤɪɵɬɨ ɜɵɩɨɥɧɹɬɶ RENAME TABLE, ɢ ɷɬɨ ɫɛɢɜɚɟɬ ɫ ɬɨɥɤɭ ɨɝɪɚɧɢɱɟɧɢɹ
ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ, ɤɨɬɨɪɵɟ ɫɫɵɥɚɸɬɫɹ ɧɚ ɬɚɛɥɢɰɭ. ȼ MySQL ɨɩɟɪɚɬɨɪ CREATE INDEX ɨɛɪɚɛɚɬɵɜɚɟɬɫɹ ɤɚɤ ALTER TABLE, ɩɨɷɬɨɦɭ ɜɵɲɟɫɤɚɡɚɧɧɨɟ ɤɚɫɚɟɬɫɹ ɢ ɷɬɨɝɨ ɨɩɟɪɚɬɨɪɚ.
ɋɦ. ɪɚɡɞɟɥ Ⱥ.3.1.
6.2.3. Ñèíòàêñèñ CREATE DATABASE
CREATE DATABASE [IF NOT EXISTS] ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ
[ɫɩɟɰɢɮɢɤɚɰɢɹ_create [, ɫɩɟɰɢɮɢɤɚɰɢɹ_create] ...]
ɫɩɟɰɢɮɢɤɚɰɢɹ_create:
[DEFAULT] CHARACTER SET ɢɦɹ_ɧɚɛɨɪɚ_ɫɢɦɜɨɥɨɜ
| [DEFAULT] COLLATE ɢɦɹ_ɩɨɪɹɞɤɚ_ɫɨɩɨɫɬɚɜɥɟɧɢɹ
CREATE DATABASE ɫɨɡɞɚɟɬ ɛɚɡɭ ɞɚɧɧɵɯ ɫ ɭɤɚɡɚɧɧɵɦ ɢɦɟɧɟɦ. Ⱦɥɹ ɢɫɩɨɥɶɡɨɜɚɧɢɹ
CREATE DATABASE ɧɟɨɛɯɨɞɢɦɨ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ CREATE ɞɥɹ ɛɚɡɵ ɞɚɧɧɵɯ.
ɉɪɚɜɢɥɚ ɢɦɟɧɨɜɚɧɢɹ ɛɚɡ ɞɚɧɧɵɯ ɨɩɢɫɚɧɵ ɜ ɪɚɡɞɟɥɟ 2.2. ȿɫɥɢ ɛɚɡɚ ɞɚɧɧɵɯ ɫ ɬɚɤɢɦ
ɢɦɟɧɟɦ ɫɭɳɟɫɬɜɭɟɬ, ɢ ɧɟ ɛɵɥɨ ɭɤɚɡɚɧɨ IF NOT EXISTS, ɝɟɧɟɪɢɪɭɟɬɫɹ ɨɲɢɛɤɚ.
ɇɚɱɢɧɚɹ ɫ MySQL 4.1.1, ɨɩɰɢɹ ɫɩɟɰɢɮɢɤɚɰɢɹ_create ɦɨɠɟɬ ɭɤɚɡɵɜɚɬɶɫɹ ɞɥɹ ɨɩɪɟɞɟɥɟɧɢɹ ɯɚɪɚɤɬɟɪɢɫɬɢɤ ɛɚɡɵ ɞɚɧɧɵɯ. ɏɚɪɚɤɬɟɪɢɫɬɢɤɢ ɛɚɡɵ ɞɚɧɧɵɯ ɫɨɯɪɚɧɹɸɬɫɹ ɜ ɮɚɣɥɟ
db.opt, ɪɚɫɩɨɥɨɠɟɧɧɨɦ ɜ ɤɚɬɚɥɨɝɟ ɞɚɧɧɵɯ. Ʉɨɧɫɬɪɭɤɰɢɹ CHARACTER SET ɨɩɪɟɞɟɥɹɟɬ ɧɚɛɨɪ ɫɢɦɜɨɥɨɜ ɞɥɹ ɛɚɡɵ ɞɚɧɧɵɯ ɩɨ ɭɦɨɥɱɚɧɢɸ. Ʉɨɧɫɬɪɭɤɰɢɹ COLLATION ɡɚɞɚɟɬ ɩɨɪɹɞɨɤ
ɫɨɩɨɫɬɚɜɥɟɧɢɹ ɩɨ ɭɦɨɥɱɚɧɢɸ. ɇɚɛɨɪɵ ɫɢɦɜɨɥɨɜ ɢ ɩɨɪɹɞɤɢ ɫɨɩɨɫɬɚɜɥɟɧɢɹ ɨɛɫɭɠɞɚɸɬɫɹ
ɜ ɝɥɚɜɟ 3.
Ȼɚɡɵ ɞɚɧɧɵɯ ɜ MySQL ɪɟɚɥɢɡɨɜɚɧɵ ɜ ɜɢɞɟ ɤɚɬɚɥɨɝɨɜ, ɤɨɬɨɪɵɟ ɫɨɞɟɪɠɚɬ ɮɚɣɥɵ, ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɬɚɛɥɢɰɚɦ ɛɚɡɵ ɞɚɧɧɵɯ. ɉɨɫɤɨɥɶɤɭ ɢɡɧɚɱɚɥɶɧɨ ɜ ɛɚɡɟ ɧɟɬ ɧɢɤɚɤɢɯ ɬɚɛɥɢɰ, ɨɩɟɪɚɬɨɪ CREATE DATABASE ɬɨɥɶɤɨ ɫɨɡɞɚɟɬ ɩɨɞɤɚɬɚɥɨɝ ɜ ɤɚɬɚɥɨɝɟ ɞɚɧɧɵɯ MySQL (ɢ
ɮɚɣɥ db.opt ɞɥɹ ɜɟɪɫɢɢ MySQL 4.1.1 ɢ ɜɵɲɟ).
Ⱦɥɹ ɫɨɡɞɚɧɢɹ ɛɚɡɵ ɞɚɧɧɵɯ ɦɨɠɧɨ ɬɚɤɠɟ ɜɨɫɩɨɥɶɡɨɜɚɬɶɫɹ ɩɪɨɝɪɚɦɦɨɣ mysqladmin.
6.2.4. Ñèíòàêñèñ CREATE INDEX
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX ɢɦɹ_ɢɧɞɟɤɫɚ [ɬɢɩ_ɢɧɞɟɤɫɚ]
ON ɢɦɹ_ɬɚɛɥɢɰɵ (ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...)
ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ:
ɢɦɹ_ɫɬɨɥɛɰɚ [(ɞɥɢɧɚ)] [ASC | DESC]
ȼ ɜɟɪɫɢɢ MySQL 3.22 ɢ ɜɵɲɟ CREATE INDEX ɨɬɨɛɪɚɠɚɟɬɫɹ ɧɚ ɨɩɟɪɚɬɨɪ ALTER TABLE,
ɱɬɨɛɵ ɫɨɡɞɚɜɚɬɶ ɢɧɞɟɤɫɵ. ɋɦ. ɪɚɡɞɟɥ 6.2.2. Ⱦɨ MySQL 3.22 ɨɩɟɪɚɬɨɪ CREATE INDEX ɧɢɱɟɝɨ ɧɟ ɞɟɥɚɥ.
Ɉɛɵɱɧɨ ɜɫɟ ɢɧɞɟɤɫɵ ɬɚɛɥɢɰɵ ɫɨɡɞɚɸɬɫɹ ɜ ɬɨ ɠɟ ɜɪɟɦɹ, ɤɨɝɞɚ ɫɨɡɞɚɟɬɫɹ ɬɚɛɥɢɰɵ ɫ
ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ CREATE TABLE. ɋɦ. ɪɚɡɞɟɥ 6.2.5. ɋɢɧɬɚɤɫɢɫ CREATE INDEX ɩɨɡɜɨɥɹɟɬ
ɞɨɛɚɜɥɹɬɶ ɢɧɞɟɤɫɵ ɜ ɫɭɳɟɫɬɜɭɸɳɭɸ ɬɚɛɥɢɰɭ.
ɋɩɢɫɨɤ ɫɬɨɥɛɰɨɜ ɜ ɮɨɪɦɟ (ɫɬɨɥɛɟɰ1,ɫɬɨɥɛɟɰ2,...) ɫɨɡɞɚɟɬ ɫɨɫɬɚɜɧɨɣ ɢɧɞɟɤɫ. Ʉɥɸɱ
ɢɧɞɟɤɫɚ ɮɨɪɦɢɪɭɟɬɫɹ ɩɭɬɟɦ ɤɨɧɤɚɬɟɧɚɰɢɢ ɡɧɚɱɟɧɢɣ ɭɤɚɡɚɧɧɵɯ ɫɬɨɥɛɰɨɜ.
Ⱦɥɹ ɫɬɨɥɛɰɨɜ CHAR ɢ VARCHAR ɤɥɸɱɨɦ ɢɧɞɟɤɫɚ ɦɨɝɭɬ ɫɥɭɠɢɬɶ ɬɨɥɶɤɨ ɱɚɫɬɢ ɫɬɨɥɛɰɨɜ,
ɟɫɥɢ ɢɫɩɨɥɶɡɨɜɚɬɶ ɫɢɧɬɚɤɫɢɫ ɢɦɹ_ɫɬɨɥɛɰɚ(ɞɥɢɧɚ), ɱɬɨɛɵ ɢɧɞɟɤɫɢɪɨɜɚɬɶ ɬɨɥɶɤɨ ɩɪɟɮɢɤɫɵ ɡɧɚɱɟɧɢɣ ɫɬɨɥɛɰɨɜ ɞɥɢɧɨɣ ɩɟɪɜɵɟ ɞɥɢɧɚ ɫɢɦɜɨɥɨɜ. ɋɬɨɥɛɰɵ ɬɢɩɨɜ BLOB ɢ TEXT ɬɚɤɠɟ
ɦɨɝɭɬ ɛɵɬɶ ɢɧɞɟɤɫɢɪɨɜɚɧɵ, ɧɨ ɞɥɢɧɚ ɩɪɟɮɢɤɫɚ ɞɥɹ ɧɢɯ ɞɨɥɠɧɚ ɛɵɬɶ ɭɤɚɡɚɧɚ.
6.2. Îïåðàòîðû îïðåäåëåíèÿ äàííûõ
261
Ɉɩɟɪɚɬɨɪ, ɩɪɢɜɟɞɟɧɧɵɣ ɧɢɠɟ, ɫɨɡɞɚɟɬ ɢɧɞɟɤɫ, ɢɫɩɨɥɶɡɭɹ ɜ ɤɚɱɟɫɬɜɟ ɤɥɸɱɚ ɩɟɪɜɵɟ 10
ɫɢɦɜɨɥɨɜ ɫɬɨɥɛɰɚ name:
CREATE INDEX part_of_name ON customer (name(10));
ɉɨɫɤɨɥɶɤɭ ɛɨɥɶɲɢɧɫɬɜɨ ɢɦɟɧ ɨɛɵɱɧɨ ɨɬɥɢɱɚɸɬɫɹ ɞɪɭɝ ɨɬ ɞɪɭɝɚ ɩɟɪɜɵɦɢ 10 ɫɢɦɜɨɥɚɦɢ, ɷɬɨɬ ɢɧɞɟɤɫ ɧɟ ɞɨɥɠɟɧ ɛɵɬɶ ɧɚɦɧɨɝɨ ɦɟɞɥɟɧɧɟɟ, ɱɟɦ ɢɧɞɟɤɫ, ɫɨɡɞɚɧɧɵɣ ɩɨ ɩɨɥɧɵɦ ɡɧɚɱɟɧɢɹɦ ɫɬɨɥɛɰɚ name. Ʉɪɨɦɟ ɬɨɝɨ, ɩɪɢɦɟɧɹɹ ɱɚɫɬɢ ɫɬɨɥɛɰɨɜ ɜ ɤɚɱɟɫɬɜɟ ɤɥɸɱɟɣ
ɢɧɞɟɤɫɚ, ɦɨɠɧɨ ɡɧɚɱɢɬɟɥɶɧɨ ɫɨɤɪɚɬɢɬɶ ɪɚɡɦɟɪ ɢɧɞɟɤɫɧɨɝɨ ɮɚɣɥɚ, ɱɬɨ ɫɷɤɨɧɨɦɢɬ ɞɢɫɤɨɜɨɟ ɩɪɨɫɬɪɚɧɫɬɜɨ ɢ ɦɨɠɟɬ ɭɫɤɨɪɢɬɶ ɜɵɩɨɥɧɟɧɢɟ ɨɩɟɪɚɰɢɢ INSERT.
ɉɪɟɮɢɤɫ ɦɨɠɟɬ ɛɵɬɶ ɞɨ 255 ɫɢɦɜɨɥɨɜ ɞɥɢɧɨɣ (ɢɥɢ 1000 ɫɢɦɜɨɥɨɜ ɞɥɹ ɬɚɛɥɢɰ MyISAM
ɢ InnoDB, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.1.2). Ɉɬɦɟɬɢɦ, ɱɬɨ ɦɚɤɫɢɦɚɥɶɧɚɹ ɞɥɢɧɚ ɩɪɟɮɢɤɫɚ
ɢɡɦɟɪɹɟɬɫɹ ɜ ɛɚɣɬɚɯ, ɜ ɬɨ ɜɪɟɦɹ ɤɚɤ ɞɥɢɧɚ ɩɪɟɮɢɤɫɚ, ɤɨɬɨɪɚɹ ɭɤɚɡɵɜɚɟɬɫɹ ɜ ɨɩɟɪɚɬɨɪɟ
CREATE INDEX, ɢɧɬɟɪɩɪɟɬɢɪɭɟɬɫɹ ɤɚɤ ɤɨɥɢɱɟɫɬɜɨ ɫɢɦɜɨɥɨɜ. ɉɪɢɧɢɦɚɣɬɟ ɷɬɨ ɜɨ ɜɧɢɦɚɧɢɟ, ɤɨɝɞɚ ɭɤɚɡɵɜɚɟɬɟ ɞɥɢɧɭ ɩɪɟɮɢɤɫɚ ɞɥɹ ɫɬɨɥɛɰɚ, ɢɫɩɨɥɶɡɭɸɳɟɝɨ ɦɧɨɝɨɛɚɣɬɧɵɣ ɧɚɛɨɪ
ɫɢɦɜɨɥɨɜ.
ɋɥɟɞɭɟɬ ɬɚɤɠɟ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɞɨɛɚɜɥɹɬɶ ɢɧɞɟɤɫ ɩɨ ɫɬɨɥɛɰɭ, ɞɨɩɭɫɤɚɸɳɟɦɭ ɡɧɚɱɟɧɢɹ
NULL, ɦɨɠɧɨ ɬɨɥɶɤɨ ɜ ɜɟɪɫɢɹɯ MySQL, ɧɚɱɢɧɚɹ ɫ 3.23.2 ɢ ɬɨɥɶɤɨ ɞɥɹ ɬɚɛɥɢɰ ɬɢɩɚ MyISAM,
InnoDB ɢɥɢ BDB. ɂɧɞɟɤɫɵ ɩɨ ɫɬɨɥɛɰɚɦ BLOB ɢ TEXT ɦɨɠɧɨ ɞɨɛɚɜɥɹɬɶ, ɟɫɥɢ ɜɵ ɪɚɛɨɬɚɟɬɟ ɫ
MySQL ɜɟɪɫɢɢ 3.23.2 ɢ ɜɵɲɟ ɢ ɢɫɩɨɥɶɡɭɟɬɟ ɬɚɛɥɢɰɵ MyISAM ɢɥɢ BDB, ɥɢɛɨ MySQL 4.0.14
ɢ ɜɵɲɟ ɢ ɬɚɛɥɢɰɵ InnoDB.
ɋɩɟɰɢɮɢɤɚɰɢɹ ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ ɦɨɠɟɬ ɡɚɜɟɪɲɚɬɶɫɹ ASC ɢɥɢ DESC. ɗɬɢ ɤɥɸɱɟɜɵɟ
ɫɥɨɜɚ ɜɜɟɞɟɧɵ ɞɥɹ ɛɭɞɭɳɢɯ ɪɚɫɲɢɪɟɧɢɣ, ɱɬɨɛɵ ɡɚɞɚɜɚɬɶ ɫɨɯɪɚɧɟɧɢɟ ɡɧɚɱɟɧɢɹ ɢɧɞɟɤɫɚ
ɩɨ ɜɨɡɪɚɫɬɚɧɢɸ ɢɥɢ ɭɛɵɜɚɧɢɸ. ȼ ɧɚɫɬɨɹɳɟɟ ɜɪɟɦɹ ɨɧɢ ɪɚɫɩɨɡɧɚɸɬɫɹ, ɨɞɧɚɤɨ ɢɝɧɨɪɢɪɭɸɬɫɹ. Ɂɧɚɱɟɧɢɹ ɢɧɞɟɤɫɚ ɜɫɟɝɞɚ ɫɨɯɪɚɧɹɸɬɫɹ ɜ ɩɨɪɹɞɤɟ ɜɨɡɪɚɫɬɚɧɢɹ.
ɂɧɞɟɤɫɵ FULLTEXT ɦɨɝɭɬ ɢɧɞɟɤɫɢɪɨɜɚɬɶ ɬɨɥɶɤɨ ɫɬɨɥɛɰɵ ɬɢɩɨɜ CHAR, VARCHAR ɢ TEXT,
ɢ ɬɨɥɶɤɨ ɜ ɬɚɛɥɢɰɚɯ MyISAM. ɗɬɢ ɢɧɞɟɤɫɵ ɞɨɫɬɭɩɧɵ ɜ ɜɟɪɫɢɢ MySQL 3.23.23 ɢ ɜɵɲɟ. ɋɦ.
ɪɚɡɞɟɥ 5.6.
ɂɧɞɟɤɫɵ SPATIAL ɦɨɝɭɬ ɢɧɞɟɤɫɢɪɨɜɚɬɶ ɬɨɥɶɤɨ ɩɪɨɫɬɪɚɧɫɬɜɟɧɧɵɟ (spatial) ɫɬɨɥɛɰɵ, ɢ
ɬɨɥɶɤɨ ɜ ɬɚɛɥɢɰɚɯ MyISAM. ɂɧɞɟɤɫɵ SPATIAL ɞɨɫɬɭɩɧɵ ɜ ɜɟɪɫɢɢ MySQL 4.1 ɢ ɜɵɲɟ. ɉɪɨɫɬɪɚɧɫɬɜɟɧɧɵɟ ɬɢɩɵ ɫɬɨɥɛɰɨɜ ɨɩɢɫɚɧɵ ɜ ɝɥɚɜɟ 7.
6.2.5. Ñèíòàêñèñ CREATE TABLE
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] ɢɦɹ_ɬɚɛɥɢɰɵ
[(ɨɩɪɟɞɟɥɟɧɢɟ_create,...)]
[ɨɩɰɢɢ_ɬɚɛɥɢɰɵ] [ɨɩɟɪɚɬɨɪ_select]
ɢɥɢ
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] ɢɦɹ_ɬɚɛɥɢɰɵ
[(] LIKE ɫɬɚɪɨɟ_ɢɦɹ_ɬɚɛɥɢɰɵ [)];
ɨɩɪɟɞɟɥɟɧɢɟ_create:
ɨɩɪɟɞɟɥɟɧɢɟ_ɫɬɨɥɛɰɚ
| [CONSTRAINT [ɫɢɦɜɨɥ]] PRIMARY KEY [ɬɢɩ_ɢɧɞɟɤɫɚ]
(ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...)
| KEY [ɢɦɹ_ɢɧɞɟɤɫɚ] [ɬɢɩ_ɢɧɞɟɤɫɚ] (ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...)
| INDEX [ɢɦɹ_ɢɧɞɟɤɫɚ] [ɬɢɩ_ɢɧɞɟɤɫɚ] (ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...)
| [CONSTRAINT [ɫɢɦɜɨɥ]] UNIQUE [INDEX]
[ɢɦɹ_ɢɧɞɟɤɫɚ] [ɬɢɩ_ɢɧɞɟɤɫɚ] (ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...)
| [FULLTEXT|SPATIAL] [INDEX] [index_name] (ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...)
| [CONSTRAINT [ɫɢɦɜɨɥ]] FOREIGN KEY
262
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
[ɢɦɹ_ɢɧɞɟɤɫɚ] (ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...) [ɨɩɪɟɞɟɥɟɧɢɟ_ɫɫɵɥɤɢ]
| CHECK (ɜɵɪɚɠɟɧɢɟ)
ɨɩɪɟɞɟɥɟɧɢɟ_ɫɬɨɥɛɰɚ:
ɢɦɹ_ɫɬɨɥɛɰɚ ɬɢɩ [NOT NULL | NULL] [DEFAULT ɡɧɚɱɟɧɢɟ_ɩɨ_ɭɦɨɥɱɚɧɢɸ]
[AUTO_INCREMENT] [[PRIMARY] KEY] [COMMENT 'ɫɬɪɨɤɚ']
[ɨɩɪɟɞɟɥɟɧɢɟ_ɫɫɵɥɤɢ]
ɬɢɩ:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TINYINT[(ɞɥɢɧɚ)] [UNSIGNED] [ZEROFILL]
SMALLINT[(ɞɥɢɧɚ)] [UNSIGNED] [ZEROFILL]
MEDIUMINT[(ɞɥɢɧɚ)] [UNSIGNED] [ZEROFILL]
INT[(ɞɥɢɧɚ)] [UNSIGNED] [ZEROFILL]
INTEGER[(ɞɥɢɧɚ)] [UNSIGNED] [ZEROFILL]
BIGINT[(ɞɥɢɧɚ)] [UNSIGNED] [ZEROFILL]
REAL[(ɞɥɢɧɚ,ɰɢɮɪ_ɩɨɫɥɟ_ɬɨɱɤɢ)] [UNSIGNED] [ZEROFILL]
DOUBLE[(ɞɥɢɧɚ,ɰɢɮɪ_ɩɨɫɥɟ_ɬɨɱɤɢ)] [UNSIGNED] [ZEROFILL]
FLOAT[(ɞɥɢɧɚ,ɰɢɮɪ_ɩɨɫɥɟ_ɬɨɱɤɢ)] [UNSIGNED] [ZEROFILL]
DECIMAL(ɞɥɢɧɚ,ɰɢɮɪ_ɩɨɫɥɟ_ɬɨɱɤɢ) [UNSIGNED] [ZEROFILL]
NUMERIC(ɞɥɢɧɚ,ɰɢɮɪ_ɩɨɫɥɟ_ɬɨɱɤɢ) [UNSIGNED] [ZEROFILL]
DATE
TIME
TIMESTAMP
DATETIME
CHAR(ɞɥɢɧɚ) [BINARY | ASCII | UNICODE]
VARCHAR(ɞɥɢɧɚ) [BINARY]
TINYBLOB
BLOB
MEDIUMBLOB
LONGBLOB
TINYTEXT
TEXT
MEDIUMTEXT
LONGTEXT
ENUM(ɡɧɚɱɟɧɢɟ1,ɡɧɚɱɟɧɢɟ2,ɡɧɚɱɟɧɢɟ3,...)
SET(ɡɧɚɱɟɧɢɟ1,ɡɧɚɱɟɧɢɟ2,ɡɧɚɱɟɧɢɟ3,...)
ɩɪɨɫɬɪɚɧɫɬɜɟɧɧɵɣ_ɬɢɩ
ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ:
ɢɦɹ_ɫɬɨɥɛɰɚ [(ɞɥɢɧɚ)] [ASC | DESC]
ɨɩɪɟɞɟɥɟɧɢɟ_ɫɫɵɥɤɢ:
REFERENCES ɢɦɹ_ɬɚɛɥɢɰɵ [(ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...)]
[MATCH FULL | MATCH PARTIAL]
[ON DELETE ɨɩɰɢɹ_ɫɫɵɥɤɢ]
[ON UPDATE ɨɩɰɢɹ_ɫɫɵɥɤɢ]
ɨɩɰɢɹ_ɫɫɵɥɤɢ:
RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT
ɨɩɰɢɢ_ɬɚɛɥɢɰɵ: ɨɩɰɢɹ_ɬɚɛɥɢɰɵ [ɨɩɰɢɹ_ɬɚɛɥɢɰɵ] ...
ɨɩɰɢɹ_ɬɚɛɥɢɰɵ:
{ENGINE|TYPE} = {BDB|HEAP|ISAM|InnoDB|MERGE|MRG_MYISAM|MYISAM}
| AUTO_INCREMENT = ɡɧɚɱɟɧɢɟ
| AVG_ROW_LENGTH = ɡɧɚɱɟɧɢɟ
| CHECKSUM = {0 | 1}
6.2. Îïåðàòîðû îïðåäåëåíèÿ äàííûõ
|
|
|
|
|
|
|
|
|
|
|
|
|
263
COMMENT = 'ɫɬɪɨɤɚ'
MAX_ROWS = ɡɧɚɱɟɧɢɟ
MIN_ROWS = ɡɧɚɱɟɧɢɟ
PACK_KEYS = {0 | 1 | DEFAULT}
PASSWORD = 'ɫɬɪɨɤɚ'
DELAY_KEY_WRITE = {0 | 1}
ROW_FORMAT = { DEFAULT | DYNAMIC | FIXED | COMPRESSED }
RAID_TYPE = { 1 | STRIPED | RAID0 }
RAID_CHUNKS = ɡɧɚɱɟɧɢɟ
RAID_CHUNKSIZE = ɡɧɚɱɟɧɢɟ
UNION = (ɢɦɹ_ɬɚɛɥɢɰɵ[,ɢɦɹ_ɬɚɛɥɢɰɵ]...)
INSERT_METHOD = { NO | FIRST | LAST }
DATA DIRECTORY = 'ɚɛɫɨɥɸɬɧɵɣ ɩɭɬɶ ɤ ɤɚɬɚɥɨɝɭ'
INDEX DIRECTORY = 'ɚɛɫɨɥɸɬɧɵɣ ɩɭɬɶ ɤ ɤɚɬɚɥɨɝɭ'
[DEFAULT] CHARACTER SET ɢɦɹ_ɧɚɛɨɪɚ_ɫɢɦɜɨɥɨɜ
[COLLATE ɢɦɹ_ɩɨɪɹɞɤɚ_ɫɨɩɨɫɬɚɜɥɟɧɢɹ]
ɨɩɟɪɚɬɨɪ_select:
[IGNORE | REPLACE] [AS] SELECT ... (ɥɸɛɨɣ ɪɚɡɪɟɲɟɧɧɵɣ ɨɩɟɪɚɬɨɪ select)
CREATE TABLE ɫɨɡɞɚɟɬ ɬɚɛɥɢɰɭ ɫ ɭɤɚɡɚɧɧɵɦ ɢɦɟɧɟɦ. ɇɟɨɛɯɨɞɢɦɨ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ
CREATE ɞɥɹ ɬɚɛɥɢɰ.
ɉɪɚɜɢɥɚ ɢɦɟɧɨɜɚɧɢɹ ɬɚɛɥɢɰ ɨɩɢɫɚɧɵ ɜ ɪɚɡɞɟɥɟ 2.2. ɉɨ ɭɦɨɥɱɚɧɢɸ ɬɚɛɥɢɰɚ ɫɨɡɞɚɟɬɫɹ
ɜ ɬɟɤɭɳɟɣ ɛɚɡɟ ɞɚɧɧɵɯ. ȿɫɥɢ ɬɚɛɥɢɰɚ ɭɠɟ ɫɭɳɟɫɬɜɭɟɬ, ɟɫɥɢ ɧɟɬ ɬɟɤɭɳɟɣ ɛɚɡɵ ɞɚɧɧɵɯ
ɢɥɢ ɟɫɥɢ ɜɨɨɛɳɟ ɧɟɬ ɛɚɡɵ ɞɚɧɧɵɯ, ɝɟɧɟɪɢɪɭɟɬɫɹ ɨɲɢɛɤɚ.
ȼ MySQL 3.22 ɢ ɛɨɥɟɟ ɩɨɡɞɧɢɯ ɜɟɪɫɢɹɯ ɢɦɹ ɬɚɛɥɢɰɵ ɦɨɠɟɬ ɫɩɟɰɢɮɢɰɢɪɨɜɚɬɶɫɹ ɤɚɤ
ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.ɢɦɹ_ɬɚɛɥɢɰɵ, ɱɬɨɛɵ ɫɨɡɞɚɬɶ ɬɚɛɥɢɰɭ ɜ ɭɤɚɡɚɧɧɨɣ ɛɚɡɟ ɞɚɧɧɵɯ. ɗɬɨ
ɪɚɛɨɬɚɟɬ ɧɟɡɚɜɢɫɢɦɨ ɨɬ ɬɨɝɨ, ɟɫɬɶ ɛɚɡɚ ɞɚɧɧɵɯ ɩɨ ɭɦɨɥɱɚɧɢɸ ɢɥɢ ɧɟɬ. ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ɢɞɟɧɬɢɮɢɤɚɬɨɪɵ ɜ ɤɚɜɵɱɤɚɯ, ɬɨ ɛɟɪɢɬɟ ɜ ɤɚɜɵɱɤɢ ɢɦɹ ɛɚɡɵ ɢ ɢɦɹ ɬɚɛɥɢɰɵ ɨɬɞɟɥɶɧɨ. ɇɚɩɪɢɦɟɪ, 'mydb'.'mytbl' – ɩɪɚɜɢɥɶɧɨ, ɚ 'mydb.mytbl' – ɧɟɬ.
ɇɚɱɢɧɚɹ ɫ MySQL 3.23 ɩɪɢ ɫɨɡɞɚɧɢɢ ɬɚɛɥɢɰɵ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ
TEMPORARY. ȼɪɟɦɟɧɧɚɹ ɬɚɛɥɢɰɚ ɜɢɞɢɦɚ ɬɨɥɶɤɨ ɞɥɹ ɬɟɤɭɳɟɝɨ ɫɟɚɧɫɚ ɢ ɭɧɢɱɬɨɠɚɟɬɫɹ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɩɪɢ ɡɚɤɪɵɬɢɢ ɫɨɟɞɢɧɟɧɢɹ. ɗɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɞɜɚ ɪɚɡɧɵɯ ɩɨɞɤɥɸɱɟɧɢɹ ɦɨɝɭɬ ɢɫɩɨɥɶɡɨɜɚɬɶ ɨɞɢɧɚɤɨɜɨɟ ɢɦɹ ɜɪɟɦɟɧɧɨɣ ɬɚɛɥɢɰɵ, ɧɟ ɤɨɧɮɥɢɤɬɭɹ ɞɪɭɝ ɫ ɞɪɭɝɨɦ ɢ ɫ
ɫɭɳɟɫɬɜɭɸɳɟɣ ɩɨɫɬɨɹɧɧɨɣ ɬɚɛɥɢɰɟɣ ɫ ɬɟɦ ɠɟ ɢɦɟɧɟɦ. (ɉɨɫɬɨɹɧɧɚɹ ɬɚɛɥɢɰɚ ɨɫɬɚɟɬɫɹ
ɫɤɪɵɬɨɣ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɧɟ ɛɭɞɟɬ ɭɞɚɥɟɧɚ ɜɪɟɦɟɧɧɚɹ.) ɇɚɱɢɧɚɹ ɫ MySQL 4.0.2, ɞɥɹ ɫɨɡɞɚɧɢɹ ɜɪɟɦɟɧɧɵɯ ɬɚɛɥɢɰ ɧɭɠɧɨ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ CREATE TEMPORARY TABLES.
ȼ MySQL 3.23 ɢ ɛɨɥɟɟ ɩɨɡɞɧɢɯ ɜɟɪɫɢɹɯ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɤɥɸɱɟɜɵɟ ɫɥɨɜɚ IF NOT
EXISTS, ɩɪɢ ɷɬɨɦ, ɟɫɥɢ ɬɚɛɥɢɰɵ ɫɭɳɟɫɬɜɨɜɚɥɚ, ɨɲɢɛɤɚ ɧɟ ɝɟɧɟɪɢɪɭɟɬɫɹ. Ɉɬɦɟɬɢɦ, ɱɬɨ
ɧɢɤɚɤɨɣ ɩɪɨɜɟɪɤɢ, ɱɬɨɛɵ ɫɭɳɟɫɬɜɭɸɳɚɹ ɬɚɛɥɢɰɚ ɢɦɟɥɚ ɫɬɪɭɤɬɭɪɭ, ɢɞɟɧɬɢɱɧɭɸ ɬɨɣ, ɱɬɨ
ɡɚɞɚɧɚ ɜ ɨɩɟɪɚɬɨɪɟ CREATE TABLE, ɧɟ ɩɪɨɢɡɜɨɞɢɬɫɹ.
MySQL ɩɪɟɞɫɬɚɜɥɹɟɬ ɤɚɠɞɭɸ ɬɚɛɥɢɰɭ ɮɚɣɥɨɦ ɮɨɪɦɚɬɚ ɬɚɛɥɢɰɵ .frm, ɪɚɫɩɨɥɨɠɟɧɧɨɦ ɜ ɤɚɬɚɥɨɝɟ ɛɚɡɵ ɞɚɧɧɵɯ. Ɇɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ ɦɨɠɟɬ ɫɨɡɞɚɜɚɬɶ ɞɥɹ ɬɚɛɥɢɰɵ ɢ ɞɪɭɝɢɟ
ɮɚɣɥɵ. ȼ ɫɥɭɱɚɟ ɬɚɛɥɢɰɵ MyISAM ɦɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ ɫɨɡɞɚɟɬ ɬɪɢ ɮɚɣɥɚ ɞɥɹ ɬɚɛɥɢɰɵ ɩɨ
ɢɦɟɧɢ ɢɦɹ_ɬɚɛɥɢɰɵ:
Ɏɚɣɥ
ɇɚɡɧɚɱɟɧɢɟ
ɢɦɹ_ɬɚɛɥɢɰɵ.frm
Ɏɚɣɥ ɨɩɢɫɚɧɢɹ ɮɨɪɦɚɬɚ ɬɚɛɥɢɰɵ.
ɢɦɹ_ɬɚɛɥɢɰɵ.MYD
Ɏɚɣɥ ɞɚɧɧɵɯ.
ɢɦɹ_ɬɚɛɥɢɰɵ.MYI
ɂɧɞɟɤɫɧɵɣ ɮɚɣɥ.
264
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
Ɏɚɣɥɵ, ɫɨɡɞɚɜɚɟɦɵɟ ɤɚɠɞɵɦ ɦɟɯɚɧɢɡɦɨɦ ɯɪɚɧɟɧɢɹ, ɨɩɢɫɚɧɵ ɜ ɤɧɢɝɟ MySQL. Ɋɭɤɨɜɨɞɫɬɜɨ ɚɞɦɢɧɢɫɬɪɚɬɨɪɚ (Ɇ. : ɂɡɞɚɬɟɥɶɫɤɢɣ ɞɨɦ “ȼɢɥɶɹɦɫ”, 2005, ISBN 5-8459-0805-1).
Ɉɛɳɚɹ ɢɧɮɨɪɦɚɰɢɹ ɨ ɫɜɨɣɫɬɜɚɯ ɪɚɡɥɢɱɧɵɯ ɬɢɩɨɜ ɫɬɨɥɛɰɨɜ ɩɪɟɞɫɬɚɜɥɟɧɚ ɜ ɝɥɚɜɟ 4.
ɂɧɮɨɪɦɚɰɢɸ ɨ ɩɪɨɫɬɪɚɧɫɬɜɟɧɧɵɯ ɬɢɩɚɯ ɦɨɠɧɨ ɧɚɣɬɢ ɜ ɝɥɚɜɟ 7.
„ȿɫɥɢ ɞɥɹ ɫɬɨɥɛɰɚ ɧɟ ɭɤɚɡɚɧɨ ɧɢ NULL, ɧɢ NOT NULL, ɩɪɢɧɢɦɚɟɬɫɹ NULL.
ɰɟɥɨɱɢɫɥɟɧɧɵɯ ɬɢɩɨɜ ɦɨɝɭɬ ɢɦɟɬɶ ɚɬɪɢɛɭɬ AUTO_INCREMENT. Ʉɨɝɞɚ
ɜɫɬɚɜɥɹɟɬɫɹ ɡɧɚɱɟɧɢɟ NULL (ɪɟɤɨɦɟɧɞɭɟɬɫɹ) ɢɥɢ 0 ɜ ɢɧɞɟɤɫɢɪɨɜɚɧɧɵɣ ɫɬɨɥɛɟɰ
AUTO_INCREMENT, ɟɦɭ ɩɪɢɫɜɚɢɜɚɟɬɫɹ ɫɥɟɞɭɸɳɟɟ ɡɧɚɱɟɧɢɟ ɢɡ ɱɢɫɥɨɜɨɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ. Ɉɛɵɱɧɨ ɷɬɨ ɛɭɞɟɬ ɡɧɚɱɟɧɢɟ + 1, ɝɞɟ ɡɧɚɱɟɧɢɟ – ɧɚɢɛɨɥɶɲɟɟ ɡɧɚɱɟɧɢɟ
ɷɬɨɝɨ ɫɬɨɥɛɰɚ, ɢɦɟɸɳɟɟɫɹ ɜ ɬɚɛɥɢɰɟ ɧɚ ɷɬɨɬ ɦɨɦɟɧɬ. ɑɢɫɥɨɜɵɟ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ AUTO_INCREMENT ɧɚɱɢɧɚɸɬɫɹ ɫ 1.
ɇɚɱɢɧɚɹ ɫ MySQL 4.1.1, ɭɤɚɡɚɧɢɟ ɮɥɚɝɚ NO_AUTO_VALUE_ON_ZERO ɞɥɹ ɨɩɰɢɢ ɫɟɪɜɟɪɚ --sql-mode ɥɢɛɨ ɞɥɹ ɫɢɫɬɟɦɧɨɣ ɩɟɪɟɦɟɧɧɨɣ sql_mode ɩɨɡɜɨɥɹɟɬ ɫɨɯɪɚɧɹɬɶ 0 ɜ
ɫɬɨɥɛɰɟ AUTO_INCREMENT ɛɟɡ ɝɟɧɟɪɚɰɢɢ ɫɥɟɞɭɸɳɟɝɨ ɡɧɚɱɟɧɢɹ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ.
„ɋɬɨɥɛɰɵ
oË ÑËÕÏÛµÂ!
ȼ ɬɚɛɥɢɰɟ ɦɨɠɟɬ ɛɵɬɶ ɬɨɥɶɤɨ ɨɞɢɧ ɫɬɨɥɛɟɰ AUTO_INCREMENT, ɨɧ ɞɨɥɠɟɧ ɛɵɬɶ ɩɪɨɢɧɞɟɤɫɢɪɨɜɚɧ
ɢ ɨɧ ɧɟ ɦɨɠɟɬ ɢɦɟɬɶ ɡɧɚɱɟɧɢɹ ɩɨ ɭɦɨɥɱɚɧɢɸ. ɇɚɱɢɧɚɹ ɫ MySQL 3.23, ɫɬɨɥɛɟɰ AUTO_INCREMENT
ɪɚɛɨɬɚɟɬ ɩɪɚɜɢɥɶɧɨ, ɬɨɥɶɤɨ ɟɫɥɢ ɫɨɞɟɪɠɢɬ ɩɨɥɨɠɢɬɟɥɶɧɵɟ ɡɧɚɱɟɧɢɹ. ȼɫɬɚɜɤɚ ɨɬɪɢɰɚɬɟɥɶɧɨ ɡɧɚɱɟɧɢɹ ɢɧɬɟɪɩɪɟɬɢɪɭɟɬɫɹ ɤɚɤ ɜɫɬɚɜɤɚ ɨɱɟɧɶ ɛɨɥɶɲɨɝɨ ɩɨɥɨɠɢɬɟɥɶɧɨɝɨ. ɗɬɨ ɫɞɟɥɚɧɨ ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɢɡɛɟɠɚɬɶ ɩɪɨɛɥɟɦ ɬɨɱɧɨɫɬɢ, ɤɨɝɞɚ ɱɢɫɥɨ “ɡɚɜɨɪɚɱɢɜɚɟɬɫɹ” ɢɡ ɩɨɥɨɠɢɬɟɥɶɧɨɝɨ ɜ ɨɬɪɢɰɚɬɟɥɶɧɨɟ, ɚ ɬɚɤɠɟ ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɝɚɪɚɧɬɢɪɨɜɚɬɶ, ɱɬɨ ɜɵ ɧɢɤɨɝɞɚ ɫɥɭɱɚɣɧɨ ɧɟ ɩɨɥɭɱɢɬɟ ɡɧɚɱɟɧɢɟ 0 ɜ
ɫɬɨɥɛɰɟ AUTO_INCREMENT.
Ⱦɥɹ ɬɚɛɥɢɰ MyISAM ɢ BDB ɦɨɠɧɨ ɨɩɪɟɞɟɥɢɬɶ ɞɨɩɨɥɧɢɬɟɥɶɧɵɣ ɫɬɨɥɛɟɰ
AUTO_INCREMENT ɜ ɫɨɫɬɚɜɧɨɦ ɤɥɸɱɟ.
ɑɬɨɛɵ ɨɛɟɫɩɟɱɢɬɶ ɫɨɜɦɟɫɬɢɦɨɫɬɶ MySQL ɫ ɧɟɤɨɬɨɪɵɦɢ ODBC-ɩɪɢɥɨɠɟɧɢɹɦɢ,
ɜɵ ɦɨɠɟɬɟ ɩɨɥɭɱɢɬɶ ɩɨɫɥɟɞɧɟɟ ɡɧɚɱɟɧɢɟ AUTO_INCREMENT, ɜɫɬɚɜɥɟɧɧɨɟ ɜ ɫɬɪɨɤɭ, ɫ
ɩɨɦɨɳɶɸ ɫɥɟɞɭɸɳɟɝɨ ɡɚɩɪɨɫɚ:
SELECT * FROM ɢɦɹ_ɬɚɛɥɢɰɵ WHERE ɫɬɨɥɛɟɰ_AUTO_INCREMENT IS NULL
ɫ MySQL 4.1, ɨɩɪɟɞɟɥɟɧɢɹ ɫɢɦɜɨɥɶɧɵɯ ɫɬɨɥɛɰɨɜ ɦɨɝɭɬ ɫɨɞɟɪɠɚɬɶ ɚɬɪɢɛɭɬ CHARACTER SET ɞɥɹ ɭɤɚɡɚɧɢɹ ɧɚɛɨɪɚ ɫɢɦɜɨɥɨɜ ɞɥɹ ɫɬɨɥɛɰɚ ɢ, ɧɟɨɛɹɡɚɬɟɥɶɧɨ,
ɩɨɪɹɞɤɚ ɫɨɩɨɫɬɚɜɥɟɧɢɹ. ɉɨɞɪɨɛɧɨɫɬɢ ɫɦ. ɜ ɝɥɚɜɟ 3.
„ɇɚɱɢɧɚɹ
CREATE TABLE t (c CHAR(20) CHARACTER SET utf8 COLLATE utf8_bin);
Ʉɪɨɦɟ ɬɨɝɨ, MySQL 4.1 ɢɧɬɟɪɩɪɟɬɢɪɭɟɬ ɫɩɟɰɢɮɢɤɚɰɢɸ ɞɥɢɧɵ ɫɢɦɜɨɥɶɧɵɯ
ɫɬɨɥɛɰɨɜ ɜ ɫɢɦɜɨɥɚɯ (ɪɚɧɧɢɟ ɜɟɪɫɢɢ ɢɧɬɟɪɩɪɟɬɢɪɨɜɚɥɢ ɟɟ ɜ ɛɚɣɬɚɯ).
„Ɂɧɚɱɟɧɢɟ NULL ɞɥɹ ɫɬɨɥɛɰɨɜ ɬɢɩɚ TIMESTAMP ɨɛɪɚɛɚɬɵɜɚɟɬɫɹ ɢɧɵɦ ɨɛɪɚɡɨɦ, ɱɟɦ
ɞɥɹ ɞɪɭɝɢɯ ɬɢɩɨɜ ɫɬɨɥɛɰɨɜ. ȼɵ ɧɟ ɦɨɠɟɬɟ ɜɫɬɚɜɥɹɬɶ ɥɢɬɟɪɚɥɶɧɵɣ NULL ɜ ɫɬɨɥɛɰɵ
TIMESTAMP. ɉɪɢɫɜɨɟɧɢɟ ɡɧɚɱɟɧɢɹ NULL ɬɚɤɢɦ ɫɬɨɥɛɰɚɦ ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɢɯ ɜ ɬɟɤɭɳɭɸ ɞɚɬɭ ɢ ɜɪɟɦɹ. ɉɨɫɤɨɥɶɤɭ ɫɬɨɥɛɰɵ TIMESTAMP ɜɟɞɭɬ ɫɟɛɹ ɩɨɞɨɛɧɵɦ ɨɛɪɚɡɨɦ,
ɚɬɪɢɛɭɬɵ NULL ɢ NOT NULL ɞɥɹ ɷɬɢɯ ɫɬɨɥɛɰɨɜ ɢɝɧɨɪɢɪɭɸɬɫɹ.
ɋ ɞɪɭɝɨɣ ɫɬɨɪɨɧɵ, ɱɬɨɛɵ ɨɛɥɟɝɱɢɬɶ ɤɥɢɟɧɬɚɦ MySQL ɩɪɢɦɟɧɟɧɢɟ ɫɬɨɥɛɰɨɜ ɬɢɩɚ
TIMESTAMP, ɫɟɪɜɟɪ ɫɨɨɛɳɚɟɬ, ɱɬɨ ɷɬɢɦ ɫɬɨɥɛɰɚɦ ɦɨɠɧɨ ɩɪɢɫɜɚɢɜɚɬɶ ɡɧɚɱɟɧɢɟ NULL
(ɱɬɨ ɢɫɬɢɧɧɨ), ɞɚɠɟ ɧɟɫɦɨɬɪɹ ɧɚ ɬɨ, ɱɬɨ ɫɬɨɥɛɰɵ ɷɬɨɝɨ ɬɢɩɚ ɧɢɤɨɝɞɚ ɧɟ ɫɨɞɟɪɠɚɬ
ɬɚɤɨɝɨ ɡɧɚɱɟɧɢɹ. ɗɬɨ ɦɨɠɧɨ ɭɜɢɞɟɬɶ, ɜɨɫɩɨɥɶɡɨɜɚɜɲɢɫɶ DESCRIBE ɢɦɹ_ɬɚɛɥɢɰɵ
ɞɥɹ ɩɨɥɭɱɟɧɢɹ ɢɧɮɨɪɦɚɰɢɢ ɨ ɫɬɪɭɤɬɭɪɟ ɬɚɛɥɢɰɵ.
6.2. Îïåðàòîðû îïðåäåëåíèÿ äàííûõ
265
Ɉɬɦɟɬɢɦ, ɱɬɨ ɩɪɢɫɜɨɟɧɢɟ ɫɬɨɥɛɰɭ TIMESTAMP ɡɧɚɱɟɧɢɹ 0 – ɷɬɨ ɧɟ ɬɨ ɠɟ ɫɚɦɨɟ, ɱɬɨ
ɩɪɢɫɜɨɟɧɢɟ NULL, ɩɨɫɤɨɥɶɤɭ 0 ɹɜɥɹɟɬɫɹ ɞɨɩɭɫɬɢɦɵɦ ɡɧɚɱɟɧɢɟɦ TIMESTAMP.
„Ɂɧɚɱɟɧɢɟ DEFAULT
ɞɨɥɠɧɨ ɛɵɬɶ ɤɨɧɫɬɚɧɬɨɣ, ɨɧɨ ɧɟ ɦɨɠɟɬ ɛɵɬɶ ɮɭɧɤɰɢɟɣ ɢɥɢ ɜɵɪɚɠɟɧɢɟɦ. ɗɬɨ ɨɡɧɚɱɚɟɬ, ɧɚɩɪɢɦɟɪ, ɱɬɨ ɧɟɥɶɡɹ ɭɫɬɚɧɨɜɢɬɶ ɜ ɤɚɱɟɫɬɜɟ ɡɧɚɱɟɧɢɹ ɩɨ
ɭɦɨɥɱɚɧɢɸ ɞɥɹ ɫɬɨɥɛɰɚ ɬɢɩɚ ɞɚɬɵ ɮɭɧɤɰɢɸ NOW() ɢɥɢ CURRENT_DATE. ȿɫɥɢ ɧɢɤɚɤɨɝɨ ɡɧɚɱɟɧɢɹ DEFAULT ɞɥɹ ɫɬɨɥɛɰɚ ɧɟ ɭɤɚɡɚɧɨ, ɬɨ MySQL ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɩɪɢɫɜɚɢɜɚɟɬ ɫɜɨɟ, ɤɚɤ ɨɩɢɫɚɧɨ ɧɢɠɟ.
ȿɫɥɢ ɫɬɨɥɛɟɰ ɞɨɩɭɫɤɚɟɬ NULL, ɡɧɚɱɟɧɢɟɦ ɩɨ ɭɦɨɥɱɚɧɢɸ ɫɬɚɧɨɜɢɬɫɹ NULL.
ȿɫɥɢ ɫɬɨɥɛɟɰ ɨɛɴɹɜɥɟɧ ɤɚɤ NOT NULL, ɡɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ ɡɚɜɢɫɢɬ ɨɬ ɬɢɩɚ:
x Ⱦɥɹ ɱɢɫɥɨɜɵɯ ɬɢɩɨɜ, ɤɪɨɦɟ ɬɟɯ, ɱɬɨ ɢɦɟɸɬ ɚɬɪɢɛɭɬ AUTO_INCREMENT, ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ 0. Ⱦɥɹ ɫɬɨɥɛɰɨɜ AUTO_INCREMENT ɡɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ – ɷɬɨ ɫɥɟɞɭɸɳɟɟ ɡɧɚɱɟɧɢɟ ɜ ɱɢɫɥɨɜɨɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ.
x Ⱦɥɹ ɬɢɩɨɜ ɞɚɬ ɢ ɜɪɟɦɟɧɢ, ɤɪɨɦɟ TIMESTAMP, ɭɦɨɥɱɚɧɢɟɦ ɛɭɞɟɬ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɟ “ɧɭɥɟɜɨɟ” ɡɧɚɱɟɧɢɟ ɞɚɧɧɨɝɨ ɬɢɩɚ. Ⱦɥɹ ɩɟɪɜɨɝɨ ɫɬɨɥɛɰɚ TIMESTAMP ɜ ɬɚɛɥɢɰɟ ɡɧɚɱɟɧɢɟɦ ɩɨ ɭɦɨɥɱɚɧɢɸ ɹɜɥɹɟɬɫɹ ɬɟɤɭɳɚɹ ɞɚɬɚ ɢ ɜɪɟɦɹ. ɋɦ. ɪɚɡɞɟɥ 4.3.
x Ⱦɥɹ ɫɬɪɨɤɨɜɵɯ ɬɢɩɨɜ, ɤɪɨɦɟ ENUM, ɡɧɚɱɟɧɢɟɦ ɩɨ ɭɦɨɥɱɚɧɢɸ ɹɜɥɹɟɬɫɹ ɩɭɫɬɚɹ
ɫɬɪɨɤɚ. Ⱦɥɹ ENUM ɡɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ – ɩɟɪɜɨɟ ɜ ɩɟɪɟɱɢɫɥɟɧɢɢ.
ɋɬɨɥɛɰɚɦ ɬɢɩɚ BLOB ɢ TEXT ɧɟ ɦɨɠɟɬ ɛɵɬɶ ɧɚɡɧɚɱɟɧɨ ɡɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ.
ɞɥɹ ɫɬɨɥɛɰɚ ɦɨɠɟɬ ɛɵɬɶ ɭɤɚɡɚɧ ɜ ɨɩɰɢɢ COMMENT. Ʉɨɦɦɟɧɬɚɪɢɣ ɨɬɨɛɪɚɠɚɟɬɫɹ ɨɩɟɪɚɬɨɪɚɦɢ SHOW CREATE TABLE ɢ SHOW FULL COLUMNS. ɗɬɚ ɨɩɰɢɹ ɞɟɣɫɬɜɢɬɟɥɶɧɚ, ɧɚɱɢɧɚɹ ɫ MySQL 4.1 (ɜ ɛɨɥɟɟ ɪɚɧɧɢɯ ɜɟɪɫɢɹɯ ɨɧɚ ɪɚɡɪɟɲɚɥɚɫɶ, ɧɨ
ɢɝɧɨɪɢɪɨɜɚɥɚɫɶ).
„ɇɚɱɢɧɚɹ ɫ MySQL 4.1.0, ɚɬɪɢɛɭɬ SERIAL ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɜ ɤɚɱɟɫɬɜɟ ɩɫɟɜɞɨɧɢɦɚ ɞɥɹ BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE. ɗɬɨ ɫɪɟɞɫɬɜɨ ɨɛɟɫɩɟɱɟɧɢɹ ɫɨɜɦɟɫɬɢɦɨɫɬɢ.
„Ɉɛɵɱɧɨ KEY – ɫɢɧɨɧɢɦ ɞɥɹ INDEX. ɇɚɱɢɧɚɹ ɫ MySQL 4.1, ɤɥɸɱɟɜɨɣ ɚɬɪɢɛɭɬ
PRIMARY KEY ɬɚɤɠɟ ɦɨɠɟɬ ɛɵɬɶ ɭɤɚɡɚɧ ɞɥɹ ɫɬɨɥɛɰɨɜ, ɤɚɤ ɢ ɩɪɨɫɬɨ KEY. ɗɬɨ ɛɵɥɨ
ɜɜɟɞɟɧɨ ɞɥɹ ɫɨɜɦɟɫɬɢɦɨɫɬɢ ɫ ɞɪɭɝɢɦɢ ɫɢɫɬɟɦɚɦɢ ɭɩɪɚɜɥɟɧɢɹ ɛɚɡɚɦɢ ɞɚɧɧɵɯ.
„ȼ MySQL ɢɧɞɟɤɫ UNIQUE – ɷɬɨ ɬɚɤɨɣ ɢɧɞɟɤɫ, ɜ ɤɨɬɨɪɨɦ ɜɫɟ ɡɧɚɱɟɧɢɹ ɤɥɸɱɚ ɞɨɥɠɧɵ ɛɵɬɶ ɭɧɢɤɚɥɶɧɵɦɢ. ɉɪɢ ɩɨɩɵɬɤɟ ɜɫɬɚɜɢɬɶ ɫɬɪɨɤɭ, ɤɥɸɱ ɢɧɞɟɤɫɚ ɤɨɬɨɪɨɣ ɩɨɜɬɨɪɹɟɬ ɭɠɟ ɫɭɳɟɫɬɜɭɸɳɢɣ, ɝɟɧɟɪɢɪɭɟɬɫɹ ɨɲɢɛɤɚ. ɂɫɤɥɸɱɟɧɢɟɦ ɢɡ ɷɬɨɝɨ ɩɪɚɜɢɥɚ ɹɜɥɹɸɬɫɹ ɫɬɨɥɛɰɵ, ɜɯɨɞɹɳɢɟ ɜ ɤɥɸɱ ɢɧɞɟɤɫɚ ɢ ɞɨɩɭɫɤɚɸɳɢɟ ɡɧɚɱɟɧɢɹ NULL.
ɗɬɨ ɢɫɤɥɸɱɟɧɢɟ ɧɟ ɤɚɫɚɟɬɫɹ ɬɚɛɥɢɰ BDB, ɜ ɤɨɬɨɪɵɯ ɭɧɢɤɚɥɶɧɨ ɢɧɞɟɤɫɢɪɨɜɚɧɧɵɣ
ɫɬɨɥɛɟɰ ɦɨɠɟɬ ɫɨɞɟɪɠɚɬɶ ɬɨɥɶɤɨ ɨɞɢɧ NULL.
„Ʉɨɦɦɟɧɬɚɪɢɣ
ɤɥɸɱ (PRIMARY KEY) – ɷɬɨ ɭɧɢɤɚɥɶɧɵɣ KEY, ɜ ɤɨɬɨɪɨɦ ɜɫɟ ɤɥɸɱɟɜɵɟ
ɫɬɨɥɛɰɵ ɨɩɪɟɞɟɥɟɧɵ ɤɚɤ NOT NULL. ȿɫɥɢ ɨɧɢ ɧɟ ɨɛɴɹɜɥɟɧɵ ɹɜɧɨ ɤɚɤ NOT NULL,
MySQL ɫɞɟɥɚɟɬ ɷɬɨ ɧɟɹɜɧɨ (ɢ ɦɨɥɱɚ). ȿɫɥɢ ɭ ɜɚɫ ɧɟɬ ɩɟɪɜɢɱɧɨɝɨ ɤɥɸɱɚ ɜ ɬɚɛɥɢɰɟ,
ɚ ɩɪɢɥɨɠɟɧɢɟ ɟɝɨ ɬɪɟɛɭɟɬ, MySQL ɜɨɡɜɪɚɳɚɟɬ ɩɟɪɜɵɣ UNIQUE-ɢɧɞɟɤɫ, ɤɨɬɨɪɵɣ ɧɟ
ɫɨɞɟɪɠɢɬ ɜ ɤɥɸɱɟɣ NULL-ɫɬɨɥɛɰɨɜ, ɜ ɤɚɱɟɫɬɜɟ PRIMARY KEY.
„ɉɟɪɜɢɱɧɵɣ
ɫɨɡɞɚɧɧɨɣ ɬɚɛɥɢɰɟ PRIMARY KEY ɪɚɡɦɟɳɚɟɬɫɹ ɩɟɪɜɵɦ, ɡɚ ɧɢɦ ɫɥɟɞɭɸɬ ɜɫɟ ɭɧɢɤɚɥɶɧɵɟ ɢɧɞɟɤɫɵ, ɡɚɬɟɦ ɧɟɭɧɢɤɚɥɶɧɵɟ ɢɧɞɟɤɫɵ. ɗɬɨ ɩɨɦɨɝɚɟɬ ɨɩɬɢɦɢɡɚɬɨɪɭ
MySQL ɩɪɚɜɢɥɶɧɨ ɪɚɫɫɬɚɜɥɹɬɶ ɩɪɢɨɪɢɬɟɬɵ ɜ ɜɵɛɨɪɟ ɢɧɞɟɤɫɨɜ ɢ ɛɵɫɬɪɟɟ ɨɛɧɚɪɭɠɢɜɚɬɶ ɞɭɛɥɢɪɭɸɳɢɟ ɭɧɢɤɚɥɶɧɵɟ ɤɥɸɱɢ.
„ȼ
266
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
„PRIMARY KEY
ɦɨɠɟɬ ɛɵɬɶ ɢɧɞɟɤɫɨɦ ɫ ɫɨɫɬɚɜɧɵɦ ɤɥɸɱɨɦ. Ɉɞɧɚɤɨ ɜɵ ɧɟ ɦɨɠɟɬɟ ɫɨɡɞɚɬɶ ɫɨɫɬɚɜɧɨɣ ɢɧɞɟɤɫ, ɢɫɩɨɥɶɡɭɹ ɚɬɪɢɛɭɬ PRIMARY KEY ɜ ɫɩɟɰɢɮɢɤɚɰɢɢ ɫɬɨɥɛɰɚ. ȿɫɥɢ
ɷɬɨ ɫɞɟɥɚɬɶ, ɬɨɥɶɤɨ ɩɟɪɜɵɣ ɫɬɨɥɛɟɰ ɛɭɞɟɬ ɨɬɦɟɱɟɧ ɤɚɤ ɩɟɪɜɢɱɧɵɣ ɤɥɸɱ. ȼɵ ɞɨɥɠɧɵ ɢɫɩɨɥɶɡɨɜɚɬɶ ɨɬɞɟɥɶɧɭɸ ɤɨɧɫɬɪɭɤɰɢɸ PRIMARY KEY(ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,...).
ɢɥɢ UNIQUE-ɢɧɞɟɤɫ ɩɨɫɬɪɨɟɧɵ ɩɨ ɨɞɧɨɦɭ ɫɬɨɥɛɰɭ ɰɟɥɨɱɢɫɥɟɧɧɨɝɨ ɬɢɩɚ, ɜɵ ɬɚɤɠɟ ɦɨɠɟɬɟ ɫɫɵɥɚɬɶɫɹ ɧɚ ɷɬɨɬ ɫɬɨɥɛɟɰ ɜ ɨɩɟɪɚɬɨɪɚɯ SELECT ɤɚɤ ɧɚ
_rowid (ɜɜɟɞɟɧɨ ɜ MySQL 3.23.11).
„ɇɚɱɢɧɚɹ ɫ MySQL 4.1.0, ɧɟɤɨɬɨɪɵɟ ɦɟɯɚɧɢɡɦɵ ɯɪɚɧɟɧɢɹ ɩɨɡɜɨɥɹɸɬ ɫɩɟɰɢɮɢɰɢɪɨɜɚɬɶ ɬɢɩ ɢɧɞɟɤɫɚ ɩɪɢ ɟɝɨ ɫɨɡɞɚɧɢɢ. ɋɢɧɬɚɤɫɢɫ ɫɩɟɰɢɮɢɤɚɰɢɢ ɬɢɩɚ ɢɧɞɟɤɫɚ ɜɵɝɥɹɞɢɬ ɤɚɤ USING ɢɦɹ_ɬɢɩɚ. Ⱦɨɩɭɫɬɢɦɵɟ ɡɧɚɱɟɧɢɹ ɢɦɹ_ɬɢɩɚ, ɤɨɬɨɪɵɟ ɩɨɞɞɟɪɠɢɜɚɸɬɫɹ ɪɚɡɥɢɱɧɵɦɢ ɦɟɯɚɧɢɡɦɚɦɢ ɯɪɚɧɟɧɢɹ, ɩɨɤɚɡɚɧɵ ɧɢɠɟ. Ɍɚɦ, ɝɞɟ ɩɟɪɟɱɢɫɥɟɧɨ
ɧɟɫɤɨɥɶɤɨ ɬɢɩɨɜ ɢɧɞɟɤɫɨɜ, ɩɨ ɭɦɨɥɱɚɧɢɸ (ɩɪɢ ɨɬɫɭɬɫɬɜɢɢ ɫɩɟɰɢɮɢɤɚɰɢɢ ɬɢɩɚ)
ɹɜɥɹɟɬɫɹ ɩɟɪɜɵɣ ɢɡ ɧɢɯ:
„ȿɫɥɢ PRIMARY KEY
Ɇɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ
Ⱦɨɩɭɫɬɢɦɵɟ ɬɢɩɵ ɢɧɞɟɤɫɨɜ
MyISAM
BTREE
InnoDB
BTREE
MEMORY/HEAP
HASH, BTREE
ɉɪɢɦɟɪ:
CREATE TABLE lookup
(id INT, INDEX USING BTREE (id))
ENGINE = MEMORY;
ɉɪɢ ɫɩɟɰɢɮɢɤɚɰɢɢ ɬɢɩɚ ɢɧɞɟɤɫɚ ɜ ɤɚɱɟɫɬɜɟ ɫɢɧɨɧɢɦɚ USING ɢɦɹ_ɬɢɩɚ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ TYPE ɢɦɹ_ɬɢɩɚ. Ɉɞɧɚɤɨ USING – ɩɪɟɞɩɨɱɬɢɬɟɥɶɧɚɹ ɮɨɪɦɚ. Ʉɪɨɦɟ ɬɨɝɨ,
ɟɫɥɢ ɫɩɟɰɢɮɢɤɚɰɢɹ ɢɧɞɟɤɫɚ ɨɩɪɟɞɟɥɟɧɚ ɫɥɨɜɨɦ TYPE, ɬɨ ɢɦɹ ɢɧɞɟɤɫɚ ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɨɛɹɡɚɬɟɥɶɧɨ, ɬɚɤ ɤɚɤ ɜ ɨɬɥɢɱɢɟ ɨɬ USING, ɫɥɨɜɨ TYPE ɧɟ ɹɜɥɹɟɬɫɹ ɡɚɪɟɡɟɪɜɢɪɨɜɚɧɧɵɦ ɫɥɨɜɨɦ, ɢ ɩɪɢ ɨɬɫɭɬɫɬɜɢɢ ɢɦɟɧɢ ɢɧɞɟɤɫɚ ɛɭɞɟɬ ɢɧɬɟɪɩɪɟɬɢɪɨɜɚɬɶɫɹ ɤɚɤ
ɷɬɨ ɢɦɹ.
ȿɫɥɢ ɜɵ ɭɤɚɡɵɜɚɟɬɟ ɧɟɞɨɩɭɫɬɢɦɵɣ ɞɥɹ ɞɚɧɧɨɝɨ ɦɟɯɚɧɢɡɦɚ ɯɪɚɧɟɧɢɹ ɬɢɩ ɢɧɞɟɤɫɚ,
ɧɨ ɫɭɳɟɫɬɜɭɟɬ ɞɪɭɝɨɣ ɞɨɩɭɫɬɢɦɵɣ ɬɢɩ, ɤɨɬɨɪɵɣ ɦɟɯɚɧɢɡɦ ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ
ɛɟɡ ɜɥɢɹɧɢɹ ɧɚ ɪɟɡɭɥɶɬɚɬɵ ɡɚɩɪɨɫɨɜ, ɦɟɯɚɧɢɡɦ ɢɫɩɨɥɶɡɭɟɬ ɷɬɨɬ ɬɢɩ.
„Ɍɨɥɶɤɨ ɦɟɯɚɧɢɡɦɵ ɯɪɚɧɟɧɢɹ MyISAM, InnoDB, BDB ɢ (ɧɚɱɢɧɚɹ ɫ MySQL 4.0.2)
MEMORY ɩɨɞɞɟɪɠɢɜɚɸɬ ɢɧɞɟɤɫɵ ɩɨ ɫɬɨɥɛɰɚɦ, ɤɨɬɨɪɵɟ ɦɨɝɭɬ ɫɨɞɟɪɠɚɬɶ ɡɧɚɱɟɧɢɹ
NULL. ȼ ɨɫɬɚɥɶɧɵɯ ɫɥɭɱɚɹɯ ɜɵ ɞɨɥɠɧɵ ɞɟɤɥɚɪɢɪɨɜɚɬɶ ɫɬɨɥɛɰɵ, ɜɯɨɞɹɳɢɟ ɜ ɤɥɸɱ
ɢɧɞɟɤɫɚ, ɤɚɤ NOT NULL, ɢɧɚɱɟ ɜɨɡɧɢɤɧɟɬ ɨɲɢɛɤɚ.
ɫɢɧɬɚɤɫɢɫ col_name(ɞɥɢɧɚ) ɜ ɫɩɟɰɢɮɢɤɚɰɢɢ ɢɧɞɟɤɫɚ, ɜɵ ɦɨɠɟɬɟ ɫɨɡɞɚɬɶ ɢɧɞɟɤɫ, ɤɨɬɨɪɵɣ ɜ ɤɥɸɱɟ ɢɫɩɨɥɶɡɭɟɬ ɬɨɥɶɤɨ ɩɟɪɜɵɟ ɞɥɢɧɚ ɫɢɦɜɨɥɨɜ ɡɧɚɱɟɧɢɣ
ɫɬɨɥɛɰɚ CHAR ɢɥɢ VARCHAR. ɂɧɞɟɤɫɢɪɨɜɚɧɢɟ ɬɨɥɶɤɨ ɩɪɟɮɢɤɫɚ ɡɧɚɱɟɧɢɣ ɫɬɨɥɛɰɚ
ɦɨɠɟɬ ɫɭɳɟɫɬɜɟɧɧɨ ɭɦɟɧɶɲɢɬɶ ɪɚɡɦɟɪ ɢɧɞɟɤɫɧɨɝɨ ɮɚɣɥɚ.
Ɇɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ MyISAM ɢ (ɧɚɱɢɧɚɹ ɫ MySQL 4.0.14) InnoDB ɬɚɤɠɟ ɩɨɞɞɟɪɠɢɜɚɸɬ ɢɧɞɟɤɫɚɰɢɸ ɫɬɨɥɛɰɨɜ TEXT ɢ BLOB. ɉɪɢ ɢɧɞɟɤɫɚɰɢɢ ɫɬɨɥɛɰɨɜ TEXT ɢ BLOB
ɞɥɢɧɭ ɢɧɞɟɤɫɢɪɭɟɦɨɝɨ ɩɪɟɮɢɤɫɚ ɭɤɚɡɵɜɚɬɶ ɨɛɹɡɚɬɟɥɶɧɨ, ɧɚɩɪɢɦɟɪ:
„ɂɫɩɨɥɶɡɭɹ
CREATE TABLE test (blob_col BLOB, INDEX(blob_col(10)));
6.2. Îïåðàòîðû îïðåäåëåíèÿ äàííûõ
267
ɉɪɟɮɢɤɫɵ ɦɨɝɭɬ ɛɵɬɶ ɞɨ 255 ɫɢɦɜɨɥɨɜ ɞɥɢɧɨɣ (ɢɥɢ 1000 ɫɢɦɜɨɥɨɜ ɞɥɹ ɬɚɛɥɢɰ
MyISAM ɢ InnoDB ɜ MySQL ɜɟɪɫɢɢ 4.1.2 ɢ ɜɵɲɟ). ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɦɚɤɫɢɦɚɥɶɧɚɹ ɞɥɢɧɚ ɢɧɞɟɤɫɢɪɭɟɦɨɝɨ ɩɪɟɮɢɤɫɚ ɢɡɦɟɪɹɟɬɫɹ ɜ ɛɚɣɬɚɯ, ɜ ɬɨ ɜɪɟɦɹ ɤɚɤ
ɞɥɢɧɚ ɩɪɟɮɢɤɫɚ ɜ ɫɩɟɰɢɮɢɤɚɰɢɢ CREATE TABLE ɢɧɬɟɪɩɪɟɬɢɪɭɟɬɫɹ ɤɚɤ ɤɨɥɢɱɟɫɬɜɨ
ɫɢɦɜɨɥɨɜ. Ɉɛ ɷɬɨɦ ɧɟɨɛɯɨɞɢɦɨ ɩɨɦɧɢɬɶ, ɤɨɝɞɚ ɢɧɞɟɤɫɢɪɭɟɦɵɣ ɫɬɨɥɛɟɰ ɢɫɩɨɥɶɡɭɟɬ ɦɧɨɝɨɛɚɣɬɧɵɣ ɧɚɛɨɪ ɫɢɦɜɨɥɨɜ.
„ɋɩɟɰɢɮɢɤɚɰɢɹ ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ ɦɨɠɟɬ ɡɚɜɟɪɲɚɬɶɫɹ ɫɥɨɜɨɦ ASC ɢɥɢ DESC. ɗɬɢ
ɫɥɨɜɚ ɩɪɟɞɭɫɦɨɬɪɟɧɵ ɞɥɹ ɛɭɞɭɳɢɯ ɪɚɫɲɢɪɟɧɢɣ, ɱɬɨɛɵ ɭɤɚɡɵɜɚɬɶ ɩɨɪɹɞɨɤ ɯɪɚɧɟɧɢɹ ɢɧɞɟɤɫɢɪɭɟɦɵɯ ɡɧɚɱɟɧɢɣ – ɩɨ ɜɨɡɪɚɫɬɚɧɢɸ ɢɥɢ ɩɨ ɭɛɵɜɚɧɢɸ. ȼ ɧɚɫɬɨɹɳɟɟ
ɜɪɟɦɹ ɨɧɢ ɪɚɫɩɨɡɧɚɸɬɫɹ, ɧɨ ɢɝɧɨɪɢɪɭɸɬɫɹ. ɂɧɞɟɤɫɧɵɟ ɡɧɚɱɟɧɢɹ ɜɫɟɝɞɚ ɯɪɚɧɹɬɫɹ
ɜ ɜɨɡɪɚɫɬɚɸɳɟɦ ɩɨɪɹɞɤɟ.
„ɉɪɢ ɢɫɩɨɥɶɡɨɜɚɧɢɢ ɤɨɧɫɬɪɭɤɰɢɣ ORDER BY ɢɥɢ GROUP BY ɞɥɹ ɫɬɨɥɛɰɨɜ BLOB ɢɥɢ
TEXT, ɫɟɪɜɟɪ ɫɨɪɬɢɪɭɟɬ ɡɧɚɱɟɧɢɹ, ɢɫɩɨɥɶɡɭɹ ɬɨɥɶɤɨ ɨɩɪɟɞɟɥɟɧɧɨɟ ɱɢɫɥɨ ɧɚɱɚɥɶɧɵɯ
ɛɚɣɬɨɜ, ɤɨɬɨɪɨɟ ɡɚɞɚɟɬɫɹ ɫɢɫɬɟɦɧɨɣ ɩɟɪɟɦɟɧɧɨɣ max_sort_length. ɋɦ. ɪɚɡɞɟɥ 4.4.2.
„ȼ MySQL 3.23.23 ɢ ɛɨɥɟɟ ɩɨɡɞɧɢɯ ɜɟɪɫɢɹɯ ɟɫɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɫɨɡɞɚɜɚɬɶ ɢɧɞɟɤɫɵ
FULLTEXT. Ɉɧɢ ɢɫɩɨɥɶɡɭɸɬɫɹ ɞɥɹ ɩɨɥɧɨɬɟɤɫɬɨɜɨɝɨ ɩɨɢɫɤɚ. ɂɧɞɟɤɫɵ FULLTEXT
ɩɨɞɞɟɪɠɢɜɚɸɬ ɬɨɥɶɤɨ ɬɚɛɥɢɰɵ MyISAM. Ɉɧɢ ɦɨɝɭɬ ɛɵɬɶ ɫɨɡɞɚɧɵ ɬɨɥɶɤɨ ɧɚ ɫɬɨɥɛɰɚɯ CHAR, VARCHAR ɢɥɢ TEXT. ɂɧɞɟɤɫɚɰɢɹ ɩɪɢ ɷɬɨɦ ɜɵɩɨɥɧɹɟɬɫɹ ɩɨ ɩɨɥɧɨɣ ɞɥɢɧɟ
ɡɧɚɱɟɧɢɹ ɫɬɨɥɛɰɵ. ɑɚɫɬɢɱɧɚɹ ɢɧɞɟɤɫɚɰɢɹ ɩɪɟɮɢɤɫɚ ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ, ɢ ɟɫɥɢ
ɭɤɚɡɚɬɶ ɞɥɢɧɭ ɩɪɟɮɢɤɫɚ, ɨɧɚ ɢɝɧɨɪɢɪɭɟɬɫɹ. ɋɦ. ɞɟɬɚɥɢ ɜ ɪɚɡɞɟɥɟ 5.6.
„ȼ MySQL 4.1 ɢ ɜɵɲɟ ɦɨɠɧɨ ɫɨɡɞɚɜɚɬɶ ɢɧɞɟɤɫɵ SPATIAL ɩɨ ɫɬɨɥɛɰɚɦ ɩɪɨɫɬɪɚɧɫɬɜɟɧɧɵɯ (spatial) ɬɢɩɨɜ. ɉɪɨɫɬɪɚɧɫɬɜɟɧɧɵɟ ɬɢɩɵ ɩɨɞɞɟɪɠɢɜɚɸɬɫɹ ɬɨɥɶɤɨ ɦɟɯɚɧɢɡɦɨɦ ɯɪɚɧɟɧɢɹ MyISAM ɢ ɢɧɞɟɤɫɢɪɭɟɦɵɟ ɫɬɨɥɛɰɵ ɞɨɥɠɧɵ ɛɵɬɶ ɨɛɴɹɜɥɟɧɵ ɤɚɤ NOT
NULL. ɋɦ. ɝɥɚɜɭ 7.
MySQL 3.23.44 ɢ ɜɵɲɟ ɬɚɛɥɢɰɵ InnoDB ɩɨɞɞɟɪɠɢɜɚɸɬ ɩɪɨɜɟɪɤɭ ɨɝɪɚɧɢɱɟɧɢɣ
ɰɟɥɨɫɬɧɨɫɬɢ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ. Ɉɬɦɟɬɢɦ, ɱɬɨ ɫɢɧɬɚɤɫɢɫ FOREIGN KEY ɞɥɹ InnoDB
ɛɨɥɟɟ ɨɝɪɚɧɢɱɢɜɚɸɳɢɣ, ɱɟɦ ɫɢɧɬɚɤɫɢɫ ɨɩɟɪɚɬɨɪɚ CREATE TABLE, ɩɪɟɞɫɬɚɜɥɟɧɧɵɣ
ɜ ɧɚɱɚɥɟ ɪɚɡɞɟɥɚ. ɋɬɨɥɛɰɵ ɬɚɛɥɢɰ, ɧɚ ɤɨɬɨɪɵɟ ɫɫɵɥɚɟɬɫɹ ɜɧɟɲɧɢɣ ɤɥɸɱ, ɞɨɥɠɧɵ
ɛɵɬɶ ɜɫɟɝɞɚ ɢɦɟɧɨɜɚɧɵ ɹɜɧɨ. InnoDB ɩɨɞɞɟɪɠɢɜɚɟɬ ɞɟɣɫɬɜɢɹ ON DELETE ɢ ON
UPDATE ɞɥɹ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 3.23.50 ɢ MySQL 4.0.8 ɫɨɨɬɜɟɬɫɬɜɟɧɧɨ. Ɍɨɱɧɵɣ ɫɢɧɬɚɤɫɢɫ ɩɪɟɞɫɬɚɜɥɟɧ ɜ ɪɚɡɞɟɥɟ 6.2.5.1.
Ⱦɥɹ ɞɪɭɝɢɯ ɦɟɯɚɧɢɡɦɨɜ ɯɪɚɧɟɧɢɹ ɫɟɪɜɟɪ MySQL ɪɚɡɛɢɪɚɟɬ ɫɢɧɬɚɤɫɢɫ FOREIGN
KEY ɢ REFERENCES ɜ ɨɩɟɪɚɬɨɪɟ CREATE TABLE, ɧɨ ɛɟɡ ɤɚɤɢɯ-ɥɢɛɨ ɞɟɣɫɬɜɢɣ. Ʉɨɧɫɬɪɭɤɰɢɹ CHECK ɪɚɫɩɨɡɧɚɟɬɫɹ, ɨɞɧɚɤɨ ɢɝɧɨɪɢɪɭɟɬɫɹ ɜɫɟɦɢ ɦɟɯɚɧɢɡɦɚɦɢ ɯɪɚɧɟɧɢɹ.
„ȼ
ɬɚɛɥɢɰ MyISAM ɢ ISAM ɤɚɠɞɵɣ NULL-ɫɬɨɥɛɟɰ ɬɪɟɛɭɟɬ ɞɨɩɨɥɧɢɬɟɥɶɧɨɝɨ ɛɢɬɚ,
ɤɨɬɨɪɵɣ ɨɤɪɭɝɥɹɟɬɫɹ ɞɨ ɛɥɢɠɚɣɲɟɝɨ ɛɚɣɬɚ. Ɇɚɤɫɢɦɚɥɶɧɚɹ ɞɥɢɧɚ ɜ ɛɚɣɬɚɯ ɦɨɠɟɬ
ɛɵɬɶ ɪɚɫɫɱɢɬɚɧɚ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
„Ⱦɥɹ
ɞɥɢɧɚ ɫɬɪɨɤɢ = 1
+ (ɫɭɦɦɚ ɞɥɢɧ ɫɬɨɥɛɰɨɜ)
+ (ɤɨɥɢɱɟɫɬɜɨ NULL-ɫɬɨɥɛɰɨɜ + ɮɥɚɝ_ɭɞɚɥɟɧɢɹ + 7)/8
+ (ɤɨɥɢɱɟɫɬɜɨ ɫɬɨɥɛɰɨɜ ɩɟɪɟɦɟɧɧɨɣ ɞɥɢɧɵ)
ɮɥɚɝ_ɭɞɚɥɟɧɢɹ ɪɚɜɟɧ 1 ɞɥɹ ɬɚɛɥɢɰ ɫɨ ɫɬɚɬɢɱɟɫɤɢɦ ɮɨɪɦɚɬɨɦ ɡɚɩɢɫɢ. ɋɬɚɬɢɱɟɫɤɢɟ
ɬɚɛɥɢɰɵ ɢɫɩɨɥɶɡɭɸɬ ɛɢɬ ɜ ɡɚɩɢɫɢ ɫɬɪɨɤɢ ɜ ɤɚɱɟɫɬɜɟ ɩɪɢɡɧɚɤɚ ɬɨɝɨ, ɱɬɨ ɡɚɩɢɫɶ
ɭɞɚɥɟɧɚ. ɮɥɚɝ_ɭɞɚɥɟɧɢɹ ɪɚɜɟɧ 0 ɞɥɹ ɞɢɧɚɦɢɱɟɫɤɢɯ ɬɚɛɥɢɰ, ɩɨɬɨɦɭ ɱɬɨ ɮɥɚɝ ɫɨɯɪɚɧɹɟɬɫɹ ɜ ɞɢɧɚɦɢɱɟɫɤɨɦ ɡɚɝɨɥɨɜɤɟ ɫɬɪɨɤɢ.
268
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɗɬɢ ɜɵɱɢɫɥɟɧɢɹ ɧɟɩɪɢɦɟɧɢɦɵ ɤ ɬɚɛɥɢɰɚɦ InnoDB, ɜ ɤɨɬɨɪɵɯ ɪɚɡɦɟɪ ɯɪɚɧɟɧɢɹ ɧɟ
ɨɬɥɢɱɚɟɬɫɹ ɞɥɹ ɫɬɨɥɛɰɨɜ NULL ɢ NOT NULL.
ɑɚɫɬɶ ɨɩɰɢɢ_ɬɚɛɥɢɰɵ ɫɢɧɬɚɤɫɢɫɚ CREATE TABLE ɦɨɠɟɬ ɩɪɢɦɟɧɹɬɶɫɹ ɜ MySQL 3.23 ɢ
ɜɵɲɟ.
Ɉɩɰɢɢ ENGINE ɢ TYPE ɫɩɟɰɢɮɢɰɢɪɭɸɬ ɦɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ ɞɥɹ ɬɚɛɥɢɰɵ. Ɉɩɰɢɹ ENGINE
ɩɨɹɜɢɥɚɫɶ ɜ MySQL 4.0.18 (ɞɥɹ ɫɟɪɢɢ 4.0) ɢ ɜ 4.1.2 (ɞɥɹ ɫɟɪɢɢ 4.1). ɗɬɨ ɩɪɟɞɩɨɱɬɢɬɟɥɶɧɵɣ ɜɚɪɢɚɧɬ ɧɚɢɦɟɧɨɜɚɧɢɹ ɨɩɰɢɢ ɜ ɷɬɢɯ ɜɟɪɫɢɹɯ, ɚ TYPE ɫɱɢɬɚɟɬɫɹ ɭɫɬɚɪɟɜɲɟɣ. TYPE ɟɳɟ
ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ ɜ ɫɟɪɢɢ ɜɵɩɭɫɤɨɜ 4.x, ɧɨ ɜɟɪɨɹɬɧɨ, ɛɭɞɟɬ ɢɫɤɥɸɱɟɧɚ ɜ MySQL 5.1.
Ɉɩɰɢɢ ENGINE ɢ TYPE ɩɪɢɧɢɦɚɸɬ ɫɥɟɞɭɸɳɢɟ ɡɧɚɱɟɧɢɹ:
Ɇɟɯɚɧɢɡɦ
ɯɪɚɧɟɧɢɹ
Ɉɩɢɫɚɧɢɟ
BDB
Ɍɚɛɥɢɰɵ, ɛɟɡɨɩɚɫɧɵɟ ɤ ɬɪɚɧɡɚɤɰɢɹɦ, ɫɨ ɫɬɪɚɧɢɱɧɨɣ ɛɥɨɤɢɪɨɜɤɨɣ.
BerkeleyDB
ɉɫɟɜɞɨɧɢɦ ɞɥɹ BDB.
HEAP
Ⱦɚɧɧɵɟ ɬɚɛɥɢɰɵ ɯɪɚɧɹɬɫɹ ɬɨɥɶɤɨ ɜ ɩɚɦɹɬɢ.
ISAM
ɂɫɯɨɞɧɵɣ ɦɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ MySQL.
InnoDB
Ɍɚɛɥɢɰɵ, ɛɟɡɨɩɚɫɧɵɟ ɤ ɬɪɚɧɡɚɤɰɢɹɦ, ɫ ɩɨɫɬɪɨɱɧɨɣ ɛɥɨɤɢɪɨɜɤɨɣ
ɢ ɜɧɟɲɧɢɦɢ ɤɥɸɱɚɦɢ.
MEMORY
ɉɫɟɜɞɨɧɢɦ ɞɥɹ HEAP. (MEMORY – ɩɪɟɞɩɨɱɬɢɬɟɥɶɧɚɹ ɮɨɪɦɚ, ɧɚɱɢɧɚɹ ɫ MySQL 4.1)
MERGE
Ʉɨɥɥɟɤɰɢɹ ɬɚɛɥɢɰ MyISAM, ɢɫɩɨɥɶɡɭɟɦɵɯ ɤɚɤ ɨɞɧɚ ɬɚɛɥɢɰɚ.
MRG_MyISAM
ɉɫɟɜɞɨɧɢɦ ɞɥɹ MERGE.
MyISAM
Ȼɢɧɚɪɧɵɣ ɩɟɪɟɧɨɫɢɦɵɣ ɦɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ; ɭɫɨɜɟɪɲɟɧɫɬɜɨɜɚɧɧɚɹ ɡɚɦɟɧɚ ISAM.
ȿɫɥɢ ɭɤɚɡɵɜɚɟɬɫɹ ɧɟɫɭɳɟɫɬɜɭɸɳɢɣ ɦɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ, ɜɦɟɫɬɨ ɧɟɝɨ MySQL ɢɫɩɨɥɶɡɭɟɬ MyISAM. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɨɩɪɟɞɟɥɟɧɢɟ ɬɚɛɥɢɰɵ ɜɤɥɸɱɚɟɬ ɨɩɰɢɸ ENGINE=BDB, ɚ ɫɟɪɜɟɪ
MySQL ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬ ɬɚɛɥɢɰ BDB, ɬɨ ɬɚɛɥɢɰɚ ɫɨɡɞɚɟɬɫɹ ɤɚɤ MyISAM. ɗɬɨ ɞɚɟɬ ɜɨɡɦɨɠɧɨɫɬɶ ɧɚɫɬɪɨɢɬɶ ɫɪɟɞɭ ɪɟɩɥɢɤɚɰɢɢ, ɜ ɤɨɬɨɪɨɣ ɩɪɢɦɟɧɹɸɬɫɹ ɬɪɚɧɡɚɤɰɢɨɧɧɵɟ ɬɚɛɥɢɰɵ ɧɚ
ɝɥɚɜɧɨɦ ɫɟɪɜɟɪɟ ɢ ɧɟɬɪɚɧɡɚɤɰɢɨɧɧɵɟ – ɧɚ ɩɨɞɱɢɧɟɧɧɨɦ (ɱɬɨɛɵ ɩɨɥɭɱɢɬɶ ɛɨɥɟɟ ɜɵɫɨɤɭɸ
ɫɤɨɪɨɫɬɶ). ȼ MySQL 4.1.1 ɝɟɧɟɪɢɪɭɟɬɫɹ ɩɪɟɞɭɩɪɟɠɞɟɧɢɟ, ɟɫɥɢ ɡɚɞɚɧɧɵɟ ɦɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ.
ɏɚɪɚɤɬɟɪɢɫɬɢɤɢ ɦɟɯɚɧɢɡɦɨɜ ɯɪɚɧɟɧɢɹ ɩɨɞɪɨɛɧɨ ɨɛɫɭɠɞɚɸɬɫɹ ɜ ɤɧɢɝɟ MySQL. Ɋɭɤɨɜɨɞɫɬɜɨ ɚɞɦɢɧɢɫɬɪɚɬɨɪɚ (Ɇ. : ɂɡɞɚɬɟɥɶɫɤɢɣ ɞɨɦ “ȼɢɥɶɹɦɫ”, 2005, ISBN 5-8459-0805-1).
Ⱦɪɭɝɢɟ ɨɩɰɢɢ ɬɚɛɥɢɰ ɫɥɭɠɚɬ ɞɥɹ ɨɩɬɢɦɢɡɚɰɢɢ ɩɨɜɟɞɟɧɢɹ ɬɚɛɥɢɰɵ. ȼ ɛɨɥɶɲɢɧɫɬɜɟ
ɫɥɭɱɚɟɜ ɜɚɦ ɧɟ ɩɪɢɞɟɬɫɹ ɭɤɚɡɵɜɚɬɶ ɧɢ ɨɞɧɭ ɢɡ ɧɢɯ. Ɉɩɰɢɢ ɪɚɛɨɬɚɸɬ ɫɨ ɜɫɟɦɢ ɦɟɯɚɧɢɡɦɚɦɢ ɯɪɚɧɟɧɢɹ, ɟɫɥɢ ɧɟ ɭɤɚɡɚɧɨ ɞɪɭɝɨɟ:
„AUTO_INCREMENT. ɇɚɱɚɥɶɧɨɟ ɡɧɚɱɟɧɢɟ AUTO_INCREMENT ɞɥɹ ɬɚɛɥɢɰɵ. ɗɬɨ ɪɚɛɨɬɚɟɬ
ɬɨɥɶɤɨ ɫ ɬɚɛɥɢɰɚɦɢ MyISAM. Ⱦɥɹ ɬɨɝɨ ɱɬɨɛɵ ɭɫɬɚɧɨɜɢɬɶ ɩɟɪɜɨɟ ɡɧɚɱɟɧɢɟ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɞɥɹ ɬɚɛɥɢɰɵ InnoDB, ɧɟɨɛɯɨɞɢɦɨ ɜɫɬɚɜɢɬɶ ɫɬɪɨɤɭ ɫɨ ɡɧɚɱɟɧɢɟɦ ɫɨɨɬɜɟɬɫɬɜɭɸɳɟɣ ɫɬɨɥɛɰɵ, ɧɚ ɟɞɢɧɢɰɭ ɦɟɧɶɲɢɦ, ɱɟɦ ɠɟɥɚɬɟɥɶɧɨɟ ɧɚɱɚɥɶɧɨɟ ɡɧɚɱɟɧɢɟ, ɚ ɡɚɬɟɦ ɭɞɚɥɢɬɶ ɟɟ.
„AVG_ROW_LENGTH. ɉɪɟɞɩɨɥɚɝɚɟɦɨɟ ɫɪɟɞɧɟɟ ɡɧɚɱɟɧɢɟ ɞɥɢɧɵ ɫɬɪɨɤɢ ɜ ɬɚɛɥɢɰɟ. ɗɬɨ
ɧɟɨɛɯɨɞɢɦɨ ɭɫɬɚɧɚɜɥɢɜɚɬɶ ɬɨɥɶɤɨ ɞɥɹ ɛɨɥɶɲɢɯ ɬɚɛɥɢɰ ɫ ɡɚɩɢɫɹɦɢ ɩɟɪɟɦɟɧɧɨɣ
ɞɥɢɧɵ.
6.2. Îïåðàòîðû îïðåäåëåíèÿ äàííûõ
269
Ʉɨɝɞɚ ɫɨɡɞɚɟɬɫɹ ɬɚɛɥɢɰɚ MyISAM, MySQL ɢɫɩɨɥɶɡɭɟɬ ɩɪɨɢɡɜɟɞɟɧɢɟ ɡɧɚɱɟɧɢɣ ɨɩɰɢɣ MAX_ROWS ɧɚ AVG_ROW_LENGTH, ɱɬɨɛɵ ɪɚɫɫɱɢɬɚɬɶ ɩɪɟɞɩɨɥɚɝɚɟɦɵɣ ɪɚɡɦɟɪ ɬɚɛɥɢɰɵ. ȿɫɥɢ ɧɟ ɭɤɚɡɚɧɚ ɥɸɛɚɹ ɢɡ ɷɬɢɯ ɨɩɰɢɣ, ɦɚɤɫɢɦɚɥɶɧɵɣ ɪɚɡɦɟɪ ɬɚɛɥɢɰɵ ɫɨɫɬɚɜɢɬ 4 Ƚɛɚɣɬ (ɢɥɢ 2 Ƚɛɚɣɬ, ɟɫɥɢ ɨɩɟɪɚɰɢɨɧɧɚɹ ɫɢɫɬɟɦɚ ɩɨɞɞɟɪɠɢɜɚɟɬ ɮɚɣɥɵ
ɪɚɡɦɟɪɨɦ ɬɨɥɶɤɨ 2 Ƚɛɚɣɬ). ɋɦɵɫɥ ɷɬɨɝɨ ɫɨɫɬɨɢɬ ɜ ɭɦɟɧɶɲɟɧɢɢ ɪɚɡɦɟɪɚ ɭɤɚɡɚɬɟɥɹ
ɡɚɩɢɫɟɣ, ɱɬɨɛɵ ɫɞɟɥɚɬɶ ɢɧɞɟɤɫɵ ɦɟɧɶɲɢɦɢ ɢ ɛɵɫɬɪɵɦɢ ɜ ɫɥɭɱɚɟ, ɤɨɝɞɚ ɧɟɬ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɜ ɛɨɥɶɲɨɣ ɬɚɛɥɢɰɟ. ȿɫɥɢ ɜɵ ɯɨɬɢɬɟ ɜɫɟɦ ɬɚɛɥɢɰɚɦ ɨɛɟɫɩɟɱɢɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɪɨɫɬɚ ɡɚ ɩɪɟɞɟɥɚɦɢ ɨɝɪɚɧɢɱɟɧɢɹ ɜ 4 Ƚɛɚɣɬ, ɢ ɫɨɝɥɚɫɧɵ ɫ ɬɟɦ, ɱɬɨ ɞɚɠɟ
ɦɚɥɟɧɶɤɢɟ ɬɚɛɥɢɰɵ ɛɭɞɭɬ ɡɚɧɢɦɚɬɶ ɧɟɫɤɨɥɶɤɨ ɛɨɥɶɲɟ ɦɟɫɬɚ ɢ ɪɚɛɨɬɚɬɶ ɦɟɞɥɟɧɧɟɟ, ɱɟɦ ɧɟɨɛɯɨɞɢɦɨ, ɦɨɠɟɬɟ ɭɜɟɥɢɱɢɬɶ ɪɚɡɦɟɪ ɭɤɚɡɚɬɟɥɹ ɡɚɩɢɫɟɣ ɩɨ ɭɦɨɥɱɚɧɢɸ,
ɭɫɬɚɧɨɜɢɜ ɫɢɫɬɟɦɧɭɸ ɩɟɪɟɦɟɧɧɭɸ myisam_data_pointer_size, ɤɨɬɨɪɚɹ ɛɵɥɚ ɞɨɛɚɜɥɟɧɚ ɜ MySQL 4.1.2.
„CHECKSUM. ɍɫɬɚɧɨɜɢɬɟ ɡɧɚɱɟɧɢɟ ɷɬɨɣ ɨɩɰɢɢ ɪɚɜɧɵɦ 1, ɟɫɥɢ ɯɨɬɢɬɟ, ɱɬɨɛɵ MySQL
ɩɨɞɞɟɪɠɢɜɚɥ ɠɢɜɭɸ ɤɨɧɬɪɨɥɶɧɭɸ ɫɭɦɦɭ ɜɫɟɯ ɫɬɪɨɤ ɬɚɛɥɢɰɵ (ɬɨ ɟɫɬɶ ɤɨɧɬɪɨɥɶɧɭɸ ɫɭɦɦɭ, ɤɨɬɨɪɭɸ MySQL ɨɛɧɨɜɥɹɟɬ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɩɪɢ ɤɚɠɞɨɦ ɢɡɦɟɧɟɧɢɢ
ɬɚɛɥɢɰɵ). ɗɬɨ ɧɟɦɧɨɝɨ ɡɚɦɟɞɥɢɬ ɨɛɧɨɜɥɟɧɢɹ ɞɚɧɧɵɯ ɜ ɬɚɛɥɢɰɟ, ɧɨ ɨɛɥɟɝɱɢɬ ɩɨɢɫɤ ɩɨɜɪɟɠɞɟɧɧɵɯ ɬɚɛɥɢɰ. Ɉɩɟɪɚɬɨɪ CHECKSUM TABLE ɫɨɨɛɳɚɟɬ ɷɬɭ ɤɨɧɬɪɨɥɶɧɭɸ
ɫɭɦɦɭ (ɬɨɥɶɤɨ ɞɥɹ MyISAM).
„COMMENT.
Ʉɨɦɦɟɧɬɚɪɢɣ ɤ ɬɚɛɥɢɰɟ, ɧɟ ɛɨɥɟɟ 60 ɫɢɦɜɨɥɨɜ ɞɥɢɧɨɣ.
„MAX_ROWS. Ɇɚɤɫɢɦɚɥɶɧɨɟ ɤɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ, ɤɨɬɨɪɨɟ ɩɥɚɧɢɪɭɟɬɫɹ ɯɪɚɧɢɬɶ ɜ ɬɚɛɥɢɰɟ.
„MIN_ROWS. Ɇɢɧɢɦɚɥɶɧɨɟ ɤɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ, ɤɨɬɨɪɨɟ ɩɥɚɧɢɪɭɟɬɫɹ ɯɪɚɧɢɬɶ ɜ ɬɚɛɥɢɰɟ.
„PACK_KEYS.
ɗɬɭ ɨɩɰɢɸ ɧɭɠɧɨ ɭɫɬɚɧɨɜɢɬɶ ɜ 1, ɟɫɥɢ ɧɟɨɛɯɨɞɢɦɨ ɫɞɟɥɚɬɶ ɢɧɞɟɤɫɵ
ɩɨɦɟɧɶɲɟ. Ɉɛɵɱɧɨ ɷɬɨ ɧɟɦɧɨɝɨ ɡɚɦɟɞɥɹɟɬ ɡɚɩɢɫɶ, ɨɞɧɚɤɨ ɭɫɤɨɪɹɟɬ ɱɬɟɧɢɟ. ɍɫɬɚɧɨɜɤɚ ɷɬɨɣ ɨɩɰɢɢ ɜ 0 ɨɬɤɥɸɱɚɟɬ ɭɩɚɤɨɜɤɭ ɤɥɸɱɟɣ. ɍɫɬɚɧɨɜɤɚ ɟɟ ɜ DEFAULT
(MySQL 4.0) ɫɨɨɛɳɚɟɬ ɦɟɯɚɧɢɡɦɭ ɯɪɚɧɟɧɢɹ, ɱɬɨ ɩɚɤɨɜɚɬɶ ɧɭɠɧɨ ɬɨɥɶɤɨ ɞɥɢɧɧɵɟ
ɫɬɨɥɛɰɵ ɬɢɩɚ CHAR/VARCHAR (ɬɨɥɶɤɨ ɞɥɹ ISAM ɢ MyISAM).
ȿɫɥɢ ɧɟ ɭɤɚɡɵɜɚɬɶ ɨɩɰɢɸ PACK_KEYS, ɩɨ ɭɦɨɥɱɚɧɢɸ ɩɚɤɭɸɬɫɹ ɬɨɥɶɤɨ ɫɬɪɨɤɢ, ɧɨ
ɧɟ ɱɢɫɥɚ. ȿɫɥɢ ɭɤɚɡɚɬɶ PACK_KEYS=1, ɬɨ ɱɢɫɥɚ ɬɨɠɟ ɩɚɤɭɸɬɫɹ.
ɉɪɢ ɭɩɚɤɨɜɤɟ ɞɜɨɢɱɧɵɯ ɱɢɫɥɨɜɵɯ ɤɥɸɱɟɣ MySQL ɩɪɢɦɟɧɹɟɬ ɫɠɚɬɢɟ ɩɪɟɮɢɤɫɨɜ:
x Ʉɚɠɞɵɣ ɤɥɸɱ ɬɪɟɛɭɟɬ ɞɨɩɨɥɧɢɬɟɥɶɧɨɝɨ ɛɚɣɬɚ ɞɥɹ ɭɤɚɡɚɧɢɹ ɬɨɝɨ, ɫɤɨɥɶɤɨ ɛɚɣɬ
ɩɪɟɞɵɞɭɳɟɝɨ ɤɥɸɱɚ ɩɨɜɬɨɪɹɸɬɫɹ ɜ ɫɥɟɞɭɸɳɟɦ.
x ɍɤɚɡɚɬɟɥɶ ɫɬɪɨɤɢ ɫɨɯɪɚɧɹɟɬɫɹ ɜ ɩɨɪɹɞɤɟ “ɫɬɚɪɲɢɣ ɛɚɣɬ ɩɟɪɜɵɣ” ɧɟɩɨɫɪɟɞɫɬɜɟɧɧɨ ɩɨɫɥɟ ɤɥɸɱɚ, ɱɬɨɛɵ ɭɥɭɱɲɢɬɶ ɫɠɚɬɢɟ.
ɗɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɟɫɥɢ ɭ ɜɚɫ ɦɧɨɝɨ ɪɚɜɧɵɯ ɤɥɸɱɟɣ ɜ ɞɜɭɯ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɵɯ ɫɬɪɨɤɚɯ, ɜɫɟ ɩɨɫɥɟɞɭɸɳɢɟ “ɬɚɤɢɟ ɠɟ” ɤɥɸɱɢ ɨɛɵɱɧɨ ɡɚɣɦɭɬ ɬɨɥɶɤɨ ɞɜɚ ɛɚɣɬɚ (ɜɤɥɸɱɚɹ ɭɤɚɡɚɬɟɥɶ ɧɚ ɫɬɪɨɤɭ).
ɋɪɚɜɧɢɬɟ ɷɬɨ ɫ ɨɛɵɱɧɵɦ ɫɥɭɱɚɟɦ, ɤɨɝɞɚ ɤɚɠɞɵɣ ɫɥɟɞɭɸɳɢɣ ɤɥɸɱ ɩɨɬɪɟɛɭɟɬ ɞɥɹ
ɯɪɚɧɟɧɢɹ ɪɚɡɦɟɪ_ɯɪɚɧɟɧɢɹ_ɞɥɹ_ɤɥɸɱɚ + ɪɚɡɦɟɪ_ɭɤɚɡɚɬɟɥɹ (ɪɚɡɦɟɪ ɭɤɚɡɚɬɟɥɹ
ɨɛɵɱɧɨ ɪɚɜɟɧ 4). Ɉɞɧɚɤɨ ɜɵɝɨɞɚ ɨɬ ɫɠɚɬɢɹ ɩɪɟɮɢɤɫɨɜ ɛɭɞɟɬ ɨɳɭɬɢɦɨɣ ɬɨɥɶɤɨ ɜ
ɬɨɦ ɫɥɭɱɚɟ, ɤɨɝɞɚ ɟɫɬɶ ɦɧɨɝɨ ɪɚɜɧɵɯ ɡɧɚɱɟɧɢɣ ɱɢɫɥɨɜɨɝɨ ɤɥɸɱɚ. Ʉɨɝɞɚ ɜɫɟ ɤɥɸɱɢ
ɚɛɫɨɥɸɬɧɨ ɪɚɡɧɵɟ, ɬɨ ɧɚ ɤɚɠɞɵɣ ɤɥɸɱ ɩɨɬɪɟɛɭɟɬɫɹ ɧɚ ɨɞɢɧ ɛɚɣɬ ɛɨɥɶɲɟ, ɟɫɥɢ
ɤɥɸɱ ɧɟ ɦɨɠɟɬ ɢɦɟɬɶ ɡɧɚɱɟɧɢɣ NULL. (ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɞɥɢɧɚ ɭɩɚɤɨɜɚɧɧɨɝɨ ɤɥɸɱɚ
ɛɭɞɟɬ ɫɨɯɪɚɧɟɧɚ ɜ ɬɨɦ ɠɟ ɛɚɣɬɟ, ɤɨɬɨɪɵɣ ɫɥɭɠɢɬ ɞɥɹ ɯɪɚɧɟɧɢɹ ɩɪɢɡɧɚɤɚ ɬɨɝɨ, ɱɬɨ
ɤɥɸɱ NULL).
270
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
„PASSWORD.
ɒɢɮɪɭɟɬ ɮɚɣɥ .frm ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɩɚɪɨɥɹ. ȼ ɫɬɚɧɞɚɪɬɧɨɣ ɜɟɪɫɢɢ
MySQL ɷɬɚ ɨɩɰɢɹ ɧɟ ɞɟɥɚɟɬ ɧɢɱɟɝɨ.
„DELAY_KEY_WRITE. ɗɬɭ ɨɩɰɢɸ ɧɭɠɧɨ ɭɫɬɚɧɨɜɢɬɶ ɜ 1, ɟɫɥɢ ɜɵ ɯɨɬɢɬɟ ɨɬɥɨɠɢɬɶ ɨɛɧɨɜɥɟɧɢɹ ɤɥɸɱɟɣ ɬɚɛɥɢɰɵ ɞɨ ɦɨɦɟɧɬɚ ɟɟ ɡɚɤɪɵɬɢɹ (ɬɨɥɶɤɨ ɞɥɹ MyISAM).
„ROW_FORMAT.
Ɉɩɪɟɞɟɥɹɟɬ, ɤɚɤ ɞɨɥɠɧɚ ɫɨɯɪɚɧɹɬɶɫɹ ɫɬɪɨɤɚ. ȼ ɧɚɫɬɨɹɳɟɟ ɜɪɟɦɹ ɷɬɚ
ɨɩɰɢɹ ɪɚɛɨɬɚɟɬ ɬɨɥɶɤɨ ɫ ɬɚɛɥɢɰɚɦɢ MyISAM. ȿɟ ɡɧɚɱɟɧɢɟɦ ɦɨɠɟɬ ɛɵɬɶ FIXED ɢɥɢ
DYNAMIC, ɫɨɨɬɜɟɬɫɬɜɟɧɧɨ, ɞɥɹ ɫɬɚɬɢɱɟɫɤɨɝɨ ɮɨɪɦɚɬɚ ɫɬɪɨɤɢ ɢɥɢ ɮɨɪɦɚɬɚ ɫ ɩɟɪɟɦɟɧɧɨɣ ɞɥɢɧɨɣ. myisampack ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɬɢɩ COMPRESSED.
„RAID_TYPE.
Ɉɩɰɢɹ RAID_TYPE ɩɨɦɨɝɚɟɬ ɩɪɟɨɞɨɥɟɬɶ ɥɢɦɢɬ ɪɚɡɦɟɪɚ ɮɚɣɥɚ ɞɚɧɧɵɯ
MyISAM ɜ 2 Ƚɛɚɣɬ/4 Ƚɛɚɣɬ (ɧɟ ɤɚɫɚɟɬɫɹ ɢɧɞɟɤɫɧɵɯ ɮɚɣɥɨɜ) ɜ ɨɩɟɪɚɰɢɨɧɧɵɯ ɫɢɫɬɟ-
ɦɚɯ, ɤɨɬɨɪɵɟ ɧɟ ɩɨɞɞɟɪɠɢɜɚɸɬ ɛɨɥɶɲɢɟ ɮɚɣɥɵ.
ȼɵ ɦɨɠɟɬɟ ɩɨɜɵɫɢɬɶ ɫɤɨɪɨɫɬɶ ɨɩɟɪɚɰɢɣ ɜɜɨɞɚ-ɜɵɜɨɞɚ, ɩɨɦɟɫɬɢɜ RAID-ɤɚɬɚɥɨɝɢ
ɧɚ ɪɚɡɧɵɟ ɮɢɡɢɱɟɫɤɢɟ ɞɢɫɤɢ. ɇɚ ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɞɨɩɭɫɤɚɟɬɫɹ ɬɨɥɶɤɨ RAID_TYPE ɢ
STRIPPED. 1 ɢ RAID0 – ɷɬɨ ɩɫɟɜɞɨɧɢɦɵ ɞɥɹ STRIPPED.
ȿɫɥɢ ɜɵ ɭɤɚɡɵɜɚɟɬɟ ɨɩɰɢɸ RAID_TYPE ɞɥɹ ɬɚɛɥɢɰɵ MyISAM, ɭɤɚɡɵɜɚɣɬɟ ɬɚɤɠɟ
RAID_CHUNKS ɢ RAID_CHUNKSIZE. Ɇɚɤɫɢɦɚɥɶɧɨɟ ɡɧɚɱɟɧɢɟ RAID_CHUNKS ɪɚɜɧɨ 255.
MyISAM ɫɨɡɞɚɫɬ ɩɨɞɤɚɬɚɥɨɝɢ RAID_CHUNKS ɫ ɢɦɟɧɚɦɢ 00, 01, 02, ... 09, 0a, 0b, ... ɜ ɤɚɬɚɥɨɝɟ ɛɚɡɵ ɞɚɧɧɵɯ. ȼ ɤɚɠɞɨɦ ɢɯ ɷɬɢɯ ɩɨɞɤɚɬɚɥɨɝɨɜ MyISAM ɫɨɡɞɚɫɬ ɮɚɣɥ
ɢɦɹ_ɬɚɛɥɢɰɵ.MYD. ɉɪɢ ɡɚɩɢɫɢ ɜ ɮɚɣɥ ɞɚɧɧɵɯ ɨɛɪɚɛɨɬɱɢɤ RAID ɨɬɨɛɪɚɠɚɟɬ ɩɟɪɜɵɟ
RAID_CHUNKSIZE*1024 ɛɚɣɬ ɧɚ ɩɟɪɜɵɣ ɮɚɣɥ, ɫɥɟɞɭɸɳɢɟ RAID_CHUNKSIZE*1024 ɛɚɣɬ
ɧɚ ɜɬɨɪɨɣ ɮɚɣɥ ɢ ɬɚɤ ɞɚɥɟɟ.
Ɉɩɰɢɹ RAID_TYPE ɪɚɛɨɬɚɟɬ ɩɨɞ ɭɩɪɚɜɥɟɧɢɟɦ ɥɸɛɨɣ ɨɩɟɪɚɰɢɨɧɧɨɣ ɫɢɫɬɟɦɵ, ɟɫɥɢ
MySQL ɛɵɥ ɫɨɛɪɚɧ ɫ ɩɨɦɨɳɶɸ configure --with-raid. ɑɬɨɛɵ ɨɩɪɟɞɟɥɢɬɶ, ɩɨɞɞɟɪɠɢɜɚɟɬ ɥɢ ɫɟɪɜɟɪ RAID-ɬɚɛɥɢɰɵ, ɜɵɩɨɥɧɢɬɟ SHOW VARIABLES LIKE 'have_raid'
ɢ ɩɨɫɦɨɬɪɢɬɟ, ɭɫɬɚɧɨɜɥɟɧɨ ɥɢ ɡɧɚɱɟɧɢɟ ɩɟɪɟɦɟɧɧɨɣ have_raid ɜ YES.
„UNION.
ɉɪɢɦɟɧɹɟɬɫɹ, ɤɨɝɞɚ ɧɭɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɤɨɥɥɟɤɰɢɸ ɢɞɟɧɬɢɱɧɵɯ ɬɚɛɥɢɰ
ɤɚɤ ɨɞɧɭ. ɗɬɨ ɪɚɛɨɬɚɟɬ ɬɨɥɶɤɨ ɫ ɬɚɛɥɢɰɚɦɢ MERGE.
ɇɚ ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɜɵ ɞɨɥɠɧɵ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɢ SELECT, UPDATE ɢ DELETE ɧɚ ɬɚɛɥɢɰɵ, ɨɬɨɛɪɚɠɚɟɦɵɟ ɜ MERGE. ɉɟɪɜɨɧɚɱɚɥɶɧɨ ɜɫɟ ɢɫɩɨɥɶɡɭɟɦɵɟ ɬɚɛɥɢɰɵ ɞɨɥɠɧɵ
ɛɵɥɢ ɪɚɫɩɨɥɚɝɚɬɶɫɹ ɜ ɬɨɦ ɠɟ ɤɚɬɚɥɨɝɟ, ɱɬɨ ɢ ɬɚɛɥɢɰɚ MERGE. ȼ MySQL 4.1.1 ɷɬɨ
ɨɝɪɚɧɢɱɟɧɢɟ ɫɧɹɬɨ.
„INSERT_METHOD. ȿɫɥɢ ɧɟɨɛɯɨɞɢɦɨ ɜɫɬɚɜɥɹɬɶ ɞɚɧɧɵɟ ɜ ɬɚɛɥɢɰɭ MERGE, ɜɵ ɞɨɥɠɧɵ
ɭɤɚɡɵɜɚɬɶ ɫ ɩɨɦɨɳɶɸ ɨɩɰɢɢ INSERT_METHOD, ɜ ɤɚɤɭɸ ɮɢɡɢɱɟɫɤɭɸ ɬɚɛɥɢɰɭ ɞɨɥɠɧɚ
ɜɫɬɚɜɥɹɬɶɫɹ ɫɬɪɨɤɚ. INSERT_METHOD – ɨɩɰɢɹ, ɩɪɢɦɟɧɢɦɚɹ ɬɨɥɶɤɨ ɤ ɬɚɛɥɢɰɚɦ MERGE.
Ɉɧɚ ɛɵɥɚ ɩɪɟɞɫɬɚɜɥɟɧɚ ɜɩɟɪɜɵɟ ɜ MySQL 4.0.0.
„DATA DIRECTORY, INDEX DIRECTORY
ɂɫɩɨɥɶɡɭɹ DATA DIRECTORY='ɤɚɬɚɥɨɝ' ɢɥɢ INDEX DIRECTORY='ɤɚɬɚɥɨɝ', ɜɵ ɦɨɠɟɬɟ ɭɤɚɡɚɬɶ, ɝɞɟ ɦɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ MyISAM ɞɨɥɠɟɧ ɪɚɡɦɟɳɚɬɶ ɮɚɣɥɵ ɞɚɧɧɵɯ ɢ ɢɧɞɟɤɫɨɜ. ɂɦɟɣɬɟ ɜ ɜɢɞɭ, ɱɬɨ ɧɭɠɧɨ ɭɤɚɡɵɜɚɬɶ ɩɨɥɧɵɣ ɩɭɬɶ, ɚ ɧɟ ɨɬɧɨɫɢɬɟɥɶɧɵɣ.
ɗɬɢ ɨɩɰɢɢ ɪɚɛɨɬɚɸɬ ɬɨɥɶɤɨ ɫ ɬɚɛɥɢɰɚɦɢ MyISAM, ɧɚɱɢɧɚɹ ɫ MySQL 4.0 ɢ ɜɵɲɟ,
ɤɨɝɞɚ ɜɵ ɧɟ ɩɪɢɦɟɧɹɟɬɟ ɨɩɰɢɸ --skip-symbolic-links. ȼɚɲɚ ɨɩɟɪɚɰɢɨɧɧɚɹ ɫɢɫɬɟɦɚ ɬɚɤɠɟ ɞɨɥɠɧɚ ɢɦɟɬɶ ɪɚɛɨɬɚɸɳɢɣ, ɛɟɡɨɩɚɫɧɵɣ ɜ ɨɬɧɨɲɟɧɢɢ ɩɨɬɨɤɨɜ ɫɢɫɬɟɦɧɵɣ ɜɵɡɨɜ realpath().
6.2. Îïåðàòîðû îïðåäåëåíèÿ äàííûõ
271
ɇɚɱɢɧɚɹ ɫ MySQL 3.23, ɜɵ ɦɨɠɟɬɟ ɫɨɡɞɚɜɚɬɶ ɨɞɧɭ ɬɚɛɥɢɰɭ ɢɡ ɞɪɭɝɨɣ, ɞɨɛɚɜɥɹɹ
SELECT ɜ ɤɨɧɟɰ ɨɩɟɪɚɬɨɪɚ CREATE TABLE:
CREATE TABLE ɢɦɹ_ɧɨɜɨɣ_ɬɚɛɥɢɰɵ SELECT * FROM ɢɦɹ_ɢɫɯɨɞɧɨɣ_ɬɚɛɥɢɰɵ;
MySQL ɫɨɡɞɚɫɬ ɧɨɜɵɟ ɫɬɨɥɛɰɵ ɞɥɹ ɜɫɟɯ ɷɥɟɦɟɧɬɨɜ SELECT. ɇɚɩɪɢɦɟɪ:
mysql> CREATE TABLE test (a INT NOT NULL AUTO_INCREMENT,
-> PRIMARY KEY (a), KEY(b))
-> TYPE=MyISAM SELECT b,c FROM test2;
Ɂɞɟɫɶ ɫɨɡɞɚɟɬɫɹ ɬɚɛɥɢɰɚ MyISAM ɫ ɬɪɟɦɹ ɫɬɨɥɛɰɚɦɢ, a, b ɢ c. ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ
ɫɬɨɥɛɰɵ ɢɡ ɨɩɟɪɚɬɨɪɚ SELECT ɞɨɛɚɜɥɹɸɬɫɹ ɜ ɩɪɚɜɭɸ ɱɚɫɬɶ ɬɚɛɥɢɰɵ, ɧɟ ɩɟɪɟɦɟɲɢɜɚɹɫɶ ɫ
ɢɦɟɸɳɢɦɢɫɹ. Ɋɚɫɫɦɨɬɪɢɦ ɫɥɟɞɭɸɳɢɣ ɩɪɢɦɟɪ:
mysql> SELECT * FROM foo;
+---+
| n |
+---+
| 1 |
+---+
mysql> CREATE TABLE bar (m INT) SELECT n FROM foo;
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM bar;
+------+---+
| m
| n |
+------+---+
|NULL | 1 |
+------+---+
1 row in set (0.00 sec)
Ⱦɥɹ ɤɚɠɞɨɣ ɫɬɪɨɤɢ ɬɚɛɥɢɰɵ foo ɜɫɬɚɜɥɹɟɬɫɹ ɫɬɪɨɤɚ ɜ bar, ɫɨ ɡɧɚɱɟɧɢɹɦɢ, ɜɵɛɪɚɧɧɵɦɢ ɢɡ foo, ɢ ɡɧɚɱɟɧɢɹɦɢ ɩɨ ɭɦɨɥɱɚɧɢɸ ɞɥɹ ɧɨɜɵɯ ɫɬɨɥɛɰɨɜ.
ȿɫɥɢ ɩɪɢ ɷɬɨɦ ɩɪɨɢɫɯɨɞɢɬ ɤɚɤɚɹ-ɥɢɛɨ ɨɲɢɛɤɚ, ɧɨɜɚɹ ɬɚɛɥɢɰɚ ɧɟ ɫɨɡɞɚɟɬɫɹ.
CREATE TABLE...SELECT ɧɟ ɫɨɡɞɚɟɬ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɤɚɤɢɟ-ɥɢɛɨ ɢɧɞɟɤɫɵ. Ɍɚɤ ɫɞɟɥɚɧɨ
ɧɚɦɟɪɟɧɨ, ɱɬɨɛɵ ɨɛɟɫɩɟɱɢɬɶ ɦɚɤɫɢɦɚɥɶɧɨ ɜɨɡɦɨɠɧɭɸ ɝɢɛɤɨɫɬɶ ɞɚɧɧɨɝɨ ɨɩɟɪɚɬɨɪɚ. ȿɫɥɢ
ɜɵ ɯɨɬɢɬɟ ɢɦɟɬɶ ɢɧɞɟɤɫɵ ɜ ɫɨɡɞɚɜɚɟɦɨɣ ɬɚɛɥɢɰɟ, ɬɨ ɞɨɥɠɧɵ ɛɭɞɟɬɟ ɭɤɚɡɚɬɶ ɫɥɟɞɭɸɳɟɟ
ɩɟɪɟɞ ɨɩɟɪɚɬɨɪɨɦ SELECT:
mysql> CREATE TABLE bar (UNIQUE (n)) SELECT n FROM foo;
ɉɪɢ ɷɬɨɦ ɦɨɝɭɬ ɩɪɨɢɫɯɨɞɢɬɶ ɧɟɤɨɬɨɪɵɟ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɬɢɩɨɜ ɫɬɨɥɛɰɨɜ. ɇɚɩɪɢɦɟɪ,
ɧɟ ɫɨɯɪɚɧɹɟɬɫɹ ɚɬɪɢɛɭɬ AUTO_INCREMENT, ɢ ɫɬɨɥɛɰɵ VARCHAR ɦɟɧɹɸɬ ɬɢɩ ɧɚ CHAR.
ɉɪɢ ɫɨɡɞɚɧɢɢ ɬɚɛɥɢɰɵ ɨɩɟɪɚɬɨɪɨɦ CREATE TABLE...SELECT ɭɛɟɞɢɬɟɫɶ, ɱɬɨ ɜɫɟ ɜɵɡɨɜɵ ɮɭɧɤɰɢɣ ɢ ɜɵɪɚɠɟɧɢɹ, ɢɫɩɨɥɶɡɭɟɦɵɟ ɜ ɡɚɩɪɨɫɟ, ɫɧɚɛɠɟɧɵ ɩɫɟɜɞɨɧɢɦɚɦɢ. ȿɫɥɢ ɷɬɨɝɨ
ɧɟ ɫɞɟɥɚɬɶ, ɬɨ ɨɩɟɪɚɬɨɪ CREATE ɡɚɜɟɪɲɢɬɫɹ ɚɜɚɪɢɣɧɨ, ɢɥɢ ɠɟ ɜɵ ɩɨɥɭɱɢɬɟ ɧɟɠɟɥɚɬɟɥɶɧɵɟ ɢɦɟɧɚ ɫɬɨɥɛɰɨɜ ɜ ɧɨɜɨɣ ɬɚɛɥɢɰɟ.
CREATE TABLE artists_and_works
SELECT artist.name, COUNT(work.artist_id) AS number_of_works
FROM artist LEFT JOIN work ON artist.id = work.artist_id
GROUP BY artist.id;
ɇɚɱɢɧɚɹ ɫ MySQL 4.1, ɬɢɩɵ ɫɝɟɧɟɪɢɪɨɜɚɧɧɵɯ ɫɬɨɥɛɰɨɜ ɦɨɠɧɨ ɡɚɞɚɜɚɬɶ ɹɜɧɨ:
272
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
CREATE TABLE foo (a TINYINT NOT NULL) SELECT b+1 AS a FROM bar;
ȼ MySQL 4.1 ɜɵ ɬɚɤɠɟ ɦɨɠɟɬɟ ɩɪɢɦɟɧɢɬɶ LIKE ɞɥɹ ɫɨɡɞɚɧɢɹ ɩɭɫɬɨɣ ɬɚɛɥɢɰɵ ɧɚ ɨɫɧɨɜɟ ɨɩɪɟɞɟɥɟɧɢɹ ɞɪɭɝɨɣ ɬɚɛɥɢɰɵ, ɜɤɥɸɱɚɹ ɥɸɛɵt ɚɬɪɢɛɭɬɵ ɫɬɨɥɛɰɨɜ ɢ ɢɧɞɟɤɫɵ, ɤɨɬɨɪɵɟ ɢɦɟɸɬɫɹ ɜ ɢɫɯɨɞɧɨɣ ɬɚɛɥɢɰɟ:
CREATE TABLE ɢɦɹ_ɧɨɜɨɣ_ɬɚɛɥɢɰɵ LIKE ɢɦɹ_ɢɫɯɨɞɧɨɣ_ɬɚɛɥɢɰɵ;
CREATE TABLE...LIKE ɧɟ ɤɨɩɢɪɭɟɬ ɨɩɰɢɢ DATA DIRECTORY ɢɥɢ INDEX DIRECTORY, ɤɨɬɨɪɵɟ ɛɵɥɢ ɫɩɟɰɢɮɢɰɢɪɨɜɚɧɵ ɜ ɢɫɯɨɞɧɨɣ ɬɚɛɥɢɰɟ.
ȼɵ ɦɨɠɟɬɟ ɩɪɟɞɜɚɪɢɬɶ SELECT ɫɥɨɜɚɦɢ IGNORE ɢɥɢ REPLACE, ɱɬɨɛɵ ɭɤɚɡɚɬɶ, ɤɚɤ ɧɭɠɧɨ
ɨɛɪɚɛɚɬɵɜɚɬɶ ɡɚɩɢɫɢ ɫ ɞɭɛɥɢɪɨɜɚɧɧɵɦɢ ɡɧɚɱɟɧɢɹɦɢ ɭɧɢɤɚɥɶɧɵɯ ɤɥɸɱɟɣ. ȿɫɥɢ ɭɤɚɡɚɧɨ
IGNORE, ɧɨɜɵɟ ɡɚɩɢɫɢ ɫ ɞɭɛɥɢɪɨɜɚɧɧɵɦɢ ɭɧɢɤɚɥɶɧɵɦɢ ɤɥɸɱɚɦɢ ɨɬɛɪɚɫɵɜɚɸɬɫɹ. ȿɫɥɢ
ɠɟ ɭɤɚɡɚɧɨ REPLACE, ɧɨɜɵɟ ɡɚɩɢɫɢ ɡɚɦɟɧɹɸɬ ɫɭɳɟɫɬɜɭɸɳɢɟ ɫ ɬɚɤɢɦɢ ɠɟ ɡɧɚɱɟɧɢɹɦɢ
ɭɧɢɤɚɥɶɧɵɯ ɤɥɸɱɟɣ. ȿɫɥɢ ɧɟ ɭɤɚɡɚɧɨ ɧɢ IGNORE, ɧɢ REPLACE, ɬɨ ɩɨɹɜɥɟɧɢɟ ɡɚɩɢɫɟɣ ɫ
ɞɭɛɥɢɪɨɜɚɧɧɵɦɢ ɡɧɚɱɟɧɢɹɦɢ ɤɥɸɱɟɣ ɩɪɢɜɨɞɢɬ ɤ ɨɲɢɛɤɟ.
ɑɬɨɛɵ ɝɚɪɚɧɬɢɪɨɜɚɬɶ, ɱɬɨ ɠɭɪɧɚɥ ɨɛɧɨɜɥɟɧɢɹ/ɛɢɧɚɪɧɵɣ ɠɭɪɧɚɥ ɦɨɝɭɬ ɛɵɬɶ ɢɫɩɨɥɶɡɨɜɚɧɵ ɞɥɹ ɩɟɪɟɫɨɡɞɚɧɢɹ ɢɫɯɨɞɧɵɯ ɬɚɛɥɢɰ, MySQL ɧɟ ɪɚɡɪɟɲɚɟɬ ɩɚɪɚɥɥɟɥɶɧɵɟ ɜɫɬɚɜɤɢ
ɜɨ ɜɪɟɦɹ ɪɚɛɨɬɵ CREATE TABLE...SELECT.
6.2.5.1. Ñîçäàíèå âíåøíèõ êëþ÷åé
ɇɚɱɢɧɚɹ ɫ MySQL 3.23.44, ɦɟɯɚɧɢɡɦ InnoDB ɨɫɧɚɳɟɧ ɨɝɪɚɧɢɱɟɧɢɹɦɢ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ.
ɋɢɧɬɚɤɫɢɫ ɨɩɪɟɞɟɥɟɧɢɹ ɜɧɟɲɧɟɝɨ ɤɥɸɱɚ InnoDB ɜɵɝɥɹɞɢɬ ɬɚɤ:
[CONSTRAINT ɫɢɦɜɨɥ] FOREIGN KEY [ɢɞɟɧɬɢɮɢɤɚɬɨɪ]
REFERENCES ɢɦɹ_ɬɚɛɥɢɰɵ (ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ,
[ON DELETE {CASCADE | SET NULL | NO ACTION |
[ON UPDATE {CASCADE | SET NULL | NO ACTION |
(ɢɦɹ_ɫɬɨɥɛɰɚ_ɢɧɞɟɤɫɚ, ...)
...)
RESTRICT | SET DEFAULT}]
RESTRICT | SET DEFAULT}]
Ɉɛɟ ɬɚɛɥɢɰɵ ɞɨɥɠɧɵ ɢɦɟɬɶ ɬɢɩ InnoDB. ȼ ɫɫɵɥɚɸɳɟɣɫɹ ɬɚɛɥɢɰɟ ɞɨɥɠɟɧ ɛɵɬɶ ɢɧɞɟɤɫ, ɭ ɤɨɬɨɪɨɝɨ ɫɬɨɥɛɟɰ ɜɧɟɲɧɟɝɨ ɤɥɸɱɚ ɹɜɥɹɟɬɫɹ ɩɟɪɜɵɦ ɜ ɫɩɢɫɤɟ ɤɥɸɱɟɜɵɯ ɫɬɨɥɛɰɨɜ.
ɍ ɬɚɛɥɢɰɵ, ɧɚ ɤɨɬɨɪɭɸ ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɫɫɵɥɤɚ, ɞɨɥɠɟɧ ɛɵɬɶ ɢɧɞɟɤɫ, ɜ ɤɥɸɱɟ ɤɨɬɨɪɨɝɨ
ɫɬɨɥɛɟɰ, ɧɚ ɤɨɬɨɪɵɣ ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɫɫɵɥɤɚ, ɭɤɚɡɚɧ ɩɟɪɜɵɦ. ɂɧɞɟɤɫɵ ɧɚ ɩɪɟɮɢɤɫɚɯ
ɫɬɨɥɛɰɨɜ ɞɥɹ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ ɧɟ ɩɨɞɞɟɪɠɢɜɚɸɬɫɹ.
InnoDB ɬɪɟɛɭɟɬ ɢɧɞɟɤɫɨɜ ɧɚ ɜɧɟɲɧɢɯ ɢ ɫɫɵɥɚɟɦɵɯ ɤɥɸɱɚɯ, ɱɬɨɛɵ ɩɪɨɜɟɪɤɚ ɨɝɪɚɧɢɱɟɧɢɣ ɜɧɟɲɧɟɝɨ ɤɥɸɱɚ ɜɵɩɨɥɧɹɥɚɫɶ ɛɵɫɬɪɟɟ ɢ ɧɟ ɬɪɟɛɨɜɚɥɚ ɫɤɚɧɢɪɨɜɚɧɢɹ ɬɚɛɥɢɰ.
ɇɚɱɢɧɚɹ ɫ MySQL 4.1.2, ɷɬɢ ɢɧɞɟɤɫɵ ɫɨɡɞɚɸɬɫɹ ɚɜɬɨɦɚɬɢɱɟɫɤɢ. ȼ ɛɨɥɟɟ ɫɬɚɪɵɯ ɜɟɪɫɢɹɯ ɢɧɞɟɤɫɵ ɞɨɥɠɧɵ ɛɵɥɢ ɫɨɡɞɚɜɚɬɶɫɹ ɹɜɧɨ, ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ ɩɨɩɵɬɤɢ ɫɨɡɞɚɧɢɹ
ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ ɡɚɜɟɪɲɚɥɢɫɶ ɨɲɢɛɤɨɣ.
ɋɜɹɡɚɧɧɵɟ ɫɬɨɥɛɰɵ ɜɨ ɜɧɟɲɧɟɦ ɤɥɸɱɟ ɢ ɜ ɤɥɸɱɟ, ɧɚ ɤɨɬɨɪɵɣ ɨɧɢ ɫɫɵɥɚɸɬɫɹ, ɞɨɥɠɧɵ ɢɦɟɬɶ ɨɞɢɧɚɤɨɜɵɣ ɜɧɭɬɪɟɧɧɢɣ ɬɢɩ ɞɚɧɧɵɯ InnoDB, ɱɬɨɛɵ ɢɯ ɦɨɠɧɨ ɛɵɥɨ ɫɪɚɜɧɢɜɚɬɶ
ɛɟɡ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɬɢɩɨɜ. Ɋɚɡɦɟɪ ɢ ɧɚɥɢɱɢɟ ɡɧɚɤɚ ɫɬɨɥɛɰɨɜ ɰɟɥɨɱɢɫɥɟɧɧɵɯ ɬɢɩɨɜ
ɞɨɥɠɧɵ ɛɵɬɶ ɨɞɢɧɚɤɨɜɵɦɢ. Ⱦɥɢɧɵ ɫɬɪɨɤɨɜɵɯ ɬɢɩɨɜ ɧɟ ɨɛɹɡɚɬɟɥɶɧɨ ɞɨɥɠɧɵ ɛɵɬɶ
ɪɚɜɧɵ. ȿɫɥɢ ɜɵ ɜɵɩɨɥɧɹɟɬɟ ɞɟɣɫɬɜɢɟ SET NULL, ɭɛɟɞɢɬɟɫɶ, ɱɬɨ ɫɬɨɥɛɟɰ ɞɨɱɟɪɧɟɣ ɬɚɛɥɢɰɵ ɧɟ ɨɛɴɹɜɥɟɧ ɤɚɤ NOT NULL.
ȿɫɥɢ MySQL ɫɨɨɛɳɚɟɬ ɨɛ ɨɲɢɛɤɟ 1005 ɩɪɢ ɩɨɩɵɬɤɟ ɜɵɩɨɥɧɢɬɶ CREATE TABLE, ɢ ɫɨɨɛɳɟɧɢɟ ɨɛ ɨɲɢɛɤɟ ɫɫɵɥɚɟɬɫɹ ɧɚ ɧɨɦɟɪ errno 150, ɷɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɫɨɡɞɚɧɢɟ ɬɚɛɥɢɰɵ
ɡɚɜɟɪɲɢɥɨɫɶ ɧɟɭɞɚɱɟɣ, ɩɨɬɨɦɭ ɱɬɨ ɨɝɪɚɧɢɱɟɧɢɟ ɜɧɟɲɧɟɝɨ ɤɥɸɱɚ ɧɟ ɛɵɥɨ ɤɨɪɪɟɤɬɧɨ
ɫɮɨɪɦɢɪɨɜɚɧɨ. ɉɨɞɨɛɧɵɦ ɠɟ ɨɛɪɚɡɨɦ, ɟɫɥɢ ALTER TABLE ɡɚɜɟɪɲɚɟɬɫɹ ɚɜɚɪɢɣɧɨ ɢ ɜɨɡɜɪɚɳɚɟɬ ɫɫɵɥɤɭ ɧɚ ɧɨɦɟɪ errno 150, ɷɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɨɩɪɟɞɟɥɟɧɢɟ ɜɧɟɲɧɟɝɨ ɤɥɸɱɚ ɧɟɜɟɪɧɨ ɫɮɨɪɦɢɪɨɜɚɧɨ ɞɥɹ ɢɡɦɟɧɹɟɦɨɣ ɬɚɛɥɢɰɵ. ɇɚɱɢɧɚɹ ɫ MySQL 4.0.13, ɜɵ ɦɨɠɟɬɟ ɢɫ-
6.2. Îïåðàòîðû îïðåäåëåíèÿ äàííûõ
273
ɩɨɥɶɡɨɜɚɬɶ SHOW INNODB STATUS, ɱɬɨɛɵ ɨɬɨɛɪɚɡɢɬɶ ɞɟɬɚɥɶɧɨɟ ɨɛɴɹɫɧɟɧɢɟ ɩɨɫɥɟɞɧɟɣ
ɩɪɨɢɡɨɲɟɞɲɟɣ ɧɚ ɫɟɪɜɟɪɟ ɨɲɢɛɤɢ, ɫɜɹɡɚɧɧɨɣ ɫ ɜɧɟɲɧɢɦ ɤɥɸɱɨɦ.
ɇɚɱɢɧɚɹ ɫ MySQL 3.23.50, InnoDB ɧɟ ɩɪɨɜɟɪɹɟɬ ɫɫɵɥɨɱɧɭɸ ɰɟɥɨɫɬɧɨɫɬɶ ɜɧɟɲɧɢɯ
ɤɥɸɱɟɣ ɢɥɢ ɤɥɸɱɟɣ, ɧɚ ɤɨɬɨɪɵɟ ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɫɫɵɥɤɚ, ɟɫɥɢ ɢɯ ɫɬɨɥɛɰɵ ɫɨɞɟɪɠɚɬ ɡɧɚɱɟɧɢɟ NULL.
Ɉɬɤɥɨɧɟɧɢɟ ɨɬ ɫɬɚɧɞɚɪɬɚ SQL. ȿɫɥɢ ɜ ɪɨɞɢɬɟɥɶɫɤɨɣ ɬɚɛɥɢɰɟ ɟɫɬɶ ɧɟɫɤɨɥɶɤɨ ɫɬɪɨɤ,
ɢɦɟɸɳɢɯ ɨɞɢɧɚɤɨɜɵɟ ɡɧɚɱɟɧɢɹ ɤɥɸɱɟɣ, ɧɚ ɤɨɬɨɪɵɟ ɫɫɵɥɚɟɬɫɹ ɜɧɟɲɧɢɣ ɤɥɸɱ, InnoDB
ɩɪɢ ɩɪɨɜɟɪɤɟ ɨɝɪɚɧɢɱɟɧɢɣ ɜɧɟɲɧɟɝɨ ɤɥɸɱɚ ɩɨɫɬɭɩɚɟɬ ɬɚɤ, ɛɭɞɬɨ ɪɨɞɢɬɟɥɶɫɤɢɯ ɫɬɪɨɤ ɫ
ɨɞɢɧɚɤɨɜɵɦɢ ɡɧɚɱɟɧɢɹɦɢ ɤɥɸɱɟɣ ɧɟ ɫɭɳɟɫɬɜɭɟɬ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɜɵ ɨɩɪɟɞɟɥɢɥɢ ɨɝɪɚɧɢɱɟɧɢɟ ɬɢɩɚ RESTRICT, ɢ ɫɭɳɟɫɬɜɭɸɬ ɞɨɱɟɪɧɢɟ ɡɚɩɢɫɢ ɫ ɧɟɫɤɨɥɶɤɢɦɢ ɪɨɞɢɬɟɥɶɫɤɢɦɢ
ɡɚɩɢɫɹɦɢ, InnoDB ɧɟ ɩɨɡɜɨɥɹɟɬ ɭɞɚɥɹɬɶ ɧɢ ɨɞɧɭ ɢɡ ɷɬɢɯ ɪɨɞɢɬɟɥɶɫɤɢɯ ɡɚɩɢɫɟɣ.
ɇɚɱɢɧɚɹ ɫ MySQL 3.23.50, ɜɵ ɬɚɤɠɟ ɦɨɠɟɬɟ ɚɫɫɨɰɢɢɪɨɜɚɬɶ ɤɨɧɫɬɪɭɤɰɢɢ ON DELETE
CASCADE ɢɥɢ ON DELETE SET NULL ɫ ɨɝɪɚɧɢɱɟɧɢɟɦ ɜɧɟɲɧɟɝɨ ɤɥɸɱɚ. ɋɨɨɬɜɟɬɫɬɜɭɸɳɚɹ
ɨɩɰɢɹ ON UPDATE ɞɨɫɬɭɩɧɚ, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ 4.0.8. ȿɫɥɢ ɭɤɚɡɚɧɨ ON DELETE CASCADE ɢ
ɭɞɚɥɹɟɬɫɹ ɫɬɪɨɤɚ ɜ ɪɨɞɢɬɟɥɶɫɤɨɣ ɬɚɛɥɢɰɟ, ɬɨ ɞɨɱɟɪɧɢɟ ɡɚɩɢɫɢ, ɭ ɤɨɬɨɪɵɯ ɡɧɚɱɟɧɢɟ
ɜɧɟɲɧɟɝɨ ɤɥɸɱɚ ɫɨɜɩɚɞɚɟɬ ɫɨ ɡɧɚɱɟɧɢɟɦ ɫɫɵɥɚɟɦɨɝɨ ɤɥɸɱɚ ɜ ɪɨɞɢɬɟɥɶɫɤɨɣ ɡɚɩɢɫɢ, ɬɚɤɠɟ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɭɞɚɥɹɸɬɫɹ. ȿɫɥɢ ɭɤɚɡɚɧɨ ON DELETE SET NULL ɢ ɭɞɚɥɹɟɬɫɹ ɫɬɪɨɤɚ ɜ
ɪɨɞɢɬɟɥɶɫɤɨɣ ɬɚɛɥɢɰɟ, ɬɨ ɞɨɱɟɪɧɢɟ ɡɚɩɢɫɢ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɨɛɧɨɜɥɹɸɬɫɹ ɬɚɤɢɦ ɨɛɪɚɡɨɦ,
ɱɬɨ ɫɬɨɥɛɰɵ ɜɧɟɲɧɟɝɨ ɤɥɸɱɚ ɩɪɢɧɢɦɚɸɬ ɡɧɚɱɟɧɢɟ NULL.
SET DEFAULT ɪɚɫɩɨɡɧɚɟɬɫɹ, ɧɨ ɢɝɧɨɪɢɪɭɟɬɫɹ.
InnoDB ɜɵɩɨɥɧɹɟɬ ɤɚɫɤɚɞɧɵɟ ɨɩɟɪɚɰɢɢ ɩɨ ɚɥɝɨɪɢɬɦɭ ɩɨɢɫɤɚ ɜ ɝɥɭɛɢɧɭ, ɧɚ ɨɫɧɨɜɟ ɡɚɩɢɫɟɣ ɜ ɢɧɞɟɤɫɚɯ, ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɯ ɨɝɪɚɧɢɱɟɧɢɹɦ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ.
Ɉɬɤɥɨɧɟɧɢɟ ɨɬ ɫɬɚɧɞɚɪɬɚ SQL. ȿɫɥɢ ON UPDATE CASCADE ɢɥɢ ON UPDATE SET NULL
ɪɟɤɭɪɫɢɜɧɨ ɨɛɧɨɜɥɹɸɬ ɬɭ ɠɟ ɬɚɛɥɢɰɭ, ɤɨɬɨɪɚɹ ɭɠɟ ɨɛɧɨɜɥɹɟɬɫɹ ɤɚɫɤɚɞɧɵɦ ɫɩɨɫɨɛɨɦ,
ɷɬɨ ɪɚɛɨɬɚɟɬ ɤɚɤ ɩɪɢ RESTRICT. ɗɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɜɵ ɧɟ ɦɨɠɟɬɟ ɢɦɟɬɶ ɨɩɟɪɚɰɢɢ ON
UPDATE CASCADE ɢɥɢ ON UPDATE SET NULL, ɫɫɵɥɚɸɳɢɟɫɹ ɫɚɦɢ ɧɚ ɫɟɛɹ. Ɍɚɤ ɫɞɟɥɚɧɨ ɞɥɹ
ɩɪɟɞɨɬɜɪɚɳɟɧɢɹ ɛɟɫɤɨɧɟɱɧɨɝɨ ɡɚɰɢɤɥɢɜɚɧɢɹ ɤɚɫɤɚɞɧɵɯ ɨɛɧɨɜɥɟɧɢɣ. ɋ ɞɪɭɝɨɣ ɫɬɨɪɨɧɵ,
ɫɫɵɥɚɸɳɢɟɫɹ ɧɚ ɫɟɛɹ ON DELETE SET NULL ɞɨɩɭɫɤɚɸɬɫɹ, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ 4.0.13. ɋɫɵɥɚɸɳɢɟɫɹ ɧɚ ɫɟɛɹ ON DELETE CASCADE ɜɨɡɦɨɠɧɵ ɫ ɬɟɯ ɩɨɪ, ɤɚɤ ɪɟɚɥɢɡɨɜɚɧɨ ON DELETE.
ɉɪɨɫɬɨɣ ɩɪɢɦɟɪ, ɜ ɤɨɬɨɪɨɦ ɫɜɹɡɵɜɚɸɬɫɹ ɪɨɞɢɬɟɥɶɫɤɚɹ ɢ ɞɨɱɟɪɧɹɹ ɬɚɛɥɢɰɵ ɱɟɪɟɡ ɨɞɧɨɫɬɨɥɛɰɨɜɵɣ ɜɧɟɲɧɢɣ ɤɥɸɱ:
CREATE TABLE parent(id INT NOT NULL,
PRIMARY KEY (id)
) TYPE=INNODB;
CREATE TABLE child(id INT, parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE CASCADE
) TYPE=INNODB;
Ȼɨɥɟɟ ɫɥɨɠɧɵɣ ɩɪɢɦɟɪ, ɜ ɤɨɬɨɪɨɦ ɬɚɛɥɢɰɚ product_order ɢɦɟɟɬ ɜɧɟɲɧɢɣ ɤɥɸɱ ɧɚ
ɞɜɟ ɞɪɭɝɢɟ ɬɚɛɥɢɰɵ. Ɉɞɢɧ ɢɯ ɧɢɯ ɫɫɵɥɚɟɬɫɹ ɧɚ ɞɜɭɯɫɬɨɥɛɰɨɜɵɣ ɢɧɞɟɤɫ ɜ ɬɚɛɥɢɰɟ
product, ɚ ɞɪɭɝɨɣ – ɧɚ ɨɞɧɨɫɬɨɥɛɰɨɜɵɣ ɜ ɬɚɛɥɢɰɟ customer:
CREATE TABLE product (category INT NOT NULL, id INT NOT NULL,
price DECIMAL,
PRIMARY KEY(category, id)) TYPE=INNODB;
274
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
CREATE TABLE customer (id INT NOT NULL,
PRIMARY KEY (id)) TYPE=INNODB;
CREATE TABLE product_order (no INT NOT NULL AUTO_INCREMENT,
product_category INT NOT NULL,
product_id INT NOT NULL,
customer_id INT NOT NULL,
PRIMARY KEY(no),
INDEX (product_category, product_id),
FOREIGN KEY (product_category, product_id)
REFERENCES product(category, id)
ON UPDATE CASCADE ON DELETE RESTRICT,
INDEX (customer_id),
FOREIGN KEY (customer_id)
REFERENCES customer(id)) TYPE=INNODB;
ɇɚɱɢɧɚɹ ɫ MySQL 3.23.50, ɚɧɚɥɢɡɚɬɨɪ ɜɵɪɚɠɟɧɢɣ InnoDB ɩɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ ɨɛɪɚɬɧɵɟ ɤɚɜɵɱɤɢ ɜɨɤɪɭɝ ɢɦɟɧ ɬɚɛɥɢɰ ɢ ɫɬɨɥɛɰɨɜ ɜ ɤɨɧɫɬɪɭɤɰɢɹɯ FOREIGN KEY...
REFERENCES... ɇɚɱɢɧɚɹ ɫ MySQL 4.0.5, ɚɧɚɥɢɡɚɬɨɪ ɜɵɪɚɠɟɧɢɣ InnoDB ɩɪɢɧɢɦɚɟɬ ɜɨ
ɜɧɢɦɚɧɢɟ ɭɫɬɚɧɨɜɤɭ ɫɢɫɬɟɦɧɨɣ ɩɟɪɟɦɟɧɧɨɣ lower_case_table_names.
6.2.5.2. Íåÿâíûå èçìåíåíèÿ ñïåöèôèêàöèé ñòîëáöîâ
ȼ ɧɟɤɨɬɨɪɵɯ ɫɥɭɱɚɹɯ MySQL ɛɟɡ ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ ɢɡɦɟɧɹɟɬ ɫɩɟɰɢɮɢɤɚɰɢɢ ɫɬɨɥɛɰɨɜ
ɩɨ ɫɪɚɜɧɟɧɢɸ ɫ ɬɟɦɢ, ɱɬɨ ɛɵɥɢ ɡɚɞɚɧɵ ɨɩɟɪɚɬɨɪɨɦ CREATE TABLE ɢɥɢ ALTER TABLE:
ɞɥɢɧɨɣ ɦɟɧɟɟ ɱɟɬɵɪɟɯ ɫɢɦɜɨɥɨɜ ɩɨɥɭɱɚɸɬ ɬɢɩ CHAR.
ɥɸɛɨɣ ɢɡ ɫɬɨɥɛɰɨɜ ɬɚɛɥɢɰɵ ɢɦɟɟɬ ɩɟɪɟɦɟɧɧɭɸ ɞɥɢɧɭ, ɜɫɹ ɫɬɪɨɤɚ ɜ ɪɟɡɭɥɶɬɚɬɟ ɢɦɟɟɬ ɩɟɪɟɦɟɧɧɭɸ ɞɥɢɧɭ. ɉɨɷɬɨɦɭ, ɟɫɥɢ ɬɚɛɥɢɰɚ ɫɨɞɟɪɠɢɬ ɥɸɛɨɣ ɫɬɨɥɛɟɰ ɫ
ɬɢɩɨɦ ɩɟɪɟɦɟɧɧɨɣ ɞɥɢɧɵ (VARCHAR, TEXT ɢɥɢ BLOB), ɜɫɟ ɫɬɨɥɛɰɵ CHAR ɞɥɢɧɨɣ ɛɨɥɟɟ ɬɪɟɯ ɫɢɦɜɨɥɨɜ ɫɬɚɧɨɜɹɬɫɹ VARCHAR. ɗɬɨ ɧɟ ɜɥɢɹɟɬ ɧɚ ɬɨ, ɤɚɤ ɫɬɨɥɛɰɵ ɛɭɞɭɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ. ȼ MySQL ɬɢɩ VARCHAR – ɷɬɨ ɩɪɨɫɬɨ ɞɪɭɝɨɣ ɫɩɨɫɨɛ ɯɪɚɧɟɧɢɹ ɫɢɦɜɨɥɨɜ. MySQL ɜɵɩɨɥɧɹɟɬ ɷɬɨ ɩɪɟɨɛɪɚɡɨɜɚɧɢɟ, ɩɨɬɨɦɭ ɱɬɨ ɷɬɨ ɩɨɡɜɨɥɹɟɬ ɫɷɤɨɧɨɦɢɬɶ
ɦɟɫɬɨ ɧɚ ɞɢɫɤɟ ɢ ɭɫɤɨɪɹɟɬ ɨɩɟɪɚɰɢɢ ɪɚɛɨɬɵ ɫ ɬɚɛɥɢɰɟɣ.
„ɇɚɱɢɧɚɹ ɫ MySQL 4.1.0, ɫɬɨɥɛɰɵ ɬɢɩɚ CHAR ɢ VARCHAR ɞɥɢɧɨɣ ɛɨɥɟɟ 255 ɫɢɦɜɨɥɨɜ
ɩɪɟɨɛɪɚɡɭɸɬɫɹ ɤ ɦɢɧɢɦɚɥɶɧɨɦɭ ɬɢɩɭ TEXT, ɤɨɬɨɪɵɣ ɦɨɠɟɬ ɜɦɟɫɬɢɬɶ ɡɚɞɚɧɧɨɟ ɤɨɥɢɱɟɫɬɜɨ ɫɢɦɜɨɥɨɜ. ɇɚɩɪɢɦɟɪ, VARCHAR (500) ɩɪɟɨɛɪɚɡɭɟɬɫɹ ɜ TEXT, ɚ VARCHAR
(200000) – ɜ MEDIUMTEXT. ɗɬɨ ɫɪɟɞɫɬɜɨ ɨɛɟɫɩɟɱɟɧɢɹ ɫɨɜɦɟɫɬɢɦɨɫɬɢ.
„ɋɬɨɥɛɰɵ VARCHAR
„ȿɫɥɢ
ɨɬɨɛɪɚɠɟɧɢɹ ɬɢɩɚ TIMESTAMP ɢɫɤɥɸɱɟɧ ɢɡ MySQL 4.1 ɢɡ-ɡɚ ɢɡɦɟɧɟɧɢɣ, ɤɨɬɨɪɵɟ ɩɪɟɬɟɪɩɟɥ ɬɢɩ ɫɬɨɥɛɰɨɜ TIMESTAMP ɜ ɷɬɨɣ ɜɟɪɫɢɢ. Ⱦɨ MySQL 4.1 ɪɚɡɦɟɪ
ɨɬɨɛɪɚɠɟɧɢɹ TIMESTAMP ɞɨɥɠɟɧ ɛɵɥ ɛɵɬɶ ɜ ɩɪɟɞɟɥɚɯ ɨɬ 2 ɞɨ 14. ȿɫɥɢ ɭɤɚɡɚɬɶ ɪɚɡɦɟɪ ɨɬɨɛɪɚɠɟɧɢɹ 0 ɢɥɢ ɛɨɥɟɟ 14, ɨɧ ɩɪɢɧɢɦɚɥɫɹ ɪɚɜɧɵɦ 14. ɇɟɱɟɬɧɵɟ ɜɟɥɢɱɢɧɵ
ɪɚɡɦɟɪɚ ɨɬ 1 ɞɨ 13 ɨɤɪɭɝɥɹɥɢɫɶ ɞɨ ɛɥɢɠɚɣɲɟɝɨ ɛɨɥɶɲɟɝɨ ɱɟɬɧɨɝɨ.
„ɇɟɜɨɡɦɨɠɧɨ ɩɪɢɫɜɨɢɬɶ ɥɢɬɟɪɚɥɶɧɨɟ ɡɧɚɱɟɧɢɟ NULL ɫɬɨɥɛɰɭ TIMESTAMP. ɉɪɢɫɜɨɟɧɢɟ NULL ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɟɝɨ ɜ ɬɟɤɭɳɭɸ ɞɚɬɭ ɢ ɜɪɟɦɹ. ɉɨɫɤɨɥɶɤɭ ɫɬɨɥɛɰɵ
TIMESTAMP ɜɟɞɭɬ ɫɟɛɹ ɩɨɞɨɛɧɵɦ ɨɛɪɚɡɨɦ, ɚɬɪɢɛɭɬɵ NULL ɢ NOT NULL ɧɟɩɪɢɦɟɧɢɦɵ
ɤ ɧɢɦ ɜ ɩɪɢɜɵɱɧɨɦ ɫɦɵɫɥɟ ɢ ɩɨɬɨɦɭ ɢɝɧɨɪɢɪɭɸɬɫɹ. DESCRIBE ɢɦɹ_ɬɚɛɥɢɰɵ ɜɫɟɝɞɚ
ɫɨɨɛɳɚɟɬ, ɱɬɨ ɫɬɨɥɛɰɵ TIMESTAMP ɦɨɝɭɬ ɩɪɢɧɢɦɚɬɶ ɡɧɚɱɟɧɢɹ NULL.
„Ɋɚɡɦɟɪ
ɜɯɨɞɹɳɢɟ ɜ ɨɩɪɟɞɟɥɟɧɢɟ ɩɟɪɜɢɱɧɨɝɨ ɤɥɸɱɚ ɫɬɚɧɨɜɹɬɫɹ NOT NULL ɧɟɡɚɜɢɫɢɦɨ ɨɬ ɬɨɝɨ, ɡɚɞɚɧɨ ɥɢ ɷɬɨ ɹɜɧɵɦ ɨɛɪɚɡɨɦ.
„ɋɬɨɥɛɰɵ,
275
6.2. Îïåðàòîðû îïðåäåëåíèÿ äàííûõ
ɫ MySQL 3.23.51, ɡɚɜɟɪɲɚɸɳɢɟ ɩɪɨɛɟɥɵ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɭɞɚɥɹɸɬɫɹ ɢɡ
ɡɧɚɱɟɧɢɣ-ɱɥɟɧɨɜ ENUM ɢ SET ɩɪɢ ɫɨɡɞɚɧɢɢ ɬɚɛɥɢɰɵ.
„MySQL ɨɬɨɛɪɚɠɚɟɬ ɧɟɤɨɬɨɪɵɟ ɬɢɩɵ ɫɬɨɥɛɰɨɜ, ɢɫɩɨɥɶɡɭɟɦɵɟ ɞɪɭɝɢɦɢ ɩɨɫɬɚɜɳɢɤɚɦɢ SQL-ɫɟɪɜɟɪɨɜ, ɜ ɬɢɩɵ MySQL. ɋɦ. ɪɚɡɞɟɥ 4.7.
„ȿɫɥɢ ɜɵ ɜɤɥɸɱɢɬɟ ɤɨɧɫɬɪɭɤɰɢɸ USING, ɱɬɨɛɵ ɫɩɟɰɢɮɢɰɢɪɨɜɚɬɶ ɬɢɩ ɢɧɞɟɤɫɚ, ɤɨɬɨɪɵɣ ɧɟ ɞɨɩɭɫɬɢɦ ɞɥɹ ɞɚɧɧɨɝɨ ɦɟɯɚɧɢɡɦɚ ɯɪɚɧɟɧɢɹ, ɧɨ ɩɪɢ ɷɬɨɦ ɫɭɳɟɫɬɜɭɟɬ
ɞɪɭɝɨɣ ɬɢɩ ɢɧɞɟɤɫɨɜ, ɤɨɬɨɪɵɣ ɦɨɠɟɬ ɛɵɬɶ ɢɫɩɨɥɶɡɨɜɚɧ ɛɟɡ ɜɥɢɹɧɢɹ ɧɚ ɢɫɩɨɥɧɟɧɢɟ ɡɚɩɪɨɫɨɜ, ɬɨ ɦɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ ɢɫɩɨɥɶɡɭɟɬ ɷɬɨɬ ɬɢɩ.
ɑɬɨɛɵ ɭɜɢɞɟɬɶ, ɢɡɦɟɧɢɥ ɥɢ MySQL ɬɢɩ ɫɬɨɥɛɰɨɜ ɩɨ ɫɪɚɜɧɟɧɢɸ ɫ ɡɚɞɚɧɧɵɦ ɜɚɦɢ,
ɜɨɫɩɨɥɶɡɭɣɬɟɫɶ ɨɩɟɪɚɬɨɪɨɦ DESCRIBE ɢɥɢ SHOW CREATE TABLE ɩɨɫɥɟ ɫɨɡɞɚɧɢɹ ɢɥɢ ɢɡɦɟɧɟɧɢɹ ɬɚɛɥɢɰ.
ȿɫɥɢ ɜɵ ɫɠɢɦɚɟɬɟ ɬɚɛɥɢɰɭ ɫ ɩɨɦɨɳɶɸ myisampack, ɬɚɤɠɟ ɦɨɝɭɬ ɩɪɨɢɡɨɣɬɢ ɢ ɞɪɭɝɢɟ
ɢɡɦɟɧɟɧɢɹ ɬɢɩɨɜ ɫɬɨɥɛɰɨɜ.
„ɇɚɱɢɧɚɹ
6.2.6. Ñèíòàêñèñ DROP DATABASE
DROP DATABASE [IF EXISTS] ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ
DROP DATABASE ɭɧɢɱɬɨɠɚɟɬ ɜɫɟ ɬɚɛɥɢɰɵ ɜ ɛɚɡɟ ɞɚɧɧɵɯ ɢ ɭɞɚɥɹɟɬ ɛɚɡɭ ɞɚɧɧɵɯ. Ȼɭɞɶɬɟ
ɨɱɟɧɶ ɜɧɢɦɚɬɟɥɶɧɵ ɩɪɢ ɨɛɪɚɳɟɧɢɢ ɫ ɷɬɢɦ ɨɩɟɪɚɬɨɪɨɦ! Ⱦɥɹ ɢɫɩɨɥɶɡɨɜɚɧɢɹ DROP
DATABASE ɧɟɨɛɯɨɞɢɦɨ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ DROP ɜ ɛɚɡɟ ɞɚɧɧɵɯ.
ȼ MySQL 3.22 ɢ ɛɨɥɟɟ ɩɨɡɞɧɢɯ ɜɟɪɫɢɹɯ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɤɥɸɱɟɜɵɟ ɫɥɨɜɚ IF
EXISTS, ɱɬɨɛɵ ɩɪɟɞɨɬɜɪɚɬɢɬɶ ɨɲɢɛɤɭ, ɫɜɹɡɚɧɧɭɸ ɫ ɩɨɩɵɬɤɨɣ ɭɞɚɥɟɧɢɹ ɧɟɫɭɳɟɫɬɜɭɸɳɟɣ
ɛɚɡɵ ɞɚɧɧɵɯ.
ȿɫɥɢ ɜɵ ɩɪɢɦɟɧɹɟɬɟ DROP DATABASE ɤ ɛɚɡɟ, ɞɨɫɬɭɩ ɤ ɤɨɬɨɪɨɣ ɨɫɭɳɟɫɬɜɥɹɟɬɫɹ ɱɟɪɟɡ
ɫɢɦɜɨɥɢɱɟɫɤɢɟ ɫɫɵɥɤɢ, ɭɞɚɥɹɟɬɫɹ ɤɚɤ ɫɚɦɚ ɛɚɡɚ, ɬɚɤ ɢ ɫɢɦɜɨɥɢɱɟɫɤɢɟ ɫɫɵɥɤɢ.
ɇɚɱɢɧɚɹ ɫ MySQL 4.1.2, DROP DATABASE ɜɨɡɜɪɚɳɚɟɬ ɤɨɥɢɱɟɫɬɜɨ ɭɞɚɥɟɧɧɵɯ ɬɚɛɥɢɰ.
ɗɬɨ ɫɨɨɬɜɟɬɫɬɜɭɟɬ ɤɨɥɢɱɟɫɬɜɭ ɭɞɚɥɟɧɧɵɯ ɮɚɣɥɨɜ .frm.
Ɉɩɟɪɚɬɨɪ DROP DATABASE ɭɞɚɥɹɟɬ ɢɡ ɡɚɞɚɧɧɨɝɨ ɤɚɬɚɥɨɝɚ ɛɚɡɵ ɞɚɧɧɵɯ ɬɟ ɮɚɣɥɵ ɢ ɩɨɞɤɚɬɚɥɨɝɢ, ɤɨɬɨɪɵɟ MySQL ɦɨɠɟɬ ɫɨɡɞɚɬɶ ɩɪɢ ɧɨɪɦɚɥɶɧɨɣ ɨɩɟɪɚɰɢɢ:
„ȼɫɟ ɮɚɣɥɵ ɫɨ ɫɥɟɞɭɸɳɢɦɢ ɪɚɫɲɢɪɟɧɢɹɦɢ:
.BAK
.ISM
.MYI
„
.DAT
.ISM
.db
.HSH
.MRG
.frm
.ISD
.MYD
ȼɫɟ ɩɨɞɤɚɬɚɥɨɝɢ ɫ ɢɦɟɧɚɦɢ, ɜɤɥɸɱɚɸɳɢɦɢ ɲɟɫɬɧɚɞɰɚɬɟɪɢɱɧɵɟ ɱɢɫɥɚ 00-ff. ɗɬɨ
ɩɨɞɤɚɬɚɥɨɝɢ, ɢɫɩɨɥɶɡɭɟɦɵɟ ɞɥɹ RAID-ɬɚɛɥɢɰ.
„Ɏɚɣɥ db.opt,
ɟɫɥɢ ɨɧ ɟɫɬɶ.
ȿɫɥɢ ɜ ɤɚɬɚɥɨɝɟ ɛɚɡɵ ɞɚɧɧɵɯ ɨɫɬɚɧɭɬɫɹ ɤɚɤɢɟ-ɬɨ ɞɪɭɝɢɟ ɮɚɣɥɵ, ɤɪɨɦɟ ɩɟɪɟɱɢɫɥɟɧɧɵɯ,
MySQL ɧɟ ɫɦɨɠɟɬ ɭɞɚɥɢɬɶ ɤɚɬɚɥɨɝ ɛɚɡɵ. ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɜɵ ɞɨɥɠɧɵ ɜɪɭɱɧɭɸ ɭɞɚɥɢɬɶ ɜɫɟ
ɨɫɬɚɜɲɢɟɫɹ ɬɚɦ ɮɚɣɥɵ ɢ ɩɨɞɤɚɬɚɥɨɝɢ ɢ ɩɨɜɬɨɪɢɬɶ ɨɩɟɪɚɬɨɪ DROP DATABASE ɫɧɨɜɚ.
6.2.7. Ñèíòàêñèñ DROP INDEX
DROP INDEX ɢɦɹ_ɢɧɞɟɤɫɚ ON ɢɦɹ_ɬɚɛɥɢɰɵ
DROP INDEX ɭɧɢɱɬɨɠɚɟɬ ɢɧɞɟɤɫ ɫ ɢɦɟɧɟɦ ɢɦɹ_ɢɧɞɟɤɫɚ, ɩɪɢɧɚɞɥɟɠɚɳɢɣ ɬɚɛɥɢɰɟ
ɢɦɹ_ɬɚɛɥɢɰɵ. ȼ MySQL 3.22 ɢ ɜɵɲɟ DROP INDEX ɨɬɨɛɪɚɠɚɟɬɫɹ ɧɚ ALTER TABLE, ɱɬɨɛɵ
ɭɞɚɥɢɬɶ ɢɧɞɟɤɫ. ɋɦ. ɪɚɡɞɟɥ 6.2.2. Ⱦɨ MySQL 3.22 DROP INDEX ɧɢɱɟɝɨ ɧɟ ɞɟɥɚɥ.
276
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
6.2.8. Ñèíòàêñèñ DROP TABLE
DROP [TEMPORARY] TABLE [IF EXISTS]
ɢɦɹ_ɬɚɛɥɢɰɵ [, ɢɦɹ_ɬɚɛɥɢɰɵ] ...
[RESTRICT | CASCADE]
DROP TABLE ɭɞɚɥɹɟɬ ɨɞɧɭ ɢɥɢ ɛɨɥɟɟ ɬɚɛɥɢɰ. ȼɵ ɞɨɥɠɧɵ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ DROP ɞɥɹ
ɤɚɠɞɨɣ ɢɡ ɷɬɢɯ ɬɚɛɥɢɰ. ɗɬɢɦ ɨɩɟɪɚɬɨɪɨɦ ɭɞɚɥɹɸɬɫɹ ɜɫɟ ɞɚɧɧɵɟ ɬɚɛɥɢɰɵ, ɚ ɬɚɤɠɟ ɟɟ ɨɩɪɟɞɟɥɟɧɢɟ, ɩɨɷɬɨɦɭ ɛɭɞɶɬɟ ɨɫɬɨɪɨɠɧɵ ɫ ɧɢɦ!
ȼ MySQL 3.22 ɢ ɛɨɥɟɟ ɩɨɡɞɧɢɯ ɜɟɪɫɢɹɯ ɜɵ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɤɥɸɱɟɜɵɟ ɫɥɨɜɚ IF
EXISTS, ɱɬɨɛɵ ɩɪɟɞɨɬɜɪɚɬɢɬɶ ɨɲɢɛɤɭ, ɜɵɡɜɚɧɧɭɸ ɩɨɩɵɬɤɨɣ ɭɞɚɥɢɬɶ ɧɟɫɭɳɟɫɬɜɭɸɳɭɸ
ɬɚɛɥɢɰɭ. ɇɚɱɢɧɚɹ ɫ MySQL 4.1, ɝɟɧɟɪɢɪɭɟɬɫɹ ɩɪɟɞɭɩɪɟɠɞɟɧɢɟ ɨ ɤɚɠɞɨɣ ɧɟɫɭɳɟɫɬɜɭɸɳɟɣ ɬɚɛɥɢɰɟ, ɟɫɥɢ ɭɤɚɡɚɧɨ IF EXISTS. ɋɦ. ɪɚɡɞɟɥ 6.5.3.20.
Ⱦɥɹ ɨɛɥɟɝɱɟɧɢɹ ɩɟɪɟɧɨɫɢɦɨɫɬɢ ɞɨɩɭɫɤɚɸɬɫɹ ɫɥɨɜɚ RESTRICT ɢ CASCADE. Ɉɞɧɚɤɨ ɧɚ
ɫɟɝɨɞɧɹɲɧɢɣ ɞɟɧɶ ɨɧɢ ɧɢɱɟɝɨ ɧɟ ɞɟɥɚɸɬ.
oË ÑËÕÏÛµÂ!
DROP TABLE ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɡɚɜɟɪɲɚɟɬ ɬɟɤɭɳɭɸ ɚɤɬɢɜɧɭɸ ɬɪɚɧɡɚɤɰɢɸ, ɟɫɥɢ ɬɨɥɶɤɨ ɜɵ ɧɟ ɢɫɩɨɥɶɡɭɟɬɟ MySQL 4.1 ɢ ɜɵɲɟ, ɚ ɬɚɤɠɟ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ TEMPORARY.
Ʉɥɸɱɟɜɨɟ ɫɥɨɜɨ TEMPORARY ɢɝɧɨɪɢɪɭɟɬɫɹ ɜ MySQL 4.0. ȼ ɜɟɪɫɢɢ MySQL 4.1 ɨɧɨ ɞɚɟɬ
ɫɥɟɞɭɸɳɢɣ ɷɮɮɟɤɬ:
„Ɉɩɟɪɚɬɨɪ ɭɞɚɥɹɟɬ ɥɸɛɵɟ ɜɪɟɦɟɧɧɵɟ ɬɚɛɥɢɰɵ.
„Ɉɩɟɪɚɬɨɪ ɧɟ ɡɚɜɟɪɲɚɟɬ ɬɟɤɭɳɭɸ ɬɪɚɧɡɚɤɰɢɸ.
„ɇɢɤɚɤɢɟ ɩɪɚɜɚ ɞɨɫɬɭɩɚ ɧɟ ɩɪɨɜɟɪɹɸɬɫɹ (ɜɪɟɦɟɧɧɵɟ ɬɚɛɥɢɰɵ ɜɢɞɢɦɵ ɬɨɥɶɤɨ ɤɥɢɟɧɬɭ, ɤɨɬɨɪɵɣ ɢɯ ɫɨɡɞɚɥ, ɩɨɷɬɨɦɭ ɧɢɤɚɤɢɟ ɩɪɨɜɟɪɤɢ ɧɟ ɬɪɟɛɭɸɬɫɹ).
ɉɪɢɦɟɧɟɧɢɟ ɫɥɨɜɚ TEMPORARY – ɯɨɪɨɲɢɣ ɫɩɨɫɨɛ ɝɚɪɚɧɬɢɪɨɜɚɬɶ, ɱɬɨ ɜɵ ɧɟ ɭɞɚɥɢɬɟ
ɫɥɭɱɚɣɧɨ ɧɢɤɚɤɢɯ ɩɨɫɬɨɹɧɧɵɯ (ɧɟ ɜɪɟɦɟɧɧɵɯ) ɬɚɛɥɢɰ.
6.2.9. Ñèíòàêñèñ RENAME TABLE
RENAME TABLE ɢɦɹ_ɬɚɛɥɢɰɵ TO ɧɨɜɨɟ_ɢɦɹ_ɬɚɛɥɢɰɵ
[,ɢɦɹ_ɬɚɛɥɢɰɵ2 TO ɧɨɜɨɟ_ɢɦɹ_ɬɚɛɥɢɰɵ2] ...
Ɉɩɟɪɚɬɨɪ ɩɟɪɟɢɦɟɧɨɜɵɜɚɟɬ ɨɞɧɭ ɢɥɢ ɧɟɫɤɨɥɶɤɨ ɬɚɛɥɢɰ. Ȼɵɥ ɞɨɛɚɜɥɟɧ ɜ MySQL
3.23.23.
Ɉɩɟɪɚɰɢɹ ɩɟɪɟɢɦɟɧɨɜɚɧɢɹ ɹɜɥɹɟɬɫɹ ɚɬɨɦɚɪɧɨɣ, ɚ ɷɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɧɢ ɨɞɢɧ ɞɪɭɝɨɣ
ɩɨɬɨɤ ɫɟɪɜɟɪɚ ɧɟ ɦɨɠɟɬ ɩɨɥɭɱɢɬɶ ɞɨɫɬɭɩ ɤ ɬɚɛɥɢɰɟ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ ɩɟɪɟɢɦɟɧɨɜɚɧɢɹ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɭ ɜɚɫ ɟɫɬɶ ɫɭɳɟɫɬɜɭɸɳɚɹ ɬɚɛɥɢɰɚ old_table, ɜɵ ɦɨɠɟɬɟ ɫɨɡɞɚɬɶ
ɞɪɭɝɭɸ ɩɭɫɬɭɸ ɬɚɛɥɢɰɭ new_table, ɤɨɬɨɪɚɹ ɢɦɟɟɬ ɬɭ ɠɟ ɫɬɪɭɤɬɭɪɭ, ɚ ɡɚɬɟɦ ɡɚɦɟɧɢɬɶ
ɫɬɚɪɭɸ ɬɚɛɥɢɰɭ ɧɚ ɧɨɜɭɸ ɩɭɫɬɭɸ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
CREATE TABLE new_table (...);
RENAME TABLE old_table TO backup_table, new_table TO old_table;
ȿɫɥɢ ɨɩɟɪɚɬɨɪ ɩɟɪɟɢɦɟɧɨɜɵɜɚɟɬ ɛɨɥɟɟ ɨɞɧɨɣ ɬɚɛɥɢɰɵ, ɨɩɟɪɚɰɢɢ ɩɟɪɟɢɦɟɧɨɜɚɧɢɹ ɜɵɩɨɥɧɹɸɬɫɹ ɫɥɟɜɚ ɧɚɩɪɚɜɨ. ȿɫɥɢ ɜɵ ɯɨɬɢɬɟ, ɱɬɨɛɵ ɞɜɟ ɬɚɛɥɢɰɵ ɨɛɦɟɧɹɥɢɫɶ ɢɦɟɧɚɦɢ, ɷɬɨ
ɦɨɠɧɨ ɫɞɟɥɚɬɶ ɩɪɢɦɟɪɧɨ ɬɚɤ (ɩɪɟɞɩɨɥɚɝɚɟɬɫɹ, ɱɬɨ ɬɚɛɥɢɰɚ tmp_table ɧɟ ɫɭɳɟɫɬɜɭɟɬ):
RENAME TABLE old_table TO tmp_table,
new_table TO old_table,
tmp_table TO new_table;
6.3. Ñëóæåáíûå îïåðàòîðû MySQL
277
Ⱦɨ ɬɟɯ ɩɨɪ ɩɨɤɚ ɞɜɟ ɛɚɡɵ ɞɚɧɧɵɯ ɧɚɯɨɞɹɬɫɹ ɜ ɨɞɧɨɣ ɮɚɣɥɨɜɨɣ ɫɢɫɬɟɦɟ, ɜɵ ɬɚɤɠɟ ɦɨɠɟɬɟ ɩɟɪɟɢɦɟɧɨɜɚɬɶ ɬɚɛɥɢɰɭ, ɱɬɨɛɵ ɩɟɪɟɦɟɫɬɢɬɶ ɟɟ ɢɡ ɨɞɧɨɣ ɛɚɡɵ ɜ ɞɪɭɝɭɸ:
RENAME TABLE ɬɟɤɭɳɚɹ_ɛɚɡɚ_ɞɚɧɧɵɯ.ɢɦɹ_ɬɚɛɥɢɰɵ
TO ɞɪɭɝɚɹ_ɛɚɡɚ_ɞɚɧɧɵɯ.ɢɦɹ_ɬɚɛɥɢɰɵ;
Ʉɨɝɞɚ ɜɵɩɨɥɧɹɟɬɫɹ RENAME, ɧɟɥɶɡɹ ɢɦɟɬɶ ɧɢɤɚɤɢɯ ɡɚɛɥɨɤɢɪɨɜɚɧɧɵɯ ɬɚɛɥɢɰ ɢɥɢ ɚɤɬɢɜɧɵɯ ɬɪɚɧɡɚɤɰɢɣ. ȼɵ ɞɨɥɠɧɵ ɬɚɤɠɟ ɨɛɥɚɞɚɬɶ ɩɪɢɜɢɥɟɝɢɹɦɢ ALTER ɢ DROP ɧɚ ɢɫɯɨɞɧɨɣ
ɬɚɛɥɢɰɟ, ɚ ɬɚɤɠɟ ɩɪɢɜɢɥɟɝɢɹɦɢ CREATE ɢ INSERT ɧɚ ɧɨɜɨɣ ɬɚɛɥɢɰɟ.
6.3. Ñëóæåáíûå îïåðàòîðû MySQL
6.3.1. Ñèíòàêñèñ DESCRIBE
(ïîëó÷èòü èíôîðìàöèþ î ñòîëáöàõ)
{DESCRIBE | DESC} ɢɦɹ_ɬɚɛɥɢɰɵ [ɢɦɹ_ɫɬɨɥɛɰɚ | wild]
DESCRIBE ɩɪɟɞɫɬɚɜɥɹɟɬ ɢɧɮɨɪɦɚɰɢɸ ɨ ɫɬɨɥɛɰɚɯ ɬɚɛɥɢɰɵ. ɗɬɨ ɫɨɤɪɚɳɟɧɢɟ ɞɥɹ SHOW
COLUMNS FROM.
ɋɦ. ɪɚɡɞɟɥ 6.5.3.4.
ɢɦɹ_ɫɬɨɥɛɰɚ ɦɨɠɟɬ ɛɵɬɶ ɢɦɟɧɟɦ ɫɬɨɥɛɰɚ ɢɥɢ ɫɬɪɨɤɨɣ, ɫɨɞɟɪɠɚɳɟɣ ɫɢɦɜɨɥɵ SQLɲɚɛɥɨɧɨɜ ‘%’ ɢ ‘_’, ɱɬɨɛɵ ɩɨɥɭɱɢɬɶ ɢɧɮɨɪɦɚɰɢɸ ɬɨɥɶɤɨ ɨ ɫɬɨɥɛɰɚɯ, ɢɦɟɧɚ ɤɨɬɨɪɵɯ ɫɨ-
ɨɬɜɟɬɫɬɜɭɸɬ ɲɚɛɥɨɧɭ. ɇɟɬ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɡɚɤɥɸɱɚɬɶ ɫɬɪɨɤɭ ɜ ɤɚɜɵɱɤɢ, ɟɫɥɢ ɬɨɥɶɤɨ ɨɧɚ
ɧɟ ɫɨɞɟɪɠɢɬ ɩɪɨɛɟɥɨɜ ɢɥɢ ɞɪɭɝɢɯ ɫɩɟɰɢɚɥɶɧɵɯ ɫɢɦɜɨɥɨɜ.
ȿɫɥɢ ɬɢɩɵ ɫɬɨɥɛɰɨɜ ɨɬɥɢɱɚɸɬɫɹ ɨɬ ɨɠɢɞɚɟɦɵɯ, ɤɨɬɨɪɵɟ ɭɤɚɡɵɜɚɥɢɫɶ ɜ ɨɩɟɪɚɬɨɪɟ
CREATE TABLE, ɩɨɦɧɢɬɟ, ɱɬɨ MySQL ɢɧɨɝɞɚ ɩɨɞɦɟɧɹɟɬ ɬɢɩɵ ɫɬɨɥɛɰɨɜ. ɋɦ. ɪɚɡɞɟɥ 6.2.5.2.
Ɉɩɟɪɚɬɨɪ DESCRIBE ɜɜɟɞɟɧ ɞɥɹ ɫɨɜɦɟɫɬɢɦɨɫɬɢ ɫ Oracle.
Ɉɩɟɪɚɬɨɪɵ SHOW CREATE TABLE ɢ SHOW TABLE STATUS ɬɚɤɠɟ ɩɪɟɞɨɫɬɚɜɥɹɸɬ ɢɧɮɨɪɦɚɰɢɸ ɨ ɬɚɛɥɢɰɚɯ. ɋɦ. ɪɚɡɞɟɥ 6.5.3.
6.3.2. Ñèíòàêñèñ USE
USE ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ
Ɉɩɟɪɚɬɨɪ USE ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ ɫɨɨɛɳɚɟɬ MySQL ɨ ɬɨɦ, ɱɬɨ ɛɚɡɭ ɫ ɢɦɟɧɟɦ
ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ ɧɭɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɩɨ ɭɦɨɥɱɚɧɢɸ (ɤɚɤ ɬɟɤɭɳɭɸ) ɜ ɩɨɫɥɟɞɭɸɳɢɯ
ɨɩɟɪɚɰɢɹɯ. Ȼɚɡɚ ɞɚɧɧɵɯ ɨɫɬɚɟɬɫɹ ɛɚɡɨɣ ɩɨ ɭɦɨɥɱɚɧɢɸ ɞɨ ɡɚɜɟɪɲɟɧɢɹ ɫɟɚɧɫɚ ɢɥɢ ɞɨ ɦɨɦɟɧɬɚ, ɤɨɝɞɚ ɛɭɞɟɬ ɜɵɩɨɥɧɟɧ ɞɪɭɝɨɣ ɨɩɟɪɚɬɨɪ USE:
mysql>
mysql>
mysql>
mysql>
USE db1;
SELECT COUNT(*) FROM mytable; # ɜɵɛɨɪɤɚ ɢɡ db1.mytable
USE db2;
SELECT COUNT(*) FROM mytable; # ɜɵɛɨɪɤɚ ɢɡ db2.mytable
ɇɚɡɧɚɱɟɧɢɟ ɨɬɞɟɥɶɧɨɣ ɛɚɡɵ ɞɚɧɧɵɯ ɤɚɤ ɢɫɩɨɥɶɡɭɟɦɨɣ ɩɨ ɭɦɨɥɱɚɧɢɸ ɧɟ ɡɚɩɪɟɳɚɟɬ
ɞɨɫɬɭɩ ɤ ɞɪɭɝɢɦ ɛɚɡɚɦ. ɋɥɟɞɭɸɳɢɣ ɩɪɢɦɟɪ ɨɛɪɚɳɚɟɬɫɹ ɤ ɬɚɛɥɢɰɟ author ɜ ɛɚɡɟ db1 ɢ ɤ
ɬɚɛɥɢɰɟ editor ɜ ɛɚɡɟ db2:
mysql> USE db1;
mysql> SELECT author_name,editor_name FROM author,db2.editor
-> WHERE author.editor_id = db2.editor.editor_id;
Ɉɩɟɪɚɬɨɪ USE ɜɜɟɞɟɧ ɞɥɹ ɨɛɟɫɩɟɱɟɧɢɹ ɫɨɜɦɟɫɬɢɦɨɫɬɢ ɫ Sybase.
278
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
6.4. Îïåðàòîðû óïðàâëåíèÿ òðàíçàêöèÿìè
è áëîêèðîâêîé MySQL
6.4.1. Ñèíòàêñèñ START TRANSACTION, COMMIT
è ROLLBACK
ɉɨ ɭɦɨɥɱɚɧɢɸ MySQL ɪɚɛɨɬɚɟɬ ɜ ɪɟɠɢɦɟ ɚɜɬɨɦɚɬɢɱɟɫɤɨɝɨ ɡɚɜɟɪɲɟɧɢɹ ɬɪɚɧɡɚɤɰɢɣ
(autocommit). ɗɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɤɚɤ ɬɨɥɶɤɨ ɜɵɩɨɥɧɹɟɬɫɹ ɨɩɟɪɚɬɨɪ ɨɛɧɨɜɥɟɧɢɹ ɞɚɧɧɵɯ,
ɤɨɬɨɪɵɣ ɦɨɞɢɮɢɰɢɪɭɟɬ ɬɚɛɥɢɰɭ, MySQL ɬɭɬ ɠɟ ɫɨɯɪɚɧɹɟɬ ɢɡɦɟɧɟɧɢɹ ɧɚ ɞɢɫɤɟ.
ȿɫɥɢ ɜɵ ɪɚɛɨɬɚɟɬɟ ɫ ɬɚɛɥɢɰɚɦɢ, ɛɟɡɨɩɚɫɧɵɦɢ ɜ ɨɬɧɨɲɟɧɢɢ ɬɪɚɧɡɚɤɰɢɣ (ɬɚɤɢɦɢ ɤɚɤ
InnoDB ɢ BDB), ɬɨ ɪɟɠɢɦ ɚɜɬɨɦɚɬɢɱɟɫɤɨɝɨ ɡɚɜɟɪɲɟɧɢɹ ɬɪɚɧɡɚɤɰɢɣ ɦɨɠɧɨ ɨɬɤɥɸɱɢɬɶ ɫɥɟɞɭɸɳɢɦ ɨɩɟɪɚɬɨɪɨɦ:
SET AUTOCOMMIT=0;
ɉɨɫɥɟ ɨɬɤɥɸɱɟɧɢɹ ɪɟɠɢɦɚ ɚɜɬɨɦɚɬɢɱɟɫɤɨɝɨ ɡɚɜɟɪɲɟɧɢɹ ɬɪɚɧɡɚɤɰɢɣ ɜɵ ɞɨɥɠɧɵ ɢɫɩɨɥɶɡɨɜɚɬɶ ɨɩɟɪɚɬɨɪ COMMIT, ɱɬɨɛɵ ɫɨɯɪɚɧɹɬɶ ɢɡɦɟɧɟɧɢɹ ɧɚ ɞɢɫɤɟ, ɥɢɛɨ ROLLBACK, ɱɬɨɛɵ
ɨɬɦɟɧɹɬɶ ɢɡɦɟɧɟɧɢɹ, ɜɵɩɨɥɧɟɧɧɵɟ ɫ ɦɨɦɟɧɬɚ ɧɚɱɚɥɚ ɬɪɚɧɡɚɤɰɢɢ.
ȿɫɥɢ ɜɵ ɯɨɬɢɬɟ ɨɬɤɥɸɱɢɬɶ ɪɟɠɢɦ ɚɜɬɨɦɚɬɢɱɟɫɤɨɝɨ ɡɚɜɟɪɲɟɧɢɹ ɬɪɚɧɡɚɤɰɢɣ ɬɨɥɶɤɨ
ɞɥɹ ɨɬɞɟɥɶɧɨɣ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɢ ɨɩɟɪɚɬɨɪɨɜ, ɦɨɠɟɬɟ ɜɨɫɩɨɥɶɡɨɜɚɬɶɫɹ ɨɩɟɪɚɬɨɪɨɦ
START TRANSACTION:
START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;
ɉɨɫɥɟ START TRANSACTION ɪɟɠɢɦ ɚɜɬɨɦɚɬɢɱɟɫɤɨɝɨ ɡɚɜɟɪɲɟɧɢɹ ɬɪɚɧɡɚɤɰɢɣ ɨɫɬɚɟɬɫɹ
ɜɵɤɥɸɱɟɧɧɵɦ ɞɨ ɹɜɧɨɝɨ ɡɚɜɟɪɲɟɧɢɹ ɬɪɚɧɡɚɤɰɢɢ ɫ ɩɨɦɨɳɶɸ COMMIT ɢɥɢ ɨɬɤɚɬɚ ɩɨɫɪɟɞɫɬɜɨɦ ROLLBACK. Ɂɚɬɟɦ ɪɟɠɢɦ ɚɜɬɨɦɚɬɢɱɟɫɤɨɝɨ ɡɚɜɟɪɲɟɧɢɹ ɜɨɡɜɪɚɳɚɟɬɫɹ ɜ ɫɜɨɟ ɩɪɟɞɵɞɭɳɟɟ ɫɨɫɬɨɹɧɢɟ.
ȼɦɟɫɬɨ START TRANSACTION ɞɥɹ ɨɛɨɡɧɚɱɟɧɢɹ ɧɚɱɚɥɚ ɬɪɚɧɡɚɤɰɢɢ ɦɨɝɭɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ
BEGIN ɢ BEGIN WORK. Ɉɩɟɪɚɬɨɪ START TRANSACTION ɩɨɹɜɢɥɫɹ ɜ ɜɟɪɫɢɢ MySQL 4.0.11. ɗɬɨ
ɫɬɚɧɞɚɪɬɧɵɣ ɫɢɧɬɚɤɫɢɫ SQL ɢ ɪɟɤɨɦɟɧɞɭɟɦɵɣ ɫɩɨɫɨɛ ɧɚɱɢɧɚɬɶ ɬɪɚɧɡɚɤɰɢɢ. BEGIN ɢ
BEGIN WORK ɞɨɫɬɭɩɧɵ, ɧɚɱɢɧɚɹ ɫ MySQL 3.23.17 ɢ MySQL 3.23.19, ɫɨɨɬɜɟɬɫɬɜɟɧɧɨ.
ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɟɫɥɢ ɜɵ ɧɟ ɢɫɩɨɥɶɡɭɟɬɟ ɬɚɛɥɢɰɵ, ɛɟɡɨɩɚɫɧɵɟ ɤ ɬɪɚɧɡɚɤɰɢɹɦ,
ɜɫɟ ɢɡɦɟɧɟɧɢɹ ɫɨɯɪɚɧɹɸɬɫɹ ɧɟɦɟɞɥɟɧɧɨ, ɧɟɡɚɜɢɫɢɦɨ ɨɬ ɪɟɠɢɦɚ ɚɜɬɨɦɚɬɢɱɟɫɤɨɝɨ ɡɚɜɟɪɲɟɧɢɹ ɬɪɚɧɡɚɤɰɢɣ.
ȿɫɥɢ ɜɵ ɞɚɟɬɟ ROLLBACK ɩɨɫɥɟ ɨɛɧɨɜɥɟɧɢɹ ɬɚɛɥɢɰɵ, ɛɟɡɨɩɚɫɧɨɣ ɤ ɬɪɚɧɡɚɤɰɢɹɦ, ɜ
ɩɪɟɞɟɥɚɯ ɬɪɚɧɡɚɤɰɢɢ, ɜɵɞɚɟɬɫɹ ɩɪɟɞɭɩɪɟɠɞɟɧɢɟ ER_WARNING_NOT_COMPLETE_ROLLBACK.
ɂɡɦɟɧɟɧɢɹ ɜ ɬɚɛɥɢɰɚɯ, ɛɟɡɨɩɚɫɧɵɯ ɤ ɬɪɚɧɡɚɤɰɢɹɦ, ɛɭɞɭɬ ɨɬɦɟɧɟɧɵ, ɚ ɜ ɧɟɛɟɡɨɩɚɫɧɵɯ –
ɨɫɬɚɧɭɬɫɹ.
ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ START TRANSACTION ɢɥɢ SET AUTOCOMMIT=0, ɬɨ ɞɨɥɠɧɵ ɞɥɹ ɪɟɡɟɪɜɧɵɯ ɤɨɩɢɣ ɢɫɩɨɥɶɡɨɜɚɬɶ ɛɢɧɚɪɧɵɣ ɠɭɪɧɚɥ MySQL, ɜɦɟɫɬɨ ɫɬɚɪɨɝɨ ɠɭɪɧɚɥɚ ɨɛɧɨɜɥɟɧɢɣ. Ɍɪɚɧɡɚɤɰɢɢ ɫɨɯɪɚɧɹɸɬɫɹ ɜ ɛɢɧɚɪɧɨɦ ɠɭɪɧɚɥɟ ɨɞɧɢɦ ɤɭɫɤɨɦ, ɞɨ COMMIT. Ɍɪɚɧɡɚɤɰɢɢ, ɨɬɦɟɧɟɧɧɵɟ ɨɩɟɪɚɬɨɪɨɦ ROLLBACK, ɜ ɠɭɪɧɚɥ ɧɟ ɡɚɧɨɫɹɬɫɹ. (ɋɭɳɟɫɬɜɭɟɬ ɨɞɧɨ ɢɫɤɥɸɱɟɧɢɟ: ɦɨɞɢɮɢɤɚɰɢɢ ɧɟɬɪɚɧɡɚɤɰɢɨɧɧɵɯ ɬɚɛɥɢɰ ɧɟ ɦɨɝɭɬ ɛɵɬɶ ɨɬɦɟɧɟɧɵ. ȿɫɥɢ
ɨɬɦɟɧɟɧɧɚɹ ɬɪɚɧɡɚɤɰɢɹ ɜɤɥɸɱɚɥɚ ɜ ɫɟɛɹ ɦɨɞɢɮɢɤɚɰɢɸ ɞɚɧɧɵɯ ɧɟɬɪɚɧɡɚɤɰɢɨɧɧɵɯ ɬɚɛɥɢɰ, ɬɚɤɚɹ ɬɪɚɧɡɚɤɰɢɹ ɰɟɥɢɤɨɦ ɡɚɩɢɫɵɜɚɟɬɫɹ ɜ ɛɢɧɚɪɧɵɣ ɠɭɪɧɚɥ ɫ ɨɩɟɪɚɬɨɪɨɦ ROLLBACK
6.4. Îïåðàòîðû óïðàâëåíèÿ òðàíçàêöèÿìè è áëîêèðîâêîé MySQL
279
ɜ ɤɨɧɰɟ, ɱɬɨɛɵ ɝɚɪɚɧɬɢɪɨɜɚɬɶ, ɱɬɨ ɦɨɞɢɮɢɤɚɰɢɢ ɷɬɢɯ ɬɚɛɥɢɰ ɛɭɞɭɬ ɪɟɩɥɢɰɢɪɨɜɚɧɵ. ɗɬɨ
ɜɟɪɧɨ, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.0.15.)
ȼɵ ɦɨɠɟɬɟ ɢɡɦɟɧɢɬɶ ɭɪɨɜɟɧɶ ɢɡɨɥɹɰɢɢ ɬɪɚɧɡɚɤɰɢɣ ɨɩɟɪɚɬɨɪɨɦ SET TRANSACTION
ISOLATION LEVEL. ɋɦ. ɪɚɡɞɟɥ 6.4.6.
6.4.2. Îïåðàòîðû, êîòîðûå íåëüçÿ îòêàòèòü
Ⱦɥɹ ɧɟɤɨɬɨɪɵɯ ɨɩɟɪɚɬɨɪɨɜ ɧɟɥɶɡɹ ɜɵɩɨɥɧɢɬɶ ɨɬɤɚɬ ɫ ɩɨɦɨɳɶɸ ROLLBACK. ȼ ɢɯ ɱɢɫɥɨ
ɜɯɨɞɹɬ ɨɩɟɪɚɬɨɪɵ ɹɡɵɤɚ ɨɩɪɟɞɟɥɟɧɢɹ ɞɚɧɧɵɯ (Data Definition Language – DDL), ɤɨɬɨɪɵɟ
ɫɨɡɞɚɸɬ ɢ ɭɧɢɱɬɨɠɚɸɬ ɛɚɡɵ ɞɚɧɧɵɯ, ɚ ɬɚɤɠɟ ɫɨɡɞɚɸɬ, ɭɞɚɥɹɸɬ ɢ ɢɡɦɟɧɹɸɬ ɬɚɛɥɢɰɵ.
ȼɵ ɞɨɥɠɧɵ ɩɪɨɟɤɬɢɪɨɜɚɬɶ ɫɜɨɢ ɬɪɚɧɡɚɤɰɢɢ ɬɚɤɢɦ ɨɛɪɚɡɨɦ, ɱɬɨɛɵ ɨɧɢ ɧɟ ɜɤɥɸɱɚɥɢ ɜ
ɫɟɛɹ ɷɬɢ ɨɩɟɪɚɬɨɪɵ. ȿɫɥɢ ɜɜɟɫɬɢ ɬɚɤɨɣ ɨɩɟɪɚɬɨɪ ɜ ɧɚɱɚɥɟ ɬɪɚɧɡɚɤɰɢɢ, ɤɨɬɨɪɚɹ ɧɟ ɦɨɠɟɬ
ɛɵɬɶ ɨɬɤɚɬɚɧɚ, ɢ ɡɚɬɟɦ ɫɥɟɞɭɸɳɢɣ ɨɩɟɪɚɬɨɪ ɩɨɡɠɟ ɡɚɜɟɪɲɢɬɫɹ ɚɜɚɪɢɣɧɨ, ɩɨɥɧɵɣ ɷɮɮɟɤɬ ɬɪɚɧɡɚɤɰɢɢ ɧɟ ɦɨɠɟɬ ɛɵɬɶ ɨɬɦɟɧɟɧ ɨɩɟɪɚɬɨɪɨɦ ROLLBACK.
6.4.3. Îïåðàòîðû, âûçûâàþùèå íåÿâíûé COMMIT
ɋɥɟɞɭɸɳɢɟ ɨɩɟɪɚɬɨɪɵ ɧɟɹɜɧɨ ɡɚɜɟɪɲɚɸɬ ɬɪɚɧɡɚɤɰɢɸ (ɤɚɤ ɟɫɥɢ ɛɵ ɩɟɪɟɞ ɢɯ ɜɵɩɨɥɧɟɧɢɟɦ ɛɵɥ ɜɵɞɚɧ COMMIT):
ALTER TABLE
DROP DATABASE
LOAD MASTER DATA
SET AUTOCOMMIT=1
BEGIN
DROP INDEX
LOCK TABLES
START TRANSACTION
CREATE INDEX
DROP TABLE
RENAME TABLE
TRUNCATE TABLE
UNLOCK TABLES ɬɚɤɠɟ ɡɚɜɟɪɲɚɟɬ ɬɪɚɧɡɚɤɰɢɸ, ɟɫɥɢ ɤɚɤɢɟ-ɥɢɛɨ ɬɚɛɥɢɰɵ ɛɵɥɢ ɛɥɨɤɢɪɨɜɚɧɵ. Ⱦɨ MySQL 4.0.13 CREATE TABLE ɡɚɜɟɪɲɚɥ ɬɪɚɧɡɚɤɰɢɸ, ɟɫɥɢ ɛɵɥɚ ɛɵ ɜɤɥɸɱɟɧɚ
ɛɢɧɚɪɧɚɹ ɪɟɝɢɫɬɪɚɰɢɹ.
Ɍɪɚɧɡɚɤɰɢɢ ɧɟ ɦɨɝɭɬ ɛɵɬɶ ɜɥɨɠɟɧɧɵɦɢ. ɗɬɨ ɫɥɟɞɫɬɜɢɟ ɬɨɝɨ, ɱɬɨ ɧɟɹɜɧɵɣ COMMIT
ɜɵɩɨɥɧɹɟɬɫɹ ɞɥɹ ɥɸɛɨɣ ɬɟɤɭɳɟɣ ɬɪɚɧɡɚɤɰɢɢ, ɤɨɝɞɚ ɜɵɩɨɥɧɹɟɬɫɹ ɨɩɟɪɚɬɨɪ START
TRANSACTION ɢɥɢ ɟɝɨ ɫɢɧɨɧɢɦɵ.
6.4.4. Ñèíòàêñèñ SAVEPOINT è ROLLBACK TO SAVEPOINT
SAVEPOINT ɢɞɟɧɬɢɮɢɤɚɬɨɪ
ROLLBACK TO SAVEPOINT ɢɞɟɧɬɢɮɢɤɚɬɨɪ
ɇɚɱɢɧɚɹ ɫ ɜɟɪɫɢɣ MySQL 4.0.14 ɢ 4.1.1, InnoDB ɩɨɞɞɟɪɠɢɜɚɟɬ SQL-ɨɩɟɪɚɬɨɪɵ
SAVEPOINT ɢ ROLLBACK TO SAVEPOINT.
Ɉɩɟɪɚɬɨɪ SAVEPOINT ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɢɦɟɧɨɜɚɧɧɭɸ ɬɨɱɤɭ ɧɚɱɚɥɚ ɬɪɚɧɡɚɤɰɢɢ ɫ ɢɦɟɧɟɦ
ɢɞɟɧɬɢɮɢɤɚɬɨɪ. ȿɫɥɢ ɬɟɤɭɳɚɹ ɬɪɚɧɡɚɤɰɢɹ ɭɠɟ ɢɦɟɟɬ ɬɨɱɤɭ ɫɨɯɪɚɧɟɧɢɹ ɫ ɬɚɤɢɦ ɠɟ ɢɦɟɧɟɦ, ɫɬɚɪɚɹ ɬɨɱɤɚ ɭɞɚɥɹɟɬɫɹ ɢ ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɧɨɜɚɹ.
Ɉɩɟɪɚɬɨɪ ROLLBACK TO SAVEPOINT ɨɬɤɚɬɵɜɚɟɬ ɬɪɚɧɡɚɤɰɢɸ ɤ ɢɦɟɧɨɜɚɧɧɨɣ ɬɨɱɤɟ ɫɨɯɪɚɧɟɧɢɹ. Ɇɨɞɢɮɢɤɚɰɢɢ ɫɬɪɨɤ, ɤɨɬɨɪɵɟ ɜɵɩɨɥɧɹɥɢɫɶ ɬɟɤɭɳɟɣ ɬɪɚɧɡɚɤɰɢɟɣ ɩɨɫɥɟ ɷɬɨɣ
ɬɨɱɤɢ, ɨɬɦɟɧɹɸɬɫɹ ɨɬɤɚɬɨɦ, ɨɞɧɚɤɨ InnoDB ɧɟ ɫɧɢɦɚɟɬ ɛɥɨɤɢɪɨɜɨɤ ɫɬɪɨɤ, ɤɨɬɨɪɵɟ ɛɵɥɢ
ɭɫɬɚɧɨɜɥɟɧɵ ɜ ɩɚɦɹɬɢ ɩɨɫɥɟ ɬɨɱɤɢ ɫɨɯɪɚɧɟɧɢɹ. (Ɉɬɦɟɬɢɦ, ɱɬɨ ɞɥɹ ɜɧɨɜɶ ɜɫɬɚɜɥɟɧɧɵɯ
ɫɬɪɨɤ ɢɧɮɨɪɦɚɰɢɹ ɨ ɛɥɨɤɢɪɨɜɤɟ ɨɩɢɪɚɟɬɫɹ ɧɚ ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɬɪɚɧɡɚɤɰɢɢ, ɫɨɯɪɚɧɟɧɧɵɣ
ɜ ɫɬɪɨɤɟ, ɛɥɨɤɢɪɨɜɤɚ ɧɟ ɯɪɚɧɢɬɫɹ ɜ ɩɚɦɹɬɢ ɨɬɞɟɥɶɧɨ. ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɛɥɨɤɢɪɨɜɤɚ ɫɬɪɨɤɢ
ɫɧɢɦɚɟɬɫɹ ɩɪɢ ɨɬɦɟɧɟ.) Ɍɨɱɤɢ ɫɨɯɪɚɧɟɧɢɹ, ɭɫɬɚɧɨɜɥɟɧɧɵɟ ɜ ɛɨɥɟɟ ɩɨɡɞɧɢɟ ɦɨɦɟɧɬɵ, ɱɟɦ
ɢɦɟɧɨɜɚɧɧɚɹ ɬɨɱɤɚ, ɭɞɚɥɹɸɬɫɹ.
280
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ȿɫɥɢ ɨɩɟɪɚɬɨɪ ɜɨɡɜɪɚɳɚɟɬ ɫɥɟɞɭɸɳɭɸ ɨɲɢɛɤɭ, ɷɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɧɚɡɜɚɧɧɚɹ ɬɨɱɤɚ
ɫɨɯɪɚɧɟɧɢɹ ɧɟ ɫɭɳɟɫɬɜɭɟɬ:
ERROR 1181: Got error 153 during ROLLBACK
ȼɫɟ ɬɨɱɤɢ ɫɨɯɪɚɧɟɧɢɹ ɬɪɚɧɡɚɤɰɢɣ ɭɞɚɥɹɸɬɫɹ, ɟɫɥɢ ɜɵɩɨɥɧɹɟɬɫɹ ɨɩɟɪɚɬɨɪ COMMIT ɢɥɢ
ROLLBACK ɛɟɡ ɭɤɚɡɚɧɢɹ ɢɦɟɧɢ ɬɨɱɤɢ ɫɨɯɪɚɧɟɧɢɹ.
6.4.5. Ñèíòàêñèñ LOCK TABLES è UNLOCK TABLES
LOCK TABLES
ɢɦɹ_ɬɚɛɥɢɰɵ [AS ɩɫɟɜɞɨɧɢɦ] {READ [LOCAL] | [LOW_PRIORITY] WRITE}
[, ɢɦɹ_ɬɚɛɥɢɰɵ [AS ɩɫɟɜɞɨɧɢɦ] {READ [LOCAL] | [LOW_PRIORITY] WRITE}] ...
UNLOCK TABLES
LOCK TABLES ɛɥɨɤɢɪɭɟɬ ɬɚɛɥɢɰɵ ɞɥɹ ɬɟɤɭɳɟɝɨ ɩɨɬɨɤɚ ɫɟɪɜɟɪɚ. UNLOCK TABLES ɫɧɢɦɚɟɬ ɥɸɛɵɟ ɛɥɨɤɢɪɨɜɤɢ, ɭɞɟɪɠɢɜɚɟɦɵɟ ɬɟɤɭɳɢɦ ɩɨɬɨɤɨɦ. ȼɫɟ ɬɚɛɥɢɰɵ, ɡɚɛɥɨɤɢɪɨɜɚɧɧɵɟ
ɜ ɬɟɤɭɳɟɦ ɩɨɬɨɤɟ, ɧɟɹɜɧɨ ɪɚɡɛɥɨɤɢɪɭɸɬɫɹ, ɤɨɝɞɚ ɩɨɬɨɤ ɜɵɩɨɥɧɹɟɬ ɞɪɭɝɨɣ ɨɩɟɪɚɬɨɪ LOCK
TABLES ɥɢɛɨ ɤɨɝɞɚ ɡɚɤɪɵɜɚɟɬɫɹ ɫɨɟɞɢɧɟɧɢɟ ɫ ɫɟɪɜɟɪɨɦ.
oË ÑËÕÏÛµÂ!
LOCK TABLES ɧɟ ɹɜɥɹɟɬɫɹ ɨɩɟɪɚɬɨɪɨɦ, ɛɟɡɨɩɚɫɧɵɦ ɜ ɨɬɧɨɲɟɧɢɢ ɬɪɚɧɡɚɤɰɢɣ, ɢ ɧɟɹɜɧɨ ɡɚɜɟɪɲɚɟɬ ɬɪɚɧɡɚɤɰɢɸ ɩɟɪɟɞ ɩɨɩɵɬɤɨɣ ɛɥɨɤɢɪɨɜɚɬɶ ɬɚɛɥɢɰɵ.
ɇɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.0.2, ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɜɵɩɨɥɧɹɬɶ LOCK TABLES, ɧɟɨɛɯɨɞɢɦɨ
ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ LOCK TABLES ɢ ɩɪɢɜɢɥɟɝɢɸ SELECT ɞɥɹ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɯ ɬɚɛɥɢɰ. ȼ
MySQL 3.23 ɧɟɨɛɯɨɞɢɦɨ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɢ SELECT, INSERT, DELETE ɢ UPDATE ɞɥɹ ɷɬɢɯ
ɬɚɛɥɢɰ.
Ɉɫɧɨɜɧɚɹ ɩɪɢɱɢɧɚ ɩɪɢɦɟɧɟɧɢɹ LOCK TABLES – ɷɦɭɥɹɰɢɹ ɬɪɚɧɡɚɤɰɢɣ ɢɥɢ ɩɨɜɵɲɟɧɢɟ
ɫɤɨɪɨɫɬɢ ɨɛɧɨɜɥɟɧɢɹ ɬɚɛɥɢɰ. ɇɢɠɟ ɷɬɨ ɨɛɴɹɫɧɹɟɬɫɹ ɛɨɥɟɟ ɩɨɞɪɨɛɧɨ.
ȿɫɥɢ ɩɨɬɨɤ ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɛɥɨɤɢɪɨɜɤɭ ɩɨ ɱɬɟɧɢɸ (READ) ɧɚ ɬɚɛɥɢɰɟ, ɬɨ ɷɬɨɬ ɩɨɬɨɤ (ɢ
ɜɫɟ ɨɫɬɚɥɶɧɵɟ) ɦɨɠɟɬ ɬɨɥɶɤɨ ɱɢɬɚɬɶ ɞɚɧɧɵɟ ɢɡ ɬɚɛɥɢɰɵ. ȿɫɥɢ ɩɨɬɨɤ ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɛɥɨɤɢɪɨɜɤɭ ɡɚɩɢɫɢ (WRITE) ɬɚɛɥɢɰɵ, ɬɨ ɥɢɲɶ ɷɬɨɬ ɩɨɬɨɤ ɦɨɠɟɬ ɱɢɬɚɬɶ ɢ ɩɢɫɚɬɶ ɜ ɬɚɛɥɢɰɭ.
Ⱦɨɫɬɭɩ ɨɫɬɚɥɶɧɵɯ ɧɢɬɟɣ ɤ ɬɚɛɥɢɰɟ ɛɥɨɤɢɪɭɟɬɫɹ.
Ɋɚɡɧɢɰɚ ɦɟɠɞɭ READ LOCAL ɢ READ ɫɨɫɬɨɢɬ ɜ ɬɨɦ, ɱɬɨ READ LOCAL ɩɨɡɜɨɥɹɟɬ ɧɟɤɨɧɮɥɢɤɬɭɸɳɢɦ ɨɩɟɪɚɬɨɪɚɦ INSERT (ɩɚɪɚɥɥɟɥɶɧɵɦ ɜɫɬɚɜɤɚɦ) ɜɵɩɨɥɧɹɬɶɫɹ, ɩɨɤɚ ɛɥɨɤɢɪɨɜɤɚ ɭɞɟɪɠɢɜɚɟɬɫɹ. Ɉɞɧɚɤɨ ɷɬɨ ɧɟ ɦɨɠɟɬ ɛɵɬɶ ɜɵɩɨɥɧɟɧɨ, ɟɫɥɢ ɜɵ ɩɵɬɚɟɬɟɫɶ ɦɚɧɢɩɭɥɢɪɨɜɚɬɶ ɮɚɣɥɚɦɢ ɛɚɡɵ ɞɚɧɧɵɯ ɢɡɜɧɟ MySQL ɜ ɬɨ ɜɪɟɦɹ, ɩɨɤɚ ɭɞɟɪɠɢɜɚɟɬɫɹ ɛɥɨɤɢɪɨɜɤɚ.
ȼ ɫɥɭɱɚɟ ɩɪɢɦɟɧɟɧɢɹ LOCK TABLES ɧɟɨɛɯɨɞɢɦɨ ɛɥɨɤɢɪɨɜɚɬɶ ɜɫɟ ɬɚɛɥɢɰɵ, ɤɨɬɨɪɵɟ
ɢɫɩɨɥɶɡɭɸɬɫɹ ɜ ɡɚɩɪɨɫɚɯ. ȿɫɥɢ ɨɞɧɚ ɢ ɬɚ ɠɟ ɬɚɛɥɢɰɚ ɢɫɩɨɥɶɡɭɟɬɫɹ ɧɟɫɤɨɥɶɤɨ ɪɚɡ ɜ ɡɚɩɪɨɫɟ (ɱɟɪɟɡ ɩɫɟɜɞɨɧɢɦɵ), ɜɵ ɞɨɥɠɧɵ ɩɨɥɭɱɢɬɶ ɛɥɨɤɢɪɨɜɤɭ ɧɚ ɤɚɠɞɵɣ ɩɫɟɜɞɨɧɢɦ. ɉɨɤɚ
ɛɥɨɤɢɪɨɜɤɚ, ɩɨɥɭɱɟɧɧɚɹ ɨɬ LOCK TABLES, ɚɤɬɢɜɧɚ, ɜɵ ɧɟ ɦɨɠɟɬɟ ɩɨɥɭɱɢɬɶ ɞɨɫɬɭɩ ɧɢ ɤ
ɤɚɤɢɦ ɬɚɛɥɢɰɚɦ, ɤɨɬɨɪɵɟ ɧɟ ɛɵɥɢ ɛɥɨɤɢɪɨɜɚɧɵ ɷɬɢɦ ɨɩɟɪɚɬɨɪɨɦ.
ȿɫɥɢ ɜɚɲ ɡɚɩɪɨɫ ɨɛɪɚɳɚɟɬɫɹ ɤ ɬɚɛɥɢɰɟ ɱɟɪɟɡ ɩɫɟɜɞɨɧɢɦ, ɜɵ ɞɨɥɠɧɵ ɛɥɨɤɢɪɨɜɚɬɶ
ɬɚɛɥɢɰɭ, ɢɫɩɨɥɶɡɭɹ ɬɨɬ ɠɟ ɩɫɟɜɞɨɧɢɦ. Ȼɥɨɤɢɪɨɜɤɚ ɬɚɛɥɢɰɵ ɧɟ ɛɭɞɟɬ ɪɚɛɨɬɚɬɶ, ɟɫɥɢ ɧɟ
ɭɤɚɡɚɧ ɩɫɟɜɞɨɧɢɦ:
mysql> LOCK TABLE t READ;
mysql> SELECT * FROM t AS myalias;
ERROR 1100: Table 'myalias' was not locked with LOCK TABLES
6.4. Îïåðàòîðû óïðàâëåíèÿ òðàíçàêöèÿìè è áëîêèðîâêîé MySQL
281
ɂ ɧɚɨɛɨɪɨɬ, ɟɫɥɢ ɬɚɛɥɢɰɚ ɛɥɨɤɢɪɨɜɚɧɚ ɩɨ ɩɫɟɜɞɨɧɢɦɭ, ɜɵ ɞɨɥɠɧɵ ɨɛɪɚɳɚɬɶɫɹ ɤ ɧɟɣ,
ɢɫɩɨɥɶɡɭɹ ɷɬɨɬ ɩɫɟɜɞɨɧɢɦ:
mysql> LOCK TABLE t AS myalias READ;
mysql> SELECT * FROM t;
ERROR 1100: Table 't' was not locked with LOCK TABLES
mysql> SELECT * FROM t AS myalias;
Ȼɥɨɤɢɪɨɜɤɢ ɩɨ ɡɚɩɢɫɢ (WRITE) ɨɛɵɱɧɨ ɢɦɟɸɬ ɛɨɥɟɟ ɜɵɫɨɤɢɣ ɩɪɢɨɪɢɬɟɬ, ɱɟɦ ɛɥɨɤɢɪɨɜɤɢ ɩɨ ɱɬɟɧɢɸ (READ), ɱɬɨɛɵ ɝɚɪɚɧɬɢɪɨɜɚɬɶ, ɱɬɨ ɨɛɧɨɜɥɟɧɢɹ ɞɚɧɧɵɯ ɩɪɨɣɞɭɬ ɤɚɤ ɦɨɠɧɨ ɛɵɫɬɪɟɟ. ɗɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɟɫɥɢ ɨɞɢɧ ɩɨɬɨɤ ɩɨɥɭɱɚɟɬ ɛɥɨɤɢɪɨɜɤɭ ɩɨ ɱɬɟɧɢɸ, ɚ ɡɚɬɟɦ
ɞɪɭɝɨɣ ɩɨɬɨɤ ɡɚɩɪɚɲɢɜɚɟɬ ɛɥɨɤɢɪɨɜɤɭ ɩɨ ɡɚɩɢɫɢ, ɬɨ ɩɨɫɥɟɞɭɸɳɢɟ ɡɚɩɪɨɫɵ ɧɚ ɛɥɨɤɢɪɨɜɤɭ ɩɨ ɱɬɟɧɢɸ ɛɭɞɭɬ ɨɠɢɞɚɬɶ, ɩɨɤɚ ɧɟ ɛɭɞɟɬ ɭɫɬɚɧɨɜɥɟɧɚ ɢ ɫɧɹɬɚ ɛɥɨɤɢɪɨɜɤɚ ɩɨ ɡɚɩɢɫɢ. ȼɵ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɛɥɨɤɢɪɨɜɤɢ ɩɨ ɡɚɩɢɫɢ ɫ ɩɨɧɢɠɟɧɧɵɦ ɩɪɢɨɪɢɬɟɬɨɦ
(LOW_PRIORITY WRITE), ɱɬɨɛɵ ɩɨɡɜɨɥɢɬɶ ɞɪɭɝɢɦ ɩɨɬɨɤɚɦ ɭɫɬɚɧɚɜɥɢɜɚɬɶ ɛɥɨɤɢɪɨɜɤɢ ɩɨ
ɱɬɟɧɢɸ, ɩɨɤɚ ɬɟɤɭɳɢɣ ɩɨɬɨɤ ɨɠɢɞɚɟɬ ɜɨɡɦɨɠɧɨɫɬɢ ɩɨɫɬɚɜɢɬɶ ɛɥɨɤɢɪɨɜɤɭ ɩɨ ɡɚɩɢɫɢ. ȼɵ
ɞɨɥɠɧɵ ɭɫɬɚɧɚɜɥɢɜɚɬɶ ɛɥɨɤɢɪɨɜɤɢ LOW_PRIORITY WRITE ɬɨɥɶɤɨ ɬɨɝɞɚ, ɤɨɝɞɚ ɭɜɟɪɟɧɵ,
ɱɬɨ ɜ ɩɪɨɰɟɫɫɟ ɪɚɛɨɬɵ ɫɟɪɜɟɪɚ ɛɭɞɟɬ ɜɪɟɦɹ, ɤɨɝɞɚ ɧɢ ɨɞɢɧ ɢɡ ɩɨɬɨɤɨɜ ɧɟ ɛɭɞɟɬ ɭɞɟɪɠɢɜɚɬɶ ɛɥɨɤɢɪɨɜɤɢ ɩɨ ɱɬɟɧɢɸ.
LOCK TABLES ɪɚɛɨɬɚɟɬ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
1. ȼ ɨɩɪɟɞɟɥɟɧɧɨɦ ɜɧɭɬɪɟɧɧɟɦ ɩɨɪɹɞɤɟ ɫɨɪɬɢɪɭɸɬɫɹ ɜɫɟ ɬɚɛɥɢɰɵ, ɩɨɞɥɟɠɚɳɢɟ ɛɥɨɤɢɪɨɜɤɟ. ɋ ɬɨɱɤɢ ɡɪɟɧɢɹ ɩɨɥɶɡɨɜɚɬɟɥɹ, ɷɬɨɬ ɩɨɪɹɞɨɤ ɧɟ ɨɩɪɟɞɟɥɟɧ.
2. ȿɫɥɢ ɬɚɛɥɢɰɚ ɛɥɨɤɢɪɭɟɬɫɹ ɢ ɩɨ ɱɬɟɧɢɸ ɢ ɩɨ ɡɚɩɢɫɢ, ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɛɥɨɤɢɪɨɜɤɚ
ɡɚɩɢɫɢ ɩɟɪɟɞ ɛɥɨɤɢɪɨɜɤɨɣ ɱɬɟɧɢɹ.
3. Ȼɥɨɤɢɪɭɟɬɫɹ ɩɨ ɨɞɧɨɣ ɬɚɛɥɢɰɟ ɡɚ ɪɚɡ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɩɨɬɨɤ ɧɟ ɩɨɥɭɱɢɬ ɜɫɟ ɛɥɨɤɢɪɨɜɤɢ.
ɗɬɚ ɩɨɥɢɬɢɤɚ ɝɚɪɚɧɬɢɪɭɟɬ, ɱɬɨ ɩɪɢ ɷɬɨɦ ɧɟ ɫɥɭɱɢɬɫɹ ɜɡɚɢɦɧɵɯ ɛɥɨɤɢɪɨɜɨɤ (deadlocks). ɋɭɳɟɫɬɜɭɸɬ, ɨɞɧɚɤɨ, ɢ ɞɪɭɝɢɟ ɨɛɫɬɨɹɬɟɥɶɫɬɜɚ ɜ ɨɬɧɨɲɟɧɢɢ ɷɬɨɣ ɩɨɥɢɬɢɤɢ, ɤɨɬɨɪɵɟ ɫɥɟɞɭɟɬ ɩɪɢɧɢɦɚɬɶ ɜɨ ɜɧɢɦɚɧɢɟ.
ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ɛɥɨɤɢɪɨɜɤɭ LOW_PRIORITY WRITE ɞɥɹ ɬɚɛɥɢɰɵ, ɷɬɨ ɨɡɧɚɱɚɟɬ
ɬɨɥɶɤɨ, ɱɬɨ MySQL ɛɭɞɟɬ ɨɠɢɞɚɬɶ ɦɨɦɟɧɬɚ, ɤɨɝɞɚ ɧɟ ɛɭɞɟɬ ɧɢ ɨɞɧɨɝɨ ɩɨɬɨɤɚ, ɤɨɬɨɪɵɣ
ɠɟɥɚɟɬ ɭɫɬɚɧɨɜɢɬɶ ɛɥɨɤɢɪɨɜɤɭ ɱɬɟɧɢɹ. Ʉɨɝɞɚ ɩɨɬɨɤɭ ɭɞɚɟɬɫɹ ɭɫɬɚɧɨɜɢɬɶ ɛɥɨɤɢɪɨɜɤɭ
ɡɚɩɢɫɢ ɨɞɧɨɣ ɬɚɛɥɢɰɵ, ɢ ɨɧ ɨɠɢɞɚɟɬ ɜɨɡɦɨɠɧɨɫɬɢ ɡɚɛɥɨɤɢɪɨɜɚɬɶ ɫɥɟɞɭɸɳɭɸ ɬɚɛɥɢɰɭ ɜ
ɫɩɢɫɤɟ, ɜɫɟ ɨɫɬɚɥɶɧɵɟ ɩɨɬɨɤɢ ɛɭɞɭɬ ɩɪɢɨɫɬɚɧɨɜɥɟɧɵ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɛɥɨɤɢɪɨɜɤɚ ɡɚɩɢɫɢ
ɧɟ ɛɭɞɟɬ ɫɧɹɬɚ. ȿɫɥɢ ɷɬɨ ɩɪɟɞɫɬɚɜɥɹɟɬ ɫɟɪɶɟɡɧɭɸ ɩɪɨɛɥɟɦɭ ɞɥɹ ɜɚɲɢɯ ɩɪɢɥɨɠɟɧɢɣ, ɜɵ
ɞɨɥɠɧɵ ɪɚɫɫɦɨɬɪɟɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɩɪɟɨɛɪɚɡɨɜɚɧɢɹ ɧɟɤɨɬɨɪɵɯ ɜɚɲɢɯ ɬɚɛɥɢɰ ɜ ɧɟɬɪɚɧɡɚɤɰɢɨɧɧɭɸ ɮɨɪɦɭ.
Ɇɨɠɧɨ ɛɟɡɨɩɚɫɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ KILL ɞɥɹ ɩɪɟɪɵɜɚɧɢɹ ɩɨɬɨɤɚ, ɤɨɬɨɪɵɣ ɨɠɢɞɚɟɬ ɛɥɨɤɢɪɨɜɤɢ ɬɚɛɥɢɰɵ. ɋɦ. ɪɚɡɞɟɥ 6.5.4.3.
Ɂɚɦɟɬɶɬɟ, ɱɬɨ ɜɵ ɧɟ ɞɨɥɠɧɵ ɛɥɨɤɢɪɨɜɚɬɶ ɧɢɤɚɤɢɯ ɬɚɛɥɢɰ ɢɡ ɬɟɯ, ɜ ɤɨɬɨɪɵɯ ɜɵɩɨɥɧɹɟɬɟ INSERT DELAYED, ɩɨɬɨɦɭ ɱɬɨ ɜ ɷɬɨɦ ɫɥɭɱɚɟ INSERT ɜɵɩɨɥɧɹɟɬɫɹ ɨɬɞɟɥɶɧɵɦ ɩɨɬɨɤɨɦ ɫɟɪɜɟɪɚ.
Ɉɛɵɱɧɨ ɜɚɦ ɧɟ ɧɭɠɧɨ ɛɥɨɤɢɪɨɜɚɬɶ ɬɚɛɥɢɰɵ, ɩɨɫɤɨɥɶɤɭ ɜɫɟ ɨɬɞɟɥɶɧɵɟ ɨɩɟɪɚɬɨɪɵ
INSERT ɚɬɨɦɚɪɧɵ – ɬɨ ɟɫɬɶ ɧɢɤɚɤɨɣ ɞɪɭɝɨɣ ɩɨɬɨɤ ɧɟ ɦɨɠɟɬ ɜɦɟɲɚɬɶɫɹ ɜ ɢɫɩɨɥɧɹɟɦɵɣ ɜ
ɞɚɧɧɵɣ ɦɨɦɟɧɬ SQL-ɨɩɟɪɚɬɨɪ. Ɉɞɧɚɤɨ ɫɭɳɟɫɬɜɭɸɬ ɫɥɭɱɚɢ, ɤɨɝɞɚ ɛɥɨɤɢɪɨɜɚɬɶ ɬɚɛɥɢɰɵ
ɜɫɟ ɠɟ ɧɟɨɛɯɨɞɢɦɨ:
„ȿɫɥɢ ɜɵ ɫɨɛɢɪɚɟɬɟɫɶ ɜɵɩɨɥɧɹɬɶ ɦɧɨɠɟɫɬɜɨ ɨɩɟɪɚɰɢɣ ɧɚɞ ɧɚɛɨɪɨɦ ɬɚɛɥɢɰ MyISAM,
ɬɨ ɝɨɪɚɡɞɨ ɛɵɫɬɪɟɟ ɩɨɥɭɱɢɬɫɹ, ɟɫɥɢ ɢɯ ɩɪɟɞɜɚɪɢɬɟɥɶɧɨ ɡɚɛɥɨɤɢɪɨɜɚɬɶ. Ȼɥɨɤɢɪɨɜ-
282
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɤɚ ɬɚɛɥɢɰ MyISAM ɭɫɤɨɪɹɟɬ ɜɫɬɚɜɤɭ, ɨɛɧɨɜɥɟɧɢɟ ɢɥɢ ɭɞɚɥɟɧɢɟ ɜ ɧɢɯ. Ɉɬɪɢɰɚɬɟɥɶɧɚɹ ɫɬɨɪɨɧɚ ɷɬɨɝɨ ɫɨɫɬɨɢɬ ɜ ɬɨɦ, ɱɬɨ ɧɢ ɨɞɢɧ ɩɨɬɨɤ ɧɟ ɦɨɠɟɬ ɨɛɧɨɜɥɹɬɶ ɡɚɛɥɨɤɢɪɨɜɚɧɧɭɸ ɩɨ ɱɬɟɧɢɸ ɬɚɛɥɢɰɭ (ɜɤɥɸɱɚɹ ɬɨɬ, ɱɬɨ ɭɫɬɚɧɨɜɢɥ ɛɥɨɤɢɪɨɜɤɭ), ɢ ɧɢ ɨɞɢɧ
ɩɨɬɨɤ ɧɟ ɦɨɠɟɬ ɩɨɥɭɱɢɬɶ ɧɢɤɚɤɨɝɨ ɞɨɫɬɭɩɚ ɤ ɬɚɛɥɢɰɟ, ɡɚɛɥɨɤɢɪɨɜɚɧɧɨɣ ɩɨ ɡɚɩɢɫɢ, ɤɪɨɦɟ ɩɨɬɨɤɚ, ɭɫɬɚɧɨɜɢɜɲɟɝɨ ɛɥɨɤɢɪɨɜɤɭ.
ɉɪɢɱɢɧɚ ɬɨɝɨ, ɱɬɨ ɧɟɤɨɬɨɪɵɟ ɨɩɟɪɚɰɢɢ MyISAM ɪɚɛɨɬɚɸɬ ɛɵɫɬɪɟɟ ɧɚ ɛɥɨɤɢɪɨɜɚɧɧɵɯ ɬɚɛɥɢɰɚɯ, ɫɜɹɡɚɧɚ ɫ ɬɟɦ, ɱɬɨ MySQL ɧɟ ɫɛɪɚɫɵɜɚɟɬ ɧɚ ɞɢɫɤ ɢɧɞɟɤɫɧɵɣ ɤɷɲ
ɞɥɹ ɷɬɢɯ ɬɚɛɥɢɰ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɧɟ ɛɭɞɟɬ ɜɵɡɜɚɧ UNLOCK TABLES. Ɉɛɵɱɧɨ ɢɧɞɟɤɫɧɵɟ ɤɷɲɢ ɫɛɪɚɫɵɜɚɸɬɫɹ ɩɨɫɥɟ ɤɚɠɞɨɝɨ SQL-ɨɩɟɪɚɬɨɪɚ.
„ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ɦɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ MySQL, ɤɨɬɨɪɵɟ ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬ ɬɪɚɧɡɚɤɰɢɣ, ɜɵ ɞɨɥɠɧɵ ɜɵɞɚɜɚɬɶ LOCK TABLES, ɟɫɥɢ ɯɨɬɢɬɟ ɝɚɪɚɧɬɢɪɨɜɚɬɶ, ɱɬɨ ɦɟɠɞɭ
SELECT ɢ UPDATE ɧɟ ɛɭɞɭɬ ɜɵɞɚɧɵ ɞɪɭɝɢɟ ɨɩɟɪɚɬɨɪɵ. ɉɪɢɜɟɞɟɧɧɵɣ ɧɢɠɟ ɩɪɢɦɟɪ
ɬɪɟɛɭɟɬ LOCK TABLES ɞɥɹ ɛɟɡɨɩɚɫɧɨɝɨ ɜɵɩɨɥɧɟɧɢɹ:
mysql>
mysql>
mysql>
->
->
mysql>
LOCK TABLES trans READ, customer WRITE;
SELECT SUM(value) FROM trans WHERE customer_id=ɢɞɟɧɬɢɮɢɤɚɬɨɪ;
UPDATE customer
SET total_value=ɫɭɦɦɚ_ɢɡ_ɩɪɟɞɵɞɭɳɟɝɨ_ɨɩɟɪɚɬɨɪɚ
WHERE customer_id=ɢɞɟɧɬɢɮɢɤɚɬɨɪ;
UNLOCK TABLES;
Ȼɟɡ LOCK TABLES ɫɭɳɟɫɬɜɭɟɬ ɜɨɡɦɨɠɧɨɫɬɶ ɬɨɝɨ, ɱɬɨ ɞɪɭɝɨɣ ɩɨɬɨɤ ɦɨɠɟɬ ɜɫɬɚɜɢɬɶ
ɧɨɜɭɸ ɫɬɪɨɤɭ ɜ ɬɚɛɥɢɰɭ trans ɦɟɠɞɭ ɜɵɩɨɥɧɟɧɢɟɦ ɜɚɲɢɯ ɨɩɟɪɚɬɨɪɨɜ SELECT ɢ
UPDATE.
ȼɵ ɦɨɠɟɬɟ ɢɡɛɟɠɚɬɶ ɩɪɢɦɟɧɟɧɢɹ LOCK TABLES ɜɨ ɦɧɨɝɢɯ ɫɥɭɱɚɹɯ, ɩɪɢɦɟɧɹɹ ɨɬɧɨɫɢɬɟɥɶɧɵɟ ɨɛɧɨɜɥɟɧɢɹ (UPDATE customer SET value=value+new_value), ɥɢɛɨ ɮɭɧɤɰɢɸ
LAST_INSERT_ID(). ɋɦ. ɪɚɡɞɟɥ 1.8.5.3.
ɂɡɛɟɠɚɬɶ ɛɥɨɤɢɪɨɜɤɢ ɬɚɛɥɢɰ ɦɨɠɧɨ ɬɚɤɠɟ ɜ ɧɟɤɨɬɨɪɵɯ ɫɥɭɱɚɹɯ, ɢɫɩɨɥɶɡɭɹ ɮɭɧɤɰɢɢ
ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɨɝɨ ɭɪɨɜɧɹ GET_LOCK() ɢ RELEASE_LOCK(). ɗɬɢ ɛɥɨɤɢɪɨɜɤɢ ɫɨɯɪɚɧɹɸɬɫɹ ɜ
ɯɷɲ-ɬɚɛɥɢɰɟ ɧɚ ɫɟɪɜɟɪɟ ɢ ɞɥɹ ɫɤɨɪɨɫɬɢ ɪɟɚɥɢɡɭɸɬɫɹ ɜɵɡɨɜɚɦɢ pthread_mutex_lock() ɢ
pthread_mutex_unlock(). ɋɦ. ɪɚɡɞɟɥ 5.8.4.
ȼɵ ɦɨɠɟɬɟ ɡɚɛɥɨɤɢɪɨɜɚɬɶ ɩɨ ɱɬɟɧɢɸ ɜɫɟ ɬɚɛɥɢɰɵ ɜɨ ɜɫɟɯ ɛɚɡɚɯ ɞɚɧɧɵɯ ɨɩɟɪɚɬɨɪɨɦ
FLUSH TABLES WITH READ LOCK. ɋɦ. ɪɚɡɞɟɥ 6.5.4.2. ɗɬɨ ɨɱɟɧɶ ɭɞɨɛɧɵɣ ɫɩɨɫɨɛ ɩɨɥɭɱɢɬɶ
ɪɟɡɟɪɜɧɵɟ ɤɨɩɢɢ, ɟɫɥɢ ɜɵ ɪɚɛɨɬɚɟɬɟ ɫ ɮɚɣɥɨɜɨɣ ɫɢɫɬɟɦɨɣ ɜɪɨɞɟ Veritas, ɤɨɬɨɪɚɹ ɦɨɠɟɬ
ɞɟɥɚɬɶ ɫɧɢɦɤɢ ɜɨ ɜɪɟɦɟɧɢ.
oË ÑËÕÏÛµÂ!
ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ALTER TABLE ɧɚ ɛɥɨɤɢɪɨɜɚɧɧɨɣ ɬɚɛɥɢɰɟ, ɨɧɚ ɦɨɠɟɬ ɪɚɡɛɥɨɤɢɪɨɜɚɬɶɫɹ.
ɋɦ. ɪɚɡɞɟɥ A.3.1.
6.4.6. Ñèíòàêñèñ SET TRANSACTION
SET [GLOBAL | SESSION] TRANSACTION ISOLATION LEVEL
{ READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE }
ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɭɪɨɜɟɧɶ ɢɡɨɥɹɰɢɢ ɫɥɟɞɭɸɳɟɣ ɬɪɚɧɡɚɤɰɢɢ, ɝɥɨɛɚɥɶɧɨ
ɥɢɛɨ ɬɨɥɶɤɨ ɞɥɹ ɬɟɤɭɳɟɝɨ ɫɟɚɧɫɚ.
ɉɨɜɟɞɟɧɢɟ SET TRANSACTION ɩɨ ɭɦɨɥɱɚɧɢɸ ɡɚɤɥɸɱɚɟɬɫɹ ɜ ɬɨɦ, ɱɬɨ ɨɧ ɭɫɬɚɧɚɜɥɢɜɚɟɬ
ɭɪɨɜɟɧɶ ɢɡɨɥɹɰɢɢ ɞɥɹ ɫɥɟɞɭɸɳɟɣ (ɟɳɟ ɧɟ ɫɬɚɪɬɨɜɚɜɲɟɣ) ɬɪɚɧɡɚɤɰɢɢ. ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ GLOBAL, ɨɩɟɪɚɬɨɪ ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɝɥɨɛɚɥɶɧɵɣ ɭɪɨɜɟɧɶ ɢɡɨɥɹɰɢɢ
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
283
ɬɪɚɧɡɚɤɰɢɣ ɩɨ ɭɦɨɥɱɚɧɢɸ ɞɥɹ ɜɫɟɯ ɧɨɜɵɯ ɫɨɟɞɢɧɟɧɢɣ, ɤɨɬɨɪɵɟ ɛɭɞɭɬ ɭɫɬɚɧɨɜɥɟɧɵ ɫ
ɷɬɨɝɨ ɦɨɦɟɧɬɚ. ɋɭɳɟɫɬɜɭɸɳɢɟ ɫɨɟɞɢɧɟɧɢɹ ɧɟ ɡɚɬɪɚɝɢɜɚɸɬɫɹ. Ⱦɥɹ ɜɵɩɨɥɧɟɧɢɹ ɷɬɨɝɨ
ɨɩɟɪɚɬɨɪɚ ɧɭɠɧɨ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ SUPER. ɉɪɢɦɟɧɟɧɢɟ ɤɥɸɱɟɜɨɝɨ ɫɥɨɜɚ SESSION ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɭɪɨɜɟɧɶ ɢɡɨɥɹɰɢɢ ɩɨ ɭɦɨɥɱɚɧɢɸ ɜɫɟɯ ɛɭɞɭɳɢɯ ɬɪɚɧɡɚɤɰɢɣ ɬɨɥɶɤɨ ɞɥɹ ɬɟɤɭɳɟɝɨ ɫɟɚɧɫɚ.
Ɉɩɢɫɚɧɢɟ ɭɪɨɜɧɟɣ ɢɡɨɥɹɰɢɢ ɬɪɚɧɡɚɤɰɢɣ InnoDB ɩɪɢɜɟɞɟɧɨ ɜ ɤɧɢɝɟ MySQL. Ɋɭɤɨɜɨɞɫɬɜɨ ɚɞɦɢɧɢɫɬɪɚɬɨɪɚ (Ɇ. : ɂɡɞɚɬɟɥɶɫɤɢɣ ɞɨɦ “ȼɢɥɶɹɦɫ”, 2005, ISBN 5-8459-0805-1).
InnoDB ɩɨɞɞɟɪɠɢɜɚɟɬ ɷɬɢ ɭɪɨɜɧɢ, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.0.5. ɍɪɨɜɟɧɶ ɢɡɨɥɹɰɢɢ ɩɨ
ɭɦɨɥɱɚɧɢɸ – REPEATABLE READ.
ȼɵ ɦɨɠɟɬɟ ɬɚɤɠɟ ɭɫɬɚɧɨɜɢɬɶ ɧɚɱɚɥɶɧɵɣ ɝɥɨɛɚɥɶɧɵɣ ɭɪɨɜɟɧɶ ɢɡɨɥɹɰɢɢ ɞɥɹ ɫɟɪɜɟɪɚ
mysqld, ɡɚɩɭɫɬɢɜ ɟɝɨ ɫ ɨɩɰɢɟɣ --transaction-isolation.
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû
äàííûõ
6.5.1. Îïåðàòîðû óïðàâëåíèÿ ó÷åòíûìè çàïèñÿìè
6.5.1.1. Ñèíòàêñèñ DROP USER
DROP USER ɩɨɥɶɡɨɜɚɬɟɥɶ
Ɉɩɟɪɚɬɨɪ DROP USER ɭɞɚɥɹɟɬ ɭɱɟɬɧɭɸ ɡɚɩɢɫɶ ɩɨɥɶɡɨɜɚɬɟɥɹ MySQL, ɤɨɬɨɪɵɣ ɧɟ
ɢɦɟɟɬ ɧɢɤɚɤɢɯ ɩɪɢɜɢɥɟɝɢɣ. Ɉɧ ɫɥɭɠɢɬ ɞɥɹ ɭɞɚɥɟɧɢɹ ɭɱɟɬɧɨɣ ɡɚɩɢɫɢ ɢɡ ɬɚɛɥɢɰɵ user.
ɍɱɟɬɧɚɹ ɡɚɩɢɫɶ ɢɦɟɧɭɟɬɫɹ ɜ ɬɚɤɨɦ ɠɟ ɮɨɪɦɚɬɟ, ɤɚɤ ɞɥɹ GRANT ɢɥɢ REVOKE, ɧɚɩɪɢɦɟɪ,
'jeffrey'@'localhost'. ɑɚɫɬɢ ɢɦɟɧɢ ɩɨɥɶɡɨɜɚɬɟɥɹ ɢ ɢɦɟɧɢ ɯɨɫɬɚ ɫɨɨɬɜɟɬɫɬɜɭɸɬ ɡɧɚɱɟɧɢɸ ɫɬɨɥɛɰɨɜ User ɢ Host ɡɚɩɢɫɢ ɨ ɩɨɥɶɡɨɜɚɬɟɥɟ ɜ ɬɚɛɥɢɰɟ user.
ɑɬɨɛɵ ɭɞɚɥɢɬɶ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɭɸ ɭɱɟɬɧɭɸ ɡɚɩɢɫɶ, ɜɵ ɞɨɥɠɧɵ ɢɫɩɨɥɶɡɨɜɚɬɶ ɫɥɟɞɭɸɳɭɸ ɩɪɨɰɟɞɭɪɭ, ɜɵɩɨɥɧɹɹ ɲɚɝɢ ɜ ɭɤɚɡɚɧɧɨɦ ɩɨɪɹɞɤɟ:
1. ȼɨɫɩɨɥɶɡɨɜɚɬɶɫɹ SHOW GRANTS ɞɥɹ ɨɩɪɟɞɟɥɟɧɢɹ, ɤɚɤɢɦɢ ɩɪɢɜɢɥɟɝɢɹɦɢ ɨɛɥɚɞɚɟɬ
ɭɱɟɬɧɚɹ ɡɚɩɢɫɶ. ɋɦ. ɪɚɡɞɟɥ 6.5.3.10.
2. ɋ ɩɨɦɨɳɶɸ REVOKE ɥɢɲɢɬɶ ɩɨɥɶɡɨɜɚɬɟɥɹ ɩɪɢɜɢɥɟɝɢɣ, ɩɨɤɚɡɚɧɧɵɯ SHOW GRANTS.
ɋɦ. ɪɚɡɞɟɥ 6.5.1.2.
3. ɍɞɚɥɢɬɶ ɭɱɟɬɧɭɸ ɡɚɩɢɫɶ ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ DROP USER, ɢɥɢ ɭɞɚɥɢɜ ɡɚɩɢɫɶ ɨ
ɩɨɥɶɡɨɜɚɬɟɥɟ ɢɡ ɬɚɛɥɢɰɵ user.
Ɉɩɟɪɚɬɨɪ DROP USER ɛɵɥ ɞɨɛɚɜɥɟɧ ɜ MySQL 4.1.1. Ⱦɨ ɷɬɨɣ ɜɟɪɫɢɢ ɧɟɨɛɯɨɞɢɦɨ ɛɵɥɨ
ɥɢɲɢɬɶ ɩɨɥɶɡɨɜɚɬɟɥɹ ɩɪɢɜɢɥɟɝɢɣ, ɤɚɤ ɨɩɢɫɚɧɨ, ɚ ɩɨɬɨɦ ɬɨɥɶɤɨ ɭɞɚɥɢɬɶ ɡɚɩɢɫɶ ɢɡ ɬɚɛɥɢɰɵ user ɢ ɫɛɪɨɫɢɬɶ ɬɚɛɥɢɰɵ ɩɪɢɜɢɥɟɝɢɣ, ɤɚɤ ɩɨɤɚɡɚɧɨ ɧɢɠɟ:
mysql> DELETE FROM mysql.user
-> WHERE User='ɢɦɹ_ɩɨɥɶɡɨɜɚɬɟɥɹ' and Host='ɢɦɹ_ɯɨɫɬɚ';
mysql> FLUSH PRIVILEGES;
6.5.1.2. Ñèíòàêñèñ GRANT è REVOKE
GRANT ɬɢɩ_ɩɪɢɜɢɥɟɝɢɢ [(ɫɩɢɫɨɤ_ɫɬɨɥɛɰɨɜ)] [, ɬɢɩ_ɩɪɢɜɢɥɟɝɢɢ
[(ɫɩɢɫɨɤ_ɫɬɨɥɛɰɨɜ)]] ...
ON {ɢɦɹ_ɬɚɛɥɢɰɵ | * | *.* | ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.*}
TO user [IDENTIFIED BY [PASSWORD] 'ɩɚɪɨɥɶ']
[, ɩɨɥɶɡɨɜɚɬɟɥɶ [IDENTIFIED BY [PASSWORD] 'ɩɚɪɨɥɶ']] ...
284
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
[REQUIRE
NONE |
[{SSL| X509}]
[CIPHER 'ɲɢɮɪ' [AND]]
[ISSUER 'ɜɵɞɚɸɳɢɣ' [AND]]
[SUBJECT 'ɫɭɛɴɟɤɬ']]
[WITH [GRANT OPTION | MAX_QUERIES_PER_HOUR ɤɨɥɢɱɟɫɬɜɨ |
MAX_UPDATES_PER_HOUR ɤɨɥɢɱɟɫɬɜɨ |
MAX_CONNECTIONS_PER_HOUR ɤɨɥɢɱɟɫɬɜɨ]]
REVOKE ɬɢɩ_ɩɪɢɜɢɥɟɝɢɢ [(ɫɩɢɫɨɤ_ɫɬɨɥɛɰɨɜ)] [, ɬɢɩ_ɩɪɢɜɢɥɟɝɢɢ
[(ɫɩɢɫɨɤ_ɫɬɨɥɛɰɨɜ)]] ...
ON {ɢɦɹ_ɬɚɛɥɢɰɵ | * | *.* | ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.*}
FROM ɩɨɥɶɡɨɜɚɬɟɥɶ [, ɩɨɥɶɡɨɜɚɬɟɥɶ] ...
REVOKE ALL PRIVILEGES, GRANT OPTION FROM ɩɨɥɶɡɨɜɚɬɟɥɶ [, ɩɨɥɶɡɨɜɚɬɟɥɶ] ...
Ɉɩɟɪɚɬɨɪɵ GRANT ɢ REVOKE ɩɨɡɜɨɥɹɸɬ ɚɞɦɢɧɢɫɬɪɚɬɨɪɭ ɫɢɫɬɟɦɵ ɫɨɡɞɚɜɚɬɶ ɭɱɟɬɧɵɟ
ɡɚɩɢɫɢ ɩɨɥɶɡɨɜɚɬɟɥɟɣ MySQL, ɜɵɞɚɜɚɬɶ ɢɦ ɩɪɢɜɢɥɟɝɢɢ ɢ ɨɬɛɢɪɚɬɶ ɢɯ. GRANT ɢ REVOKE
ɪɟɚɥɢɡɨɜɚɧɵ ɜ ɜɟɪɫɢɢ MySQL 3.22.11 ɢ ɜɵɲɟ. ȼ ɛɨɥɟɟ ɪɚɧɧɢɯ ɜɟɪɫɢɹɯ MySQL ɷɬɢ ɨɩɟɪɚɬɨɪɵ ɧɟ ɞɟɥɚɥɢ ɧɢɱɟɝɨ.
ɂɧɮɨɪɦɚɰɢɹ ɨɛ ɭɱɟɬɧɵɯ ɡɚɩɢɫɹɯ ɯɪɚɧɢɬɫɹ ɜ ɬɚɛɥɢɰɚɯ ɛɚɡɵ ɞɚɧɧɵɯ mysql. ɗɬɚ ɛɚɡɚ ɢ
ɫɢɫɬɟɦɚ ɭɩɪɚɜɥɟɧɢɹ ɞɨɫɬɭɩɨɦ ɩɨɞɪɨɛɧɨ ɨɩɢɫɵɜɚɸɬɫɹ ɜ ɤɧɢɝɟ MySQL. Ɋɭɤɨɜɨɞɫɬɜɨ ɚɞɦɢɧɢɫɬɪɚɬɨɪɚ.
ɉɪɢɜɢɥɟɝɢɢ ɦɨɝɭɬ ɛɵɬɶ ɜɵɞɚɧɵ ɧɚ ɱɟɬɵɪɟɯ ɭɪɨɜɧɹɯ:
„Ƚɥɨɛɚɥɶɧɵɣ ɭɪɨɜɟɧɶ. Ƚɥɨɛɚɥɶɧɵɟ ɩɪɢɜɢɥɟɝɢɢ ɤɚɫɚɸɬɫɹ ɜɫɟɯ ɛɚɡ ɞɚɧɧɵɯ ɧɚ ɫɟɪɜɟɪɟ. ɗɬɢ ɩɪɢɜɢɥɟɝɢɢ ɫɨɯɪɚɧɹɸɬɫɹ ɜ ɬɚɛɥɢɰɟ mysql.user. GRANT ALL ON *.* ɢ
REVOKE ALL ON *.* ɜɵɞɚɸɬ ɢ ɨɬɛɢɪɚɸɬ ɬɨɥɶɤɨ ɝɥɨɛɚɥɶɧɵɟ ɩɪɢɜɢɥɟɝɢɢ.
„ɍɪɨɜɟɧɶ ɛɚɡɵ ɞɚɧɧɵɯ. ɉɪɢɜɢɥɟɝɢɢ ɭɪɨɜɧɹ ɛɚɡɵ ɞɚɧɧɵɯ ɤɚɫɚɸɬɫɹ ɜɫɟɯ ɬɚɛɥɢɰ ɜ
ɛɚɡɟ ɞɚɧɧɵɯ. ɗɬɢ ɩɪɢɜɢɥɟɝɢɢ ɯɪɚɧɹɬɫɹ ɜ ɬɚɛɥɢɰɚɯ mysql.db ɢ mysql.host. GRANT
ALL ON ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.* ɢ REVOKE ALL ON ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.* ɜɵɞɚɸɬ ɢ ɨɬɛɢɪɚɸɬ ɬɨɥɶɤɨ ɩɪɢɜɢɥɟɝɢɢ ɭɪɨɜɧɹ ɛɚɡɵ ɞɚɧɧɵɯ.
„ɍɪɨɜɟɧɶ ɬɚɛɥɢɰɵ. ɉɪɢɜɢɥɟɝɢɢ ɭɪɨɜɧɹ ɬɚɛɥɢɰɵ ɤɚɫɚɸɬɫɹ ɜɫɟɯ ɫɬɨɥɛɰɨɜ ɜ ɞɚɧɧɨɣ
ɬɚɛɥɢɰɟ. ɗɬɢ ɩɪɢɜɢɥɟɝɢɢ ɯɪɚɧɹɬɫɹ ɜ ɬɚɛɥɢɰɟ mysql.tables_priv. GRANT ALL ON
ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.ɢɦɹ_ɬɚɛɥɢɰɵ ɢ REVOKE ALL ON ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.ɢɦɹ_ɬɚɛɥɢɰɵ
ɜɵɞɚɸɬ ɢ ɨɬɛɢɪɚɸɬ ɬɨɥɶɤɨ ɩɪɢɜɢɥɟɝɢɢ ɭɪɨɜɧɹ ɬɚɛɥɢɰɵ.
„ɍɪɨɜɟɧɶ ɫɬɨɥɛɰɚ. ɉɪɢɜɢɥɟɝɢɢ ɭɪɨɜɧɹ ɫɬɨɥɛɰɚ ɤɚɫɚɸɬɫɹ ɨɬɞɟɥɶɧɵɯ ɫɬɨɥɛɰɨɜ ɜ
ɬɚɛɥɢɰɟ. ɗɬɢ ɩɪɢɜɢɥɟɝɢɢ ɯɪɚɧɹɬɫɹ ɜ ɬɚɛɥɢɰɟ mysql.columns_priv. ɉɪɢ ɢɫɩɨɥɶɡɨɜɚɧɢɢ REVOKE ɜɵ ɞɨɥɠɧɵ ɭɤɚɡɚɬɶ ɬɟ ɠɟ ɫɬɨɥɛɰɵ, ɤɨɬɨɪɵɟ ɩɪɢɫɭɬɫɬɜɨɜɚɥɢ ɜ GRANT.
ɑɬɨɛɵ ɨɛɥɟɝɱɢɬɶ ɥɢɲɟɧɢɟ ɜɫɟɯ ɩɪɢɜɢɥɟɝɢɣ, ɜ MySQL 4.1.2 ɞɨɛɚɜɥɟɧ ɫɥɟɞɭɸɳɢɣ
ɫɢɧɬɚɤɫɢɫ, ɤɨɬɨɪɵɣ ɭɞɚɥɹɟɬ ɜɫɟ ɩɪɢɜɢɥɟɝɢɢ ɭɪɨɜɧɹ ɛɚɡ ɞɚɧɧɵɯ, ɬɚɛɥɢɰ ɢ ɫɬɨɥɛɰɨɜ ɞɥɹ
ɭɤɚɡɚɧɧɵɯ ɩɨɥɶɡɨɜɚɬɟɥɟɣ:
REVOKE ALL PRIVILEGES, GRANT OPTION FROM ɩɨɥɶɡɨɜɚɬɟɥɶ [, ɩɨɥɶɡɨɜɚɬɟɥɶ] ...
Ⱦɨ ɜɟɪɫɢɢ MySQL 4.1.2 ɜɫɟ ɩɪɢɜɢɥɟɝɢɢ ɧɟɥɶɡɹ ɛɵɥɨ ɭɞɚɥɢɬɶ ɫɪɚɡɭ. ɇɟɨɛɯɨɞɢɦɨ ɛɵɥɨ ɜɵɩɨɥɧɢɬɶ ɞɜɚ ɨɩɟɪɚɬɨɪɚ:
REVOKE ALL PRIVILEGES FROM ɩɨɥɶɡɨɜɚɬɟɥɶ [, ɩɨɥɶɡɨɜɚɬɟɥɶ] ...
REVOKE GRANT OPTION FROM ɩɨɥɶɡɨɜɚɬɟɥɶ [, ɩɨɥɶɡɨɜɚɬɟɥɶ] ...
Ⱦɥɹ ɨɩɟɪɚɬɨɪɨɜ GRANT ɢ REVOKE ɚɪɝɭɦɟɧɬ ɬɢɩ_ɩɪɢɜɢɥɟɝɢɢ ɦɨɠɟɬ ɩɪɢɧɢɦɚɬɶ ɡɧɚɱɟɧɢɹ,
ɩɟɪɟɱɢɫɥɟɧɧɵɟ ɜ ɬɚɛɥ. 6.1.
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
285
Ɍɚɛɥɢɰɚ 6.1. ɉɪɢɜɢɥɟɝɢɢ, ɢɫɩɨɥɶɡɭɟɦɵɟ ɜ ɨɩɟɪɚɬɨɪɚɯ GRANT ɢ REVOKE
ɉɪɢɜɢɥɟɝɢɹ
ɇɚɡɧɚɱɟɧɢɟ
ALL [PRIVILEGES]
ɍɫɬɚɧɚɜɥɢɜɚɟɬ ɩɪɨɫɬɵɟ ɩɪɢɜɢɥɟɝɢɢ ɤɪɨɦɟ GRANT OPTION.
ALTER
ɉɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ ALTER TABLE.
CREATE
ɉɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ CREATE TABLE.
CREATE TEMPORARY TABLES
ɉɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ CREATE TEMPORARY TABLE.
DELETE
ɉɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ DELETE.
DROP
ɉɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ DROP TABLE.
EXECUTE
ɉɨɡɜɨɥɹɟɬ ɡɚɩɭɫɤɚɬɶ ɯɪɚɧɢɦɵɟ ɩɪɨɰɟɞɭɪɵ (MySQL 5.0) .
FILE
ɉɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ SELECT...INTO OUTFILE
ɢ LOAD DATA INFILE.
INDEX
ɉɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ CREATE INDEX ɢ DROP INDEX.
INSERT
ɉɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ INSERT.
LOCK TABLES
ɉɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ LOCK TABLES ɜ ɬɚɛɥɢɰɚɯ,
ɞɥɹ ɤɨɬɨɪɵɯ ɢɦɟɟɬɫɹ ɩɪɢɜɢɥɟɝɢɹ SELECT.
PROCESS
ɉɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ SHOW FULL PROCESSLIST.
REFERENCES
ɉɨɤɚ ɧɟ ɪɟɚɥɢɡɨɜɚɧɨ.
RELOAD
ɉɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ FLUSH.
REPLICATION CLIENT
ɉɨɡɜɨɥɹɟɬ ɡɚɩɪɚɲɢɜɚɬɶ, ɹɜɥɹɟɬɫɹ ɥɢ ɫɟɪɜɟɪ ɝɥɚɜɧɵɦ ɢɥɢ
ɩɨɞɱɢɧɟɧɧɵɦ ɜ ɪɟɩɥɢɤɚɰɢɢ.
REPLICATION SLAVE
ɇɟɨɛɯɨɞɢɦɨ ɞɥɹ ɩɨɞɱɢɧɟɧɧɵɯ ɫɟɪɜɟɪɨɜ ɜ ɪɟɩɥɢɤɚɰɢɢ
(ɞɥɹ ɱɬɟɧɢɹ ɛɢɧɚɪɧɵɯ ɠɭɪɧɚɥɨɜ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ).
SELECT
ɉɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ SELECT.
SHOW DATABASES
SHOW DATABASES ɩɨɤɚɡɵɜɚɟɬ ɜɫɟ ɛɚɡɵ ɞɚɧɧɵɯ.
SHUTDOWN
ɉɨɡɜɨɥɹɟɬ ɩɪɢɦɟɧɹɬɶ mysqladmin shutdown.
SUPER
ɉɨɡɜɨɥɹɟɬ ɜɵɩɨɥɧɹɬɶ ɨɩɟɪɚɬɨɪɵ CHANGE MASTER, KILL,
PURGE MASTER LOGS ɢ SET GLOBAL, ɤɨɦɚɧɞɭ mysqladmin
debug, ɪɚɡɪɟɲɚɟɬ ɩɨɞɤɥɸɱɚɬɶɫɹ (ɨɞɢɧ ɪɚɡ), ɞɚɠɟ ɟɫɥɢ ɞɨɫɬɢɝɧɭɬɨ ɱɢɫɥɨ ɫɨɟɞɢɧɟɧɢɣ max_connections.
UPDATE
ɉɨɡɜɨɥɹɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶ UPDATE.
USAGE
ɋɢɧɨɧɢɦ ɞɥɹ “ɨɬɫɭɬɫɬɜɢɹ ɩɪɢɜɢɥɟɝɢɣ”.
GRANT OPTION
ɉɨɡɜɨɥɹɟɬ ɩɟɪɟɞɚɜɚɬɶ ɩɪɢɜɢɥɟɝɢɢ ɞɪɭɝɢɦ ɩɨɥɶɡɨɜɚɬɟɥɹɦ.
USAGE ɦɨɠɧɨ ɩɪɢɦɟɧɹɬɶ ɞɥɹ ɫɨɡɞɚɧɢɹ ɩɨɥɶɡɨɜɚɬɟɥɹ, ɥɢɲɟɧɧɨɝɨ ɜɫɟɯ ɩɪɢɜɢɥɟɝɢɣ.
ɇɨɜɵɟ ɩɪɢɜɢɥɟɝɢɢ CREATE TEMPORARY TABLES, EXECUTE, LOCK TABLES, REPLICATION…,
SHOW DATABASES ɢ SUPER ɜɜɟɞɟɧɵ ɜ MySQL 4.0.2. ɑɬɨɛɵ ɢɯ ɢɫɩɨɥɶɡɨɜɚɬɶ ɩɨɫɥɟ ɨɛɧɨɜɥɟɧɢɹ
ɫɟɪɜɟɪɚ ɞɨ ɜɟɪɫɢɢ 4.0.2, ɧɟɨɛɯɨɞɢɦɨ ɡɚɩɭɫɬɢɬɶ ɫɰɟɧɚɪɢɣ mysql_fix_privilege_tables.
ȼ ɫɬɚɪɵɯ ɜɟɪɫɢɹɯ MySQL, ɝɞɟ ɧɟ ɛɵɥɨ ɩɪɢɜɢɥɟɝɢɢ SUPER, ɜɦɟɫɬɨ ɧɟɟ ɦɨɠɧɨ ɛɵɥɨ
ɢɫɩɨɥɶɡɨɜɚɬɶ PROCESS.
286
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ȼɵ ɦɨɠɟɬɟ ɜɵɞɚɜɚɬɶ ɝɥɨɛɚɥɶɧɵɟ ɩɪɢɜɢɥɟɝɢɢ, ɩɪɢɦɟɧɹɹ ɫɢɧɬɚɤɫɢɫ ON *.*, ɥɢɛɨ ɩɪɢɜɢɥɟɝɢɢ ɭɪɨɜɧɹ ɛɚɡɵ ɞɚɧɧɵɯ, ɢɫɩɨɥɶɡɭɹ ON ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.*. ȿɫɥɢ ɜɵ ɭɤɚɡɵɜɚɟɬɟ ON
*, ɢ ɭ ɜɚɫ ɟɫɬɶ ɬɟɤɭɳɚɹ ɛɚɡɚ ɞɚɧɧɵɯ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɩɪɢɜɢɥɟɝɢɢ ɛɭɞɭɬ ɜɵɞɚɧɵ ɧɚ ɬɟɤɭɳɭɸ ɛɚɡɭ ɞɚɧɧɵɯ. (ȼɧɢɦɚɧɢɟ! ȿɫɥɢ ɢɫɩɨɥɶɡɨɜɚɬɶ ɫɢɧɬɚɤɫɢɫ ON *, ɧɟ ɢɦɟɹ ɬɟɤɭɳɟɣ ɛɚɡɵ, ɬɨ ɩɪɢɜɢɥɟɝɢɢ ɛɭɞɭɬ ɜɵɞɚɧɵ ɝɥɨɛɚɥɶɧɨ.)
ɉɪɢɜɢɥɟɝɢɢ EXECUTION, FILE, PROCESS, RELOAD, REPLICATION CLIENT, REPLICATION
SLAVE, SHOW DATABASES, SHUTDOWN ɢ SUPER ɹɜɥɹɸɬɫɹ ɚɞɦɢɧɢɫɬɪɚɬɢɜɧɵɦɢ ɢ ɦɨɝɭɬ ɛɵɬɶ
ɜɵɞɚɧɵ ɬɨɥɶɤɨ ɝɥɨɛɚɥɶɧɨ (ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɫɢɧɬɚɤɫɢɫɚ ON *.*).
Ⱦɪɭɝɢɟ ɩɪɢɜɢɥɟɝɢɢ ɦɨɝɭɬ ɛɵɬɶ ɜɵɞɚɧɵ ɝɥɨɛɚɥɶɧɨ ɥɢɛɨ ɧɚ ɛɨɥɟɟ ɧɢɡɤɨɦ ɭɪɨɜɧɟ.
Ⱦɥɹ ɬɚɛɥɢɰ ɦɨɠɧɨ ɭɫɬɚɧɨɜɢɬɶ ɬɨɥɶɤɨ ɫɥɟɞɭɸɳɢɟ ɬɢɩɵ ɩɪɢɜɢɥɟɝɢɣ: SELECT, INSERT,
UPDATE, DELETE, CREATE, DROP, GRANT OPTION, INDEX ɢ ALTER.
Ⱦɥɹ ɫɬɨɥɛɰɨɜ (ɤɨɝɞɚ ɢɫɩɨɥɶɡɭɟɬɫɹ ɤɨɧɫɬɪɭɤɰɢɹ ɫɩɢɫɨɤ_ɫɬɨɥɛɰɨɜ) ɦɨɠɧɨ ɭɫɬɚɧɨɜɢɬɶ
ɬɨɥɶɤɨ ɫɥɟɞɭɸɳɢɟ ɬɢɩɵ ɩɪɢɜɢɥɟɝɢɣ: SELECT, INSERT ɢ UPDATE.
GRANT ALL ɜɵɞɚɟɬ ɩɪɢɜɢɥɟɝɢɢ, ɫɭɳɟɫɬɜɭɸɳɢɟ ɬɨɥɶɤɨ ɧɚ ɬɟɤɭɳɟɦ ɭɪɨɜɧɟ. ɇɚɩɪɢɦɟɪ,
ɟɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ GRANT ALL ON ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.*, ɱɬɨ ɹɜɥɹɟɬɫɹ ɨɩɟɪɚɬɨɪɨɦ ɭɪɨɜɧɹ
ɛɚɡɵ ɞɚɧɧɵɯ, ɬɨ ɧɢɤɚɤɢɟ ɢɡ ɝɥɨɛɚɥɶɧɵɯ ɩɪɢɜɢɥɟɝɢɣ ɧɚɩɨɞɨɛɢɟ FILE ɧɟ ɜɵɞɚɸɬɫɹ.
MySQL ɩɨɡɜɨɥɹɟɬ ɧɚɡɧɚɱɚɬɶ ɩɪɢɜɢɥɟɝɢɢ ɭɪɨɜɧɹ ɛɚɡɵ ɞɚɧɧɵɯ, ɞɚɠɟ ɟɫɥɢ ɷɬɚ ɛɚɡɚ ɧɟ
ɫɭɳɟɫɬɜɭɟɬ. ɗɬɨ ɨɛɥɟɝɱɚɟɬ ɩɨɞɝɨɬɨɜɤɭ ɤ ɷɤɫɩɥɭɚɬɚɰɢɢ ɛɚɡɵ ɞɚɧɧɵɯ. Ɉɞɧɚɤɨ ɜ ɧɚɫɬɨɹɳɢɣ ɦɨɦɟɧɬ MySQL ɧɟ ɩɨɡɜɨɥɹɟɬ ɧɚɡɧɚɱɚɬɶ ɩɪɢɜɢɥɟɝɢɣ ɭɪɨɜɧɹ ɬɚɛɥɢɰɵ ɞɥɹ ɧɟɫɭɳɟɫɬɜɭɸɳɢɯ ɬɚɛɥɢɰ.
MySQL ɧɟ ɭɞɚɥɹɟɬ ɧɢɤɚɤɢɯ ɩɪɢɜɢɥɟɝɢɣ ɚɜɬɨɦɚɬɢɱɟɫɤɢ, ɞɚɠɟ ɟɫɥɢ ɜɵ ɭɧɢɱɬɨɠɚɟɬɟ
ɬɚɛɥɢɰɭ ɢɥɢ ɛɚɡɭ ɞɚɧɧɵɯ.
oË ÑËÕÏÛµÂ!
ɒɚɛɥɨɧɧɵɟ ɫɢɦɜɨɥɵ ‘%’ ɢ ‘_’ ɞɨɩɭɫɤɚɸɬɫɹ ɜ ɫɩɟɰɢɮɢɤɚɰɢɹɯ ɢɦɟɧ ɛɚɡ ɞɚɧɧɵɯ ɞɥɹ ɨɩɟɪɚɬɨɪɨɜ
GRANT ɝɥɨɛɚɥɶɧɨɝɨ ɭɪɨɜɧɹ ɢ ɭɪɨɜɧɹ ɛɚɡɵ ɞɚɧɧɵɯ. ɗɬɨ ɨɡɧɚɱɚɟɬ, ɧɚɩɪɢɦɟɪ, ɱɬɨ ɟɫɥɢ ɜɵ ɯɨɬɢɬɟ
ɢɫɩɨɥɶɡɨɜɚɬɶ ɫɢɦɜɨɥ ‘_’ ɤɚɤ ɱɚɫɬɶ ɢɦɟɧɢ ɛɚɡɵ, ɬɨ ɞɨɥɠɧɵ ɩɪɟɞɜɚɪɹɬɶ ɟɝɨ ɨɛɪɚɬɧɨɣ ɤɨɫɨɣ ɱɟɪɬɨɣ
(‘\_’) ɜ ɚɪɝɭɦɟɧɬɚɯ ɨɩɟɪɚɬɨɪɚ GRANT, ɱɬɨɛɵ ɩɪɟɞɨɬɜɪɚɬɢɬɶ ɧɟɠɟɥɚɬɟɥɶɧɵɣ ɞɨɫɬɭɩ ɤ ɛɚɡɚɦ ɫ
ɢɦɟɧɚɦɢ, ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɦɢ ɲɚɛɥɨɧɭ, ɧɚɩɪɢɦɟɪ, GRANT...ON 'foo\_bar'.* TO...
Ⱦɥɹ ɬɨɝɨ ɱɬɨɛɵ ɜɵɞɚɜɚɬɶ ɩɪɚɜɚ ɞɨɫɬɭɩɚ ɩɨɥɶɡɨɜɚɬɟɥɹɦ ɫ ɪɚɡɥɢɱɧɵɯ ɯɨɫɬɨɜ, MySQL ɩɨɞɞɟɪɠɢɜɚɟɬ ɫɩɟɰɢɮɢɤɚɰɢɸ ɡɧɚɱɟɧɢɹ ɩɨɥɶɡɨɜɚɬɟɥɶ ɜ ɮɨɪɦɟ ɢɦɹ_ɩɨɥɶɡɨɜɚɬɟɥɹ@ɢɦɹ_ɯɨɫɬɚ.
ȿɫɥɢ ɜɚɦ ɧɭɠɧɨ ɭɤɚɡɚɬɶ ɫɬɪɨɤɭ ɢɦɹ_ɩɨɥɶɡɨɜɚɬɟɥɹ, ɜɤɥɸɱɚɸɳɭɸ ɫɩɟɰɢɚɥɶɧɵɟ ɫɢɦɜɨɥɵ
(ɬɚɤɢɟ ɤɚɤ ‘-’), ɥɢɛɨ ɫɬɪɨɤɭ ɢɦɹ_ɯɨɫɬɚ, ɜɤɥɸɱɚɸɳɭɸ ɫɩɟɰɢɚɥɶɧɵɟ ɫɢɦɜɨɥɵ ɢɥɢ ɫɢɦɜɨɥɵ ɲɚɛɥɨɧɨɜ (ɩɨɞɨɛɧɵɟ ‘%’), ɦɨɠɟɬɟ ɩɨɦɟɫɬɢɬɶ ɜ ɤɚɜɵɱɤɢ ɢɦɹ ɩɨɥɶɡɨɜɚɬɟɥɹ ɢ ɢɦɹ ɯɨɫɬɚ
(ɧɚɩɪɢɦɟɪ, 'test-user'@'test-hostname'). ɂɦɟɧɚ ɩɨɥɶɡɨɜɚɬɟɥɹ ɢ ɯɨɫɬɚ ɛɟɪɭɬɫɹ ɜ ɤɚɜɵɱɤɢ ɩɨ ɨɬɞɟɥɶɧɨɫɬɢ.
ȼɵ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɲɚɛɥɨɧɧɵɟ ɫɢɦɜɨɥɵ ɜ ɢɦɟɧɢ ɯɨɫɬɚ. ɇɚɩɪɢɦɟɪ,
ɢɦɹ_ɩɨɥɶɡɨɜɚɬɟɥɹ@'%.loc.gov' ɤɚɫɚɟɬɫɹ ɩɨɥɶɡɨɜɚɬɟɥɟɣ ɢɦɹ_ɩɨɥɶɡɨɜɚɬɟɥɹ ɧɚ ɥɸɛɵɯ
ɯɨɫɬɚɯ ɞɨɦɟɧɚ loc.gov, ɚ ɢɦɹ_ɩɨɥɶɡɨɜɚɬɟɥɹ@'144.155.166.%' ɤɚɫɚɟɬɫɹ ɩɨɥɶɡɨɜɚɬɟɥɟɣ
ɢɦɹ_ɩɨɥɶɡɨɜɚɬɟɥɹ ɫ ɥɸɛɨɝɨ ɯɨɫɬɚ ɩɨɞɫɟɬɢ ɤɥɚɫɫɚ ɋ 144.155.166.
ɉɪɨɫɬɚɹ ɮɨɪɦɚ ɢɦɹ_ɩɨɥɶɡɨɜɚɬɟɥɹ – ɫɢɧɨɧɢɦ ɞɥɹ ɢɦɹ_ɩɨɥɶɡɨɜɚɬɟɥɹ@'%'.
MySQL ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬ ɫɢɦɜɨɥɵ ɲɚɛɥɨɧɚ ɜ ɢɦɟɧɚɯ ɩɨɥɶɡɨɜɚɬɟɥɟɣ. Ⱥɧɨɧɢɦɧɵɟ
ɩɨɥɶɡɨɜɚɬɟɥɢ ɡɚɜɨɞɹɬɫɹ ɜɫɬɚɜɤɨɣ ɫɬɪɨɤ ɫ User='' ɜ ɬɚɛɥɢɰɭ mysql.user, ɢɥɢ ɫɨɡɞɚɧɢɟɦ
ɩɨɥɶɡɨɜɚɬɟɥɹ ɫ ɩɭɫɬɵɦ ɢɦɟɧɟɦ, ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ GRANT:
mysql> GRANT ALL ON test.* TO ''@'localhost' ...
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
287
dÖÒÕËÖÒÏ!
ȿɫɥɢ ɜɵ ɪɚɡɪɟɲɚɟɬɟ ɚɧɨɧɢɦɧɨɦɭ ɩɨɥɶɡɨɜɚɬɟɥɸ ɩɨɞɤɥɸɱɚɬɶɫɹ ɤ ɫɟɪɜɟɪɭ MySQL, ɬɨ ɬɚɤɠɟ ɞɨɥɠɧɵ ɜɵɞɚɬɶ ɩɪɢɜɢɥɟɝɢɢ ɜɫɟɦ ɥɨɤɚɥɶɧɵɦ ɩɨɥɶɡɨɜɚɬɟɥɹɦ ɢɦɹ_ɩɨɥɶɡɨɜɚɬɟɥɹ@localhost. ȼ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ ɭɱɟɬɧɚɹ ɡɚɩɢɫɶ ɚɧɨɧɢɦɧɨɝɨ ɩɨɥɶɡɨɜɚɬɟɥɹ ɞɥɹ ɥɨɤɚɥɶɧɨɝɨ ɯɨɫɬɚ ɜ ɬɚɛɥɢɰɟ
mysql.user ɛɭɞɟɬ ɡɚɞɟɣɫɬɜɨɜɚɧɚ, ɤɨɝɞɚ ɢɦɟɧɨɜɚɧɧɵɟ ɩɨɥɶɡɨɜɚɬɟɥɢ ɩɨɩɵɬɚɸɬɫɹ ɩɨɞɤɥɸɱɢɬɶɫɹ
ɤ ɫɟɪɜɟɪɭ MySQL ɫ ɥɨɤɚɥɶɧɨɣ ɦɚɲɢɧɵ! (ɍɱɟɬɧɚɹ ɡɚɩɢɫɶ ɚɧɨɧɢɦɧɨɝɨ ɩɨɥɶɡɨɜɚɬɟɥɹ ɫɨɡɞɚɟɬɫɹ ɜɨ
ɜɪɟɦɹ ɭɫɬɚɧɨɜɤɢ MySQL.)
ȼɵ ɦɨɠɟɬɟ ɨɩɪɟɞɟɥɢɬɶ, ɤɚɫɚɟɬɫɹ ɥɢ ɷɬɨ ɜɚɫ, ɜɵɩɨɥɧɢɜ ɫɥɟɞɭɸɳɢɣ ɡɚɩɪɨɫ:
mysql> SELECT Host, User FROM mysql.user WHERE User='';
ȿɫɥɢ ɜɨ ɢɡɛɟɠɚɧɢɟ ɨɩɢɫɚɧɧɨɣ ɩɪɨɛɥɟɦɵ ɜɵ ɯɨɬɢɬɟ ɭɞɚɥɢɬɶ ɭɱɟɬɧɭɸ ɡɚɩɢɫɶ ɚɧɨɧɢɦɧɨɝɨ ɥɨɤɚɥɶɧɨɝɨ ɩɨɥɶɡɨɜɚɬɟɥɹ, ɩɪɢɦɟɧɢɬɟ ɫɥɟɞɭɸɳɢɟ ɨɩɟɪɚɬɨɪɵ:
mysql> DELETE FROM mysql.user WHERE Host='localhost' AND User='';
mysql> FLUSH PRIVILEGES;
ɇɚ ɞɚɧɧɵɣ ɦɨɦɟɧɬ GRANT ɩɨɞɞɟɪɠɢɜɚɟɬ ɢɦɟɧɚ ɯɨɫɬɨɜ, ɬɚɛɥɢɰ, ɛɚɡ ɞɚɧɧɵɯ ɢ ɫɬɨɥɛɰɨɜ
ɞɨ 60 ɫɢɦɜɨɥɨɜ ɞɥɢɧɨɣ. ɂɦɹ ɩɨɥɶɡɨɜɚɬɟɥɹ ɦɨɠɟɬ ɛɵɬɶ ɧɟ ɞɥɢɧɧɟɟ 16 ɫɢɦɜɨɥɨɜ.
ɉɪɢɜɢɥɟɝɢɢ ɧɚ ɞɨɫɬɭɩ ɤ ɬɚɛɥɢɰɚɦ ɢ ɫɬɨɥɛɰɚɦ ɮɨɪɦɢɪɭɸɬɫɹ ɥɨɝɢɱɟɫɤɢɦ ɂɅɂ ɢɡ
ɩɪɢɜɢɥɟɝɢɣ, ɜɵɞɚɧɧɵɯ ɧɚ ɤɚɠɞɨɦ ɢɡ ɱɟɬɵɪɟɯ ɭɪɨɜɧɟɣ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɜ ɬɚɛɥɢɰɟ
mysql.user ɭɤɚɡɚɧɨ, ɱɬɨ ɩɨɥɶɡɨɜɚɬɟɥɶ ɢɦɟɟɬ ɝɥɨɛɚɥɶɧɭɸ ɩɪɢɜɢɥɟɝɢɸ SELECT, ɬɨ ɷɬɚ ɩɪɢɜɢɥɟɝɢɹ ɧɟ ɦɨɠɟɬ ɛɵɬɶ ɨɬɜɟɪɝɧɭɬɚ ɧɚ ɭɪɨɜɧɟ ɛɚɡɵ ɞɚɧɧɵɯ, ɬɚɛɥɢɰɵ ɢɥɢ ɫɬɨɥɛɰɚ.
ɉɪɢɜɢɥɟɝɢɢ ɧɚ ɫɬɨɥɛɰɵ ɦɨɝɭɬ ɛɵɬɶ ɜɵɱɢɫɥɟɧɵ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
ɝɥɨɛɚɥɶɧɵɟ ɩɪɢɜɢɥɟɝɢɢ
ɂɅɂ (ɩɪɢɜɢɥɟɝɢɢ ɛɚɡɵ ɞɚɧɧɵɯ ɂ ɩɪɢɜɢɥɟɝɢɢ ɯɨɫɬɚ)
ɂɅɂ ɩɪɢɜɢɥɟɝɢɢ ɬɚɛɥɢɰɵ
ɂɅɂ ɩɪɢɜɢɥɟɝɢɢ ɫɬɨɥɛɰɚ
ȼ ɛɨɥɶɲɢɧɫɬɜɟ ɫɥɭɱɚɟɜ ɩɪɢɜɢɥɟɝɢɢ ɜɵɞɚɸɬɫɹ ɩɨɥɶɡɨɜɚɬɟɥɸ ɬɨɥɶɤɨ ɧɚ ɨɞɧɨɦ ɢɡ
ɭɪɨɜɧɟɣ, ɩɨɷɬɨɦɭ ɜ ɪɟɚɥɶɧɨɣ ɠɢɡɧɢ ɷɬɨ ɜɫɟ ɧɟ ɬɚɤ ɫɥɨɠɧɨ.
ȿɫɥɢ ɜɵɞɚɸɬɫɹ ɩɪɢɜɢɥɟɝɢɢ ɞɥɹ ɤɨɦɛɢɧɚɰɢɢ “ɢɦɹ ɩɨɥɶɡɨɜɚɬɟɥɹ/ɢɦɹ ɯɨɫɬɚ”, ɤɨɬɨɪɨɣ
ɧɟɬ ɜ ɬɚɛɥɢɰɟ mysql.user, ɷɬɚ ɤɨɦɛɢɧɚɰɢɹ ɛɭɞɟɬ ɞɨɛɚɜɥɟɧɚ ɬɭɞɚ ɢ ɨɫɬɚɧɟɬɫɹ ɬɚɦ ɞɨ ɬɟɯ
ɩɨɪ, ɩɨɤɚ ɧɟ ɛɭɞɟɬ ɭɞɚɥɟɧɚ ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ DELETE.
Ⱦɪɭɝɢɦɢ ɫɥɨɜɚɦɢ, GRANT ɦɨɠɟɬ ɫɨɡɞɚɜɚɬɶ ɧɨɜɵɯ ɩɨɥɶɡɨɜɚɬɟɥɟɣ, ɧɨ REVOKE ɧɟ ɦɨɠɟɬ
ɭɞɚɥɹɬɶ ɢɯ. ȼɵ ɞɨɥɠɧɵ ɞɟɥɚɬɶ ɷɬɨ ɹɜɧɨ, ɩɪɢɦɟɧɹɹ DROP USER ɢɥɢ DELETE.
ȼ MySQL 3.22.12 ɢ ɩɨɫɥɟɞɭɸɳɢɯ ɜɟɪɫɢɹɯ, ɟɫɥɢ ɫɨɡɞɚɟɬɫɹ ɧɨɜɵɣ ɩɨɥɶɡɨɜɚɬɟɥɶ ɢɥɢ
ɟɫɥɢ ɜɵ ɢɦɟɟɬɟ ɝɥɨɛɚɥɶɧɵɟ ɩɪɢɜɢɥɟɝɢɢ ɧɚ ɜɵɞɚɱɭ ɞɪɭɝɢɯ ɩɪɢɜɢɥɟɝɢɣ, ɬɨ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɢɣ ɩɚɪɨɥɶ ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɤɨɧɫɬɪɭɤɰɢɟɣ IDENTIFYED BY, ɟɫɥɢ ɬɚɤɨɜɚɹ ɭɤɚɡɚɧɚ. ȿɫɥɢ ɭ
ɩɨɥɶɡɨɜɚɬɟɥɹ ɭɠɟ ɛɵɥ ɩɚɪɨɥɶ, ɨɧ ɡɚɦɟɧɹɟɬɫɹ ɧɨɜɵɦ.
dÖÒÕËÖÒÏ!
ȿɫɥɢ ɜɵ ɫɨɡɞɚɟɬɟ ɧɨɜɨɝɨ ɩɨɥɶɡɨɜɚɬɟɥɹ ɛɟɡ ɤɨɧɫɬɪɭɤɰɢɢ IDENTIFYED BY, ɬɨ ɩɨɥɶɡɨɜɚɬɟɥɶ ɧɟ
ɢɦɟɟɬ ɩɚɪɨɥɹ. ɗɬɨ ɧɟɛɟɡɨɩɚɫɧɨ.
ɉɚɪɨɥɢ ɬɚɤɠɟ ɦɨɝɭɬ ɭɫɬɚɧɚɜɥɢɜɚɬɶɫɹ ɨɩɟɪɚɬɨɪɨɦ SET PASSWORD. ɋɦ. ɪɚɡɞɟɥ 6.5.1.3.
ȿɫɥɢ ɜɵ ɧɟ ɯɨɬɢɬɟ ɨɬɩɪɚɜɥɹɬɶ ɩɚɪɨɥɶ ɜ ɜɢɞɟ ɨɬɤɪɵɬɨɝɨ ɬɟɤɫɬɚ, ɦɨɠɧɨ ɩɨɫɥɟ ɤɥɸɱɟɜɨɝɨ ɫɥɨɜɚ PASSWORD ɞɚɜɚɬɶ ɡɚɲɢɮɪɨɜɚɧɧɵɣ ɩɚɪɨɥɶ, ɤɨɬɨɪɵɣ ɜɨɡɜɪɚɳɚɟɬ SQL-ɮɭɧɤɰɢɹ
PASSWORD() ɢɥɢ ɮɭɧɤɰɢɹ C API make_scrambled_password().
288
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ȿɫɥɢ ɜɵɞɚɟɬɫɹ ɩɪɢɜɢɥɟɝɢɹ ɧɚ ɞɨɫɬɭɩ ɤ ɛɚɡɟ ɞɚɧɧɵɯ, ɡɚɩɢɫɶ ɜ mysql.user ɡɚɜɨɞɢɬɫɹ
ɩɪɢ ɧɟɨɛɯɨɞɢɦɨɫɬɢ. ȿɫɥɢ ɜɫɟ ɩɪɢɜɢɥɟɝɢɢ ɧɚ ɞɨɫɬɭɩ ɤ ɛɚɡɟ ɨɬɛɢɪɚɸɬɫɹ ɨɩɟɪɚɬɨɪɨɦ
REVOKE, ɷɬɚ ɡɚɩɢɫɶ ɭɞɚɥɹɟɬɫɹ.
ȿɫɥɢ ɭ ɩɨɥɶɡɨɜɚɬɟɥɹ ɧɟɬ ɩɪɢɜɢɥɟɝɢɣ ɧɚ ɞɨɫɬɭɩ ɤ ɬɚɛɥɢɰɟ, ɬɨ ɷɬɚ ɬɚɛɥɢɰɚ ɧɟ ɨɬɨɛɪɚɠɚɟɬɫɹ ɜ ɨɬɜɟɬ ɧɚ ɡɚɩɪɨɫ ɫɩɢɫɤɚ ɬɚɛɥɢɰ ɛɚɡɵ (ɧɚɩɪɢɦɟɪ, ɨɩɟɪɚɬɨɪɨɦ SHOW TABLES). ȿɫɥɢ
ɩɨɥɶɡɨɜɚɬɟɥɶ ɧɟ ɢɦɟɟɬ ɩɪɢɜɢɥɟɝɢɣ ɧɚ ɞɨɫɬɭɩ ɤ ɛɚɡɟ ɞɚɧɧɵɯ, ɢɦɹ ɛɚɡɵ ɧɟ ɨɬɨɛɪɚɠɚɟɬɫɹ ɜ
ɨɬɜɟɬ ɧɚ ɡɚɩɪɨɫ SHOW DATABASES, ɟɫɥɢ ɬɨɥɶɤɨ ɭ ɩɨɥɶɡɨɜɚɬɟɥɹ ɧɟɬ ɫɩɟɰɢɚɥɶɧɨɣ ɩɪɢɜɢɥɟɝɢɢ SHOW DATABASES.
Ʉɨɧɫɬɪɭɤɰɢɹ WITH GRANT OPTION ɞɚɟɬ ɩɨɥɶɡɨɜɚɬɟɥɸ ɜɨɡɦɨɠɧɨɫɬɶ ɫɚɦɨɦɭ ɜɵɞɚɜɚɬɶ
ɩɪɢɜɢɥɟɝɢɢ ɡɚɞɚɧɧɨɝɨ ɭɪɨɜɧɹ ɞɪɭɝɢɦ ɩɨɥɶɡɨɜɚɬɟɥɹɦ. ȼɵ ɞɨɥɠɧɵ ɛɵɬɶ ɨɫɬɨɪɨɠɧɵ ɫ ɬɟɦ,
ɤɨɦɭ ɜɵɞɚɟɬɟ ɩɪɢɜɢɥɟɝɢɢ ɫ GRANT OPTION, ɩɨɫɤɨɥɶɤɭ ɞɜɚ ɩɨɥɶɡɨɜɚɬɟɥɹ ɫ ɪɚɡɧɵɦɢ ɩɪɢɜɢɥɟɝɢɹɦɢ ɦɨɝɭɬ ɨɛɴɟɞɢɧɢɬɶ ɫɜɨɢ ɩɪɢɜɢɥɟɝɢɢ!
ȼɵ ɧɟ ɦɨɠɟɬɟ ɜɵɞɚɬɶ ɞɪɭɝɨɦɭ ɩɨɥɶɡɨɜɚɬɟɥɸ ɩɪɢɜɢɥɟɝɢɸ, ɤɨɬɨɪɨɣ ɧɟ ɢɦɟɟɬɟ ɫɚɦɢ.
GRANT OPTION ɩɨɡɜɨɥɹɟɬ ɜɵɞɚɜɚɬɶ ɬɨɥɶɤɨ ɬɟ ɩɪɢɜɢɥɟɝɢɢ, ɤɨɬɨɪɵɟ ɜɚɦ ɩɪɢɧɚɞɥɟɠɚɬ.
ɂɦɟɣɬɟ ɜ ɜɢɞɭ, ɱɬɨ ɤɨɝɞɚ ɜɵ ɞɚɟɬɟ ɩɨɥɶɡɨɜɚɬɟɥɸ ɩɪɢɜɢɥɟɝɢɸ GRANT OPTION ɧɚ ɨɩɪɟɞɟɥɟɧɧɨɦ ɭɪɨɜɧɟ ɩɪɢɜɢɥɟɝɢɣ, ɬɨ ɜɫɟ ɩɪɢɜɢɥɟɝɢɢ, ɩɪɢɧɚɞɥɟɠɚɳɢɟ ɟɦɭ ɧɚ ɷɬɨɦ ɭɪɨɜɧɟ (ɢ
ɬɟ, ɤɨɬɨɪɵɟ ɨɧ ɩɨɥɭɱɢɬ ɜ ɛɭɞɭɳɟɦ!), ɨɧ ɫɦɨɠɟɬ ɜɵɞɚɜɚɬɶ ɞɪɭɝɢɦ. ɉɪɟɞɩɨɥɨɠɢɦ, ɱɬɨ ɜɵ
ɞɚɟɬɟ ɩɨɥɶɡɨɜɚɬɟɥɸ ɩɪɢɜɢɥɟɝɢɸ INSERT ɜ ɛɚɡɟ ɞɚɧɧɵɯ. ȿɫɥɢ ɡɚɬɟɦ ɜɵ ɞɚɞɢɬɟ ɟɦɭ ɩɪɢɜɢɥɟɝɢɸ SELECT ɧɚ ɷɬɭ ɠɟ ɛɚɡɭ ɫ ɨɩɰɢɟɣ GRANT OPTION, ɬɨ ɨɧ ɫɦɨɠɟɬ ɜɵɞɚɜɚɬɶ ɞɪɭɝɢɦ ɧɟ
ɬɨɥɶɤɨ ɩɪɢɜɢɥɟɝɢɸ SELECT, ɧɨ ɢ INSERT. ȿɫɥɢ ɜɵ ɞɚɞɢɬɟ ɟɦɭ ɩɨɡɠɟ ɟɳɟ ɢ ɩɪɢɜɢɥɟɝɢɸ
UPDATE ɧɚ ɬɭ ɠɟ ɛɚɡɭ ɞɚɧɧɵɯ, ɬɨ ɨɧ ɫɦɨɠɟɬ ɪɚɡɞɚɜɚɬɶ INSERT, SELECT ɢ UPDATE.
ȼɵ ɧɟ ɞɨɥɠɧɵ ɞɚɜɚɬɶ ɩɪɢɜɢɥɟɝɢɣ ALTER ɨɛɵɱɧɨɦɭ ɩɨɥɶɡɨɜɚɬɟɥɸ. ȿɫɥɢ ɜɵ ɷɬɨ ɫɞɟɥɚɟɬɟ, ɨɧ ɫɦɨɠɟɬ ɩɨɬɨɦ ɪɚɡɪɭɲɢɬɶ ɫɢɫɬɟɦɭ, ɩɪɨɫɬɨ ɩɟɪɟɢɦɟɧɨɜɚɜ ɬɚɛɥɢɰɵ!
Ɉɩɰɢɢ MAX_QUERIES_PER_HOUR ɤɨɥɢɱɟɫɬɜɨ, MAX_UPDATES_PER_HOUR ɤɨɥɢɱɟɫɬɜɨ ɢ
MAX_CONNECTIONS_PER_HOUR ɤɨɥɢɱɟɫɬɜɨ – ɧɨɜɵɟ ɞɥɹ MySQL 4.0.2. Ɉɧɢ ɨɝɪɚɧɢɱɢɜɚɸɬ
ɤɨɥɢɱɟɫɬɜɨ ɡɚɩɪɨɫɨɜ, ɨɛɧɨɜɥɟɧɢɣ ɢ ɪɟɝɢɫɬɪɚɰɢɣ, ɤɨɬɨɪɵɟ ɩɨɥɶɡɨɜɚɬɟɥɶ ɦɨɠɟɬ ɜɵɩɨɥɧɢɬɶ ɜ ɬɟɱɟɧɢɟ ɱɚɫɚ. ȿɫɥɢ ɡɧɚɱɟɧɢɟ ɤɨɥɢɱɟɫɬɜɨ ɪɚɜɧɨ 0 (ɩɨ ɭɦɨɥɱɚɧɢɸ), ɷɬɨ ɨɡɧɚɱɚɟɬ,
ɱɬɨ ɨɝɪɚɧɢɱɟɧɢɣ ɞɥɹ ɞɚɧɧɨɝɨ ɩɨɥɶɡɨɜɚɬɟɥɹ ɧɟ ɧɚɤɥɚɞɵɜɚɟɬɫɹ.
oË ÑËÕÏÛµÂ!
ɑɬɨɛɵ ɭɤɚɡɚɬɶ ɥɸɛɭɸ ɢɡ ɷɬɢɯ ɨɩɰɢɣ ɞɥɹ ɩɨɥɶɡɨɜɚɬɟɥɹ, ɧɟ ɡɚɬɪɚɝɢɜɚɹ ɟɝɨ ɫɭɳɟɫɬɜɭɸɳɢɯ ɩɪɢɜɢɥɟɝɢɣ, ɩɪɢɦɟɧɹɣɬɟ GRANT USAGE ON *.* ... WITH MAX ...
MySQL ɦɨɠɟɬ ɩɪɨɜɟɪɹɬɶ ɚɬɪɢɛɭɬɵ ɫɟɪɬɢɮɢɤɚɬɚ X509 ɜ ɞɨɩɨɥɧɟɧɢɟ ɤ ɨɛɵɱɧɨɣ ɚɭɬɟɧɬɢɮɢɤɚɰɢɢ, ɨɫɧɨɜɚɧɧɨɣ ɧɚ ɢɦɟɧɢ ɩɨɥɶɡɨɜɚɬɟɥɹ ɢ ɩɚɪɨɥɟ. ɑɬɨɛɵ ɫɩɟɰɢɮɢɰɢɪɨɜɚɬɶ
SSL-ɨɩɰɢɢ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɨɣ ɭɱɟɬɧɨɣ ɡɚɩɢɫɢ MySQL, ɩɪɢɦɟɧɹɣɬɟ ɤɨɧɫɬɪɭɤɰɢɸ REQUIRE
ɨɩɟɪɚɬɨɪɚ GRANT. ɋɭɳɟɫɬɜɭɸɬ ɪɚɡɥɢɱɧɵɟ ɜɨɡɦɨɠɧɨɫɬɢ ɭɫɬɚɧɨɜɢɬɶ ɨɝɪɚɧɢɱɟɧɢɹ ɧɚ ɬɢɩɵ
ɩɨɞɤɥɸɱɟɧɢɣ ɩɨɥɶɡɨɜɚɬɟɥɹ:
„ȿɫɥɢ ɞɥɹ ɭɱɟɬɧɨɣ ɡɚɩɢɫɢ ɧɟ ɭɫɬɚɧɨɜɥɟɧɵ ɬɪɟɛɨɜɚɧɢɹ SSL ɢɥɢ X509, ɬɨ ɧɟɲɢɮɪɨɜɚɧɧɵɟ ɩɨɞɤɥɸɱɟɧɢɹ ɪɚɡɪɟɲɚɸɬɫɹ, ɟɫɥɢ ɭɤɚɡɚɧɵ ɩɪɚɜɢɥɶɧɨ ɢɦɹ ɢ ɩɚɪɨɥɶ. Ɉɞɧɚɤɨ
ɲɢɮɪɨɜɚɧɧɵɟ ɩɨɞɤɥɸɱɟɧɢɹ ɬɚɤɠɟ ɦɨɝɭɬ ɛɵɬɶ ɭɫɬɚɧɨɜɥɟɧɵ ɩɨɥɶɡɨɜɚɬɟɥɟɦ, ɟɫɥɢ
ɤɥɢɟɧɬ ɢɦɟɟɬ ɩɪɚɜɢɥɶɧɵɣ ɫɟɪɬɢɮɢɤɚɬ ɢ ɮɚɣɥɵ ɤɥɸɱɟɣ.
„Ɉɩɰɢɹ REQUIRE SSL ɫɨɨɛɳɚɟɬ ɫɟɪɜɟɪɭ, ɱɬɨ ɭɱɟɬɧɨɣ ɡɚɩɢɫɢ ɪɚɡɪɟɲɚɸɬɫɹ ɬɨɥɶɤɨ
ɲɢɮɪɨɜɚɧɧɵɟ SSL-ɩɨɞɤɥɸɱɟɧɢɹ. Ɉɬɦɟɬɢɦ, ɱɬɨ ɷɬɚ ɨɩɰɢɹ ɦɨɠɟɬ ɛɵɬɶ ɨɩɭɳɟɧɚ, ɟɫɥɢ ɟɫɬɶ ɤɚɤɢɟ-ɥɢɛɨ ɡɚɩɢɫɢ ɤɨɧɬɪɨɥɹ ɞɨɫɬɭɩɚ, ɪɚɡɪɟɲɚɸɳɢɟ ɧɟ SSL-ɩɨɞɤɥɸɱɟɧɢɹ.
mysql> GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost'
-> IDENTIFIED BY 'goodsecret' REQUIRE SSL;
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
289
„REQUIRE X509
ɨɡɧɚɱɚɟɬ, ɱɬɨ ɤɥɢɟɧɬ ɞɨɥɠɟɧ ɢɦɟɬɶ ɞɟɣɫɬɜɢɬɟɥɶɧɵɣ ɫɟɪɬɢɮɢɤɚɬ, ɧɨ
ɤɚɤɨɣ ɢɦɟɧɧɨ, ɤɟɦ ɢ ɤɨɦɭ ɜɵɞɚɧɧɵɣ – ɧɟ ɜɚɠɧɨ. ȿɞɢɧɫɬɜɟɧɧɨɟ ɬɪɟɛɨɜɚɧɢɟ, ɱɬɨɛɵ
ɦɨɠɧɨ ɛɵɥɨ ɫɜɟɪɢɬɶ ɟɝɨ ɫɢɝɧɚɬɭɪɭ ɨɞɧɢɦ ɢɡ ɫɟɪɬɢɮɢɤɚɬɨɜ CA.
mysql> GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost'
-> IDENTIFIED BY 'goodsecret' REQUIRE X509;
„REQUIRE ISSUER 'ɜɵɞɚɸɳɢɣ'
ɧɚɤɥɚɞɵɜɚɟɬ ɨɝɪɚɧɢɱɟɧɢɟ ɧɚ ɩɨɩɵɬɤɢ ɩɨɞɤɥɸɱɟɧɢɣ,
ɤɨɬɨɪɨɟ ɫɨɫɬɨɢɬ ɜ ɬɨɦ, ɱɬɨ ɤɥɢɟɧɬ ɞɨɥɠɟɧ ɩɪɟɞɫɬɚɜɢɬɶ ɞɟɣɫɬɜɢɬɟɥɶɧɵɣ ɫɟɪɬɢɮɢɤɚɬ X509, ɜɵɞɚɧɧɵɣ CA 'ɜɵɞɚɸɳɢɣ'. ȿɫɥɢ ɤɥɢɟɧɬ ɩɪɟɞɫɬɚɜɥɹɟɬ ɞɟɣɫɬɜɢɬɟɥɶɧɵɣ
ɫɟɪɬɢɮɢɤɚɬ, ɧɨ ɜɵɞɚɧɧɵɣ ɤɟɦ-ɬɨ ɞɪɭɝɢɦ, ɫɟɪɜɟɪ ɨɬɜɟɪɝɚɟɬ ɩɨɞɤɥɸɱɟɧɢɟ. ɉɪɢɦɟɧɟɧɢɟ ɫɟɪɬɢɮɢɤɚɬɚ X509 ɜɫɟɝɞɚ ɩɨɞɪɚɡɭɦɟɜɚɟɬ ɲɢɮɪɨɜɚɧɢɟ, ɩɨɷɬɨɦɭ ɨɩɰɢɹ SSL
ɧɟɨɛɹɡɚɬɟɥɶɧɚ.
mysql> GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost'
-> IDENTIFIED BY 'goodsecret'
-> REQUIRE ISSUER '/C=FI/ST=Some-State/L=Helsinki/
O=MySQL Finland AB/CN=Tonu Samuel/[email protected]';
Ɉɬɦɟɬɢɦ, ɱɬɨ ɡɧɚɱɟɧɢɟ ISSUER ɞɨɥɠɧɨ ɛɵɬɶ ɩɪɟɞɫɬɚɜɥɟɧɨ ɤɚɤ ɨɞɧɚ ɫɬɪɨɤɚ.
„REQUIRE SUBJECT 'ɫɭɛɴɟɤɬ'
ɧɚɤɥɚɞɵɜɚɟɬ ɨɝɪɚɧɢɱɟɧɢɟ ɧɚ ɩɨɩɵɬɤɢ ɩɨɞɤɥɸɱɟɧɢɣ,
ɤɨɬɨɪɨɟ ɫɨɫɬɨɢɬ ɜ ɬɨɦ, ɱɬɨ ɤɥɢɟɧɬ ɞɨɥɠɟɧ ɩɪɟɞɴɹɜɢɬɶ ɞɟɣɫɬɜɢɬɟɥɶɧɵɣ ɫɟɪɬɢɮɢɤɚɬ
X509 ɫ ɭɤɚɡɚɧɢɟɦ ɫɭɛɴɟɤɬɚ 'ɫɭɛɴɟɤɬ' ɜ ɧɟɦ. ȿɫɥɢ ɤɥɢɟɧɬ ɩɪɟɞɫɬɚɜɥɹɟɬ ɞɟɣɫɬɜɢɬɟɥɶɧɵɣ ɫɟɪɬɢɮɢɤɚɬ, ɧɨ ɫ ɞɪɭɝɢɦ ɫɭɛɴɟɤɬɨɦ ɜ ɧɟɦ, ɫɟɪɜɟɪ ɨɬɜɟɪɝɚɟɬ ɩɨɞɤɥɸɱɟɧɢɟ.
mysql> GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost'
-> IDENTIFIED BY 'goodsecret'
-> REQUIRE SUBJECT '/C=EE/ST=Some-State/L=Tallinn/
O=MySQL demo client certificate/
CN=Tonu Samuel/[email protected]';
Ɉɬɦɟɬɢɦ, ɱɬɨ ɡɧɚɱɟɧɢɟ SUBJECT ɞɨɥɠɧɨ ɛɵɬɶ ɩɪɟɞɫɬɚɜɥɟɧɨ ɤɚɤ ɨɞɧɚ ɫɬɪɨɤɚ.
„REQUIRE CIPHER 'ɲɢɮɪ'
ɧɟɨɛɯɨɞɢɦɨ ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɝɚɪɚɧɬɢɪɨɜɚɬɶ, ɱɬɨ ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɞɨɫɬɚɬɨɱɧɨ ɫɬɪɨɝɢɣ ɲɢɮɪ ɢ ɞɥɢɧɚ ɤɥɸɱɚ. SSL ɫɚɦ ɩɨ ɫɟɛɟ ɦɨɠɟɬ
ɛɵɬɶ ɨɫɥɚɛɥɟɧ, ɟɫɥɢ ɩɪɢɦɟɧɹɸɬɫɹ ɫɬɚɪɵɟ ɚɥɝɨɪɢɬɦɵ ɫ ɤɨɪɨɬɤɢɦɢ ɤɥɸɱɚɦɢ ɲɢɮɪɨɜɚɧɢɹ. ɂɫɩɨɥɶɡɭɹ ɷɬɭ ɨɩɰɢɸ, ɜɵ ɦɨɠɟɬɟ ɡɚɬɪɟɛɨɜɚɬɶ ɧɟɤɨɬɨɪɵɣ ɤɨɧɤɪɟɬɧɵɣ
ɦɟɬɨɞ ɲɢɮɪɨɜɚɧɢɹ, ɱɬɨɛɵ ɪɚɡɪɟɲɢɬɶ ɫɨɟɞɢɧɟɧɢɟ.
mysql> GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost'
-> IDENTIFIED BY 'goodsecret'
-> REQUIRE CIPHER 'EDH-RSA-DES-CBC3-SHA';
Ɉɩɰɢɢ SUBJECT, ISSUER ɢ CIPHER ɦɨɝɭɬ ɤɨɦɛɢɧɢɪɨɜɚɬɶɫɹ ɜ ɤɨɧɫɬɪɭɤɰɢɢ REQUIRE:
mysql> GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost'
-> IDENTIFIED BY 'goodsecret'
-> REQUIRE SUBJECT '/C=EE/ST=Some-State/L=Tallinn/
O=MySQL demo client certificate/
CN=Tonu Samuel/[email protected]'
-> AND ISSUER '/C=FI/ST=Some-State/L=Helsinki/
O=MySQL Finland AB/CN=Tonu Samuel/[email protected]'
-> AND CIPHER 'EDH-RSA-DES-CBC3-SHA';
ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɡɧɚɱɟɧɢɹ SUBJECT ɢ ISSUER ɞɨɥɠɧɵ ɛɵɬɶ ɩɪɟɞɫɬɚɜɥɟɧɵ ɤɚɤ
ɨɞɧɚ ɫɬɪɨɤɚ.
290
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɇɚɱɢɧɚɹ ɫ MySQL 4.0.4, ɞɨɩɭɫɤɚɟɬɫɹ ɧɟɨɛɹɡɚɬɟɥɶɧɨɟ ɫɥɨɜɨ AND ɦɟɠɞɭ ɨɩɰɢɹɦɢ
REQUIRE.
ɉɨɫɥɟɞɨɜɚɬɟɥɶɧɨɫɬɶ ɨɩɰɢɣ ɧɟ ɢɦɟɟɬ ɡɧɚɱɟɧɢɹ, ɧɨ ɧɢ ɨɞɧɚ ɧɟ ɞɨɥɠɧɚ ɩɨɜɬɨɪɹɬɶɫɹ.
ɉɪɢ ɡɚɩɭɫɤɟ mysqld ɜɫɟ ɩɪɢɜɢɥɟɝɢɢ ɫɱɢɬɵɜɚɸɬɫɹ ɜ ɩɚɦɹɬɶ. ɉɪɢɜɢɥɟɝɢɢ ɧɚ ɛɚɡɵ ɞɚɧɧɵɯ, ɬɚɛɥɢɰɵ ɢ ɫɬɨɥɛɰɵ ɜɫɬɭɩɚɸɬ ɜ ɞɟɣɫɬɜɢɟ ɫɪɚɡɭ, ɚ ɩɪɢɜɢɥɟɝɢɢ ɭɪɨɜɧɹ ɩɨɥɶɡɨɜɚɬɟɥɹ –
ɩɪɢ ɫɥɟɞɭɸɳɟɦ ɟɝɨ ɩɨɞɤɥɸɱɟɧɢɢ. Ɉ ɦɨɞɢɮɢɤɚɰɢɹɯ ɜ ɬɚɛɥɢɰɚɯ ɩɪɢɜɢɥɟɝɢɣ, ɜɵɩɨɥɧɹɟɦɵɯ ɨɩɟɪɚɬɨɪɚɦɢ GRANT ɢ REVOKE, ɫɟɪɜɟɪ ɢɡɜɟɳɚɟɬɫɹ ɧɟɦɟɞɥɟɧɧɨ. ȿɫɥɢ ɜɵ ɦɨɞɢɮɢɰɢɪɭɟɬɟ ɬɚɛɥɢɰɵ ɩɪɢɜɢɥɟɝɢɣ ɜɪɭɱɧɭɸ (ɩɪɢɦɟɧɹɹ INSERT, UPDATE ɢ ɬɚɤ ɞɚɥɟɟ), ɜɵ ɞɨɥɠɧɵ
ɜɵɩɨɥɧɢɬɶ ɨɩɟɪɚɬɨɪ FLUSH PRIVILEGES ɢɥɢ ɡɚɩɭɫɬɢɬɶ mysqladmin flush-privileges,
ɱɬɨɛɵ ɫɨɨɛɳɢɬɶ ɫɟɪɜɟɪɭ ɨ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɩɟɪɟɡɚɝɪɭɡɤɢ ɬɚɛɥɢɰ ɩɪɢɜɢɥɟɝɢɣ ɜ ɩɚɦɹɬɶ.
Ɂɚɦɟɬɶɬɟ, ɱɬɨ ɟɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ɩɪɢɜɢɥɟɝɢɢ ɧɚ ɬɚɛɥɢɰɵ ɢ ɫɬɨɥɛɰɵ ɜɫɟɝɨ ɞɥɹ ɨɞɧɨɝɨ ɩɨɥɶɡɨɜɚɬɟɥɹ, ɫɟɪɜɟɪ ɩɪɨɜɟɪɹɟɬ ɷɬɢ ɩɪɢɜɢɥɟɝɢɢ ɞɥɹ ɜɫɟɯ ɩɨɥɶɡɨɜɚɬɟɥɟɣ ɢ ɷɬɨ ɧɟɫɤɨɥɶɤɨ ɡɚɦɟɞɥɹɟɬ ɪɚɛɨɬɭ MySQL. Ⱥɧɚɥɨɝɢɱɧɨ, ɟɫɥɢ ɜɵ ɨɝɪɚɧɢɱɢɜɚɟɬɟ ɤɨɥɢɱɟɫɬɜɨ ɡɚɩɪɨɫɨɜ, ɨɛɧɨɜɥɟɧɢɣ ɢɥɢ ɩɨɞɤɥɸɱɟɧɢɣ ɞɥɹ ɥɸɛɵɯ ɩɨɥɶɡɨɜɚɬɟɥɟɣ, ɫɟɪɜɟɪ ɞɨɥɠɟɧ ɜɟɫɬɢ
ɦɨɧɢɬɨɪɢɧɝ ɷɬɢɯ ɡɧɚɱɟɧɢɣ.
ɇɚɢɛɨɥɶɲɢɟ ɪɚɡɥɢɱɢɹ ɦɟɠɞɭ ɫɬɚɧɞɚɪɬɨɦ SQL ɢ ɜɟɪɫɢɟɣ GRANT ɨɬ MySQL:
„ȼ MySQL ɩɪɢɜɢɥɟɝɢɢ ɚɫɫɨɰɢɢɪɭɸɬɫɹ ɫ ɤɨɦɛɢɧɚɰɢɟɣ ɢɦɹ ɩɨɥɶɡɨɜɚɬɟɥɹ/ɢɦɹ ɯɨɫɬɚ,
ɚ ɧɟ ɬɨɥɶɤɨ ɫ ɨɞɧɢɦ ɢɦɟɧɟɦ ɩɨɥɶɡɨɜɚɬɟɥɹ.
„ɋɬɚɧɞɚɪɬ SQL ɧɟ ɩɪɟɞɭɫɦɚɬɪɢɜɚɟɬ ɩɪɢɜɢɥɟɝɢɣ ɝɥɨɛɚɥɶɧɨɝɨ ɭɪɨɜɧɹ ɢ ɭɪɨɜɧɹ ɛɚɡɵ
ɞɚɧɧɵɯ, ɤɚɤ ɢ ɬɢɩɨɜ ɩɪɢɜɢɥɟɝɢɣ, ɩɨɞɞɟɪɠɢɜɚɟɦɵɯ MySQL.
„MySQL ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬ ɫɬɚɧɞɚɪɬɧɵɯ SQL-ɩɪɢɜɢɥɟɝɢɣ TRIGGER ɢ UNDER.
„ɋɬɚɧɞɚɪɬɧɵɟ ɩɪɢɜɢɥɟɝɢɢ SQL ɨɪɝɚɧɢɡɨɜɚɧɵ ɜ ɢɟɪɚɪɯɢɱɟɫɤɨɣ ɦɚɧɟɪɟ. ȿɫɥɢ ɜɵ
ɭɞɚɥɹɟɬɟ ɩɨɥɶɡɨɜɚɬɟɥɹ, ɜɫɟ ɟɝɨ ɩɪɢɜɢɥɟɝɢɢ ɢɡɵɦɚɸɬɫɹ. ȼ MySQL ɜɵɞɚɧɧɵɟ ɩɪɢɜɢɥɟɝɢɢ ɧɟ ɨɬɧɢɦɚɸɬɫɹ ɚɜɬɨɦɚɬɢɱɟɫɤɢ, ɜɵ ɞɨɥɠɧɵ ɷɬɨ ɞɟɥɚɬɶ ɫɚɦɢ.
„ȼ ɫɬɚɧɞɚɪɬɧɨɦ SQL, ɤɨɝɞɚ ɭɞɚɥɹɟɬɫɹ ɬɚɛɥɢɰɚ, ɜɫɟ ɩɪɢɜɢɥɟɝɢɢ ɧɚ ɧɟɟ ɨɬɧɢɦɚɸɬɫɹ.
ȼ ɫɬɚɧɞɚɪɬɧɨɦ SQL, ɤɨɝɞɚ ɨɬɧɢɦɚɟɬɫɹ ɩɪɢɜɢɥɟɝɢɹ, ɜɫɟ ɩɪɢɜɢɥɟɝɢɢ, ɜɵɞɚɧɧɵɟ ɧɚ
ɨɫɧɨɜɟ ɞɚɧɧɨɣ ɩɪɢɜɢɥɟɝɢɢ, ɬɚɤɠɟ ɨɬɧɢɦɚɸɬɫɹ. ȼ MySQL ɩɪɢɜɢɥɟɝɢɢ ɦɨɝɭɬ ɛɵɬɶ
ɨɬɧɹɬɵ ɬɨɥɶɤɨ ɹɜɧɵɦ ɜɵɡɨɜɨɦ ɨɩɟɪɚɬɨɪɚ REVOKE, ɥɢɛɨ ɦɚɧɢɩɭɥɹɰɢɹɦɢ ɜ ɬɚɛɥɢɰɚɯ
ɩɪɢɜɢɥɟɝɢɣ.
„ȼ MySQL, ɟɫɥɢ ɜɵ ɢɦɟɟɬɟ ɩɪɢɜɢɥɟɝɢɸ INSERT ɬɨɥɶɤɨ ɧɚ ɧɟɤɨɬɨɪɵɟ ɫɬɨɥɛɰɵ ɬɚɛɥɢɰɵ, ɜɵ ɦɨɠɟɬɟ ɜɵɩɨɥɧɹɬɶ ɨɩɟɪɚɬɨɪ INSERT ɜ ɷɬɨɣ ɬɚɛɥɢɰɟ. ɋɬɨɥɛɰɚɦ, ɞɥɹ ɤɨɬɨɪɵɯ ɭ ɜɚɫ ɧɟɬ ɩɪɢɜɢɥɟɝɢɢ INSERT, ɛɭɞɭɬ ɩɪɢɫɜɨɟɧɵ ɡɧɚɱɟɧɢɹ ɩɨ ɭɦɨɥɱɚɧɢɸ.
ɋɬɚɧɞɚɪɬ SQL ɬɪɟɛɭɟɬ, ɱɬɨɛɵ ɜɵ ɢɦɟɥɢ ɩɪɢɜɢɥɟɝɢɸ INSERT ɧɚ ɜɫɟ ɫɬɨɥɛɰɵ.
6.5.1.3. Ñèíòàêñèñ SET PASSWORD
SET PASSWORD = PASSWORD('ɩɚɪɨɥɶ')
SET PASSWORD FOR ɩɨɥɶɡɨɜɚɬɟɥɶ = PASSWORD('ɩɚɪɨɥɶ')
Ɉɩɟɪɚɬɨɪ SET PASSWORD ɧɚɡɧɚɱɚɟɬ ɩɚɪɨɥɶ ɫɭɳɟɫɬɜɭɸɳɟɣ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɨɣ ɭɱɟɬɧɨɣ
ɡɚɩɢɫɢ MySQL.
ɉɟɪɜɵɣ ɫɢɧɬɚɤɫɢɫ ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɩɚɪɨɥɶ ɬɟɤɭɳɟɦɭ ɩɨɥɶɡɨɜɚɬɟɥɸ. Ʌɸɛɨɣ ɤɥɢɟɧɬ,
ɩɨɞɤɥɸɱɟɧɧɵɣ ɤ ɫɟɪɜɟɪɭ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɧɟɚɧɨɧɢɦɧɨɣ ɭɱɟɬɧɨɣ ɡɚɩɢɫɢ, ɦɨɠɟɬ ɢɡɦɟɧɹɬɶ ɫɜɨɣ ɩɚɪɨɥɶ.
ȼɬɨɪɨɣ ɫɢɧɬɚɤɫɢɫ ɩɪɢɫɜɚɢɜɚɟɬ ɩɚɪɨɥɶ ɭɤɚɡɚɧɧɨɣ ɭɱɟɬɧɨɣ ɡɚɩɢɫɢ ɧɚ ɬɟɤɭɳɟɦ ɯɨɫɬɟ
ɫɟɪɜɟɪɚ. Ɍɨɥɶɤɨ ɤɥɢɟɧɬɵ, ɢɦɟɸɳɢɟ ɞɨɫɬɭɩ ɤ ɛɚɡɟ ɞɚɧɧɵɯ mysql, ɦɨɝɭɬ ɷɬɨ ɞɟɥɚɬɶ. Ɂɧɚɱɟɧɢɟ ɩɨɥɶɡɨɜɚɬɟɥɶ ɞɨɥɠɧɨ ɛɵɬɶ ɡɚɞɚɧɨ ɜ ɮɨɪɦɚɬɟ ɢɦɹ_ɩɨɥɶɡɨɜɚɬɟɥɹ@ɢɦɹ_ɯɨɫɬɚ, ɝɞɟ
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
291
ɢɦɹ_ɩɨɥɶɡɨɜɚɬɟɥɹ ɢ ɢɦɹ_ɯɨɫɬɚ – ɬɨɱɧɨ ɬɚɤɢɟ ɠɟ, ɤɨɬɨɪɵɟ ɭɤɚɡɚɧɵ ɜ ɫɬɨɥɛɰɚɯ User ɢ
Host ɬɚɛɥɢɰɵ mysql.user. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɭ ɜɚɫ ɟɫɬɶ ɫɬɪɨɤɚ ɫɨ ɡɧɚɱɟɧɢɹɦɢ User ɢ Host,
ɪɚɜɧɵɦɢ, ɫɨɨɬɜɟɬɫɬɜɟɧɧɨ, 'bob' ɢ '%.loc.gov', ɜɵ ɞɨɥɠɧɵ ɧɚɩɢɫɚɬɶ ɨɩɟɪɚɬɨɪ ɫɥɟɞɭɸ-
ɳɢɦ ɨɛɪɚɡɨɦ:
mysql> SET PASSWORD FOR 'bob'@'%.loc.gov' = PASSWORD('newpass');
ɗɬɨ ɷɤɜɢɜɚɥɟɧɬɧɨ ɬɚɤɨɦɭ ɜɚɪɢɚɧɬɭ:
mysql> UPDATE mysql.user SET Password=PASSWORD('newpass')
-> WHERE User='bob' AND Host='%.loc.gov';
mysql> FLUSH PRIVILEGES;
6.5.2. Îïåðàòîðû îáñëóæèâàíèÿ òàáëèö
6.5.2.1. Ñèíòàêñèñ ANALYZE TABLE
ANALYZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE ɢɦɹ_ɬɚɛɥɢɰɵ [, ɢɦɹ_ɬɚɛɥɢɰɵ] ...
ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɚɧɚɥɢɡɢɪɭɟɬ ɢ ɫɨɯɪɚɧɹɟɬ ɪɚɫɩɪɟɞɟɥɟɧɢɟ ɤɥɸɱɟɣ ɞɥɹ ɬɚɛɥɢɰɵ. ȼɨ ɜɪɟɦɹ ɚɧɚɥɢɡɚ ɬɚɛɥɢɰɚ ɛɥɨɤɢɪɭɟɬɫɹ ɩɨ ɱɬɟɧɢɸ. ɗɬɨ ɪɚɛɨɬɚɟɬ ɞɥɹ ɬɚɛɥɢɰ MyISAM ɢ BDB, ɚ
ɬɚɤɠɟ (ɧɚɱɢɧɚɹ ɫ MySQL 4.0.13) InnoDB. Ⱦɥɹ ɬɚɛɥɢɰ MyISAM ɞɚɧɧɵɣ ɨɩɟɪɚɬɨɪ ɷɤɜɢɜɚɥɟɧɬɟɧ myisamchk -a.
MySQL ɢɫɩɨɥɶɡɭɟɬ ɫɨɯɪɚɧɟɧɧɵɟ ɞɚɧɧɵɟ ɨ ɪɚɫɩɪɟɞɟɥɟɧɢɢ ɤɥɸɱɟɣ, ɱɬɨɛɵ ɩɪɢɧɢɦɚɬɶ
ɪɟɲɟɧɢɹ ɨɬɧɨɫɢɬɟɥɶɧɨ ɩɨɪɹɞɤɚ ɨɛɴɟɞɢɧɟɧɢɹ ɬɚɛɥɢɰ, ɤɨɝɞɚ ɜ ɡɚɩɪɨɫɟ ɬɪɟɛɭɟɬɫɹ ɨɛɴɟɞɢɧɟɧɢɟ ɩɨ ɧɟɤɨɧɫɬɚɧɬɧɵɦ ɡɧɚɱɟɧɢɹɦ.
Ɉɩɟɪɚɬɨɪ ɜɨɡɜɪɚɳɚɟɬ ɬɚɛɥɢɰɭ ɫɨ ɫɥɟɞɭɸɳɢɦɢ ɫɬɨɥɛɰɚɦɢ:
ɋɬɨɥɛɟɰ
Ɂɧɚɱɟɧɢɟ
Table
Op
Msg_type
Msg_text
ɂɦɹ ɬɚɛɥɢɰɵ
ȼɫɟɝɞɚ Analyze
Ɍɢɩ: status, error, info ɢɥɢ warning
ɋɨɨɛɳɟɧɢɟ
ȼɵ ɦɨɠɟɬɟ ɩɪɨɜɟɪɢɬɶ ɫɨɯɪɚɧɟɧɧɨɟ ɪɚɫɩɪɟɞɟɥɟɧɢɟ ɤɥɸɱɟɣ ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ SHOW
INDEX. ɋɦ. ɪɚɡɞɟɥ 6.5.3.7.
ȿɫɥɢ ɬɚɛɥɢɰɚ ɧɟ ɢɡɦɟɧɹɥɚɫɶ ɫ ɩɨɫɥɟɞɧɟɝɨ ɜɵɩɨɥɧɟɧɢɹ ANALYZE TABLE, ɟɟ ɚɧɚɥɢɡ ɜɵɩɨɥɧɹɬɶɫɹ ɧɟ ɛɭɞɟɬ.
Ⱦɨ MySQL 4.1.1 ɨɩɟɪɚɬɨɪ ANALYZE TABLE ɧɟ ɡɚɩɢɫɵɜɚɥɫɹ ɜ ɛɢɧɚɪɧɵɣ ɠɭɪɧɚɥ. ɇɚɱɢɧɚɹ ɫ MySQL 4.1.1, ɨɧ ɡɚɩɢɫɵɜɚɟɬɫɹ, ɟɫɥɢ ɬɨɥɶɤɨ ɧɟ ɛɵɥɨ ɭɤɚɡɚɧɨ ɧɟɨɛɹɡɚɬɟɥɶɧɨɟ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ NO_WRITE_TO_BINLOG (ɢɥɢ ɟɝɨ ɩɫɟɜɞɨɧɢɦ LOCAL).
6.5.2.2. Ñèíòàêñèñ BACKUP TABLE
BACKUP TABLE ɢɦɹ_ɬɚɛɥɢɰɵ [, ɢɦɹ_ɬɚɛɥɢɰɵ] ...
TO '/ɩɭɬɶ/ɤ/ɤɚɬɚɥɨɝɭ/ɪɟɡɟɪɜɧɵɯ_ɤɨɩɢɣ'
oË ÑËÕÏÛµÂ!
ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɭɫɬɚɪɟɥ. Ɇɵ ɪɚɛɨɬɚɟɦ ɧɚɞ ɟɝɨ ɭɥɭɱɲɟɧɧɨɣ ɡɚɦɟɧɨɣ, ɤɨɬɨɪɚɹ ɩɨɡɜɨɥɢɬ ɜɵɩɨɥɧɹɬɶ
ɝɨɪɹɱɟɟ ɪɟɡɟɪɜɧɨɟ ɤɨɩɢɪɨɜɚɧɢɟ. Ⱥ ɩɨɤɚ ɜɦɟɫɬɨ ɷɬɨɝɨ ɦɨɠɧɨ ɩɨɥɶɡɨɜɚɬɶɫɹ ɫɰɟɧɚɪɢɟɦ
mysqlhotcopy.
BACKUP TABLE ɤɨɩɢɪɭɟɬ ɜ ɪɟɡɟɪɜɧɵɣ ɤɚɬɚɥɨɝ ɦɢɧɢɦɚɥɶɧɵɣ ɧɚɛɨɪ ɮɚɣɥɨɜ ɬɚɛɥɢɰɵ,
ɧɟɨɛɯɨɞɢɦɵɣ ɞɥɹ ɟɟ ɩɨɫɥɟɞɭɸɳɟɝɨ ɜɨɫɫɬɚɧɨɜɥɟɧɢɹ, ɩɨɫɥɟ ɫɛɪɨɫɚ ɜɫɟɯ ɛɭɮɟɪɢɡɨɜɚɧɧɵɯ
292
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɢɡɦɟɧɟɧɢɣ ɧɚ ɞɢɫɤ. ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɪɚɛɨɬɚɟɬ ɬɨɥɶɤɨ ɫ ɬɚɛɥɢɰɚɦɢ MyISAM. Ɉɧ ɤɨɩɢɪɭɟɬ
ɮɚɣɥɵ ɨɩɪɟɞɟɥɟɧɢɹ ɬɚɛɥɢɰ .frm ɢ ɮɚɣɥɵ ɞɚɧɧɵɯ .MYD. ɂɧɞɟɤɫɧɵɟ ɮɚɣɥɵ .MYI ɦɨɝɭɬ
ɛɵɬɶ ɩɟɪɟɫɬɪɨɟɧɵ ɧɚ ɨɫɧɨɜɟ ɩɟɪɜɵɯ ɞɜɭɯ. Ʉɚɬɚɥɨɝ ɞɨɥɠɟɧ ɛɵɬɶ ɭɤɚɡɚɧ ɫ ɩɨɥɧɵɦ ɩɭɬɟɦ.
ɉɪɢ ɪɟɡɟɪɜɧɨɦ ɤɨɩɢɪɨɜɚɧɢɢ ɤɚɠɞɚɹ ɬɚɛɥɢɰɚ ɩɨɫɥɟɞɨɜɚɬɟɥɶɧɨ ɛɥɨɤɢɪɭɟɬɫɹ ɩɨ ɱɬɟɧɢɸ. ȿɫɥɢ ɜɵ ɯɨɬɢɬɟ ɫɤɨɩɢɪɨɜɚɬɶ ɧɟɫɤɨɥɶɤɨ ɬɚɛɥɢɰ ɜ ɜɢɞɟ ɫɧɢɦɤɚ (ɩɪɟɞɨɬɜɪɚɳɚɹ ɢɡɦɟɧɟɧɢɹ ɜ ɧɢɯ ɜɨ ɜɪɟɦɹ ɪɟɡɟɪɜɢɪɨɜɚɧɢɹ), ɜɵ ɞɨɥɠɧɵ ɫɧɚɱɚɥɚ ɜɵɩɨɥɧɢɬɶ ɨɩɟɪɚɬɨɪ LOCK
TABLES, ɱɬɨɛɵ ɭɫɬɚɧɨɜɢɬɶ ɛɥɨɤɢɪɨɜɤɭ ɩɨ ɱɬɟɧɢɸ ɞɥɹ ɜɫɟɯ ɬɚɛɥɢɰ ɝɪɭɩɩɵ.
Ɉɩɟɪɚɬɨɪ ɜɨɡɜɪɚɳɚɟɬ ɬɚɛɥɢɰɭ ɫɨ ɫɥɟɞɭɸɳɢɦɢ ɫɬɨɥɛɰɚɦɢ:
ɋɬɨɥɛɟɰ
Ɂɧɚɱɟɧɢɟ
Table
ɂɦɹ ɬɚɛɥɢɰɵ
Op
ȼɫɟɝɞɚ Backup
Msg_type
Ɍɢɩ: status, error, info ɢɥɢ warning
Msg_text
ɋɨɨɛɳɟɧɢɟ
Ɉɩɟɪɚɬɨɪ BACKUP TABLE ɞɨɫɬɭɩɟɧ ɜ MySQL 3.23.25 ɢ ɩɨɫɥɟɞɭɸɳɢɯ ɜɟɪɫɢɹɯ.
6.5.2.3. Ñèíòàêñèñ CHECK TABLE
CHECK TABLE ɢɦɹ_ɬɚɛɥɢɰɵ [, ɢɦɹ_ɬɚɛɥɢɰɵ] ... [ɨɩɰɢɹ] ...
ɨɩɰɢɹ = {QUICK | FAST | MEDIUM | EXTENDED | CHANGED}
CHECK TABLE ɪɚɛɨɬɚɟɬ ɬɨɥɶɤɨ ɫ ɬɚɛɥɢɰɚɦɢ MyISAM ɢ InnoDB. Ⱦɥɹ ɬɚɛɥɢɰ MyISAM ɷɬɨ ɬɨ
ɠɟ ɫɚɦɨɟ, ɱɬɨ ɢ ɡɚɩɭɫɤ myisamchk --medium-check ɢɦɹ_ɬɚɛɥɢɰɵ.
ȿɫɥɢ ɧɢɤɚɤɚɹ ɨɩɰɢɹ ɧɟ ɭɤɚɡɚɧɚ, ɩɪɢɧɢɦɚɟɬɫɹ MEDIUM.
ɉɪɨɜɟɪɹɟɬ ɬɚɛɥɢɰɭ ɢɥɢ ɝɪɭɩɩɭ ɬɚɛɥɢɰ ɧɚ ɧɚɥɢɱɢɟ ɨɲɢɛɨɤ. Ⱦɥɹ ɬɚɛɥɢɰ MyISAM ɨɛɧɨɜɥɹɟɬɫɹ ɫɬɚɬɢɫɬɢɱɟɫɤɚɹ ɢɧɮɨɪɦɚɰɢɹ ɨ ɤɥɸɱɚɯ. Ɉɩɟɪɚɬɨɪ ɜɨɡɜɪɚɳɚɟɬ ɬɚɛɥɢɰɭ ɫɨ ɫɥɟɞɭɸɳɢɦɢ ɫɬɨɥɛɰɚɦɢ:
ɋɬɨɥɛɟɰ
Ɂɧɚɱɟɧɢɟ
Table
ɂɦɹ ɬɚɛɥɢɰɵ
Op
ȼɫɟɝɞɚ Check
Msg_type
Ɍɢɩ: status, error, info ɢɥɢ warning
Msg_text
ɋɨɨɛɳɟɧɢɟ
ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɨɩɟɪɚɬɨɪ ɦɨɠɟɬ ɜɵɞɚɬɶ ɦɧɨɠɟɫɬɜɨ ɫɬɪɨɤ ɢɧɮɨɪɦɚɰɢɢ ɨ ɤɚɠɞɨɣ ɩɪɨɜɟɪɹɟɦɨɣ ɬɚɛɥɢɰɟ. ɉɨɫɥɟɞɧɹɹ ɫɬɪɨɤɚ ɛɭɞɟɬ ɫɨɞɟɪɠɚɬɶ ɡɧɚɱɟɧɢɟ Msg_type, ɪɚɜɧɨɟ
status ɢ Msg_text ɨɛɵɱɧɨ ɞɨɥɠɧɨ ɛɵɬɶ OK. ȿɫɥɢ ɜɵ ɧɟ ɩɨɥɭɱɚɟɬɟ OK ɢɥɢ ɠɟ Table is
already up to date (Ɍɚɛɥɢɰɚ ɭɠɟ ɨɛɧɨɜɥɟɧɚ), ɨɛɵɱɧɨ ɜɵ ɞɨɥɠɧɵ ɛɭɞɟɬɟ ɡɚɩɭɫɬɢɬɶ ɜɨɫɫɬɚɧɨɜɥɟɧɢɟ ɬɚɛɥɢɰɵ. ɋɨɨɛɳɟɧɢɟ Table is already up to date ɨɡɧɚɱɚɟɬ, ɱɬɨ ɦɟɯɚɧɢɡɦ
ɯɪɚɧɟɧɢɹ ɫɨɨɛɳɚɟɬ ɨ ɬɨɦ, ɱɬɨ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɩɪɨɜɟɪɹɬɶ ɬɚɛɥɢɰɭ ɧɟ ɛɵɥɨ.
ɋɭɳɟɫɬɜɭɸɬ ɫɥɟɞɭɸɳɢɟ ɬɢɩɵ ɩɪɨɜɟɪɨɤ:
Ɍɢɩ
Ɂɧɚɱɟɧɢɟ
QUICK
ɇɟ ɫɤɚɧɢɪɨɜɚɬɶ ɫɬɪɨɤɢ ɜ ɩɨɢɫɤɚɯ ɧɟɤɨɪɪɟɤɬɧɵɯ ɫɜɹɡɟɣ.
FAST
ɉɪɨɜɟɪɹɬɶ ɬɨɥɶɤɨ ɬɟ ɬɚɛɥɢɰɵ, ɤɨɬɨɪɵɟ ɧɟ ɛɵɥɢ ɤɨɪɪɟɤɬɧɨ ɡɚɤɪɵɬɵ.
CHANGED
ɉɪɨɜɟɪɹɬɶ ɬɨɥɶɤɨ ɬɟ ɬɚɛɥɢɰɵ, ɤɨɬɨɪɵɟ ɢɡɦɟɧɢɥɢɫɶ ɫ ɦɨɦɟɧɬɚ ɩɨɫɥɟɞɧɟɣ
ɩɪɨɜɟɪɤɢ ɢɥɢ ɧɟ ɛɵɥɢ ɤɨɪɪɟɤɬɧɨ ɡɚɤɪɵɬɵ.
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
293
Ɍɢɩ
Ɂɧɚɱɟɧɢɟ
MEDIUM
ɋɤɚɧɢɪɨɜɚɬɶ ɫɬɪɨɤɢ ɞɥɹ ɩɪɨɜɟɪɤɢ ɬɨɝɨ, ɱɬɨ ɭɞɚɥɟɧɧɵɟ ɫɜɹɡɢ ɜ ɩɨɪɹɞɤɟ. ɉɪɢ
ɷɬɨɦ ɬɚɤɠɟ ɜɵɱɢɫɥɹɟɬɫɹ ɤɨɧɬɪɨɥɶɧɚɹ ɫɭɦɦɚ ɤɥɸɱɚ ɫɬɪɨɤ ɢ ɫɜɟɪɹɟɬɫɹ ɫ ɤɨɧɬɪɨɥɶɧɨɣ ɫɭɦɦɨɣ ɜɫɟɯ ɤɥɸɱɟɣ.
EXTENDED
ȼɵɩɨɥɧɹɬɶ ɩɨɥɧɵɣ ɩɨɢɫɤ ɜɫɟɯ ɤɥɸɱɟɣ ɜɨ ɜɫɟɯ ɫɬɪɨɤɚɯ. Ƚɚɪɚɧɬɢɪɭɟɬ ɫɬɨɩɪɨɰɟɧɬɧɭɸ ɰɟɥɨɫɬɧɨɫɬɶ ɬɚɛɥɢɰɵ, ɧɨ ɬɪɟɛɭɟɬ ɦɧɨɝɨ ɜɪɟɦɟɧɢ.
ȿɫɥɢ ɧɟ ɭɤɚɡɚɧɚ ɧɢ ɨɞɧɚ ɢɡ ɨɩɰɢɣ QUICK, MEDIUM ɢɥɢ EXTENDED, ɬɨ ɩɨ ɭɦɨɥɱɚɧɢɸ ɞɥɹ
ɬɚɛɥɢɰ ɞɢɧɚɦɢɱɟɫɤɨɝɨ ɮɨɪɦɚɬɚ MyISAM ɜɵɛɢɪɚɟɬɫɹ MEDIUM. Ɍɢɩɨɦ ɩɪɨɜɟɪɤɢ ɞɥɹ ɫɬɚɬɢɱɟɫɤɢɯ ɬɚɛɥɢɰ MyISAM ɬɚɤɠɟ ɛɭɞɟɬ MEDIUM, ɟɫɥɢ ɬɨɥɶɤɨ ɧɟ ɭɤɚɡɚɧɨ CHANGED ɢɥɢ FAST. ȼ
ɷɬɨɦ ɫɥɭɱɚɟ ɩɨ ɭɦɨɥɱɚɧɢɸ ɜɵɛɢɪɚɟɬɫɹ QUICK. Ⱦɥɹ CHANGED ɢ FAST ɩɪɨɜɟɪɤɚ ɫɬɪɨɤ ɨɩɭɫɤɚɟɬɫɹ, ɩɨɬɨɦɭ ɱɬɨ ɫɬɪɨɤɢ ɨɱɟɧɶ ɪɟɞɤɨ ɨɤɚɡɵɜɚɸɬɫɹ ɩɨɜɪɟɠɞɟɧɧɵɦɢ.
ȼɵ ɦɨɠɟɬɟ ɤɨɦɛɢɧɢɪɨɜɚɬɶ ɨɩɰɢɢ ɩɪɨɜɟɪɤɢ, ɤɚɤ ɩɨɤɚɡɚɧɨ ɜ ɫɥɟɞɭɸɳɟɦ ɩɪɢɦɟɪɟ, ɜɵɩɨɥɧɹɸɳɟɦ ɛɵɫɬɪɭɸ ɩɪɨɜɟɪɤɭ ɬɚɛɥɢɰɭ ɧɚ ɩɪɟɞɦɟɬ ɬɨɝɨ, ɛɵɥɚ ɥɢ ɨɧɚ ɡɚɤɪɵɬɚ ɩɪɚɜɢɥɶɧɨ:
CHECK TABLE test_table FAST QUICK;
oË ÑËÕÏÛµÂ!
ȼ ɧɟɤɨɬɨɪɵɯ ɫɥɭɱɚɹɯ CHECK TABLE ɢɡɦɟɧɹɟɬ ɬɚɛɥɢɰɭ! ɗɬɨ ɩɪɨɢɫɯɨɞɢɬ, ɟɫɥɢ ɬɚɛɥɢɰɚ ɩɨɦɟɱɟɧɚ,
ɤɚɤ “ɩɨɜɪɟɠɞɟɧɧɚɹ” ɢɥɢ “ɧɟɩɪɚɜɢɥɶɧɨ ɡɚɤɪɵɬɚɹ”, ɧɨ CHECK TABLE ɧɟ ɧɚɯɨɞɢɬ ɜ ɧɟɣ ɧɢɤɚɤɢɯ ɩɪɨɛɥɟɦ. ȼ ɷɬɨɦ ɫɥɭɱɚɟ CHECK TABLE ɩɨɦɟɱɚɟɬ ɟɟ ɤɚɤ ɢɫɩɪɚɜɧɭɸ.
ȿɫɥɢ ɬɚɛɥɢɰɚ ɩɨɜɪɟɠɞɟɧɚ, ɜɟɪɨɹɬɧɟɟ ɜɫɟɝɨ, ɱɬɨ ɩɪɢɱɢɧɚ ɩɪɨɛɥɟɦ ɫɜɹɡɚɧɚ ɫ ɢɧɞɟɤɫɚɦɢ, ɚ ɧɟ ɫ ɞɚɧɧɵɦɢ. ȼɫɟ ɬɢɩɵ ɩɪɨɜɟɪɨɤ ɜɤɥɸɱɚɸɬ ɩɨɥɧɭɸ ɩɪɨɜɟɪɤɭ ɢɧɞɟɤɫɨɜ ɢ ɩɨɷɬɨɦɭ
ɜ ɫɨɫɬɨɹɧɢɢ ɨɛɧɚɪɭɠɢɬɶ ɛɨɥɶɲɢɧɫɬɜɨ ɨɲɢɛɨɤ.
ȿɫɥɢ ɜɵ ɬɨɥɶɤɨ ɯɨɬɢɬɟ ɩɪɨɜɟɪɢɬɶ ɬɚɛɥɢɰɭ, ɨ ɤɨɬɨɪɨɣ ɩɪɟɞɩɨɥɚɝɚɟɬɟ, ɱɬɨ ɫ ɧɟɣ ɜɫɟ ɜ
ɩɨɪɹɞɤɟ, ɜɵ ɧɟ ɞɨɥɠɧɵ ɭɤɚɡɵɜɚɬɶ ɧɢɤɚɤɢɯ ɨɩɰɢɣ ɩɪɨɜɟɪɤɢ ɢɥɢ ɨɩɰɢɸ QUICK. ɉɨɫɥɟɞɧɹɹ
ɞɨɥɠɧɚ ɩɪɢɦɟɧɹɬɶɫɹ, ɤɨɝɞɚ ɧɭɠɧɨ ɫɩɟɲɢɬɶ, ɢ ɪɢɫɤ ɬɨɝɨ, ɱɬɨ QUICK ɧɟ ɧɚɣɞɟɬ ɨɲɢɛɨɤ ɜ
ɮɚɣɥɟ ɞɚɧɧɵɯ, ɧɟɜɟɥɢɤ. (ȼ ɛɨɥɶɲɢɧɫɬɜɟ ɫɥɭɱɚɟɜ ɩɪɢ ɧɨɪɦɚɥɶɧɨɣ ɪɚɛɨɬɟ MySQL ɞɨɥɠɟɧ
ɧɚɣɬɢ ɥɸɛɵɟ ɨɲɢɛɤɢ ɜ ɮɚɣɥɟ ɞɚɧɧɵɯ. ȿɫɥɢ ɷɬɨ ɩɪɨɢɫɯɨɞɢɬ, ɬɚɛɥɢɰɚ ɩɨɦɟɱɚɟɬɫɹ ɤɚɤ
“ɩɨɜɪɟɠɞɟɧɧɚɹ” ɢ ɧɟ ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɧɟ ɛɭɞɟɬ ɜɨɫɫɬɚɧɨɜɥɟɧɚ.)
FAST ɢ CHANGED ɧɚɢɛɨɥɟɟ ɯɨɪɨɲɨ ɩɨɞɯɨɞɹɬ ɞɥɹ ɩɪɢɦɟɧɟɧɢɹ ɜ ɫɰɟɧɚɪɢɹɯ (ɧɚɩɪɢɦɟɪ,
ɜɵɩɨɥɧɹɟɦɵɯ ɚɜɬɨɦɚɬɢɱɟɫɤɢ ɞɟɦɨɧɨɦ cron), ɟɫɥɢ ɜɵ ɯɨɬɢɬɟ ɩɪɨɜɟɪɹɬɶ ɬɚɛɥɢɰɵ ɜ ɫɨɨɬɜɟɬɫɬɜɢɟ ɫ ɤɚɤɢɦ-ɧɢɛɭɞɶ ɝɪɚɮɢɤɨɦ. ȼ ɛɨɥɶɲɢɧɫɬɜɟ ɫɥɭɱɚɟɜ ɨɩɰɢɹ FAST ɛɨɥɟɟ ɩɪɟɞɩɨɱɬɢɬɟɥɶɧɚ, ɧɟɠɟɥɢ CHANGED. (ȿɞɢɧɫɬɜɟɧɧɵɣ ɫɥɭɱɚɣ, ɤɨɝɞɚ FAST ɧɟ ɩɪɟɞɩɨɱɬɢɬɟɥɶɧɚ, ɷɬɨ
ɟɫɥɢ ɜɵ ɩɪɟɞɩɨɥɚɝɚɟɬɟ, ɱɬɨ ɧɚɲɥɢ ɨɲɢɛɤɭ ɜ ɤɨɞɟ MyISAM.)
EXTENDED ɩɪɟɞɧɚɡɧɚɱɟɧɚ ɞɥɹ ɩɪɢɦɟɧɟɧɢɹ ɬɨɥɶɤɨ ɩɨɫɥɟ ɬɨɝɨ, ɤɚɤ ɜɵɩɨɥɧɹɥɚɫɶ ɨɛɵɱɧɚɹ ɩɪɨɜɟɪɤɚ, ɧɨ ɩɪɨɢɫɯɨɞɹɬ ɫɬɪɚɧɧɵɟ ɨɲɢɛɤɢ ɩɪɢ ɩɨɩɵɬɤɚɯ MySQL ɨɛɧɨɜɢɬɶ ɫɬɪɨɤɭ
ɢɥɢ ɧɚɣɬɢ ɟɟ ɩɨ ɤɥɸɱɭ. (ɗɬɨ ɨɱɟɧɶ ɦɚɥɨɜɟɪɨɹɬɧɨ, ɟɫɥɢ ɧɨɪɦɚɥɶɧɚɹ ɩɪɨɜɟɪɤɚ ɡɚɜɟɪɲɢɥɚɫɶ ɭɫɩɟɲɧɨ.)
ɇɢɠɟ ɨɩɢɫɚɧɵ ɧɟɤɨɬɨɪɵɟ ɩɪɨɛɥɟɦɵ, ɨ ɤɨɬɨɪɵɯ ɫɨɨɛɳɚɟɬ CHECK TABLE, ɢ ɤɨɬɨɪɵɟ ɧɟ
ɦɨɝɭɬ ɛɵɬɶ ɢɫɩɪɚɜɥɟɧɵ ɚɜɬɨɦɚɬɢɱɟɫɤɢ.
„Found row where the auto_increment column has the value 0 (ɇɚɣɞɟɧɚ ɫɬɪɨɤɚ,
ɜ ɤɨɬɨɪɨɣ ɫɬɨɥɛɟɰ auto_increment ɢɦɟɟɬ ɡɧɚɱɟɧɢɟ 0).
ɗɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɜ ɬɚɛɥɢɰɟ ɫɨɞɟɪɠɢɬɫɹ ɫɬɪɨɤɚ, ɜ ɤɨɬɨɪɨɣ ɡɧɚɱɟɧɢɟ ɫɬɨɥɛɰɚ
AUTO_INCREMENT ɪɚɜɧɨ 0. (ɋɭɳɟɫɬɜɭɟɬ ɜɨɡɦɨɠɧɨɫɬɶ ɫɨɡɞɚɬɶ ɫɬɪɨɤɭ ɫɨ ɡɧɚɱɟɧɢɟɦ
ɫɬɨɥɛɰɚ AUTO_INCREMENT, ɪɚɜɧɵɦ 0, ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ UPDATE.)
294
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɗɬɨ ɧɟ ɹɜɥɹɟɬɫɹ ɨɲɢɛɤɨɣ ɫɚɦɨ ɩɨ ɫɟɛɟ, ɧɨ ɦɨɠɟɬ ɜɵɡɜɚɬɶ ɩɪɨɛɥɟɦɵ, ɟɫɥɢ ɜɵ ɪɟɲɢɬɟ ɜɵɝɪɭɡɢɬɶ ɞɚɦɩ ɬɚɤɨɣ ɬɚɛɥɢɰɵ, ɚ ɡɚɬɟɦ ɜɨɫɫɬɚɧɨɜɢɬɶ ɟɟ ɢɡ ɞɚɦɩɚ ɥɢɛɨ ɜɵɩɨɥɧɢɬɶ ALTER TABLE. ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɫɬɨɥɛɟɰ AUTO_INCREMENT ɢɡɦɟɧɢɬ ɡɧɚɱɟɧɢɟ ɜ
ɫɨɨɬɜɟɬɫɬɜɢɢ ɫ ɩɪɚɜɢɥɚɦɢ, ɞɟɣɫɬɜɭɸɳɢɦɢ ɨɬɧɨɫɢɬɟɥɶɧɨ ɬɚɤɢɯ ɫɬɨɥɛɰɨɜ, ɱɬɨ ɦɨɠɟɬ ɩɪɢɜɟɫɬɢ ɤ ɩɪɨɛɥɟɦɟ ɞɭɛɥɢɪɨɜɚɧɢɹ ɤɥɸɱɟɣ.
ɑɬɨɛɵ ɢɡɛɚɜɢɬɶɫɹ ɨɬ ɭɩɨɦɹɧɭɬɨɝɨ ɩɪɟɞɭɩɪɟɠɞɟɧɢɹ, ɩɪɨɫɬɨ ɜɵɩɨɥɧɢɬɟ ɨɩɟɪɚɬɨɪ
UPDATE ɢ ɩɪɢɫɜɨɣɬɟ ɫɬɨɥɛɰɭ ɡɧɚɱɟɧɢɟ, ɨɬɥɢɱɧɨɟ ɨɬ 0.
6.5.2.4. Ñèíòàêñèñ CHECKSUM TABLE
CHECKSUM TABLE ɢɦɹ_ɬɚɛɥɢɰɵ [, ɢɦɹ_ɬɚɛɥɢɰɵ] ... [ QUICK | EXTENDED ]
ɋɨɨɛɳɚɟɬ ɤɨɧɬɪɨɥɶɧɭɸ ɫɭɦɦɭ ɬɚɛɥɢɰɵ.
ȿɫɥɢ ɭɤɚɡɚɧɚ ɨɩɰɢɹ QUICK, ɫɨɨɛɳɚɟɬɫɹ ɚɤɬɭɚɥɶɧɚɹ ɤɨɧɬɪɨɥɶɧɚɹ ɫɭɦɦɚ ɬɚɛɥɢɰɵ, ɟɫɥɢ
ɨɧɚ ɞɨɫɬɭɩɧɚ, ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ ɜɵɞɚɟɬɫɹ NULL. ɗɬɨ ɞɟɥɚɟɬɫɹ ɨɱɟɧɶ ɛɵɫɬɪɨ. Ⱥɤɬɭɚɥɶɧɚɹ ɤɨɧɬɪɨɥɶɧɚɹ ɫɭɦɦɚ ɜɤɥɸɱɚɟɬɫɹ ɨɩɰɢɟɣ ɬɚɛɥɢɰɵ CHECKSUM=1, ɤɨɬɨɪɚɹ ɜ ɧɚɫɬɨɹɳɟɟ
ɜɪɟɦɹ ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ ɬɨɥɶɤɨ ɞɥɹ ɬɚɛɥɢɰ MyISAM. ɋɦ. ɪɚɡɞɟɥ 6.2.5.
ɉɪɢ ɜɵɛɨɪɟ ɪɟɠɢɦɚ EXTENDED ɜɵɩɨɥɧɹɟɬɫɹ ɱɬɟɧɢɟ ɜɫɟɣ ɬɚɛɥɢɰɵ – ɫɬɪɨɤɚ ɡɚ ɫɬɪɨɤɨɣ, ɢ
ɜɵɱɢɫɥɹɟɬɫɹ ɤɨɧɬɪɨɥɶɧɚɹ ɫɭɦɦɚ. Ⱦɥɹ ɛɨɥɶɲɢɯ ɬɚɛɥɢɰ ɷɬɨ ɦɨɠɟɬ ɩɪɨɢɫɯɨɞɢɬɶ ɨɱɟɧɶ ɞɨɥɝɨ.
ɉɨ ɭɦɨɥɱɚɧɢɸ, ɟɫɥɢ ɧɟ ɭɤɚɡɚɧɨ ɧɢ QUICK, ɧɢ EXTENDED, MySQL ɜɨɡɜɪɚɳɚɟɬ ɚɤɬɭɚɥɶɧɭɸ ɤɨɧɬɪɨɥɶɧɭɸ ɫɭɦɦɭ ɬɚɛɥɢɰɵ, ɟɫɥɢ ɦɟɯɚɧɢɡɦ ɯɪɚɧɟɧɢɹ ɟɟ ɩɨɞɞɟɪɠɢɜɚɟɬ, ɥɢɛɨ ɜɵɩɨɥɧɹɟɬ ɫɤɚɧɢɪɨɜɚɧɢɟ ɬɚɛɥɢɰɵ ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ.
Ɉɩɟɪɚɬɨɪ ɪɟɚɥɢɡɨɜɚɧ ɜ ɜɟɪɫɢɢ MySQL 4.1.1.
6.5.2.5. Ñèíòàêñèñ OPTIMIZE TABLE
OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE ɢɦɹ_ɬɚɛɥɢɰɵ [, ɢɦɹ_ɬɚɛɥɢɰɵ] ...
OPTIMIZE TABLE ɧɟɨɛɯɨɞɢɦɨ ɢɫɩɨɥɶɡɨɜɚɬɶ, ɟɫɥɢ ɭɞɚɥɹɥɚɫɶ ɛɨɥɶɲɚɹ ɱɚɫɬɶ ɫɬɪɨɤ ɬɚɛɥɢɰɵ ɥɢɛɨ ɜɵɩɨɥɧɹɥɨɫɶ ɦɧɨɝɨ ɢɡɦɟɧɟɧɢɣ ɜ ɬɚɛɥɢɰɟ ɫ ɩɟɪɟɦɟɧɧɨɣ ɞɥɢɧɨɣ ɫɬɪɨɤɢ (ɬɚɛɥɢɰɵ ɫ ɫɬɨɥɛɰɚɦɢ ɬɢɩɨɜ VARCHAR, BLOB ɢɥɢ TEXT). ɍɞɚɥɟɧɧɵɟ ɫɬɪɨɤɢ ɩɨɦɟɳɚɸɬɫɹ ɜ ɫɜɹɡɧɵɣ ɫɩɢɫɨɤ ɢ ɩɨɫɥɟɞɭɸɳɢɟ ɨɩɟɪɚɰɢɢ INSERT ɩɨɜɬɨɪɧɨ ɢɫɩɨɥɶɡɭɸɬ ɫɬɚɪɵɟ ɩɨɡɢɰɢɢ ɡɚɩɢɫɟɣ. ȼɵ ɦɨɠɟɬɟ ɩɪɢɦɟɧɹɬɶ OPTIMIZE TABLE ɞɥɹ ɜɨɫɫɬɚɧɨɜɥɟɧɢɹ ɧɟɢɫɩɨɥɶɡɨɜɚɧɧɨɝɨ
ɩɪɨɫɬɪɚɧɫɬɜɚ ɢ ɞɟɮɪɚɝɦɟɧɬɚɰɢɢ ɮɚɣɥɚ ɞɚɧɧɵɯ.
ȼ ɛɨɥɶɲɢɧɫɬɜɟ ɫɥɭɱɚɟɜ ɜɨɨɛɳɟ ɧɟɬ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɜɵɡɵɜɚɬɶ OPTIMIZE TABLE. Ⱦɚɠɟ
ɟɫɥɢ ɜɧɨɫɢɬɫɹ ɦɧɨɠɟɫɬɜɨ ɢɡɦɟɧɟɧɢɣ ɜ ɫɬɪɨɤɢ ɩɟɪɟɦɟɧɧɨɣ ɞɥɢɧɵ, ɦɚɥɨɜɟɪɨɹɬɧɨ, ɱɬɨ ɜɚɦ
ɩɨɧɚɞɨɛɢɬɫɹ ɞɟɥɚɬɶ ɷɬɨ ɱɚɳɟ ɪɚɡɚ ɜ ɧɟɞɟɥɸ ɢɥɢ ɜ ɦɟɫɹɰ, ɞɚ ɢ ɬɨ – ɬɨɥɶɤɨ ɞɥɹ ɧɟɤɨɬɨɪɵɯ
ɬɚɛɥɢɰ.
ȼ ɧɚɫɬɨɹɳɢɣ ɦɨɦɟɧɬ OPTIMIZE TABLE ɪɚɛɨɬɚɟɬ ɬɨɥɶɤɨ ɫ ɬɚɛɥɢɰɚɦɢ MyISAM ɢ BDB.
ɉɪɢɱɟɦ ɜ ɫɥɭɱɚɟ ɬɚɛɥɢɰ BDB ɨɩɟɪɚɬɨɪ OPTIMIZE TABLE ɨɬɨɛɪɚɠɚɟɬɫɹ ɧɚ ANALYZE TABLE.
ɋɦ. ɪɚɡɞɟɥ 6.5.2.1.
Ɇɨɠɧɨ ɡɚɫɬɚɜɢɬɶ ɪɚɛɨɬɚɬɶ OPTIMIZE TABLE ɫ ɞɪɭɝɢɦɢ ɬɢɩɚɦɢ ɬɚɛɥɢɰ, ɡɚɩɭɫɬɢɜ ɫɟɪɜɟɪ mysqld ɫ ɨɩɰɢɟɣ --skip-new ɢɥɢ --safe-mode, ɧɨ ɜ ɷɬɨɦ ɫɥɭɱɚɟ OPTIMIZE TABLE ɩɪɨɫɬɨ ɨɬɨɛɪɚɠɚɟɬɫɹ ɧɚ ALTER TABLE.
OPTIMIZE TABLE ɪɚɛɨɬɚɟɬ ɬɚɤ:
1. ȿɫɥɢ ɜ ɬɚɛɥɢɰɟ ɟɫɬɶ ɭɞɚɥɟɧɧɵɟ ɢɥɢ ɪɚɡɞɟɥɟɧɧɵɟ ɫɬɪɨɤɢ, ɜɨɫɫɬɚɧɚɜɥɢɜɚɟɬ ɬɚɛɥɢɰɭ.
2. ȿɫɥɢ ɫɬɪɚɧɢɰɵ ɢɧɞɟɤɫɚ ɧɟ ɨɬɫɨɪɬɢɪɨɜɚɧɵ, ɫɨɪɬɢɪɭɟɬ ɢɯ.
3. ȿɫɥɢ ɫɬɚɬɢɫɬɢɱɟɫɤɚɹ ɢɧɮɨɪɦɚɰɢɹ ɭɫɬɚɪɟɥɚ (ɢ ɜɨɫɫɬɚɧɨɜɥɟɧɢɟ ɧɟ ɦɨɠɟɬ ɛɵɬɶ ɜɵɩɨɥɧɟɧɨ ɩɪɨɫɬɨɣ ɫɨɪɬɢɪɨɜɤɨɣ ɢɧɞɟɤɫɚ), ɨɛɧɨɜɥɹɟɬ ɟɟ.
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
295
Ɉɬɦɟɬɢɦ, ɱɬɨ MySQL ɛɥɨɤɢɪɭɟɬ ɬɚɛɥɢɰɭ ɧɚ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ OPTIMIZE TABLE.
Ⱦɨ MySQL 4.1.1 ɨɩɟɪɚɬɨɪ OPTIMIZE TABLE ɧɟ ɡɚɩɢɫɵɜɚɥɫɹ ɜ ɛɢɧɚɪɧɵɣ ɠɭɪɧɚɥ. ɇɚɱɢɧɚɹ ɫ MySQL 4.1.1, ɨɧ ɡɚɩɢɫɵɜɚɟɬɫɹ, ɟɫɥɢ ɬɨɥɶɤɨ ɧɟ ɛɵɥɨ ɭɤɚɡɚɧɨ ɧɟɨɛɛɹɡɚɬɟɥɶɧɨɟ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ NO_WRITE_TO_BINLOG (ɢɥɢ ɟɝɨ ɩɫɟɜɞɨɧɢɦ LOCAL).
6.5.2.6. Ñèíòàêñèñ REPAIR TABLE
REPAIR [LOCAL | NO_WRITE_TO_BINLOG] TABLE
ɢɦɹ_ɬɚɛɥɢɰɵ [, ɢɦɹ_ɬɚɛɥɢɰɵ] ... [QUICK] [EXTENDED] [USE_FRM]
REPAIR TABLE ɨɫɭɳɟɫɬɜɥɹɟɬ ɜɨɫɫɬɚɧɨɜɥɟɧɢɟ (ɪɟɦɨɧɬ) ɩɨɜɪɟɠɞɟɧɧɵɯ ɬɚɛɥɢɰ. ɉɨ
ɭɦɨɥɱɚɧɢɸ, ɨɧ ɢɦɟɟɬ ɬɨɬ ɠɟ ɷɮɮɟɤɬ, ɱɬɨ ɢ myisamchk --recover ɢɦɹ_ɬɚɛɥɢɰɵ.
REPAIR TABLE ɪɚɛɨɬɚɟɬ ɬɨɥɶɤɨ ɫ ɬɚɛɥɢɰɚɦɢ MyISAM. Ɉɛɵɱɧɨ ɜɨɨɛɳɟ ɧɟɬ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɜɵɡɵɜɚɬɶ REPAIR TABLE. Ɉɞɧɚɤɨ ɟɫɥɢ ɧɟɩɪɢɹɬɧɨɫɬɶ ɜɫɟ ɠɟ ɜɨɡɧɢɤɥɚ, ɬɨ ɜɟɫɶɦɚ ɜɟɪɨɹɬɧɨ, ɱɬɨ REPAIR TABLE ɫɦɨɠɟɬ ɜɨɫɫɬɚɧɨɜɢɬɶ ɜɫɟ ɜɚɲɢ ɞɚɧɧɵɟ ɜ ɬɚɛɥɢɰɚɯ MyISAM. ȿɫɥɢ
ɜɚɲɢ ɬɚɛɥɢɰɵ ɱɚɫɬɨ ɩɨɥɭɱɚɸɬ ɩɨɜɪɟɠɞɟɧɢɹ, ɜɚɦ ɧɭɠɧɨ ɧɚɣɬɢ ɩɪɢɱɢɧɭ ɷɬɨɝɨ, ɱɬɨɛɵ
ɢɡɛɟɠɚɬɶ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɱɚɫɬɨ ɡɚɩɭɫɤɚɬɶ REPAIR TABLE.
Ɉɩɟɪɚɬɨɪ ɜɨɡɜɪɚɳɚɟɬ ɬɚɛɥɢɰɭ ɫɨ ɫɥɟɞɭɸɳɢɦɢ ɫɬɨɥɛɰɚɦɢ:
ɋɬɨɥɛɟɰ
Ɂɧɚɱɟɧɢɟ
Table
Op
Msg_type
Msg_text
ɂɦɹ ɬɚɛɥɢɰɵ
ȼɫɟɝɞɚ Repair
Ɍɢɩ: status, error, info ɢɥɢ warning
ɋɨɨɛɳɟɧɢɟ
Ɉɩɟɪɚɬɨɪ REPAIR TABLE ɦɨɠɟɬ ɜɵɞɚɬɶ ɦɧɨɠɟɫɬɜɨ ɫɬɪɨɤ ɫ ɢɧɮɨɪɦɚɰɢɟɣ ɨ ɤɚɠɞɨɣ
ɜɨɫɫɬɚɧɚɜɥɢɜɚɟɦɨɣ ɬɚɛɥɢɰɟ. ɉɨɫɥɟɞɧɹɹ ɫɬɪɨɤɚ ɛɭɞɟɬ ɫɨɞɟɪɠɚɬɶ ɡɧɚɱɟɧɢɟ Msg_type, ɪɚɜɧɨɟ status, ɢ Msg_text ɨɛɵɱɧɨ ɞɨɥɠɧɨ ɛɵɬɶ OK. ȿɫɥɢ ɜɵ ɧɟ ɩɨɥɭɱɚɟɬɟ OK, ɬɨ ɜɚɦ ɧɭɠɧɨ
ɩɨɩɪɨɛɨɜɚɬɶ ɜɨɫɫɬɚɧɨɜɢɬɶ ɬɚɛɥɢɰɭ ɤɨɦɚɧɞɨɣ myisamchk --safe-recover, ɩɨɬɨɦɭ ɱɬɨ
REPAIR TABLE ɩɨɤɚ ɧɟ ɪɟɚɥɢɡɭɟɬ ɜɫɟɯ ɜɨɡɦɨɠɧɨɫɬɟɣ myisamchk. Ɇɵ ɩɥɚɧɢɪɭɟɦ ɜ ɛɭɞɭɳɟɦ ɫɞɟɥɚɬɶ ɷɬɨɬ ɨɩɟɪɚɬɨɪ ɛɨɥɟɟ ɝɢɛɤɢɦ.
ȿɫɥɢ ɭɤɚɡɚɧɨ QUICK, REPAIR TABLE ɩɵɬɚɟɬɫɹ ɜɨɫɫɬɚɧɨɜɢɬɶ ɬɨɥɶɤɨ ɞɟɪɟɜɨ ɢɧɞɟɤɫɚ.
ɗɬɨɬ ɬɢɩ ɜɨɫɫɬɚɧɨɜɥɟɧɢɹ ɚɧɚɥɨɝɢɱɟɧ myisamchk --recover --quick.
ȿɫɥɢ ɩɪɢɦɟɧɹɟɬɫɹ ɨɩɰɢɹ EXTENED, MySQL ɫɨɡɞɚɟɬ ɢɧɞɟɤɫɵ ɫɬɪɨɤɭ ɡɚ ɫɬɪɨɤɨɣ, ɜɦɟɫɬɨ
ɬɨɝɨ, ɱɬɨɛɵ ɫɨɡɞɚɜɚɬɶ ɢɯ ɫɨɪɬɢɪɨɜɤɨɣ ɩɨ ɨɞɧɨɦɭ ɡɚ ɪɚɡ. (Ⱦɨ ɜɟɪɫɢɢ MySQL 4.1 ɷɬɨ ɦɨɠɟɬ ɛɵɬɶ ɥɭɱɲɟ, ɱɟɦ ɫɨɪɬɢɪɨɜɤɚ ɩɨ ɤɥɸɱɚɦ ɮɢɤɫɢɪɨɜɚɧɧɨɣ ɞɥɢɧɵ, ɨɫɨɛɟɧɧɨ ɟɫɥɢ ɭ ɜɚɫ
ɞɥɢɧɧɵɣ ɤɥɸɱ ɬɢɩɚ CHAR, ɤɨɬɨɪɵɣ ɯɨɪɨɲɨ ɩɚɤɭɟɬɫɹ.) ɗɬɨɬ ɬɢɩ ɜɨɫɫɬɚɧɨɜɥɟɧɢɹ ɚɧɚɥɨɝɢɱɟɧ myisamchk --safe-recover.
ɇɚɱɢɧɚɹ ɫ MySQL 4.0.2, ɜ REPAIR TABLE ɞɨɛɚɜɥɟɧ ɪɟɠɢɦ USE_FRM. ȿɝɨ ɫɥɟɞɭɟɬ ɩɪɢɦɟɧɹɬɶ, ɟɫɥɢ ɨɬɫɭɬɫɬɜɭɟɬ ɮɚɣɥ ɢɧɞɟɤɫɨɜ .MYI, ɢɥɢ ɟɫɥɢ ɟɝɨ ɡɚɝɨɥɨɜɨɤ ɩɨɜɪɟɠɞɟɧ. ȼ ɷɬɨɦ
ɪɟɠɢɦɟ MySQL ɩɟɪɟɫɨɡɞɚɟɬ ɮɚɣɥ .MYI, ɢɫɩɨɥɶɡɭɹ ɢɧɮɨɪɦɚɰɢɸ ɢɡ ɮɚɣɥɚ .frm. ɗɬɨɬ ɫɩɨɫɨɛ ɜɨɫɫɬɚɧɨɜɥɟɧɢɹ ɧɟɜɨɡɦɨɠɧɨ ɨɫɭɳɟɫɬɜɢɬɶ ɫɪɟɞɫɬɜɚɦɢ myisamchk.
dÖÒÕËÖÒÏ!
ȿɫɥɢ ɫɟɪɜɟɪ ɚɜɚɪɢɣɧɨ ɡɚɜɟɪɲɚɟɬɫɹ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ ɨɩɟɪɚɬɨɪɚ REPAIR TABLE, ɜɚɠɧɨ ɩɨɫɥɟ ɟɝɨ ɩɟɪɟɡɚɩɭɫɤɚ ɧɟɦɟɞɥɟɧɧɨ ɫɧɨɜɚ ɡɚɩɭɫɬɢɬɶ REPAIR TABLE, ɩɪɟɠɞɟ ɱɟɦ ɜɵɩɨɥɧɹɬɶ ɤɚɤɢɟɬɨ ɦɚɧɢɩɭɥɹɰɢɢ ɫ ɬɚɛɥɢɰɟɣ. (ȼɫɟɝɞɚ ɩɨɥɟɡɧɨ ɧɚɱɢɧɚɬɶ ɫ ɫɨɡɞɚɧɢɹ ɪɟɡɟɪɜɧɨɣ ɤɨɩɢɢ.) ȼ ɯɭɞɲɟɦ
ɫɥɭɱɚɟ ɜɵ ɦɨɠɟɬɟ ɩɨɥɭɱɢɬɶ ɧɨɜɵɣ ɱɢɫɬɵɣ ɢɧɞɟɤɫɧɵɣ ɮɚɣɥ, ɛɟɡ ɢɧɮɨɪɦɚɰɢɢ ɨ ɮɚɣɥɟ ɞɚɧɧɵɯ, ɢ
ɫɥɟɞɭɸɳɚɹ ɨɩɟɪɚɰɢɹ, ɤɨɬɨɪɭɸ ɜɵ ɩɨɩɵɬɚɟɬɟɫɶ ɜɵɩɨɥɧɢɬɶ, ɦɨɠɟɬ ɩɟɪɟɡɚɩɢɫɚɬɶ ɮɚɣɥ ɞɚɧɧɵɯ.
ɗɬɨ ɦɚɥɨɜɟɪɨɹɬɧɨ, ɧɨ ɜɫɟ ɠɟ ɜɨɡɦɨɠɧɨ.
296
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
Ⱦɨ MySQL 4.1.1 ɨɩɟɪɚɬɨɪɵ REPAIR TABLE ɧɟ ɡɚɩɢɫɵɜɚɥɫɹ ɜ ɛɢɧɚɪɧɵɣ ɠɭɪɧɚɥ. ɇɚɱɢɧɚɹ ɫ MySQL 4.1.1, ɨɧɢ ɡɚɩɢɫɵɜɚɸɬɫɹ, ɟɫɥɢ ɬɨɥɶɤɨ ɧɟ ɛɵɥɨ ɭɤɚɡɚɧɨ ɧɟɨɛɹɡɚɬɟɥɶɧɨɟ
ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ NO_WRITE_TO_BINLOG (ɢɥɢ ɟɝɨ ɩɫɟɜɞɨɧɢɦ LOCAL).
6.5.2.7. Ñèíòàêñèñ RESTORE TABLE
RESTORE TABLE ɢɦɹ_ɬɚɛɥɢɰɵ [, ɢɦɹ_ɬɚɛɥɢɰɵ] ...
FROM '/ɩɭɬɶ/ɤ/ɤɚɬɚɥɨɝɭ/ɪɟɡɟɪɜɧɵɯ_ɤɨɩɢɣ'
ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɜɨɫɫɬɚɧɚɜɥɢɜɚɟɬ ɬɚɛɥɢɰɭ ɢɡ ɪɟɡɟɪɜɧɨɣ ɤɨɩɢɢ (ɧɟ ɩɭɬɚɬɶ ɫ “ɜɨɫɫɬɚɧɨɜɥɟɧɢɟɦ” ɨɩɟɪɚɬɨɪɨɦ REPAIR TABLE, ɤɨɬɨɪɵɣ, ɩɨ ɫɭɬɢ, ɜɵɩɨɥɧɹɟɬ ɪɟɦɨɧɬ ɩɨɜɪɟɠɞɟɧɧɨɣ
ɬɚɛɥɢɰɵ – ɩɪɢɦ. ɩɟɪɟɜ.), ɤɨɬɨɪɚɹ ɛɵɥɚ ɫɨɡɞɚɧɚ ɨɩɟɪɚɬɨɪɨɦ BACKUP TABLE. ɋɭɳɟɫɬɜɭɸɳɢɟ ɬɚɛɥɢɰɵ ɩɟɪɟɡɚɩɢɫɚɧɵ ɧɟ ɛɭɞɭɬ, ɟɫɥɢ ɜɵ ɩɨɩɵɬɚɟɬɟɫɶ ɜɨɫɫɬɚɧɨɜɢɬɶ ɩɨɜɟɪɯ ɫɭɳɟɫɬɜɭɸɳɟɣ ɬɚɛɥɢɰɵ, ɬɨ ɩɨɥɭɱɢɬɟ ɫɨɨɛɳɟɧɢɟ ɨɛ ɨɲɢɛɤɟ. Ɍɨɱɧɨ ɬɚɤ ɠɟ, ɤɚɤ ɢ BACKUP TABLE,
ɜ ɧɚɫɬɨɹɳɟɟ ɜɪɟɦɹ RESTORE TABLE ɪɚɛɨɬɚɟɬ ɬɨɥɶɤɨ ɫ ɬɚɛɥɢɰɚɦɢ MyISAM. Ʉɚɬɚɥɨɝ ɞɨɥɠɟɧ
ɛɵɬɶ ɭɤɚɡɚɧ ɫ ɩɨɥɧɵɦ ɩɭɬɟɦ.
Ɋɟɡɟɪɜɧɚɹ ɤɨɩɢɹ ɤɚɠɞɨɣ ɬɚɛɥɢɰɵ ɫɨɫɬɨɢɬ ɢɡ ɟɝɨ ɮɚɣɥɚ ɮɨɪɦɚɬɚ .frm ɢ ɮɚɣɥɚ ɞɚɧɧɵɯ
.MYD. Ɉɩɟɪɚɰɢɹ ɜɨɫɫɬɚɧɨɜɥɟɧɢɹ ɢɡ ɪɟɡɟɪɜɧɨɣ ɤɨɩɢɢ ɜɨɡɜɪɚɳɚɟɬ ɜ ɛɚɡɭ ɷɬɢ ɮɚɣɥɵ, ɚ ɡɚɬɟɦ ɫɬɪɨɢɬ ɩɨ ɧɢɦ ɢɧɞɟɤɫɧɵɣ ɮɚɣɥ .MYI. ȼɨɫɫɬɚɧɨɜɥɟɧɢɟ ɢɡ ɤɨɩɢɢ ɬɪɟɛɭɟɬ ɛɨɥɶɲɟ ɜɪɟɦɟɧɢ, ɱɟɦ ɪɟɡɟɪɜɧɨɟ ɤɨɩɢɪɨɜɚɧɢɟ, ɩɨɫɤɨɥɶɤɭ ɧɭɠɧɨ ɩɟɪɟɫɬɪɚɢɜɚɬɶ ɢɧɞɟɤɫɵ. ɑɟɦ ɛɨɥɶɲɟ
ɭ ɬɚɛɥɢɰɵ ɢɧɞɟɤɫɨɜ, ɬɟɦ ɞɨɥɶɲɟ ɷɬɨ ɩɪɨɢɫɯɨɞɢɬ.
Ɉɩɟɪɚɬɨɪ ɜɨɡɜɪɚɳɚɟɬ ɬɚɛɥɢɰɭ ɫɨ ɫɥɟɞɭɸɳɢɦɢ ɫɬɨɥɛɰɚɦɢ:
ɋɬɨɥɛɟɰ
Ɂɧɚɱɟɧɢɟ
Table
Op
Msg_type
Msg_text
ɂɦɹ ɬɚɛɥɢɰɵ
ȼɫɟɝɞɚ Restore
Ɍɢɩ: status, error, info ɢɥɢ warning
ɋɨɨɛɳɟɧɢɟ
6.5.3. Ñèíòàêñèñ SET è SHOW
SET ɩɨɡɜɨɥɹɟɬ ɭɫɬɚɧɚɜɥɢɜɚɬɶ ɩɟɪɟɦɟɧɧɵɟ ɢ ɨɩɰɢɢ.
SHOW ɢɦɟɟɬ ɦɧɨɠɟɫɬɜɨ ɪɚɡɥɢɱɧɵɯ ɮɨɪɦ, ɤɨɬɨɪɵɟ ɩɪɟɞɫɬɚɜɥɹɸɬ ɢɧɮɨɪɦɚɰɢɸ ɨ ɛɚɡɚɯ
ɞɚɧɧɵɯ, ɬɚɛɥɢɰɚɯ, ɫɬɨɥɛɰɚɯ, ɚ ɬɚɤɠɟ ɢɧɮɨɪɦɚɰɢɸ ɨ ɫɨɫɬɨɹɧɢɢ ɫɟɪɜɟɪɚ.
ȼ ɞɚɧɧɨɦ ɪɚɡɞɟɥɟ ɨɩɢɫɚɧɵ ɫɥɟɞɭɸɳɢɟ ɫɢɧɬɚɤɫɢɱɟɫɤɢɟ ɮɨɪɦɵ:
SHOW
SHOW
SHOW
SHOW
SHOW
SHOW
SHOW
SHOW
SHOW
SHOW
SHOW
SHOW
SHOW
SHOW
SHOW
SHOW
SHOW
[FULL] COLUMNS FROM ɢɦɹ_ɬɚɛɥɢɰɵ [FROM ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ] [LIKE 'ɲɚɛɥɨɧ']
CREATE DATABASE ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ
CREATE TABLE ɢɦɹ_ɬɚɛɥɢɰɵ
DATABASES [LIKE 'ɲɚɛɥɨɧ']
[STORAGE] ENGINES
ERRORS [LIMIT [ɫɦɟɳɟɧɢɟ,] row_count]
GRANTS FOR ɩɨɥɶɡɨɜɚɬɟɥɶ
INDEX FROM ɢɦɹ_ɬɚɛɥɢɰɵ [FROM ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ]
INNODB STATUS
[BDB] LOGS
PRIVILEGES
[FULL] PROCESSLIST
STATUS [LIKE 'ɲɚɛɥɨɧ']
TABLE STATUS [FROM ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ] [LIKE 'ɲɚɛɥɨɧ']
[OPEN] TABLES [FROM ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ] [LIKE 'ɲɚɛɥɨɧ']
[GLOBAL | SESSION] VARIABLES [LIKE 'ɲɚɛɥɨɧ']
WARNINGS [LIMIT [ɫɦɟɳɟɧɢɟ,] row_count]
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
297
ȿɫɥɢ ɫɢɧɬɚɤɫɢɫ ɞɚɧɧɨɝɨ ɨɩɟɪɚɬɨɪɚ SHOW ɜɤɥɸɱɚɟɬ ɱɚɫɬɶ LIKE 'ɲɚɛɥɨɧ', ɬɨ 'ɲɚɛɥɨɧ' –
ɷɬɨ ɫɬɪɨɤɚ, ɤɨɬɨɪɚɹ ɦɨɠɟɬ ɫɨɞɟɪɠɚɬɶ ɫɢɦɜɨɥɵ SQL-ɲɚɛɥɨɧɨɜ ‘%’ ɢ ‘_’. ɒɚɛɥɨɧ (pattern)
ɩɪɢɦɟɧɹɟɬɫɹ ɞɥɹ ɨɝɪɚɧɢɱɟɧɢɹ ɜɵɜɨɞɚ ɬɨɥɶɤɨ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɦɢ ɡɧɚɱɟɧɢɹɦɢ.
Ɉɬɦɟɬɢɦ, ɱɬɨ ɫɭɳɟɫɬɜɭɸɬ ɢ ɞɪɭɝɢɟ ɮɨɪɦɵ ɷɬɢɯ ɨɩɟɪɚɬɨɪɨɜ, ɨɩɢɫɚɧɧɵɟ ɜ ɞɪɭɝɢɯ
ɦɟɫɬɚɯ:
„Ɉɩɟɪɚɬɨɪ SET PASSWORD – ɞɥɹ ɩɪɢɫɜɨɟɧɢɹ ɩɚɪɨɥɹ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɨɣ ɭɱɟɬɧɨɣ ɡɚɩɢɫɢ, ɨɩɢɫɚɧ ɜ ɪɚɡɞɟɥɟ 6.5.1.3.
„Ɉɩɟɪɚɬɨɪ SHOW ɢɦɟɟɬ ɮɨɪɦɵ, ɩɪɟɞɨɫɬɚɜɥɹɸɳɢɟ ɢɧɮɨɪɦɚɰɢɸ ɨ ɝɥɚɜɧɵɯ ɢ ɩɨɞɱɢɧɟɧɧɵɯ ɫɟɪɜɟɪɚɯ ɜ ɪɟɩɥɢɤɚɰɢɢ:
SHOW
SHOW
SHOW
SHOW
SHOW
BINLOG EVENTS
MASTER LOGS
MASTER STATUS
SLAVE HOSTS
SLAVE STATUS
ɗɬɢ ɮɨɪɦɵ SHOW ɨɩɢɫɚɧɵ ɜ ɪɚɡɞɟɥɟ 6.6.
6.5.3.1. Ñèíòàêñèñ SET
SET ɩɪɢɫɜɚɢɜɚɧɢɟ_ɩɟɪɟɦɟɧɧɨɣ [, ɩɪɢɫɜɚɢɜɚɧɢɟ_ɩɟɪɟɦɟɧɧɨɣ] ...
ɩɪɢɫɜɚɢɜɚɧɢɟ_ɩɟɪɟɦɟɧɧɨɣ:
ɢɦɹ_ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɨɣ_ɩɟɪɟɦɟɧɧɨɣ = ɜɵɪɚɠɟɧɢɟ
| [GLOBAL|SESSION] ɢɦɹ_ɫɢɫɬɟɦɧɨɣ_ɩɟɪɟɦɟɧɧɨɣ = ɜɵɪɚɠɟɧɢɟ
| @@[global.|session.]ɢɦɹ_ɫɢɫɬɟɦɧɨɣ_ɩɟɪɟɦɟɧɧɨɣ = ɜɵɪɚɠɟɧɢɟ
Ɉɩɟɪɚɬɨɪ SET ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɡɧɚɱɟɧɢɟ ɪɚɡɥɢɱɧɵɦ ɬɢɩɚɦ ɩɟɪɟɦɟɧɧɵɯ, ɤɨɬɨɪɵɟ ɜɥɢɹɸɬ ɧɚ ɪɚɛɨɬɭ ɫɟɪɜɟɪɚ ɢ ɤɥɢɟɧɬɚ. Ɉɧ ɦɨɠɟɬ ɩɪɢɦɟɧɹɬɶɫɹ ɞɥɹ ɩɪɢɫɜɨɟɧɢɹ ɡɧɚɱɟɧɢɣ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɢɦ ɢ ɫɢɫɬɟɦɧɵɦ ɩɟɪɟɦɟɧɧɵɦ.
ȼ MySQL 4.0.3 ɛɵɥɢ ɞɨɛɚɜɥɟɧɵ ɨɩɰɢɢ GLOBAL ɢ SESSION, ɚ ɬɚɤɠɟ ɛɵɥɚ ɨɛɟɫɩɟɱɟɧɚ
ɜɨɡɦɨɠɧɨɫɬɶ ɧɚɢɛɨɥɟɟ ɜɚɠɧɵɦ ɫɢɫɬɟɦɧɵɦ ɩɟɪɟɦɟɧɧɵɦ ɢɡɦɟɧɹɬɶɫɹ ɞɢɧɚɦɢɱɟɫɤɢ ɜ ɩɪɨɰɟɫɫɟ ɪɚɛɨɬɵ.
ȼ ɛɨɥɟɟ ɫɬɚɪɵɯ ɜɟɪɫɢɹɯ MySQL ɢɫɩɨɥɶɡɨɜɚɥɚɫɶ ɨɩɰɢɹ SET OPTION ɜɦɟɫɬɨ SET, ɧɨ ɧɚ
ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɨɧɚ ɭɫɬɚɪɟɥɚ. ɉɪɨɫɬɨ ɨɩɭɫɤɚɣɬɟ ɫɥɨɜɨ OPTION.
ɋɥɟɞɭɸɳɢɟ ɩɪɢɦɟɪɵ ɩɨɤɚɡɵɜɚɸɬ ɪɚɡɥɢɱɧɵɣ ɫɢɧɬɚɤɫɢɫ, ɤɨɬɨɪɵɣ ɢɫɩɨɥɶɡɭɟɬɫɹ ɞɥɹ
ɭɫɬɚɧɨɜɤɢ ɩɟɪɟɦɟɧɧɵɯ.
ɉɨɥɶɡɨɜɚɬɟɥɶɫɤɚɹ ɩɟɪɟɦɟɧɧɚɹ ɡɚɩɢɫɵɜɚɟɬɫɹ ɤɚɤ @ɢɦɹ_ɩɟɪɟɦɟɧɧɨɣ ɢ ɦɨɠɟɬ ɛɵɬɶ ɭɫɬɚɧɨɜɥɟɧɚ ɫɥɟɞɭɸɳɢɦ ɨɛɪɚɡɨɦ:
SET @ɢɦɹ_ɩɟɪɟɦɟɧɧɨɣ = ɜɵɪɚɠɟɧɢɟ;
Ȼɨɥɟɟ ɩɨɞɪɨɛɧɚɹ ɢɧɮɨɪɦɚɰɢɹ ɨ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɢɯ ɩɟɪɟɦɟɧɧɵɯ ɩɪɟɞɫɬɚɜɥɟɧɚ ɜ ɪɚɡɞɟɥɟ 2.3.
Ʉ ɫɢɫɬɟɦɧɵɦ ɩɟɪɟɦɟɧɧɵɦ ɜ ɨɩɟɪɚɬɨɪɟ SET ɦɨɠɧɨ ɨɛɪɚɳɚɬɶɫɹ ɤɚɤ ɤ ɢɦɹ_ɩɟɪɟɦɟɧɧɨɣ.
ɂɦɟɧɢ ɦɨɠɟɬ ɩɪɟɞɲɟɫɬɜɨɜɚɬɶ ɧɟɨɛɹɡɚɬɟɥɶɧɨɟ ɫɥɨɜɨ GLOBAL ɢɥɢ @@global. ɞɥɹ ɹɜɧɨɝɨ
ɭɤɚɡɚɧɢɹ, ɱɬɨ ɷɬɨ ɝɥɨɛɚɥɶɧɚɹ ɩɟɪɟɦɟɧɧɚɹ, ɥɢɛɨ SESSION ɢɥɢ @@session. – ɱɬɨ ɷɬɨ ɩɟɪɟɦɟɧɧɚɹ ɫɟɚɧɫɚ. LOCAL ɢ @@local. – ɫɢɧɨɧɢɦɵ SESSION ɢ @@session..
ȿɫɥɢ ɧɟ ɭɤɚɡɚɧ ɧɢ ɨɞɢɧ ɦɨɞɢɮɢɤɚɬɨɪ, SET ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɩɟɪɟɦɟɧɧɵɟ ɫɟɚɧɫɚ.
ɋɢɧɬɚɤɫɢɫ @@ɢɦɹ_ɩɟɪɟɦɟɧɧɨɣ ɞɥɹ ɫɢɫɬɟɦɧɵɯ ɩɟɪɟɦɟɧɧɵɯ ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ MySQL ɫ
ɰɟɥɶɸ ɨɛɟɫɩɟɱɟɧɢɹ ɫɨɜɦɟɫɬɢɦɨɫɬɢ ɫ ɧɟɤɨɬɨɪɵɦɢ ɞɪɭɝɢɦɢ ɫɢɫɬɟɦɚɦɢ ɛɚɡ ɞɚɧɧɵɯ.
298
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɉɪɢ ɭɫɬɚɧɨɜɤɟ ɧɟɫɤɨɥɶɤɢɯ ɫɢɫɬɟɦɧɵɯ ɩɟɪɟɦɟɧɧɵɯ ɜ ɨɞɧɨɦ ɨɩɟɪɚɬɨɪɟ, ɩɨɫɥɟɞɧɹɹ ɨɩɰɢɹ
GLOBAL ɢɥɢ SESSION ɩɪɢɦɟɧɹɟɬɫɹ ɞɥɹ ɩɟɪɟɦɟɧɧɵɯ, ɞɥɹ ɤɨɬɨɪɵɯ ɦɨɞɢɮɢɤɚɬɨɪ ɧɟ ɭɤɚɡɚɧ.
SET
SET
SET
SET
SET
sort_buffer_size=10000;
@@local.sort_buffer_size=10000;
GLOBAL sort_buffer_size=1000000, SESSION sort_buffer_size=1000000;
@@sort_buffer_size=1000000;
@@global.sort_buffer_size=1000000, @@local.sort_buffer_size=1000000;
ȿɫɥɢ ɜɵ ɭɫɬɚɧɚɜɥɢɜɚɟɬɟ ɫɢɫɬɟɦɧɭɸ ɩɟɪɟɦɟɧɧɭɸ, ɢɫɩɨɥɶɡɭɹ SESSION (ɩɨ ɭɦɨɥɱɚɧɢɸ),
ɟɟ ɡɧɚɱɟɧɢɟ ɨɫɬɚɟɬɫɹ ɜ ɫɢɥɟ ɞɨ ɤɨɧɰɚ ɫɟɚɧɫɚ ɥɢɛɨ ɞɨ ɬɨɝɨ ɦɨɦɟɧɬɚ, ɤɨɝɞɚ ɟɣ ɛɭɞɟɬ ɩɪɢɫɜɨɟɧɨ ɞɪɭɝɨɟ ɡɧɚɱɟɧɢɟ. ȿɫɥɢ ɜɵ ɭɫɬɚɧɚɜɥɢɜɚɟɬɟ ɫɢɫɬɟɦɧɭɸ ɩɟɪɟɦɟɧɧɭɸ, ɭɤɚɡɚɜ ɫɥɨɜɨ
GLOBAL, ɱɬɨ ɬɪɟɛɭɟɬ ɩɪɢɜɢɥɟɝɢɢ SUPER, ɟɟ ɡɧɚɱɟɧɢɟ ɡɚɩɨɦɢɧɚɟɬɫɹ ɢ ɢɫɩɨɥɶɡɭɟɬɫɹ ɜ ɧɨɜɵɯ
ɫɨɟɞɢɧɟɧɢɹɯ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɫɟɪɜɟɪ ɧɟ ɛɭɞɟɬ ɩɟɪɟɡɚɩɭɳɟɧ. ȿɫɥɢ ɜɵ ɯɨɬɢɬɟ ɫɞɟɥɚɬɶ ɭɫɬɚɧɨɜɤɭ ɩɟɪɟɦɟɧɧɨɣ ɧɟɢɡɦɟɧɹɟɦɨɣ, ɜɵ ɞɨɥɠɧɵ ɩɨɦɟɫɬɢɬɶ ɟɟ ɜ ɮɚɣɥ ɨɩɰɢɣ.
ɑɬɨɛɵ ɩɪɟɞɨɬɜɪɚɬɢɬɶ ɧɟɩɪɚɜɢɥɶɧɨɟ ɢɫɩɨɥɶɡɨɜɚɧɢɟ, MySQL ɝɟɧɟɪɢɪɭɟɬ ɨɲɢɛɤɭ, ɟɫɥɢ
ɜɵ ɩɪɢɦɟɧɹɟɬɟ SET GLOBAL ɫ ɩɟɪɟɦɟɧɧɨɣ, ɤɨɬɨɪɭɸ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɬɨɥɶɤɨ ɫ SET
SESSION, ɥɢɛɨ ɟɫɥɢ ɜɵ ɧɟ ɭɤɚɡɵɜɚɟɬɟ GLOBAL, ɭɫɬɚɧɚɜɥɢɜɚɹ ɡɧɚɱɟɧɢɟ ɝɥɨɛɚɥɶɧɨɣ ɩɟɪɟɦɟɧɧɨɣ.
ȿɫɥɢ ɜɵ ɯɨɬɢɬɟ ɭɫɬɚɧɨɜɢɬɶ ɩɟɪɟɦɟɧɧɭɸ ɫɟɚɧɫɚ ɜ ɡɧɚɱɟɧɢɟ GLOBAL ɢɥɢ ɡɧɚɱɟɧɢɟ
GLOBAL ɜ ɡɚɪɚɧɟɟ ɫɤɨɦɩɢɥɢɪɨɜɚɧɧɨɟ ɜ MySQL ɡɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ, ɦɨɠɟɬɟ ɭɫɬɚɧɚɜɥɢɜɚɬɶ ɢɯ ɜ DEFAULT. ɇɚɩɪɢɦɟɪ, ɫɥɟɞɭɸɳɢɟ ɞɜɚ ɨɩɟɪɚɬɨɪɚ ɢɞɟɧɬɢɱɧɵ ɢ ɭɫɬɚɧɚɜɥɢɜɚɸɬ
ɩɟɪɟɦɟɧɧɭɸ ɫɟɚɧɫɚ max_join_size ɜ ɝɥɨɛɚɥɶɧɨɟ ɡɧɚɱɟɧɢɟ:
SET max_join_size=DEFAULT;
SET @@session.max_join_size=@@global.max_join_size;
ɋɩɢɫɨɤ ɛɨɥɶɲɢɧɫɬɜɚ ɫɢɫɬɟɦɧɵɯ ɩɟɪɟɦɟɧɧɵɯ ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɨɩɟɪɚɬɨɪɨɦ SHOW
VARIABLES. ɋɦ. ɪɚɡɞɟɥ 6.5.3.19. ɑɬɨɛɵ ɩɨɥɭɱɢɬɶ ɨɬɞɟɥɶɧɭɸ ɩɟɪɟɦɟɧɧɭɸ ɩɨ ɢɦɟɧɢ, ɢɥɢ ɫɩɢɫɨɤ
ɩɟɪɟɦɟɧɧɵɯ ɫ ɢɦɟɧɚɦɢ, ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɦɢ ɲɚɛɥɨɧɭ, ɜɨɫɩɨɥɶɡɭɣɬɟɫɶ ɤɨɧɫɬɪɭɤɰɢɟɣ LIKE:
SHOW VARIABLES LIKE 'max_join_size';
SHOW GLOBAL VARIABLES LIKE 'max_join_size';
ȼɵ ɬɚɤɠɟ ɦɨɠɟɬɟ ɩɨɥɭɱɢɬɶ ɡɧɚɱɟɧɢɟ ɫɩɟɰɢɮɢɱɟɫɤɨɣ ɩɟɪɟɦɟɧɧɨɣ ɫ ɩɨɦɨɳɶɸ ɫɢɧɬɚɤɫɢɫɚ @@[global.|local.]ɢɦɹ_ɩɟɪɟɦɟɧɧɨɣ ɜ ɨɩɟɪɚɬɨɪɟ SELECT:
SELECT @@max_join_size, @@global.max_join_size;
Ʉɨɝɞɚ ɜɵ ɡɚɩɪɚɲɢɜɚɟɬɟ ɩɟɪɟɦɟɧɧɭɸ ɜ ɨɩɟɪɚɬɨɪɟ SELECT @@ɢɦɹ_ɩɟɪɟɦɟɧɧɨɣ (ɬɨ ɟɫɬɶ,
ɛɟɡ ɭɤɚɡɚɧɢɹ global., session., local.), MySQL ɜɨɡɜɪɚɳɚɟɬ ɡɧɚɱɟɧɢɟ SESSION, ɟɫɥɢ ɨɧɨ
ɫɭɳɟɫɬɜɭɟɬ, ɢ GLOBAL – ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ.
ȼ ɫɥɟɞɭɸɳɟɦ ɫɩɢɫɤɟ ɩɪɟɞɫɬɚɜɥɟɧɵ ɩɟɪɟɦɟɧɧɵɟ, ɤɨɬɨɪɵɟ ɢɦɟɸɬ ɧɟɫɬɚɧɞɚɪɬɧɵɣ ɫɢɧɬɚɤɫɢɫ ɥɢɛɨ ɧɟ ɩɪɢɜɨɞɹɬɫɹ ɜ ɫɩɢɫɤɟ ɫɢɫɬɟɦɧɵɯ ɩɟɪɟɦɟɧɧɵɯ ɜ ɤɧɢɝɟ MySQL. Ɋɭɤɨɜɨɞɫɬɜɨ ɚɞɦɢɧɢɫɬɪɚɬɨɪɚ (Ɇ. : ɂɡɞɚɬɟɥɶɫɤɢɣ ɞɨɦ “ȼɢɥɶɹɦɫ”, 2005, ISBN 5-8459-0805-1).
ɇɟɫɦɨɬɪɹ ɧɚ ɬɨ ɱɬɨ ɷɬɢ ɩɟɪɟɦɟɧɧɵɟ ɧɟ ɨɬɨɛɪɚɠɚɸɬɫɹ ɱɟɪɟɡ SHOW VARIABLES, ɜɵ ɦɨɠɟɬɟ
ɩɨɥɭɱɢɬɶ ɢɯ ɡɧɚɱɟɧɢɹ ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ SELECT (ɡɚ ɢɫɤɥɸɱɟɧɢɟɦ CHARACTER SET ɢɥɢ
NAMES). ɇɚɩɪɢɦɟɪ:
mysql> SELECT @@AUTOCOMMIT;
+--------------+
| @@autocommit |
+--------------+
|
1 |
+--------------+
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
299
„AUTOCOMMIT = {0 | 1}.
ɍɫɬɚɧɚɜɥɢɜɚɟɬ ɪɟɠɢɦ ɚɜɬɨɦɚɬɢɱɟɫɤɨɝɨ ɡɚɜɟɪɲɟɧɢɹ ɬɪɚɧɡɚɤɰɢɣ. ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɨ ɡɧɚɱɟɧɢɟ 1, ɜɫɟ ɢɡɦɟɧɟɧɢɹ ɜ ɞɚɧɧɵɯ ɬɚɛɥɢɰ ɜɫɬɭɩɚɸɬ ɜ
ɫɢɥɭ ɧɟɦɟɞɥɟɧɧɨ. ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɨ ɡɧɚɱɟɧɢɟ 0, ɜɵ ɨɛɹɡɚɧɵ ɡɚɜɟɪɲɢɬɶ ɬɪɚɧɡɚɤɰɢɸ ɹɜɧɨ ɨɩɟɪɚɬɨɪɨɦ COMMIT ɥɢɛɨ ɨɬɦɟɧɢɬɶ ɟɟ ɫ ɩɨɦɨɳɶɸ ROLLBACK. ȿɫɥɢ ɜɵ ɩɟɪɟɤɥɸɱɚɟɬɟ AUTOCOMMIT ɫ 0 ɧɚ 1, MySQL ɜɵɩɨɥɧɹɟɬ ɚɜɬɨɦɚɬɢɱɟɫɤɢɣ COMMIT ɞɥɹ
ɜɫɟɯ ɚɤɬɢɜɧɵɯ ɬɪɚɧɡɚɤɰɢɣ. Ⱦɪɭɝɨɣ ɫɩɨɫɨɛ ɧɚɱɚɬɶ ɬɪɚɧɡɚɤɰɢɸ – ɷɬɨ ɢɫɩɨɥɶɡɨɜɚɬɶ
ɨɩɟɪɚɬɨɪ START TRANSACTION ɢɥɢ BEGIN. ɋɦ. ɪɚɡɞɟɥ 6.4.1.
„BIG_TABLES = {0 | 1}.
ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɚ ɜ 1, ɜɫɟ ɜɪɟɦɟɧɧɵɟ ɬɚɛɥɢɰɵ ɫɨɯɪɚɧɹɸɬɫɹ ɧɚ ɞɢɫɤɟ ɜɦɟɫɬɨ ɬɨɝɨ, ɱɬɨɛɵ ɫɨɯɪɚɧɹɬɶɫɹ ɜ ɩɚɦɹɬɢ. ɗɬɨ ɧɟɦɧɨɝɨ ɦɟɞɥɟɧɧɟɟ, ɨɞɧɚɤɨ ɩɪɢ ɜɵɩɨɥɧɟɧɢɢ ɨɩɟɪɚɬɨɪɚ SELECT, ɤɨɬɨɪɵɣ ɬɪɟɛɭɟɬ ɫɨɡɞɚɧɢɹ ɛɨɥɶɲɨɣ ɜɪɟɦɟɧɧɨɣ ɬɚɛɥɢɰɵ, ɧɟ ɜɨɡɧɢɤɚɟɬ ɨɲɢɛɤɚ The table ɢɦɹ_ɬɚɛɥɢɰɵ is full (Ɍɚɛɥɢɰɚ
ɢɦɹ_ɬɚɛɥɢɰɵ ɩɟɪɟɩɨɥɧɟɧɚ). Ɂɧɚɱɟɧɢɟɦ ɩɨ ɭɦɨɥɱɚɧɢɸ ɞɥɹ ɧɨɜɵɯ ɫɨɟɞɢɧɟɧɢɣ ɹɜɥɹɟɬɫɹ 0 (ɪɚɡɦɟɳɚɬɶ ɜɪɟɦɟɧɧɵɟ ɬɚɛɥɢɰɵ ɜ ɩɚɦɹɬɢ). ɇɚɱɢɧɚɹ ɫ MySQL 4.0, ɜɚɦ,
ɤɚɤ ɩɪɚɜɢɥɨ, ɧɟ ɩɪɢɞɟɬɫɹ ɭɫɬɚɧɚɜɥɢɜɚɬɶ ɷɬɭ ɩɟɪɟɦɟɧɧɭɸ, ɩɨɫɤɨɥɶɤɭ MySQL ɫɚɦ ɜ
ɫɥɭɱɚɟ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɩɪɟɨɛɪɚɡɭɟɬ ɜɪɟɦɟɧɧɵɟ ɬɚɛɥɢɰɵ ɜ ɩɚɦɹɬɢ ɜ ɬɚɛɥɢɰɵ ɧɚ
ɞɢɫɤɟ. Ɋɚɧɟɟ ɷɬɚ ɩɟɪɟɦɟɧɧɚɹ ɧɚɡɵɜɚɥɚɫɶ SQL_BIG_TABLES.
„CHARACTER SET {ɢɦɹ_ɧɚɛɨɪɚ_ɫɢɦɜɨɥɨɜ | DEFAULT}.
ɗɬɨ ɩɟɪɟɞɚɟɬ ɜɫɟ ɫɬɪɨɤɢ ɨɬ ɤɥɢɟɧɬɚ ɢ ɨɛɪɚɬɧɨ ɫ ɡɚɞɚɧɧɵɦ ɨɬɨɛɪɚɠɟɧɢɟɦ. Ⱦɨ ɜɟɪɫɢɢ MySQL 4.1 ɟɞɢɧɫɬɜɟɧɧɵɦ ɞɨɩɭɫɬɢɦɵɦ ɡɧɚɱɟɧɢɟɦ ɞɥɹ ɢɦɹ_ɧɚɛɨɪɚ_ɫɢɦɜɨɥɨɜ ɛɵɥɨ cp1551_koi8, ɨɞɧɚɤɨ ɜɵ ɦɨɠɟɬɟ ɞɨɛɚɜɢɬɶ ɧɨɜɵɟ ɜɚɪɢɚɧɬɵ ɨɬɨɛɪɚɠɟɧɢɹ, ɪɟɞɚɤɬɢɪɭɹ ɮɚɣɥ sql/convert.cc ɢɫɯɨɞɧɨɝɨ ɞɢɫɬɪɢɛɭɬɢɜɚ MySQL. ɇɚɱɢɧɚɹ ɫ MySQL 4.1, SET CHARACTER SET ɭɫɬɚɧɚɜɥɢɜɚɟɬ
ɬɪɢ ɫɢɫɬɟɦɧɵɯ ɩɟɪɟɦɟɧɧɵɯ: character_set_client ɢ character_set_results ɭɫɬɚɧɚɜɥɢɜɚɸɬɫɹ ɜ ɡɚɞɚɧɧɵɣ ɧɚɛɨɪ ɫɢɦɜɨɥɨɜ, ɚ character_set_connection – ɜ ɡɧɚɱɟɧɢɟ character_set_database.
Ɉɬɨɛɪɚɠɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ ɦɨɠɟɬ ɛɵɬɶ ɜɨɫɫɬɚɧɨɜɥɟɧɨ ɭɤɚɡɚɧɢɟɦ DEFAULT.
ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɫɢɧɬɚɤɫɢɫ SET CHARACTER SET ɨɬɥɢɱɚɟɬɫɹ ɨɬ ɩɪɢɧɹɬɨɝɨ ɞɥɹ
ɭɫɬɚɧɨɜɤɢ ɞɪɭɝɢɯ ɛɨɥɶɲɢɧɫɬɜɚ ɞɪɭɝɢɯ ɨɩɰɢɣ.
„FOREIGN_KEY_CHECKS = {0 | 1}. ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɚ ɜ 1 (ɩɨ ɭɦɨɥɱɚɧɢɸ), ɨɝɪɚɧɢɱɟɧɢɹ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ ɞɥɹ InnoDB ɤɨɧɬɪɨɥɢɪɭɸɬɫɹ ɫɟɪɜɟɪɨɦ. ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɨ ɜ 0,
ɢɝɧɨɪɢɪɭɸɬɫɹ. Ɉɬɤɥɸɱɟɧɢɟ ɩɪɨɜɟɪɨɤ ɜɧɟɲɧɢɯ ɤɥɸɱɟɣ ɦɨɠɟɬ ɨɤɚɡɚɬɶɫɹ ɭɞɨɛɧɵɦ
ɞɥɹ ɩɟɪɟɡɚɝɪɭɡɤɢ ɬɚɛɥɢɰ InnoDB ɜ ɩɨɪɹɞɤɟ, ɨɬɥɢɱɚɸɳɟɦɫɹ ɨɬ ɬɨɝɨ, ɤɨɬɨɪɵɣ ɬɪɟɛɭɸɬ ɢɯ ɨɬɧɨɲɟɧɢɹ ɪɨɞɢɬɟɥɶɫɤɢɣ/ɞɨɱɟɪɧɢɣ. ɗɬɚ ɩɟɪɟɦɟɧɧɚɹ ɛɵɥɚ ɞɨɛɚɜɥɟɧɚ ɜ
MySQL 3.23.52.
„IDENTITY = ɡɧɚɱɟɧɢɟ. ɗɬɚ ɩɟɪɟɦɟɧɧɚɹ – ɫɢɧɨɧɢɦ LAST_INSERT_ID. ȼɜɟɞɟɧɚ ɫ ɰɟɥɶɸ ɞɨɫɬɢɠɟɧɢɹ ɫɨɜɦɟɫɬɢɦɨɫɬɢ ɫ ɞɪɭɝɢɦɢ ɛɚɡɚɦɢ ɞɚɧɧɵɯ. ɇɚɱɢɧɚɹ ɫ MySQL
3.23.25, ɜɵ ɦɨɠɟɬɟ ɩɨɥɭɱɢɬɶ ɟɟ ɡɧɚɱɟɧɢɟ ɫ ɩɨɦɨɳɶɸ SELECT @IDENTITY. ɇɚɱɢɧɚɹ ɫ
MySQL 4.0.3, ɜɵ ɬɚɤɠɟ ɦɨɠɟɬɟ ɭɫɬɚɧɨɜɢɬɶ ɟɟ ɡɧɚɱɟɧɢɟ ɱɟɪɟɡ SET IDENTITY.
„INSERT_ID = ɡɧɚɱɟɧɢɟ.
ɍɫɬɚɧɚɜɥɢɜɚɟɬ ɡɧɚɱɟɧɢɟ, ɤɨɬɨɪɨɟ ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɧɨ ɫɥɟɞɭɸɳɢɦ ɨɩɟɪɚɬɨɪɨɦ INSERT ɢɥɢ ALTER TABLE ɞɥɹ ɜɫɬɚɜɤɢ ɜ ɫɬɨɥɛɟɰ AUTO_INCREMENT.
ȼ ɨɫɧɨɜɧɨɦ ɷɬɨ ɢɫɩɨɥɶɡɭɟɬɫɹ ɫ ɛɢɧɚɪɧɵɦ ɠɭɪɧɚɥɨɦ.
„LAST_INSERT_ID = ɡɧɚɱɟɧɢɟ. ɍɫɬɚɧɚɜɥɢɜɚɟɬ ɡɧɚɱɟɧɢɟ, ɤɨɬɨɪɨɟ ɛɭɞɟɬ ɜɨɡɜɪɚɳɟɧɨ
LAST_INSERT_ID() ɜ ɨɩɟɪɚɬɨɪɚɯ, ɨɛɧɨɜɥɹɸɳɢɯ ɬɚɛɥɢɰɭ. ɍɫɬɚɧɨɜɤɚ ɷɬɨɣ ɩɟɪɟɦɟɧɧɨɣ ɧɟ ɨɛɧɨɜɥɹɟɬ ɡɧɚɱɟɧɢɹ, ɜɨɡɜɪɚɳɚɟɦɨɝɨ ɮɭɧɤɰɢɟɣ C API mysql_insert_id().
„NAMES {'ɢɦɹ_ɧɚɛɨɪɚ_ɫɢɦɜɨɥɨɜ' | DEFAULT}
300
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
SET NAMES ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɬɪɢ ɫɢɫɬɟɦɧɵɟ ɩɟɪɟɦɟɧɧɵɟ ɫɟɚɧɫɚ: character_set_client,
character_set_connection ɢ character_set_result ɜ ɭɤɚɡɚɧɧɵɣ ɧɚɛɨɪ ɫɢɦɜɨɥɨɜ.
Ɉɬɨɛɪɚɠɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ ɦɨɠɟɬ ɛɵɬɶ ɜɨɫɫɬɚɧɨɜɥɟɧɨ, ɟɫɥɢ ɭɤɚɡɚɬɶ DEFAULT.
Ɉɛɪɚɬɢɬɟ ɜɧɢɦɚɧɢɟ, ɱɬɨ ɫɢɧɬɚɤɫɢɫ SET NAMES ɨɬɥɢɱɚɟɬɫɹ ɨɬ ɩɪɢɧɹɬɨɝɨ ɞɥɹ ɭɫɬɚ-
ɧɨɜɤɢ ɞɪɭɝɢɯ ɛɨɥɶɲɢɧɫɬɜɚ ɞɪɭɝɢɯ ɨɩɰɢɣ. ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɩɨɹɜɢɥɫɹ ɜ MySQL 4.1.0.
ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɚ ɜ 1 (ɩɨ ɭɦɨɥɱɚɧɢɸ), ɜɵ ɦɨɠɟɬɟ
ɧɚɣɬɢ ɩɨɫɥɟɞɧɸɸ ɜɫɬɚɜɥɟɧɧɭɸ ɜ ɬɚɛɥɢɰɭ ɫɬɪɨɤɭ, ɤɨɬɨɪɚɹ ɫɨɞɟɪɠɢɬ ɫɬɨɥɛɟɰ
AUTO_INCREMENT, ɢɫɩɨɥɶɡɭɹ ɫɥɟɞɭɸɳɭɸ ɤɨɧɫɬɪɭɤɰɢɸ:
„SQL_AUTO_IS_NULL = {0 | 1}.
WHERE ɫɬɨɥɛɟɰ_auto_increment IS NULL
ɗɬɨ ɩɨɜɟɞɟɧɢɟ ɯɚɪɚɤɬɟɪɧɨ ɞɥɹ ɧɟɤɨɬɨɪɵɯ ODBC-ɩɪɨɝɪɚɦɦ, ɬɚɤɢɯ ɤɚɤ Access.
SQL_AUTO_IS_NULL ɛɵɥɚ ɞɨɛɚɜɥɟɧɚ ɜ MySQL 3.23.52.
„SQL_BIG_SELECTS = {0 | 1}.
ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɚ ɜ 0, MySQL ɩɪɟɪɵɜɚɟɬ ɜɵɩɨɥɧɟɧɢɟ ɨɩɟɪɚɬɨɪɨɜ SELECT, ɤɨɬɨɪɵɟ ɩɪɟɞɩɨɥɨɠɢɬɟɥɶɧɨ ɛɭɞɭɬ ɜɵɩɨɥɧɹɬɶɫɹ ɞɨɥɝɨ (ɬɨ
ɟɫɬɶ ɨɩɟɪɚɬɨɪɨɜ, ɞɥɹ ɤɨɬɨɪɵɯ ɨɩɬɢɦɢɡɚɬɨɪ ɨɠɢɞɚɟɬ, ɱɬɨ ɤɨɥɢɱɟɫɬɜɨ ɩɪɨɜɟɪɹɟɦɵɯ
ɫɬɪɨɤ ɩɪɟɜɵɫɢɬ ɡɧɚɱɟɧɢɟ max_join_size). ɗɬɨ ɭɞɨɛɧɨ, ɤɨɝɞɚ ɩɪɢɦɟɧɹɸɬɫɹ ɧɟɪɚɰɢɨɧɚɥɶɧɵɟ ɭɫɥɨɜɢɹ ɜ ɤɨɧɫɬɪɭɤɰɢɢ WHERE. Ɂɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ ɷɬɨɣ ɩɟɪɟɦɟɧɧɨɣ ɞɥɹ ɧɨɜɵɯ ɫɨɟɞɢɧɟɧɢɣ ɪɚɜɧɨ 1, ɱɬɨ ɪɚɡɪɟɲɚɟɬ ɜɵɩɨɥɧɹɬɶ ɥɸɛɵɟ ɨɩɟɪɚɬɨɪɵ
SELECT. ȿɫɥɢ ɜɵ ɭɫɬɚɧɚɜɥɢɜɚɟɬɟ ɡɧɚɱɟɧɢɟ ɫɢɫɬɟɦɧɨɣ ɩɟɪɟɦɟɧɧɨɣ max_join_size ɜ
ɡɧɚɱɟɧɢɟ, ɨɬɥɢɱɧɨɟ ɨɬ DEFAULT, SQL_BIG_SELECTS ɩɪɢɧɢɦɚɟɬ ɡɧɚɱɟɧɢɟ 0.
„SQL_BUFFER_RESULT = {0 | 1}
SQL_BUFFER_RESULT ɩɪɢɧɭɠɞɚɟɬ ɨɩɟɪɚɬɨɪɵ SELECT ɩɨɦɟɳɚɬɶ ɪɟɡɭɥɶɬɚɬɵ ɜɨ ɜɪɟ-
ɦɟɧɧɵɟ ɬɚɛɥɢɰɵ. ɗɬɨ ɩɨɦɨɝɚɟɬ MySQL ɨɫɜɨɛɨɠɞɚɬɶ ɬɚɛɥɢɱɧɵɟ ɛɥɨɤɢɪɨɜɤɢ ɩɨɪɚɧɶɲɟ ɢ ɦɨɠɟɬ ɛɵɬɶ ɜɵɝɨɞɧɨ ɜ ɫɥɭɱɚɹɯ, ɤɨɝɞɚ ɬɪɟɛɭɟɬɫɹ ɦɧɨɝɨ ɜɪɟɦɟɧɢ ɞɥɹ ɨɬɩɪɚɜɤɢ ɪɟɡɭɥɶɬɚɬɨɜ ɤɥɢɟɧɬɚɦ. ɉɟɪɟɦɟɧɧɚɹ ɛɵɥɚ ɞɨɛɚɜɥɟɧɚ ɜ MySQL 3.23.13.
„SQL_LOG_BIN = {0 | 1}. ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɚ ɜ 0, ɧɢɤɚɤɨɣ ɛɢɧɚɪɧɨɣ ɪɟɝɢɫɬɪɚɰɢɢ ɞɥɹ
ɤɥɢɟɧɬɚ ɧɟ ɜɵɩɨɥɧɹɟɬɫɹ. Ʉɥɢɟɧɬ ɞɨɥɠɟɧ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ SUPER ɞɥɹ ɭɫɬɚɧɨɜɤɢ
ɷɬɨɣ ɨɩɰɢɢ. ɉɟɪɟɦɟɧɧɚɹ ɛɵɥɚ ɞɨɛɚɜɥɟɧɚ ɜ MySQL 3.23.16.
„SQL_LOG_OFF = {0 | 1}. ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɚ ɜ 1, ɪɟɝɢɫɬɪɚɰɢɹ ɡɚɩɪɨɫɨɜ ɤɥɢɟɧɬɚ ɜ
ɨɛɳɟɦ ɠɭɪɧɚɥɟ ɧɟ ɜɟɞɟɬɫɹ. Ʉɥɢɟɧɬ ɞɨɥɠɟɧ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ SUPER ɞɥɹ ɭɫɬɚɧɨɜɤɢ ɷɬɨɣ ɨɩɰɢɢ.
„SQL_LOG_UPDATE = {0 | 1}. ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɚ ɜ 0, ɪɟɝɢɫɬɪɚɰɢɹ ɨɛɧɨɜɥɟɧɢɣ ɤɥɢɟɧɬɚ ɜ ɠɭɪɧɚɥɟ ɧɟ ɜɟɞɟɬɫɹ. Ʉɥɢɟɧɬ ɞɨɥɠɟɧ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ SUPER ɞɥɹ ɭɫɬɚɧɨɜɤɢ
ɷɬɨɣ ɨɩɰɢɢ. ɉɟɪɟɦɟɧɧɚɹ ɛɵɥɚ ɞɨɛɚɜɥɟɧɚ ɜ MySQL 3.23.5. ɇɚɱɢɧɚɹ ɫ MySQL 5.0.0,
ɫɱɢɬɚɟɬɫɹ ɭɫɬɚɪɟɜɲɟɣ ɢ ɨɬɨɛɪɚɠɚɟɬɫɹ ɧɚ SQL_LOG_BIN.
„SQL_QUOTE_SHOW_CREATE = {0 | 1}.
ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɚ ɜ 1, SHOW CREATE TABLE ɜɵɜɨɞɢɬ ɢɦɟɧɚ ɬɚɛɥɢɰ ɢ ɫɬɨɥɛɰɨɜ ɜ ɤɚɜɵɱɤɚɯ. ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɚ ɜ 0, ɤɚɜɵɱɤɢ ɨɬɤɥɸɱɚɸɬɫɹ. ɉɨ ɭɦɨɥɱɚɧɢɸ ɷɬɚ ɨɩɰɢɹ ɜɤɥɸɱɟɧɚ, ɱɬɨɛɵ ɪɟɩɥɢɤɚɰɢɹ ɪɚɛɨɬɚɥɚ ɫ
ɬɚɛɥɢɰɚɦɢ ɢ ɫɬɨɥɛɰɚɦɢ ɜ ɤɚɜɵɱɤɚɯ. ɉɟɪɟɦɟɧɧɚɹ ɩɨɹɜɢɥɚɫɶ ɜ MySQL 3.23.26. ɋɦ.
ɪɚɡɞɟɥ 6.5.3.6.
„SQL_SAFE_UPDATES = {0 | 1}. ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɚ ɜ 1, MySQL ɩɪɟɪɵɜɚɟɬ ɨɩɟɪɚɬɨɪɵ
UPDATE ɢ DELETE, ɭ ɤɨɬɨɪɵɯ ɧɟ ɢɫɩɨɥɶɡɭɸɬɫɹ ɤɥɸɱɟɜɵɟ ɡɧɚɱɟɧɢɹ ɜ ɤɨɧɫɬɪɭɤɰɢɢ
WHERE. ɗɬɨ ɩɨɡɜɨɥɹɟɬ ɩɟɪɟɯɜɚɬɵɜɚɬɶ ɨɩɟɪɚɬɨɪɵ UPDATE ɢ DELETE, ɭ ɤɨɬɨɪɵɯ ɧɟɩɪɚɜɢɥɶɧɨ ɢɫɩɨɥɶɡɭɸɬɫɹ ɤɥɸɱɢ ɢ ɤɨɬɨɪɵɟ ɦɨɝɭɬ ɧɟɩɪɟɞɧɚɦɟɪɟɧɧɨ ɢɡɦɟɧɢɬɶ ɢɥɢ
ɭɞɚɥɢɬɶ ɛɨɥɶɲɨɟ ɤɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ. ɉɟɪɟɦɟɧɧɚɹ ɛɵɥɚ ɞɨɛɚɜɥɟɧɚ ɜ MySQL 3.22.32.
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
301
„SQL_SELECT_LIMIT = {ɡɧɚɱɟɧɢɟ | DEFAULT}.
Ɇɚɤɫɢɦɚɥɶɧɨɟ ɤɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ, ɤɨɬɨɪɵɟ ɦɨɠɟɬ ɜɟɪɧɭɬɶ ɨɩɟɪɚɬɨɪ SELECT. Ɂɧɚɱɟɧɢɟ ɩɨ ɭɦɨɥɱɚɧɢɸ ɞɥɹ ɧɨɜɵɯ ɩɨɞɤɥɸɱɟɧɢɣ – “ɧɟɨɝɪɚɧɢɱɟɧɧɨɟ” (unlimited). ȿɫɥɢ ɜɵ ɢɡɦɟɧɹɟɬɟ ɷɬɨ ɨɝɪɚɧɢɱɟɧɢɟ, ɡɧɚɱɟɧɢɟ
ɩɨ ɭɦɨɥɱɚɧɢɸ ɦɨɠɟɬ ɛɵɬɶ ɜɨɫɫɬɚɧɨɜɥɟɧɨ ɩɪɢɫɜɨɟɧɢɟɦ SQL_SELECT_LIMIT ɡɧɚɱɟɧɢɹ
DEFAULT.
„SQL_WARNINGS = {0 | 1}.
ɗɬɚ ɩɟɪɟɦɟɧɧɚɹ ɭɫɬɚɧɚɜɥɢɜɚɟɬ ɪɟɠɢɦ ɜɵɜɨɞɚ ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ ɨɞɧɨɫɬɪɨɱɧɵɦɢ ɨɩɟɪɚɬɨɪɚɦɢ INSERT. ɉɨ ɭɦɨɥɱɚɧɢɸ ɢɦɟɟɬ ɡɧɚɱɟɧɢɟ 0.
ɍɫɬɚɧɨɜɢɬɟ ɟɟ ɪɚɜɧɨɣ 1, ɱɬɨɛɵ ɝɟɧɟɪɢɪɨɜɚɬɶ ɫɬɪɨɤɢ ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ. ɉɟɪɟɦɟɧɧɚɹ
ɛɵɥɚ ɞɨɛɚɜɥɟɧɚ ɜ MySQL 3.22.11.
„TIMESTAMP = {ɡɧɚɱɟɧɢɟ_timestamp | DEFAULT}. ɍɫɬɚɧɚɜɥɢɜɚɟɬ ɬɟɤɭɳɟɟ ɜɪɟɦɹ ɞɥɹ
ɤɥɢɟɧɬɚ. ɉɪɢɦɟɧɹɟɬɫɹ ɞɥɹ ɩɨɥɭɱɟɧɢɹ ɨɪɢɝɢɧɚɥɶɧɨɣ ɜɪɟɦɟɧɧɨɣ ɦɟɬɤɢ ɩɪɢ ɢɫɩɨɥɶɡɨɜɚɧɢɢ ɛɢɧɚɪɧɨɝɨ ɠɭɪɧɚɥɚ ɞɥɹ ɜɨɫɫɬɚɧɨɜɥɟɧɢɹ ɫɬɪɨɤ. ɡɧɚɱɟɧɢɟ_timestamp
ɞɨɥɠɧɨ ɛɵɬɶ ɜɪɟɦɟɧɧɨɣ ɦɟɬɤɨɣ ɜ ɮɨɪɦɚɬɟ Unix, ɚ ɧɟ ɜɪɟɦɟɧɧɨɣ ɦɟɬɤɨɣ MySQL.
„UNIQUE_CHECKS = {0 | 1}. ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɚ ɜ 1 (ɤɚɤ ɩɨ ɭɦɨɥɱɚɧɢɸ), ɜɵɩɨɥɧɹɟɬɫɹ
ɩɪɨɜɟɪɤɚ ɭɧɢɤɚɥɶɧɨɫɬɢ ɜɬɨɪɢɱɧɵɯ ɢɧɞɟɤɫɨɜ ɜ ɬɚɛɥɢɰɚɯ InnoDB. ȿɫɥɢ ɭɫɬɚɧɨɜɥɟɧɚ
ɜ 0, ɧɢɤɚɤɢɯ ɩɪɨɜɟɪɨɤ ɭɧɢɤɚɥɶɧɨɫɬɢ ɧɟ ɜɵɩɨɥɧɹɟɬɫɹ. ɉɟɪɟɦɟɧɧɚɹ ɩɨɹɜɢɥɚɫɶ ɜ
MySQL 3.23.52.
6.5.3.2. Ñèíòàêñèñ SHOW CHARACTER SET
SHOW CHARACTER SET [LIKE 'ɲɚɛɥɨɧ']
Ɉɩɟɪɚɬɨɪ SHOW CHARACTER SET ɜɵɜɨɞɢɬ ɫɩɢɫɨɤ ɜɫɟɯ ɞɨɩɭɫɬɢɦɵɯ ɫɢɦɜɨɥɶɧɵɯ ɧɚɛɨɪɨɜ. Ɉɧ ɩɪɢɧɢɦɚɟɬ ɧɟɨɛɹɡɚɬɟɥɶɧɭɸ ɤɨɧɫɬɪɭɤɰɢɸ LIKE ɞɥɹ ɭɤɚɡɚɧɢɹ ɲɚɛɥɨɧɚ ɢɦɟɧ ɧɚɛɨɪɨɜ ɫɢɦɜɨɥɨɜ. ɇɚɩɪɢɦɟɪ:
mysql> SHOW CHARACTER SET LIKE 'latin%';
+---------+-----------------------------+-------------------+--------+
| Charset | Description
| Default collation | Maxlen |
+---------+-----------------------------+-------------------+--------+
| latin1 | ISO 8859-1 West European
| latin1_swedish_ci |
1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci |
1 |
| latin5 | ISO 8859-9 Turkish
| latin5_turkish_ci |
1 |
| latin7 | ISO 8859-13 Baltic
| latin7_general_ci |
1 |
+---------+-----------------------------+-------------------+--------+
ɋɬɨɥɛɟɰ Maxlen ɩɨɤɚɡɵɜɚɟɬ ɦɚɤɫɢɦɚɥɶɧɨɟ ɱɢɫɥɨ ɛɚɣɬ, ɧɟɨɛɯɨɞɢɦɵɯ ɞɥɹ ɯɪɚɧɟɧɢɹ
ɨɞɧɨɝɨ ɫɢɦɜɨɥɚ. SHOW CHARACTER SET ɞɨɫɬɭɩɟɧ, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.1.0.
6.5.3.3. Ñèíòàêñèñ SHOW COLLATION
SHOW COLLATION [LIKE 'ɲɚɛɥɨɧ']
ȼɵɜɨɞ SHOW COLLATION ɜɤɥɸɱɚɟɬ ɜɫɟ ɞɨɩɭɫɬɢɦɵɟ ɩɨɪɹɞɤɢ ɫɨɩɨɫɬɚɜɥɟɧɢɹ ɧɚɛɨɪɨɜ
ɫɢɦɜɨɥɨɜ. ɉɪɢɧɢɦɚɟɬ ɧɟɨɛɹɡɚɬɟɥɶɧɭɸ ɤɨɧɫɬɪɭɤɰɢɸ LIKE ɞɥɹ ɭɤɚɡɚɧɢɹ ɲɚɛɥɨɧɚ ɧɚɢɦɟɧɨɜɚɧɢɣ ɩɨɪɹɞɤɚ ɫɨɩɨɫɬɚɜɥɟɧɢɹ. ɇɚɩɪɢɦɟɪ:
mysql> SHOW COLLATION LIKE 'latin1%';
+-------------------+---------+----+---------+----------+---------+
| Collation
| Charset | Id | Default | Compiled | Sortlen |
+-------------------+---------+----+---------+----------+---------+
| latin1_german1_ci | latin1 | 5 |
|
|
0 |
302
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
| latin1_swedish_ci | latin1 | 8 | Yes
| Yes
|
0 |
| latin1_danish_ci | latin1 | 15 |
|
|
0 |
| latin1_german2_ci | latin1 | 31 |
| Yes
|
2 |
| latin1_bin
| latin1 | 47 |
| Yes
|
0 |
| latin1_general_ci | latin1 | 48 |
|
|
0 |
| latin1_general_cs | latin1 | 49 |
|
|
0 |
| latin1_spanish_ci | latin1 | 94 |
|
|
0 |
+-------------------+---------+----+---------+----------+---------+
ɋɬɨɥɛɟɰ Default ɩɨɤɚɡɵɜɚɟɬ, ɹɜɥɹɟɬɫɹ ɥɢ ɞɚɧɧɵɣ ɩɨɪɹɞɨɤ ɫɨɩɨɫɬɚɜɥɟɧɢɹ ɩɨɪɹɞɤɨɦ
ɩɨ ɭɦɨɥɱɚɧɢɸ ɞɥɹ ɞɚɧɧɨɝɨ ɧɚɛɨɪɚ ɫɢɦɜɨɥɨɜ. Compiled ɨɬɪɚɠɚɟɬ, ɹɜɥɹɟɬɫɹ ɥɢ ɞɚɧɧɵɣ
ɧɚɛɨɪ ɫɢɦɜɨɥɨɦ ɡɚɪɚɧɟɟ ɫɤɨɦɩɢɥɢɪɨɜɚɧɧɵɦ ɧɚ ɫɟɪɜɟɪɟ. Sortlen ɢɦɟɟɬ ɨɬɧɨɲɟɧɢɟ ɤ
ɨɛɴɟɦɭ ɩɚɦɹɬɢ, ɧɟɨɛɯɨɞɢɦɨɦɭ ɞɥɹ ɫɨɪɬɢɪɨɜɤɢ ɫɬɪɨɤ, ɩɪɟɞɫɬɚɜɥɟɧɧɵɯ ɜ ɞɚɧɧɨɦ ɧɚɛɨɪɟ
ɫɢɦɜɨɥɨɜ.
SHOW COLLATION ɞɨɫɬɭɩɟɧ, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.1.0.
6.5.3.4. Ñèíòàêñèñ SHOW COLUMNS
SHOW [FULL] COLUMNS FROM ɢɦɹ_ɬɚɛɥɢɰɵ [FROM ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ] [LIKE 'ɲɚɛɥɨɧ']
SHOW COLUMNS ɜɵɜɨɞɢɬ ɫɩɢɫɨɤ ɫɬɨɥɛɰɨɜ ɡɚɞɚɧɧɨɣ ɬɚɛɥɢɰɵ. ȿɫɥɢ ɬɢɩɵ ɫɬɨɥɛɰɨɜ ɨɬɥɢɱɚɸɬɫɹ ɨɬ ɬɟɯ, ɱɬɨ ɛɵɥɢ ɡɚɞɚɧɵ ɨɩɟɪɚɬɨɪɨɦ CREATE TABLE, ɢɦɟɣɬɟ ɜ ɜɢɞɭ, ɱɬɨ MySQL
ɢɧɨɝɞɚ ɢɡɦɟɧɹɟɬ ɬɢɩɵ ɫɬɨɥɛɰɨɜ ɩɪɢ ɫɨɡɞɚɧɢɢ ɢɥɢ ɢɡɦɟɧɟɧɢɢ ɫɬɪɭɤɬɭɪɵ ɬɚɛɥɢɰɵ. ɍɫɥɨɜɢɹ, ɩɪɢ ɤɨɬɨɪɵɯ ɷɬɨ ɩɪɨɢɫɯɨɞɢɬ, ɨɩɢɫɚɧɵ ɜ ɪɚɡɞɟɥɟ 6.2.5.2.
Ʉɥɸɱɟɜɨɟ ɫɥɨɜɨ FULL ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ, ɧɚɱɢɧɚɹ ɫ MySQL 3.23.32 ɢ ɜɵɲɟ. Ɉɧɨ
ɜɤɥɸɱɚɟɬ ɜ ɜɵɜɨɞ ɢɧɮɨɪɦɚɰɢɸ ɨ ɩɪɢɜɢɥɟɝɢɹɯ, ɤɨɬɨɪɵɟ ɜɵ ɢɦɟɟɬɟ ɞɥɹ ɞɨɫɬɭɩɚ ɤ ɞɚɧɧɵɦ
ɫɬɨɥɛɰɚɦ. ɇɚɱɢɧɚɹ ɫ MySQL 4.1, ɫɥɨɜɨ FULL ɬɚɤɠɟ ɜɤɥɸɱɚɟɬ ɜɵɜɨɞ ɜɫɟɯ ɤɨɦɦɟɧɬɚɪɢɟɜ ɨ
ɫɬɨɥɛɰɚɯ.
ȼɵ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.ɢɦɹ_ɬɚɛɥɢɰɵ ɤɚɤ ɚɥɶɬɟɪɧɚɬɢɜɭ ɫɢɧɬɚɤɫɢɫɭ
ɢɦɹ_ɬɚɛɥɢɰɵ FROM ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ. ɋɥɟɞɭɸɳɢɟ ɞɜɚ ɨɩɟɪɚɬɨɪɚ ɷɤɜɢɜɚɥɟɧɬɧɵ:
mysql> SHOW COLUMNS FROM mytable FROM mydb;
mysql> SHOW COLUMNS FROM mydb.mytable;
SHOW FIELDS – ɷɬɨ ɫɢɧɨɧɢɦ SHOW COLUMNS. ȼɵ ɬɚɤɠɟ ɦɨɠɟɬɟ ɜɵɜɟɫɬɢ ɫɩɢɫɨɤ ɫɬɨɥɛɰɨɜ
ɫ ɩɨɦɨɳɶɸ ɤɨɦɚɧɞɵ mysqlshow ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ ɢɦɹ_ɬɚɛɥɢɰɵ.
Ɉɩɟɪɚɬɨɪ DESCRIBE ɜɵɜɨɞɢɬ ɢɧɮɨɪɦɚɰɢɸ, ɩɨɞɨɛɧɭɸ SHOW COLUMNS. ɋɦ. ɪɚɡɞɟɥ 6.3.1.
6.5.3.5. Ñèíòàêñèñ SHOW CREATE DATABASE
SHOW CREATE DATABASE ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ
ȼɵɜɨɞɢɬ ɨɩɟɪɚɬɨɪ CREATE DATABASE, ɤɨɬɨɪɵɣ ɫɨɡɞɚɫɬ ɭɤɚɡɚɧɧɭɸ ɛɚɡɭ ɞɚɧɧɵɯ. Ⱦɨɛɚɜɥɟɧ ɜ MySQL 4.1.
mysql> SHOW CREATE DATABASE test\G
*************************** 1. row ***************************
Database: test
Create Database: CREATE DATABASE `test`
/*!40100 DEFAULT CHARACTER SET latin1 */
6.5.3.6. Ñèíòàêñèñ SHOW CREATE TABLE
SHOW CREATE TABLE ɢɦɹ_ɬɚɛɥɢɰɵ
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
303
ȼɵɜɨɞɢɬ ɨɩɟɪɚɬɨɪ CREATE TABLE, ɤɨɬɨɪɵɣ ɫɨɡɞɚɫɬ ɭɤɚɡɚɧɧɭɸ ɬɚɛɥɢɰɭ. Ⱦɨɛɚɜɥɟɧ ɜ
MySQL 3.23.20.
mysql> SHOW CREATE TABLE t\G
*************************** 1. row ***************************
Table: t
Create Table: CREATE TABLE t (
id INT(11) default NULL auto_increment,
s char(60) default NULL,
PRIMARY KEY (id)
) TYPE=MyISAM
SHOW CREATE TABLE ɡɚɤɥɸɱɚɟɬ ɜ ɤɚɜɵɱɤɢ ɢɦɟɧɚ ɬɚɛɥɢɰɵ ɢ ɫɬɨɥɛɰɨɜ ɜ ɫɨɨɬɜɟɬɫɬɜɢɢ
ɫɨ ɡɧɚɱɟɧɢɟɦ ɨɩɰɢɢ SQL_QUOTE_SHOW_CREATE. ɋɦ. ɪɚɡɞɟɥ 6.5.3.1.
6.5.3.7. Ñèíòàêñèñ SHOW DATABASES
SHOW DATABASES [LIKE 'ɲɚɛɥɨɧ']
SHOW DATABASES ɜɵɜɨɞɢɬ ɫɩɢɫɨɤ ɛɚɡ ɞɚɧɧɵɯ ɧɚ ɯɨɫɬɟ ɫɟɪɜɟɪɚ MySQL. ȼɵ ɬɚɤɠɟ ɦɨɠɟɬɟ ɩɨɥɭɱɢɬɶ ɷɬɨɬ ɫɩɢɫɨɤ ɫ ɩɨɦɨɳɶɸ ɤɨɦɚɧɞɵ mysqlshow. ɇɚɱɢɧɚɹ ɫ MySQL 4.0.2, ɜɵ
ɭɜɢɞɢɬɟ ɬɨɥɶɤɨ ɬɟ ɛɚɡɵ ɞɚɧɧɵɯ, ɞɥɹ ɞɨɫɬɭɩɚ ɤ ɤɨɬɨɪɵɦ ɢɦɟɟɬɟ ɤɚɤɢɟ-ɥɢɛɨ ɩɪɢɜɢɥɟɝɢɢ,
ɟɫɥɢ ɬɨɥɶɤɨ ɭ ɜɚɫ ɧɟɬ ɝɥɨɛɚɥɶɧɨɣ ɩɪɢɜɢɥɟɝɢɢ SHOW DATABASES.
ȿɫɥɢ ɫɟɪɜɟɪ ɡɚɩɭɳɟɧ ɫ ɨɩɰɢɟɣ --skip-show-database, ɜɵ ɧɟ ɫɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ
ɷɬɨɬ ɨɩɟɪɚɬɨɪ ɜɨɨɛɳɟ, ɟɫɥɢ ɧɟ ɢɦɟɟɬɟ ɩɪɢɜɢɥɟɝɢɢ SHOW DATABASES.
6.5.3.8. Ñèíòàêñèñ SHOW ENGINES
SHOW [STORAGE] ENGINES
SHOW ENGINES ɜɵɜɨɞɢɬ ɢɧɮɨɪɦɚɰɢɸ ɨ ɫɨɫɬɨɹɧɢɢ ɦɟɯɚɧɢɡɦɨɜ ɯɪɚɧɟɧɢɹ. ɗɬɨ ɨɫɨɛɟɧɧɨ
ɭɞɨɛɧɨ ɞɥɹ ɩɪɨɜɟɪɤɢ ɬɨɝɨ, ɤɚɤɢɟ ɦɟɯɚɧɢɡɦɵ ɯɪɚɧɟɧɢɹ ɩɨɞɞɟɪɠɢɜɚɸɬɫɹ ɫɟɪɜɟɪɨɦ, ɢɥɢ
ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɭɜɢɞɟɬɶ, ɤɚɤɨɣ ɦɟɯɚɧɢɡɦ ɢɫɩɨɥɶɡɭɟɬɫɹ ɩɨ ɭɦɨɥɱɚɧɢɸ. Ɉɩɟɪɚɬɨɪ ɪɟɚɥɢɡɨɜɚɧ ɜ MySQL 4.1.2. ɋɭɳɟɫɬɜɭɟɬ ɭɫɬɚɪɟɜɲɢɣ ɫɢɧɨɧɢɦ – SHOW TABLE TYPES.
mysql> SHOW ENGINES\G
*************************** 1. row ***************************
Type: MyISAM
Support: DEFAULT
Comment: Default type from 3.23 with great performance
*************************** 2. row ***************************
Type: HEAP
Support: YES
Comment: Hash based, stored in memory, useful for temporary tables
*************************** 3. row ***************************
Type: MEMORY
Support: YES
Comment: Alias for HEAP
*************************** 4. row ***************************
Type: MERGE
Support: YES
Comment: Collection of identical MyISAM tables
*************************** 5. row ***************************
304
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
Type: MRG_MYISAM
Support: YES
Comment: Alias for MERGE
*************************** 6. row ***************************
Type: ISAM
Support: NO
Comment: Obsolete table type; Is replaced by MyISAM
*************************** 7. row ***************************
Type: MRG_ISAM
Support: NO
Comment: Obsolete table type; Is replaced by MRG_MYISAM
*************************** 8. row ***************************
Type: InnoDB
Support: YES
Comment: Supports transactions, row-level locking and foreign keys
*************************** 9. row ***************************
Type: INNOBASE
Support: YES
Comment: Alias for INNODB
*************************** 10. row ***************************
Type: BDB
Support: YES
Comment: Supports transactions and page-level locking
*************************** 11. row ***************************
Type: BERKELEYDB
Support: YES
Comment: Alias for BDB
Ɂɧɚɱɟɧɢɟ Support ɩɨɤɚɡɵɜɚɟɬ, ɩɨɞɞɟɪɠɢɜɚɟɬɫɹ ɥɢ ɞɚɧɧɵɣ ɦɟɯɚɧɢɡɦ ɫɟɪɜɟɪɨɦ, ɢ ɤɚɤɨɣ ɢɡ ɧɢɯ ɹɜɥɹɟɬɫɹ ɦɟɯɚɧɢɡɦɨɦ ɩɨ ɭɦɨɥɱɚɧɢɸ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɫɟɪɜɟɪ ɡɚɩɭɳɟɧ ɫ ɨɩɰɢɟɣ --default-table-type=InnoDB, ɬɨ ɡɧɚɱɟɧɢɟɦ Support ɞɥɹ InnoDB ɛɭɞɟɬ DEFAULT.
6.5.3.9. Ñèíòàêñèñ SHOW ERRORS
SHOW ERRORS [LIMIT [ɫɦɟɳɟɧɢɟ,] ɤɨɥɢɱɟɫɬɜɨ_ɫɬɪɨɤ]
SHOW COUNT(*) ERRORS
ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɩɨɯɨɠ ɧɚ SHOW WARNINGS, ɫ ɬɟɦ ɨɬɥɢɱɢɟɦ, ɱɬɨ ɜɦɟɫɬɨ ɨɬɨɛɪɚɠɟɧɢɹ
ɨɲɢɛɨɤ, ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ ɢ ɩɪɢɦɟɱɚɧɢɣ, ɨɧ ɜɵɜɨɞɢɬ ɬɨɥɶɤɨ ɨɲɢɛɤɢ. SHOW ERRORS ɞɨɫɬɭɩɟɧ, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.1.0.
Ʉɨɧɫɬɪɭɤɰɢɹ LIMIT ɢɦɟɟɬ ɬɨɬ ɠɟ ɫɢɧɬɚɤɫɢɫ, ɱɬɨ ɢ ɜ ɨɩɟɪɚɬɨɪɟ SELECT. ɋɦ. ɪɚɡɞɟɥ
6.1.7.
Ɉɩɟɪɚɬɨɪ SHOW COUNT(*) ERRORS ɨɬɨɛɪɚɠɚɟɬ ɤɨɥɢɱɟɫɬɜɨ ɨɲɢɛɨɤ. ȼɵ ɬɚɤɠɟ ɦɨɠɟɬɟ
ɩɨɥɭɱɢɬɶ ɷɬɨ ɤɨɥɢɱɟɫɬɜɨ ɢɡ ɩɟɪɟɦɟɧɧɨɣ error_count:
SHOW COUNT(*) ERRORS;
SELECT @@error_count;
Ȼɨɥɟɟ ɩɨɞɪɨɛɧɭɸ ɢɧɮɨɪɦɚɰɢɸ ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɜ ɪɚɡɞɟɥɟ 6.5.3.20.
6.5.3.10. Ñèíòàêñèñ SHOW GRANTS
SHOW GRANTS FOR ɩɨɥɶɡɨɜɚɬɟɥɶ
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
305
ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɜɵɜɨɞɢɬ ɨɩɟɪɚɬɨɪɵ GRANT, ɤɨɬɨɪɵɟ ɞɨɥɠɧɵ ɛɵɬɶ ɜɵɩɨɥɧɟɧɵ ɞɥɹ ɞɭɛɥɢɪɨɜɚɧɢɹ ɧɚɛɨɪɚ ɩɪɢɜɢɥɟɝɢɣ ɩɨɥɶɡɨɜɚɬɟɥɹ MySQL.
mysql> SHOW GRANTS FOR 'root'@'localhost';
+---------------------------------------------------------------------+
| Grants for root@localhost
|
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
ɇɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.1.2, ɱɬɨɛɵ ɩɨɥɭɱɢɬɶ ɫɩɢɫɨɤ ɩɪɢɜɢɥɟɝɢɣ ɬɟɤɭɳɟɝɨ ɫɟɚɧɫɚ,
ɦɨɠɧɨ ɜɨɫɩɨɥɶɡɨɜɚɬɶɫɹ ɥɸɛɵɦ ɢɡ ɩɟɪɟɱɢɫɥɟɧɧɵɯ ɧɢɠɟ ɨɩɟɪɚɬɨɪɨɜ:
SHOW GRANTS;
SHOW GRANTS FOR CURRENT_USER;
SHOW GRANTS FOR CURRENT_USER();
Ⱦɨ MySQL 4.1.2 ɭɡɧɚɬɶ, ɤɚɤɨɣ ɩɨɥɶɡɨɜɚɬɟɥɶ ɛɵɥ ɚɭɬɟɧɬɢɮɢɰɢɪɨɜɚɧ ɜ ɬɟɤɭɳɟɦ ɫɟɚɧɫɟ, ɦɨɠɧɨ ɛɵɥɨ ɫ ɩɨɦɨɳɶɸ ɮɭɧɤɰɢɸ CURRENT_USER() (ɧɨɜɚɹ ɜ MySQL 4.0.6). Ɂɚɬɟɦ ɩɨɥɭɱɟɧɧɨɟ ɡɧɚɱɟɧɢɟ ɧɭɠɧɨ ɛɵɥɨ ɭɤɚɡɚɬɶ ɜ ɨɩɟɪɚɬɨɪɟ SHOW GRANTS. ɋɦ. ɪɚɡɞɟɥ 5.8.3.
SHOW GRANTS ɞɨɫɬɭɩɟɧ, ɧɚɱɢɧɚɹ ɫ MySQL 3.23.4.
6.5.3.11. Ñèíòàêñèñ SHOW INDEX
SHOW INDEX FROM ɢɦɹ_ɬɚɛɥɢɰɵ [FROM ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ]
SHOW INDEX ɜɨɡɜɪɚɳɚɟɬ ɢɧɮɨɪɦɚɰɢɸ ɨɛ ɢɧɞɟɤɫɚɯ ɬɚɛɥɢɰɵ ɜ ɮɨɪɦɚɬɟ, ɩɨɞɨɛɧɨɦ ɬɨɦɭ, ɱɬɨ ɜɵɞɚɟɬ ɜɵɡɨɜ SQLStatistics ɜ ODBC.
SHOW INDEX ɜɨɡɜɪɚɳɚɟɬ ɫɥɟɞɭɸɳɢɟ ɩɨɥɹ:
„Table.
ɂɦɹ ɬɚɛɥɢɰɵ.
„Non_unique. 0, ɟɫɥɢ ɢɧɞɟɤɫ ɧɟ ɦɨɠɟɬ ɢɦɟɬɶ ɞɭɛɥɢɪɨɜɚɧɧɵɯ ɤɥɸɱɟɣ, 1 – ɟɫɥɢ ɦɨɠɟɬ.
„Key_name.
ɂɦɹ ɢɧɞɟɤɫɚ.
„Seq_in_index.
„Column_name.
ɉɨɪɹɞɨɤ ɫɬɨɥɛɰɚ ɜ ɤɥɸɱɟ ɢɧɞɟɤɫɚ, ɧɚɱɢɧɚɹ ɫ 1.
ɂɦɹ ɫɬɨɥɛɰɚ.
„Collation.
Ʉɚɤ ɫɬɨɥɛɟɰ ɫɨɪɬɢɪɭɟɬɫɹ ɜ ɢɧɞɟɤɫɟ. ȼ MySQL ɦɨɠɟɬ ɢɦɟɬɶ ɡɧɚɱɟɧɢɹ
‘A’ (ascending – ɩɨ ɜɨɡɪɚɫɬɚɧɢɸ) ɢɥɢ NULL (ɧɟ ɫɨɪɬɢɪɨɜɚɧɨ).
„Cardinality.
Ʉɨɥɢɱɟɫɬɜɨ ɭɧɢɤɚɥɶɧɵɯ ɡɧɚɱɟɧɢɣ ɜ ɢɧɞɟɤɫɟ. ɗɬɨ ɨɛɧɨɜɥɹɟɬɫɹ ɨɩɟɪɚɬɨɪɨɦ ANALYZE TABLE ɢɥɢ ɤɨɦɚɧɞɨɣ myisamchk -a. Cardinality ɪɚɫɫɱɢɬɵɜɚɟɬɫɹ
ɧɚ ɛɚɡɟ ɫɬɚɬɢɫɬɢɤɢ, ɯɪɚɧɢɬɫɹ ɜ ɜɢɞɟ ɰɟɥɨɝɨ ɱɢɫɥɚ, ɩɨɷɬɨɦɭ ɧɟɬ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɜ
ɛɨɥɶɲɨɣ ɬɨɱɧɨɫɬɢ ɞɥɹ ɦɚɥɟɧɶɤɢɯ ɬɚɛɥɢɰ.
„Sub_part. Ʉɨɥɢɱɟɫɬɜɨ ɩɪɨɢɧɞɟɤɫɢɪɨɜɚɧɧɵɯ ɫɢɦɜɨɥɨɜ ɫɬɨɥɛɰɚ, ɟɫɥɢ ɤɥɸɱ ɩɨɫɬɪɨɟɧ ɧɚ ɱɚɫɬɢ ɫɬɨɥɛɰɚ. ȿɫɥɢ ɜɫɹ ɫɬɨɥɛɟɰ ɹɜɥɹɟɬɫɹ ɤɥɸɱɨɦ ɢɧɞɟɤɫɚ, ɩɪɢɧɢɦɚɟɬ ɡɧɚɱɟɧɢɟ NULL.
„Packed.
„Null.
ɉɨɤɚɡɵɜɚɟɬ, ɭɩɚɤɨɜɚɧ ɥɢ ɢɧɞɟɤɫ. ȿɫɥɢ ɧɟɬ – NULL.
ɋɨɞɟɪɠɢɬ YES, ɟɫɥɢ ɫɬɨɥɛɟɰ ɞɨɩɭɫɤɚɟɬ ɡɧɚɱɟɧɢɟ NULL.
„Index_type.
„Comment.
ɂɫɩɨɥɶɡɨɜɚɧɧɵɣ ɦɟɬɨɞ ɢɧɞɟɤɫɚɰɢɢ (BTREE, FULLTEXT, HASH, RTREE).
Ɋɚɡɥɢɱɧɵɟ ɡɚɦɟɱɚɧɢɹ. Ⱦɨ MySQL 4.0.2, ɤɨɝɞɚ ɛɵɥ ɞɨɛɚɜɥɟɧ ɫɬɨɥɛɟɰ
Index_type, Comment ɩɨɤɚɡɵɜɚɟɬ, ɹɜɥɹɟɬɫɹ ɥɢ ɢɧɞɟɤɫ FULLTEXT.
306
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɋɬɨɥɛɰɵ Packed ɢ Comment ɩɨɹɜɢɥɢɫɶ ɜ ɜɟɪɫɢɢ MySQL 3.23.0. ɋɬɨɥɛɰɵ Null ɢ
Index_type ɛɵɥɢ ɞɨɛɚɜɥɟɧɵ ɜ MySQL 4.0.2.
ȼ ɤɚɱɟɫɬɜɟ ɚɥɶɬɟɪɧɚɬɢɜɵ ɫɢɧɬɚɤɫɢɫɚ ɢɦɹ_ɬɚɛɥɢɰɵ FROM ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ ɦɨɠɧɨ ɢɫɩɨɥɶɡɨɜɚɬɶ ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.ɢɦɹ_ɬɚɛɥɢɰɵ. ɋɥɟɞɭɸɳɢɟ ɞɜɚ ɨɩɟɪɚɬɨɪɚ ɷɤɜɢɜɚɥɟɧɬɧɵ:
mysql> SHOW INDEX FROM mytable FROM mydb;
mysql> SHOW INDEX FROM mydb.mytable;
SHOW KEYS – ɫɢɧɨɧɢɦ ɞɥɹ SHOW INDEX. ɋɩɢɫɨɤ ɢɧɞɟɤɫɨɜ ɦɨɠɧɨ ɬɚɤɠɟ ɩɨɥɭɱɢɬɶ ɩɨ
ɤɨɦɚɧɞɟ mysqlshow -k ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ ɢɦɹ_ɬɚɛɥɢɰɵ.
6.5.3.12. Ñèíòàêñèñ SHOW INNODB STATUS
SHOW INNODB STATUS
ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɩɨɤɚɡɵɜɚɟɬ ɩɨɞɪɨɛɧɭɸ ɢɧɮɨɪɦɚɰɢɸ ɨ ɫɨɫɬɨɹɧɢɢ ɦɟɯɚɧɢɡɦɚ ɯɪɚɧɟɧɢɹ
InnoDB.
6.5.3.13. Ñèíòàêñèñ SHOW LOGS
SHOW [BDB] LOGS
SHOW LOGS ɜɵɜɨɞɢɬ ɢɧɮɨɪɦɚɰɢɸ ɨ ɫɭɳɟɫɬɜɭɸɳɢɯ ɠɭɪɧɚɥɶɧɵɯ ɮɚɣɥɚɯ. Ɉɩɟɪɚɬɨɪ
ɛɵɥ ɪɟɚɥɢɡɨɜɚɧ ɜ MySQL 3.23.29. ȼ ɧɚɫɬɨɹɳɢɣ ɦɨɦɟɧɬ ɨɧ ɨɬɨɛɪɚɠɚɟɬ ɬɨɥɶɤɨ ɢɧɮɨɪɦɚɰɢɸ ɨ ɠɭɪɧɚɥɶɧɵɯ ɮɚɣɥɚɯ Berkley DB, ɩɨɷɬɨɦɭ ɩɫɟɜɞɨɧɢɦɨɦ ɞɥɹ ɧɟɝɨ (ɞɨɫɬɭɩɧɵɦ ɫ
MySQL 4.1.1) ɹɜɥɹɟɬɫɹ SHOW BDB LOGS.
SHOW LOGS ɜɨɡɜɪɚɳɚɟɬ ɫɥɟɞɭɸɳɢɟ ɩɨɥɹ:
„File.
ɉɨɥɧɵɣ ɩɭɬɶ ɤ ɮɚɣɥɭ ɠɭɪɧɚɥɚ.
„Type.
Ɍɢɩ ɮɚɣɥɚ ɠɭɪɧɚɥɚ (BDB ɞɥɹ ɠɭɪɧɚɥɨɜ Berkley DB).
„Status.
ɋɨɫɬɨɹɧɢɟ ɮɚɣɥɚ ɠɭɪɧɚɥɚ (FREE, ɟɫɥɢ ɮɚɣɥ ɦɨɠɟɬ ɛɵɬɶ ɭɞɚɥɟɧ, ɢɥɢ IN
USE, ɟɫɥɢ ɮɚɣɥ ɧɟɨɛɯɨɞɢɦ ɩɨɞɫɢɫɬɟɦɟ ɨɛɪɚɛɨɬɤɢ ɬɪɚɧɡɚɤɰɢɣ).
6.5.3.14. Ñèíòàêñèñ SHOW PRIVILEGES
SHOW PRIVILEGES
SHOW PRIVILEGES ɜɵɜɨɞɢɬ ɫɩɢɫɨɤ ɫɢɫɬɟɦɧɵɯ ɩɪɢɜɢɥɟɝɢɣ, ɩɨɞɞɟɪɠɢɜɚɟɦɵɯ ɫɟɪɜɟɪɨɦ
MySQL. ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɪɟɚɥɢɡɨɜɚɧ ɜ MySQL 4.1.0.
mysql> SHOW PRIVILEGES\G
*************************** 1. row ***************************
Privilege: Select
Context: Tables
Comment: To retrieve rows from table
*************************** 2. row ***************************
Privilege: Insert
Context: Tables
Comment: To insert data into tables
*************************** 3. row ***************************
Privilege: Update
Context: Tables
Comment: To update existing rows
*************************** 4. row ***************************
Privilege: Delete
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
307
Context: Tables
Comment: To delete existing rows
*************************** 5. row ***************************
Privilege: Index
Context: Tables
Comment: To create or drop indexes
*************************** 6. row ***************************
Privilege: Alter
Context: Tables
Comment: To alter the table
*************************** 7. row ***************************
Privilege: Create
Context: Databases,Tables,Indexes
Comment: To create new databases and tables
*************************** 8. row ***************************
Privilege: Drop
Context: Databases,Tables
Comment: To drop databases and tables
*************************** 9. row ***************************
Privilege: Grant
Context: Databases,Tables
Comment: To give to other users those privileges you possess
*************************** 10. row ***************************
Privilege: References
Context: Databases,Tables
Comment: To have references on tables
*************************** 11. row ***************************
Privilege: Reload
Context: Server Admin
Comment: To reload or refresh tables, logs and privileges
*************************** 12. row ***************************
Privilege: Shutdown
Context: Server Admin
Comment: To shutdown the server
*************************** 13. row ***************************
Privilege: Process
Context: Server Admin
Comment: To view the plain text of currently executing queries
*************************** 14. row ***************************
Privilege: File
Context: File access on server
Comment: To read and write files on the server
6.5.3.15. Ñèíòàêñèñ SHOW PROCESSLIST
SHOW [FULL] PROCESSLIST
SHOW PROCESSLIST ɜɵɜɨɞɢɬ ɫɩɢɫɨɤ ɪɚɛɨɬɚɸɳɢɯ ɩɨɬɨɤɨɜ ɫɟɪɜɟɪɚ. ɗɬɭ ɠɟ ɢɧɮɨɪɦɚɰɢɸ ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɤɨɦɚɧɞɨɣ mysqladmin processlist. ȿɫɥɢ ɭ ɜɚɫ ɟɫɬɶ ɩɪɢɜɢɥɟɝɢɹ
SUPER, ɜɵ ɦɨɠɟɬɟ ɜɢɞɟɬɶ ɜɫɟ ɩɨɬɨɤɢ, ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ – ɬɨɥɶɤɨ ɫɜɨɢ ɫɨɛɫɬɜɟɧɧɵɟ (ɬɨ
ɟɫɬɶ ɩɨɬɨɤɢ, ɚɫɫɨɰɢɢɪɨɜɚɧɧɵɟ ɫ ɭɱɟɬɧɨɣ ɡɚɩɢɫɶɸ ɩɨɥɶɡɨɜɚɬɟɥɹ MySQL, ɩɨɞ ɤɨɬɨɪɨɣ ɜɵ
308
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɜɨɲɥɢ). ɋɦ. ɪɚɡɞɟɥ 6.5.4.3. ȿɫɥɢ ɧɟ ɭɤɚɡɚɧɨ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ FULL, ɜɵɜɨɞɹɬɫɹ ɬɨɥɶɤɨ ɩɟɪɜɵɟ 100 ɫɢɦɜɨɥɨɜ ɤɚɠɞɨɝɨ ɡɚɩɪɨɫɚ.
ɇɚɱɢɧɚɹ ɫ MySQL 4.0.12, ɷɬɨɬ ɨɩɟɪɚɬɨɪ ɫɨɨɛɳɚɟɬ ɢɦɟɧɚ ɯɨɫɬɨɜ ɞɥɹ ɩɨɞɤɥɸɱɟɧɢɣ
TCP/IP ɜ ɮɨɪɦɚɬɟ ɢɦɹ_ɯɨɫɬɚ:ɩɨɪɬ_ɤɥɢɟɧɬɚ, ɱɬɨɛɵ ɦɨɠɧɨ ɛɵɥɨ ɩɨɧɹɬɶ, ɤɚɤɨɣ ɤɥɢɟɧɬ ɱɬɨ
ɞɟɥɚɟɬ.
ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɨɱɟɧɶ ɭɞɨɛɟɧ, ɟɫɥɢ ɜɵ ɩɨɥɭɱɚɟɬɟ ɫɨɨɛɳɟɧɢɟ ɨɛ ɨɲɢɛɤɟ ɧɚɩɨɞɨɛɢɟ
“ɫɥɢɲɤɨɦ ɦɧɨɝɨ ɩɨɞɤɥɸɱɟɧɢɣ” ɢ ɯɨɬɢɬɟ ɪɚɡɨɛɪɚɬɶɫɹ ɜ ɬɨɦ, ɱɬɨ ɩɪɨɢɫɯɨɞɢɬ. MySQL
ɪɟɡɟɪɜɢɪɭɟɬ ɞɨɩɨɥɧɢɬɟɥɶɧɨɟ ɩɨɞɤɥɸɱɟɧɢɟ ɞɥɹ ɭɱɟɬɧɨɣ ɡɚɩɢɫɢ ɫ ɩɪɢɜɢɥɟɝɢɟɣ SUPER,
ɱɬɨɛɵ ɜɫɟɝɞɚ ɞɚɜɚɬɶ ɜɨɡɦɨɠɧɨɫɬɶ ɚɞɦɢɧɢɫɬɪɚɬɨɪɭ ɩɨɞɤɥɸɱɢɬɶɫɹ ɢ ɩɪɨɜɟɪɢɬɶ ɫɢɫɬɟɦɭ
(ɩɪɟɞɩɨɥɚɝɚɟɬɫɹ, ɱɬɨ ɜɵ ɧɟ ɞɚɟɬɟ ɷɬɭ ɩɪɢɜɢɥɟɝɢɸ ɜɫɟɦ ɩɨɥɶɡɨɜɚɬɟɥɹɦ).
ɇɢɠɟ ɨɩɢɫɚɧɵ ɧɟɤɨɬɨɪɵɟ ɫɨɫɬɨɹɧɢɹ, ɤɨɬɨɪɵɟ ɨɛɵɱɧɨ ɦɨɠɧɨ ɭɜɢɞɟɬɶ ɜ ɜɵɜɨɞɟ SHOW
PROCESSLIST:
„Checking table.
ɉɨɬɨɤ ɜɵɩɨɥɧɹɟɬ (ɚɜɬɨɦɚɬɢɱɟɫɤɭɸ) ɩɪɨɜɟɪɤɭ ɬɚɛɥɢɰɵ.
„Closing table.
Ɉɡɧɚɱɚɟɬ, ɱɬɨ ɩɨɬɨɤ ɫɛɪɚɫɵɜɚɟɬ ɢɡɦɟɧɟɧɧɵɟ ɞɚɧɧɵɟ ɬɚɛɥɢɰɵ ɧɚ
ɞɢɫɤ ɢ ɡɚɤɪɵɜɚɟɬ ɢɫɩɨɥɶɡɭɟɦɵɟ ɬɚɛɥɢɰɵ. ɗɬɨ ɞɨɥɠɧɨ ɛɵɬɶ ɛɵɫɬɪɨɣ ɨɩɟɪɚɰɢɟɣ.
ȿɫɥɢ ɠɟ ɧɟɬ, ɜɚɦ ɫɬɨɢɬ ɩɪɨɜɟɪɢɬɶ, ɧɟ ɩɟɪɟɩɨɥɧɟɧ ɥɢ ɞɢɫɤ ɢ ɧɟ ɫɥɢɲɤɨɦ ɥɢ ɢɧɬɟɧɫɢɜɧɨ ɨɧ ɢɫɩɨɥɶɡɭɟɬɫɹ.
„Connect Out. ɉɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɪɟɩɥɢɤɚɰɢɢ ɩɨɞɤɥɸɱɚɟɬɫɹ ɤ ɝɥɚɜɧɨɦɭ.
„Copying to tmp table on disk.
ȼɪɟɦɟɧɧɵɣ ɪɟɡɭɥɶɬɢɪɭɸɳɢɣ ɧɚɛɨɪ ɨɤɚɡɚɥɫɹ ɛɨɥɶɲɟ tmp_table_size ɢ ɩɨɬɨɤ ɩɟɪɟɧɨɫɢɬ ɜɪɟɦɟɧɧɭɸ ɬɚɛɥɢɰɭ ɢɡ ɩɚɦɹɬɢ ɧɚ ɞɢɫɤ ɞɥɹ
ɷɤɨɧɨɦɢɢ ɩɚɦɹɬɢ.
„Creating tmp file. ɉɨɬɨɤ ɫɨɡɞɚɟɬ ɜɪɟɦɟɧɧɵɣ ɮɚɣɥ ɞɥɹ ɫɨɯɪɚɧɟɧɢɹ ɱɚɫɬɢ ɪɟɡɭɥɶɬɚɬɚ ɡɚɩɪɨɫɚ.
„Deleting from main table. ɉɨɬɨɤ ɜɵɩɨɥɧɹɟɬ ɩɟɪɜɭɸ ɱɚɫɬɶ ɨɩɟɪɚɰɢɢ ɦɧɨɝɨɬɚɛɥɢɱɧɨɝɨ ɭɞɚɥɟɧɢɹ, ɭɞɚɥɹɹ ɞɚɧɧɵɟ ɢɡ ɩɟɪɜɨɣ ɬɚɛɥɢɰɵ.
„Deleting from reference table. ɉɨɬɨɤ ɜɵɩɨɥɧɹɟɬ ɜɬɨɪɭɸ ɱɚɫɬɶ ɨɩɟɪɚɰɢɢ ɦɧɨɝɨɬɚɛɥɢɱɧɨɝɨ ɭɞɚɥɟɧɢɹ, ɭɞɚɥɹɹ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɫɬɪɨɤɢ ɢɡ ɞɪɭɝɢɯ ɬɚɛɥɢɰ.
„Flushing tables. ɉɨɬɨɤ ɜɵɩɨɥɧɹɟɬ FLUSH TABLES ɢ ɨɠɢɞɚɟɬ, ɤɨɝɞɚ ɜɫɟ ɩɨɬɨɤɢ ɡɚɤɪɨɸɬ ɫɜɨɢ ɬɚɛɥɢɰɵ.
„Killed. Ʉɬɨ-ɬɨ ɩɨɫɥɚɥ ɤɨɦɚɧɞɭ ɩɪɟɪɵɜɚɧɢɹ ɩɨɬɨɤɚ, ɢ ɨɧ ɞɨɥɠɟɧ ɛɵɬɶ ɩɪɟɪɜɚɧ ɩɪɢ
ɫɥɟɞɭɸɳɟɣ ɩɪɨɜɟɪɤɟ ɮɥɚɝɚ ɭɞɚɥɟɧɢɹ ɩɨɬɨɤɚ. ɗɬɨɬ ɮɥɚɝ ɩɪɨɜɟɪɹɟɬɫɹ ɧɚ ɤɚɠɞɨɦ
ɲɚɝɟ ɨɫɧɨɜɧɨɝɨ ɰɢɤɥɚ MySQL, ɧɨ ɢɧɨɝɞɚ ɷɬɨ ɦɨɠɟɬ ɩɨɬɪɟɛɨɜɚɬɶ ɧɟɤɨɬɨɪɨɝɨ ɜɪɟɦɟɧɢ ɧɚ ɩɪɟɪɵɜɚɧɢɟ ɩɨɬɨɤɚ. ȿɫɥɢ ɩɨɬɨɤ ɛɥɨɤɢɪɨɜɚɧ ɤɚɤɢɦ-ɬɨ ɞɪɭɝɢɦ ɩɨɬɨɤɨɦ,
ɩɪɟɪɵɜɚɧɢɟ ɩɪɨɢɡɨɣɞɟɬ ɧɟɦɟɞɥɟɧɧɨ ɩɨɫɥɟ ɨɫɜɨɛɨɠɞɟɧɢɹ ɛɥɨɤɢɪɨɜɤɢ.
„Sending data. ɉɨɬɨɤ ɨɛɪɚɛɚɬɵɜɚɟɬ ɫɬɪɨɤɢ ɞɥɹ ɨɩɟɪɚɬɨɪɚ SELECT ɢ ɨɬɩɪɚɜɥɹɟɬ ɢɯ
ɤɥɢɟɧɬɭ.
„Sorting for group. ɉɨɬɨɤ ɜɵɩɨɥɧɹɟɬ ɫɨɪɬɢɪɨɜɤɭ ɞɥɹ ɭɞɨɜɥɟɬɜɨɪɟɧɢɹ ɭɫɥɨɜɢɸ
GROUP BY.
„Sorting for order.
ɉɨɬɨɤ ɜɵɩɨɥɧɹɟɬ ɫɨɪɬɢɪɨɜɤɭ ɞɥɹ ɭɞɨɜɥɟɬɜɨɪɟɧɢɹ ɭɫɥɨɜɢɸ
ORDER BY.
„Opening tables.
ɉɨɬɨɤ ɩɵɬɚɟɬɫɹ ɨɬɤɪɵɬɶ ɬɚɛɥɢɰɭ. ɗɬɨ ɞɨɥɠɧɨ ɛɵɬɶ ɨɱɟɧɶ ɛɵɫɬɪɨɣ ɨɩɟɪɚɰɢɟɣ, ɟɫɥɢ ɬɨɥɶɤɨ ɧɢɱɬɨ ɧɟ ɦɟɲɚɟɬ ɨɬɤɪɵɬɢɸ. ɇɚɩɪɢɦɟɪ, ɨɩɟɪɚɬɨɪɵ
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
309
ALTER TABLE ɢɥɢ LOCK TABLE ɦɨɝɭɬ ɩɪɟɞɨɬɜɪɚɬɢɬɶ ɨɬɤɪɵɬɢɟ ɬɚɛɥɢɰ ɞɨ ɫɜɨɟɝɨ ɡɚ-
ɜɟɪɲɟɧɢɹ.
„Removing duplicates.
Ɂɚɩɪɨɫ ɢɫɩɨɥɶɡɨɜɚɥ SELECT DISTINCT ɬɚɤɢɦ ɨɛɪɚɡɨɦ, ɱɬɨ
MySQL ɧɟ ɦɨɠɟɬ ɨɩɬɢɦɢɡɢɪɨɜɚɬɶ ɨɩɟɪɚɰɢɸ DISTINCT ɧɚ ɪɚɧɧɟɣ ɫɬɚɞɢɢ. ɂɡ-ɡɚ
ɷɬɨɝɨ MySQL ɩɨɬɪɟɛɨɜɚɥɚɫɶ ɞɨɩɨɥɧɢɬɟɥɶɧɚɹ ɫɬɚɞɢɹ ɞɥɹ ɭɞɚɥɟɧɢɹ ɜɫɟɯ ɞɭɛɥɢɪɨɜɚɧɧɵɯ ɫɬɪɨɤ ɢɡ ɪɟɡɭɥɶɬɢɪɭɸɳɟɝɨ ɧɚɛɨɪɚ ɩɟɪɟɞ ɨɬɩɪɚɜɤɨɣ ɟɝɨ ɤɥɢɟɧɬɭ.
„Reopen table. ɉɨɬɨɤ ɩɨɥɭɱɢɥ ɛɥɨɤɢɪɨɜɤɭ ɬɚɛɥɢɰɵ, ɧɨ ɩɨɫɥɟ ɷɬɨɝɨ ɛɵɥ ɢɡɜɟɳɟɧ ɨ
ɬɨɦ, ɱɬɨ ɫɬɪɭɤɬɭɪɚ ɬɚɛɥɢɰɵ ɢɡɦɟɧɢɥɚɫɶ. Ɉɧ ɨɫɜɨɛɨɠɞɚɟɬ ɛɥɨɤɢɪɨɜɤɭ, ɡɚɤɪɵɜɚɟɬ
ɬɚɛɥɢɰɭ, ɚ ɡɚɬɟɦ ɜɧɨɜɶ ɨɬɤɪɵɜɚɟɬ ɟɟ.
„Repair by sorting. Ʉɨɞ ɜɨɫɫɬɚɧɨɜɥɟɧɢɹ ɢɫɩɨɥɶɡɭɟɬ ɫɨɪɬɢɪɨɜɤɭ ɞɥɹ ɫɨɡɞɚɧɢɹ ɢɧɞɟɤɫɨɜ.
„Repair with keycache. Ʉɨɞ ɜɨɫɫɬɚɧɨɜɥɟɧɢɹ ɢɫɩɨɥɶɡɭɟɬ ɫɨɡɞɚɧɢɟ ɤɥɸɱɟɣ ɨɞɧɨɝɨ
ɡɚ ɞɪɭɝɢɦ ɩɨ ɤɷɲɭ ɤɥɸɱɟɣ. ɗɬɨ ɩɪɨɢɫɯɨɞɢɬ ɧɚɦɧɨɝɨ ɦɟɞɥɟɧɧɟɟ, ɱɟɦ Repair by
sorting.
„Searching rows for update.
ɉɨɬɨɤ ɜɵɩɨɥɧɹɟɬ ɩɟɪɜɭɸ ɮɚɡɭ ɩɨɢɫɤɚ ɜɫɟɯ ɩɨɞɥɟɠɚɳɢɯ ɨɛɧɨɜɥɟɧɢɸ ɫɬɪɨɤ. ɗɬɨ ɞɨɥɠɧɨ ɛɵɬɶ ɫɞɟɥɚɧɨ, ɟɫɥɢ UPDATE ɢɡɦɟɧɹɟɬ ɡɧɚɱɟɧɢɹ ɤɥɸɱɚ ɢɧɞɟɤɫɚ, ɤɨɬɨɪɵɣ ɢɫɩɨɥɶɡɭɟɬɫɹ ɞɥɹ ɩɨɢɫɤɚ ɢɡɦɟɧɹɟɦɵɯ ɫɬɪɨɤ.
„Sleeping. ɉɨɬɨɤ ɨɠɢɞɚɟɬ ɧɨɜɨɝɨ ɨɩɟɪɚɬɨɪɚ ɨɬ ɤɥɢɟɧɬɚ.
„System lock.
ɉɨɬɨɤ ɨɠɢɞɚɟɬ ɩɨɥɭɱɟɧɢɹ ɜɧɟɲɧɟɣ ɫɢɫɬɟɦɧɨɣ ɛɥɨɤɢɪɨɜɤɢ ɬɚɛɥɢɰɵ.
ȿɫɥɢ ɜɵ ɧɟ ɢɫɩɨɥɶɡɭɟɬɟ ɫɪɚɡɭ ɧɟɫɤɨɥɶɤɨ ɫɟɪɜɟɪɨɜ mysqld ɧɚ ɨɞɧɨɣ ɦɚɲɢɧɟ, ɤɨɬɨɪɵɟ ɨɫɭɳɟɫɬɜɥɹɸɬ ɞɨɫɬɭɩ ɤ ɨɞɧɢɦ ɢ ɬɟɦ ɠɟ ɬɚɛɥɢɰɚɦ, ɦɨɠɟɬɟ ɨɬɤɥɸɱɢɬɶ ɜɧɟɲɧɢɟ ɛɥɨɤɢɪɨɜɤɢ, ɭɤɚɡɚɜ ɨɩɰɢɸ --skip-external-locking.
„Upgrading lock.
Ɉɛɪɚɛɨɬɱɢɤ ɨɬɥɨɠɟɧɧɵɯ ɜɫɬɚɜɨɤ INSERT DELAYED ɩɵɬɚɟɬɫɹ ɩɨɥɭɱɢɬɶ ɛɥɨɤɢɪɨɜɤɭ ɬɚɛɥɢɰɵ ɞɥɹ ɜɫɬɚɜɤɢ ɫɬɪɨɤ.
„Updating. ɉɨɬɨɤ ɢɳɟɬ ɫɬɪɨɤɢ ɞɥɹ ɨɛɧɨɜɥɟɧɢɹ ɢ ɨɛɧɨɜɥɹɟɬ ɢɯ.
„User lock.
ɉɨɬɨɤ ɨɠɢɞɚɟɬ ɜɨɡɜɪɚɬɚ GET_LOCK().
„Waiting for tables.
ɉɨɬɨɤ ɩɨɥɭɱɢɥ ɢɡɜɟɳɟɧɢɟ ɨ ɬɨɦ, ɱɬɨ ɫɬɪɭɤɬɭɪɚ ɬɚɛɥɢɰɵ ɢɡɦɟɧɢɥɚɫɶ ɢ ɧɭɠɞɚɟɬɫɹ ɜ ɩɨɜɬɨɪɧɨɦ ɨɬɤɪɵɬɢɢ ɬɚɛɥɢɰɵ, ɱɬɨɛɵ ɩɨɥɭɱɢɬɶ ɧɨɜɭɸ
ɫɬɪɭɤɬɭɪɭ. Ɉɞɧɚɤɨ ɱɬɨɛɵ ɛɵɬɶ ɝɨɬɨɜɵɦ ɩɨɜɬɨɪɧɨ ɨɬɤɪɵɬɶ ɬɚɛɥɢɰɭ, ɩɨɬɨɤ ɜɵɧɭɠɞɟɧ ɨɠɢɞɚɬɶ, ɤɨɝɞɚ ɜɫɟ ɨɫɬɚɥɶɧɵɟ ɩɨɬɨɤɢ ɡɚɤɪɨɸɬ ɬɚɛɥɢɰɭ.
Ɍɚɤɨɟ ɢɡɜɟɳɟɧɢɟ ɩɨɫɬɭɩɚɟɬ, ɤɨɝɞɚ ɞɪɭɝɨɣ ɩɨɬɨɤ ɢɫɩɨɥɶɡɭɟɬ FLUSH TABLES, ɥɢɛɨ ɜ
ɨɛɪɚɛɨɬɤɟ ɧɚɯɨɞɢɬɫɹ ɨɞɢɧ ɢɡ ɫɥɟɞɭɸɳɢɯ ɨɩɟɪɚɬɨɪɨɜ: FLUSH TABLES ɢɦɹ_ɬɚɛɥɢɰɵ,
ALTER TABLE, RENAME TABLE, REPAIR TABLE, ANALYZE TABLE ɢɥɢ OPTIMIZE TABLE.
„Waiting for handler insert.
Ɉɛɪɚɛɨɬɱɢɤ ɨɬɥɨɠɟɧɧɵɯ ɜɫɬɚɜɨɤ INSERT DELAYED
ɨɛɪɚɛɨɬɚɥ ɜɫɟ ɨɬɥɨɠɟɧɧɵɟ ɜɫɬɚɜɤɢ ɢ ɨɠɢɞɚɟɬ ɧɨɜɵɯ.
Ȼɨɥɶɲɢɧɫɬɜɨ ɫɨɫɬɨɹɧɢɣ ɫɨɨɬɜɟɬɫɬɜɭɸɬ ɨɱɟɧɶ ɛɵɫɬɪɵɦ ɨɩɟɪɚɰɢɹɦ. ȿɫɥɢ ɩɨɬɨɤ ɨɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɜ ɨɞɧɨɦ ɢɡ ɷɬɢɯ ɫɨɫɬɨɹɧɢɣ ɧɚ ɦɧɨɝɨ ɫɟɤɭɧɞ, ɷɬɨ ɦɨɠɟɬ ɝɨɜɨɪɢɬɶ ɨ ɩɪɨɛɥɟɦɟ,
ɤɨɬɨɪɭɸ ɧɟɨɛɯɨɞɢɦɨ ɢɫɫɥɟɞɨɜɚɬɶ.
ɋɭɳɟɫɬɜɭɟɬ ɪɹɞ ɞɪɭɝɢɯ ɫɨɫɬɨɹɧɢɣ, ɧɟ ɩɟɪɟɱɢɫɥɟɧɧɵɯ ɜ ɩɪɟɞɵɞɭɳɟɦ ɫɩɢɫɤɟ, ɨɞɧɚɤɨ
ɦɧɨɝɢɟ ɢɯ ɧɢɯ ɩɪɢɦɟɧɹɸɬɫɹ ɬɨɥɶɤɨ ɩɪɢ ɩɨɢɫɤɟ ɨɲɢɛɨɤ ɜ ɤɨɞɟ ɫɟɪɜɟɪɚ.
6.5.3.16. Ñèíòàêñèñ SHOW STATUS
SHOW STATUS [LIKE 'ɲɚɛɥɨɧ']
310
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
SHOW STATUS ɜɵɜɨɞɢɬ ɢɧɮɨɪɦɚɰɢɸ ɨ ɫɨɫɬɨɹɧɢɢ ɫɟɪɜɟɪɚ. ɗɬɭ ɠɟ ɢɧɮɨɪɦɚɰɢɸ ɦɨɠɧɨ
ɩɨɥɭɱɢɬɶ ɤɨɦɚɧɞɨɣ mysqladmin extended-status.
ɑɚɫɬɢɱɧɵɣ ɜɵɜɨɞ ɩɪɟɞɫɬɚɜɥɟɧ ɧɢɠɟ. ɇɚ ɜɚɲɟɦ ɫɟɪɜɟɪɟ ɫɩɢɫɨɤ ɩɟɪɟɦɟɧɧɵɯ ɢ ɢɯ
ɡɧɚɱɟɧɢɣ ɦɨɠɟɬ ɨɬɥɢɱɚɬɶɫɹ. ɋɦɵɫɥ ɤɚɠɞɨɣ ɩɟɪɟɦɟɧɧɨɣ ɩɨɞɪɨɛɧɨ ɨɩɢɫɚɧ ɜ ɤɧɢɝɟ
MySQL. Ɋɭɤɨɜɨɞɫɬɜɨ ɚɞɦɢɧɢɫɬɪɚɬɨɪɚ (Ɇ. : ɂɡɞɚɬɟɥɶɫɤɢɣ ɞɨɦ “ȼɢɥɶɹɦɫ”, 2005, ISBN
5-8459-0805-1).
mysql> SHOW STATUS;
+--------------------------+------------+
| Variable_name
| Value
|
+--------------------------+------------+
| Aborted_clients
| 0
|
| Aborted_connects
| 0
|
| Bytes_received
| 155372598 |
| Bytes_sent
| 1176560426 |
| Connections
| 30023
|
| Created_tmp_disk_tables | 0
|
| Created_tmp_tables
| 8340
|
| Created_tmp_files
| 60
|
...
| Open_tables
| 1
|
| Open_files
| 2
|
| Open_streams
| 0
|
| Opened_tables
| 44600
|
| Questions
| 2026873
|
...
| Table_locks_immediate
| 1920382
|
| Table_locks_waited
| 0
|
| Threads_cached
| 0
|
| Threads_created
| 30022
|
| Threads_connected
| 1
|
| Threads_running
| 1
|
| Uptime
| 80380
|
+--------------------------+------------+
ɋ ɤɨɧɫɬɪɭɤɰɢɟɣ LIKE ɨɩɟɪɚɬɨɪ ɜɵɜɨɞɢɬ ɬɨɥɶɤɨ ɬɟ ɩɟɪɟɦɟɧɧɵɟ, ɢɦɟɧɚ ɤɨɬɨɪɵɯ ɫɨɨɬɜɟɬɫɬɜɭɸɬ ɲɚɛɥɨɧɭ:
mysql> SHOW STATUS LIKE 'Key%';
+--------------------+----------+
| Variable_name
| Value
|
+--------------------+----------+
| Key_blocks_used
| 14955
|
| Key_read_requests | 96854827 |
| Key_reads
| 162040
|
| Key_write_requests | 7589728 |
| Key_writes
| 3813196 |
+--------------------+----------+
6.5.3.17. Ñèíòàêñèñ SHOW TABLE STATUS
SHOW TABLE STATUS [FROM ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ] [LIKE 'ɲɚɛɥɨɧ']
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
311
SHOW TABLE STATUS (ɧɨɜɵɣ ɨɩɟɪɚɬɨɪ ɜ MySQL 3.23) ɪɚɛɨɬɚɟɬ ɩɨɞɨɛɧɨ SHOW TABLE, ɧɨ
ɜɵɜɨɞɢɬ ɛɨɥɶɲɨɣ ɨɛɴɟɦ ɢɧɮɨɪɦɚɰɢɢ ɨ ɤɚɠɞɨɣ ɬɚɛɥɢɰɟ. ȼɵ ɦɨɠɟɬɟ ɩɨɥɭɱɢɬɶ ɷɬɨɬ ɫɩɢɫɨɤ ɬɚɤɠɟ ɫ ɩɨɦɨɳɶɸ ɤɨɦɚɧɞɵ mysqlshow --status ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.
SHOW TABLE STATUS ɜɨɡɜɪɚɳɚɟɬ ɫɥɟɞɭɸɳɢɟ ɩɨɥɹ:
„Name.
ɂɦɹ ɬɚɛɥɢɰɵ.
„Type.
Ɍɢɩ ɬɚɛɥɢɰɵ.
„Row_format.
„Rows.
Ɏɨɪɦɚɬ ɯɪɚɧɟɧɢɹ ɫɬɪɨɤɢ (Fixed, Dynamic ɢɥɢ Compressed).
Ʉɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ.
„Avg_row_length.
„Data_length.
ɋɪɟɞɧɹɹ ɞɥɢɧɚ ɫɬɪɨɤɢ.
Ⱦɥɢɧɚ ɮɚɣɥɚ ɞɚɧɧɵɯ.
„Max_data_length.
Ɇɚɤɫɢɦɚɥɶɧɚɹ ɞɥɢɧɚ ɮɚɣɥɚ ɞɚɧɧɵɯ. Ⱦɥɹ ɫɬɪɨɤ ɫ ɮɢɤɫɢɪɨɜɚɧɧɨɣ ɞɥɢɧɨɣ – ɦɚɤɫɢɦɚɥɶɧɨɟ ɤɨɥɢɱɟɫɬɜɨ ɫɬɪɨɤ ɜ ɬɚɛɥɢɰɟ. Ⱦɥɹ ɫɬɪɨɤ ɫ ɞɢɧɚɦɢɱɟɫɤɢɦ ɮɨɪɦɚɬɨɦ ɷɬɨ ɨɛɳɟɟ ɱɢɫɥɨ ɛɚɣɬ ɞɚɧɧɵɯ, ɤɨɬɨɪɵɟ ɦɨɠɧɨ ɩɨɦɟɫɬɢɬɶ ɜ ɬɚɛɥɢɰɭ, ɭɱɢɬɵɜɚɹ ɪɚɡɦɟɪ ɢɫɩɨɥɶɡɭɟɦɨɝɨ ɭɤɚɡɚɬɟɥɹ ɞɚɧɧɵɯ.
„Index_length. Ⱦɥɢɧɚ ɢɧɞɟɤɫɧɨɝɨ ɮɚɣɥɚ.
„Data_free.
Ʉɨɥɢɱɟɫɬɜɨ ɜɵɞɟɥɟɧɧɵɯ, ɧɨ ɧɟ ɢɫɩɨɥɶɡɭɟɦɵɯ ɛɚɣɬ.
„Auto_increment.
ɋɥɟɞɭɸɳɟɟ ɡɧɚɱɟɧɢɟ AUTO_INCREMENT.
„Create_time.
ȼɪɟɦɹ ɫɨɡɞɚɧɢɹ ɬɚɛɥɢɰɵ.
„Update_time.
ȼɪɟɦɹ ɨɛɧɨɜɥɟɧɢɹ ɮɚɣɥɚ ɞɚɧɧɵɯ.
„Check_time.
„Collation.
„Checksum.
Ʉɨɝɞɚ ɬɚɛɥɢɰɚ ɩɪɨɜɟɪɹɥɚɫɶ ɜ ɩɨɫɥɟɞɧɢɣ ɪɚɡ.
ɇɚɛɨɪ ɫɢɦɜɨɥɨɜ ɢ ɩɨɪɹɞɨɤ ɫɨɩɨɫɬɚɜɥɟɧɢɹ ɬɚɛɥɢɰɵ (ɧɨɜɨɟ ɜ 4.1.1).
Ⱥɤɬɭɚɥɶɧɚɹ ɤɨɧɬɪɨɥɶɧɚɹ ɫɭɦɦɚ (ɟɫɥɢ ɟɫɬɶ) (ɧɨɜɨɟ ɜ 4.1.1).
„Create_options.
Ⱦɨɩɨɥɧɢɬɟɥɶɧɵɟ ɨɩɰɢɢ, ɢɫɩɨɥɶɡɨɜɚɧɧɵɟ ɜ CREATE TABLE.
„Comment.
Ʉɨɦɦɟɧɬɚɪɢɣ, ɜɜɟɞɟɧɧɵɣ ɩɪɢ ɫɨɡɞɚɧɢɢ ɬɚɛɥɢɰɵ (ɢɥɢ ɧɟɤɨɬɨɪɚɹ ɢɧɮɨɪɦɚɰɢɹ ɨ ɬɨɦ, ɩɨɱɟɦɭ MySQL ɧɟ ɦɨɠɟɬ ɩɨɥɭɱɢɬɶ ɞɨɫɬɭɩ ɤ ɞɚɧɧɵɦ ɜ ɬɚɛɥɢɰɟ).
ȼ ɤɨɦɦɟɧɬɚɪɢɹɯ ɤ ɬɚɛɥɢɰɟ InnoDB ɫɨɨɛɳɚɟɬ ɨ ɫɜɨɛɨɞɧɨɦ ɦɟɫɬɟ ɜ ɬɚɛɥɢɱɧɨɦ ɩɪɨɫɬɪɚɧɫɬɜɟ, ɤɨɬɨɪɨɦɭ ɩɪɢɧɚɞɥɟɠɢɬ ɬɚɛɥɢɰɚ. Ⱦɥɹ ɬɚɛɥɢɰ, ɪɚɫɩɨɥɨɠɟɧɧɵɯ ɜ ɪɚɡɞɟɥɹɟɦɨɦ
ɬɚɛɥɢɱɧɨɦ ɩɪɨɫɬɪɚɧɫɬɜɟ (shared tablespace), ɷɬɨ ɫɜɨɛɨɞɧɨɟ ɦɟɫɬɨ ɜ ɪɚɡɞɟɥɹɟɦɨɦ ɬɚɛɥɢɱɧɨɦ ɩɪɨɫɬɪɚɧɫɬɜɟ. ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ ɦɧɨɠɟɫɬɜɟɧɧɵɟ ɬɚɛɥɢɱɧɵɟ ɩɪɨɫɬɪɚɧɫɬɜɚ, ɢ ɬɚɛɥɢɰɚ ɢɦɟɟɬ ɫɜɨɟ ɫɨɛɫɬɜɟɧɧɨɟ ɬɚɛɥɢɱɧɨɟ ɩɪɨɫɬɪɚɧɫɬɜɨ, ɨɬɨɛɪɚɠɚɟɬɫɹ ɫɜɨɛɨɞɧɨɟ ɩɪɨɫɬɪɚɧɫɬɜɨ ɬɨɥɶɤɨ ɷɬɨɣ ɬɚɛɥɢɰɵ.
Ⱦɥɹ ɬɚɛɥɢɰ ɬɢɩɚ MEMORY (HEAP) ɡɧɚɱɟɧɢɟ Data_length, Max_data_length ɢ Index_length
ɩɪɢɧɢɦɚɸɬ ɩɪɢɦɟɪɧɨɟ ɡɧɚɱɟɧɢɟ ɨɛɴɟɦɚ ɜɵɞɟɥɟɧɧɨɣ ɩɚɦɹɬɢ. Ⱥɥɝɨɪɢɬɦ ɪɚɫɩɪɟɞɟɥɟɧɢɹ
ɩɚɦɹɬɢ ɪɟɡɟɪɜɢɪɭɟɬ ɩɚɦɹɬɶ ɛɨɥɶɲɢɦɢ ɤɭɫɤɚɦɢ, ɱɬɨɛɵ ɭɦɟɧɶɲɢɬɶ ɤɨɥɢɱɟɫɬɜɨ ɧɟɨɛɯɨɞɢɦɵɯ ɨɩɟɪɚɰɢɣ.
6.5.3.18. Ñèíòàêñèñ SHOW TABLES
SHOW [OPEN] TABLES [FROM ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ] [LIKE 'ɲɚɛɥɨɧ']
SHOW TABLES ɜɵɜɨɞɢɬ ɫɩɢɫɨɤ ɜɫɟɯ ɩɨɫɬɨɹɧɧɵɯ (ɧɟ ɜɪɟɦɟɧɧɵɯ) ɬɚɛɥɢɰ ɡɚɞɚɧɧɨɣ ɛɚɡɵ
ɞɚɧɧɵɯ. ɗɬɨɬ ɠɟ ɫɩɢɫɨɤ ɦɨɠɧɨ ɩɨɥɭɱɢɬɶ ɤɨɦɚɧɞɨɣ mysqlshow ɢɦɹ_ɛɚɡɵ_ɞɚɧɧɵɯ.
312
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
oË ÑËÕÏÛµÂ!
ȿɫɥɢ ɭ ɜɚɫ ɧɟɬ ɩɪɢɜɢɥɟɝɢɣ ɜ ɬɚɛɥɢɰɟ, ɷɬɚ ɬɚɛɥɢɰɚ ɧɟ ɛɭɞɟɬ ɩɪɢɫɭɬɫɬɜɨɜɚɬɶ ɜ ɫɩɢɫɤɟ.
SHOW TABLES ɜɵɜɨɞɢɬ ɫɩɢɫɨɤ ɬɚɛɥɢɰ, ɤɨɬɨɪɵɟ ɜ ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɨɬɤɪɵɬɵ ɜ ɬɚɛɥɢɱɧɨɦ
ɤɷɲɟ. ɉɨɥɟ Comment ɜ ɫɩɢɫɤɟ ɝɨɜɨɪɢɬ ɨ ɬɨɦ, ɫɤɨɥɶɤɨ ɪɚɡ ɬɚɛɥɢɰɚ ɛɵɥɚ cached ɢ in_use.
ɋɥɨɜɨ OPEN ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ, ɧɚɱɢɧɚɹ ɫ MySQL 3.23.33 ɢ ɜ ɩɨɫɥɟɞɭɸɳɢɯ ɜɟɪɫɢɹɯ.
6.5.3.19. Ñèíòàêñèñ SHOW VARIABLES
SHOW [GLOBAL | SESSION] VARIABLES [LIKE 'ɲɚɛɥɨɧ']
SHOW VARIABLES ɜɵɜɨɞɢɬ ɡɧɚɱɟɧɢɹ ɧɟɤɨɬɨɪɵɯ ɫɢɫɬɟɦɧɵɯ ɩɟɪɟɦɟɧɧɵɯ MySQL. ɗɬɚ
ɢɧɮɨɪɦɚɰɢɹ ɬɚɤɠɟ ɦɨɠɟɬ ɛɵɬɶ ɩɨɥɭɱɟɧɚ ɤɨɦɚɧɞɨɣ mysqladmin variables.
Ɉɩɰɢɢ GLOBAL ɢ SESSION ɩɨɹɜɢɥɢɫɶ ɜ MySQL 4.0.3. ɍɤɚɡɚɜ GLOBAL, ɜɵ ɩɨɥɭɱɢɬɟ ɡɧɚɱɟɧɢɹ, ɤɨɬɨɪɵɟ ɛɭɞɭɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɧɨɜɵɦɢ ɩɨɞɤɥɸɱɟɧɢɹɦɢ ɤ MySQL. Ɂɚɞɚɜ SESSION,
ɜɵ ɩɨɥɭɱɢɬɟ ɡɧɚɱɟɧɢɹ, ɞɟɣɫɬɜɭɸɳɢɟ ɜ ɬɟɤɭɳɟɦ ɫɟɚɧɫɟ. ȿɫɥɢ ɧɟ ɭɤɚɡɵɜɚɬɶ ɧɢ ɨɞɧɭ ɢɡ
ɷɬɢɯ ɨɩɰɢɣ, ɩɨ ɭɦɨɥɱɚɧɢɸ ɩɪɢɧɢɦɚɟɬɫɹ SESSION. LOCAL – ɷɬɨ ɫɢɧɨɧɢɦ ɞɥɹ SESSION.
ȿɫɥɢ ɡɧɚɱɟɧɢɹ ɩɨ ɭɦɨɥɱɚɧɢɸ ɜɚɦ ɧɟ ɩɨɞɯɨɞɹɬ, ɦɨɠɟɬɟ ɭɫɬɚɧɨɜɢɬɶ ɡɧɚɱɟɧɢɹ ɛɨɥɶɲɢɧɫɬɜɚ ɢɡ ɷɬɢɯ ɩɟɪɟɦɟɧɧɵɯ, ɢɫɩɨɥɶɡɭɹ ɨɩɰɢɢ ɤɨɦɚɧɞɧɨɣ ɫɬɪɨɤɢ ɩɪɢ ɡɚɩɭɫɤɟ mysqld,
ɢɥɢ ɠɟ ɜɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ SET. ɋɦ. ɪɚɡɞɟɥ 6.5.3.1.
ɑɚɫɬɢɱɧɵɣ ɜɵɜɨɞ ɩɪɟɞɫɬɚɜɥɟɧ ɧɢɠɟ. ɋɩɢɫɨɤ ɩɟɪɟɦɟɧɧɵɯ ɢ ɢɯ ɡɧɚɱɟɧɢɹ ɦɨɝɭɬ ɨɬɥɢɱɚɬɶɫɹ ɧɚ ɜɚɲɟɦ ɫɟɪɜɟɪɟ. Ɉɩɢɫɚɧɢɟ ɤɚɠɞɨɣ ɩɟɪɟɦɟɧɧɨɣ ɦɨɠɧɨ ɧɚɣɬɢ ɜ ɤɧɢɝɟ MySQL. Ɋɭɤɨɜɨɞɫɬɜɨ ɚɞɦɢɧɢɫɬɪɚɬɨɪɚ (Ɇ. : ɂɡɞɚɬɟɥɶɫɤɢɣ ɞɨɦ “ȼɢɥɶɹɦɫ”, 2005, ISBN 5-8459-0805-1).
mysql> SHOW VARIABLES;
+---------------------------------+-----------------------------+
| Variable_name
| Value
|
+---------------------------------+-----------------------------+
| back_log
| 50
|
| basedir
| /usr/local/mysql
|
| bdb_cache_size
| 8388572
|
| bdb_log_buffer_size
| 32768
|
| bdb_home
| /usr/local/mysql
|
...
| max_connections
| 100
|
| max_connect_errors
| 10
|
| max_delayed_threads
| 20
|
| max_error_count
| 64
|
| max_heap_table_size
| 16777216
|
| max_join_size
| 4294967295
|
| max_relay_log_size
| 0
|
| max_sort_length
| 1024
|
...
| timezone
| EEST
|
| tmp_table_size
| 33554432
|
| tmpdir
| /tmp/:/mnt/hd2/tmp/
|
| version
| 4.0.4-beta
|
| wait_timeout
| 28800
|
+---------------------------------+-----------------------------+
ɋ ɤɨɧɫɬɪɭɤɰɢɟɣ LIKE ɨɩɟɪɚɬɨɪ ɜɵɜɨɞɢɬ ɬɨɥɶɤɨ ɬɟ ɩɟɪɟɦɟɧɧɵɟ, ɢɦɟɧɚ ɤɨɬɨɪɵɯ ɫɨɨɬɜɟɬɫɬɜɭɸɬ ɲɚɛɥɨɧɭ:
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
313
mysql> SHOW VARIABLES LIKE 'have%';
+--------------------+----------+
| Variable_name
| Value
|
+--------------------+----------+
| have_bdb
| YES
|
| have_innodb
| YES
|
| have_isam
| YES
|
| have_raid
| NO
|
| have_symlink
| DISABLED |
| have_openssl
| YES
|
| have_query_cache
| YES
|
+--------------------+----------+
6.5.3.20. Ñèíòàêñèñ SHOW WARNINGS
SHOW WARNINGS [LIMIT [ɫɦɟɳɟɧɢɟ,] ɤɨɥɢɱɟɫɬɜɨ_ɫɬɪɨɤ]
SHOW COUNT(*) WARNINGS
SHOW WARNINGS ɜɵɜɨɞɢɬ ɨɲɢɛɤɢ, ɩɪɟɞɭɩɪɟɠɞɟɧɢɹ ɢ ɞɪɭɝɢɟ ɡɚɦɟɱɚɧɢɹ, ɤɨɬɨɪɵɟ ɩɨɹɜɥɹɸɬɫɹ ɜ ɪɟɡɭɥɶɬɚɬɟ ɜɵɩɨɥɧɟɧɢɹ ɩɨɫɥɟɞɧɟɝɨ ɨɩɟɪɚɬɨɪɚ, ɤɨɬɨɪɵɣ ɝɟɧɟɪɢɪɭɟɬ ɫɨɨɛɳɟɧɢɹ,
ɢɥɢ ɠɟ ɧɢɱɟɝɨ, ɟɫɥɢ ɩɨɫɥɟɞɧɢɣ ɨɩɟɪɚɬɨɪ, ɢɫɩɨɥɶɡɭɸɳɢɣ ɬɚɛɥɢɰɭ, ɧɟ ɫɝɟɧɟɪɢɪɨɜɚɥ ɧɢɤɚɤɢɯ ɫɨɨɛɳɟɧɢɣ. ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɪɟɚɥɢɡɨɜɚɧ ɜ MySQL 4.1.0. Ɋɨɞɫɬɜɟɧɧɵɣ ɨɩɟɪɚɬɨɪ –
SHOW ERRORS – ɩɨɤɚɡɵɜɚɟɬ ɬɨɥɶɤɨ ɨɲɢɛɤɢ. ɋɦ. ɪɚɡɞɟɥ 6.5.3.9.
ɋɩɢɫɨɤ ɫɨɨɛɳɟɧɢɣ ɨɱɢɳɚɟɬɫɹ ɞɥɹ ɤɚɠɞɨɝɨ ɧɨɜɨɝɨ ɨɩɟɪɚɬɨɪɚ, ɢɫɩɨɥɶɡɨɜɚɧɧɨɝɨ ɜ ɬɚɛɥɢɰɟ.
Ɉɩɟɪɚɬɨɪ SHOW COUNT(*) WARNINGS ɨɬɨɛɪɚɠɚɟɬ ɨɛɳɟɟ ɤɨɥɢɱɟɫɬɜɨ ɨɲɢɛɨɤ, ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ ɢ ɡɚɦɟɱɚɧɢɣ. ȼɵ ɦɨɠɟɬɟ ɩɪɨɱɢɬɚɬɶ ɷɬɨ ɡɧɚɱɟɧɢɟ ɜ ɩɟɪɟɦɟɧɧɨɣ warning_count:
SHOW COUNT(*) WARNINGS;
SELECT @@warning_count;
Ɂɧɚɱɟɧɢɟ warning_count ɦɨɠɟɬ ɛɨɥɶɲɟ, ɱɟɦ ɤɨɥɢɱɟɫɬɜɨ ɫɨɨɛɳɟɧɢɣ, ɨɬɨɛɪɚɠɟɧɧɨɟ
SHOW WARNINGS, ɟɫɥɢ ɡɧɚɱɟɧɢɟ ɫɢɫɬɟɦɧɨɣ ɩɟɪɟɦɟɧɧɨɣ max_error_count ɭɫɬɚɧɨɜɥɟɧɨ ɞɨɫɬɚɬɨɱɧɨ ɦɚɥɵɦ, ɬɨ ɟɫɬɶ ɬɚɤ, ɱɬɨ ɧɟ ɜɫɟ ɫɨɨɛɳɟɧɢɹ ɫɨɯɪɚɧɹɸɬɫɹ. ɉɪɢɜɟɞɟɧɧɵɣ ɧɢɠɟ
ɩɪɢɦɟɪ ɩɨɤɚɡɵɜɚɟɬ, ɤɚɤ ɷɬɨ ɦɨɠɟɬ ɫɥɭɱɢɬɶɫɹ.
Ʉɨɧɫɬɪɭɤɰɢɹ LIMIT ɢɦɟɟɬ ɬɨɬ ɠɟ ɫɢɧɬɚɤɫɢɫ, ɱɬɨ ɢ ɜ ɨɩɟɪɚɬɨɪɟ SELECT. ɋɦ. ɪɚɡɞɟɥ 6.1.7.
ɋɟɪɜɟɪ MySQL ɨɬɩɪɚɜɥɹɟɬ ɨɛɪɚɬɧɨ ɨɛɳɟɟ ɤɨɥɢɱɟɫɬɜɨ ɨɲɢɛɨɤ, ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ ɢ
ɡɚɦɟɱɚɧɢɣ, ɩɨɥɭɱɟɧɧɵɯ ɜ ɪɟɡɭɥɶɬɚɬɟ ɩɨɫɥɟɞɧɟɝɨ ɨɩɟɪɚɬɨɪɚ. ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ
ɩɪɨɝɪɚɦɦɧɵɣ ɢɧɬɟɪɮɟɣɫ C API, ɷɬɨ ɡɧɚɱɟɧɢɟ ɦɨɠɟɬ ɛɵɬɶ ɩɨɥɭɱɟɧɨ ɜɵɡɨɜɨɦ
mysql_warning_count().
Ɉɬɦɟɬɢɦ, ɱɬɨ ɩɨɞɫɢɫɬɟɦɚ ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ ɩɨɹɜɢɥɚɫɶ ɜ MySQL 4.1.0, ɤɨɝɞɚ ɦɧɨɝɢɟ
ɨɩɟɪɚɬɨɪɵ ɟɳɟ ɧɟ ɝɟɧɟɪɢɪɨɜɚɥɢ ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ. ȼ ɜɟɪɫɢɢ MySQL 4.1.1 ɫɢɬɭɚɰɢɹ ɫ
ɩɪɟɞɭɩɪɟɠɞɟɧɢɹɦɢ, ɝɟɧɟɪɢɪɭɟɦɵɦɢ ɬɚɤɢɦɢ ɨɩɟɪɚɬɨɪɚɦɢ, ɤɚɤ LOAD DATA INFILE, ɚ ɬɚɤɠɟ
ɨɩɟɪɚɬɨɪɚɦɢ DML (ɹɡɵɤɚ ɦɚɧɢɩɭɥɹɰɢɢ ɞɚɧɧɵɦɢ), ɩɨɞɨɛɧɵɦɢ INSERT, UPDATE, CREATE
TABLE ɢ ALTER TABLE (ɜ ɞɪɭɝɢɯ ɢɫɬɨɱɧɢɤɚɯ ɩɪɢɧɹɬɨ ɫɱɢɬɚɬɶ, ɱɬɨ CREATE TABLE ɢ ALTER
TABLE – ɷɬɨ ɨɩɟɪɚɬɨɪɵ DDL (Data Definition Language – ɹɡɵɤ ɨɩɪɟɞɟɥɟɧɢɹ ɞɚɧɧɵɯ), ɚ ɧɟ
DML – ɩɪɢɦ. ɩɟɪ.), ɡɧɚɱɢɬɟɥɶɧɨ ɭɥɭɱɲɢɥɚɫɶ.
ɋɥɟɞɭɸɳɢɣ ɨɩɟɪɚɬɨɪ DROP TABLE ɝɟɧɟɪɢɪɭɟɬ ɫɨɨɛɳɟɧɢɟ:
mysql> DROP TABLE IF EXISTS no_such_table;
mysql> SHOW WARNINGS;
314
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
+-------+------+-------------------------------+
| Level | Code | Message
|
+-------+------+-------------------------------+
| Note | 1051 | Unknown table 'no_such_table' |
+-------+------+-------------------------------+
ɇɢɠɟ ɩɪɟɞɫɬɚɜɥɟɧ ɩɪɨɫɬɨɣ ɩɪɢɦɟɪ, ɩɨɤɚɡɵɜɚɸɳɢɣ ɫɢɧɬɚɤɫɢɫ ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ
CREATE TABLE ɢ ɡɚɦɟɧɭ ɩɪɟɞɭɩɪɟɠɞɟɧɢɹ ɩɨɫɥɟ ɨɩɟɪɚɬɨɪɚ INSERT:
mysql> CREATE TABLE t1 (a TINYINT NOT NULL, b CHAR(4)) TYPE=MyISAM;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
Level: Warning
Code: 1287
Message: 'TYPE=storage_engine' is deprecated, use
'ENGINE=storage_engine' instead
1 row in set (0.00 sec)
mysql> INSERT INTO t1 VALUES(10, 'mysql'),(NULL,'test'),
-> (300,'open source');
Query OK, 3 rows affected, 4 warnings (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 4
mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
Level: Warning
Code: 1265
Message: Data truncated for column 'b' at row 1
*************************** 2. row ***************************
Level: Warning
Code: 1263
Message: Data truncated, NULL supplied to NOT NULL column 'a' at row 2
*************************** 3. row ***************************
Level: Warning
Code: 1264
Message: Data truncated, out of range for column 'a' at row 3
*************************** 4. row ***************************
Level: Warning
Code: 1265
Message: Data truncated for column 'b' at row 3
4 rows in set (0.00 sec)
Ɇɚɤɫɢɦɚɥɶɧɨɟ ɤɨɥɢɱɟɫɬɜɨ ɨɲɢɛɨɤ, ɫɨɨɛɳɟɧɢɣ ɢ ɡɚɦɟɱɚɧɢɣ, ɤɨɬɨɪɵɟ ɫɨɯɪɚɧɹɸɬɫɹ ɦɟɠɞɭ ɜɵɡɨɜɚɦɢ ɨɩɟɪɚɬɨɪɨɜ, ɨɩɪɟɞɟɥɟɧɵ ɡɧɚɱɟɧɢɟɦ ɫɢɫɬɟɦɧɨɣ ɩɟɪɟɦɟɧɧɨɣ
max_error_count. ɉɨ ɭɦɨɥɱɚɧɢɸ ɨɧɨ ɪɚɜɧɨ 64. ɑɬɨɛɵ ɢɡɦɟɧɢɬɶ ɤɨɥɢɱɟɫɬɜɨ ɫɨɨɛɳɟɧɢɣ,
ɤɨɬɨɪɵɟ ɧɭɠɧɨ ɫɨɯɪɚɧɹɬɶ, ɩɪɨɫɬɨ ɢɡɦɟɧɢɬɟ ɡɧɚɱɟɧɢɟ ɷɬɨɣ ɩɟɪɟɦɟɧɧɨɣ. ȼ ɫɥɟɞɭɸɳɟɦ
ɩɪɢɦɟɪɟ ɨɩɟɪɚɬɨɪ ALTER TABLE ɝɟɧɟɪɢɪɭɟɬ ɬɪɢ ɩɪɟɞɭɩɪɟɠɞɚɸɳɢɯ ɫɨɨɛɳɟɧɢɹ, ɧɨ ɫɨɯɪɚɧɹɟɬɫɹ ɬɨɥɶɤɨ ɨɞɧɨ, ɩɨɬɨɦɭ ɱɬɨ max_error_count ɩɪɢɫɜɨɟɧɨ ɡɧɚɱɟɧɢɟ 1:
mysql> SHOW VARIABLES LIKE 'max_error_count';
+-----------------+-------+
| Variable_name
| Value |
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
315
+-----------------+-------+
| max_error_count | 64
|
+-----------------+-------+
1 row in set (0.00 sec)
mysql> SET max_error_count=1;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER TABLE t1 MODIFY b CHAR;
Query OK, 3 rows affected, 3 warnings (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 3
mysql> SELECT @@warning_count;
+-----------------+
| @@warning_count |
+-----------------+
|
3 |
+-----------------+
1 row in set (0.01 sec)
mysql> SHOW WARNINGS;
+---------+------+----------------------------------------+
| Level
| Code | Message
|
+---------+------+----------------------------------------+
| Warning | 1263 | Data truncated for column 'b' at row 1 |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)
ɑɬɨɛɵ ɨɬɤɥɸɱɢɬɶ ɫɨɨɛɳɟɧɢɹ ɜɨɨɛɳɟ, ɩɪɢɫɜɨɣɬɟ ɩɟɪɟɦɟɧɧɨɣ max_error_count ɡɧɚɱɟɧɢɟ 0. ȼ ɷɬɨɦ ɫɥɭɱɚɟ warning_count ɩɨ-ɩɪɟɠɧɟɦɭ ɩɨɤɚɡɵɜɚɟɬ, ɫɤɨɥɶɤɨ ɩɪɟɞɭɩɪɟɠɞɟɧɢɣ ɛɵɥɨ ɫɝɟɧɟɪɢɪɨɜɚɧɨ, ɧɨ ɧɢ ɨɞɧɨ ɢɡ ɧɢɯ ɧɟ ɫɨɯɪɚɧɹɟɬɫɹ.
6.5.4 Äðóãèå îïåðàòîðû àäìèíèñòðèðîâàíèÿ
6.5.4.1. Ñèíòàêñèñ CACHE INDEX
CACHE INDEX
ɫɩɢɫɨɤ_ɢɧɞɟɤɫɨɜ_ɬɚɛɥɢɰɵ [, ɫɩɢɫɨɤ_ɢɧɞɟɤɫɨɜ_ɬɚɛɥɢɰɵ] ...
IN ɢɦɹ_ɤɷɲɚ_ɤɥɸɱɟɣ
ɫɩɢɫɨɤ_ɢɧɞɟɤɫɨɜ_ɬɚɛɥɢɰɵ:
ɢɦɹ_ɬɚɛɥɢɰɵ [[INDEX] (ɢɦɹ_ɢɧɞɟɤɫɚ [, ɢɦɹ_ɢɧɞɟɤɫɚ] ...)]
Ɉɩɟɪɚɬɨɪ CACHE INDEX ɧɚɡɧɚɱɚɟɬ ɢɧɞɟɤɫɚɦ ɬɚɛɥɢɰ ɫɩɟɰɢɚɥɶɧɵɣ ɤɷɲ ɤɥɸɱɟɣ. ɗɬɨ
ɞɟɣɫɬɜɢɬɟɥɶɧɨ ɬɨɥɶɤɨ ɞɥɹ ɬɚɛɥɢɰ MyISAM.
ɋɥɟɞɭɸɳɢɣ ɨɩɟɪɚɬɨɪ ɧɚɡɧɚɱɚɟɬ ɢɧɞɟɤɫɚɦ ɬɚɛɥɢɰ t1, t2 ɢ t3 ɤɥɸɱɟɜɨɣ ɤɷɲ ɫ ɢɦɟɧɟɦ
hot_cache:
mysql> CACHE INDEX t1, t2, t3 IN hot_cache;
+---------+--------------------+----------+----------+
| Table
| Op
| Msg_type | Msg_text |
+---------+--------------------+----------+----------+
| test.t1 | assign_to_keycache | status
| OK
|
| test.t2 | assign_to_keycache | status
| OK
|
| test.t3 | assign_to_keycache | status
| OK
|
+---------+--------------------+----------+----------+
316
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɋɢɧɬɚɤɫɢɫ CACHE INDEX ɩɨɡɜɨɥɹɟɬ ɩɪɢɜɹɡɚɬɶ ɤ ɤɷɲɭ ɬɨɥɶɤɨ ɨɬɞɟɥɶɧɵɟ ɢɧɞɟɤɫɵ ɬɚɛɥɢɰɵ. Ɉɞɧɚɤɨ ɧɚ ɫɚɦɨɦ ɞɟɥɟ ɬɟɤɭɳɚɹ ɪɟɚɥɢɡɚɰɢɹ ɩɪɢɜɹɡɵɜɚɟɬ ɜɫɟ ɢɧɞɟɤɫɵ ɬɚɛɥɢɰɵ ɤ
ɭɤɚɡɚɧɧɨɦɭ ɤɷɲɭ, ɩɨɷɬɨɦɭ ɧɟɬ ɨɫɨɛɨɝɨ ɫɦɵɫɥɚ ɫɩɟɰɢɮɢɰɢɪɨɜɚɬɶ ɱɬɨ-ɬɨ ɟɳɟ, ɤɪɨɦɟ ɢɦɟɧɢ ɬɚɛɥɢɰɵ.
Ʉɷɲ ɤɥɸɱɟɣ, ɧɚ ɤɨɬɨɪɵɣ ɫɫɵɥɚɟɬɫɹ ɨɩɟɪɚɬɨɪ CACHE INDEX, ɦɨɠɟɬ ɛɵɬɶ ɫɨɡɞɚɧ ɭɫɬɚɧɨɜɤɨɣ ɟɝɨ ɪɚɡɦɟɪɚ ɜ ɩɚɪɚɦɟɬɪɟ ɨɩɟɪɚɬɨɪɚ SET ɥɢɛɨ ɜ ɧɚɫɬɪɨɣɤɚɯ ɩɚɪɚɦɟɬɪɨɜ ɫɟɪɜɟɪɚ.
ɇɚɩɪɢɦɟɪ:
mysql> SET GLOBAL keycache1.key_buffer_size=128*1024;
ɉɚɪɚɦɟɬɪɵ ɤɷɲɚ ɤɥɸɱɟɣ ɞɨɫɬɭɩɧɵ ɤɚɤ ɱɥɟɧɵ ɫɬɪɭɤɬɭɪɢɪɨɜɚɧɧɨɣ ɫɢɫɬɟɦɧɨɣ ɩɟɪɟɦɟɧɧɨɣ (ɫɦ. ɪɚɡɞɟɥ 2.4.1).
Ʉɷɲ ɤɥɸɱɟɣ ɞɨɥɠɟɧ ɫɭɳɟɫɬɜɨɜɚɬɶ ɞɨ ɩɪɢɜɹɡɤɢ ɢɧɞɟɤɫɨɜ ɤ ɧɟɦɭ:
mysql> CACHE INDEX t1 in non_existent_cache;
ERROR 1283 (HY000): Unknown key cache 'non_existent_cache'
ɉɨ ɭɦɨɥɱɚɧɢɸ ɢɧɞɟɤɫɵ ɬɚɛɥɢɰ ɩɪɢɜɹɡɵɜɚɸɬɫɹ ɤ ɝɥɚɜɧɨɦɭ (ɩɨ ɭɦɨɥɱɚɧɢɢɸ) ɤɷɲɭ
ɤɥɸɱɟɣ, ɤɨɬɨɪɵɣ ɫɨɡɞɚɟɬɫɹ ɩɪɢ ɡɚɩɭɫɤɟ ɫɟɪɜɟɪɚ. Ʉɨɝɞɚ ɤɷɲ ɤɥɸɱɟɣ ɪɚɡɪɭɲɚɟɬɫɹ, ɜɫɟ
ɧɚɡɧɚɱɟɧɧɵɟ ɟɦɭ ɢɧɞɟɤɫɵ ɨɩɹɬɶ ɩɟɪɟɧɚɡɧɚɱɚɸɬɫɹ ɧɚ ɤɷɲ ɩɨ ɭɦɨɥɱɚɧɢɸ.
ɇɚɡɧɚɱɟɧɢɟ ɢɧɞɟɤɫɨɜ ɤɚɫɚɟɬɫɹ ɫɟɪɜɟɪɚ ɝɥɨɛɚɥɶɧɨ. ȿɫɥɢ ɨɞɢɧ ɤɥɢɟɧɬ ɧɚɡɧɚɱɚɟɬ ɢɧɞɟɤɫ ɨɩɪɟɞɟɥɟɧɧɨɦɭ ɤɷɲɭ, ɷɬɨɬ ɤɷɲ ɛɭɞɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɩɪɢ ɜɵɩɨɥɧɟɧɢɢ ɜɫɟɯ ɡɚɩɪɨɫɨɜ, ɧɟɡɚɜɢɫɢɦɨ ɨɬ ɬɨɝɨ, ɤɚɤɨɣ ɤɥɢɟɧɬ ɢɯ ɢɧɢɰɢɢɪɭɟɬ.
CACHE INDEX ɩɨɹɜɢɥɫɹ ɜ MySQL 4.1.1.
6.5.4.2. Ñèíòàêñèñ FLUSH
FLUSH [LOCAL | NO_WRITE_TO_BINLOG] ɨɩɰɢɹ_ɨɱɢɫɬɤɢ [, ɨɩɰɢɹ_ɨɱɢɫɬɤɢ] ...
Ɉɩɟɪɚɬɨɪ FLUSH ɫɥɟɞɭɟɬ ɩɪɢɦɟɧɹɬɶ, ɤɨɝɞɚ ɜɵ ɯɨɬɢɬɟ ɨɱɢɫɬɢɬɶ ɜɧɭɬɪɟɧɧɢɟ ɤɷɲɢ, ɤɨɬɨɪɵɟ ɢɫɩɨɥɶɡɭɟɬ MySQL. Ⱦɥɹ ɜɵɩɨɥɧɟɧɢɹ FLUSH ɧɟɨɛɯɨɞɢɦɨ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɸ RELOAD.
Ⱥɪɝɭɦɟɧɬ ɨɩɰɢɹ_ɨɱɢɫɬɤɢ ɦɨɠɟɬ ɩɪɢɧɢɦɚɬɶ ɥɸɛɨɟ ɢɡ ɩɟɪɟɱɢɫɥɟɧɧɵɯ ɧɢɠɟ ɡɧɚɱɟɧɢɣ.
„HOSTS.
Ɉɱɢɳɚɟɬ ɬɚɛɥɢɰɵ ɤɷɲɚ ɯɨɫɬɨɜ. ȼɵ ɞɨɥɠɧɵ ɫɛɪɚɫɵɜɚɬɶ ɬɚɛɥɢɰɵ ɯɨɫɬɨɜ,
ɟɫɥɢ ɤɚɤɨɣ-ɧɢɛɭɞɶ ɢɡ ɜɚɲɢɯ ɯɨɫɬɨɜ ɦɟɧɹɟɬ IP-ɚɞɪɟɫ, ɥɢɛɨ ɟɫɥɢ ɜɵ ɩɨɥɭɱɚɟɬɟ ɫɨɨɛɳɟɧɢɟ ɨɛ ɨɲɢɛɤɟ ɧɚɩɨɞɨɛɢɟ Host ... is blocked (ɏɨɫɬ … ɡɚɛɥɨɤɢɪɨɜɚɧ). Ʉɨɝɞɚ ɫɥɭɱɚɟɬɫɹ ɛɨɥɶɲɟ, ɱɟɦ max_connect_errors ɩɪɢ ɩɨɩɵɬɤɟ ɩɨɞɤɥɸɱɢɬɶɫɹ ɤ ɫɟɪɜɟɪɭ MySQL ɫ ɞɚɧɧɨɝɨ ɯɨɫɬɚ, MySQL ɩɪɟɞɩɨɥɚɝɚɟɬ, ɱɬɨ ɫ ɯɨɫɬɨɦ ɱɬɨ-ɬɨ ɧɟ ɜ ɩɨɪɹɞɤɟ ɢ ɛɥɨɤɢɪɭɟɬ ɞɚɥɶɧɟɣɲɢɟ ɩɨɩɵɬɤɢ ɟɝɨ ɩɨɞɤɥɸɱɟɧɢɹ. ɋɛɪɨɫ ɤɷɲɚ ɯɨɫɬɨɜ
ɨɩɹɬɶ ɫɧɢɦɚɟɬ ɷɬɭ ɛɥɨɤɢɪɨɜɤɭ ɢ ɩɨɡɜɨɥɹɟɬ ɩɪɨɞɨɥɠɢɬɶ ɩɨɩɵɬɤɢ ɩɨɞɤɥɸɱɟɧɢɹ.
ȼɵ ɦɨɠɟɬɟ ɡɚɩɭɫɬɢɬɶ ɫɟɪɜɟɪ mysqld ɫ ɨɩɰɢɟɣ --max_connect_errors=999999999,
ɱɬɨɛɵ ɢɡɛɟɠɚɬɶ ɨɲɢɛɨɤ ɩɨɞɨɛɧɨɝɨ ɪɨɞɚ.
„DES_KEY_FILE. ɉɟɪɟɡɚɝɪɭɠɚɟɬ DES-ɤɥɸɱɢ ɢɡ ɮɚɣɥɚ, ɡɚɞɚɧɧɨɝɨ ɨɩɰɢɟɣ --des-keyfile ɜɨ ɜɪɟɦɹ ɡɚɩɭɫɤɚ ɫɟɪɜɟɪɚ.
„LOGS.
Ɂɚɤɪɵɜɚɟɬ ɢ ɡɚɧɨɜɨ ɨɬɤɪɵɜɚɟɬ ɜɫɟ ɠɭɪɧɚɥɶɧɵɟ ɮɚɣɥɵ. ȿɫɥɢ ɜɵ ɭɤɚɡɚɥɢ
ɢɦɟɧɚ ɠɭɪɧɚɥɶɧɵɯ ɮɚɣɥɨɜ ɢɡɦɟɧɟɧɢɣ ɢɥɢ ɛɢɧɚɪɧɨɝɨ ɠɭɪɧɚɥɚ ɛɟɡ ɪɚɫɲɢɪɟɧɢɹ,
ɧɨɦɟɪ ɜ ɪɚɫɲɢɪɟɧɢɢ ɢɦɟɧɢ ɛɭɞɟɬ ɭɜɟɥɢɱɟɧ ɧɚ ɟɞɢɧɢɰɭ ɩɨ ɫɪɚɜɧɟɧɢɸ ɫ ɩɪɟɞɵɞɭɳɢɦ ɮɚɣɥɨɦ. ȿɫɥɢ ɠɟ ɜɵ ɭɤɚɠɟɬɟ ɢɦɟɧɚ ɫ ɪɚɫɲɢɪɟɧɢɹɦɢ, MySQL ɩɪɨɫɬɨ ɡɚɤɪɨɟɬ
ɢ ɨɬɤɪɨɟɬ ɮɚɣɥ ɠɭɪɧɚɥɚ ɨɛɧɨɜɥɟɧɢɣ ɢɥɢ ɛɢɧɚɪɧɨɝɨ ɠɭɪɧɚɥɚ. ȼ ɫɪɟɞɟ Unix ɷɬɨ ɬɨ
ɠɟ ɫɚɦɨɟ, ɱɬɨ ɩɨɫɥɚɬɶ ɫɢɝɧɚɥ SIGHUP ɫɟɪɜɟɪɭ mysqld.
6.5. Îïåðàòîðû àäìèíèñòðèðîâàíèÿ áàçû äàííûõ
317
„PRIVILEGES.
ɉɟɪɟɡɚɝɪɭɠɚɟɬ ɜ ɩɚɦɹɬɶ ɢɧɮɨɪɦɚɰɢɸ ɨ ɩɪɢɜɢɥɟɝɢɹɯ ɢɡ ɬɚɛɥɢɰ ɩɪɢɜɢɥɟɝɢɣ ɛɚɡɵ ɞɚɧɧɵɯ mysql.
„QUERY CACHE.
ȼɵɩɨɥɧɹɟɬ ɞɟɮɪɚɝɦɟɧɬɚɰɢɸ ɤɷɲɚ ɡɚɩɪɨɫɨɜ ɫ ɰɟɥɶɸ ɛɨɥɟɟ ɷɮɮɟɤɬɢɜɧɨɝɨ ɢɫɩɨɥɶɡɨɜɚɧɢɹ ɩɚɦɹɬɢ. ȼ ɨɬɥɢɱɢɟ ɨɬ RESET QUERY CACHE, ɷɬɨɬ ɨɩɟɪɚɬɨɪ ɧɟ
ɭɞɚɥɹɟɬ ɧɢɤɚɤɢɯ ɡɚɩɪɨɫɨɜ ɢɡ ɤɷɲɚ
„STATUS. ɋɛɪɚɫɵɜɚɟɬ ɛɨɥɶɲɢɧɫɬɜɨ ɩɟɪɟɦɟɧɧɵɯ ɫɨɫɬɨɹɧɢɹ ɜ ɧɨɥɶ. ɗɬɨ ɬɨ, ɱɬɨ ɫɬɨɢɬ
ɩɪɢɦɟɧɹɬɶ ɬɨɥɶɤɨ ɩɪɢ ɨɬɥɚɞɤɟ ɡɚɩɪɨɫɨɜ. ɋɦ. ɪɚɡɞɟɥ 1.7.1.3.
„{TABLE | TABLES} [ɢɦɹ_ɬɚɛɥɢɰɵ [, ɢɦɹ_ɬɚɛɥɢɰɵ] ...]
Ʉɨɝɞɚ ɧɟ ɧɚɡɜɚɧɚ ɬɚɛɥɢɰɚ, ɡɚɤɪɵɜɚɟɬ ɜɫɟ ɨɬɤɪɵɬɵɟ ɬɚɛɥɢɰɵ ɢ ɩɪɢɧɭɠɞɚɟɬ ɤ ɡɚɤɪɵɬɢɸ ɜɫɟ ɢɫɩɨɥɶɡɭɟɦɵɟ ɬɚɛɥɢɰɵ. Ɍɚɤɠɟ ɨɱɢɳɚɟɬ ɤɷɲ ɡɚɩɪɨɫɨɜ. ɋ ɨɞɧɢɦ ɢɥɢ
ɛɨɥɟɟ ɢɦɟɧɟɦ ɬɚɛɥɢɰɵ ɫɛɪɚɫɵɜɚɟɬ ɬɨɥɶɤɨ ɷɬɢ ɬɚɛɥɢɰɵ. FLUSH TABLES ɬɚɤɠɟ ɭɞɚɥɹɟɬ ɜɫɟ ɪɟɡɭɥɶɬɚɬɵ ɡɚɩɪɨɫɨɜ ɢɡ ɤɷɲɚ, ɤɚɤ ɢ ɨɩɟɪɚɬɨɪ RESET QUERY CACHE.
„TABLES WITH READ LOCK.
Ɂɚɤɪɵɜɚɟɬ ɜɫɟ ɨɬɤɪɵɬɵɟ ɬɚɛɥɢɰɵ ɢ ɛɥɨɤɢɪɭɟɬ ɜɫɟ ɬɚɛɥɢɰɵ ɜɨ ɜɫɟɯ ɛɚɡɚɯ ɞɚɧɧɵɯ ɛɥɨɤɢɪɨɜɤɨɣ ɩɨ ɱɬɟɧɢɸ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ ɧɟ ɛɭɞɟɬ ɜɵɩɨɥɧɟɧ UNLOCK TABLES. ɗɬɨ ɨɱɟɧɶ ɭɞɨɛɧɵɣ ɫɩɨɫɨɛ ɩɨɥɭɱɢɬɶ ɪɟɡɟɪɜɧɭɸ ɤɨɩɢɸ,
ɟɫɥɢ ɜɵ ɩɨɥɶɡɭɟɬɟɫɶ ɬɚɤɨɣ ɮɚɣɥɨɜɨɣ ɫɢɫɬɟɦɨɣ, ɤɚɤ Veritas, ɤɨɬɨɪɚɹ ɩɨɡɜɨɥɹɟɬ ɞɟɥɚɬɶ ɫɧɢɦɤɢ ɩɨ ɜɪɟɦɟɧɢ.
„USER_RESOURCES. ɋɛɪɚɫɵɜɚɟɬ ɜ ɧɨɥɶ ɜɫɟ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɢɟ ɪɟɫɭɪɫɵ. ɗɬɨ ɩɨɡɜɨɥɹɟɬ
ɤɥɢɟɧɬɚɦ, ɤɨɬɨɪɵɟ ɞɨɫɬɢɝɥɢ ɥɢɦɢɬɚ ɩɨ ɦɚɤɫɢɦɚɥɶɧɨɦɭ ɱɢɫɥɭ ɫɨɟɞɢɧɟɧɢɣ, ɦɚɤɫɢɦɚɥɶɧɨɦɭ ɱɢɫɥɭ ɡɚɩɪɨɫɨɜ ɢɥɢ ɨɛɧɨɜɥɟɧɢɣ, ɩɪɨɞɨɥɠɢɬɶ ɪɚɛɨɬɭ. ɋɦ. ɪɚɡɞɟɥ
6.5.1.2.
Ⱦɨ ɜɟɪɫɢɢ MySQL 4.1.1 ɨɩɟɪɚɬɨɪɵ FLUSH ɧɟ ɡɚɩɢɫɵɜɚɥɢɫɶ ɜ ɛɢɧɚɪɧɵɣ ɠɭɪɧɚɥ. ɇɚɱɢɧɚɹ ɫ MySQL 4.1.1, ɷɬɨɬ ɨɩɟɪɚɬɨɪ ɡɚɩɢɫɵɜɚɟɬɫɹ, ɟɫɥɢ ɬɨɥɶɤɨ ɧɟ ɛɵɥɨ ɭɤɚɡɚɧɨ ɧɟɨɛɹɡɚɬɟɥɶɧɨɟ ɤɥɸɱɟɜɨɟ ɫɥɨɜɨ NO_WRITE_TO_BINLOG (ɢɥɢ ɟɝɨ ɩɫɟɜɞɨɧɢɦ LOCAL). ɂɫɤɥɸɱɟɧɢɹɦɢ
ɹɜɥɹɸɬɫɹ FLUSH LOGS, FLUSH MASTER, FLUSH SLAVE ɢ FLUSH TABLES WITH READ LOCK, ɤɨɬɨɪɵɟ ɧɟ ɪɟɝɢɫɬɪɢɪɭɸɬɫɹ ɜ ɥɸɛɨɦ ɫɥɭɱɚɟ, ɩɨɫɤɨɥɶɤɭ ɨɧɢ ɦɨɝɭɬ ɛɵɬɶ ɩɪɢɱɢɧɚɦɢ ɩɪɨɛɥɟɦ
ɩɪɢ ɪɟɩɥɢɤɚɰɢɢ ɧɚ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ.
ȼɵ ɦɨɠɟɬɟ ɩɨɥɭɱɢɬɶ ɞɨɫɬɭɩ ɤ ɧɟɤɨɬɨɪɵɦ ɢɡ ɷɬɢɯ ɨɩɟɪɚɬɨɪɨɜ ɫ ɩɨɦɨɳɶɸ ɭɬɢɥɢɬɵ
mysqladmin, ɢɫɩɨɥɶɡɭɹ ɤɨɦɚɧɞɵ flushhosts, flush-logs, flush-privileges, flush-status
ɢɥɢ flush-tables.
ȼɡɝɥɹɧɢɬɟ ɬɚɤɠɟ ɧɚ ɨɩɟɪɚɬɨɪ RESET, ɢɫɩɨɥɶɡɭɟɦɵɣ ɩɪɢ ɪɟɩɥɢɤɚɰɢɢ (ɫɦ. ɪɚɡɞɟɥ
6.5.4.5).
6.5.4.3. Ñèíòàêñèñ KILL
KILL [CONNECTION | QUERY] ɢɞɟɧɬɢɮɢɤɚɬɨɪ_ɩɨɬɨɤɚ
Ʉɚɠɞɨɟ ɩɨɞɤɥɸɱɟɧɢɟ ɤ mysqld ɡɚɩɭɫɤɚɟɬ ɨɬɞɟɥɶɧɵɣ ɩɨɬɨɤ. ȼɵ ɦɨɠɟɬɟ ɩɪɨɫɦɨɬɪɟɬɶ
ɜɫɟ ɜɵɩɨɥɧɹɸɳɢɟɫɹ ɩɨɬɨɤɢ ɫ ɩɨɦɨɳɶɸ SHOW PROCESSLIST ɢ ɩɪɟɪɜɚɬɶ ɥɸɛɨɣ ɢɡ ɧɢɯ ɫ
ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ KILL ɢɞɟɧɬɢɮɢɤɚɬɨɪ_ɩɨɬɨɤɚ.
ɇɚɱɢɧɚɹ ɫ MySQL 5.0.0, KILL ɩɪɟɞɭɫɦɚɬɪɢɜɚɟɬ ɧɟɨɛɹɡɚɬɟɥɶɧɵɟ ɦɨɞɢɮɢɤɚɬɨɪɵ
CONNECTION ɢ QUERY:
– ɷɬɨ ɬɨ ɠɟ ɫɚɦɨɟ, ɱɬɨ KILL ɛɟɡ ɦɨɞɢɮɢɤɚɬɨɪɨɜ. ɗɬɨ ɩɪɟɪɵɜɚɟɬ
ɫɨɟɞɢɧɟɧɢɟ, ɚɫɫɨɰɢɢɪɨɜɚɧɧɨɟ ɫ ɭɤɚɡɚɧɧɵɦ ɩɨɬɨɤɨɦ ɢɞɟɧɬɢɮɢɤɚɬɨɪ_ɩɨɬɨɤɚ.
„KILL CONNECTION
„KILL QUERY
ɩɪɟɪɵɜɚɟɬ ɨɩɟɪɚɬɨɪ, ɤɨɬɨɪɵɣ ɜ ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɜɵɩɨɥɧɹɟɬɫɹ ɜ ɫɨɟɞɢɧɟɧɢɢ, ɧɨ ɨɫɬɚɜɥɹɟɬ ɫɨɟɞɢɧɟɧɢɟ ɧɟ ɬɪɨɧɭɬɵɦ.
318
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ȿɫɥɢ ɭ ɜɚɫ ɟɫɬɶ ɩɪɢɜɢɥɟɝɢɹ PROCESS, ɜɵ ɦɨɠɟɬɟ ɜɢɞɟɬɶ ɜɫɟ ɩɨɬɨɤɢ ɫɟɪɜɟɪɚ. ɂɦɟɹ ɩɪɢɜɢɥɟɝɢɸ SUPER, ɜɵ ɦɨɠɟɬɟ ɩɪɟɪɵɜɚɬɶ ɥɸɛɨɣ ɩɨɬɨɤ ɢ ɥɸɛɨɣ ɨɩɟɪɚɬɨɪ. ȼ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ ɜɵ ɫɦɨɠɟɬɟ ɜɢɞɟɬɶ ɢ ɩɪɟɪɵɜɚɬɶ ɬɨɥɶɤɨ ɫɜɨɢ ɫɨɛɫɬɜɟɧɧɵɟ ɩɨɬɨɤɢ ɢ ɨɩɟɪɚɬɨɪɵ.
Ⱦɥɹ ɩɪɨɫɦɨɬɪɚ ɢ ɩɪɟɪɵɜɚɧɢɹ ɩɨɬɨɤɨɜ ɬɚɤɠɟ ɢɫɩɨɥɶɡɭɸɬɫɹ mysqladmin processlist ɢ
mysqladmin kill.
oË ÑËÕÏÛµÂ!
ȼ ɧɚɫɬɨɹɳɟɟ ɜɪɟɦɹ ɜɵ ɧɟ ɦɨɠɟɬɟ ɩɪɢɦɟɧɹɬɶ KILL ɫ ɛɢɛɥɢɨɬɟɤɨɣ ɜɫɬɪɨɟɧɧɨɝɨ ɫɟɪɜɟɪɚ MySQL,
ɬɚɤ ɤɚɤ ɜɫɬɪɨɟɧɧɵɣ ɫɟɪɜɟɪ ɡɚɩɭɫɤɚɟɬɫɹ ɜɧɭɬɪɢ ɩɨɬɨɤɨɜ ɩɪɢɥɨɠɟɧɢɹ ɯɨɫɬɚ; ɨɧ ɧɟ ɫɨɡɞɚɟɬ ɫɜɨɢɯ
ɫɨɛɫɬɜɟɧɧɵɯ ɩɨɬɨɤɨɜ.
Ʉɨɝɞɚ ɜɵ ɜɵɩɨɥɧɹɟɬɟ KILL, ɭɫɬɚɧɚɜɥɢɜɚɟɬɫɹ ɫɩɟɰɢɚɥɶɧɵɣ ɮɥɚɝ ɩɪɟɪɵɜɚɧɢɹ ɩɨɬɨɤɚ. ȼ
ɛɨɥɶɲɢɧɫɬɜɟ ɫɥɭɱɚɟɜ ɦɨɠɟɬ ɩɨɬɪɟɛɨɜɚɬɶɫɹ ɧɟɤɨɬɨɪɨɟ ɜɪɟɦɹ ɞɥɹ ɪɟɚɥɶɧɨɣ ɨɫɬɚɧɨɜɤɢ
ɜɵɩɨɥɧɟɧɢɹ ɩɨɬɨɤɚ, ɩɨɫɤɨɥɶɤɭ ɮɥɚɝɢ ɩɪɟɪɵɜɚɧɢɹ ɩɪɨɜɟɪɹɸɬɫɹ ɫɟɪɜɟɪɨɦ ɬɨɥɶɤɨ ɱɟɪɟɡ
ɨɩɪɟɞɟɥɟɧɧɵɟ ɢɧɬɟɪɜɚɥɵ ɜɪɟɦɟɧɢ.
„ȼ ɰɢɤɥɚɯ SELECT, ORDER BY ɢ GROUP BY, ɮɥɚɝ ɩɪɨɜɟɪɹɟɬɫɹ ɩɨɫɥɟ ɱɬɟɧɢɹ ɛɥɨɤɚ
ɫɬɪɨɤ. ȿɫɥɢ ɮɥɚɝ ɩɪɟɪɵɜɚɧɢɹ ɭɫɬɚɧɨɜɥɟɧ, ɜɵɩɨɥɧɟɧɢɟ ɨɩɟɪɚɬɨɪɚ ɩɪɟɤɪɚɳɚɟɬɫɹ.
„ȼɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ ALTER TABLE ɮɥɚɝ ɩɪɟɪɵɜɚɧɢɹ ɩɪɨɜɟɪɹɟɬɫɹ ɩɟɪɟɞ ɱɬɟɧɢɟɦ
ɛɥɨɤɚ ɫɬɪɨɤ ɢɡ ɢɫɯɨɞɧɨɣ ɬɚɛɥɢɰɵ. ȿɫɥɢ ɮɥɚɝ ɭɫɬɚɧɨɜɥɟɧ, ɜɵɩɨɥɧɟɧɢɟ ɨɩɟɪɚɬɨɪɚ
ɩɪɟɤɪɚɳɚɟɬɫɹ ɢ ɜɪɟɦɟɧɧɚɹ ɬɚɛɥɢɰɚ ɭɞɚɥɹɟɬɫɹ.
„ȼɨ ɜɪɟɦɹ ɜɵɩɨɥɧɟɧɢɹ UPDATE ɢɥɢ DELETE ɮɥɚɝ ɩɪɟɪɵɜɚɧɢɹ ɩɪɨɜɟɪɹɟɬɫɹ ɩɨɫɥɟ ɱɬɟɧɢɹ ɤɚɠɞɨɝɨ ɛɥɨɤɚ ɢ ɩɨɫɥɟ ɤɚɠɞɨɣ ɨɛɧɨɜɥɟɧɧɨɣ ɢɥɢ ɭɞɚɥɟɧɧɨɣ ɫɬɪɨɤɢ. ȿɫɥɢ ɮɥɚɝ
ɭɫɬɚɧɨɜɥɟɧ, ɜɵɩɨɥɧɟɧɢɟ ɨɩɟɪɚɬɨɪɚ ɩɪɟɤɪɚɳɚɟɬɫɹ. ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɟɫɥɢ ɧɟ
ɩɪɢɦɟɧɹɥɢɫɶ ɬɪɚɧɡɚɤɰɢɢ, ɢɡɦɟɧɟɧɢɹ ɧɟ ɨɬɤɚɬɵɜɚɸɬɫɹ!
„GET_LOCK() ɩɪɟɪɵɜɚɟɬɫɹ ɢ ɜɨɡɜɪɚɳɚɟɬ NULL.
ɨɛɫɥɭɠɢɜɚɸɳɢɣ INSERT DELAYED, ɧɟɦɟɞɥɟɧɧɨ ɫɛɪɚɫɵɜɚɟɬ ɜɫɟ ɫɬɪɨɤɢ, ɤɨɬɨɪɵɟ ɧɚɯɨɞɹɬɫɹ ɜ ɩɚɦɹɬɢ, ɢ ɩɪɟɪɵɜɚɟɬɫɹ.
„ȿɫɥɢ ɩɨɬɨɤ ɭɞɟɪɠɢɜɚɟɬ ɛɥɨɤɢɪɨɜɤɭ ɬɚɛɥɢɰɵ, ɛɥɨɤɢɪɨɜɤɚ ɧɟɦɟɞɥɟɧɧɨ ɨɬɦɟɧɹɟɬɫɹ.
„ȿɫɥɢ ɩɨɬɨɤ ɨɠɢɞɚɟɬ ɨɫɜɨɛɨɠɞɟɧɢɹ ɞɢɫɤɨɜɨɝɨ ɩɪɨɫɬɪɚɧɫɬɜɚ, ɱɬɨɛɵ ɜɵɩɨɥɧɢɬɶ ɡɚɩɢɫɶ, ɩɪɨɰɟɞɭɪɚ ɡɚɩɢɫɢ ɩɪɟɪɵɜɚɟɬɫɹ ɫ ɫɨɨɛɳɟɧɢɟɦ ɨɛ ɨɲɢɛɤɟ “ɩɟɪɟɩɨɥɧɟɧɢɟ
ɞɢɫɤɚ”.
„ɉɨɬɨɤ,
6.5.4.4. Ñèíòàêñèñ LOAD INDEX INTO CACHE
LOAD INDEX INTO CACHE
ɫɩɢɫɨɤ_ɢɧɞɟɤɫɨɜ_ɬɚɛɥɢɰɵ [, ɫɩɢɫɨɤ_ɢɧɞɟɤɫɨɜ_ɬɚɛɥɢɰɵ] ...
ɫɩɢɫɨɤ_ɢɧɞɟɤɫɨɜ_ɬɚɛɥɢɰɵ:
ɢɦɹ_ɬɚɛɥɢɰɵ
[[INDEX] (ɢɦɹ_ɢɧɞɟɤɫɚ [, ɢɦɹ_ɢɧɞɟɤɫɚ] ...)]
[IGNORE LEAVES]
Ɉɩɟɪɚɬɨɪ LOAD INDEX INTO CACHE ɩɪɟɞɜɚɪɢɬɟɥɶɧɨ ɡɚɝɪɭɠɚɟɬ ɢɧɞɟɤɫ ɬɚɛɥɢɰɵ ɜ ɤɷɲ
ɤɥɸɱɟɣ, ɫ ɤɨɬɨɪɵɦ ɨɧ ɫɜɹɡɚɧ ɹɜɧɵɦ ɜɵɡɨɜɨɦ ɨɩɟɪɚɬɨɪɚ CACHE INDEX, ɥɢɛɨ ɜ ɤɷɲ ɤɥɸɱɟɣ
ɩɨ ɭɦɨɥɱɚɧɢɸ ɜ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ. LOAD INDEX INTO CACHE ɩɪɢɦɟɧɹɟɬɫɹ ɬɨɥɶɤɨ ɜ ɨɬɧɨɲɟɧɢɢ ɬɚɛɥɢɰ MyISAM.
Ɇɨɞɢɮɢɤɚɬɨɪ IGNORE LEAVES ɡɚɝɪɭɡɤɭ ɜ ɤɷɲ ɬɨɥɶɤɨ ɛɥɨɤɨɜ ɧɟɥɢɫɬɨɜɵɯ ɭɡɥɨɜ ɢɧɞɟɤɫɧɨɝɨ ɞɟɪɟɜɚ.
6.6. Îïåðàòîðû ðåïëèêàöèè
319
ɋɥɟɞɭɸɳɢɣ ɨɩɟɪɚɬɨɪ ɩɪɟɞɜɚɪɢɬɟɥɶɧɨ ɡɚɝɪɭɠɚɟɬ ɭɡɥɵ (ɢɧɞɟɤɫɧɵɟ ɛɥɨɤɢ) ɢɧɞɟɤɫɨɜ
ɬɚɛɥɢɰ t1 ɢ t2:
mysql> LOAD INDEX INTO CACHE t1, t2 IGNORE LEAVES;
+---------+--------------+----------+----------+
| Table
| Op
| Msg_type | Msg_text |
+---------+--------------+----------+----------+
| test.t1 | preload_keys | status
| OK
|
| test.t2 | preload_keys | status
| OK
|
+---------+--------------+----------+----------+
ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɩɪɟɞɜɚɪɢɬɟɥɶɧɨ ɡɚɝɪɭɠɚɟɬ ɜɫɟ ɢɧɞɟɤɫɧɵɟ ɛɥɨɤɢ t1 ɢ ɧɟɥɢɫɬɨɜɵɟ ɭɡɥɵ t2.
ɋɢɧɬɚɤɫɢɫ LOAD INDEX INTO CACHE ɩɨɡɜɨɥɹɟɬ ɭɤɚɡɚɬɶ, ɱɬɨ ɩɪɟɞɜɚɪɢɬɟɥɶɧɨ ɡɚɝɪɭɠɚɬɶɫɹ ɜ ɤɷɲ ɧɭɠɧɨ ɬɨɥɶɤɨ ɨɬɞɟɥɶɧɵɟ ɢɧɞɟɤɫɵ ɬɚɛɥɢɰɵ. Ɉɞɧɚɤɨ ɬɟɤɭɳɚɹ ɪɟɚɥɢɡɚɰɢɹ ɡɚɝɪɭɠɚɟɬ ɫɪɚɡɭ ɜɫɟ ɢɧɞɟɤɫɵ ɬɚɛɥɢɰɵ ɜ ɤɷɲ, ɩɨɷɬɨɦɭ ɧɟɬ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɫɩɟɰɢɮɢɰɢɪɨɜɚɬɶ
ɱɬɨ-ɬɨ ɤɪɨɦɟ ɢɦɟɧɢ ɬɚɛɥɢɰɵ.
LOAD INDEX INTO CACHE ɛɵɥ ɞɨɛɚɜɥɟɧ ɜ MySQL 4.1.1.
6.5.4.5. Ñèíòàêñèñ RESET
RESET ɨɩɰɢɹ_ɫɛɪɨɫɚ [, ɨɩɰɢɹ_ɫɛɪɨɫɚ] ...
Ɉɩɟɪɚɬɨɪ RESET ɢɫɩɨɥɶɡɭɟɬɫɹ ɞɥɹ ɨɱɢɫɬɤɢ ɫɨɫɬɨɹɧɢɹ ɪɚɡɥɢɱɧɵɯ ɨɩɟɪɚɰɢɣ ɫɟɪɜɟɪɚ.
Ɉɧ ɬɚɤɠɟ ɜɵɫɬɭɩɚɟɬ ɤɚɤ ɛɨɥɟɟ ɫɬɪɨɝɚɹ ɜɟɪɫɢɹ ɨɩɟɪɚɬɨɪɚ FLUSH (ɫɦ. ɪɚɡɞɟɥ 6.5.4.2).
ɑɬɨɛɵ ɜɵɩɨɥɧɢɬɶ RESET, ɧɟɨɛɯɨɞɢɦɨ ɨɛɥɚɞɚɬɶ ɩɪɢɜɢɥɟɝɢɟɣ RELOAD.
ɨɩɰɢɹ_ɫɛɪɨɫɚ ɦɨɠɟɬ ɛɵɬɶ ɨɞɧɨɣ ɢɡ ɫɥɟɞɭɸɳɢɯ:
„MASTER.
ɍɞɚɥɹɟɬ ɜɫɟ ɛɢɧɚɪɧɵɟ ɠɭɪɧɚɥɵ, ɩɟɪɟɱɢɫɥɟɧɧɵɟ ɜ ɢɧɞɟɤɫɧɨɦ ɮɚɣɥɟ, ɨɱɢɳɚɟɬ ɢɧɞɟɤɫɧɵɣ ɮɚɣɥ ɛɢɧɚɪɧɨɝɨ ɠɭɪɧɚɥɚ ɢ ɫɨɡɞɚɟɬ ɧɨɜɵɣ ɮɚɣɥ ɛɢɧɚɪɧɨɝɨ ɠɭɪɧɚɥɚ.
„QUERY CACHE. ɍɞɚɥɹɟɬ ɜɫɟ ɪɟɡɭɥɶɬɚɬɵ ɢɡ ɤɷɲɚ ɡɚɩɪɨɫɨɜ.
„SLAVE.
Ɂɚɫɬɚɜɥɹɟɬ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ “ɡɚɛɵɬɶ” ɫɜɨɸ ɩɨɡɢɰɢɸ ɪɟɩɥɢɤɚɰɢɢ ɜ ɛɢɧɚɪɧɨɦ ɠɭɪɧɚɥɟ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ. Ɋɚɧɟɟ ɨɩɟɪɚɬɨɪ ɧɚɡɵɜɚɥɫɹ FLUSH SLAVE. ɋɦ.
ɪɚɡɞɟɥ 6.6.2.
6.6. Îïåðàòîðû ðåïëèêàöèè
6.6.1. Îïåðàòîðû SQL äëÿ óïðàâëåíèÿ
ãëàâíûìè ñåðâåðàìè
Ɋɟɩɥɢɤɚɰɢɟɣ ɦɨɠɧɨ ɭɩɪɚɜɥɹɬɶ ɱɟɪɟɡ SQL-ɢɧɬɟɪɮɟɣɫ. ȼ ɧɚɫɬɨɹɳɟɦ ɪɚɡɞɟɥɟ ɨɩɢɫɵɜɚɸɬɫɹ ɨɩɟɪɚɬɨɪɵ ɞɥɹ ɭɩɪɚɜɥɟɧɢɹ ɝɥɚɜɧɵɦɢ ɫɟɪɜɟɪɚɦɢ ɪɟɩɥɢɤɚɰɢɢ. Ɋɚɡɞɟɥ 6.6.2 ɩɨɫɜɹɳɟɧ ɭɩɪɚɜɥɟɧɢɸ ɩɨɞɱɢɧɟɧɧɵɦɢ ɫɟɪɜɟɪɚɦɢ.
6.6.1.1. Ñèíòàêñèñ PURGE MASTER LOGS
PURGE {MASTER | BINARY} LOGS TO 'ɢɦɹ_ɠɭɪɧɚɥɚ'
PURGE {MASTER | BINARY} LOGS BEFORE 'ɞɚɬɚ'
ɍɞɚɥɹɟɬ ɜɫɟ ɛɢɧɚɪɧɵɟ ɠɭɪɧɚɥɵ, ɩɟɪɟɱɢɫɥɟɧɧɵɟ ɜ ɢɧɞɟɤɫɟ ɠɭɪɧɚɥɚ, ɤɨɬɨɪɵɟ ɫɬɪɨɝɨ
ɩɪɟɞɲɟɫɬɜɭɸɬ ɡɚɞɚɧɧɨɦɭ ɢɦɟɧɢ ɢɥɢ ɞɚɬɟ. ɉɪɢ ɷɬɨɦ ɨɧɢ ɬɚɤɠɟ ɭɞɚɥɹɸɬɫɹ ɢɡ ɫɩɢɫɤɚ ɜ ɢɧɞɟɤɫɧɨɦ ɮɚɣɥɟ ɠɭɪɧɚɥɚ ɢ, ɬɚɤɢɦ ɨɛɪɚɡɨɦ, ɡɚɞɚɧɧɵɣ ɮɚɣɥ ɠɭɪɧɚɥɚ ɫɬɚɧɨɜɢɬɫɹ ɩɟɪɜɵɦ.
320
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɉɪɢɦɟɪɵ:
PURGE MASTER LOGS TO 'mysql-bin.010';
PURGE MASTER LOGS BEFORE '2003-04-02 22:46:26';
ȼɚɪɢɚɧɬ BEFORE ɞɨɫɬɭɩɟɧ, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.1. Ⱥɪɝɭɦɟɧɬ ɞɚɬɵ ɦɨɠɟɬ ɛɵɬɶ
ɩɪɟɞɫɬɚɜɥɟɧ ɜ ɮɨɪɦɚɬɟ 'ȽȽȽȽ-ɆɆ-ȾȾ ɱɱ:ɦɦ:ɫɫ'. MASTER ɢ BINARY ɹɜɥɹɸɬɫɹ ɫɢɧɨɧɢɦɚɦɢ,
ɧɨ BINARY ɩɨɹɜɢɥɫɹ ɬɨɥɶɤɨ ɜ ɜɟɪɫɢɢ MySQL 4.1.1.
ȿɫɥɢ ɟɫɬɶ ɚɤɬɢɜɧɵɣ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ, ɜ ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɱɢɬɚɸɳɢɣ ɠɭɪɧɚɥ, ɤɨɬɨɪɵɣ ɜɵ ɩɵɬɚɟɬɟɫɶ ɭɞɚɥɢɬɶ, ɷɬɨɬ ɨɩɟɪɚɬɨɪ ɧɟ ɞɟɥɚɟɬ ɧɢɱɟɝɨ ɢ ɡɚɜɟɪɲɚɟɬɫɹ ɨɲɢɛɤɨɣ. Ɉɞɧɚɤɨ ɟɫɥɢ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɩɪɟɛɵɜɚɟɬ ɜ ɛɟɡɞɟɣɫɬɜɢɢ, ɚ ɜɵ ɩɵɬɚɟɬɟɫɶ ɭɞɚɥɢɬɶ ɨɞɢɧ ɢɡ
ɠɭɪɧɚɥɨɜ, ɤɨɬɨɪɵɟ ɨɧ ɯɨɱɟɬ ɱɢɬɚɬɶ, ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɧɟ ɫɦɨɠɟɬ ɩɪɨɞɨɥɠɚɬɶ ɪɟɩɥɢɤɚɰɢɸ, ɤɨɝɞɚ ɜɟɪɧɟɬɫɹ ɤ ɪɚɛɨɬɟ. ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɛɟɡɨɩɚɫɟɧ, ɟɫɥɢ ɩɨɞɱɢɧɟɧɧɵɟ ɫɟɪɜɟɪɵ
ɡɚɧɹɬɵ ɪɟɩɥɢɤɚɰɢɟɣ. ɇɟɬ ɧɟɨɛɯɨɞɢɦɨɫɬɢ ɨɫɬɚɧɚɜɥɢɜɚɬɶ ɢɯ.
Ⱦɥɹ ɨɱɢɫɬɤɢ ɠɭɪɧɚɥɨɜ ɜɵɩɨɥɧɢɬɟ ɩɟɪɟɱɢɫɥɟɧɧɵɟ ɧɢɠɟ ɲɚɝɢ.
1. ɇɚ ɤɚɠɞɨɦ ɩɨɞɱɢɧɟɧɧɨɦ ɫɟɪɜɟɪɟ ɫ ɩɨɦɨɳɶɸ SHOW SLAVE STATUS ɨɩɪɟɞɟɥɢɬɟ, ɤɚɤɨɣ ɠɭɪɧɚɥ ɜ ɧɚɫɬɨɹɳɢɣ ɦɨɦɟɧɬ ɱɢɬɚɟɬɫɹ.
2. ɉɨɥɭɱɢɬɟ ɫɩɢɫɨɤ ɠɭɪɧɚɥɨɜ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ ɫ ɩɨɦɨɳɶɸ ɨɩɟɪɚɬɨɪɚ SHOW MASTER
LOGS.
3. Ɉɩɪɟɞɟɥɢɬɟ ɫɚɦɵɣ ɪɚɧɧɢɣ ɠɭɪɧɚɥ ɫɪɟɞɢ ɩɨɞɱɢɧɟɧɧɵɯ ɫɟɪɜɟɪɨɜ. ɗɬɨ ɢ ɛɭɞɟɬ ɰɟɥɟɜɨɣ ɠɭɪɧɚɥ. ȿɫɥɢ ɜɫɟ ɠɭɪɧɚɥɵ ɧɚ ɩɨɞɱɢɧɟɧɧɵɯ ɫɟɪɜɟɪɚɯ ɹɜɥɹɸɬɫɹ ɚɤɬɭɚɥɶɧɵɦɢ, ɰɟɥɟɜɵɦ ɛɭɞɟɬ ɩɨɫɥɟɞɧɢɣ ɠɭɪɧɚɥ ɜ ɫɩɢɫɤɟ.
4. ɋɞɟɥɚɣɬɟ ɪɟɡɟɪɜɧɭɸ ɤɨɩɢɸ ɜɫɟɯ ɠɭɪɧɚɥɨɜ, ɤɨɬɨɪɵɟ ɫɨɛɢɪɚɟɬɟɫɶ ɭɞɚɥɹɬɶ. (ɗɬɨ ɧɟ
ɨɛɹɡɚɬɟɥɶɧɨ, ɧɨ ɠɟɥɚɬɟɥɶɧɨ.)
5. Ɉɱɢɫɬɢɬɟ ɜɫɟ ɠɭɪɧɚɥɵ ɡɚ ɢɫɤɥɸɱɟɧɢɟɦ ɰɟɥɟɜɨɝɨ.
6.6.1.2. Ñèíòàêñèñ RESET MASTER
RESET MASTER
ɍɞɚɥɹɟɬ ɜɫɟ ɛɢɧɚɪɧɵɟ ɠɭɪɧɚɥɵ, ɩɟɪɟɱɢɫɥɟɧɧɵɟ ɜ ɢɧɞɟɤɫɧɨɦ ɮɚɣɥɟ, ɨɱɢɳɚɟɬ ɫɚɦ ɢɧɞɟɤɫɧɵɣ ɮɚɣɥ ɢ ɫɨɡɞɚɟɬ ɧɨɜɵɣ ɮɚɣɥ ɛɢɧɚɪɧɨɝɨ ɠɭɪɧɚɥɚ.
Ⱦɨ ɜɟɪɫɢɢ MySQL 3.23.26 ɷɬɨɬ ɨɩɟɪɚɬɨɪ ɧɚɡɵɜɚɥɫɹ FLUSH MASTER.
6.6.1.3. Ñèíòàêñèñ SET SQL_LOG_BIN
SET SQL_LOG_BIN = {0 | 1}
ȼɤɥɸɱɚɟɬ ɢ ɜɵɤɥɸɱɚɟɬ ɛɢɧɚɪɧɭɸ ɪɟɝɢɫɬɪɚɰɢɸ ɬɟɤɭɳɟɝɨ ɫɨɟɞɢɧɟɧɢɹ (SQL_LOG_BIN –
ɷɬɨ ɩɟɪɟɦɟɧɧɚɹ ɫɟɚɧɫɚ), ɟɫɥɢ ɤɥɢɟɧɬ ɩɨɞɤɥɸɱɟɧ, ɢɫɩɨɥɶɡɭɹ ɭɱɟɬɧɭɸ ɡɚɩɢɫɶ ɫ ɩɪɢɜɢɥɟɝɢɟɣ SUPER. Ɉɩɟɪɚɬɨɪ ɛɭɞɟɬ ɨɬɤɥɨɧɟɧ ɫ ɨɲɢɛɤɨɣ, ɟɫɥɢ ɤɥɢɟɧɬ ɧɟ ɢɦɟɟɬ ɷɬɨɣ ɩɪɢɜɢɥɟɝɢɢ.
(Ⱦɨ MySQL 4.1.2 ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɨɩɟɪɚɬɨɪ ɩɨɩɪɨɫɬɭ ɢɝɧɨɪɢɪɨɜɚɥɫɹ.)
6.6.1.4. Ñèíòàêñèñ SHOW BINLOG EVENTS
SHOW BINLOG EVENTS
[IN 'ɢɦɹ_ɠɭɪɧɚɥɚ'] [FROM ɩɨɡɢɰɢɹ] [LIMIT [ɫɦɟɳɟɧɢɟ,] ɤɨɥɢɱɟɫɬɜɨ_ɫɬɪɨɤ]
ɉɨɤɚɡɵɜɚɟɬ ɫɨɛɵɬɢɹ, ɡɚɧɟɫɟɧɧɵɟ ɜ ɛɢɧɚɪɧɵɣ ɠɭɪɧɚɥ. ȿɫɥɢ ɧɟ ɭɤɚɡɵɜɚɬɶ
'ɢɦɹ_ɠɭɪɧɚɥɚ', ɨɬɨɛɪɚɠɚɟɬɫɹ ɩɟɪɜɵɣ ɛɢɧɚɪɧɵɣ ɠɭɪɧɚɥ.
Ʉɨɧɫɬɪɭɤɰɢɹ LIMIT ɢɦɟɟɬ ɬɨɬ ɠɟ ɫɢɧɬɚɤɫɢɫ, ɱɬɨ ɢ ɜ ɨɩɟɪɚɬɨɪɟ SELECT (ɫɦ. ɪɚɡɞɟɥ 6.1.7).
ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɞɨɫɬɭɩɟɧ, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.0.
6.6. Îïåðàòîðû ðåïëèêàöèè
321
6.6.1.5. Ñèíòàêñèñ SHOW MASTER LOGS
SHOW MASTER LOGS
ȼɵɜɨɞɢɬ ɫɩɢɫɨɤ ɮɚɣɥɨɜ ɛɢɧɚɪɧɵɯ ɠɭɪɧɚɥɨɜ ɧɚ ɝɥɚɜɧɨɦ ɫɟɪɜɟɪɟ. ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɩɪɢɦɟɧɹɟɬɫɹ ɤɚɤ ɱɚɫɬɶ ɩɪɨɰɟɞɭɪɵ, ɨɩɢɫɚɧɧɨɣ ɜ ɪɚɡɞɟɥɟ 6.6.1.1, ɱɬɨɛɵ ɨɩɪɟɞɟɥɢɬɶ, ɤɚɤɢɟ
ɠɭɪɧɚɥɵ ɦɨɝɭɬ ɛɵɬɶ ɨɱɢɳɟɧɵ.
6.6.1.6. Ñèíòàêñèñ SHOW MASTER STATUS
SHOW MASTER STATUS
ɉɪɟɞɫɬɚɜɥɹɟɬ ɢɧɮɨɪɦɚɰɢɸ ɨ ɫɨɫɬɨɹɧɢɢ ɮɚɣɥɨɜ ɛɢɧɚɪɧɵɯ ɠɭɪɧɚɥɨɜ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ.
6.6.1.7. Ñèíòàêñèñ SHOW SLAVE HOSTS
SHOW SLAVE HOSTS
ȼɵɜɨɞɢɬ ɫɩɢɫɨɤ ɩɨɞɱɢɧɟɧɧɵɯ ɫɟɪɜɟɪɨɜ, ɡɚɪɟɝɢɫɬɪɢɪɨɜɚɧɧɵɯ ɜ ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɧɚ
ɝɥɚɜɧɨɦ. Ʌɸɛɨɣ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ, ɤɨɬɨɪɵɣ ɧɟ ɛɵɥ ɡɚɩɭɳɟɧ ɫ ɨɩɰɢɟɣ --report-host=
ɢɦɹ_ɩɨɞɱɢɧɟɧɧɨɝɨ_ɫɟɪɜɟɪɚ, ɜ ɫɩɢɫɤɟ ɩɨɤɚɡɚɧ ɧɟ ɛɭɞɟɬ.
6.6.2. SQL-îïåðàòîðû äëÿ óïðàâëåíèÿ
ïîä÷èíåííûìè ñåðâåðàìè
Ɋɟɩɥɢɤɚɰɢɟɣ ɦɨɠɧɨ ɭɩɪɚɜɥɹɬɶ ɱɟɪɟɡ SQL-ɢɧɬɟɪɮɟɣɫ. ȼ ɧɚɫɬɨɹɳɟɦ ɪɚɡɞɟɥɟ ɨɩɢɫɵɜɚɸɬɫɹ ɨɩɟɪɚɬɨɪɵ ɞɥɹ ɭɩɪɚɜɥɟɧɢɹ ɩɨɞɱɢɧɟɧɧɵɦɢ ɫɟɪɜɟɪɚɦɢ ɪɟɩɥɢɤɚɰɢɢ. Ɋɚɡɞɟɥ 6.6.1
ɩɨɫɜɹɳɟɧ ɭɩɪɚɜɥɟɧɢɸ ɝɥɚɜɧɵɦɢ ɫɟɪɜɟɪɚɦɢ.
6.6.2.1. Ñèíòàêñèñ CHANGE MASTER TO
CHANGE MASTER TO ɨɩɪɟɞɟɥɟɧɢɟ_ɝɥɚɜɧɨɝɨ_ɫɟɪɜɟɪɚ
[, ɨɩɪɟɞɟɥɟɧɢɟ_ɝɥɚɜɧɨɝɨ_ɫɟɪɜɟɪɚ] ...
ɨɩɪɟɞɟɥɟɧɢɟ_ɝɥɚɜɧɨɝɨ_ɫɟɪɜɟɪɚ:
MASTER_HOST = 'ɢɦɹ_ɯɨɫɬɚ'
| MASTER_USER = 'ɢɦɹ_ɩɨɥɶɡɨɜɚɬɟɥɹ'
| MASTER_PASSWORD = 'ɩɚɪɨɥɶ'
| MASTER_PORT = ɧɨɦɟɪ_ɩɨɪɬɚ
| MASTER_CONNECT_RETRY = ɤɨɥɢɱɟɫɬɜɨ
| MASTER_LOG_FILE = 'ɢɦɹ_ɠɭɪɧɚɥɚ_ɝɥɚɜɧɨɝɨ_ɫɟɪɜɟɪɚ'
| MASTER_LOG_POS = ɩɨɡɢɰɢɹ_ɜ_ɠɭɪɧɚɥɟ_ɝɥɚɜɧɨɝɨ_ɫɟɪɜɟɪɚ
| RELAY_LOG_FILE = 'ɢɦɹ_ɠɭɪɧɚɥɚ_ɪɟɬɪɚɧɫɥɹɰɢɣ'
| RELAY_LOG_POS = ɩɨɡɢɰɢɹ_ɜ_ɠɭɪɧɚɥɟ_ɪɟɬɪɚɧɫɥɹɰɢɣ
| MASTER_SSL = {0|1}
| MASTER_SSL_CA = 'ɢɦɹ_ɮɚɣɥɚ_ca'
| MASTER_SSL_CAPATH = 'ɢɦɹ_ɤɚɬɚɥɨɝɚ_ca'
| MASTER_SSL_CERT = 'ɢɦɹ_ɮɚɣɥɚ_ɫɟɪɬɢɮɢɤɚɬɚ'
| MASTER_SSL_KEY = 'ɢɦɹ_ɮɚɣɥɚ_ɤɥɸɱɟɣ'
| MASTER_SSL_CIPHER = 'ɫɩɢɫɨɤ_ɲɢɮɪɨɜ'
ɂɡɦɟɧɹɟɬ ɩɚɪɚɦɟɬɪɵ, ɤɨɬɨɪɵɟ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɢɫɩɨɥɶɡɭɟɬ ɞɥɹ ɩɨɞɤɥɸɱɟɧɢɹ ɢ
ɜɡɚɢɦɨɞɟɣɫɬɜɢɹ ɫ ɜɟɞɭɳɢɦ ɫɟɪɜɟɪɨɦ.
MASTER_USER, MASTER_PASSWORD, MASTER_SSL, MASTER_SSL_CA, MASTER_SSL_CAPATH,
MASTER_SSL_CERT, MASTER_SSL_KEY ɢ MASTER_SSL_CIPHER ɩɪɟɞɨɫɬɚɜɥɹɸɬ ɢɧɮɨɪɦɚɰɢɸ
ɩɨɞɱɢɧɟɧɧɨɦɭ ɫɟɪɜɟɪɭ ɨ ɬɨɦ, ɤɚɤ ɩɨɞɤɥɸɱɢɬɶɫɹ ɤ ɝɥɚɜɧɨɦɭ.
322
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
Ɉɩɰɢɢ ɭɩɪɚɜɥɟɧɢɹ ɪɟɬɪɚɧɫɥɹɰɢɟɣ (RELAY_LOG_FILE ɢ RELAY_LOG_POS) ɞɨɫɬɭɩɧɵ, ɧɚɱɢɧɚɹ ɫ MySQL 4.0.
Ɉɩɰɢɢ SSL (MASTER_SSL, MASTER_SSL_CA, MASTER_SSL_CAPATH, MASTER_SSL_CERT,
MASTER_SSL_KEY ɢ MASTER_SSL_CIPHER) ɞɨɫɬɭɩɧɵ, ɧɚɱɢɧɚɹ ɫ ɜɟɪɫɢɢ MySQL 4.1.1. ȼɵ
ɦɨɠɟɬɟ ɢɡɦɟɧɹɬɶ ɷɬɢ ɨɩɰɢɢ ɞɚɠɟ ɧɚ ɩɨɞɱɢɧɟɧɧɵɯ ɫɟɪɜɟɪɚɯ, ɤɨɬɨɪɵɟ ɛɵɥɢ ɫɤɨɦɩɢɥɢɪɨɜɚɧɵ ɛɟɡ ɩɨɞɞɟɪɠɤɢ SSL. Ɉɧɢ ɫɨɯɪɚɧɹɸɬɫɹ ɜ ɮɚɣɥɟ master.info, ɧɨ ɢɝɧɨɪɢɪɭɸɬɫɹ ɞɨ
ɬɟɯ ɩɨɪ, ɩɨɤɚ ɜɵ ɧɟ ɢɫɩɨɥɶɡɭɟɬɟ ɫɟɪɜɟɪ ɫ ɩɨɞɞɟɪɠɤɨɣ SSL.
ȿɫɥɢ ɜɵ ɧɟ ɭɤɚɡɵɜɚɟɬɟ ɬɨɬ ɢɥɢ ɢɧɨɣ ɩɚɪɚɦɟɬɪ, ɫɨɯɪɚɧɹɟɬɫɹ ɟɝɨ ɫɬɚɪɨɟ ɡɧɚɱɟɧɢɟ, ɡɚ
ɢɫɤɥɸɱɟɧɢɟɦ ɨɩɢɫɚɧɧɵɯ ɧɢɠɟ ɫɥɭɱɚɟɜ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɢɡɦɟɧɢɥɫɹ ɩɚɪɨɥɶ ɞɥɹ ɩɨɞɤɥɸɱɟɧɢɹ ɤ ɝɥɚɜɧɨɦɭ ɫɟɪɜɟɪɭ MySQL, ɜɵ ɞɨɥɠɧɵ ɜɵɩɨɥɧɢɬɶ ɫɥɟɞɭɸɳɢɟ ɨɩɟɪɚɬɨɪɵ, ɱɬɨɛɵ
ɫɨɨɛɳɢɬɶ ɩɨɞɱɢɧɟɧɧɨɦɭ ɫɟɪɜɟɪɭ ɨ ɧɨɜɨɦ ɩɚɪɨɥɟ:
mysql> STOP SLAVE; -- ɟɫɥɢ ɪɟɩɥɢɤɚɰɢɹ ɛɵɥɚ ɡɚɩɭɳɟɧɚ
mysql> CHANGE MASTER TO MASTER_PASSWORD='new3cret';
mysql> START SLAVE; -- ɟɫɥɢ ɧɭɠɧɨ ɩɟɪɟɡɚɩɭɫɬɢɬɶ ɪɟɩɥɢɤɚɰɢɸ
ɉɚɪɚɦɟɬɪɵ, ɤɨɬɨɪɵɟ ɧɟ ɢɡɦɟɧɢɥɢɫɶ, ɭɤɚɡɵɜɚɬɶ ɧɟ ɧɭɠɧɨ (ɯɨɫɬ, ɩɨɪɬ, ɩɨɥɶɡɨɜɚɬɟɥɶ ɢ
ɬɚɤ ɞɚɥɟɟ).
MASTER_HOST ɢ MASTER_PORT – ɷɬɨ ɢɦɹ ɯɨɫɬɚ (ɢɥɢ IP-ɚɞɪɟɫ), ɧɚ ɤɨɬɨɪɨɦ ɧɚɯɨɞɢɬɫɹ
ɝɥɚɜɧɵɣ ɫɟɪɜɟɪ, ɢ ɧɨɦɟɪ ɟɝɨ ɩɨɪɬɚ TCP/IP. ɋɥɟɞɭɟɬ ɨɬɦɟɬɢɬɶ, ɱɬɨ ɟɫɥɢ MASTER_HOST ɹɜɥɹɟɬɫɹ localhost, ɬɨ, ɤɚɤ ɢ ɜ ɞɪɭɝɢɯ ɱɚɫɬɹɯ MySQL, ɩɨɪɬ ɦɨɠɟɬ ɛɵɬɶ ɩɪɨɢɝɧɨɪɢɪɨɜɚɧ
(ɟɫɥɢ ɦɨɝɭɬ ɛɵɬɶ ɢɫɩɨɥɶɡɨɜɚɧɵ ɫɨɤɟɬ-ɮɚɣɥɵ Unix, ɧɚɩɪɢɦɟɪ).
ȿɫɥɢ ɜɵ ɭɤɚɡɵɜɚɟɬɟ MASTER_HOST ɢɥɢ MASTER_PORT, ɬɨ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɩɪɟɞɩɨɥɚɝɚɟɬ, ɱɬɨ ɝɥɚɜɧɵɦ ɫɟɪɜɟɪɨɦ ɫɬɚɥ ɞɪɭɝɨɣ ɫɟɪɜɟɪ, ɧɟɠɟɥɢ ɪɚɧɶɲɟ (ɞɚɠɟ ɟɫɥɢ ɜɵ ɭɤɚɡɵɜɚɟɬɟ ɬɨɬ ɠɟ ɫɚɦɵɣ ɯɨɫɬ ɢ ɩɨɪɬ). ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɫɬɚɪɵɟ ɡɧɚɱɟɧɢɹ ɢɦɟɧɢ ɛɢɧɚɪɧɨɝɨ ɠɭɪɧɚɥɚ
ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ ɢ ɩɨɡɢɰɢɢ ɜ ɧɟɦ ɪɚɫɫɦɚɬɪɢɜɚɸɬɫɹ ɤɚɤ ɧɟɞɟɣɫɬɜɢɬɟɥɶɧɵɟ. ɉɨɷɬɨɦɭ,
ɟɫɥɢ ɜɵ ɧɟ ɭɤɚɡɵɜɚɟɬɟ ɜ ɨɩɟɪɚɬɨɪɟ MASTER_LOG_FILE ɢ MASTER_LOG_POS, ɨɧɢ ɩɪɢɧɢɦɚɸɬɫɹ
ɬɚɤɢɦɢ: MASTER_LOG_FILE='' ɢ MASTER_LOG_POS=4.
MASTER_LOG_FILE ɢ MASTER_LOG_POS – ɷɬɨ ɤɨɨɪɞɢɧɚɬɵ, ɩɨ ɤɨɬɨɪɵɦ ɩɨɬɨɤ ɜɜɨɞɚɜɵɜɨɞɚ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ ɧɚɱɧɟɬ ɱɬɟɧɢɟ ɛɢɧɚɪɧɨɝɨ ɠɭɪɧɚɥɚ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ ɩɪɢ
ɫɥɟɞɭɸɳɟɦ ɡɚɩɭɫɤɟ. ȿɫɥɢ ɜɵ ɭɤɚɡɵɜɚɟɬɟ ɨɛɚ ɷɬɢ ɩɚɪɚɦɟɬɪɚ, ɬɨ ɧɟ ɦɨɠɟɬɟ ɭɤɚɡɚɬɶ
RELAY_LOG_FILE ɢɥɢ RELAY_LOG_POS. ȿɫɥɢ ɠɟ ɧɟ ɭɤɚɡɚɧ ɧɢ MASTER_LOG_FILE, ɧɢ
MASTER_LOG_POS, ɬɨ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɢɫɩɨɥɶɡɭɟɬ ɩɨɫɥɟɞɧɢɟ ɤɨɨɪɞɢɧɚɬɵ ɩɨɬɨɤɚ SQL
ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ, ɤɨɬɨɪɵɟ ɛɵɥɢ ɩɟɪɟɞ ɬɟɦ, ɤɚɤ ɜɵɩɨɥɧɢɥɫɹ CHANGE MASTER. ɗɬɨ ɝɚɪɚɧɬɢɪɭɟɬ, ɱɬɨ ɪɟɩɥɢɤɚɰɢɹ ɧɟ ɛɭɞɟɬ ɢɦɟɬɶ ɧɢɤɚɤɢɯ ɪɚɡɪɵɜɨɜ, ɞɚɠɟ ɟɫɥɢ ɩɨɬɨɤ SQL ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ ɩɨɡɠɟ ɫɪɚɜɧɢɜɚɥɫɹ ɫ ɩɨɬɨɤɨɦ ɜɜɨɞɚ-ɜɵɜɨɞɚ. Ɍɚɤɨɟ ɛɟɡɨɩɚɫɧɨɟ ɩɨɜɟɞɟɧɢɟ ɛɵɥɨ ɩɪɟɞɫɬɚɜɥɟɧɨ ɜ MySQL 4.0.17 ɢ MySQL 4.1.1. (Ⱦɨ ɷɬɢɯ ɜɟɪɫɢɣ ɢɫɩɨɥɶɡɨɜɚɥɢɫɶ ɩɨɫɥɟɞɧɢɟ ɤɨɨɪɞɢɧɚɬɵ ɩɨɬɨɤɚ ɜɜɨɞɚ-ɜɵɜɨɞɚ, ɤɨɬɨɪɵɟ ɨɧ ɢɦɟɥɚ ɩɟɪɟɞ ɬɟɦ, ɤɚɤ
ɜɵɩɨɥɧɢɥɫɹ CHANGE MASTER. ɗɬɨ ɩɪɢɜɨɞɢɥɨ ɤ ɬɨɦɭ, ɱɬɨ ɩɨɬɨɤ SQL ɦɨɝ ɩɨɬɟɪɹɬɶ ɧɟɤɨɬɨɪɵɟ ɫɨɛɵɬɢɹ ɫ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ, ɬɚɤɢɦ ɨɛɪɚɡɨɦ, ɩɪɟɪɵɜɚɹ ɪɟɩɥɢɤɚɰɢɸ.)
CHANGE MASTER ɭɞɚɥɹɟɬ ɜɫɟ ɮɚɣɥɵ ɠɭɪɧɚɥɨɜ ɪɟɬɪɚɧɫɥɹɰɢɣ ɢ ɧɚɱɢɧɚɟɬ ɧɨɜɵɣ ɠɭɪɧɚɥ,
ɟɫɥɢ ɬɨɥɶɤɨ ɧɟ ɛɵɥɢ ɭɤɚɡɚɧɵ RELAY_LOG_FILE ɢɥɢ RELAY_LOG_POS. ȼ ɷɬɨɦ ɫɥɭɱɚɟ ɠɭɪɧɚɥɵ ɪɟɬɪɚɧɫɥɹɰɢɣ ɫɨɯɪɚɧɹɸɬɫɹ. ɋ MySQL 4.1.1 ɡɧɚɱɟɧɢɟ ɝɥɨɛɚɥɶɧɨɣ ɩɟɪɟɦɟɧɧɨɣ
relay_log_purge ɩɨ ɭɦɨɥɱɚɧɢɸ ɪɚɜɧɨ 0.
CHANGE MASTER ɨɛɧɨɜɥɹɟɬ ɫɨɞɟɪɠɢɦɨɟ ɮɚɣɥɨɜ master.info ɢ relay-log.info.
CHANGE MASTER ɩɪɢɦɟɧɢɦ ɞɥɹ ɧɚɫɬɪɨɣɤɢ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ, ɤɨɝɞɚ ɭ ɜɚɫ ɟɫɬɶ ɫɧɢɦɨɤ ɞɚɧɧɵɯ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ ɢ ɡɚɩɢɫɚɧɨ ɢɦɹ ɮɚɣɥɚ ɛɢɧɚɪɧɨɝɨ ɠɭɪɧɚɥɚ ɢ ɫɦɟɳɟɧɢɹ ɜ
6.6. Îïåðàòîðû ðåïëèêàöèè
323
ɧɟɦ, ɤɨɬɨɪɵɟ ɛɵɥɢ ɞɟɣɫɬɜɢɬɟɥɶɧɵ ɧɚ ɦɨɦɟɧɬ ɩɨɥɭɱɟɧɢɹ ɫɧɢɦɤɚ. ɉɨɫɥɟ ɡɚɝɪɭɡɤɢ ɫɧɢɦɤɚ
ɞɚɧɧɵɯ ɧɚ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɜɵ ɦɨɠɟɬɟ ɡɚɩɭɫɬɢɬɶ ɧɚ ɧɟɦ
CHANGE MASTER TO MASTER_LOG_FILE='ɢɦɹ_ɠɭɪɧɚɥɚ_ɝɥɚɜɧɨɝɨ_ɫɟɪɜɟɪɚ',
MASTER_LOG_POS=ɩɨɡɢɰɢɹ_ɜ_ɠɭɪɧɚɥɟ_ɝɥɚɜɧɨɝɨ_ɫɟɪɜɟɪɚ.
ɉɪɢɦɟɪɵ:
mysql> CHANGE MASTER TO
-> MASTER_HOST='master2.mycompany.com',
-> MASTER_USER='replication',
-> MASTER_PASSWORD='bigs3cret',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='master2-bin.001',
-> MASTER_LOG_POS=4,
-> MASTER_CONNECT_RETRY=10;
mysql> CHANGE MASTER TO
-> RELAY_LOG_FILE='slave-relay-bin.006',
-> RELAY_LOG_POS=4025;
ɉɟɪɜɵɣ ɩɪɢɦɟɪ ɦɟɧɹɟɬ ɝɥɚɜɧɵɣ ɫɟɪɜɟɪ ɢ ɤɨɨɪɞɢɧɚɬɵ ɟɝɨ ɛɢɧɚɪɧɨɝɨ ɠɭɪɧɚɥɚ. ɗɬɨ
ɩɪɢɦɟɧɹɟɬɫɹ, ɤɨɝɞɚ ɜɵ ɯɨɬɢɬɟ ɧɚɫɬɪɨɢɬɶ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɞɥɹ ɪɟɩɥɢɤɚɰɢɢ ɞɚɧɧɵɯ ɫ
ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ.
ȼɬɨɪɨɣ ɩɪɢɦɟɪ ɞɟɦɨɧɫɬɪɢɪɭɟɬ ɨɩɟɪɚɰɢɸ, ɤɨɬɨɪɚɹ ɜɵɩɨɥɧɹɟɬɫɹ ɧɟ ɬɚɤ ɱɚɫɬɨ. ɗɬɨ ɞɟɥɚɟɬɫɹ, ɤɨɝɞɚ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɭɠɟ ɢɦɟɟɬ ɠɭɪɧɚɥɵ ɪɟɬɪɚɧɫɥɹɰɢɣ, ɤɨɬɨɪɵɟ ɜɵ ɯɨɬɢɬɟ
ɩɨ ɤɚɤɢɦ-ɬɨ ɩɪɢɱɢɧɚɦ ɜɵɩɨɥɧɢɬɶ ɫɧɨɜɚ. ɑɬɨɛɵ ɷɬɨ ɫɞɟɥɚɬɶ, ɧɭɠɧɨ ɜɪɟɦɟɧɧɨ ɫɞɟɥɚɬɶ
ɝɥɚɜɧɵɣ ɫɟɪɜɟɪ ɧɟɞɨɫɬɭɩɧɵɦ. ȼɚɦ ɧɭɠɧɨ ɩɪɨɫɬɨ ɩɪɢɦɟɧɢɬɶ CHANGE MASTER TO ɢ ɡɚɩɭɫɬɢɬɶ ɩɨɬɨɤ SQL (START SLAVE SQL_THREAD).
ȼɵ ɞɚɠɟ ɦɨɠɟɬɟ ɢɫɩɨɥɶɡɨɜɚɬɶ ɜɬɨɪɭɸ ɨɩɟɪɚɰɢɸ ɜ ɫɪɟɞɟ ɛɟɡ ɪɟɩɥɢɤɚɰɢɢ ɫ ɜɵɞɟɥɟɧɧɵɦ ɫɟɪɜɟɪɨɦ, ɧɟ ɹɜɥɹɸɳɢɦɫɹ ɩɨɞɱɢɧɟɧɧɵɦ, ɞɥɹ ɜɨɫɫɬɚɧɨɜɥɟɧɢɹ ɩɨɫɥɟ ɚɜɚɪɢɢ. ɉɪɟɞɩɨɥɨɠɢɦ, ɱɬɨ ɜɚɲ ɫɟɪɜɟɪ ɩɨɬɟɪɩɟɥ ɤɪɚɯ, ɢ ɜɵ ɜɨɫɫɬɚɧɚɜɥɢɜɚɟɬ ɪɟɡɟɪɜɧɭɸ ɤɨɩɢɸ. ȼɵ
ɯɨɬɢɬɟ ɩɨɜɬɨɪɢɬɶ ɨɩɟɪɚɬɨɪɵ, ɫɨɞɟɪɠɚɳɢɟɫɹ ɜ ɟɝɨ ɫɨɛɫɬɜɟɧɧɨɦ ɛɢɧɚɪɧɨɦ ɠɭɪɧɚɥɟ (ɧɟ ɜ
ɠɭɪɧɚɥɟ ɪɟɬɪɚɧɫɥɹɰɢɣ, ɚ ɜ ɨɛɵɱɧɨɦ ɛɢɧɚɪɧɨɦ ɠɭɪɧɚɥɟ), ɢɦɟɸɳɢɦ ɢɦɹ, ɞɨɩɭɫɬɢɦ,
myhost-bin.*. ɉɟɪɜɨɟ, ɱɬɨ ɩɨɬɪɟɛɭɟɬɫɹ ɫɞɟɥɚɬɶ, – ɫɨɡɞɚɬɶ ɪɟɡɟɪɜɧɭɸ ɤɨɩɢɸ ɛɢɧɚɪɧɵɯ
ɠɭɪɧɚɥɨɜ ɜ ɤɚɤɨɦ-ɬɨ ɛɟɡɨɩɚɫɧɨɦ ɦɟɫɬɟ ɧɚ ɫɥɭɱɚɣ, ɟɫɥɢ ɜɵ ɧɟɬɨɱɧɨ ɜɵɩɨɥɧɢɬɟ ɫɥɟɞɭɸɳɭɸ ɩɪɨɰɟɞɭɪɭ ɢ ɫɟɪɜɟɪ ɧɟɩɪɟɞɧɚɦɟɪɟɧɧɨ ɩɨɜɪɟɞɢɬ ɢɯ. ȿɫɥɢ ɢɫɩɨɥɶɡɭɟɬɫɹ MySQL 4.1.1
ɢɥɢ ɛɨɥɟɟ ɧɨɜɨɣ ɜɟɪɫɢɢ, ɜɜɟɞɢɬɟ SET GLOBAL relay_log_purge=0 ɞɥɹ ɛɨɥɶɲɟɣ ɛɟɡɨɩɚɫɧɨɫɬɢ. Ɂɚɬɟɦ ɡɚɩɭɫɬɢɬɟ ɫɟɪɜɟɪ ɛɟɡ ɨɩɰɢɢ --log-bin ɫ ɧɨɜɵɦ (ɨɬɥɢɱɧɵɦ ɨɬ ɫɬɚɪɨɝɨ)
ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɦ ɫɟɪɜɟɪɚ (server ID), ɚ ɬɚɤɠɟ ɫ ɨɩɰɢɹɦɢ --relay-log=myhost-bin
(ɱɬɨɛɵ ɩɪɟɞɫɬɚɜɢɬɶ ɫɟɪɜɟɪɭ ɷɬɨɬ ɛɢɧɚɪɧɵɣ ɠɭɪɧɚɥ ɤɚɤ ɠɭɪɧɚɥ ɪɟɬɪɚɧɫɥɹɰɢɣ) ɢ
--skip-slave-start. ɉɨɫɥɟ ɬɨɝɨ, ɤɚɤ ɫɟɪɜɟɪ ɫɬɚɪɬɭɟɬ, ɜɵɩɨɥɧɢɬɟ ɫɥɟɞɭɸɳɢɟ ɨɩɟɪɚɬɨɪɵ:
mysql> CHANGE MASTER TO
-> RELAY_LOG_FILE='myhost-bin.153',
-> RELAY_LOG_POS=410,
-> MASTER_HOST='ɮɢɤɬɢɜɧɚɹ_ɫɬɪɨɤɚ';
mysql> START SLAVE SQL_THREAD;
ɋɟɪɜɟɪ ɩɪɨɱɢɬɚɟɬ ɢ ɜɵɩɨɥɧɢɬ ɫɜɨɣ ɫɨɛɫɬɜɟɧɧɵɣ ɛɢɧɚɪɧɵɣ ɠɭɪɧɚɥ, ɬɚɤɢɦ ɨɛɪɚɡɨɦ
ɪɟɚɥɢɡɨɜɚɜ ɜɨɫɫɬɚɧɨɜɥɟɧɢɟ ɩɨɫɥɟ ɚɜɚɪɢɢ. Ʉɚɤ ɬɨɥɶɤɨ ɜɨɫɫɬɚɧɨɜɥɟɧɢɟ ɡɚɜɟɪɲɢɬɫɹ, ɜɵɩɨɥɧɢɬɟ STOP SLAVE, ɨɫɬɚɧɨɜɢɬɟ ɫɟɪɜɟɪ, ɭɞɚɥɢɬɟ ɮɚɣɥɵ master.info ɢ relay-log.info ɢ
ɩɟɪɟɡɚɩɭɫɬɢɬɟ ɫɟɪɜɟɪ ɫ ɨɛɵɱɧɵɦɢ ɨɩɰɢɹɦɢ.
324
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɇɚ ɞɚɧɧɵɣ ɦɨɦɟɧɬ ɭɤɚɡɚɧɢɟ MASTER_HOST (ɞɚɠɟ ɫ ɮɢɤɬɢɜɧɵɦ ɡɧɚɱɟɧɢɟɦ) ɧɟɨɛɯɨɞɢɦɨ, ɱɬɨɛɵ ɡɚɫɬɚɜɢɬɶ ɫɟɪɜɟɪ ɫɱɢɬɚɬɶ ɫɟɛɹ ɩɨɞɱɢɧɟɧɧɵɦ. ȼɵɞɚɬɶ ɫɟɪɜɟɪɭ ɧɨɜɵɣ, ɨɬɥɢɱɚɸɳɢɣɫɹ ɨɬ ɫɬɚɪɨɝɨ, ɢɞɟɧɬɢɮɢɤɚɬɨɪ ɫɟɪɜɟɪɚ ɬɚɤɠɟ ɧɟɨɛɯɨɞɢɦɨ, ɢɧɚɱɟ ɨɧ ɭɜɢɞɢɬ ɜ
ɠɭɪɧɚɥɟ ɫɨɛɵɬɢɹ, ɩɨɦɟɱɟɧɧɵɟ ɟɝɨ ɫɨɛɫɬɜɟɧɧɵɦ ɢɞɟɧɬɢɮɢɤɚɬɨɪɨɦ, ɢ, ɪɟɲɢɜ, ɱɬɨ ɷɬɨ
ɩɨɩɵɬɤɚ ɰɢɤɥɢɱɟɫɤɨɣ ɪɟɩɥɢɤɚɰɢɢ, ɩɪɨɩɭɫɬɢɬ ɢɯ. ȼ ɛɭɞɭɳɟɦ ɦɵ ɩɥɚɧɢɪɭɟɦ ɞɨɛɚɜɢɬɶ
ɨɩɰɢɢ, ɱɬɨɛɵ ɢɡɛɚɜɢɬɶɫɹ ɨɬ ɷɬɢɯ ɧɟɛɨɥɶɲɢɯ ɧɟɭɞɨɛɫɬɜ.
6.6.2.2. Ñèíòàêñèñ LOAD DATA FROM MASTER
LOAD DATA FROM MASTER
Ȼɟɪɟɬ ɫɧɢɦɨɤ ɞɚɧɧɵɯ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ ɢ ɡɚɝɪɭɠɚɟɬ ɟɝɨ ɧɚ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ. ɉɪɢ ɷɬɨɦ
ɨɛɧɨɜɥɹɸɬɫɹ ɡɧɚɱɟɧɢɹ MASTER_LOG_FILE ɢ MASTER_LOG_POS ɬɚɤɢɦ ɨɛɪɚɡɨɦ, ɱɬɨɛɵ ɪɟɩɥɢɤɚɰɢɹ ɧɚɱɚɥɚɫɶ ɫ ɩɪɚɜɢɥɶɧɨɣ ɩɨɡɢɰɢɢ. ɍɱɢɬɵɜɚɸɬɫɹ ɜɫɟ ɩɪɚɜɢɥɚ, ɢɫɤɥɸɱɚɸɳɢɟ ɛɚɡɵ ɞɚɧɧɵɯ, ɢɥɢ ɬɚɛɥɢɰɵ, ɭɤɚɡɚɧɧɵɟ ɨɩɰɢɹɦɢ --replicate-*-do-* ɢ --replicate-*-ignore-*.
Ɉɩɰɢɹ --replicate-rewrite-db ɧɟ ɩɪɢɧɢɦɚɟɬɫɹ ɜɨ ɜɧɢɦɚɧɢɟ (ɩɨɫɤɨɥɶɤɭ ɤɬɨ-ɧɢɛɭɞɶ
ɦɨɠɟɬ ɭɫɬɚɧɨɜɢɬɶ ɧɟɨɞɧɨɡɧɚɱɧɵɣ ɪɟɠɢɦ ɨɬɨɛɪɚɠɟɧɢɹ, ɬɚɤɨɣ ɤɚɤ --replicate-rewrite-db=
db1->db3 ɢ --replicate-rewrite-db=db2->db3, ɱɬɨ ɡɚɩɭɬɚɟɬ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɩɪɢ
ɡɚɝɪɭɡɤɟ ɬɚɛɥɢɰ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ).
ɉɪɢɦɟɧɟɧɢɟ ɷɬɨɝɨ ɨɩɟɪɚɬɨɪɚ ɩɨɞɱɢɧɹɟɬɫɹ ɫɥɟɞɭɸɳɢɦ ɭɫɥɨɜɢɹɦ:
„Ɉɧ ɪɚɛɨɬɚɟɬ ɬɨɥɶɤɨ ɫ ɬɚɛɥɢɰɚɦɢ MyISAM.
„Ɍɪɟɛɭɟɬ ɝɥɨɛɚɥɶɧɨɣ ɛɥɨɤɢɪɨɜɤɢ ɱɬɟɧɢɹ ɧɚ ɝɥɚɜɧɨɦ ɫɟɪɜɟɪɟ ɜ ɩɪɨɰɟɫɫɟ ɩɨɥɭɱɟɧɢɹ
ɫɧɢɦɤɚ ɞɚɧɧɵɯ, ɱɬɨ ɩɪɟɞɨɬɜɪɚɳɚɟɬ ɨɛɧɨɜɥɟɧɢɹ ɧɚ ɝɥɚɜɧɨɦ ɫɟɪɜɟɪɟ ɜ ɩɪɨɰɟɫɫɟ
ɨɩɟɪɚɰɢɢ ɡɚɝɪɭɡɤɢ.
ȼ ɛɭɞɭɳɟɦ ɦɵ ɩɥɚɧɢɪɭɟɦ ɨɛɟɫɩɟɱɢɬɶ ɪɚɛɨɬɭ ɷɬɨɝɨ ɨɩɟɪɚɬɨɪɚ ɫ ɬɚɛɥɢɰɚɦɢ InnoDB ɢ
ɢɫɤɥɸɱɢɬɶ ɧɟɨɛɯɨɞɢɦɨɫɬɶ ɝɥɨɛɚɥɶɧɨɣ ɛɥɨɤɢɪɨɜɤɢ ɱɬɟɧɢɹ ɡɚ ɫɱɟɬ ɩɪɢɦɟɧɟɧɢɹ ɧɟɛɥɨɤɢɪɭɸɳɟɝɨ ɨɧɥɚɣɧɨɜɨɝɨ ɪɟɡɟɪɜɧɨɝɨ ɤɨɩɢɪɨɜɚɧɢɹ.
ɉɪɢ ɡɚɝɪɭɡɤɟ ɛɨɥɶɲɢɯ ɬɚɛɥɢɰ ɦɨɠɟɬ ɩɨɧɚɞɨɛɢɬɶɫɹ ɭɜɟɥɢɱɢɬɶ ɡɧɚɱɟɧɢɹ
net_read_timeout ɢ net_write_timeout ɤɚɤ ɧɚ ɝɥɚɜɧɨɦ, ɬɚɤ ɢ ɧɚ ɩɨɞɱɢɧɟɧɧɨɦ ɫɟɪɜɟɪɚɯ.
Ɉɬɦɟɬɢɦ, ɱɬɨ LOAD DATA FROM MASTER ɧɟ ɜɵɩɨɥɧɹɟɬ ɤɨɩɢɪɨɜɚɧɢɹ ɬɚɛɥɢɰ ɢɡ ɛɚɡɵ
ɞɚɧɧɵɯ mysql. ɗɬɨ ɩɨɡɜɨɥɹɟɬ ɢɦɟɬɶ ɪɚɡɧɵɯ ɩɨɥɶɡɨɜɚɬɟɥɟɣ ɫ ɪɚɡɥɢɱɧɵɦɢ ɩɪɢɜɢɥɟɝɢɹɦɢ
ɧɚ ɝɥɚɜɧɨɦ ɢ ɩɨɞɱɢɧɟɧɧɨɦ ɫɟɪɜɟɪɚɯ.
Ɉɩɟɪɚɬɨɪ LOAD DATA FROM MASTER ɬɪɟɛɭɟɬ ɧɚɥɢɱɢɹ ɫɩɟɰɢɚɥɶɧɨɝɨ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɨɣ
ɭɱɟɬɧɨɣ ɡɚɩɢɫɢ ɪɟɩɥɢɤɚɰɢɢ, ɤɨɬɨɪɵɣ ɢɫɩɨɥɶɡɭɟɬɫɹ ɞɥɹ ɩɨɞɤɥɸɱɟɧɢɹ ɤ ɝɥɚɜɧɨɦɭ ɢ ɢɦɟɟɬ
ɩɪɢɜɢɥɟɝɢɢ RELOAD ɢ SUPER, ɚ ɬɚɤɠɟ ɩɪɢɜɢɥɟɝɢɸ SELECT ɧɚ ɜɫɟ ɬɚɛɥɢɰɵ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ, ɤɨɬɨɪɵɟ ɧɭɠɧɨ ɡɚɝɪɭɠɚɬɶ. ȼɫɟ ɬɚɛɥɢɰɵ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ, ɤ ɤɨɬɨɪɵɦ ɭ ɩɨɥɶɡɨɜɚɬɟɥɹ
ɧɟɬ ɩɪɚɜ ɞɨɫɬɭɩɚ ɩɨ SELECT, ɢɝɧɨɪɢɪɭɸɬɫɹ ɨɩɟɪɚɬɨɪɨɦ LOAD DATA FROM MASTER. ɗɬɨ ɫɜɹɡɚɧɨ ɫ ɬɟɦ, ɱɬɨ ɝɥɚɜɧɵɣ ɫɟɪɜɟɪ ɫɤɪɵɜɚɟɬ ɢɯ ɨɬ ɩɨɥɶɡɨɜɚɬɟɥɹ: LOAD DATA FROM MASTER
ɜɵɡɵɜɚɟɬ SHOW DATABASES, ɱɬɨɛɵ ɩɨɥɭɱɢɬɶ ɫɩɢɫɨɤ ɛɚɡ ɞɚɧɧɵɯ ɞɥɹ ɡɚɝɪɭɡɤɢ, ɚ SHOW
DATABASES ɜɨɡɜɪɚɳɚɟɬ ɬɨɥɶɤɨ ɬɟ ɛɚɡɵ ɞɚɧɧɵɯ, ɜ ɤɨɬɨɪɵɯ ɭ ɩɨɥɶɡɨɜɚɬɟɥɹ ɟɫɬɶ ɤɚɤɢɟ-ɬɨ
ɩɪɢɜɢɥɟɝɢɢ. ɋɦ. ɪɚɡɞɟɥ 6.5.3.7. ɋɨ ɫɬɨɪɨɧɵ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ ɩɨɥɶɡɨɜɚɬɟɥɶ, ɤɨɬɨɪɵɣ ɜɵɩɨɥɧɹɟɬ LOAD DATA FROM MASTER, ɞɨɥɠɟɧ ɢɦɟɬɶ ɩɪɚɜɚ ɧɚ ɭɞɚɥɟɧɢɟ ɢ ɫɨɡɞɚɧɢɟ ɛɚɡ
ɞɚɧɧɵɯ ɢ ɬɚɛɥɢɰ, ɤɨɬɨɪɵɟ ɩɨɞɥɟɠɚɬ ɤɨɩɢɪɨɜɚɧɢɸ.
6.6.2.3. Ñèíòàêñèñ LOAD TABLE èìÿ_òàáëèöû FROM MASTER
LOAD TABLE ɢɦɹ_ɬɚɛɥɢɰɵ FROM MASTER
6.6. Îïåðàòîðû ðåïëèêàöèè
325
ɉɟɪɟɧɨɫɢɬ ɤɨɩɢɸ ɬɚɛɥɢɰɵ ɫ ɝɥɚɜɧɨɝɨ ɧɚ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ. ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɪɟɚɥɢɡɨɜɚɧ ɝɥɚɜɧɵɦ ɨɛɪɚɡɨɦ ɞɥɹ ɨɬɥɚɞɤɢ LOAD DATA FROM MASTER. Ɉɧ ɬɪɟɛɭɟɬ, ɱɬɨɛɵ ɩɨɥɶɡɨɜɚɬɟɥɶɫɤɚɹ ɭɱɟɬɧɚɹ ɡɚɩɢɫɶ, ɤɨɬɨɪɚɹ ɩɨɞɤɥɸɱɚɟɬɫɹ ɤ ɝɥɚɜɧɨɦɭ ɫɟɪɜɟɪɭ, ɢɦɟɥɚ ɬɚɦ ɩɪɢɜɢɥɟɝɢɢ RELOAD ɢ SUPER, ɚ ɬɚɤɠɟ SELECT ɧɚ ɬɚɛɥɢɰɭ, ɤɨɬɨɪɭɸ ɧɭɠɧɨ ɡɚɝɪɭɠɚɬɶ.
ɋɨ ɫɬɨɪɨɧɵ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ ɩɨɥɶɡɨɜɚɬɟɥɶ, ɡɚɩɭɫɤɚɸɳɢɣ LOAD TABLE...FROM
MASTER ɞɨɥɠɟɧ ɢɦɟɬɶ ɩɪɢɜɢɥɟɝɢɢ ɧɚ ɭɧɢɱɬɨɠɟɧɢɟ ɢ ɫɨɡɞɚɧɢɟ ɬɚɛɥɢɰɵ.
ɍɫɥɨɜɢɹ ɩɪɢɦɟɧɟɧɢɹ LOAD DATA FROM MASTER ɩɪɢɦɟɧɢɦɵ ɢ ɡɞɟɫɶ. ɇɚɩɪɢɦɟɪ, LOAD
TABLE FROM MASTER ɪɚɛɨɬɚɟɬ ɬɨɥɶɤɨ ɫ ɬɚɛɥɢɰɚɦɢ MyISAM. Ɂɚɦɟɱɚɧɢɹ ɩɨ ɭɫɬɚɧɨɜɤɟ ɬɚɣɦɚɭɬɨɜ ɬɚɤɠɟ ɚɤɬɭɚɥɶɧɵ.
6.6.2.4. Ñèíòàêñèñ MASTER_POS_WAIT()
SELECT MASTER_POS_WAIT('ɢɦɹ_ɠɭɪɧɚɥɚ_ɝɥɚɜɧɨɝɨ_ɫɟɪɜɟɪɚ',
ɩɨɡɢɰɢɹ_ɜ_ɠɭɪɧɚɥɟ_ɝɥɚɜɧɨɝɨ_ɫɟɪɜɟɪɚ)
ɗɬɨ ɮɭɧɤɰɢɹ, ɚ ɧɟ ɨɩɟɪɚɬɨɪ. ɉɪɢɦɟɧɹɟɬɫɹ ɞɥɹ ɨɛɟɫɩɟɱɟɧɢɹ ɬɨɝɨ, ɱɬɨɛɵ ɩɨɞɱɢɧɟɧɧɵɣ
ɫɟɪɜɟɪ ɩɪɨɱɢɬɚɥ ɢ ɢɫɩɨɥɧɢɥ ɜɫɟ ɫɨɛɵɬɢɹ ɢɡ ɛɢɧɚɪɧɨɝɨ ɠɭɪɧɚɥɚ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ ɞɨ ɭɤɚɡɚɧɧɨɣ ɩɨɡɢɰɢɢ. ɋɦ. ɩɨɥɧɨɟ ɨɩɢɫɚɧɢɟ ɜ ɪɚɡɞɟɥɟ 5.8.4.
6.6.2.5. Ñèíòàêñèñ RESET SLAVE
RESET SLAVE
Ɂɚɫɬɚɜɥɹɟɬ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɡɚɛɵɬɶ ɩɨɡɢɰɢɸ ɪɟɩɥɢɤɚɰɢɢ ɜ ɛɢɧɚɪɧɨɦ ɠɭɪɧɚɥɟ
ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ. ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɩɪɟɞɧɚɡɧɚɱɟɧ ɞɥɹ “ɱɢɫɬɨɝɨ” ɫɬɚɪɬɚ. Ɉɧ ɭɞɚɥɹɟɬ ɮɚɣɥɵ
master.info ɢ relay-log.info, ɜɫɟ ɫɬɚɪɵɟ ɠɭɪɧɚɥɵ ɪɟɬɪɚɧɫɥɹɰɢɣ ɢ ɫɬɚɪɬɭɟɬ ɧɨɜɵɣ
ɠɭɪɧɚɥ ɪɟɬɪɚɧɫɥɹɰɢɣ.
oË ÑËÕÏÛµÂ!
ȼɫɟ ɠɭɪɧɚɥɵ ɪɟɬɪɚɧɫɥɹɰɢɣ ɭɞɚɥɹɸɬɫɹ, ɞɚɠɟ ɟɫɥɢ ɨɧɢ ɟɳɟ ɧɟ ɩɨɥɧɨɫɬɶɸ ɨɛɪɚɛɨɬɚɧɵ ɩɨɬɨɤɨɦ
SQL ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ. (ɗɬɨ ɜɟɫɶɦɚ ɜɟɪɨɹɬɧɨ, ɨɫɨɛɟɧɧɨ ɟɫɥɢ ɛɵɥɚ ɞɚɧɚ ɤɨɦɚɧɞɚ STOP
SLAVE ɢɥɢ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɫɢɥɶɧɨ ɧɚɝɪɭɠɟɧ.)
ɂɧɮɨɪɦɚɰɢɹ ɨ ɩɨɞɤɥɸɱɟɧɢɢ, ɫɨɯɪɚɧɹɟɦɚɹ ɜ ɮɚɣɥɟ master.info, ɧɟɦɟɞɥɟɧɧɨ ɨɱɢɳɚɟɬɫɹ ɫ ɢɫɩɨɥɶɡɨɜɚɧɢɟɦ ɥɸɛɵɯ ɡɧɚɱɟɧɢɣ, ɭɤɚɡɚɧɧɵɯ ɱɟɪɟɡ ɫɨɨɬɜɟɬɫɬɜɭɸɳɢɟ ɨɩɰɢɢ ɡɚɩɭɫɤɚ. ɗɬɚ ɢɧɮɨɪɦɚɰɢɹ ɜɤɥɸɱɚɟɬ ɜ ɫɟɛɹ: ɢɦɹ ɯɨɫɬɚ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ, ɧɨɦɟɪ ɩɨɪɬɚ, ɢɦɹ
ɩɨɥɶɡɨɜɚɬɟɥɹ ɢ ɩɚɪɨɥɶ. ȿɫɥɢ ɩɨɬɨɤ SQL ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ ɧɚɯɨɞɢɥɫɹ ɜ ɩɪɨɰɟɫɫɟ
ɪɟɩɥɢɤɚɰɢɢ ɜɪɟɦɟɧɧɵɯ ɬɚɛɥɢɰ, ɤɨɝɞɚ ɨɧ ɛɵɥ ɨɫɬɚɧɨɜɥɟɧ ɢ ɜɵɩɨɥɧɟɧ ɨɩɟɪɚɬɨɪ RESET
SLAVE, ɪɟɩɥɢɰɢɪɨɜɚɧɧɵɟ ɜɪɟɦɟɧɧɵɟ ɬɚɛɥɢɰɵ ɭɞɚɥɹɸɬɫɹ ɫ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ.
Ⱦɨ ɜɟɪɫɢɢ MySQL 3.23.26 ɷɬɨɬ ɨɩɟɪɚɬɨɪ ɧɚɡɵɜɚɥɫɹ FLUSH SLAVE.
6.6.2.6. Ñèíòàêñèñ SET GLOBAL SQL_SLAVE_SKIP_COUNTER
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = n
ɉɪɨɩɭɫɤɚɟɬ ɫɥɟɞɭɸɳɢɟ n ɫɨɛɵɬɢɣ ɫ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ. ɗɬɨ ɩɪɢɦɟɧɢɦɨ ɞɥɹ ɜɨɫɫɬɚɧɨɜɥɟɧɢɹ ɩɨɫɥɟ ɨɫɬɚɧɨɜɚ ɪɟɩɥɢɤɚɰɢɢ, ɜɵɡɜɚɧɧɨɣ ɧɟɤɨɬɨɪɵɦ ɨɩɟɪɚɬɨɪɨɦ.
ɗɬɨɬ ɨɩɟɪɚɬɨɪ ɞɨɩɭɫɬɢɦ, ɬɨɥɶɤɨ ɤɨɝɞɚ ɩɨɬɨɤ ɩɨɞɱɢɧɟɧɧɨɝɨ ɧɟ ɪɚɛɨɬɚɟɬ. ȼ ɩɪɨɬɢɜɧɨɦ
ɫɥɭɱɚɟ ɝɟɧɟɪɢɪɭɟɬɫɹ ɨɲɢɛɤɚ.
Ⱦɨ MySQL 4.0 ɜ ɷɬɨɦ ɨɩɟɪɚɬɨɪɟ ɩɪɨɩɭɫɤɚɣɬɟ ɫɥɨɜɨ GLOBAL.
6.6.2.7. Ñèíòàêñèñ SHOW SLAVE STATUS
SHOW SLAVE STATUS
326
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
ɉɪɟɞɫɬɚɜɥɹɟɬ ɢɧɮɨɪɦɚɰɢɸ ɨ ɫɨɫɬɨɹɧɢɢ ɫɭɳɟɫɬɜɟɧɧɵɯ ɩɚɪɚɦɟɬɪɨɜ ɩɨɬɨɤɨɜ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ. ȿɫɥɢ ɜɵɩɨɥɧɢɬɶ ɷɬɨɬ ɨɩɟɪɚɬɨɪ ɢɡ ɫɪɟɞɵ ɤɥɢɟɧɬɫɤɨɣ ɩɪɨɝɪɚɦɦɵ mysql,
ɦɨɠɧɨ ɭɤɚɡɚɬɶ ɨɝɪɚɧɢɱɢɬɟɥɶ \G ɜɦɟɫɬɨ ɬɨɱɤɢ ɫ ɡɚɩɹɬɨɣ ɢ ɩɨɥɭɱɢɬɶ ɛɨɥɟɟ ɱɢɬɚɛɟɥɶɧɵɣ
ɜɟɪɬɢɤɚɥɶɧɵɣ ɜɵɜɨɞ:
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: localhost
Master_User: root
Master_Port: 3306
Connect_Retry: 3
Master_Log_File: gbichot-bin.005
Read_Master_Log_Pos: 79
Relay_Log_File: gbichot-relay-bin.005
Relay_Log_Pos: 548
Relay_Master_Log_File: gbichot-bin.005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 79
Relay_Log_Space: 552
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 8
ȼ ɡɚɜɢɫɢɦɨɫɬɢ ɨɬ ɭɫɬɚɧɨɜɥɟɧɧɨɣ ɜɟɪɫɢɢ MySQL ɜɵ ɦɨɠɟɬɟ ɧɟ ɭɜɢɞɟɬɶ ɧɟɤɨɬɨɪɵɯ
ɩɨɥɟɣ ɢɡ ɱɢɫɥɚ ɩɪɢɜɟɞɟɧɧɵɯ ɜɵɲɟ. ȼ ɱɚɫɬɧɨɫɬɢ, ɧɟɤɨɬɨɪɵɟ ɩɨɥɹ ɩɪɢɫɭɬɫɬɜɭɸɬ ɬɨɥɶɤɨ ɜ
MySQL 4.1.1.
SHOW SLAVE STATUS ɜɨɡɜɪɚɳɚɟɬ ɫɥɟɞɭɸɳɢɟ ɩɨɥɹ:
„Slave_IO_State.
Ʉɨɩɢɹ ɩɨɥɹ State ɢɡ ɜɵɜɨɞɚ SHOW PROCESSLIST ɞɥɹ ɩɨɬɨɤɚ ɜɜɨɞɚɜɵɜɨɞɚ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ. ɂɧɮɨɪɦɢɪɭɟɬ ɨ ɬɨɦ, ɱɬɨ ɩɨɬɨɤ ɩɵɬɚɟɬɫɹ ɩɨɞɤɥɸɱɢɬɶɫɹ ɤ ɝɥɚɜɧɨɦɭ ɫɟɪɜɟɪɭ, ɨɠɢɞɚɟɬ ɫɨɛɵɬɢɣ ɫ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ, ɩɨɜɬɨɪɧɨ ɩɨɞɤɥɸɱɚɟɬɫɹ ɢ ɬɚɤ ɞɚɥɟɟ. Ɉɛɪɚɳɚɬɶ ɜɧɢɦɚɧɢɟ ɧɚ ɷɬɨ ɩɨɥɟ ɧɟɨɛɯɨɞɢɦɨ, ɩɨɬɨɦɭ ɱɬɨ,
ɧɚɩɪɢɦɟɪ, ɩɨɬɨɤ ɦɨɠɟɬ ɪɚɛɨɬɚɬɶ, ɧɨ ɟɦɭ ɧɟ ɭɞɚɟɬɫɹ ɩɨɞɤɥɸɱɢɬɶɫɹ ɤ ɝɥɚɜɧɨɦɭ
ɫɟɪɜɟɪɭ. Ɍɨɥɶɤɨ ɡɞɟɫɶ ɜɵ ɦɨɠɟɬɟ ɨɛɧɚɪɭɠɢɬɶ ɩɪɨɛɥɟɦɭ ɩɨɞɤɥɸɱɟɧɢɹ. ɋɨɫɬɨɹɧɢɟ
ɩɨɬɨɤɚ SQL ɧɟ ɤɨɩɢɪɭɟɬɫɹ, ɩɨɬɨɦɭ ɱɬɨ ɨɧɨ ɩɪɨɳɟ. ȿɫɥɢ ɨɧ ɪɚɛɨɬɚɟɬ, ɬɨ ɩɪɨɛɥɟɦ
ɧɟɬ, ɚ ɟɫɥɢ ɧɟɬ, ɬɨ ɜɵ ɧɚɣɞɟɬɟ ɨɲɢɛɤɭ ɜ ɩɨɥɟ Last_Error, ɤɨɬɨɪɨɟ ɨɩɢɫɚɧɨ ɧɢɠɟ.
ɗɬɨ ɩɨɥɟ ɩɨɹɜɢɥɨɫɶ ɜ ɜɟɪɫɢɢ MySQL 4.1.1.
6.6. Îïåðàòîðû ðåïëèêàöèè
„Master_Host.
ɏɨɫɬ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ.
„Master_User.
ɂɦɹ ɩɨɥɶɡɨɜɚɬɟɥɹ, ɩɨɞɤɥɸɱɟɧɧɨɝɨ ɤ ɝɥɚɜɧɨɦɭ ɫɟɪɜɟɪɭ.
„Master_Port.
ɇɨɦɟɪ ɩɨɪɬɚ ɧɚ ɝɥɚɜɧɨɦ ɫɟɪɜɟɪɟ.
„Connect_Retry.
327
Ɍɟɤɭɳɟɟ ɡɧɚɱɟɧɢɟ ɨɩɰɢɢ --master-connect-retry.
„Master_Log_File.
ɂɦɹ ɮɚɣɥɚ ɛɢɧɚɪɧɨɝɨ ɠɭɪɧɚɥɚ ɧɚ ɝɥɚɜɧɨɦ ɫɟɪɜɟɪɟ, ɢɡ ɤɨɬɨɪɨɝɨ
ɩɨɬɨɤ ɜɜɨɞɚ-ɜɵɜɨɞɚ ɨɫɭɳɟɫɬɜɥɹɟɬ ɱɬɟɧɢɟ.
„Read_Master_Log_Pos. ɉɨɡɢɰɢɹ ɜ ɛɢɧɚɪɧɨɦ ɠɭɪɧɚɥɟ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ, ɞɨ ɤɨɬɨɪɨɣ
ɞɨɱɢɬɚɥ ɩɨɬɨɤ ɜɜɨɞɚ-ɜɵɜɨɞɚ.
„Relay_Log_File. ɂɦɹ ɮɚɣɥɚ ɠɭɪɧɚɥɚ ɪɟɬɪɚɧɫɥɹɰɢɣ, ɢɡ ɤɨɬɨɪɨɝɨ ɩɨɬɨɤ SQL ɨɫɭɳɟɫɬɜɥɹɟɬ ɱɬɟɧɢɟ ɢ ɜɵɩɨɥɧɟɧɢɟ ɨɩɟɪɚɬɨɪɨɜ.
„Relay_Log_Pos. ɉɨɡɢɰɢɹ ɜ ɠɭɪɧɚɥɟ ɪɟɬɪɚɧɫɥɹɰɢɣ, ɞɨ ɤɨɬɨɪɨɣ ɩɨɬɨɤ SQL ɩɪɨɱɢɬɚɥ ɢ ɜɵɩɨɥɧɢɥ ɨɩɟɪɚɬɨɪɵ.
„Relay_Master_Log_File. ɂɦɹ ɮɚɣɥɚ ɛɢɧɚɪɧɨɝɨ ɠɭɪɧɚɥɚ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ, ɤɨɬɨɪɵɣ ɫɨɞɟɪɠɢɬ ɩɨɫɥɟɞɧɢɣ ɨɩɟɪɚɬɨɪ, ɜɵɩɨɥɧɟɧɧɵɣ ɩɨɬɨɤɨɦ SQL.
„Slave_IO_Running. Ɂɚɩɭɳɟɧ ɥɢ ɩɨɬɨɤ ɜɜɨɞɚ-ɜɵɜɨɞɚ.
„Slave_SQL_Running.
Ɂɚɩɭɳɟɧ ɥɢ ɩɨɬɨɤ SQL.
„Replicate_Do_DB,
Replicate_Ignore_DB
ɋɩɢɫɨɤ ɛɚɡ ɞɚɧɧɵɯ, ɤɨɬɨɪɵɟ ɭɤɚɡɚɧɵ ɨɩɰɢɹɦɢ --replicate-do-db ɢ
--replicateignore-db, ɟɫɥɢ ɬɚɤɨɜɵɟ ɛɵɥɢ.
„Replicate_Do_Table,
Replicate_Ignore_Table,
Replicate_Wild_Do_Table,
Replicate_Wild_Ignore_Table
ɋɩɢɫɨɤ ɬɚɛɥɢɰ, ɤɨɬɨɪɵɟ ɭɤɚɡɚɧɵ ɨɩɰɢɹɦɢ --replicate-do-table ,
--replicate-ignore-table, --replicate-wild-do-table ɢ --replicate-wildignore_table, ɟɫɥɢ ɬɚɤɨɜɵɟ ɛɵɥɢ.
ɗɬɢ ɩɨɥɹ ɩɪɢɫɭɬɫɬɜɭɸɬ, ɧɚɱɢɧɚɹ ɫ MySQL 4.1.1.
„Last_Errno, Last_Error. ɇɨɦɟɪ ɨɲɢɛɤɢ ɢ ɫɨɨɛɳɟɧɢɟ ɨɛ ɨɲɢɛɤɟ, ɜɨɡɜɪɚɳɟɧɧɨɟ
ɩɨɫɥɟɞɧɢɦ ɡɚɩɪɨɫɨɦ. ɇɨɦɟɪ ɨɲɢɛɤɢ 0 ɢ ɫɨɨɛɳɟɧɢɟ ɜ ɜɢɞɟ ɩɭɫɬɨɣ ɫɬɪɨɤɢ ɨɡɧɚɱɚɸɬ, ɱɬɨ ɨɲɢɛɨɤ ɧɟɬ. ȿɫɥɢ Last_Error ɧɟ ɩɭɫɬɨɟ, ɨɧɨ ɬɚɤɠɟ ɩɨɹɜɥɹɟɬɫɹ ɜ ɠɭɪɧɚɥɟ
ɨɲɢɛɨɤ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ.
ɇɚɩɪɢɦɟɪ:
Last_Errno: 1051
Last_Error: error 'Unknown table 'z'' on query 'drop table z'
ɗɬɨ ɨɡɧɚɱɚɟɬ, ɱɬɨ ɬɚɛɥɢɰɚ z ɧɚɯɨɞɢɥɚɫɶ ɧɚ ɝɥɚɜɧɨɦ ɫɟɪɜɟɪɟ ɢ ɛɵɥɚ ɬɚɦ ɭɞɚɥɟɧɚ,
ɧɨ ɨɧɚ ɧɟ ɫɭɳɟɫɬɜɭɟɬ ɧɚ ɩɨɞɱɢɧɟɧɧɨɦ ɫɟɪɜɟɪɟ, ɩɨɷɬɨɦɭ ɨɩɟɪɚɬɨɪ DROP TABLE ɧɟ
ɩɪɨɲɟɥ. (ɗɬɨ ɦɨɠɟɬ ɫɥɭɱɢɬɶɫɹ, ɧɚɩɪɢɦɟɪ, ɟɫɥɢ ɩɪɢ ɧɚɫɬɪɨɣɤɟ ɪɟɩɥɢɤɚɰɢɢ ɜɵ ɡɚɛɵɥɢ ɫɤɨɩɢɪɨɜɚɬɶ ɬɚɛɥɢɰɭ ɧɚ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ.)
„Skip_Counter. ɉɨɫɥɟɞɧɟɟ ɢɫɩɨɥɶɡɨɜɚɧɧɨɟ ɡɧɚɱɟɧɢɟ SQL_SLAVE_SKIP_COUNTER.
328
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
„Exec_Master_Log_Pos.
ɉɨɡɢɰɢɹ ɩɨɫɥɟɞɧɟɝɨ ɢɫɩɨɥɧɟɧɧɨɝɨ ɩɨɬɨɤɨɦ SQL ɨɩɟɪɚɬɨɪɚ ɜ ɛɢɧɚɪɧɨɦ ɠɭɪɧɚɥɟ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ (Relay_Master_Log_File).
(Relay_Master_Log_File, Exec_Master_Log_Pos) ɜ ɛɢɧɚɪɧɨɦ ɠɭɪɧɚɥɟ ɝɥɚɜɧɨɝɨ
ɫɟɪɜɟɪɚ ɫɨɨɬɜɟɬɫɬɜɭɸɬ (Relay_Log_File, Relay_Log_Pos) ɜ ɠɭɪɧɚɥɟ ɪɟɬɪɚɧɫɥɹɰɢɣ.
„Relay_Log_Space. ɋɭɦɦɚɪɧɵɣ ɪɚɡɦɟɪ ɜɫɟɯ ɫɭɳɟɫɬɜɭɸɳɢɯ ɠɭɪɧɚɥɨɜ ɪɟɬɪɚɧɫɥɹɰɢɣ.
„Until_Condition,
Until_Log_File,
Until_Log_Pos
Ɂɧɚɱɟɧɢɹ, ɭɤɚɡɚɧɧɵɟ ɜ ɤɨɧɫɬɪɭɤɰɢɢ UNTIL ɨɩɟɪɚɬɨɪɚ START SLAVE.
Until_Condition ɜɤɥɸɱɚɟɬ ɫɥɟɞɭɸɳɢɟ ɡɧɚɱɟɧɢɹ:
x None, ɟɫɥɢ ɤɨɧɫɬɪɭɤɰɢɹ UNTIL ɧɟ ɭɤɚɡɵɜɚɥɚɫɶ.
x Master, ɟɫɥɢ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɱɢɬɚɟɬ ɞɨ ɡɚɞɚɧɧɨɣ ɩɨɡɢɰɢɢ ɜ ɛɢɧɚɪɧɵɯ
ɠɭɪɧɚɥɚɯ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ.
x Relay, ɟɫɥɢ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɱɢɬɚɟɬ ɞɨ ɡɚɞɚɧɧɨɣ ɩɨɡɢɰɢɢ ɜ ɫɜɨɢɯ ɠɭɪɧɚɥɚɯ ɪɟɬɪɚɧɫɥɹɰɢɣ.
Until_Log_File ɢ Until_Log_Pos ɨɡɧɚɱɚɸɬ ɢɦɹ ɮɚɣɥɚ ɠɭɪɧɚɥɚ ɢ ɩɨɡɢɰɢɸ ɜ ɧɟɦ,
ɨɩɪɟɞɟɥɹɸɳɢɟ ɬɨɱɤɭ, ɜ ɤɨɬɨɪɨɣ ɩɨɬɨɤ SQL ɩɪɟɤɪɚɬɢɬ ɜɵɩɨɥɧɟɧɢɟ.
ɗɬɢ ɩɨɥɹ ɩɪɢɫɭɬɫɬɜɭɸɬ, ɧɚɱɢɧɚɹ ɫ MySQL 4.1.1.
„Master_SSL_Allowed,
Master_SSL_CA_File,
Master_SSL_CA_Path,
Master_SSL_Cert,
Master_SSL_Cipher,
Master_SSL_Key
ɗɬɢ ɩɨɥɹ ɩɨɤɚɡɵɜɚɸɬ ɩɚɪɚɦɟɬɪɵ SSL, ɢɫɩɨɥɶɡɭɟɦɵɟ ɩɨɞɱɢɧɟɧɧɵɦ ɫɟɪɜɟɪɨɦ ɩɪɢ
ɩɨɞɤɥɸɱɟɧɢɢ ɤ ɝɥɚɜɧɨɦɭ ɫɟɪɜɟɪɭ, ɟɫɥɢ ɨɧɢ ɟɫɬɶ.
Master_SSL_Allowed ɩɪɢɧɢɦɚɟɬ ɫɥɟɞɭɸɳɢɟ ɡɧɚɱɟɧɢɹ:
x Yes, ɟɫɥɢ SSL-ɩɨɞɤɥɸɱɟɧɢɟ ɤ ɝɥɚɜɧɨɦɭ ɫɟɪɜɟɪɭ ɪɚɡɪɟɲɟɧɨ.
x No, ɟɫɥɢ SSL-ɩɨɞɤɥɸɱɟɧɢɟ ɤ ɝɥɚɜɧɨɦɭ ɫɟɪɜɟɪɭ ɧɟ ɪɚɡɪɟɲɟɧɨ.
x Ignored, SSL-ɩɨɞɤɥɸɱɟɧɢɟ ɤ ɝɥɚɜɧɨɦɭ ɫɟɪɜɟɪɭ ɪɚɡɪɟɲɟɧɨ, ɧɨ ɩɨɞɱɢɧɟɧɧɵɣ
ɫɟɪɜɟɪ ɧɟ ɩɨɞɞɟɪɠɢɜɚɟɬ SSL.
Ɂɧɚɱɟɧɢɹ ɨɫɬɚɥɶɧɵɯ ɩɨɥɟɣ, ɢɦɟɸɳɢɯ ɨɬɧɨɲɟɧɢɟ ɤ SSL, ɫɨɨɬɜɟɬɫɬɜɭɸɬ ɡɧɚɱɟɧɢɹɦ ɨɩɰɢɣ --master-ca, --master-capath, --master-cert, --master-cipher ɢ
--master-key.
ɗɬɢ ɩɨɥɹ ɩɪɟɞɫɬɚɜɥɟɧɵ, ɧɚɱɢɧɚɹ ɫ MySQL 4.1.1.
„Seconds_Behind_Master. Ʉɨɥɢɱɟɫɬɜɨ ɫɟɤɭɧɞ, ɩɪɨɲɟɞɲɢɯ ɨɬ ɜɪɟɦɟɧɧɨɣ ɦɟɬɤɢ ɩɨɫɥɟɞɧɟɝɨ ɫɨɛɵɬɢɹ ɫ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ, ɜɵɩɨɥɧɟɧɧɨɝɨ ɩɨɬɨɤɨɦ SQL. Ɉɧɨ ɛɭɞɟɬ
ɪɚɜɧɨ NULL, ɟɫɥɢ ɟɳɟ ɧɟ ɜɵɩɨɥɧɹɥɨɫɶ ɧɢ ɨɞɧɨ ɢɡ ɧɢɯ, ɥɢɛɨ ɫɪɚɡɭ ɩɨɫɥɟ ɜɵɞɚɱɢ
CHANGE MASTER ɢɥɢ RESET SLAVE. ɗɬɨ ɩɨɥɟ ɦɨɠɟɬ ɛɵɬɶ ɢɫɩɨɥɶɡɨɜɚɧɨ ɞɥɹ ɨɩɪɟɞɟɥɟɧɢɹ ɬɨɝɨ, ɧɚɫɤɨɥɶɤɨ “ɡɚɩɚɡɞɵɜɚɟɬ” ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ. ɗɬɨ ɪɚɛɨɬɚɟɬ, ɞɚɠɟ ɟɫɥɢ ɱɚɫɵ ɧɚ ɝɥɚɜɧɨɦ ɢ ɩɨɞɱɢɧɟɧɧɨɦ ɫɟɪɜɟɪɟ ɧɟ ɫɢɧɯɪɨɧɢɡɢɪɨɜɚɧɵ.
ɗɬɢ ɩɨɥɹ ɩɪɟɞɫɬɚɜɥɟɧɵ, ɧɚɱɢɧɚɹ ɫ MySQL 4.1.1.
6.6. Îïåðàòîðû ðåïëèêàöèè
329
6.6.2.8. Ñèíòàêñèñ START SLAVE
START SLAVE [ɬɢɩ_ɩɨɬɨɤɚ [, ɬɢɩ_ɩɨɬɨɤɚ] ... ]
START SLAVE [SQL_THREAD] UNTIL
MASTER_LOG_FILE = 'ɢɦɹ_ɠɭɪɧɚɥɚ', MASTER_LOG_POS = ɩɨɡɢɰɢɹ_ɜ_ɠɭɪɧɚɥɟ
START SLAVE [SQL_THREAD] UNTIL
RELAY_LOG_FILE = 'ɢɦɹ_ɠɭɪɧɚɥɚ', RELAY_LOG_POS = ɩɨɡɢɰɢɹ_ɜ_ɠɭɪɧɚɥɟ
ɬɢɩ_ɩɨɬɨɤɚ: IO_THREAD | SQL_THREAD
START SLAVE ɛɟɡ ɨɩɰɢɣ ɡɚɩɭɫɤɚɟɬ ɨɛɚ ɩɨɬɨɤɚ ɪɟɩɥɢɤɚɰɢɢ ɧɚ ɩɨɞɱɢɧɟɧɧɨɦ ɫɟɪɜɟɪɟ.
ɉɨɬɨɤ ɜɜɨɞɚ-ɜɵɜɨɞɚ ɱɢɬɚɟɬ ɡɚɩɪɨɫɵ ɫ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ ɢ ɩɨɦɟɳɚɟɬ ɢɯ ɜ ɠɭɪɧɚɥ
ɪɟɬɪɚɧɫɥɹɰɢɣ. ɉɨɬɨɤ SQL ɱɢɬɚɟɬ ɠɭɪɧɚɥ ɪɟɬɪɚɧɫɥɹɰɢɣ ɢ ɜɵɩɨɥɧɹɟɬ ɡɚɩɪɨɫɵ. START
SLAVE ɬɪɟɛɭɟɬ ɧɚɥɢɱɢɹ ɩɪɢɜɢɥɟɝɢɢ SUPER.
ȿɫɥɢ START SLAVE ɭɞɚɥɨɫɶ ɡɚɩɭɫɬɢɬɶ ɩɨɬɨɤɢ ɪɟɩɥɢɤɚɰɢɢ ɧɚ ɩɨɞɱɢɧɟɧɧɨɦ ɫɟɪɜɟɪɟ, ɨɧ
ɜɨɡɜɪɚɳɚɟɬ ɭɩɪɚɜɥɟɧɢɟ ɛɟɡ ɨɲɢɛɨɤ. Ɉɞɧɚɤɨ ɞɚɠɟ ɜ ɷɬɨɦ ɫɥɭɱɚɟ ɦɨɠɟɬ ɫɥɭɱɢɬɶɫɹ, ɱɬɨ
ɩɨɬɨɤɢ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ ɫɬɚɪɬɭɸɬ, ɚ ɩɨɡɠɟ ɨɫɬɚɧɨɜɹɬɫɹ (ɧɚɩɪɢɦɟɪ, ɩɨɬɨɦɭ, ɱɬɨ ɨɧ
ɧɟ ɜ ɫɨɫɬɨɹɧɢɢ ɩɨɞɤɥɸɱɢɬɶɫɹ ɤ ɝɥɚɜɧɨɦɭ ɫɟɪɜɟɪɭ ɢɥɢ ɩɪɨɱɟɫɬɶ ɟɝɨ ɛɢɧɚɪɧɵɣ ɠɭɪɧɚɥ,
ɥɢɛɨ ɢɡ-ɡɚ ɤɚɤɢɯ-ɬɨ ɞɪɭɝɢɯ ɩɪɨɛɥɟɦ). START SLAVE ɧɟ ɩɪɟɞɭɩɪɟɠɞɚɟɬ ɨɛ ɷɬɨɦ. ȼɵ ɞɨɥɠɧɵ ɩɪɨɜɟɪɹɬɶ ɠɭɪɧɚɥ ɨɲɢɛɨɤ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ ɧɚ ɩɪɟɞɦɟɬ ɧɚɥɢɱɢɹ ɫɨɨɛɳɟɧɢɣ ɨɛ
ɨɲɢɛɤɚɯ, ɫɝɟɧɟɪɢɪɨɜɚɧɧɵɯ ɩɨɬɨɤɚɦɢ ɪɟɩɥɢɤɚɰɢɢ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ, ɥɢɛɨ ɩɪɨɜɟɪɹɬɶ, ɤɚɤ ɨɧ ɪɚɛɨɬɚɟɬ, ɫ ɩɨɦɨɳɶɸ SHOW SLAVE STATUS.
ɇɚɱɢɧɚɹ ɫ MySQL 4.0.2, ɦɨɠɧɨ ɞɨɛɚɜɥɹɬɶ ɨɩɰɢɢ IO_THREAD ɢ SQL_THREAD ɤ ɨɩɟɪɚɬɨɪɭ, ɱɬɨɛɵ ɭɤɚɡɚɬɶ, ɤɚɤɨɣ ɢɡ ɩɨɬɨɤɨɜ ɧɚɞɨ ɡɚɩɭɫɬɢɬɶ.
ɇɚɱɢɧɚɹ ɫ MySQL 4.1.1, ɦɨɠɧɨ ɭɤɚɡɵɜɚɬɶ ɤɨɧɫɬɪɭɤɰɢɸ UNTIL ɞɥɹ ɭɤɚɡɚɧɢɹ ɬɨɝɨ, ɱɬɨ
ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɞɨɥɠɟɧ ɡɚɩɭɫɬɢɬɶɫɹ ɢ ɪɚɛɨɬɚɬɶ ɞɨ ɬɟɯ ɩɨɪ, ɩɨɤɚ SQL ɧɟ ɞɨɫɬɢɝɧɟɬ
ɡɚɞɚɧɧɨɣ ɬɨɱɤɢ ɜ ɛɢɧɚɪɧɨɦ ɠɭɪɧɚɥɟ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ ɢɥɢ ɜ ɠɭɪɧɚɥɟ ɪɟɬɪɚɧɫɥɹɰɢɣ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ. Ʉɨɝɞɚ ɩɨɬɨɤ SQL ɞɨɫɬɢɝɚɟɬ ɷɬɨɣ ɬɨɱɤɢ, ɨɧ ɨɫɬɚɧɚɜɥɢɜɚɟɬɫɹ. ȿɫɥɢ ɜ
ɨɩɟɪɚɬɨɪɟ ɭɤɚɡɚɧɚ ɨɩɰɢɹ SQL_THREAD, ɨɧ ɡɚɩɭɫɤɚɟɬ ɬɨɥɶɤɨ ɩɨɬɨɤ SQL. ȼ ɩɪɨɬɢɜɧɨɦ ɫɥɭɱɚɟ ɡɚɩɭɫɤɚɸɬɫɹ ɨɛɚ ɩɨɬɨɤɚ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ. ȿɫɥɢ ɩɨɬɨɤ SQL ɭɠɟ ɪɚɛɨɬɚɟɬ, ɤɨɧɫɬɪɭɤɰɢɹ UNTIL ɢɝɧɨɪɢɪɭɟɬɫɹ ɢ ɜɵɞɚɟɬɫɹ ɩɪɟɞɭɩɪɟɠɞɟɧɢɟ.
ɋ ɤɨɧɫɬɪɭɤɰɢɟɣ UNTIL ɨɛɹɡɚɬɟɥɶɧɨ ɭɤɚɡɵɜɚɬɶ ɢ ɢɦɹ ɮɚɣɥɚ ɠɭɪɧɚɥɚ, ɢ ɩɨɡɢɰɢɸ ɜ ɧɟɦ.
ɇɟ ɫɦɟɲɢɜɚɣɬɟ ɨɩɰɢɢ ɠɭɪɧɚɥɚ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ ɢ ɠɭɪɧɚɥɚ ɪɟɬɪɚɧɫɥɹɰɢɣ ɩɨɞɱɢɧɟɧɧɨɝɨ
ɫɟɪɜɟɪɚ.
Ʉɨɧɫɬɪɭɤɰɢɹ UNTIL ɫɛɪɚɫɵɜɚɟɬɫɹ ɩɨɫɥɟɞɭɸɳɢɦ ɨɩɟɪɚɬɨɪɨɦ STOP SLAVE, ɥɢɛɨ START
SLAVE ɛɟɡ ɤɨɧɫɬɪɭɤɰɢɢ UNTIL, ɥɢɛɨ ɩɟɪɟɡɚɩɭɫɤɨɦ ɫɟɪɜɟɪɚ.
Ʉɨɧɫɬɪɭɤɰɢɹ UNTIL ɦɨɠɟɬ ɨɤɚɡɚɬɶɫɹ ɭɞɨɛɧɨɣ ɞɥɹ ɨɬɥɚɞɤɢ ɪɟɩɥɢɤɚɰɢɢ ɥɢɛɨ ɞɥɹ ɜɵɩɨɥɧɟɧɢɹ ɪɟɩɥɢɤɚɰɢɢ ɬɨɥɶɤɨ ɞɨ ɨɩɪɟɞɟɥɟɧɧɨɣ ɬɨɱɤɢ, ɜ ɤɨɬɨɪɨɣ ɧɭɠɧɨ ɢɡɛɟɠɚɬɶ ɪɟɩɥɢɤɚɰɢɢ ɨɬɞɟɥɶɧɨɝɨ ɨɩɟɪɚɬɨɪɚ. ɇɚɩɪɢɦɟɪ, ɟɫɥɢ ɧɟɠɟɥɚɬɟɥɶɧɵɣ ɨɩɟɪɚɬɨɪ DROP TABLE ɛɵɥ
ɜɵɩɨɥɧɟɧ ɧɚ ɝɥɚɜɧɨɦ ɫɟɪɜɟɪɟ, ɜɵ ɦɨɠɟɬɟ ɩɪɢɦɟɧɢɬɶ UNTIL, ɱɬɨɛɵ ɫɨɨɛɳɢɬɶ ɩɨɞɱɢɧɟɧɧɨɦɭ ɫɟɪɜɟɪɭ, ɱɬɨ ɪɟɩɥɢɤɚɰɢɸ ɧɭɠɧɨ ɜɵɩɨɥɧɹɬɶ ɞɨ ɷɬɨɝɨ ɨɩɟɪɚɬɨɪɚ ɢɫɤɥɸɱɢɬɟɥɶɧɨ, ɧɨ
ɧɟ ɞɚɥɟɟ. Ⱦɥɹ ɩɨɢɫɤɚ ɧɭɠɧɨɝɨ ɫɨɛɵɬɢɹ ɜ ɠɭɪɧɚɥɟ ɩɨɥɶɡɭɣɬɟɫɶ ɤɨɦɚɧɞɨɣ mysqlbinlog ɫ
ɛɢɧɚɪɧɵɦ ɠɭɪɧɚɥɨɦ ɝɥɚɜɧɨɝɨ ɫɟɪɜɟɪɚ ɢɥɢ ɠɭɪɧɚɥɨɦ ɪɟɬɪɚɧɫɥɹɰɢɣ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ ɥɢɛɨ ɨɩɟɪɚɬɨɪɨɦ SHOW BINLOG EVENTS.
ȿɫɥɢ ɜɵ ɢɫɩɨɥɶɡɭɟɬɟ UNTIL ɞɥɹ ɬɨɝɨ, ɱɬɨɛɵ ɨɪɝɚɧɢɡɨɜɚɬɶ ɪɟɩɥɢɤɚɰɢɸ ɩɨ ɪɚɡɞɟɥɚɦ,
ɦɵ ɪɟɤɨɦɟɧɞɭɟɦ ɡɚɩɭɫɤɚɬɶ ɩɨɞɱɢɧɟɧɧɵɣ ɫɟɪɜɟɪ ɫ ɨɩɰɢɟɣ --skip-slave-start, ɱɬɨɛɵ
ɩɪɟɞɨɬɜɪɚɬɢɬɶ ɚɜɬɨɦɚɬɢɱɟɫɤɢɣ ɡɚɩɭɫɤ ɩɨɬɨɤɚ SQL ɩɪɢ ɫɬɚɪɬɟ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ.
ȼɨɡɦɨɠɧɨ, ɥɭɱɲɟ ɭɤɚɡɵɜɚɬɶ ɷɬɭ ɨɩɰɢɸ ɜ ɮɚɣɥɟ ɨɩɰɢɣ, ɱɟɦ ɜ ɤɨɦɚɧɞɧɨɣ ɫɬɪɨɤɟ, ɱɬɨɛɵ
ɧɟɨɠɢɞɚɧɧɵɣ ɩɟɪɟɡɚɩɭɫɤ ɫɟɪɜɟɪɚ ɧɟ ɩɪɢɜɟɥ ɤ ɬɨɦɭ, ɱɬɨ ɨɧɚ ɛɭɞɟɬ ɡɚɛɵɬɚ.
330
Ãëàâà 6. Ñèíòàêñèñ îïåðàòîðîâ SQL
Ɉɩɟɪɚɬɨɪ SHOW SLAVE STATUS ɜɤɥɸɱɚɟɬ ɩɨɥɹ ɜɵɜɨɞɚ, ɤɨɬɨɪɵɟ ɨɬɨɛɪɚɠɚɸɬ ɬɟɤɭɳɢɟ
ɭɫɬɚɧɨɜɤɢ UNTIL.
Ⱦɨ ɜɟɪɫɢɢ MySQL 4.0.5 ɷɬɨɬ ɨɩɟɪɚɬɨɪ ɧɚɡɵɜɚɥɫɹ SLAVE START. ȼ ɧɚɫɬɨɹɳɟɟ ɜɪɟɦɹ ɬɚɤɨɟ ɧɚɩɢɫɚɧɢɟ ɩɪɢɧɢɦɚɟɬɫɹ ɞɥɹ ɨɛɪɚɬɧɨɣ ɫɨɜɦɟɫɬɢɦɨɫɬɢ, ɧɨ ɫɱɢɬɚɟɬɫɹ ɭɫɬɚɪɟɜɲɢɦ.
6.6.2.9. Ñèíòàêñèñ STOP SLAVE
STOP SLAVE [ɬɢɩ_ɩɨɬɨɤɚ [, ɬɢɩ_ɩɨɬɨɤɚ] ... ]
ɬɢɩ_ɩɨɬɨɤɚ: IO_THREAD | SQL_THREAD
Ɉɫɬɚɧɚɜɥɢɜɚɟɬ ɩɨɬɨɤɢ ɪɟɩɥɢɤɚɰɢɢ ɩɨɞɱɢɧɟɧɧɨɝɨ ɫɟɪɜɟɪɚ. STOP SLAVE ɬɪɟɛɭɟɬ ɩɪɢɜɢɥɟɝɢɢ SUPER.
Ʉɚɤ ɢ START SLAVE, ɧɚɱɢɧɚɹ ɫ MySQL 4.0.2, ɷɬɨɬ ɨɩɟɪɚɬɨɪ ɦɨɠɟɬ ɢɫɩɨɥɶɡɨɜɚɬɶɫɹ ɫ
ɨɩɰɢɹɦɢ IO_THREAD ɢ SQL_THREAD ɞɥɹ ɢɦɟɧɨɜɚɧɢɹ ɩɨɬɨɤɚ ɢɥɢ ɩɨɬɨɤɨɜ, ɤɨɬɨɪɵɟ ɧɭɠɧɨ
ɨɫɬɚɧɨɜɢɬɶ.
Ⱦɨ MySQL 4.0.5 ɨɩɟɪɚɬɨɪ ɧɚɡɵɜɚɥɫɹ SLAVE STOP. ȼ ɧɚɫɬɨɹɳɟɟ ɜɪɟɦɹ SLAVE STOP
ɩɪɢɧɢɦɚɟɬɫɹ ɞɥɹ ɨɛɪɚɬɧɨɣ ɫɨɜɦɟɫɬɢɦɨɫɬɢ, ɧɨ ɫɱɢɬɚɟɬɫɹ ɭɫɬɚɪɟɜɲɢɦ.
Скачать