----创建商品数据表,项目统一前缀为:shop_
----goods_id 的数据类型设置为:mediumint,占3个字节,无符号的范围是0到16777215
----goods_name 商品名称不可以为空,varchar类型,最长字符设置为32
----goods_price商品价格的数据类型应设置为 decimal ,在处理和金钱有关系的数据的时候,整型和浮点型都无能为力了,因为精确度不够,这就要用到decimal类型,decimal(10,2)中的10,表示小数点左边和右边存储的十进制的最大位数,2表示小数点后面的位数为2位,float存贮字段,会自动将数值四舍五入,而decimal不会,如果超过了精度和标度值,decimal会报错,而float会自动四舍五入,并且不报错。
----goods-number 商品数量,采用smallint 数据类型,2个字节,无符号的范围是0到65535。考虑进货成本,六万多件商品已经足够用了
----goods_weigt ,商品重量,
----is_sale 上架 ,下架,采用enum类型,枚举类型,如果在is_sale字段列插入除了“上架”“下架”的其他字符,则视为空字符串,enum的索引从1开始,空字符串作为错误值的索引值为 0 ,可以用在找出被指定无效枚举值的数据行:SELECT * FROM tbl_name WHERE enum_col=0;, where is_sale ='上架' 和 where is_sale = 1 ,的含义是一样的。
----goods_is 设置为主键 primary key ,有唯一性
----每个商品名称不可以重复,所以要设置为 unique key,规范数据的唯一性,起到约束作用,同时也在这个key上建立一个主键索引,唯一标识数据库表中的每条记录。
----每个数据表只能有一个 PRIMARY KEY,但可以有多个 UNIQUE
----key为普通索引,市场价格,本店价格,商品分类 商品品牌,都要建立索引
----key index互为别名
----存储引擎选择 Innodb,而不是myisam,需要大量的select的时候,采用myisam引擎,查询速度快,电商项目执行的insert或update较多,所以采用innodb,这种引擎支持外键
create table shop_goods(
goods_id mediumint unsigned not null auto_increment comment '主键',
goods_name varchar(32) not null comment '商品名称',
goods_price decimal(10,2) not null default 0 comment '市场价格',
goods_shop_price decimal(10,2) not null default 0 comment '本店价格',
goods_number smallint not null default 1 comment '商品数量',
goods_weight smallint not null default 0 comment '商品重量',
cat_id mediumint not null default 0 comment '商品分类',
brand_id mediumint not null default 0 comment '商品品牌',
goods_big_logo char(100) not null default '' comment '商品大图片',
goods_small_logo char(100) not null default '' comment '商品缩略图',
goods_introduce text comment '商品介绍',
is_sale enum('上架','下架') not null default '上架' comment '上架,下架',
is_rec enum('推荐','不推荐') not null default '不推荐' comment '是否推荐',
is_hot enum('热销','不热销') not null default '不热销' comment '是否热销',
is_new enum('新品','不新品') not null default '不新品' comment '是否新品',
add_time int not null comment '添加信息时间',
upd_time int not null comment '修改信息时间',
is_del enum('删除','不删除') not null default '不删除' comment '是否删除',
primary key (goods_id),
unique key (goods_name),
key (goods_shop_price),
key(goods_price),
key(cat_id),
key(brand_id),
key(add_time)
)engine=Innodb charset=utf8;
//以上代码运行正常,出现 [Err] 1064 错误,请检查一下里面的空格,应用英文下的空格!
//创建一个shop数据库
create database shop