diff --git "a/05 \350\226\233\347\245\226\344\277\241/20230921 \344\275\234\344\270\232.md" "b/05 \350\226\233\347\245\226\344\277\241/20230921 \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..c1ede06209e0965f61f56d2fa16824c634b47b4b --- /dev/null +++ "b/05 \350\226\233\347\245\226\344\277\241/20230921 \344\275\234\344\270\232.md" @@ -0,0 +1,108 @@ +笔记 + +``` +spu: 一种规格 +sku: 一种单位,唯一表示符 +``` + +代码 + +```mysql +CREATE DATABASE x CHARSET utf8; +use x; +/*==============================================================*/ +/* DBMS name: MySQL 5.0 */ +/* Created on: 2023-09-21 11:22:58 */ +/*==============================================================*/ + + +drop table if exists commodity; + +drop table if exists middle; + +drop table if exists property; + +drop table if exists specification; + +drop table if exists value; + +/*==============================================================*/ +/* Table: commodity */ +/*==============================================================*/ +create table commodity +( + co_id int not null auto_increment, + co_name varchar(30) not null, + introductory varchar(100) not null, + primary key (co_id) +); +INSERT into commodity VALUES (1,'华为手机Mate 60 Pro','遥遥领先'), +(2,'Redmi K60 至尊版 天玑9200+','小米配天玑越用越懵逼'); + +/*==============================================================*/ +/* Table: middle */ +/*==============================================================*/ +create table middle +( + m_id int not null auto_increment, + sp_id int, + p_id int, + v_id int, + primary key (m_id) +); +INSERT into middle VALUES(1,1,1,1),(2,1,2,5),(3,2,1,2),(4,2,2,8),(5,3,1,3),(6,3,2,6),(7,4,1,4),(8,4,2,7); + +/*==============================================================*/ +/* Table: property */ +/*==============================================================*/ +create table property +( + p_id int not null auto_increment, + p_name varchar(10) not null, + primary key (p_id) +); + +INSERT into property VALUES(1,'颜色'),(2,'存储版本'); +/*==============================================================*/ +/* Table: specification */ +/*==============================================================*/ +create table specification +( + sp_id int not null auto_increment, + co_id int, + sp_title varchar(50) not null, + price decimal(9,2) not null, + inventory varchar(10) not null, + primary key (sp_id) +); + +INSERT into specification VALUES (1,1,'华为(HUAWEI)旗舰手机 Mate 60 Pro 12GB+512GB ',6999,500),(2,1,'华为(HUAWEI)旗舰手机 Mate 60 Pro 12GB+1TB',7999,300),(3,2,' Redmi K60 至尊版 天玑9200+ 16GB+512GB',2999,5000),(4,2,' Redmi K60 至尊版 天玑9200+ 16GB+1TB',3299,5000); + +/*==============================================================*/ +/* Table: value */ +/*==============================================================*/ +create table value +( + v_id int not null auto_increment, + v_name varchar(20) not null, + primary key (v_id) +); + +INSERT into value VALUES(1,'雅川青'),(2,'鸭蛋黑'),(3,'晴雪'),(4,'耀眼的黑'),(5,'12GB+512GB'),(6,'16GB+512GB'),(7,'16GB+1TB'),(8,'12GB+1TB'); + + +alter table middle add constraint FK_Relationship_2 foreign key (sp_id) + references specification (sp_id) on delete restrict on update restrict; + +alter table middle add constraint FK_Relationship_3 foreign key (p_id) + references property (p_id) on delete restrict on update restrict; + +alter table middle add constraint FK_Relationship_4 foreign key (v_id) + references value (v_id) on delete restrict on update restrict; + +alter table specification add constraint FK_belong_pp foreign key (co_id) + references commodity (co_id) on delete restrict on update restrict; + +SELECT * from commodity c INNER JOIN specification s ON c.co_id=s.co_id INNER JOIN middle m ON s.sp_id=m.sp_id INNER JOIN property p ON m.p_id=p.p_id INNER JOIN value v on m.v_id=v.v_id where v.v_name='雅川青' OR c.co_name='华为手机Mate 60 Pro'; +``` +