Answers
我的(多年前同事教的,原本还有后缀 info,relation 来区别表类型,还有二级主题默认为base,我觉查看表列表时排序乱给去掉了):
表名由"分区
模块
主题[_二级主题]"组成, 推荐最多两级主题.
分区分别为:
a: 应用区, 存放应用数据
b: 仓库区, 存放历史数据
m: 市场区, 存放结果数据
s: 缓冲区, 存放缓冲数据
字段名推荐:
id 主键, CHAR(20)
pid 父键, CHAR(20)
xx_id 外键, CHAR(20), xx为关联表缩写
dflag 删除标识, TINYINT, 0为正常, 1为删除, 可用其他数字表示其他状态
ctime 创建时间, DATETIME或TIMESTAMP
mtime 修改时间, DATETIME或TIMESTAMP
btime 开始时间, DATETIME或TIMESTAMP
etime 结束时间, DATETIME或TIMESTAMP
FIRST
answered 10 years, 1 month ago
这是去哪儿内部的一个分享ppt
去哪儿MySQL开发规范.pdf:
http://vdisk.weibo.com/s/dR8G_5nKajgp
看以参照下,不过每个都有自己的规范,相对而言有个约束有利于协作
Cybunny
answered 10 years, 1 month ago
我认为表像这么设计的话, 各个表的关系一目了然. 我认为最烂的表就是comment表引用了user表的user_id, 在comment表却叫u_id, 这尼玛真是让人蛋疼.
MySQL关键字最好大写, 记得为每个字段写注释, 更严格的话, 为每个字段加上
反引号
DROP DATABASE IF EXISTS guodou;
CREATE DATABASE guodou;
use guodou;
DROP TABLE IF EXISTS user;
CREATE TABLE user(
user_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_nickname VARCHAR(20) NOT NULL COMMENT "昵称",
user_email VARCHAR(100) NOT NULL COMMENT "注册邮箱",
user_passwd CHAR(32) NOT NULL COMMENT "密码, MD5",
user_avatar CHAR(32) NULL COMMENT "头像, MD5",
user_desc TEXT NULL COMMENT "个人简介",
user_website VARCHAR(200) NULL COMMENT "个人网站",
user_gender CHAR(1) NOT NULL DEFAULT "O" COMMENT "性别, M=Man, W=woman, O=other",
user_ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT "创建日期",
user_isadmin CHAR(1) NOT NULL DEFAULT "N" COMMENT "是否为管理员, Y=是, N=否",
UNIQUE KEY(user_nickname),
UNIQUE KEY(user_email)
)ENGINE=MyISAM AUTO_INCREMENT=100000000 DEFAULT CHARSET=utf8 COMMENT="用户表";
DROP TABLE IF EXISTS product;
CREATE TABLE product(
product_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(100) NOT NULL COMMENT "产品名称",
product_path VARCHAR(80) NULL COMMENT "所属类别path",
product_price FLOAT(10, 2) NOT NULL DEFAULT 0.00 COMMENT "价格",
product_currency CHAR(5) NOT NULL DEFAULT "RMB" COMMENT "币种",
product_from VARCHAR(20) NOT NULL COMMENT "来源, 比如天猫, 京东, fancy",
product_from_url VARCHAR(1024) NOT NULL COMMENT "来源URL",
product_like_count INT UNSIGNED NOT NULL DEFAULT 0 COMMENT "该产品有多少喜欢",
product_img_dir CHAR(32) NOT NULL COMMENT "存放该产品的所有图片的目录名, MD5",
product_brand VARCHAR(50) NULL COMMENT "品牌",
user_id INT UNSIGNED NOT NULL COMMENT "用户ID"
)ENGINE=MyISAM AUTO_INCREMENT=100000000 DEFAULT CHARSET=utf8 COMMENT="产品表";
DROP TABLE IF EXISTS comment;
CREATE TABLE comment(
comment_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
comment_content TEXT NOT NULL COMMENT "评论内容",
comment_ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT "发布该评论时间",
comment_ref INT UNSIGNED NULL COMMENT "所引用的评论ID, 即comment_id",
comment_poke_count INT UNSIGNED NOT NULL DEFAULT 0 COMMENT "被点赞多少次",
product_id INT UNSIGNED NOT NULL COMMENT "产品ID",
user_id INT UNSIGNED NOT NULL COMMENT "用户ID"
)ENGINE=MyISAM AUTO_INCREMENT=100000000 DEFAULT CHARSET=utf8 COMMENT="评论表";
DROP TABLE IF EXISTS pokelog;
CREATE TABLE pokelog(
comment_id INT UNSIGNED NOT NULL COMMENT "评论ID",
user_id INT UNSIGNED NOT NULL COMMENT "用户ID"
)ENGINE=MyISAM AUTO_INCREMENT=100000000 DEFAULT CHARSET=utf8 COMMENT="点赞log";
DROP TABLE IF EXISTS likelog;
CREATE TABLE likelog(
product_id INT UNSIGNED NOT NULL COMMENT "产品ID",
user_id INT UNSIGNED NOT NULL COMMENT "用户ID"
)ENGINE=MyISAM AUTO_INCREMENT=100000000 DEFAULT CHARSET=utf8 COMMENT="喜欢log";
DROP TABLE IF EXISTS tag;
CREATE TABLE tag(
tag_name VARCHAR(50) NULL COMMENT "标签",
comment_id INT UNSIGNED NOT NULL COMMENT "评论ID",
product_id INT UNSIGNED NOT NULL COMMENT "产品ID",
user_id INT UNSIGNED NOT NULL COMMENT "用户ID"
)ENGINE=MyISAM AUTO_INCREMENT=100000000 DEFAULT CHARSET=utf8 COMMENT="用户为某产品做出评论所使用的标签";
DROP TABLE IF EXISTS choiceness;
CREATE TABLE choiceness(
choiceness_date DATE NOT NULL COMMENT "Y-M-D, 每日精选",
user_id INT UNSIGNED NOT NULL DEFAULT 0 COMMENT "操作人, 0=系统程序, >0即为用户ID",
product_id INT UNSIGNED COMMENT "产品ID",
INDEX(choiceness_date)
)ENGINE=MyISAM AUTO_INCREMENT=100000000 DEFAULT CHARSET=utf8 COMMENT="精选的产品, 将放于首页";
DROP TABLE IF EXISTS hot;
CREATE TABLE hot(
hot_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT "创建日期",
product_id INT UNSIGNED COMMENT "产品ID",
INDEX(hot_date)
)ENGINE=MyISAM AUTO_INCREMENT=100000000 DEFAULT CHARSET=utf8 COMMENT="热门的产品";
alucart
answered 10 years, 1 month ago