logo icon
Taninut.com

145

 
 
 
Content ID145
Category ID10
Writer ID(not set)
User ID(not set)
Lang Modeth
Content Nameออกฐานข้อมูลสินค้าแบบ Variations product แบบที่ 2
Content Urlออกฐานข้อมูลสินค้าแบบ-Variations-product-แบบที่-2
Content Desc<div class="group w-full text-gray-800 dark:text-gray-100 border-b border-black/10 dark:border-gray-900/50 dark:bg-gray-800">
<div class="text-base gap-4 md:gap-6 md:max-w-2xl lg:max-w-xl xl:max-w-3xl p-4 md:py-6 flex lg:px-0 m-auto">
<div class="relative flex w-[calc(100%-50px)] flex-col gap-1 md:gap-3 lg:w-[calc(100%-115px)]">
<div class="flex flex-grow flex-col gap-3">
<div class="min-h-[20px] flex flex-col items-start gap-4 whitespace-pre-wrap"><strong>ออกฐานข้อมูลสินค้าแบบ Variations product&nbsp; :</strong><br><br>ฐานข้อมูลสินค้าแบบ Variations product จะประกอบด้วยตารางดังนี้<br><br></div>
</div>
</div>
</div>
</div>
<div class="group w-full text-gray-800 dark:text-gray-100 border-b border-black/10 dark:border-gray-900/50 bg-gray-50 dark:bg-[#444654]">
<div class="text-base gap-4 md:gap-6 md:max-w-2xl lg:max-w-xl xl:max-w-3xl p-4 md:py-6 flex lg:px-0 m-auto">
<div class="relative flex w-[calc(100%-50px)] flex-col gap-1 md:gap-3 lg:w-[calc(100%-115px)]">
<div class="flex flex-grow flex-col gap-3">
<div class="min-h-[20px] flex flex-col items-start gap-4 whitespace-pre-wrap">
<div class="markdown prose w-full break-words dark:prose-invert light">
<p><strong>Table: Product</strong></p>
<table>
<thead>
<tr>
<th>Field Name</th>
<th>Data Type</th>
<th>Key</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>product_id</td>
<td>INT</td>
<td>Primary</td>
<td>รหัสสินค้า</td>
</tr>
<tr>
<td>product_name</td>
<td>VARCHAR(255)</td>
<td>&nbsp;</td>
<td>ชื่อสินค้า</td>
</tr>
<tr>
<td>product_description</td>
<td>TEXT</td>
<td>&nbsp;</td>
<td>รายละเอียดสินค้า</td>
</tr>
<tr>
<td>product_image</td>
<td>VARCHAR(255)</td>
<td>&nbsp;</td>
<td>ชื่อไฟล์รูปภาพสินค้า</td>
</tr>
<tr>
<td>price</td>
<td>DECIMAL(10, 2)</td>
<td>&nbsp;</td>
<td>ราคาสินค้า</td>
</tr>
<tr>
<td>stock</td>
<td>INT</td>
<td>&nbsp;</td>
<td>จำนวนสต็อกสินค้า</td>
</tr>
</tbody>
</table>
<p><br><strong>Table: Variation option</strong></p>
<table>
<thead>
<tr>
<th>Field Name</th>
<th>Data Type</th>
<th>Key</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>variation_option_id</td>
<td>INT</td>
<td>Primary</td>
<td>รหัสตัวเลือกของสินค้า</td>
</tr>
<tr>
<td>variation_option_name</td>
<td>VARCHAR(255)</td>
<td>&nbsp;</td>
<td>ชื่อตัวเลือกของสินค้า</td>
</tr>
</tbody>
</table>
<br>
<p><strong>Table: Variation value</strong></p>
<table>
<thead>
<tr>
<th>Field Name</th>
<th>Data Type</th>
<th>Key</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>variation_value_id</td>
<td>INT</td>
<td>Primary</td>
<td>รหัสค่าตัวเลือกของสินค้า</td>
</tr>
<tr>
<td>variation_value_name</td>
<td>VARCHAR(255)</td>
<td>&nbsp;</td>
<td>ชื่อค่าตัวเลือกของสินค้า</td>
</tr>
<tr>
<td>variation_option_id</td>
<td>INT</td>
<td>Foreign</td>
<td>รหัสตัวเลือกของสินค้า</td>
</tr>
</tbody>
</table>
<br>
<p><strong>Table: Product variation</strong></p>
<table>
<thead>
<tr>
<th>Field Name</th>
<th>Data Type</th>
<th>Key</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>product_variation_id</td>
<td>INT</td>
<td>Primary</td>
<td>รหัสสินค้าแบบตัวเลือก</td>
</tr>
<tr>
<td>product_id</td>
<td>INT</td>
<td>Foreign</td>
<td>รหัสสินค้า</td>
</tr>
<tr>
<td>variation_value_id</td>
<td>INT</td>
<td>Foreign</td>
<td>รหัสค่าตัวเลือกของสินค้า</td>
</tr>
<tr>
<td>price</td>
<td>DECIMAL(10, 2)</td>
<td>&nbsp;</td>
<td>ราคาสินค้าตามตัวเลือก</td>
</tr>
<tr>
<td>stock</td>
<td>INT</td>
<td>&nbsp;</td>
<td>จำนวนสต็อกสินค้าตามตัวเลือก</td>
</tr>
</tbody>
</table>
<p><br><strong>Table: Product variation option</strong></p>
<table>
<thead>
<tr>
<th>Field Name</th>
<th>Data Type</th>
<th>Key</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>product_variation_id</td>
<td>INT</td>
<td>Foreign</td>
<td>รหัสสินค้าแบบตัวเลือก</td>
</tr>
<tr>
<td>variation_option_id</td>
<td>INT</td>
<td>Foreign</td>
<td>รหัสตัวเลือกของสินค้า</td>
</tr>
<tr>
<td>variation_value_id</td>
<td>INT</td>
<td>Foreign</td>
<td>รหัสค่าตัวเลือกของสินค้า</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<p>&nbsp;</p>
<p>ตัวอย่างฐานข้อมูลสินค้าแบบ Variations product แสดง table ตามนี้<br><br></p>
<p><strong>Table: Product</strong></p>
<table>
<thead>
<tr>
<th>product_id</th>
<th>product_name</th>
<th>product_description</th>
<th>product_image</th>
<th>price</th>
<th>stock</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Product A</td>
<td>Description A</td>
<td>image1.jpg</td>
<td>100</td>
<td>10</td>
</tr>
<tr>
<td>2</td>
<td>Product B</td>
<td>Description B</td>
<td>image2.jpg</td>
<td>200</td>
<td>5</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p><strong>Table: Variation option</strong></p>
<table>
<thead>
<tr>
<th>variation_option_id</th>
<th>variation_option_name</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Size</td>
</tr>
<tr>
<td>2</td>
<td>Color</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p><strong>Table: Variation value</strong></p>
<table>
<thead>
<tr>
<th>variation_value_id</th>
<th>variation_value_name</th>
<th>variation_option_id</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>S</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>M</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>L</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>Red</td>
<td>2</td>
</tr>
<tr>
<td>5</td>
<td>Green</td>
<td>2</td>
</tr>
<tr>
<td>6</td>
<td>Blue</td>
<td>2</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p><strong>Table: Product variation</strong></p>
<table>
<thead>
<tr>
<th>product_variation_id</th>
<th>product_id</th>
<th>variation_value_id</th>
<th>price</th>
<th>stock</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>100</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>2</td>
<td>110</td>
<td>4</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>3</td>
<td>120</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>1</td>
<td>4</td>
<td>100</td>
<td>2</td>
</tr>
<tr>
<td>5</td>
<td>1</td>
<td>5</td>
<td>100</td>
<td>3</td>
</tr>
<tr>
<td>6</td>
<td>1</td>
<td>6</td>
<td>100</td>
<td>4</td>
</tr>
<tr>
<td>7</td>
<td>2</td>
<td>1</td>
<td>200</td>
<td>2</td>
</tr>
<tr>
<td>8</td>
<td>2</td>
<td>2</td>
<td>220</td>
<td>3</td>
</tr>
<tr>
<td>9</td>
<td>2</td>
<td>3</td>
<td>230</td>
<td>1</td>
</tr>
<tr>
<td>10</td>
<td>2</td>
<td>4</td>
<td>200</td>
<td>0</td>
</tr>
<tr>
<td>11</td>
<td>2</td>
<td>5</td>
<td>210</td>
<td>2</td>
</tr>
<tr>
<td>12</td>
<td>2</td>
<td>6</td>
<td>210</td>
<td>1</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p><strong>Table: Product variation option</strong></p>
<table>
<thead>
<tr>
<th>product_variation_id</th>
<th>variation_option_id</th>
<th>variation_value_id</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>2</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>2</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>2</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>8</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>9</td>
<td>1</td>
<td>3</td>
</tr>
<tr>
<td>10</td>
<td>2</td>
<td>4</td>
</tr>
<tr>
<td>11</td>
<td>2</td>
<td>5</td>
</tr>
<tr>
<td>12</td>
<td>2</td>
<td>6</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>ฐานข้อมูลสินค้าแบบ Variations product ประกอบด้วยตาราง Product, Variation option, Variation value, Product variation, และ Product variation option โดยมีความสัมพันธ์กันดังนี้<br><br></p>
<ul>
<li>ตาราง Product เก็บข้อมูลสินค้าหลัก โดยมี primary key เป็น product_id และเป็นตารางหลักของฐานข้อมูล</li>
<li>ตาราง Variation option เก็บข้อมูลตัวเลือกของสินค้า เช่น ไซส์, สี โดยมี primary key เป็น variation_option_id</li>
<li>ตาราง Variation value เก็บข้อมูลค่าตัวเลือกของสินค้า เช่น S, M, L หรือสีแดง, สีเขียว โดยมี primary key เป็น variation_value_id และ foreign key เชื่อมต่อกับตาราง Variation option</li>
<li>ตาราง Product variation เก็บข้อมูลสินค้าแต่ละรายการที่มีตัวเลือก โดยมี primary key เป็น product_variation_id และ foreign key เชื่อมต่อกับตาราง Product และ Variation value</li>
<li>ตาราง Product variation option เก็บข้อมูลตัวเลือกของสินค้าสำหรับแต่ละรายการ โดยมี primary key เป็น (product_variation_id, variation_option_id) และ foreign key เชื่อมต่อกับตาราง Product variation และ Variation value<br><br></li>
</ul>
<p>ดังนั้น การออกแบบฐานข้อมูลสินค้าแบบ Variations product จะเน้นการเชื่อมโยงข้อมูลระหว่างตาราง โดยมีการใช้ primary key และ foreign key เพื่อให้สามารถเชื่อมข้อมูลระหว่างตารางได้อย่างถูกต้องและมีประสิทธิภาพในการจัดเก็บและการดึงข้อมูลสินค้าต่าง ๆ ที่มีตัวเลือกอยู่ในตัวแต่ละรายการได้อย่างมีประสิทธิภาพและเร็วด้วย SQL Query ที่เหมาะสม<br><br></p>
<div class="group w-full text-gray-800 dark:text-gray-100 border-b border-black/10 dark:border-gray-900/50 dark:bg-gray-800">
<div class="text-base gap-4 md:gap-6 md:max-w-2xl lg:max-w-xl xl:max-w-3xl p-4 md:py-6 flex lg:px-0 m-auto">
<div class="relative flex w-[calc(100%-50px)] flex-col gap-1 md:gap-3 lg:w-[calc(100%-115px)]">
<div class="flex flex-grow flex-col gap-3">
<div class="min-h-[20px] flex flex-col items-start gap-4 whitespace-pre-wrap">SQL ฐานข้อมูลสินค้าแบบ Variations product</div>
</div>
</div>
</div>
</div>
<div class="group w-full text-gray-800 dark:text-gray-100 border-b border-black/10 dark:border-gray-900/50 bg-gray-50 dark:bg-[#444654]">
<div class="text-base gap-4 md:gap-6 md:max-w-2xl lg:max-w-xl xl:max-w-3xl p-4 md:py-6 flex lg:px-0 m-auto">
<div class="relative flex w-[calc(100%-50px)] flex-col gap-1 md:gap-3 lg:w-[calc(100%-115px)]">
<div class="flex flex-grow flex-col gap-3">
<div class="min-h-[20px] flex flex-col items-start gap-4 whitespace-pre-wrap">
<div class="markdown prose w-full break-words dark:prose-invert light">
<p>SQL สำหรับฐานข้อมูลสินค้าแบบ Variations product สามารถเขียนได้ตามนี้</p>
<p>สร้างตารางสำหรับเก็บข้อมูลสินค้า (Product)<br><br></p>
<div class="bg-black rounded-md mb-4">
<div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md">&nbsp;</div>
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> Product (
product_id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY</span> KEY,
product_name <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">255</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,
product_description TEXT,
product_image <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">255</span>),
price <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>, <span class="hljs-number">2</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,
stock <span class="hljs-type">INT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>
);
</code></div>
</div>
<p>สร้างตารางสำหรับเก็บข้อมูลตัวเลือกของสินค้า (Variation option)<br><br></p>
<div class="bg-black rounded-md mb-4">
<div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md">&nbsp;</div>
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> Variation_option (
variation_option_id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY</span> KEY,
variation_option_name <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">255</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>
);<br><br>
</code></div>
</div>
<p>สร้างตารางสำหรับเก็บข้อมูลค่าตัวเลือกของสินค้า (Variation value)<br><br></p>
<div class="bg-black rounded-md mb-4">
<div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md">&nbsp;</div>
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> Variation_value (
variation_value_id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY</span> KEY,
variation_value_name <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">255</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,
variation_option_id <span class="hljs-type">INT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,
<span class="hljs-keyword">FOREIGN</span> KEY (variation_option_id) <span class="hljs-keyword">REFERENCES</span> Variation_option(variation_option_id)
);<br><br>
</code></div>
</div>
<p>สร้างตารางสำหรับเก็บข้อมูลสินค้าแต่ละรายการพร้อมกับค่าตัวเลือก (Product variation)<br><br></p>
<div class="bg-black rounded-md mb-4">
<div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md">&nbsp;</div>
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> Product_variation (
product_variation_id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY</span> KEY,
product_id <span class="hljs-type">INT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,
variation_value_id <span class="hljs-type">INT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,
price <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>, <span class="hljs-number">2</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,
stock <span class="hljs-type">INT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,
<span class="hljs-keyword">FOREIGN</span> KEY (product_id) <span class="hljs-keyword">REFERENCES</span> Product(product_id),
<span class="hljs-keyword">FOREIGN</span> KEY (variation_value_id) <span class="hljs-keyword">REFERENCES</span> Variation_value(variation_value_id)
);<br><br>
</code></div>
</div>
<p>สร้างตารางสำหรับเก็บข้อมูลตัวเลือกของสินค้าสำหรับแต่ละรายการ (Product variation option)<br><br></p>
<div class="bg-black rounded-md mb-4">
<div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md">&nbsp;</div>
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> Product_variation_option (
product_variation_id <span class="hljs-type">INT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,
variation_option_id <span class="hljs-type">INT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,
variation_value_id <span class="hljs-type">INT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,
<span class="hljs-keyword">PRIMARY</span> KEY (product_variation_id, variation_option_id, variation_value_id),
<span class="hljs-keyword">FOREIGN</span> KEY (product_variation_id) <span class="hljs-keyword">REFERENCES</span> Product_variation(product_variation_id),
<span class="hljs-keyword">FOREIGN</span> KEY (variation_option_id) <span class="hljs-keyword">REFERENCES</span> Variation_option(variation_option_id),
<span class="hljs-keyword">FOREIGN</span> KEY (variation_value_id) <span class="hljs-keyword">REFERENCES</span> Variation_value(variation_value_id)
);
</code></div>
<div class="p-4 overflow-y-auto">
<div class="bg-black rounded-md mb-4">&nbsp;</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<p>&nbsp;</p>
<p>เพิ่มข้อมูลเข้าไปในตารางสินค้าแบบ Variations product สามารถทำได้ด้วยคำสั่ง INSERT INTO ตามตัวอย่างนี้</p>
<p>เพิ่มข้อมูลสินค้าหลัก (Product)</p>
<div class="bg-black rounded-md mb-4">
<div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md">&nbsp;</div>
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> Product (product_id, product_name, product_description, product_image, price, stock)<br>
<span class="hljs-keyword">VALUES</span> (<span class="hljs-number">1</span>, <span class="hljs-string">'Product A'</span>, <span class="hljs-string">'Description A'</span>, <span class="hljs-string">'image1.jpg'</span>, <span class="hljs-number">100</span>, <span class="hljs-number">10</span>),
<br> (<span class="hljs-number">2</span>, <span class="hljs-string">'Product B'</span>, <span class="hljs-string">'Description B'</span>, <span class="hljs-string">'image2.jpg'</span>, <span class="hljs-number">200</span>, <span class="hljs-number">5</span>);
</code></div>
</div>
<p>เพิ่มข้อมูลตัวเลือกของสินค้า (Variation option)</p>
<div class="bg-black rounded-md mb-4">
<div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md">&nbsp;</div>
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> Variation_option (variation_option_id, variation_option_name)
<br><span class="hljs-keyword">VALUES</span> (<span class="hljs-number">1</span>, <span class="hljs-string">'Size'</span>), (<span class="hljs-number">2</span>, <span class="hljs-string">'Color'</span>);
</code></div>
</div>
<p>เพิ่มข้อมูลค่าตัวเลือกของสินค้า (Variation value)</p>
<div class="bg-black rounded-md mb-4">
<div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md">&nbsp;</div>
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> Variation_value (variation_value_id, variation_value_name, variation_option_id)
<br><span class="hljs-keyword">VALUES</span> (<span class="hljs-number">1</span>, <span class="hljs-string">'S'</span>, <span class="hljs-number">1</span>), (<span class="hljs-number">2</span>, <span class="hljs-string">'M'</span>, <span class="hljs-number">1</span>), (<span class="hljs-number">3</span>, <span class="hljs-string">'L'</span>, <span class="hljs-number">1</span>), <br>(<span class="hljs-number">4</span>, <span class="hljs-string">'Red'</span>, <span class="hljs-number">2</span>), (<span class="hljs-number">5</span>, <span class="hljs-string">'Green'</span>, <span class="hljs-number">2</span>), (<span class="hljs-number">6</span>, <span class="hljs-string">'Blue'</span>, <span class="hljs-number">2</span>);
</code></div>
</div>
<p>เพิ่มข้อมูลสินค้าแต่ละรายการพร้อมกับค่าตัวเลือก (Product variation)</p>
<div class="bg-black rounded-md mb-4">
<div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md">&nbsp;</div>
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-scss">INSERT INTO Product_variation (product_variation_id, product_id, variation_value_id, price, stock)<br>
VALUES (<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">100</span>, <span class="hljs-number">3</span>), (<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">110</span>, <span class="hljs-number">4</span>), (<span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">120</span>, <span class="hljs-number">3</span>), <br>(<span class="hljs-number">4</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">100</span>, <span class="hljs-number">2</span>), (<span class="hljs-number">5</span>, <span class="hljs-number">1</span>, <span class="hljs-number">5</span>, <span class="hljs-number">100</span>, <span class="hljs-number">3</span>), (<span class="hljs-number">6</span>, <span class="hljs-number">1</span>, <span class="hljs-number">6</span>, <span class="hljs-number">100</span>, <span class="hljs-number">4</span>),
(<span class="hljs-number">7</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">200</span>, <span class="hljs-number">2</span>), <br>(<span class="hljs-number">8</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">220</span>, <span class="hljs-number">3</span>), (<span class="hljs-number">9</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">230</span>, <span class="hljs-number">1</span>), (<span class="hljs-number">10</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">200</span>, <span class="hljs-number">0</span>), (<span class="hljs-number">11</span>, <span class="hljs-number">2</span>, <span class="hljs-number">5</span>, <span class="hljs-number">210</span>, <span class="hljs-number">2</span>), <br>(<span class="hljs-number">12</span>, <span class="hljs-number">2</span>, <span class="hljs-number">6</span>, <span class="hljs-number">210</span>, <span class="hljs-number">1</span>);
</code></div>
</div>
<p>เพิ่มข้อมูลตัวเลือกของสินค้าสำหรับแต่ละรายการ (Product variation option)<br><br></p>
<div class="bg-black rounded-md mb-4">
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-scss">INSERT INTO Product_variation_option (product_variation_id, variation_option_id, variation_value_id)<br>
VALUES (<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>), (<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>), (<span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>), (<span class="hljs-number">4</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>), (<span class="hljs-number">5</span>, <span class="hljs-number">2</span>, <span class="hljs-number">5</span>), <br>(<span class="hljs-number">6</span>, <span class="hljs-number">2</span>, <span class="hljs-number">6</span>), (<span class="hljs-number">7</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>), (<span class="hljs-number">8</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>), (<span class="hljs-number">9</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>), (<span class="hljs-number">10</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>), <br>(<span class="hljs-number">11</span>, <span class="hljs-number">2</span>, <span class="hljs-number">5</span>), (<span class="hljs-number">12</span>, <span class="hljs-number">2</span>, <span class="hljs-number">6</span>);</code></div>
</div>
<p><br>ฐานข้อมูลสินค้าแบบ Variations product มีความสัมพันธ์แบบ One-to-Many หรือแบบหนึ่งต่อหลาย ซึ่งหมายความว่าแต่ละสินค้าสามารถมีได้หลายตัวเลือก (Variation) โดยแต่ละตัวเลือก (Variation) สามารถมีค่าตัวเลือก (Value) ได้หลายค่า</p>
<p>โดยเริ่มต้นจาก<strong>ตาราง Product </strong>ซึ่งมี primary key เป็น product_id และมีความสัมพันธ์กับตาราง Product_variation โดยเชื่อมโยงด้วย foreign key คือ product_id ซึ่งหมายความว่าแต่ละสินค้าสามารถมีได้หลายรายการ (Variation) ซึ่งอยู่ในตาราง Product_variation</p>
<p><strong>ตาราง Product_variation</strong> จะมี primary key เป็น product_variation_id และมีความสัมพันธ์กับตาราง Variation_value โดยเชื่อมโยงด้วย foreign key คือ variation_value_id ซึ่งหมายความว่าแต่ละรายการสินค้าแต่ละตัวเลือกจะมีค่าตัวเลือก (Value) เดียวหรือมากกว่าได้</p>
<p><strong>ตาราง Variation_value</strong> จะมี primary key เป็น variation_value_id และมีความสัมพันธ์กับตาราง Variation_option โดยเชื่อมโยงด้วย foreign key คือ variation_option_id ซึ่งหมายความว่าแต่ละค่าตัวเลือก (Value) จะเป็นค่าของตัวเลือก (Option) เดียวหรือมากกว่าได้</p>
<p>สุดท้าย<strong>ตาราง Product_variation_option</strong> จะมี primary key เป็น product_variation_id, variation_option_id และ variation_value_id ซึ่งหมายความว่าแต่ละรายการสินค้าแต่ละตัวเลือกจะมีค่าตัวเลือก (Value) ของตัวเลือก (Option) เดียวหรือมากกว่าได้</p>
<p>&nbsp;</p>
<p><strong>ออกแบบฐานข้อมูล discount table&nbsp;</strong></p>
<p>การออกแบบตาราง discount_types สำหรับเก็บข้อมูลประเภทส่วนลดสามารถทำได้ดังนี้</p>
<p>&nbsp;</p>
<p><strong>ออกแบบฐานข้อมูล table Order สำหรับสินค้าแบบ Variations product</strong></p>
<p>สำหรับการออกแบบฐานข้อมูล table Order สำหรับสินค้าแบบ Variations product นั้น จะต้องมีการเก็บข้อมูลต่างๆ ที่เกี่ยวข้องกับสินค้าและการสั่งซื้อ เช่น รายละเอียดสินค้าแต่ละตัวที่มีความแตกต่างกันเช่น สี ขนาด รุ่น หรือโมเดล ราคา รวมถึงข้อมูลที่เกี่ยวข้องกับการสั่งซื้อเช่น วันที่สั่งซื้อ ชื่อลูกค้า ที่อยู่จัดส่ง ราคารวม และสถานะการสั่งซื้อ เป็นต้น</p>
<p>ดังนั้น ออกแบบฐานข้อมูล table Order สำหรับสินค้าแบบ Variations product สามารถออกแบบได้ดังนี้</p>
<p>Table: Orders</p>
<table>
<thead>
<tr>
<th>Field Name</th>
<th>Data Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>order_id</td>
<td>INT</td>
<td>รหัสการสั่งซื้อ</td>
</tr>
<tr>
<td>customer_name</td>
<td>VARCHAR</td>
<td>ชื่อลูกค้า</td>
</tr>
<tr>
<td>customer_address</td>
<td>VARCHAR</td>
<td>ที่อยู่จัดส่ง</td>
</tr>
<tr>
<td>total_price</td>
<td>DECIMAL(10,2)</td>
<td>ราคารวม</td>
</tr>
<tr>
<td>order_date</td>
<td>DATE</td>
<td>วันที่สั่งซื้อ</td>
</tr>
<tr>
<td>status</td>
<td>VARCHAR</td>
<td>สถานะการสั่งซื้อ (เช่น ยังไม่ชำระเงิน, กำลังจัดส่ง, จัดส่งแล้ว)</td>
</tr>
</tbody>
</table>
<p>Table: Order_Details</p>
<table>
<thead>
<tr>
<th>Field Name</th>
<th>Data Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>order_detail_id</td>
<td>INT</td>
<td>รหัสรายละเอียดการสั่งซื้อ</td>
</tr>
<tr>
<td>order_id</td>
<td>INT</td>
<td>รหัสการสั่งซื้อ</td>
</tr>
<tr>
<td>product_id</td>
<td>INT</td>
<td>รหัสสินค้า</td>
</tr>
<tr>
<td>variation_id</td>
<td>INT</td>
<td>รหัส variation</td>
</tr>
<tr>
<td>quantity</td>
<td>INT</td>
<td>จำนวน</td>
</tr>
<tr>
<td>price</td>
<td>DECIMAL(10,2)</td>
<td>ราคาต่อหน่วย</td>
</tr>
<tr>
<td>subtotal</td>
<td>DECIMAL(10,2)</td>
<td>ราคารวม</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>สำหรับฐานข้อมูล Table: Orders นี้จะเก็บข้อมูลเกี่ยวกับรายการสั่งซื้อของลูกค้า ดังนี้</p>
<table>
<thead>
<tr>
<th>Field Name</th>
<th>Data Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>order_id</td>
<td>INT</td>
<td>รหัสการสั่งซื้อ</td>
</tr>
<tr>
<td>customer_name</td>
<td>VARCHAR</td>
<td>ชื่อลูกค้า</td>
</tr>
<tr>
<td>customer_address</td>
<td>VARCHAR</td>
<td>ที่อยู่จัดส่ง</td>
</tr>
<tr>
<td>total_price</td>
<td>DECIMAL(10,2)</td>
<td>ราคารวม</td>
</tr>
<tr>
<td>order_date</td>
<td>DATE</td>
<td>วันที่สั่งซื้อ</td>
</tr>
<tr>
<td>status</td>
<td>VARCHAR</td>
<td>สถานะการสั่งซื้อ (เช่น ยังไม่ชำระเงิน, กำลังจัดส่ง, จัดส่งแล้ว)</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>สำหรับฐานข้อมูล Table: Order_Details นี้จะเก็บข้อมูลเกี่ยวกับรายละเอียดของสินค้าในแต่ละรายการสั่งซื้อ ดังนี้</p>
<table>
<thead>
<tr>
<th>Field Name</th>
<th>Data Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>order_detail_id</td>
<td>INT</td>
<td>รหัสรายละเอียดการสั่งซื้อ</td>
</tr>
<tr>
<td>order_id</td>
<td>INT</td>
<td>รหัสการสั่งซื้อ</td>
</tr>
<tr>
<td>product_id</td>
<td>INT</td>
<td>รหัสสินค้า</td>
</tr>
<tr>
<td>variation_id</td>
<td>INT</td>
<td>รหัส variation</td>
</tr>
<tr>
<td>quantity</td>
<td>INT</td>
<td>จำนวน</td>
</tr>
<tr>
<td>price</td>
<td>DECIMAL(10,2)</td>
<td>ราคาต่อหน่วย</td>
</tr>
<tr>
<td>subtotal</td>
<td>DECIMAL(10,2)</td>
<td>ราคารวม</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p><strong>สร้าง Table ชื่อ SQL Orders </strong>หรือเป็นการใช้ SQL เพื่อจัดการกับ Table Orders ที่มีอยู่แล้ว ดังนั้นผมจะแสดงตัวอย่างทั้งสองกรณีด้านล่าง</p>
<ol>
<li>สร้าง Table&nbsp; &nbsp;Orders ด้วยคำสั่ง SQL</li>
</ol>
<div class="bg-black rounded-md mb-4">
<div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md">&nbsp;</div>
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> Orders (
order_id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY</span> KEY,
customer_name <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">50</span>),
customer_address <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>),
total_price <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>,<span class="hljs-number">2</span>),
order_date <span class="hljs-type">DATE</span>,
status <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">50</span>)
);
</code></div>
</div>
<ol start="2">
<li>แทรกข้อมูลลงใน Table Orders ด้วยคำสั่ง SQL</li>
</ol>
<div class="bg-black rounded-md mb-4">
<div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md">&nbsp;</div>
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> Orders (order_id, customer_name, customer_address, total_price, order_date, status)
<span class="hljs-keyword">VALUES</span> (<span class="hljs-number">1</span>, <span class="hljs-string">'John Smith'</span>, <span class="hljs-string">'123 Main St, Anytown USA'</span>, <span class="hljs-number">99.99</span>, <span class="hljs-string">'2023-03-31'</span>, <span class="hljs-string">'Pending'</span>);</code></div>
</div>
<p>&nbsp;</p>
<p><strong>SQL ตาราง&nbsp; Order_Details</strong> เป็นตารางที่ใช้เก็บข้อมูลรายละเอียดสินค้าในแต่ละรายการสั่งซื้อ ดังนั้นเราจะออกแบบตารางดังนี้</p>
<ol>
<li>สร้าง SQL Table&nbsp; <strong>Order_Details</strong> ด้วยคำสั่ง SQL</li>
</ol>
<div class="bg-black rounded-md mb-4">
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> Order_Details (
order_detail_id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY</span> KEY,
order_id <span class="hljs-type">INT</span>,
product_id <span class="hljs-type">INT</span>,
variation_id <span class="hljs-type">INT</span>,
quantity <span class="hljs-type">INT</span>,
price <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>,<span class="hljs-number">2</span>),
subtotal <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>,<span class="hljs-number">2</span>),
<span class="hljs-keyword">FOREIGN</span> KEY (order_id) <span class="hljs-keyword">REFERENCES</span> SQL_Orders(order_id)
);</code></div>
<div class="p-4 overflow-y-auto">
<p>เมื่อเรามีตาราง Order_Details แล้ว เราสามารถเพิ่มข้อมูลลงในตารางด้วยคำสั่ง INSERT INTO ได้ดังนี้<br><br>&nbsp; &nbsp; &nbsp; 2.แทรกข้อมูลลงใน Table Order_Details ด้วยคำสั่ง SQL</p>
<div class="bg-black rounded-md mb-4">
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> Order_Details (order_detail_id, order_id, product_id, variation_id, quantity, price, subtotal)
<span class="hljs-keyword">VALUES</span> (<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">101</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">49.99</span>, <span class="hljs-number">99.98</span>);</code></div>
</div>
</div>
</div>
Content Short
Content View3252
Content Thumb Highlight
Content Thumb/uploads/article/HiqXwKNYN3.png
Content Img Altออกฐานข้อมูลสินค้าแบบ Variations product แบบที่ 2
Content Tag
Content Date(not set)
Active Status1
Sort99999
Meta Titleออกฐานข้อมูลสินค้าแบบ Variations product แบบที่ 2
Meta Descออกฐานข้อมูลสินค้าแบบ Variations product แบบที่ 2
Meta Keywordออกฐานข้อมูลสินค้าแบบ Variations product แบบที่ 2
Og Titleออกฐานข้อมูลสินค้าแบบ Variations product แบบที่ 2
Og Descออกฐานข้อมูลสินค้าแบบ Variations product แบบที่ 2
Status1
Create Date Time2023-03-31 12:51:16
Update Date Time2023-03-31 19:59:59
Create By
Update By