How to add Foreign key in a table after the creation of the
table?
Answers were Sorted based on User's Feedback
Answer / sagar98
ALTER TABLE <table_name>
ADD CONSTRAINT <fk_name> FOREIGN KEY (column_name)
REFERENCES <table_name_where_this_column_acts_as_PK>
(Column_name)
| Is This Answer Correct ? | 19 Yes | 0 No |
Answer / purushotham
we can add a foreign key to a table
1)while creation of table
2)After the creation of the table
by using
Alter table table_name add foreign key(column_name)
References column_name from table_name(that consists of
primary key)
| Is This Answer Correct ? | 7 Yes | 4 No |
Answer / devi
alter table table2
add(constraint table2_col2_fk foreign key(col2) references
table1(col1);
here col2 is the foreign key of table2 which refer to the
col1 of table1 which is h epimary key of table1
| Is This Answer Correct ? | 3 Yes | 0 No |
Answer / subbu
You cannot add foreign key after creation of the table until
unless you have assigned the primary key while creating the
table. I mean without a primary key assigned for a table, we
cannot add foreign key.
Once the primary key is found, then query to add is been
already given by the above posters.
| Is This Answer Correct ? | 2 Yes | 2 No |
Answer / venkatesh
u have create two tables an the time one in first table must an should using primary key .in way of using ddl cmd.
A)alter table table name add fk(clm name) reference column name from table name consists of primary key
| Is This Answer Correct ? | 0 Yes | 0 No |
Can we use update in sql function?
What is bind variable in pl sql?
Why sql query is slow?
What is a clob in sql?
What is pl/sql and what is it used for?
What is a behavioral trigger?
Which is faster subquery or join?
how to include comments in sql statements? : Sql dba
Differentiate between sga and pga.
What is difference between sql and oracle?
What is sqlca in db2?
From an Employee table, how will you display the record which has a maximum salary?
29 Answers Cap Gemini, Exilant, Synechron,
Oracle (3259)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)