fangtx_create.sql 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. drop database if exists fangtx;
  2. create database fangtx default charset utf8 collate utf8_bin;
  3. use fangtx;
  4. /* 创建用户表 */
  5. create table `tb_user`
  6. (
  7. `userid` int auto_increment comment '编号',
  8. `username` varchar(20) not null comment '用户名',
  9. `password` char(32) not null comment '用户口令',
  10. `realname` varchar(20) not null comment '真实姓名',
  11. `sex` bool default 1 comment '性别',
  12. `tel` varchar(20) not null comment '手机号',
  13. `email` varchar(255) default '' comment '邮箱',
  14. `regdate` datetime default now() comment '注册日期',
  15. `point` int default 0 comment '积分',
  16. `lastvisit` datetime default now() comment '最后访问时间',
  17. `is_authenticated` bit default 0 comment '是否认证',
  18. primary key (`userid`)
  19. );
  20. /* 创建地区表 */
  21. create table `tb_district`
  22. (
  23. `distid` int not null comment '编号',
  24. `pid` int comment '父级行政单位',
  25. `name` varchar(255) not null comment '名称',
  26. `ishot` bool default 0 comment '是否为热门城市',
  27. `intro` varchar(255) default '' comment '介绍',
  28. primary key (distid)
  29. );
  30. /* 创建经理人表 */
  31. create table `tb_agent`
  32. (
  33. `agentid` int not null auto_increment comment '编号',
  34. `name` varchar(255) not null comment '姓名',
  35. `tel` varchar(20) not null comment '电话',
  36. `servstar` int not null default 0 comment '满意度星级',
  37. `realstar` int not null default 0 comment '真实度星级',
  38. `profstar` int not null default 0 comment '专业度星级',
  39. `certificated` bool not null default 0 comment '是否持有专业认证',
  40. primary key (`agentid`)
  41. );
  42. /* 创建用户登录日志表 */
  43. create table `tb_login_log`
  44. (
  45. `logid` bigint auto_increment comment '编号',
  46. `userid` int not null comment '用户',
  47. `ipaddr` varchar(255) not null comment 'IP地址',
  48. `logdate` datetime default now() comment '登录时间日期',
  49. `devcode` varchar(255) default '' comment '设备代码',
  50. primary key (`logid`)
  51. );
  52. /* 创建楼盘表 */
  53. create table `tb_estate`
  54. (
  55. `estateid` int not null auto_increment comment '编号',
  56. `distid` int not null comment '所在三级行政区域',
  57. `name` varchar(255) not null comment '名称',
  58. `hot` int default 0 comment '热度',
  59. `intro` varchar(511) default '' comment '介绍',
  60. primary key (`estateid`)
  61. );
  62. /* 创建经理人楼盘中间表 */
  63. create table `tb_agent_estate`
  64. (
  65. `agent_estate_id` int not null auto_increment comment '编号',
  66. `agentid` int not null comment '经理人',
  67. `estateid` int not null comment '楼盘',
  68. primary key (`agent_estate_id`)
  69. );
  70. /* 创建户型表 */
  71. create table `tb_house_type`
  72. (
  73. `typeid` int comment '编号',
  74. `name` varchar(255) not null comment '名称',
  75. primary key (`typeid`)
  76. );
  77. /* 创建房源信息表 */
  78. create table `tb_house_info`
  79. (
  80. `houseid` int not null auto_increment comment '编号',
  81. `title` varchar(50) not null comment '标题',
  82. `area` int not null comment '面积',
  83. `floor` int not null comment '楼层',
  84. `totalfloor` int not null comment '总楼层',
  85. `direction` varchar(10) not null comment '朝向',
  86. `price` int not null comment '价格',
  87. `priceunit` varchar(10) not null comment '价格单位',
  88. `detail` varchar(511) default '' comment '详情',
  89. `mainphoto` varchar(255) not null comment '主图',
  90. `pubdate` date not null comment '发布日期',
  91. `street` varchar(255) not null comment '街道',
  92. `hassubway` bool default 0 comment '是否有地铁',
  93. `isshared` bool default 0 comment '是否支持合租',
  94. `hasagentfees` bool default 0 comment '是否有中介费',
  95. `typeid` int not null comment '户型',
  96. `userid` int not null comment '发布用户',
  97. `distid2` int not null comment '所在二级行政区域',
  98. `distid3` int not null comment '所在三级行政区域',
  99. `estateid` int comment '楼盘',
  100. `agentid` int comment '经理人',
  101. primary key (`houseid`)
  102. );
  103. /* 创建房源照片表 */
  104. create table `tb_house_photo`
  105. (
  106. `photoid` int not null auto_increment comment '编号',
  107. `houseid` int not null comment '房源',
  108. `path` varchar(255) not null comment '资源路径',
  109. primary key (`photoid`)
  110. );
  111. /* 创建标签表 */
  112. create table `tb_tag`
  113. (
  114. `tagid` int auto_increment comment '编号',
  115. `content` varchar(20) not null comment '内容',
  116. primary key (`tagid`)
  117. );
  118. /* 创建房源标签中间表 */
  119. create table `tb_house_tag`
  120. (
  121. `house_tag_id` int auto_increment comment '编号',
  122. `houseid` int not null comment '房源',
  123. `tagid` int not null comment '标签',
  124. primary key (`house_tag_id`)
  125. );
  126. /* 创建用户浏览历史记录表 */
  127. create table `tb_record`
  128. (
  129. `recordid` bigint auto_increment comment '编号',
  130. `userid` int not null comment '用户',
  131. `houseid` int not null comment '房源',
  132. `recorddate` datetime not null comment '浏览时间日期',
  133. primary key (`recordid`)
  134. );
  135. /* 创建用户令牌表 */
  136. create table `tb_user_token`
  137. (
  138. `tokenid` int auto_increment comment '编号',
  139. `token` char(32) not null comment '令牌',
  140. `userid` int not null comment '用户',
  141. primary key (`tokenid`)
  142. );
  143. /* 创建角色表 */
  144. create table `tb_role`
  145. (
  146. `roleid` int auto_increment comment '编号',
  147. `rolename` varchar(255) not null comment '角色名',
  148. primary key (`roleid`)
  149. );
  150. /* 创建权限表 */
  151. create table `tb_privilege`
  152. (
  153. `privid` int auto_increment comment '编号',
  154. `method` varchar(15) not null comment '请求方法',
  155. `url` varchar(1024) not null comment '资源的URL',
  156. PRIMARY KEY (`privid`)
  157. );
  158. /* 创建用户角色中间表 */
  159. create table `tb_user_role`
  160. (
  161. `urid` int auto_increment comment '编号',
  162. `userid` int not null comment '用户',
  163. `roleid` int not null comment '角色',
  164. primary key (`urid`)
  165. );
  166. /* 创建角色权限中间表 */
  167. create table `tb_role_privilege`
  168. (
  169. `rpid` int auto_increment comment '编号',
  170. `roleid` int not null comment '角色',
  171. `privid` int not null comment '权限',
  172. primary key (`rpid`)
  173. );
  174. create unique index `uni_idx_agent_estate` on `tb_agent_estate` (`agentid`, `estateid`);
  175. create unique index `uni_idx_record` on `tb_record` (`userid`, `houseid`);
  176. create unique index `uni_idx_userid` on `tb_user_token` (`userid`);
  177. create unique index `uni_idx_username` on `tb_user` (`username`);
  178. create unique index `uni_idx_tel` on `tb_user` (`tel`);
  179. create unique index `uni_idx_email` on `tb_user` (`email`);
  180. create unique index `uni_idx_house_tag` on `tb_house_tag` (`houseid`, `tagid`);
  181. alter table `tb_agent_estate` add constraint `fk_agent_estate_agentid` foreign key (`agentid`) references `tb_agent` (`agentid`);
  182. alter table `tb_agent_estate` add constraint `fk_agent_estate_estateid` foreign key (`estateid`) references `tb_estate` (`estateid`);
  183. alter table `tb_district` add constraint `fk_district_pid` foreign key (`pid`) references `tb_district` (`distid`);
  184. alter table `tb_estate` add constraint `fk_estate_distid` foreign key (`distid`) references `tb_district` (`distid`);
  185. alter table `tb_house_info` add constraint `fk_house_info_agentid` foreign key (`agentid`) references tb_agent (`agentid`);
  186. alter table `tb_house_info` add constraint `fk_house_info_distid2` foreign key (`distid2`) references tb_district (`distid`);
  187. alter table `tb_house_info` add constraint `fk_house_info_distid3` foreign key (`distid3`) references tb_district (`distid`);
  188. alter table `tb_house_info` add constraint `fk_house_info_estateid` foreign key (`estateid`) references tb_estate (`estateid`);
  189. alter table `tb_house_info` add constraint `fk_house_info_typeid` foreign key (`typeid`) references tb_house_type (`typeid`);
  190. alter table `tb_house_info` add constraint `fk_house_info_userid` foreign key (`userid`) references tb_user (`userid`);
  191. alter table `tb_house_photo` add constraint `fk_house_photo_houseid` foreign key (`houseid`) references `tb_house_info` (`houseid`);
  192. alter table `tb_house_tag` add constraint `fk_house_tag_houseid` foreign key (`houseid`) references `tb_house_info` (`houseid`);
  193. alter table `tb_house_tag` add constraint `fk_house_tag_tagid` foreign key (`tagid`) references `tb_tag` (`tagid`);
  194. alter table `tb_login_log` add constraint `fk_login_log_userid` foreign key (`userid`) references `tb_user` (`userid`);
  195. alter table `tb_record` add constraint `fk_record_houseid` foreign key (`houseid`) references `tb_house_info` (`houseid`);
  196. alter table `tb_record` add constraint `fk_record_userid` foreign key (`userid`) references `tb_user` (`userid`);
  197. alter table `tb_user_token` add constraint `fk_token_userid` foreign key (`userid`) references `tb_user` (`userid`);
  198. alter table `tb_user_role` add constraint `uni_user_role` unique (`userid`, `roleid`);
  199. alter table `tb_role_privilege` add constraint `uni_role_priv` unique (`roleid`, `privid`);
  200. alter table `tb_role_privilege` add constraint `fk_role_privilege_privid` foreign key (`privid`) references `tb_privilege` (`privid`);
  201. alter table `tb_role_privilege` add constraint `fk_role_privilege_roleid` foreign key (`roleid`) references `tb_role` (`roleid`);
  202. alter table `tb_user_role` add constraint `fk_user_role_roleid` foreign key (`roleid`) references `tb_role` (`roleid`);
  203. alter table `tb_user_role` add constraint `fk_user_role_userid` foreign key (`userid`) references `tb_user` (`userid`);