How to update to table with Foreign key

Asked

Viewed 583 times

-1

I have table discipline and table teacher. The teacher contains discipline. How do I change the name of the discipline and automatically change in the teachers?

I’m a little confused about using On update Box and delete Case.. It deletes and modifies all children, but how I use update?

public void createTableDisciplina(){
    try {
        conn.createStatement().execute("CREATE TABLE Disciplina(Nome varchar(50) NOT NULL primary key)");
    } catch (SQLException ex) {
        Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public void createTableProfessor(){
    try {
        conn.createStatement().execute("CREATE TABLE Professor(Nome varchar(50) NOT NULL primary key, Nome_Disciplina varchar(50) NOT NULL references Disciplina(Nome) ON DELETE Cascade, NumAulas int NOT NULL, NumFaltas int)");
    } catch (SQLException ex) {
        Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
    }
}
  • Arthur, you need to consult Help and do the Soft Tour here, you need to present part of your code and also the table you are talking about. Edit your question, but first see how to address the questions here. Welcome!

  • 1

    Thank you, I’ve adjusted!

1 answer

1


Arthur, the ideal is that since there is a relationship between both tables only one of them (the discipline table) contains the name of the discipline.

The relationship is just this... the teacher table relates to the discipline table through the id of this discipline (immutable) and made the relationship we know which name of the discipline to which has the id linked to a teacher.

Considering this implementation you would only need to change the name of the discipline in the discipline table and would not need to worry about the rest as the relationship would make the id "translation" into the name or other columns you want from the discipline table.

Edited:

Arthur the origin of the problem lies in the way you are creating the table.

A primary call (PRIMARY KEY) can never be a value you can change, it must be an incremental and unique number for each row of your table, so I would create an entire ID field in both tables as the primary key and create an entire field in the class table which would receive the same id of the discipline you want to link, for example:

CREATE TABLE disciplina (
    /* NOT NULL é dispensável considerando que uma chave primaria não pode ser nula. */
    id INTEGER PRIMARY KEY, 
    nome VARCHAR(100) NOT NULL
);

CREATE TABLE professor (
    id INTEGER PRIMARY KEY,
    nome varchar(100) NOT NULL,
    /*não utilizarei DELETE CASCADE pois se utilizar e um professor for removido a disciplina também será...*/
    id_disciplina INTEGER NOT NULL REFERENCES disciplina(nome),
    numAulas INTEGER NOT NULL,
    numFaltas INTEGER
);

INSERT INTO disciplina VALUES(0 /*id da disciplina*/, 'Português');

INSERT INTO professor(id, nome, id_disciplina, numAulas) 
VALUES(0 /*id do professor*/, 'Zé do Caroço', 0 /*id da disciplina relacionada*/, 0 /*numAulas*/);

/*Este SELECT retornará o nome do professor e de sua disciplina relacionada.*/
SELECT professor.nome, disciplina.nome
FROM professor
INNER JOIN disciplina ON professor.id_disciplina = disciplina.id;
  • Thanks @Bruno Berman, posted the code.. But I did not understand how to make such a relationship in the discipline table

  • Thanks guy helped me a lot!! I’ll give a read on this Join Ner..

  • But what if I had another table that has a teacher. For example, class, and a teacher who has discipline. And I want to delete the teacher, like I do?

  • The way I put it, you would normally delete the teacher, but you could take subjects without an assigned teacher, in case there’s only one teacher per discipline. Delete will only occur automatically in the related tables when a DELETE CASCADE is specified for example: class relates to teacher (contains a field referencing the teacher table) but with DELETE CASCADE condition. If I delete a class the teachers linked to it will also be removed.

Browser other questions tagged

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