problems to create multiple tables in Oracle

Asked

Viewed 321 times

1

I am beginner in the use of Oracle, wore the firebird before. I’d like to know, why I can’t create multiple tables at once in the SQLdeveloper. It creates only the first table (Country).

I try to run the following instructions, to create all tables at once. (in this case I’m using only 3 tables to not get too long).

CREATE TABLE PAIS
(
  IdPais INTEGER NOT NULL,
  NomePais VARCHAR(60) NOT NULL,
  SiglaPais VARCHAR(3) NOT NULL,
  StatusPais CHAR(1) NOT NULL 
);

ALTER TABLE PAIS ADD
(
  CONSTRAINT PAIS PRIMARY KEY (IdPais)
);

CREATE SEQUENCE SEQ_ID_PAIS
MINVALUE 1
MAXVALUE 9999999999
START WITH 1
INCREMENT BY 1
NOCACHE
CYCLE;

CREATE OR REPLACE TRIGGER TRG_ID_PAIS BEFORE INSERT ON PAIS FOR EACH ROW BEGIN <<COLUMN_SEQUENCES>> BEGIN IF :NEW.IDPAIS IS NULL THEN SELECT SEQ_ID_PAIS.NEXTVAL INTO :NEW.IDPAIS FROM DUAL; END IF; END COLUMN_SEQUENCES; END;


CREATE TABLE ESTADO
(
  IdEstado INTEGER NOT NULL,
  IdPais INTEGER NOT NULL,
  NomeEstado VARCHAR(60)  NOT NULL,
  SiglaEstado VARCHAR(2)  NOT NULL,
  StatusEstado CHAR(1) NOT NULL 
);

ALTER TABLE ESTADO ADD
(
  CONSTRAINT ESTADO PRIMARY KEY (IdEstado)
);

CREATE SEQUENCE SEQ_ID_ESTADO
MINVALUE 1
MAXVALUE 9999999999
START WITH 1
INCREMENT BY 1
NOCACHE
CYCLE;

CREATE OR REPLACE TRIGGER TRG_ID_ESTADO BEFORE INSERT ON ESTADO FOR EACH ROW BEGIN <<COLUMN_SEQUENCES>> BEGIN IF :NEW.IDESTADO IS NULL THEN SELECT SEQ_ID_ESTADO.NEXTVAL INTO :NEW.IDESTADO FROM DUAL; END IF; END COLUMN_SEQUENCES; END;


CREATE TABLE CIDADE
(
  IdCidade INTEGER NOT NULL,
  IdEstado INTEGER NOT NULL,
  NomeCidade VARCHAR(60)  NOT NULL,  
  StatusCidade CHAR(1) NOT NULL 
);

ALTER TABLE CIDADE ADD
(
  CONSTRAINT CIDADE PRIMARY KEY (IdCidade)
);

CREATE SEQUENCE SEQ_ID_CIDADE
MINVALUE 1
MAXVALUE 9999999999
START WITH 1
INCREMENT BY 1
NOCACHE
CYCLE;

CREATE OR REPLACE TRIGGER TRG_ID_CIDADE BEFORE INSERT ON CIDADE FOR EACH ROW BEGIN <<COLUMN_SEQUENCES>> BEGIN IF :NEW.IDCIDADE IS NULL THEN SELECT SEQ_ID_CIDADE.NEXTVAL INTO :NEW.IDCIDADE FROM DUAL; END IF; END COLUMN_SEQUENCES; END;
  • You are running as script?

  • 1

    @Reginaldorigo yes, I am ! (in the SQL spreadsheet - alt+F10)

  • 1

    try to put under each command the bar / and forehead

  • 1

    @adventistaam as well, the bar ? Where to put and what the purpose ?

  • I do it in mine and it works rsrs, it’s like a delimiter of each command

  • 1

    @adventist, right, more where I place specifically ?

  • 1

    as @adventistaam indicated, place a bar at the end of each create. ex: create table test(id number); / create table teste2(id number);

  • 1

    @Renatoafonso Right, but it just works by creating the table. It just worked by leaving the CREATE TABLE. If I’m going to use it the way it is in my script it doesn’t run ! For the creation of SEQUENCE and TRIGGER together the tables did not work. (I tried to add to the end of each semicolon also to test, and it did not help)

Show 3 more comments

1 answer

1


I can’t remember if Oracle Database allows more than one object to have the same name. In the script you posted, the primary key of each table is with the same table name.

You can set the primary key in several ways:

--código #2
CREATE TABLE PAIS
(
  IdPais INTEGER NOT NULL,
  NomePais VARCHAR2(60) NOT NULL,
  SiglaPais VARCHAR2(3) NOT NULL,
  StatusPais CHAR(1) NOT NULL,
  constraint pkPAIS primary key (IdPais)
);
/

or

-- código #3
CREATE TABLE PAIS
(
  IdPais INTEGER NOT NULL,
  NomePais VARCHAR2(60) NOT NULL,
  SiglaPais VARCHAR2(3) NOT NULL,
  StatusPais CHAR(1) NOT NULL 
);
/
ALTER TABLE PAIS
  ADD pkPAIS primary key (IdPais);
/

If there is no need to name the restriction, you can also use this form:

-- código #1
CREATE TABLE PAIS
(
  IdPais INTEGER NOT NULL primary key,
  NomePais VARCHAR2(60) NOT NULL,
  SiglaPais VARCHAR2(3) NOT NULL,
  StatusPais CHAR(1) NOT NULL 
);
/
  • he accepts with the same name, the problem I’m facing, is that he doesn’t create all the tables at once. Using the Script I posted as an example. If I compile one table at a time, then it works. When I try to run the 3 tables together, it gives me the following error: Error(3,1): PLS-00103: Found the "CREATE" symbol (this error points to the VARCHAR(60) NOT NULL Parent line,).

  • @Alunooracle: After you create one table at a time, you run the other commands one at a time and also no error occurs? If so, it also seems to me the lack of / delimiting each execution block. However, the error line number is strange. ||| The #3 code I posted as an example, it runs error-free?

  • I used code #2, I was right, what happens is that I had not delimited all the parts with the /. Now it worked out, thank you very much and sorry !

  • 1

    Just one more question, the correct way to create a FK via script would be: FOREIGN KEY (Idpais) REFERENCES PAIS (Idpais) in the case of the STATE table.

  • 1

    @Alunooracle: Can be done through ALTER TABLE or directly in the table declaration. // ... CONSTRAINT fkPais FOREIGN KEY (Idpais) REFERENCES PAIS (Idpais)

  • 1

    the way I quoted above is incorrect ? FOREIGN KEY (Idpais) REFERENCES PAIS (Idpais)

Show 1 more comment

Browser other questions tagged

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