How to save multiple users to an account like Netflix for example

Asked

Viewed 703 times

4

Next, I have an application that supports only 1 account per user, I want to update the database on MySQL so that it supports a system that can be saved for example, child accounts, derived from that parent account, which is the main.

My application only supports 1 account by user as already stated, all user account information is stored in only one row.

Mysql

SQL |
    \-[Table] Users |
                    \-[rows] id  | UserID | Username | Resto...
                              1  |   845  |  João    |   ...
                              2  |   846  |  Mãria   |   ...
                             ... |   ...  |    ...   |   ...

In this example, we have John, he has 3 children, I should support the sub-accounts, but I don’t know how to do it. The accounts children need to have a UserID single other than parent accounts so that the system can process it based on UserID and thus process the other information, as well as exclude a son when necessary.


Children

... | UserID | Username | Resto das configurações pessoas de cada filho...
... |  888   | F.João 1 | ...
... |  777   | F.João 2 | ...
... |  222   | F.João 3 | ...
... | outros |  filhos  | de outros usuários ...

Is there any way to do that?

If possible, but no more important than the SQL, would like a example in php how to catch this son and delete it, based on the account father, which is the main.

This was necessary because users could not access on 2 different platforms with the same account. That’s why I need a Netflix-based system.

  • What you need is a "one for many" relationship. Create a new "profiles" table and link it to your "users" table".

  • I think I understand (in theory, the practice is always different), but since the question is open, it is better to wait for someone to answer. Thanks for the tip.

  • 1

    I’ll formulate an answer when you’re free. For now, try researching the relationship, I think it will help you take a few more steps.

  • 1

    You can also do it by creating a column userParent and when a user is created as dependent, just relate the userParent with the userid. Later when determined bond no longer exists between users, just delete the value of userParent.

2 answers

4


If daughter accounts log in

You can use n:m for a table to relate to itself, I cannot say that this is the best way, however this way you can have "infinite" levels (children, grandchildren, dependents, partner, wife, etc), an example with Mysql and innoDB:

n:m na mesma tabela

CREATE TABLE IF NOT EXISTS `mydb`.`usuarios` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `nome` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `mydb`.`usuarios_has_usuarios` (
  `usuarios_id` INT NOT NULL,
  `usuarios_id1` INT NOT NULL,
  PRIMARY KEY (`usuarios_id`, `usuarios_id1`),
  INDEX `fk_usuarios_has_usuarios_usuarios1_idx` (`usuarios_id1` ASC),
  INDEX `fk_usuarios_has_usuarios_usuarios_idx` (`usuarios_id` ASC),
  CONSTRAINT `fk_usuarios_has_usuarios_usuarios`
    FOREIGN KEY (`usuarios_id`)
    REFERENCES `mydb`.`usuarios` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_usuarios_has_usuarios_usuarios1`
    FOREIGN KEY (`usuarios_id1`)
    REFERENCES `mydb`.`usuarios` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

The advantage of using foreign key, is that if there are dependent users they will be "tied" to other users. Even if you try to remove the PAI user (assuming it is an accident) the table will not allow this. However you can yes use no foreign key, but if you delete or update something wrong, it will start displaying incoherent data.

An example without a foreign key would be to create an extra column in the users table:

CREATE TABLE IF NOT EXISTS `mydb`.`usuarios` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `parentId` INT NULL,
  `nome` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = myIsam;

Allow NULL in parentId, but you can also use NOT NULL (which probably will stay 0), does not change much.

If daughter accounts do not log in (it is only for saving data)

As I talked to the author, the goal is not to have logins for the children, only saved preference data. In this case I would recommend to create 3 tables, one of "profiles", one for "accounts" and the other for "preferences".

Table accounts and profiles relate 1:n and the preference table relates to the profile table 1:1. It is not necessary, the structure I described works either "with" or "without" the foreign keys, but as I said, the advantage is tying the data.

inserir a descrição da imagem aqui

CREATE TABLE IF NOT EXISTS `mydb`.`contas` (
  `id_contas` INT NOT NULL AUTO_INCREMENT,
  `login` VARCHAR(45) NULL,
  `senha` VARCHAR(45) NULL,
  PRIMARY KEY (`id_contas`))
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `mydb`.`perfis` (
  `id_perfis` INT NOT NULL,
  `nome` VARCHAR(45) NULL,
  `perfiscol` VARCHAR(45) NULL,
  `contas_id_contas` INT NOT NULL,
  `preferencias_id_preferencias` INT NOT NULL,
  PRIMARY KEY (`id_perfis`, `contas_id_contas`),
  INDEX `fk_perfis_contas_idx` (`contas_id_contas` ASC),
  INDEX `fk_perfis_preferencias1_idx` (`preferencias_id_preferencias` ASC),
  CONSTRAINT `fk_perfis_contas`
    FOREIGN KEY (`contas_id_contas`)
    REFERENCES `mydb`.`contas` (`id_contas`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_perfis_preferencias1`
    FOREIGN KEY (`preferencias_id_preferencias`)
    REFERENCES `mydb`.`preferencias` (`id_preferencias`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `mydb`.`preferencias` (
  `id_preferencias` INT NOT NULL AUTO_INCREMENT,
  `preferencia1` VARCHAR(45) NULL,
  `preferencia2` VARCHAR(45) NULL,
  PRIMARY KEY (`id_preferencias`))
ENGINE = InnoDB;

However if it is a table without a foreign key, it will have to have a columnar to point out the ids of the other tables, for example:

inserir a descrição da imagem aqui

CREATE TABLE IF NOT EXISTS `mydb`.`contas` (
  `id_contas` INT NOT NULL AUTO_INCREMENT,
  `login` VARCHAR(45) NULL,
  `senha` VARCHAR(45) NULL,
  PRIMARY KEY (`id_contas`))
ENGINE = myIsam;

CREATE TABLE IF NOT EXISTS `mydb`.`perfis` (
  `id_perfis` INT NOT NULL,
  `contas_id_contas` INT NULL,
  `nome` VARCHAR(45) NULL,
  PRIMARY KEY (`id_perfis`))
ENGINE = myIsam;

CREATE TABLE IF NOT EXISTS `mydb`.`preferencias` (
  `id_preferencias` INT NOT NULL AUTO_INCREMENT,
  `perfis_id_perfis` INT NULL,
  `preferencia1` VARCHAR(45) NULL,
  `preferencia2` VARCHAR(45) NULL,
  PRIMARY KEY (`id_preferencias`))
ENGINE = myIsam;

How to use the darlings

To log in you only need to select contas, and save the id in the session.

SELECT id_contas, login, senha FROM contas WHERE login=? AND senha=?

To display the profiles, you can use something like

SELECT id_perfis, nome, perfiscol FROM perfis WHERE contas_id_contas=? #O ? deve ser o ID que foi salvo na sessão

To view profiles and settings:

SELECT
    P1.id_perfis AS ID_PERFIL,
    P1.nome AS NOME,
    P1.perfiscol AS PERFIL_DATA,
    P2.id_preferencias AS ID_PREFERENCIA,
    P2.preferencia1 AS PREFERENCIA_1,
    P2.preferencia2 AS PREFERENCIA_2

FROM
    perfis P1, preferencias P2

where
    P1.id_perfis = P2.perfis_id_perfis
  • I’m having a little trouble understanding, I was already trying to implement what Bluex said, but no foreign keys, just using the php and Join because I’ve used it to something like I don’t know if that example could be applied here, or improved, so I’ll wait a little longer before I mark some response.

  • @Florida edited the answer, but I’m having a doubt, do you want the child user to access by own account? What kind of data do you want to bring? Maybe this will help me formulate the queries for you.

  • No, the login system is only in the main account,(parent account), as well as the login, password, and important information relevant to all the users, in the child accounts, are for example the language, preferences of tabs, private Privacy of the child. That is, the parent account is like a child account, but it is the main one and brings login data, password, level and how many child accounts it can have. All children log in through the parent account and then make the choice of the profile(son) they want to use. If you don’t understand something, you can ask.

  • 1

    @Florida, I get it, so solutions here seem to be of no use to you. I’ll try to reformulate.

  • So it should work, to prevent them from staying fragments when an account is deleted for example, I can create a function in the php and erase her children. The two examples are very useful, who prefer foreign keys can use them, I liked the two alternatives.

2

When we want to develop a database we must structure it in order to create relationships between tables, this relationship part of principle of there is something in common in the tables, for example:

Tabela Pai:

id | Username | Resto..-
 1 |   João   |  ...
 2 |   Maria  |  ...

Table Sons:

id | pai_id | Username | Resto...
1  |   1    |  António |  ...
2  |   2    |  Pedro   |  ...
3  |   1    |  Miguel  |  ...
4  |   1    | Joaquim  |  ...

Notice that there is one element in common in both tables, in this case the field pai_id refers to the id in the parent table, obviously it is up to you to choose the name given to the parent table field, but as a rule pai_id comes from the following nomenclature tabela_field, in this way things become more organized and easy to understand.

I remember saying that the Fields id existing in the parent and child tables are AUTO_INCREMENT, while the field pai_id no, this is filled only when the child is created.

Structure of a database:

Estrutura de uma base de dados

This image demonstrates the structure of a database and you can see that there are such common elements.

In practice:

To get your child, the query would be something like this:

SELECT * FROM `filhos` WHERE pai_id='valor';

To exclude you could use the same field as an identification:

DELETE FROM `filhos` WHERE pai_id='valor';

All said was the basis for the development of your application, it is up to you to structure in your own way.


Regarding the system and following a bit of your logic (although I do not know exactly how the Netflix system works) I will present an idea of implementing this system:

  • The main account would be in the table pai and this would be responsible for payments.
  • The sub-accounts would be in the table filhos and these would be responsible for filtering content.

Functioning: The user would log in to the main account and in turn a list would appear with the sub-accounts, the user would choose his sub-account and log in to that sub-account.

Browser other questions tagged

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