How to add a new column with foreign key
constraint
in a single statement in oracle
?Can any one give an example query?
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.
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.
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.
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.
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With