How do you declare a foreign key referencing another_table(id)?

Study for the SQL Basics Test. Improve your knowledge with multiple choice questions and detailed explanations. Prepare effectively to master SQL concepts!

Multiple Choice

How do you declare a foreign key referencing another_table(id)?

Explanation:
Declaring a foreign key constraint to enforce referential integrity between tables. The proper form specifies the local column in parentheses after FOREIGN KEY, then points to the target table and its referenced column with REFERENCES. So the correct syntax is FOREIGN KEY (column_name) REFERENCES other_table(id). This ensures that any value entered in column_name must exist in other_table.id, tying the two tables together through a valid reference (the referenced column is typically a primary key or has a unique constraint). In a CREATE TABLE statement, this can be defined as a separate constraint or inline with the column definition, but the key idea is clearly indicating the local column and the referenced column. The other forms fail because they omit the required parentheses around the local column or omit the specific referenced column, or present an incomplete constraint.

Declaring a foreign key constraint to enforce referential integrity between tables. The proper form specifies the local column in parentheses after FOREIGN KEY, then points to the target table and its referenced column with REFERENCES. So the correct syntax is FOREIGN KEY (column_name) REFERENCES other_table(id). This ensures that any value entered in column_name must exist in other_table.id, tying the two tables together through a valid reference (the referenced column is typically a primary key or has a unique constraint). In a CREATE TABLE statement, this can be defined as a separate constraint or inline with the column definition, but the key idea is clearly indicating the local column and the referenced column. The other forms fail because they omit the required parentheses around the local column or omit the specific referenced column, or present an incomplete constraint.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy