How to Create a Table With a Foreign Key in SQL? - GeeksforGeeks

31 Aug, 2021
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve the data from relational databases like MySQL, Oracle, SQL Server, PostGre, etc.
In order to create a table with Foreign Key, we must have to know few Key Constraints:
Super Key :
An attribute, or set of attributes, that uniquely identifies a tuple within a relation.
Ex: Consider a customer database then,
The customer_id of the relation customer is sufficient to distinguish one tuple from another. Thus, customer_id is a super key. Similarly, the combination of customer_id and customer_name is a super key for the relation customer. Here the customer_name is not a super key, because several people may have the same name.
Candidate Key :
A super key such that no proper subset is a super key within the relation.
Ex: The combination of customer_name and customer_street is sufficient to distinguish the members of the customer relation. Then both, {customer_id} and {customer_name,customer_street} are candidate keys. Although customer_id and customer_name together can distinguish customer tuples, their combination does not form a candidate key , since the customer_id alone is a candidate key.
Primary Key :
The candidate key is selected to identify tuples uniquely within the relation. Out of all the available candidate keys, a database designer can identify a primary key. The candidate keys that are not selected as the primary key are called alternate keys.
Ex: For the customer relation, we can choose customer_id as the primary key.
Foreign Key :
Foreign keys represent the relationships between tables. A foreign key is a column (or a group of columns) whose values are derived from the primary key of some other table. The table in which a foreign key is defined is called a Foreign table or Details table. The table that defines the primary key and is referenced by the foreign key is called the Primary table or Master table.
We can add a foreign key to a relation in the following ways
Method-1 :
Syntax :
CREATE TABLE TABLE_NAME(
    Column 1 datatype,
    Column 2 datatype,
    Column 3 datatype FOREIGN KEY REFERENCES Table_name(Column name), //Column which has to be a forigen key
    ..
    ..
    Column n
)
Consider The following customer relationship
In order to create the following table, we use the following command
CREATE TABLE Customer(
    Customer_id int primary key,
    Customer_name varchar(20),
    Customer_Address varchar(20),
)
Now Consider the  Sales relation,
So in this relation, Customer_id is a foreign key that is obtained from the above customer relation. We can create the table using the following command.
CREATE TABLE SALES(
    Customer_id int FOREIGN KEY REFERENCES Customer(Customer_id)
    Item_id int,
    Payment_Mode varchar(20), 
)
Method -2 :
Syntax :
CREATE TABLE TABLE_NAME(
    Column 1 datatype,
    Column 2 datatype,
    Column 3 datatype  //Column which has to be a forigen key
    ..
    ..
    Column n
    CONSTRAINT Constraint_name FOREIGN KEY (Column name) REFERENCES Table_Name(Column name),
)
Now in order to create the same sales table using customer table we can run the following command
CREATE TABLE SALES(
    Customer_id int,
    Item_id int,
    Payment_Mode varchar(20),
    CONSTRAINT FK_Sales FOREIGN KEY (Customer_id)REFERENCES Customer(Customer_id)
)
Then we get the following table with customer_id as the foreign key.
DBMS-SQL
Picked
DBMS
SQL
DBMS
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Types of Functional dependencies in DBMS
KDD Process in Data Mining
What is Temporary Table in SQL?
Conflict Serializability in DBMS
Deadlock in DBMS
SQL | DDL, DQL, DML, DCL and TCL Commands
How to find Nth highest salary from a table
SQL | ALTER (RENAME)
How to Update Multiple Columns in Single Update Statement in SQL?
How to Create a Table With Multiple Foreign Keys in SQL?