
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to refer primary key as Foreign to various table in MySQL?
Following is the syntax −
alter table yourSecondTableName add constraint `yourConstraintName` foreign key(`yourSecondTableNamePrimaryKey`) references yourFirstTableName(yourFirstTablePrimaryKeyColumnName);
To understand the above syntax, let us first create a table −
mysql> create table demo65 −> ( −> id int not null primary key, −> name varchar(20) −> ); Query OK, 0 rows affected (0.57 sec)
Following is the query to create second table −
mysql> create table demo66 −> ( −> user_id int not null primary key, −> address varchar(200) −> ); Query OK, 0 rows affected (1.80 sec)
Following is the query to refer primary key as a foreign key −
mysql> alter table demo66 −> add constraint `id_fk` −> foreign key(`user_id`) −> references demo65(id); Query OK, 0 rows affected (3.76 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the overall description of the table using SHOW CREATE TABLE command. Following is the query −
mysql> show create table demo66;
This will produce the following output −
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table +--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | demo66 | CREATE TABLE `demo66` ( `user_id` int NOT NULL, `address` varchar(200) DEFAULT NULL, PRIMARY KEY (`user_id`), CONSTRAINT `id_fk` FOREIGN KEY (`user_id`) REFERENCES `demo65` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- How to make MySQL table primary key auto increment?
- How to get primary key of a table in MySQL?
- How to reset the primary key of a table in mysql?
- How to identify composite primary key in any MySQL database table?
- Difference between Primary key and Foreign key in Database
- Difference Between Primary key and Foreign key in DBMS
- How to use Primary Key Constraints and Foreign Key Constraints to enforce database integrity in Oracle?
- ALTER TABLE to add a composite primary key in MySQL?
- Difference between Primary key and Foreign key in SQL Database
- How to identify foreign key in MySQL DB?
- Set existing column as Primary Key in MySQL?
- MySQL Syntax to create Foreign Key?
- How to get the primary key “column name” of a specific table in MySQL?
- How can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?
- Can we remove a primary key from MySQL table?

Advertisements