AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

SQL FOREIGN KEY


<< Previous Page
SQL tutorial
Next Page >>
SQL tutorial

SQL Tutorial » SQL FOREIGN KEY

What is an SQL foreign key?

A foreign key is a key used to link two tables together.

A FOREIGN KEY is one or more or collection of fields in one table, that refers to the PRIMARY KEY in another table.

The table with the foreign key is named child table, and the table with the primary key is named parent table.

 

SQL FOREIGN KEY on CREATE TABLE

The following SQL creates a FOREIGN KEY on the "ID_column" column when the "my_table_name1" table is created:

CREATE TABLE my_table_name1 (
ID_column1 int NOT NULL,
Number_column1 int NOT NULL,
ID_column_table2 int,
PRIMARY KEY (ID_column1),
FOREIGN KEY (ID_column_table2) REFERENCES my_table_name2(ID_column_table2)
);

Related subjects:
How to create database phpmyadmin? SQL create table PHP select / view data SQL primary key SQL alter table

Tags: sql foreign key: constraint failed, as primary key, vs primary key, on delete cascade, multiple tables, cascade, constraint is incorrectly formed, index postgresql foreign key
mysql foreign key, sql foreign key on delete, add foreign key mysql, alter table add foreign key, primary key and foreign key examples,
What is an SQL foreign key?
How foreign key is implemented in SQL?
Where is foreign key in SQL?
How does a foreign key work?

 

SQL FOREIGN KEY on ALTER TABLE

To create a FOREIGN KEY constraint on the "ID_column" column when the "my_table_name1" table is already created, use the following SQL:

ALTER TABLE my_table_name1
ADD FOREIGN KEY (ID_column2) REFERENCES my_table_name1(ID_column2);

To allow FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the next SQL syntax:

ALTER TABLE my_table_name1
ADD CONSTRAINT FK_my_table_name2
FOREIGN KEY (ID_column2) REFERENCES Persons(ID_column2);

 

DROP a FOREIGN KEY Constraint

To drop a FOREIGN KEY constraint, use the following SQL:

ALTER TABLE my_table_name1
DROP FOREIGN KEY FK_name_column2;

 



sql foreign key: constraint failed, as primary key, vs primary key, on delete cascade, multiple tables, cascade, constraint is incorrectly formed, index postgresql foreign key
mysql foreign key, sql foreign key on delete, add foreign key mysql, alter table add foreign key, primary key and foreign key examples,
What is an SQL foreign key?
How foreign key is implemented in SQL?
Where is foreign key in SQL?
How does a foreign key work?

SQL FOREIGN KEY - sql tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home - Database
SQL - Database
SQL Syntax
SQL Select Data
SQL Insert Data
SQL Update
SQL Delete Row
PHP mysqli_query()
SQL order
SQL Create Database
SQL Display Data
SQL Count()
SQL mysqli_connect
SQL create table
SQL Where And & Or
SQL like Syntax
SQL Between Operator
SQL IN Operator
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...