1. 使用全局表
  • 在schema.xml中配置全局表
<table name="dict" primaryKey="did" type="global" dataNode="node1,node2,node3"/>
  • 启动执行mycat
mycat console或mycat start
  • 连接mycat,执行建表语句
CREATE TABLE dict(
    did BIGINT AUTO_INCREMENT,
    title   VARCHAR(50),
    content   TEXT ,
    CONSTRAINT pk_did PRIMARY KEY(did)
)ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='字典表';
  • 插入数据
INSERT INTO dict(title,content) VALUES ('info-a',@@hostname);
INSERT INTO dict(title,content) VALUES ('info-b',@@hostname);
INSERT INTO dict(title,content) VALUES ('info-c',@@hostname);
  • 查询
select * from dict;
  1. 增加全局区域表
CREATE TABLE area (
	id INT NOT NULL PRIMARY KEY auto_increment,
	NAME VARCHAR (20) NOT NULL
)ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='区域表';

 insert into area(name) values('北京');
 insert into area(name) values('上海');
 insert into area(name) values('广州');
 insert into area(name) values('深圳');
  1. ER关系表
  • 配置ER关系
<table name="t_order" primaryKey="id" autoIncrement="true" dataNode="node1,node2,node3" rule="mod-long">
    <childTable name="t_order_detail" primaryKey="id" joinKey="order_id" parentKey="id" />
</table>
  • 创建表
CREATE TABLE `t_order` (
`id` int(11) NOT NULL PRIMARY KEY,
`sn` varchar(64) NOT NULL,
`create_time` datetime NOT NULL,
KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='订单表';

CREATE TABLE `t_order_detail` (
`id` int(11) NOT NULL PRIMARY KEY,
`order_id` int(11) NOT NULL,
`address` varchar(128) NOT NULL,
`create_time` datetime NOT NULL,
KEY `fk_ordid` (`order_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='订单详情表';
  • 插入数据
INSERT INTO `t_order` (id,sn,create_time) VALUES (next value for MYCATSEQ_ORDERSEQ, 'BJ0001', NOW());
INSERT INTO `t_order` (id,sn,create_time) VALUES (next value for MYCATSEQ_ORDERSEQ, 'BJ0002', NOW());
INSERT INTO `t_order` (id,sn,create_time) VALUES (next value for MYCATSEQ_ORDERSEQ, 'BJ0003', NOW());
INSERT INTO `t_order` (id,sn,create_time) VALUES (next value for MYCATSEQ_ORDERSEQ, 'BJ0004',NOW());

INSERT INTO t_order_detail(id,order_id,address,create_time) VALUES (next value for MYCATSEQ_ORDERDETAILSEQ,1,'test data of ORDER1(ID=1,BJ001) ',NOW());
INSERT INTO t_order_detail(id,order_id,address,create_time) VALUES (next value for MYCATSEQ_ORDERDETAILSEQ,2,'test data of ORDER1(ID=1,BJ002) ',NOW());
INSERT INTO t_order_detail(id,order_id,address,create_time) VALUES (next value for MYCATSEQ_ORDERDETAILSEQ,2,'test data of ORDER1(ID=1,BJ002) ',NOW());
INSERT INTO t_order_detail(id,order_id,address,create_time) VALUES (next value for MYCATSEQ_ORDERDETAILSEQ,3,'test data of ORDER1(ID=1,BJ003) ',NOW());
INSERT INTO t_order_detail(id,order_id,address,create_time) VALUES (next value for MYCATSEQ_ORDERDETAILSEQ,3,'test data of ORDER1(ID=1,BJ003) ',NOW());
INSERT INTO t_order_detail(id,order_id,address,create_time) VALUES (next value for MYCATSEQ_ORDERDETAILSEQ,4,'test data of ORDER1(ID=1,BJ004) ',NOW());
  • 查询
select * from t_order a join t_order_detail b on a.id = b.order_id;