DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Copy SQL Execution Plan from One Database to Another in Oracle 19c
  • Exploring the New Boolean Data Type in Oracle 23c AI
  • Oracle SQL: Understanding Execution Plan and Performance Tuning
  • Fixing Common Oracle Database Problems

Trending

  • The Smart Way to Talk to Your Database: Why Hybrid API + NL2SQL Wins
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • Event-Driven Architectures: Designing Scalable and Resilient Cloud Solutions
  • Medallion Architecture: Why You Need It and How To Implement It With ClickHouse
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Create a Foreign Key on a Table in Oracle SQL

How to Create a Foreign Key on a Table in Oracle SQL

There are two ways to create a foreign key on a table in Oracle: the inline method and the out-of-line method. I’ll explain them in this article for you.

By 
Ben Brumm user avatar
Ben Brumm
·
Nov. 21, 17 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
300.2K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, I’ll explain how to create a foreign key on a table in Oracle SQL.

What Is a Foreign Key?

First, let’s explain what a foreign key is.

A foreign key is a constraint that’s added to a table. It allows you to specify that a column in a table refers to the primary key of another table.

It’s used to relate data in two tables and improve the integrity of your data.

There are two ways to create a foreign key on a table in Oracle: the inline method and the out-of-line method. I’ve documented them both in my guide to the CREATE TABLE statement here and I’ll explain them in this article for you.

Foreign Key Using an Inline Constraint

To create a foreign key using the inline constraint syntax, use a command like this:

CREATE table_name (
column_name data_type REFERENCES other_table_name (other_column_name)
…
);

Using this syntax, you specify your CREATE keyword, then the table name, then open brackets. For the column, you want to specify as the foreign key, and add the word REFERENCES to the end (after the data type).

Then, specify the name of the other table. This is the table that contains the primary key that your new foreign key will link to. For example, if you have a department table, and you’re creating a new employee table that has a department ID field in it, your “other table” would be the department table.

Next, specify the name of the column that the foreign key refers to in the other table, inside brackets. If we use the employee and department table, this would be the department_id column (or whatever you have called the primary key in the department table).

Finally, continue to define your columns and table as normal. Your foreign key will be created when you run the statement.

Foreign Key Using an Out-of-Line Constraint

The other way that you can declare a foreign key is using the out-of-line constraint method.

This is done by declaring a constraint after all of your columns have been declared.

The advantage of this method is that you can give your foreign key constraint a name, which is helpful for altering, enabling, and disabling it at a later date. Using the inline method, a name is generated automatically by the Oracle database.

The syntax for creating a foreign key using the out-of-line method is:

CREATE table_name (
column_name data_type,
...
CONSTRAINT fk_tbl1_tbl2
  FOREIGN KEY (this_tables_column)
  REFERENCES other_table_name (other_column_name)
);

You start by declaring your table name and all of your columns as required.

After all of the columns are declared, but are still inside the brackets, you add the word CONSTRAINT. This denotes that you will be defining a constraint.

Next, provide a name for your new constraint. This can be up to 30 characters. I prefer to stick to a standard when naming my constraints, so I start the names with fk.

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. Using the example above, if you’re creating an employee table and want a department_id to refer to the department table, this would be department table. 

Then, open the brackets, add in the name of the column in the other table (in this case, the department table), and then close the brackets.

Finally, close the brackets to the create table statement and add a semicolon. The statement is now ready to run. The foreign key will be created with the name you have specified.

More information on foreign key constraint parameters is available in the Oracle documentation.

So, that’s how you can create a foreign key on a new table. Leave any questions in the comments!

Database sql Oracle Database

Opinions expressed by DZone contributors are their own.

Related

  • Copy SQL Execution Plan from One Database to Another in Oracle 19c
  • Exploring the New Boolean Data Type in Oracle 23c AI
  • Oracle SQL: Understanding Execution Plan and Performance Tuning
  • Fixing Common Oracle Database Problems

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!