看了我常用的数据库设计技巧,同事们都开始悄悄模仿……
用户名称字段定义成:yong_hu_ming、用户_name、name、user_name_123456789用户名称字段定义成:user_name字段名:PRODUCT_NAME、PRODUCT_name字段名:product_name字段名:productname、productName、product name、product@name字段名:product_name尽可能选择占用存储空间小的字段类型,在满足正常业务需求的情况下,从小到大,往上选。 如果字符串长度固定,或者差别不大,可以选择char类型。如果字符串长度差别较大,可以选择varchar类型。 是否字段,可以选择bit类型。 枚举字段,可以选择tinyint类型。 主键字段,可以选择bigint类型。 金额字段,可以选择decimal类型。 时间字段,可以选择timestamp或datetime类型。
在innodb中,需要额外的空间存储null值,需要占用更多的空间。 null值可能会导致索引失效。 null值只能用is null或者is not null判断,用=号判断永远返回false。
alter table product_sku add column brand_id int(10) not null default 0;create table class (id int(10) primary key auto_increment,cname varchar(15));
create table student(id int(10) primary key auto_increment,name varchar(15) not null,gender varchar(10) not null,cid int,foreign key(cid) references class(id));
a foreign key constraint failscreate table product_sku(id int(10) primary key auto_increment,spu_id int(10) not null,brand_id int(10) not null,name varchar(15) not null);
create table product_sku (id int(10) primary key auto_increment,spu_id int(10) not null,brand_id int(10) not null,name varchar(15) not null,KEY `ix_spu_id` (`spu_id`) USING BTREE,KEY `ix_brand_id` (`brand_id`) USING BTREE);
timestamp:用4个字节来保存数据,它的取值范围为1970-01-01 00:00:01 UTC ~ 2038-01-19 03:14:07。此外,它还跟时区有关。 datetime:用8个字节来保存数据,它的取值范围为1000-01-01 00:00:00 ~ 9999-12-31 23:59:59。它跟时区无关。
CREATE TABLE `order` (`id` bigint NOT NULL AUTO_INCREMENT,`code` varchar(20) COLLATE utf8mb4_bin NOT NULL,`name` varchar(30) COLLATE utf8mb4_bin NOT NULL,PRIMARY KEY (`id`),UNIQUE KEY `un_code` (`code`),KEY `un_code_name` (`code`,`name`) USING BTREE,KEY `idx_name` (`name`)) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
select * from order where name='yoyo';