
This table definition specifies foreign key constraints for the brand_id and category_id fields, specifying that they reference the brands and categories table respectively.
Phpmyadmin foreign key update#
`categories ` ( `id ` ) ON DELETE RESTRICT ON UPDATE CASCADE ) `brands ` ( `id ` ) ON DELETE RESTRICT ON UPDATE CASCADE, CONSTRAINT `category_id ` FOREIGN KEY ( `category_id ` ) REFERENCES `e_store `. `products ` ( `id ` INT UNSIGNED NOT NULL AUTO_INCREMENT, `name ` VARCHAR ( 250 ) NOT NULL, `brand_id ` INT UNSIGNED NOT NULL, `category_id ` INT UNSIGNED NOT NULL, `attributes ` JSON NOT NULL, PRIMARY KEY ( `id ` ), INDEX `CATEGORY_ID ` ( `category_id ` ASC ), INDEX `BRAND_ID ` ( `brand_id ` ASC ), CONSTRAINT `brand_id ` FOREIGN KEY ( `brand_id ` ) REFERENCES `e_store `. Next, create a products table with the id, name, brand_id, category_id, and attributes fields: CREATE TABLE `e_store `. `categories ` ( `name ` ) VALUES ( 'Camera' ) `categories ` ( `name ` ) VALUES ( 'Mobile Phone' ) INSERT INTO `e_store `. `categories ` ( `name ` ) VALUES ( 'Television' ) INSERT INTO `e_store `. Then, add some categories: INSERT INTO `e_store `.

`brands ` ( `name ` ) VALUES ( 'Canon' ) `brands ` ( `name ` ) VALUES ( 'Nokia' ) INSERT INTO `e_store `. `brands ` ( `name ` ) VALUES ( 'Samsung' ) INSERT INTO `e_store `. Next, add some sample brands: INSERT INTO `e_store `. `categories ` ( `id ` INT UNSIGNED NOT NULL auto_increment, `name ` VARCHAR ( 250 ) NOT NULL, PRIMARY KEY ( `id ` ) ) `brands ` ( `id ` INT UNSIGNED NOT NULL auto_increment, `name ` VARCHAR ( 250 ) NOT NULL, PRIMARY KEY ( `id ` ) ) Ĭreate the categories table: CREATE TABLE `e_store `. The brands and categories tables will each have an id and a name field.Ĭreate the brands table: CREATE TABLE `e_store `. The database will be named e_store and have three tables named brands, categories, and products respectively.Ĭreate the e_store database: CREATE DATABASE IF NOT EXISTS `e_store ` DEFAULT CHARACTER SET utf8ĭEFAULT COLLATE utf8_general_ci SET default_storage_engine = INNODB However, with the JSON data type, this use case can be approached differently. Traditionally, the Entity–attribute–value model (EAV) pattern would be used to allow customers to compare the features of products. Step 1 - Defining the Schemaįor the purposes of this tutorial, you will be building from a schema that defines the inventory of an online store that sells a variety of electronics. This tutorial was verified with MySQL v8.0.23, PHP v7.3.24, Composer v2.0.9, and Laravel v8.26.1. This may be an alternative option if you are having difficulty setting up your local environment. Note: Laravel now provides a tool called Sail to work with Docker that will configure an environment with MySQL, PHP, and Composer. You can consult our tutorial on installing Composer. This tutorial utilizes Laravel installation via Composer in mind.You can consult our tutorials on installing Linux, Apache, MySQL, and PHP MySQL 5.7.8 or later and PHP 7.3.24 or later.If you would like to follow along with this article, you will need: You will build an admin panel that supports displaying products, adding new products, modifying existing products, and deleting products. The second half of this article will utilize the Eloquent ORM with Laravel to communicate with the database. It will step through using the built-in functions available to MySQL to create, read, update, and delete rows. The first half of this article will design a database with JSON fields. It allows you to structure some parts of your database and leave others to be flexible. The JSON data type in MySQL grants you the strengths of both of these systems. In these schema-less databases, there is no imposed structural restriction, only data to be saved. In comparison, NoSQL databases encourage flexibility in design. By its nature, the structured query language enforces data type and size constraints.


SQL databases tend to be rigid in design.

MySQL version 5.7.8 introduces a JSON data type that allows you to access data in JSON documents.
