MySQL: Tables for user types

1

I need to implement a login, but I have several types of Users, so to know what kind of users they are, I must make 3 queries (because they are 3 types of users) since I have a Users table to which the 3 tables ( Professor, student and attorney) are related to a foreing key, my question is how can I do it with the user's name? Know what type of user is with a single query, thanks

    
asked by Joseph Leon 15.03.2017 в 22:55
source

3 answers

2

I would make three tables:

  • Users
  • Types
  • Users_Types
  • The third table would be an auxiliary table in which you can relate the users to the types, in this way in a single query you could find all the users, their names, of the type that they are and more data that you need so much of the table users as of the table types (see Query 1 below).

    I leave you this basic design. Then you can create the indexes, the primary keys, control the records that will be unique, etc. It's just to show how it would work.

    SQL Fiddle

    MySQL 5.6 Schema Setup :

    CREATE TABLE usuario
        ('usuario_id' int, 'usuario_nom' varchar(70), 'usuario_ape' varchar(70))
    ;
    
    
    CREATE TABLE tipo
        ('tipo_id' int, 'tipo_nom' varchar(70));
    
    
    CREATE TABLE usuario_tipo
        ('usuario_id' int, 'tipo_id' int);
    
    INSERT INTO usuario ('usuario_id', 'usuario_nom')
      VALUES
    (1,"Pedro"),
    (2,"Santiago"),
    (3,"Juan"),
    (4,"Andrés");
    
    
    
    INSERT INTO tipo
        ('tipo_id', 'tipo_nom')
    VALUES
        (1, "Profesor"),
        (2, "Alumno"),
        (3, "Apoderado");
    
    INSERT INTO usuario_tipo
        ('usuario_id', 'tipo_id')
    VALUES
        (1, 2),
        (3, 1),
        (2, 2),
        (4, 3);
    

    Query 1 :

    SELECT u.usuario_nom, t.tipo_nom
      FROM usuario u
      LEFT JOIN usuario_tipo ut 
          ON u.usuario_id = ut.usuario_id
      LEFT JOIN tipo t 
          ON t.tipo_id = ut.tipo_id     
    

    Results :

    | usuario_nom |  tipo_nom |
    |-------------|-----------|
    |        Juan |  Profesor |
    |       Pedro |    Alumno |
    |    Santiago |    Alumno |
    |      Andrés | Apoderado |
    
        
    answered by 15.03.2017 в 23:44
    1

    Ideally, your user table should contain the type of user that is to achieve the desired effect, the answer given by A. Cedano is ideal if you are allowed to change your model of the database, but I will assume more information that you want to infer from the relationship with the other tables, so it would be something like this without knowing the scheme of your tables but you can adapt it according to your need:

    select *
    from (
    select 'PROFESOR' as TIPOUSUARIO, p.nombre, u.usuario from profesor as p join usuarios as u on p.id = u.id
    union all
    select 'ALUMNO' as TIPOUSUARIO, p.nombre, u.usuario from alumno as p join usuarios as u on p.id = u.id
    union all
    select 'APODERADO' as TIPOUSUARIO, p.nombre, u.usuario from apoderado as p join usuarios as u on p.id = u.id
      ) as unionTablas
     where usuario = 'a';
    

    Link sqlfiddle: link

        
    answered by 15.03.2017 в 23:29
    1

    I would create only one Users table with the necessary fields, and an extra field that would be typeUser of type ENUM.

    CREATE TABLE 'Usuarios' ( 'id' INT NOT NULL AUTO_INCREMENT , 'nombre' VARCHAR(50) NOT NULL , 'tipoUser' ENUM('Profesor','Alumno','Apoderado') NOT NULL , PRIMARY KEY ('id')) ENGINE = InnoDB;
    

    Then at the time of making a query to pick up all the teachers you just have to make the following query:

    SELECT * FROM Usuarios WHERE tipoUser='Profesor';
    

    It's the easiest and most efficient thing there is. So you get rid of creating unnecessary tables, the only thing they do is give you more work at the time of making a query, because you have to go relating the Foreign Keys, etc.

    I think my answer will only be useful if only those three types of users are going to exist and they will not exist anymore.

        
    answered by 16.03.2017 в 10:49