I need to relate two tables, that the value of one column in table A be taken in another column in table B

0

I have a table that is products, and another table that is typeProducts, the structure of the first is basic: id, code, description, typeProduct, the table typeProducts only takes id and description, so I need the product table, in the type field, see the records stored in the types table.

Some suggestions, I have been reading about the 3FN database however I am still a novice and I am learning on the way.

    
asked by Adrian 12.08.2016 в 15:28
source

3 answers

1

You have two tables that are the following:

products

  • id
  • name
  • code
  • description
  • product_type_id

product_type

  • id
  • name
  • description

and you have related the two tables so that when executing a query it could be as follows

SELECT p.id AS Id, p.name AS NombreProducto, t.nombre AS tipoProducto  FROM productos p
INNER JOIN tipo_producto t ON p.id_tipo_producto = t.id

in this way the previous query will show you all the products next to what type of product it is.

// tabla de tipo de producto
CREATE TABLE 'tipo_producto' (
  'id' INT NOT NULL,
  'nombre' VARCHAR(45) NULL,
  'descripcion' VARCHAR(45) NULL,
  PRIMARY KEY ('id'));


// tabla de productos
CREATE TABLE 'productos' (
  'id' INT NOT NULL,
  'nombre' VARCHAR(45) NULL,
  'descripcion' VARCHAR(45) NULL,
  'id_tipo_producto' INT NULL,
  PRIMARY KEY ('id'),
  INDEX 'id_tipo_producto_idx' ('id_tipo_producto' ASC),
  CONSTRAINT 'id_tipo_producto'
    FOREIGN KEY ('id_tipo_producto')
    REFERENCES 'tipo_producto' ('id')
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);
    
answered by 12.08.2016 / 15:50
source
1

Relate to a foreign key the Product_type column of the table < em> Products that points to the id column of ProductType , this way when you see the productType of Products you will know what type of product is easily. In a diagram (or something similar) it would look like this:

Products:

Id Codigo  Descripcion                 TipoProducto
1  ABC123  Tennis Jordan blablabla...  1
2  DBA567  Vestido Louis Vuitton...    2
3  DAN082  Macbook Pro 15' retina...   2

TypeProducts

Id Descripcion
1  Moda y Accesorios
2  Computación

I hope it serves you.

    
answered by 12.08.2016 в 16:00
0

To relate two columns of two different tables, first define who is part of who (son-father) for example:

If we have drinks and we must specify the type, we know then that drinks have types, they would be two tables, one of drinks and one of types, example:

  • Drink: Orange Juice ---------------- Type: Juices
  • Drink: Fruit Cocktail ------------- Type: Cocktail

Then we proceed to create two indexes of type unique in each of the tables, both in the child table and in the parent table, and point to the column that they want to relate.

Finally, a foreign key is created in the child table that refers to the column of the parent table from where the information needs to be acquired.

That's how my query is:

//Tabla tipoproductos
CREATE TABLE 'tipoproductos' 
(
'idTipo' int(11) NOT NULL AUTO_INCREMENT,
'descripcionTipo' varchar(45) NOT NULL,
//Llaves o indexes
 PRIMARY KEY ('idTipo'),
 UNIQUE KEY 'tipoProd_idx' ('descripcionTipo')
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

CREATE TABLE 'productos' 
(
'idProducto' int(11) NOT NULL AUTO_INCREMENT,
'codProducto' varchar(45) NOT NULL,
'descripcionProducto' varchar(45) NOT NULL,
'tipoProducto' varchar(45) NOT NULL,
'existenciaProducto' int(11) DEFAULT NULL,
'costoProducto' int(11) DEFAULT NULL,
'precioProducto' int(11) DEFAULT NULL,

//Llaves o indexes
PRIMARY KEY ('idProducto'),
UNIQUE KEY 'tipoProd_idx' ('tipoProducto'),

//Aqui la tabla hijo se relaciona
CONSTRAINT 'tipoProd_FK'
FOREIGN KEY ('tipoProducto')

REFERENCES 'tipoproductos' ('descripcionTipo') 
 ON DELETE NO ACTION
 ON UPDATE NO ACTION
)ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

Thanks for your directions @Lucho T. you showed me the way to find the right answer.

    
answered by 12.08.2016 в 17:27