记录一些日常使用过的Mariadb操作。
DUMP
-- single table without data
mariadb-dump -uname -p -d database tablename > tablename.sql
--single table with data
mariadb-dump -uname -p database tablename > tablename.sql
-- entire data
-- especilly: $(date +%Y%m%d) means year-month-day(eg: 20210101), just care about the space bettew date and +
mariadb-dump -uname -p database > backup$(date +%Y%m%d).sql
-- restore backup
mariadb -uname -p database < backup.sql
-- or login mariadb, then source backup
source /fullpath/backup.sql
UNIQUE KEY
-- when created
create table if not exists test1(
key1 varchar(1) not null,
key2 varchar(1) not null,
key3 varchar(1),
unique key uk_key1_key2 (key1, key2));
-- add unique key
alter table test1 add unique key uk_key1_key2 (key1, key2));
-- delete unique key
alter table test1 drop index uk_key1_key2;
-- on duplicate key update 插入不报错,直接更新剩余的字段,但是可能造成业务场景出错
insert into test1 (key1, key2, key3) values ('1', '1', 'd') on duplicate key update key3=values(key3);
这里主要是联合唯一,这是由于在设计文件表的时候产生的问题,多用户下,uid唯一,sha256唯一,但是filename在不同的用户那里呈现的是多态的,可能同一个文件不同的用户命名不同,也可能同一个文件名表示的具体文件不同,所以我的设计是uid+sha256组成联合唯一索引,这样filename可以根据用户期望情况展示,同时磁盘也不会多存。当然,这种设计的前提是单个用户的文件名不允许重复。