「這是我參與8月更文挑戰的第4天,活動詳情查看:8月更文挑戰」mysql
-u
-p
-S
-h
-P
-e
複製代碼
示例:sql
# mysql -S /tmp/mysql.sock
# mysql -uroot -p -hlocalhost -P3306
複製代碼
-e
參數的命令是不進行交互:mysql -uroot -e "show processlist";
shell
# mysql -uroot -e "show processlist;"
+----+------+-----------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+----------+------------------+
| 10 | root | localhost | NULL | Query | 0 | starting | show processlist |
+----+------+-----------+------+---------+------+----------+------------------+
複製代碼
<
用於恢復數據,將外部數據導入到 MySQL 中。數據庫
好比咱們將 t_xz_invite
表的數據導入到 MySQL 中。bash
# mysql -uroot account < t_xz_invite.sq
複製代碼
help —— 幫助命令微信
\c
清除當前輸入,結束上一個命令(control + c)\q
退出 mysql(control + d)(exit)\G
格式化輸出結果source
恢復數據(同 <)庫markdown
表post
至關於 MySQL 的一個密碼本(字符集編碼)。ui
咱們能夠經過 show charset;
命令來查看 MySQL 支持的字符集。編碼
mysql> show charset;
+----------+---------------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+---------------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
| utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
+----------+---------------------------------+---------------------+--------+
41 rows in set (0.02 sec)
複製代碼
MySQL5.7 默認的字符集是 latin1
,固然,這是一個歷史遺留的問題。
中文在下面幾個字符集中佔得字節:
注意:utf8mb4 支持 emoji。
咱們使用 show collation;
命令查看排序規則。
注意:排序規則涉及 字符串大小寫敏感 問題。
mysql> show collation;
+--------------------------+----------+-----+---------+----------+---------+
| Collation | Charset | Id | Default | Compiled | Sortlen |
+--------------------------+----------+-----+---------+----------+---------+
| big5_chinese_ci | big5 | 1 | Yes | Yes | 1 |
| big5_bin | big5 | 84 | | Yes | 1 |
| dec8_swedish_ci | dec8 | 3 | Yes | Yes | 1 |
| dec8_bin | dec8 | 69 | | Yes | 1 |
| gb18030_unicode_520_ci | gb18030 | 250 | | Yes | 8 |
+--------------------------+----------+-----+---------+----------+---------+
222 rows in set (0.00 sec)
複製代碼
咱們以 utf8mb4
爲例:
| utf8mb4_general_ci | utf8mb4 | 45 | Yes | Yes | 1 |
| utf8mb4_bin | utf8mb4 | 46 | | Yes | 1 |
| utf8mb4_unicode_ci | utf8mb4 | 224 | | Yes | 8 |
| utf8mb4_icelandic_ci | utf8mb4 | 225 | | Yes | 8 |
| utf8mb4_latvian_ci | utf8mb4 | 226 | | Yes | 8 |
| utf8mb4_romanian_ci | utf8mb4 | 227 | | Yes | 8 |
| utf8mb4_slovenian_ci | utf8mb4 | 228 | | Yes | 8 |
| utf8mb4_polish_ci | utf8mb4 | 229 | | Yes | 8 |
| utf8mb4_estonian_ci | utf8mb4 | 230 | | Yes | 8 |
| utf8mb4_spanish_ci | utf8mb4 | 231 | | Yes | 8 |
| utf8mb4_swedish_ci | utf8mb4 | 232 | | Yes | 8 |
| utf8mb4_turkish_ci | utf8mb4 | 233 | | Yes | 8 |
| utf8mb4_czech_ci | utf8mb4 | 234 | | Yes | 8 |
| utf8mb4_danish_ci | utf8mb4 | 235 | | Yes | 8 |
| utf8mb4_lithuanian_ci | utf8mb4 | 236 | | Yes | 8 |
| utf8mb4_slovak_ci | utf8mb4 | 237 | | Yes | 8 |
| utf8mb4_spanish2_ci | utf8mb4 | 238 | | Yes | 8 |
| utf8mb4_roman_ci | utf8mb4 | 239 | | Yes | 8 |
| utf8mb4_persian_ci | utf8mb4 | 240 | | Yes | 8 |
| utf8mb4_esperanto_ci | utf8mb4 | 241 | | Yes | 8 |
| utf8mb4_hungarian_ci | utf8mb4 | 242 | | Yes | 8 |
| utf8mb4_sinhala_ci | utf8mb4 | 243 | | Yes | 8 |
| utf8mb4_german2_ci | utf8mb4 | 244 | | Yes | 8 |
| utf8mb4_croatian_ci | utf8mb4 | 245 | | Yes | 8 |
| utf8mb4_unicode_520_ci | utf8mb4 | 246 | | Yes | 8 |
| utf8mb4_vietnamese_ci | utf8mb4 | 247 | | Yes | 8 |
複製代碼
以 _ci
結尾的排序規則是大小寫不敏感的,好比 utf8mb4_general_ci
,然後面的 Yes
表明這是默認的排序規則。
所以,當咱們須要大小寫敏感的時候,須要修改相應的排序規則。
數據類型在 MySQL 中被規劃爲 4 種:
類 | 類型 | 說明 |
---|---|---|
整數 | tinyint | 極小整數類型(0-255),有符號的爲 -128 - 127 |
整數 | smallint | 較小整數數據類型(-2^15 到 2^15-1) |
整數 | mediumint | 中型整數數據類型 |
整數 | int | 常規(平均)大小的整數數據類型(-2^31 到 2^31-1) |
整數 | bigint | 較大整數數據類型(-2^63 到 2^63-1) |
浮點數 | float | 小型單精度(四個字節)浮點數 |
浮點數 | double | 常規雙精度(八個字節)浮點數 |
定點數 | decimal | 包含整數部分、小數部分或同時包括兩者的精確值數值 |
BIT | bit | 位字段值 |
2^31=2147483648
,所以 int 最多存儲 10 位。
須要特別注意的是:
char
:
varchar
:
enum
:
char 最多存儲 255 個字符,varchar 最多存儲 65535 個字符(實際上是 65533 個,用兩個來存儲長度)
如何選擇兩種數據類型呢?
datetime
:
timestamp
:
mysql> create database account charset utf8mb4 collate utf8mb4_bin;
Query OK, 1 row affected (0.00 sec)
複製代碼
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| account |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
複製代碼
mysql> show create database account;
+----------+-----------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------------------------------+
| account | CREATE DATABASE `account` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin */ |
+----------+-----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
複製代碼
複製代碼
修改數據庫:
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> show create database test;
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| test | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter database test charset utf8mb4;
Query OK, 1 row affected (0.00 sec)
mysql> show create database test;
+----------+------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------+
| test | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ |
+----------+------------------------------------------------------------------+
1 row in set (0.00 sec)
複製代碼
* 修改排序類型同理。
複製代碼
create table student (
id INT primary key NOT NULL AUTO_INCREMENT COMMENT '學號',
sname VARCHAR(255) NOT NULL COMMENT '姓名',
age TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '年齡',
gender ENUM('m','f','n') NOT NULL DEFAULT 'n' COMMENT '性別',
intime DATETIME NOT NULL DEFAULT NOW() COMMENT '入學時間'
) ENGINE InnoDB CHARSET utf8mb4;
複製代碼
建表規範:
查詢建表信息:
mysql> show tables;
+-------------------+
| Tables_in_account |
+-------------------+
| student |
+-------------------+
1 row in set (0.00 sec)
mysql> show create table student;
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '學號',
`sname` varchar(255) NOT NULL COMMENT '姓名',
`age` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '年齡',
`gender` enum('m','f','n') NOT NULL DEFAULT 'n' COMMENT '性別',
`intime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '入學時間',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
複製代碼
mysql> create table test like student;
Query OK, 0 rows affected (0.04 sec)
複製代碼
mysql> drop table test;
Query OK, 0 rows affected (0.02 sec)
複製代碼
mysql> desc student;
+--------+---------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(255) | NO | | NULL | |
| age | tinyint(3) unsigned | NO | | 0 | |
| gender | enum('m','f','n') | NO | | n | |
| intime | datetime | NO | | CURRENT_TIMESTAMP | |
+--------+---------------------+------+-----+-------------------+----------------+
5 rows in set (0.00 sec)
複製代碼
mysql> alter table student add qq varchar(20) NOT NULL COMMENT 'qq號';
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+--------+---------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(255) | NO | | NULL | |
| age | tinyint(3) unsigned | NO | | 0 | |
| gender | enum('m','f','n') | NO | | n | |
| intime | datetime | NO | | CURRENT_TIMESTAMP | |
| qq | varchar(20) | NO | | NULL | |
+--------+---------------------+------+-----+-------------------+----------------+
6 rows in set (0.00 sec)
複製代碼
mysql> alter table student add wechat varchar(60) NOT NULL UNIQUE COMMENT '微信號' after sname;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+--------+---------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(255) | NO | | NULL | |
| wechat | varchar(60) | NO | UNI | NULL | |
| age | tinyint(3) unsigned | NO | | 0 | |
| gender | enum('m','f','n') | NO | | n | |
| intime | datetime | NO | | CURRENT_TIMESTAMP | |
| qq | varchar(20) | NO | | NULL | |
+--------+---------------------+------+-----+-------------------+----------------+
7 rows in set (0.00 sec)
複製代碼
mysql> alter table student add num INT NOT NULL COMMENT '身份證' FIRST;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+--------+---------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+-------------------+----------------+
| num | int(11) | NO | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(255) | NO | | NULL | |
| wechat | varchar(60) | NO | UNI | NULL | |
| age | tinyint(3) unsigned | NO | | 0 | |
| gender | enum('m','f','n') | NO | | n | |
| intime | datetime | NO | | CURRENT_TIMESTAMP | |
| qq | varchar(20) | NO | | NULL | |
+--------+---------------------+------+-----+-------------------+----------------+
8 rows in set (0.00 sec)
複製代碼
mysql> alter table student drop num;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+--------+---------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(255) | NO | | NULL | |
| wechat | varchar(60) | NO | UNI | NULL | |
| age | tinyint(3) unsigned | NO | | 0 | |
| gender | enum('m','f','n') | NO | | n | |
| intime | datetime | NO | | CURRENT_TIMESTAMP | |
| qq | varchar(20) | NO | | NULL | |
+--------+---------------------+------+-----+-------------------+----------------+
7 rows in set (0.01 sec)
複製代碼
mysql> alter table student modify sname varchar(64) NOT NULL COMMENT '姓名';
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0
複製代碼
mysql> alter table student change gender sex char(4) NOT NULL COMMENT '性別';
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+--------+---------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(64) | NO | | NULL | |
| wechat | varchar(60) | NO | UNI | NULL | |
| age | tinyint(3) unsigned | NO | | 0 | |
| sex | char(4) | NO | | NULL | |
| intime | datetime | NO | | CURRENT_TIMESTAMP | |
| qq | varchar(20) | NO | | NULL | |
+--------+---------------------+------+-----+-------------------+----------------+
7 rows in set (0.00 sec)
複製代碼
-- 插入一行,不帶字段
mysql> insert into student values(1,'關羽',20,'m',now());
Query OK, 1 row affected (0.00 sec)
-- 插入一行,帶字段
mysql> insert into student(sname,age,sex) values ('劉備',21,'m');
Query OK, 1 row affected (0.01 sec)
-- 多行插入
mysql> insert into student(sname,age,sex) values ('趙雲',22,'m'),('黃忠',23,'m');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+--------+-----+-----+---------------------+
| id | sname | age | sex | intime |
+----+--------+-----+-----+---------------------+
| 1 | 關羽 | 20 | m | 2021-01-24 20:21:50 |
| 2 | 劉備 | 21 | m | 2021-01-24 20:23:23 |
| 3 | 趙雲 | 22 | m | 2021-01-24 20:24:38 |
| 4 | 黃忠 | 23 | m | 2021-01-24 20:24:38 |
+----+--------+-----+-----+---------------------+
4 rows in set (0.00 sec)
複製代碼