Syntax

SQL Add a Foreign Key Syntax

In SQL, add a foreign key: links a column to another table — ALTER TABLE orders ADD FOREIGN KEY (user_id) REFERENCES users (id);

Quick Answer

Syntax
ALTER TABLE orders ADD FOREIGN KEY (user_id) REFERENCES users (id);
SQLSyntax

Add a Foreign Key

Code example
ALTER TABLE orders ADD FOREIGN KEY (user_id) REFERENCES users (id);

Links a column to another table.

Learn More

How to add a foreign key in SQL

  1. Open your SQL file or editor.
  2. Copy the example, then replace its sample names and values for your code.

SQL Add a Foreign Key Syntax

Use ALTER TABLE orders ADD FOREIGN KEY (user_id) REFERENCES users (id);.

Related Syntax References

Frequently asked questions

What is the SQL syntax for add a foreign key?

Use this pattern: ALTER TABLE orders ADD FOREIGN KEY (user_id) REFERENCES users (id);

Can I copy this SQL code example?

Yes. Use Copy code and adapt the example to your program.

Is this SQL syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify