Warm tip: This article is reproduced from serverfault.com, please click

MYSQL filled some rows, but was't suppost to

发布于 2020-11-28 00:18:35

So I made a basic struct of a database, and mysql filled some rows on a table and I don't know why. Maybe I can explain myself with the tables

mysql> show fields
    -> from products;
+--------------------+--------------+------+-----+---------+-------+
| Field              | Type         | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| ID_PRODUCT         | decimal(8,0) | NO   | PRI | NULL    |       |
| ID_CATEGORY        | decimal(8,0) | NO   | MUL | NULL    |       |
| NAME               | text         | NO   |     | NULL    |       |
| BRAND              | text         | NO   |     | NULL    |       |
| PICTURE            | longblob     | YES  |     | NULL    |       |
| OBSERVATIONS       | text         | YES  |     | NULL    |       |
| QUANT_UNIT         | text         | NO   |     | NULL    |       |
+--------------------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec) 

So this is a table with products, and I have one other table with the prices so I can save the history of prices

mysql> show fields from prices;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| ID_PRODUCT | decimal(8,0) | NO   | MUL | NULL    |       |
| PRICE      | text         | NO   |     | NULL    |       |
| TIME       | timestamp    | NO   |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

And if I insert the values into the product and prices tables

mysql> insert into products (id_product, id_category, name, brand, quant_unit)
    -> values (1, 0, 'rice', 'XPTO', 'kg');
Query OK, 1 row affected (0.01 sec)

mysql> select * from products;
+------------+--------------+--------+----------+----------------+-------------+--------------------+
| ID_PRODUCT | ID_CATEGORY  | NAME   | BRAND    | PICTURE        | OBSERVATION | QUANT_UNIT         |
+------------+--------------+--------+----------+----------------+-------------+--------------------+
|          0 |            0 | mug    | no_brand | 0x             | favorite    | un                 |
|          1 |            0 | rice   | xpto     | 0x             | NULL        | kg                 |
+------------+--------------+--------+----------+----------------+-------------+--------------------+
2 rows in set (0.00 sec)
mysql> insert into prices (id_product, price, time)
    -> values (0, '5', CURRENT_TIMESTAMP);
Query OK, 1 row affected (0.01 sec)

mysql> select * from prices;
+------------+-------+---------------------+
| ID_PRODUCT | PRICE | TIME                |
+------------+-------+---------------------+
|          0 | 7.30  | 2020-11-27 23:25:34 |
|          1 | 5     | 2020-11-27 23:29:12 |
+------------+-------+---------------------+
2 rows in set (0.00 sec)

all fine so far, but if I do a select with both of the tables

mysql> select pro.id_product, pro.id_category, pro.name, pro.brand, pro.quant_unit, pre.price
    -> from products pro, prices pri;
+------------+--------------+--------+----------+-------------+-------+
| id_product | id_category  | name   | brand    | quant_unit  | price |
+------------+--------------+--------+----------+-------------+-------+
|          0 |            0 | mug    | no_brand | un          | 7.30  |
|          1 |            0 | rice   | xpto     | kg          | 7.30  |
|          0 |            0 | mug    | no_brand | un          | 5     |
|          1 |            0 | rice   | xpto     | kg          | 5     |
+------------+--------------+--------+----------+-------------+-------+
4 rows in set (0.00 sec)

those are my constraints to connect the tables

alter table PRICES add constraint FK_PRICES_COST_PRODUCTS foreign key (ID_PRODUCT)
      references PRODUCTS (ID_PRODUCT) on delete restrict on update restrict;

alter table PRODUCTS add constraint FK_PRODUCTS_PERTENCE_CATEGORY foreign key (ID_CATEGORY)
      references CATEGORY (ID_CATEGORY) on delete restrict on update restrict;

I can't even search the problem because it doesn't make sense to me... But I'm new at SQL, so I think I'm doing something wrong...

I will appreciate any feedback, thanks!

Questioner
Nélio Marcelino
Viewed
0
FaNo_FN 2020-11-28 08:35:44

I advise against using comma-join but in your query you didn't specify what to match between those two tables. You can simply fix that by adding WHERE in your query like this:

select pro.id_product, pro.id_category, pro.name, pro.brand, pro.quant_unit, pre.price
from products pro, prices pri 
where pro.id_product=pri.id_product;

But nowadays, most people uses JOIN instead of comma-join, hence:

select pro.id_product, pro.id_category, pro.name, pro.brand, pro.quant_unit, pre.price
from products pro JOIN prices pri 
ON pro.id_product=pri.id_product;

There's a lot of JOIN types in MySQL and you can refer to this documentation.