Make INNER JOIN with more than one table

Asked

Viewed 48 times

-1

I want to relate Inner Join in the Employee table with Address, And department and function but only that the function table is foreign key in the department tableinserir a descrição da imagem aqui.

INNER JOIN no C#

SqlDataAdapter QIG4A = new SqlDataAdapter();
                string sql4A = "SELECT DP.ID_DEPARTAMENTO, DP.NOME_DEP, FUNCAO.NOME_FUNCAO, FUNCAO.SALARIO FROM DEPARTAMENTO AS DP INNER JOIN FUNCAO ON DP.ID_FUNCAO = FUNCAO.ID_FUNCAO;";
                DataTable tb4A = new DataTable();
                QIG4A = new SqlDataAdapter(sql4A, Q1.SMS());
                QIG4A.Fill(tb4A);
                comboBoxEx3.DataSource = tb4A;
                comboBoxEx3.DisplayMember = "NOME_DEP";
                comboBoxEx4.DataSource = tb4A;
                comboBoxEx4.DisplayMember = "NOME_FUNCAO";
                comboBoxEx18.DataSource = tb4A;
                comboBoxEx18.DisplayMember = "SALARIO";
                comboBoxEx19.DataSource = tb4A;
                comboBoxEx19.DisplayMember = "ID_DEPARTAMENTO";

the tables

CREATE TABLE FUNCIONARIO
(
    ID_FUNCIONARIO INT IDENTITY(1,1),
    NOME_FUNCIONARIO VARCHAR(100) NOT NULL,
    NOME_PAI VARCHAR(100) NOT NULL,
    NOME_MAE VARCHAR(100)NOT NULL,
    N_BI VARCHAR(50)UNIQUE NOT NULL,
    DATA_NASC VARCHAR(100) NOT NULL,
    GENERO VARCHAR(100) NOT NULL,
    ESTA_CIVIL VARCHAR(100)NOT NULL,
    FOTO VARCHAR (100) NOT NULL,
    ID_DEPARTAMENTO INT,
    ID_MUNICIPIO INT,
    DATA_HORA VARCHAR (100)NOT NULL,
    USUARIO VARCHAR (100)NOT NULL,
    CONSTRAINT PK_FUNC PRIMARY KEY (ID_FUNCIONARIO),

    CONSTRAINT PK_FUNC_MUNI FOREIGN KEY (ID_MUNICIPIO)
    REFERENCES FUNCAO (ID_FUNCAO),

    CONSTRAINT PK_FUNC_DEP FOREIGN KEY (ID_DEPARTAMENTO)
    REFERENCES DEPARTAMENTO (ID_DEPARTAMENTO)

)

CREATE TABLE ENDERECO
(
    ID_ENDERECO INT IDENTITY(1,1),
    ID_PROVINCIA INT,
    ID_MUNICIPO INT,
    RUA VARCHAR (100),
    BAIRRO VARCHAR (100),
    N_CASA VARCHAR(100),
    CONSTRAINT PK_ENDERECO PRIMARY KEY (ID_ENDERECO),
    CONSTRAINT PK_END_PROV FOREIGN KEY (ID_PROVINCIA)
    REFERENCES PROVINCIA(ID_PROVINCIA),
    CONSTRAINT PK_END_MUN FOREIGN KEY (ID_MUNICIPO)
    REFERENCES MUNICIPIO(ID_MUNICIPO)
)

CREATE TABLE PROVINCIA
(
    ID_PROVINCIA int identity(1,1),
    NOME_PROVINCIA VARCHAR(100),
    CONSTRAINT PK_PROVINCIA PRIMARY KEY(ID_PROVINCIA)
)

CREATE TABLE MUNICIPIO
(
    ID_MUNICIPO INT IDENTITY(1,1),
    NOME_MUNICIPIO VARCHAR (100),
    ID_PROVINCIA INT,
    CONSTRAINT PK_MUNICIPO PRIMARY KEY (ID_MUNICIPO),
    CONSTRAINT PK_MUN_PROV FOREIGN KEY(ID_PROVINCIA)
     REFERENCES PROVINCIA(ID_PROVINCIA)
)

CREATE TABLE DEPARTAMENTO
(
    ID_DEPARTAMENTO INT IDENTITY(1,1),
    NOME_DEP VARCHAR(100)NOT NULL,
    ID_FUNCAO INT ,
    CONSTRAINT PK_DEP PRIMARY KEY (ID_DEPARTAMENTO),
    CONSTRAINT PK_DEP_FUNCAO FOREIGN KEY (ID_FUNCAO)
    REFERENCES FUNCAO (ID_FUNCAO)
)
USE PRESENCA1
CREATE TABLE FUNCAO
(
    ID_FUNCAO INT IDENTITY(1,1),
    NOME_FUNCAO VARCHAR(100)NOT NULL,
    SALARIO VARCHAR(100)NOT NULL,
    DATA VARCHAR(100) NOT NULL,
    CONSTRAINT PK_FUNCAO PRIMARY KEY (ID_FUNCAO)
)

And also in datagridviews are appearing Ids instead of names

  • "only that the function table is inside the table" How so a table inside the table? it does not exist, explain better. To do another join just add in the command sql another join, always before the where when there is

  • Or rather! The function table is foreign key in the departamentom table I also need the function tebela and it is also giving me error in Insert (insert in the official table with the foreign keys)

1 answer

0

Mario, see that, vc created a relationship between the Department tables and function, this will allow the joins, when necessary. For the employee and department tables, you will need to create a table that will store the relationships between these tables, like the Function and Iddepto, this will solve whenever you need to relate an employee to a department and vice versa. This applies to other tables in their structure, which do not have a relationship.

Browser other questions tagged

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