Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new column with foreign key constraint in a single statement in oracle

How to add a new column with foreign key constraint in a single statement in oracle?Can any one give an example query?

like image 941
Tom Sebastian Avatar asked Jul 18 '13 11:07

Tom Sebastian


People also ask

How do I add a foreign key constraint to an existing table in Oracle?

Create a foreign key constraint The CONSTRAINT clause is optional. If you omit it, Oracle will assign a system-generated name to the foreign key constraint. Second, specify the FOREIGN KEY clause to defines one or more column as a foreign key and parent table with columns to which the foreign key columns reference.

How can create table with primary key and foreign key in Oracle?

After naming your constraint, add the words FOREIGN KEY to specify that it is a foreign key constraint. Then, open brackets and add in the name of the column in this table that will be the foreign key. Then, close the brackets. Next, add the word REFERENCES , then the name of the other table you're referring to.

How can I insert foreign key values in a table in Oracle?

insert into person (id, name, last_name, city_id) values (2, 'Elvis', 'Presley', 4); ... then you'd get an error (ORA-02291) that the parent key - that is, a matching id value in the city tables - doesn't exist. You can read more about foreign keys in the database concepts guide. Show activity on this post.

How do I add a column to a constraint?

The basic syntax of an ALTER TABLE command to add a NOT NULL constraint to a column in a table is as follows. ALTER TABLE table_name MODIFY column_name datatype NOT NULL; The basic syntax of ALTER TABLE to ADD UNIQUE CONSTRAINT to a table is as follows.


Video Answer


2 Answers

ALTER TABLE CODE_LIST_TYPE_ERROR  ADD ID_CODE_LISTS VARCHAR2(50) NOT NULL CONSTRAINT CODE_LIST_TYPE_ERROR_FK REFERENCES CODE_LISTS(ID); 

oracle query to modify table and add new column which is refrence of another table

like image 27
Neeraj Gahlawat Avatar answered Sep 27 '22 22:09

Neeraj Gahlawat


    alter table tab1     add c1 number(20) constraint tab1_c1_fk references tab2(c2); 

c1 new column in the table tab1 with the FK tab1_c1_fk on the table tab2 column c2.

like image 181
schurik Avatar answered Sep 27 '22 21:09

schurik