How do I make two references of a table in other 2 tables?

Asked

Viewed 58 times

0

Well, I have 3 tables

  1. tbl_users
  2. tbl_article
  3. tbl_profile

I need tbl_usuarios to be related to tbl_articles and tbl_perfil. How could it be done?

Table tbl_usuarios:

+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| usuario_id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| usuario_usuario | varchar(30)  | NO   |     | NULL    |                |
| usuario_email   | text         | NO   |     | NULL    |                |
| usuario_senha   | text         | NO   |     | NULL    |                |
| usuario_imagem  | varchar(100) | YES  |     | NULL    |                |
| usuario_status  | int(1)       | NO   |     | NULL    |                |
| id_perfil       | int(11)      | YES  | MUL | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+

Table tbl_article

+---------------+--------------+------+-----+-------------------+----------------+
| Field         | Type         | Null | Key | Default           | Extra          |
+---------------+--------------+------+-----+-------------------+----------------+
| artigo_id     | int(11)      | NO   | PRI | NULL              | auto_increment |
| artigo_titulo | varchar(100) | NO   |     | NULL              |                |
| artigo_imagem | varchar(80)  | YES  |     | NULL              |                |
| artigo_autor  | varchar(45)  | NO   |     | NULL              |                |
| artigo_post   | text         | NO   |     | NULL              |                |
| artigo_data   | timestamp    | YES  |     | CURRENT_TIMESTAMP |                |
| artigo_status | int(1)       | NO   |     | NULL              |                |
| id_artigo     | int(11)      | YES  |     | NULL              |                |
+---------------+--------------+------+-----+-------------------+----------------+

Table tbl_profile:

+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| perfil_id        | int(11)      | NO   | PRI | NULL    | auto_increment |
| perfil_cargo     | varchar(100) | YES  |     | NULL    |                |
| perfil_telefone  | int(30)      | YES  |     | NULL    |                |
| perfil_descricao | text         | YES  |     | NULL    |                |
| id_perfil        | int(11)      | YES  |     | NULL    |                |
+------------------+--------------+------+-----+---------+----------------+

This will serve so that when the user writes his article I will get the image of the user that is in tbl_usuario and other information that are in another table

I need to know how I do it.

  • You can use $this->db->Join("a", "Cod=cod1");

  • I’ll assemble the query to show you!

  • What is the difference between the "tbl_profile" table and the "tbl_users" table? Can’t profile fields be in the users table? It would look better this way. Then just use a foreign key in the article_author field that references the user_id.

  • I get it, it’s supposed to be a good idea.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.