CREATE TABLE COMENZI 
   (	ID_COMANDA NUMBER(20,0) NOT NULL ENABLE, 
	ID_LUCRARE NUMBER(20,0), 
	NR_COMANDA VARCHAR2(100) NOT NULL ENABLE, 
	DATA_COMANDA DATE NOT NULL ENABLE, 
	ID_PART NUMBER(10,0), 
	DATA_LIVRARE DATE, 
	DATA_LIVRAT DATE, 
	NR_LIVRARE VARCHAR2(50), 
	ID_AGENT NUMBER(10,0), 
	ID_DELEGAT NUMBER(10,0), 
	ID_MASINA NUMBER(10,0), 
	INTERNA NUMBER(1,0) DEFAULT 1 NOT NULL ENABLE, 
	STERS NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE, 
	ID_UTIL NUMBER(10,0) NOT NULL ENABLE, 
	DATAORA DATE DEFAULT SYSDATE NOT NULL ENABLE, 
	ID_UTILS NUMBER(10,0), 
	DATAORAS DATE, 
	ID_GESTIUNE NUMBER(10,0), 
	ID_SECTIE NUMBER(5,0), 
	ID_SECTIE2 NUMBER(5,0), 
	ID_LIVRARE NUMBER(5,0), 
	ID_FACTURARE NUMBER(5,0), 
	ID_CODCLIENT VARCHAR2(20), 
	COMANDA_EXTERNA VARCHAR2(100), 
	ID_SUCURSALA NUMBER(5,0), 
	PROC_DISCOUNT NUMBER(10,4) DEFAULT 0, 
	ID_CTR NUMBER(8,0), 
	DATAORA_UM DATE, 
	ID_UTIL_UM NUMBER(10,0), 
	 CONSTRAINT FK_COMENZI_006 FOREIGN KEY (ID_UTIL)
	  REFERENCES CONTAFIN_ORACLE.UTILIZATORI (ID_UTIL) ENABLE NOVALIDATE, 
	 CONSTRAINT FK_COMENZI_007 FOREIGN KEY (ID_UTILS)
	  REFERENCES CONTAFIN_ORACLE.UTILIZATORI (ID_UTIL) ENABLE NOVALIDATE, 
	 CONSTRAINT FK_COMENZI_005 FOREIGN KEY (ID_MASINA)
	  REFERENCES NOM_MASINI (ID_MASINA) ENABLE NOVALIDATE, 
	 CONSTRAINT FK_COMENZI_001 FOREIGN KEY (ID_LUCRARE)
	  REFERENCES NOM_LUCRARI (ID_LUCRARE) ENABLE NOVALIDATE, 
	 CONSTRAINT FK_COMENZI_002 FOREIGN KEY (ID_PART)
	  REFERENCES NOM_PARTENERI (ID_PART) ENABLE NOVALIDATE, 
	 CONSTRAINT FK_COMENZI_003 FOREIGN KEY (ID_AGENT)
	  REFERENCES NOM_PARTENERI (ID_PART) ENABLE NOVALIDATE, 
	 CONSTRAINT FK_COMENZI_004 FOREIGN KEY (ID_DELEGAT)
	  REFERENCES NOM_PARTENERI (ID_PART) ENABLE NOVALIDATE, 
	 CONSTRAINT FK_COMENZI_008 FOREIGN KEY (ID_GESTIUNE)
	  REFERENCES NOM_GESTIUNI (ID_GESTIUNE) ENABLE, 
	 CONSTRAINT FK_COMENZI_009 FOREIGN KEY (ID_LIVRARE)
	  REFERENCES ADRESE_PARTENERI (ID_ADRESA) ENABLE, 
	 CONSTRAINT FK_COMENZI_010 FOREIGN KEY (ID_FACTURARE)
	  REFERENCES ADRESE_PARTENERI (ID_ADRESA) ENABLE, 
	 CONSTRAINT FK_COMENZI_011 FOREIGN KEY (ID_SUCURSALA)
	  REFERENCES CONTAFIN_ORACLE.NOM_FIRME (ID_FIRMA) ENABLE, 
	 CONSTRAINT FK_COMENZI_012 FOREIGN KEY (ID_CTR)
	  REFERENCES CONTRACTE (ID_CTR) ENABLE
   );
  ALTER TABLE COMENZI ADD CONSTRAINT PK_COMENZI PRIMARY KEY (ID_COMANDA) USING INDEX PK_COMENZI  ENABLE;

  CREATE UNIQUE INDEX PK_COMENZI ON COMENZI (ID_COMANDA);
  CREATE INDEX IDX_COMENZI_002 ON COMENZI (STERS);
  ALTER TABLE COMENZI MODIFY (ID_COMANDA NOT NULL ENABLE);
  ALTER TABLE COMENZI MODIFY (NR_COMANDA NOT NULL ENABLE);
  ALTER TABLE COMENZI MODIFY (DATA_COMANDA NOT NULL ENABLE);
  ALTER TABLE COMENZI MODIFY (INTERNA NOT NULL ENABLE);
  ALTER TABLE COMENZI MODIFY (STERS NOT NULL ENABLE);
  ALTER TABLE COMENZI MODIFY (ID_UTIL NOT NULL ENABLE);
  ALTER TABLE COMENZI MODIFY (DATAORA NOT NULL ENABLE);
  

   COMMENT ON COLUMN COMENZI.ID_SECTIE IS 'sectia pe care se lucreaza';
   COMMENT ON COLUMN COMENZI.ID_SECTIE2 IS 'sectia care a dat comanda';
   COMMENT ON COLUMN COMENZI.ID_LIVRARE IS 'Adresa de livrare';
   COMMENT ON COLUMN COMENZI.ID_FACTURARE IS 'Adesa de facturare';
   COMMENT ON COLUMN COMENZI.ID_CODCLIENT IS 'Cod extern de client';
   COMMENT ON COLUMN COMENZI.COMANDA_EXTERNA IS 'Comanda externa';
   COMMENT ON COLUMN COMENZI.DATAORA_UM IS 'Data ultimei modificari';
   COMMENT ON COLUMN COMENZI.ID_UTIL_UM IS 'Utilizator ultima modificare';
   
   
   CREATE TABLE COMENZI_ELEMENTE 
   (	ID_COMANDA_ELEMENT NUMBER(20,0) NOT NULL ENABLE, 
	ID_COMANDA NUMBER(20,0) NOT NULL ENABLE, 
	ID_ARTICOL NUMBER(20,0) NOT NULL ENABLE, 
	ID_POL NUMBER(20,0) NOT NULL ENABLE, 
	PRET NUMBER(14,3) NOT NULL ENABLE, 
	CANTITATE NUMBER(14,3) NOT NULL ENABLE, 
	STERS NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE, 
	ID_UTILS NUMBER(10,0), 
	DATAORAS DATE, 
	ID_VALUTA NUMBER(10,0) DEFAULT 0 NOT NULL ENABLE, 
	PRET_CU_TVA NUMBER(1,0), 
	ID_SECTIE NUMBER(5,0), 
	DISCOUNT_UNITAR NUMBER(20,4) DEFAULT 0, 
	 CONSTRAINT FK_COMENZI_ELEMENTE_003 FOREIGN KEY (ID_UTILS)
	  REFERENCES CONTAFIN_ORACLE.UTILIZATORI (ID_UTIL) ENABLE NOVALIDATE, 
	 CONSTRAINT FK_COMENZI_ELEMENTE_001 FOREIGN KEY (ID_ARTICOL)
	  REFERENCES NOM_ARTICOLE (ID_ARTICOL) ENABLE NOVALIDATE, 
	 CONSTRAINT FK_COMENZI_ELEMENTE_002 FOREIGN KEY (ID_POL)
	  REFERENCES CRM_POLITICI_PRETURI (ID_POL) ENABLE NOVALIDATE, 
	 CONSTRAINT FK_COMENZI_ELEMENTE_004 FOREIGN KEY (ID_COMANDA)
	  REFERENCES COMENZI (ID_COMANDA) ENABLE NOVALIDATE, 
	 CONSTRAINT FK_COMENZI_ELEMENTE_005 FOREIGN KEY (ID_VALUTA)
	  REFERENCES NOM_VALUTE (ID_VALUTA) ENABLE NOVALIDATE
   ) ;
ALTER TABLE COMENZI_ELEMENTE ADD CONSTRAINT PK_COMENZI_ELEMENTE PRIMARY KEY (ID_COMANDA_ELEMENT) USING INDEX PK_COMENZI_ELEMENTE  ENABLE;
  CREATE UNIQUE INDEX PK_COMENZI_ELEMENTE ON COMENZI_ELEMENTE (ID_COMANDA_ELEMENT);
  ALTER TABLE COMENZI_ELEMENTE MODIFY (ID_COMANDA_ELEMENT NOT NULL ENABLE);
  ALTER TABLE COMENZI_ELEMENTE MODIFY (ID_COMANDA NOT NULL ENABLE);
  ALTER TABLE COMENZI_ELEMENTE MODIFY (ID_ARTICOL NOT NULL ENABLE);
  ALTER TABLE COMENZI_ELEMENTE MODIFY (ID_POL NOT NULL ENABLE);
  ALTER TABLE COMENZI_ELEMENTE MODIFY (PRET NOT NULL ENABLE);
  ALTER TABLE COMENZI_ELEMENTE MODIFY (CANTITATE NOT NULL ENABLE);
  ALTER TABLE COMENZI_ELEMENTE MODIFY (STERS NOT NULL ENABLE);
  ALTER TABLE COMENZI_ELEMENTE MODIFY (ID_VALUTA NOT NULL ENABLE);
  ALTER TABLE COMENZI_ELEMENTE ADD CONSTRAINT PK_COMENZI_ELEMENTE PRIMARY KEY (ID_COMANDA_ELEMENT)
  USING INDEX PK_COMENZI_ELEMENTE  ENABLE;