天天看點

MySQL三大範式1、第一範式(1NF)2、第二範式(2NF)3、第三範式(3NF)

1、第一範式(1NF)

資料表中的所有字段都是不可分割的原子值?

不滿足第一範式:
address字段屬性值為:中國湖北武漢光谷

address字段可以拆分為: 中國	湖北		武漢		光谷
           

2、第二範式(2NF)

  • 必須滿足第一範式的前提下,除主鍵外的每一列都必須完全依賴于主鍵。
  • 如果出現不完全依賴,隻可能出現在聯合主鍵的情況下
create table order{
	customer_id int,
	producr_id int,
	customer_name varchar(20);
	product_name varchar(20);
	primary key(customer_id,producr_id)
};

--問題?
--除主鍵意外其他列,隻依賴于主鍵的部分字段。(不滿足第二範式)。
--解決方法:拆表。

create table order(
	order_id int primary key,
	customer_id int,
	product_id int,
);

create table customer(
	id int,
	name varchar(20),
);

create table product(
	id int,
	name varchar(20),
);
           

3、第三範式(3NF)

  • 必須滿足第二範式的前提下,除主鍵外的其他列之間不能有傳遞依賴關系
create table order(
	order_id int primary key,
	customer_id int,
	product_id int,
	customer_phone varchar(11),
);


--不滿足第三範式:customer_id 與 customer_phone 之間有傳遞依賴關系
--解決:拆分

create table order(
	order_id int primary key,
	customer_id int,
	product_id int,
);

create table customer(
	id int,
	name varchar(20),
	phone varchar(11),
);