Whenever a table is created in Oracle its shld be done by Four step process
- Table Creation
- Adding Constraints(Primary Key, Foreign Key)
- Adding Synonyms
- Giving Grants
- Alter Queries
Table Creation
create table TABLE_NAME_1 ( COLUMN_NAME_1 varchar2(50), COLUMN_NAME_2 NUMBER(1), COLUMN_NAME_3 DATE not null, COLUMN_NAME_4 NUMBER(38) );
Constraint
Query 1
alter table TABLE_NAME_2 add constraint FK_COLUMN_NAME_1 foreign key (COLUMN_NAME_1) references TABLE_NAME_1 (COLUMN_NAME_1);
Query 2
alter table TABLE_NAME_3 add constraint fk_column_1 foreign key (COLUMN_1, COLUMN_2) references TABLE_NAME_2 (COLUMN_1, COLUMN_2);
Synonym
CREATE PUBLIC SYNONYM TABLE_NAME_1 FOR OWNER_NAME.TABLE_NAME_1; CREATE PUBLIC SYNONYM SEQUENCE_NAME_SEQ FOR OWNER_NAME.SEQUENCE_NAME_SEQ; GRANT ALL ON SEQUENCE_NAME_SEQ TO ADMIN,PART,ALL_DEVELOPERS;
Grants
GRANT ALL ON TABLE_NAME_1 TO ADMIN,PART,ALL_DEVELOPERS; GRANT INSERT,UPDATE,DELETE,SELECT ON TABLE_NAME_1 TO ADMIN,PART,ALL_DEVELOPERS;
Other Queries
ALTER TABLE TABLE_NAME_1 COLUMN_NAME_1 NOT NULL; ALTER TABLE TABLE_NAME_1 MODIFY COLUMN_NAME_1 varchar2(50); ALTER TABLE TABLE_NAME_1 DROP COLUMN COLUMN_NAME_1; ALTER TABLE TABLE_NAME_1 DROP CONSTRAINT COLUMN_NAME_1;
Things to Consider
While creating Primary key(No Null Values allowed like unique key) as a combination of two or more columns you should take in to consideration only the non null columns as part of unique key.
ALTER TABLE TABLE_NAME ADD CONSTRAINT PK_TABLE_NAME PRIMARY KEY (COLUMN_NAME_1, COLUMN_NAME_2, COLUMN_NAME_3) USING INDEX TABLESPACE INDX;
COLUMN_NAME_1, COLUMN_NAME_2, COLUMN_NAME_3 should be a Non Null column.