How to create a table in Oracle using Foreign key

Asked

Viewed 529 times

-2

The situation is that I have a table called OS and I want to create a link from this OS table to the CLIENT using the CPF column as FK. Where I am missing?

I am editing the databank straight from Netbeans. The client table has already been created and has CPF as PK.

CREATE TABLE OS (
NUMEROOS NUMBER(9) NOT NULL PRIMARY KEY,
DATAABERTURA VARCHAR2(40) NOT NULL,
HORARIOABERTURA VARCHAR2(40) NOT NULL,
STATUS VARCHAR2(40) NOT NULL,
CPF VARCHAR2(14) FOREIGN KEY REFERENCES CLIENTE(CPF),
DISPOSITIVO VARCHAR2(40) NOT NULL,
MARCA VARCHAR2(40) NOT NULL,
MODELO VARCHAR2(40) NOT NULL,
NUMEROSERIE VARCHAR2(40),
IMEI VARCHAR2(40),
DESCRICAOPROBLEMA VARCHAR2(400) NOT NULL,
OBSERVACOES VARCHAR2(40)
)

1 answer

0

Is to put a Constraint referencing a foreign key in this way:

CREATE TABLE TB_FONTE_FINANCIAMENTO
(
  FF_ID NUMBER(10) PRIMARY KEY NOT NULL,
  FF_DESCRICAO VARCHAR2(100) NOT NULL,
  FF_SITUACAO VARCHAR2(10) DEFAULT 'Ativo' NOT NULL
);
CREATE TABLE TB_PROJETO
(
  PJ_ID NUMBER(10) PRIMARY KEY NOT NULL,
  PJ_DESCRICAO VARCHAR2(100) NOT NULL,
  PJ_SITUACAO VARCHAR2(10) DEFAULT 'Ativo' NOT NULL,
  FF_ID NUMBER(10) NOT NULL,
  CONSTRAINT FK_FONTE_FINANCIAMENTO FOREIGN KEY (FF_ID) REFERENCES TB_FONTE_FINANCIAMENTO (FF_ID)
);
  • I did so, apparently it worked... thank you

  • @Alexandrebezerra if the solution helped you, do not forget to mark as solved.

  • How do you mark as resolved?

Browser other questions tagged

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