Database Register

Asked

Viewed 62 times

-1

My database has this structure.

inserir a descrição da imagem aqui

I would like to know how to create the record on PEOPLE and at the same time get the ID and save it on PESSOAS_FISICAS.

I created this command for such, but it didn’t work.

select ID_PESSOAS,
   NOME,
   CPF,
   SEXO,
   ENDERECO,
   COMPLEMENTO,
   CIDADE,
   ESTADO,
   FK_ID_PESSOAS
from DB_PESSOAS, DB_PESSOAS_FISICAS
WHERE (DB_PESSOAS.ID_PESSOAS = DB_PESSOAS_FISICAS.FK_ID_PESSOAS)

2 answers

0


Hello friend all right? good guess what you are looking for is lastInsertId. It is responsible for taking the last ID registered in the bank, so this way you can register the ID (PEOPLE Table ) in (PESSOAS_FISICAS). Since you didn’t say the language to exemplify, do a search in the correct syntax around. Well I hope I helped!

  • 1

    The syntax is in the tag is [tag:oracle].

  • Can I do this on the same insertion screen? Or is there some other procedure to fill in?

  • Yes you can do in the same insertion screen, is register take the last ID before closing the connection and perform another registration, with the last registered ID.

  • I haven’t been able to yet because I don’t know how to apply this in APEX, but I did some tests, and I believe it will, thank you very much.

0

When entering, you need to get the value that was entered in the key field, in your case the field id. After the insert use RETURNING {campo_chave} INTO {variavel} knowing that the variavel must be declared in advance. See insert down below:

    insert into db_pessoa values (1,'PEDRO') RETURNING id INTO variavel_id;

Below is an example tested in SQL Fiddle.

SQL Fiddle

Oracle 11g R2 Schema Setup:

CREATE TABLE db_pessoa
 (id number
 ,nome CHAR(50)
);

 
 CREATE TABLE db_pessoa_fisica
 (fk_id number
 ,sexo CHAR(50)
);

Query 1:

DECLARE
   variavel_id number;
BEGIN
   insert into db_pessoa values (1,'PEDRO') RETURNING id INTO variavel_id;
   insert into db_pessoa_fisica values (variavel_id,'MASCULINO');
END;

Results:

Query 2:

select * from db_pessoa

Results:

ID NAME
1 PEDRO

Query 3:

select * from db_pessoa_fisica

Results:

FK_ID SEX
1 MASCULINE

Browser other questions tagged

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