mysql---三大范式
第一范式 1NF
数据表中的所有字段都是不可分割的原子值
地址:甘肃省兰州市城关区城关大道99号
这里面省份,市,区,详细地址都可以单独提出来,所以不满足第一范式
解决方法:将省份,市,区,详细地址单独设为一个字段
第二范式 2NF
必须是满足第一范式的前提下,第二范式要求,除主键外的每一列都必须完全依赖于主键.
如果要出现不完全依赖,只可能发生在联合主键的情况下
create table myorder(
product_id int,
customer_id int,
product_name varchar(20),
customer_name varchar(20),
primary key(product_id,customer_id) ---联合主键 product_id和customer_id都是主键
);
问题:上表中,除主键外的其他列只依赖于主键的部分字段(product_name只和product_id有关,customer_name只和customer_id有关,不满足第二范式完全依赖)
解决方法:拆表
create table myorder(
order_id int primary key,
product_id int,
customer_id int
); ------myorder表中product_id和customer_id完全依赖于主键order_id
create table product(
id int primary key,
name varchar(20)
); ------product表中name完全依赖于id
create table customer(
id int primary key,
name varchar(20)
); ------customer全依赖于id
第三范式 3NF
必须先满足第二范式,除开主键列的其他列之间不能存在传递依赖关系
create table myorder(
order_id int primary key,
product_id int,
customer_id int,
customer_phone int
);
----这里的customer_phone依赖于主键order_id,但同时它也跟customer_id有关,可以通过customer_id找到,这就是传递依赖关系
----解决方法,将customer_phone放到customer表中

浙公网安备 33010602011771号