WOW! This is interesting. How would INSERT INTO
be executed?
I donât understand the question.
You would insert into those tables exactly as above.
Yes, but the Query for doing that, I am confused.
Is there a possibility for you to show in my sqlfiddle link!
How did you generate the INSERT queries for the fiddle? Do the same thing, but with the changed structure.
I will TRY! Iâll LET YOU KNOW!
CREATE TABLE `items`(
`item_id` BIGINT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(255) NOT NULL,
`date_added` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE `categories`(
`category_id` BIGINT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(255) NOT NULL,
`parent` BIGINT(10) UNSIGNED DEFAULT NULL
);
CREATE TABLE `item_category`(
`item_id` BIGINT(10) UNSIGNED NOT NULL,
`category_id` BIGINT(10) UNSIGNED NOT NULL,
FOREIGN KEY (`item_id`) REFERENCES `items`(`item_id`),
FOREIGN KEY (`category_id`) REFERENCES `categories`(`category_id`)
);
INSERT INTO `items`(`name`) VALUES('Pepsi');
INSERT INTO `items`(`name`) VALUES('Pepsi MAX');
INSERT INTO `categories`(`name`) VALUES('Pepsi');
INSERT INTO `categories`(`name`, `parent`) VALUES(`Pepsi Zero`, 1);
INSERT INTO `item_category` (`item_id`, `category_id`) VALUES
(1, 1);
INSERT INTO `item_category` (`item_id`, `category_id`) VALUES
(2, 2);
Does NOT work in the sqlfiddle.
why have you put backticks around the words Pepsi Zero?
Backticks are for column/table/database references. Quote marks (single or double) are for strings.
OOPS!
SELECT
i.*,
c.name
FROM items i
INNER JOIN item_category ic ON i.item_id = ic.item_id
INNER JOIN categories c ON ic.category_id = c.category_i
How do I determine if item is coming from Parent or Child inside parent?
Select c.parent as well. If c.parent is Null, then the category is a base category. If itâs not, then itâs a subcategory.
So, CASE IF c.parent == NULL, right?
I wouldnt use the database engine to make the evaluation; If you only want base categories, add a âWHERE c.parent == NULLâ to your query. If you only want subcategories, add the not-equals to version. If youâre going to use both in your page, pull all categories and then use your scripting language to divvy up the results.
Actually,
https://dev.mysql.com/doc/refman/8.0/en/working-with-null.html
3.3.4.6 Working with NULL Values
The
NULL
value can be surprising until you get used to it. Conceptually,NULL
means âa missing unknown valueâ and it is treated somewhat differently from other values.To test for
NULL
, use theIS NULL
andIS NOT NULL
operators, as shown here:
mysql> SELECT 1 IS NULL, 1 IS NOT NULL;
+-----------+---------------+
| 1 IS NULL | 1 IS NOT NULL |
+-----------+---------------+
| 0 | 1 |
+-----------+---------------+
Absolutely correct. My brain jumped ahead into scripting language and even declared it with a double-equals
Can I use -1 instead?
you could, but it will seriously mess you up
Why?
it is possible to do this, but i have yet to meet the person who faithfully implements it
itâs 6th normal form, or something
you end up with hunnerts of little tables (1 or 2 columns only)
try it â the best teacher is experience
âdonât touch the hot stoveâ
âwhy?â
âit will seriously mess you upâ
âwhy?â
âgo ahead, try itâ
âOUCHIES!!â
âhere, have a tissue and some aloeâ
So what would be a preferable default value?