5. ? 虚拟列
1. CREATE TABLE example_virtual_columns(
a INT(11) PRIMARY KEY,
b VARCHAR(32),
c INT(11) AS (a mod 10) VIRTUAL,
d VARCHAR(5) as (left(b, 5)) PERSISTENT);
虚拟列c的值将会在查询时计算,而虚拟列d的值被存储在表中,查询的时候直接从表里取出。
2. 在执行插入操作时,虚拟列使用default关键字代替就可以。如果为虚拟列指定值,将会导
致错误的发生:
mariadb[dbtest]> INSERT INTO example_virtual_columns VALUES (16, "abcdefghijkl", default, default);
Query OK, 1 row affected (0.01 sec)
mariadb[dbtest]> SELECT * FROM example_virtual_columns;
+-------+-------------------+--------+------------------+
| a | b | c | d |
+-------+-------------------+--------+------------------+
| 16 | abcdefghijkl | 6 | abcde |
+-------+-------------------+--------+------------------+
1 row in set (0.00 sec)
mariadb[dbtest]> INSERT INTO example_virtual_columns VALUES (17, "abcdefghijkl", default, "there");
ERROR 1906 (HY000): The value specified for computed column 'd' in table 'example_virtual_columns' ignored
6. ? 动态列(1)
1. 动态列适合于某些不确定的场景,例如某个商品的属性个数不确定并且将来可能还会添加。
2. 想要使用动态列,首先表中必须包含blob类型的列:
create table assets (
item_name varchar(32) primary key,
dynamic_cols blob
);
3. 接下来,就可以使用MariaDB定义的动态列操作函数对动态列进行存取操作:
INSERT INTO assets VALUES ("MariaDB T-shirt", COLUMN_CREATE("color", "blue", "size", "XL"));
INSERT INTO assets VALUES ("Thinkpad Laptop", COLUMN_CREATE ("color", "black", "price", 500));
以上两条语句往assets表中插入了两行记录,接下来查询商品的颜色情况:
mariadb[dbtest]> SELECT item_name, COLUMN_GET(dynamic_cols, "color" as char) AS color FROM
assets;
+----------------------------+--------+
| item_name | color |
+----------------------------+--------+
| MariaDB T-shirt | blue |
| Thinkpad Laptop | black |
+----------------------------+--------+
20. ?MariaDB多源复制相关的命令:
1. CHANGE MASTER ["connection-name"] TO …
2. START SLAVE ["connection-name"]
3. START ALL SLAVE
4. STOP SLAVE ["connection-name"]
5. STOP ALL SLAVE
6. RESET SLAVE ["connection-name"]
7. SHOW RELAYLOG ["connection-name"] EVENTS
8. SHOW SLAVE ["connection-name"] STATUS
9. SHOW ALL SLAVE STATUS