Question:

Which of the following is correct syntax for inserting foreign key constraint in a relation?

Updated On: May 28, 2025
  • ALTER TABLE table_name ADD FOREIGN KEY(attribute_name) REFERENCES referenced_table_name(attribute_name)
  • ADD TABLE table_name ADD FOREIGN KEY(attribute_name) REFERENCES referenced_table_name(attribute_name)
  • ALTER TABLE table_name REFERENCES referenced_table_name(attribute_name) ADD FOREIGN KEY(attribute_name)
  • MODIFY TABLE table_name ADD FOREIGN KEY(attribute_name) REFERENCES referenced_table_name(attribute_name)
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Approach Solution - 1

The correct syntax for inserting a foreign key constraint in a relation is: ALTER TABLE table_name ADD FOREIGN KEY(attribute_name) REFERENCES referenced_table_name(attribute_name). This is because the ALTER TABLE statement is used to modify an existing table's structure. By using ADD FOREIGN KEY, we specify that a particular column in the table should establish a relationship with a primary key in the referenced table. Here are the steps and components involved in understanding this syntax: 

  • ALTER TABLE table_name: This part of the syntax indicates that we are modifying the structure of the specified table, table_name.
  • ADD FOREIGN KEY(attribute_name): This keyword specifies that we are adding a foreign key constraint to a column, identified as attribute_name, in the current table.
  • REFERENCES referenced_table_name(attribute_name): This section indicates that the foreign key established in the current table will refer to a column in the referenced_table_name. The column referenced is specified within the parentheses.

Using this syntax correctly ensures that referential integrity is maintained between tables in a database, preventing inconsistency in data relationships.

Was this answer helpful?
0
0
Hide Solution
collegedunia
Verified By Collegedunia

Approach Solution -2

The correct syntax for inserting a foreign key constraint in a relation is ALTER TABLE table_name ADD FOREIGN KEY(attribute name) REFERENCES referenced_table_name(attribute name).

Additional Context:

  • Correct Syntax (Option 1):
    • Begins with ALTER TABLE to modify existing table
    • Uses ADD FOREIGN KEY clause
    • Specifies REFERENCES with target table and column
  • Example Implementation:
    ALTER TABLE orders 
    ADD FOREIGN KEY (customer_id) 
    REFERENCES customers(customer_id);
        
  • Why Other Options Fail:
    • Option 2: Incorrectly uses ADD TABLE
    • Option 3: Wrong clause order
    • Option 4: Invalid MODIFY TABLE command
  • Key Requirements:
    • Referenced column must be a primary key or unique
    • Data types must match
    • Can add ON DELETE/UPDATE rules

Correct Answer: (1) ALTER TABLE table_name ADD FOREIGN KEY(attribute name) REFERENCES referenced_table_name(attribute name).

Was this answer helpful?
0
0