Primary Key SQL : Foreign & Unique key MySQL with Table Example

Posted on

Every developer wants to know about Primary Key SQL. By the way, In MySQL table already existing primary key fields. Thus in these fields, constraints MySQL primary key is unique for each row and also, discusses foreign key SQL.

Over there, an option for autoincrement primary key. Thereby, when inserting any data in the table row than primary key autoincrements a unique key. As a rule, we also called the primary key.

Here we define all the below sections that how to implement primary keys in SQL. Coupled with, other fields and also another table using the constraints primary key in SQL with the example.

Types of the following content we discuss

  • Primary key SQL
  • MySQL primary key
  • Create a table with primary key SQLSQL composite primary key
  • Foreign key in SQL
  • SQL create table primary key autoincrement
  • Unique key in SQL
  • Primary key example
  • Alter table add primary key oracle
  • SQL server add a primary key to an existing table with data
  • What is a primary key and foreign key
  • Candidate key in SQL


Primary Key SQL

In brief, the key primary is uniquely identified data records in rows of the database table. Afterward, The primary key must Endue unique value.

Here the primary key column cannot contain null values. Therefore, ultimate tables should be a primary key. Also and every table can be only one unique key. Also, learn the code about Python Pass

Accordingly, each table should have a primary key. Therefore, because we identify to rows for the records in the table. So then it is mandatory to assign a field for the primary key.

By the way, we know maximum developers are confusing that primary key and unique key. As a rule, the Primary key is not to accept Null value but the unique key is to accept Null value in the constraints in SQL database table.

Here, Student Table Example

RollNameclassContact No

Note:- As an example, there is a student table and also has multiple student data records rows in this table. Consequently, there are four filed in which heading of the student data records.

Afterward, fields name Roll no, name, class, and contact number. Here these are all student data but find the table unique value of the column. Therefore, the Student table one column is unique in all field records.

Accordingly, a roll number is a primary key and other column records should be the same but a unique key should be not inserted the same records in the database table.

Also, Primary Keys in SQL Query



Roll integer (15) NOT NULL PRIMARY KEY,

Name varchar (255),

Class varchar (255),

Contact No varchar (255),


Create a Table with Key SQL

First of all, We have to create a table in MySQL database. In contrast, we define all steps with the example that how to create a table and each point in which the SQL server.

Also, there is one point more ‘unique’ option likewise, the primary key. Similarly, we are inserting a record in a table than if you have to assign a unique column. So, record not be insert in the table if records already inserting.

As an example, create a database in databases and then create a table as you want. Therefore, the sections giving you an example step by step so follow these steps for creating a table with a database query.

Hence, Customer Table

IdCustomer NameCountryYear
2HarryUnited State2018

Here Create table MySQL Query by code



Id integer (15) NOT NULL PRIMARY KEY,

Customer Name varchar (255),

Country varchar (255),

Year varchar (255),


Here insert query for record inserting in a table by code

INSERT INTO Customer ( Id, customer name, country, year ) values ( 1, 'Ronny', 'India', 2020 );

Also, Select Query by code

SELECT * FROM Customer;

It seems like, We have to show the keys in SQL and also MySQL database structure show with the graphics and the explain step by step for creating a database. Also, creating a SQL table with some inserting records of data in the table.

Primary Key Example

First of all, create a database in MySQL

Step1:- Finally, Go to the PHPMyAdmin dashboard and click the ‘new‘ button of the left side top sidebar. Also then you visible a database heading in the below all, section see ‘Name field’.

Here, click and then write the database name to create the database name. Afterward, go to show the button on the right-side button name is “Create”. Finally, click the button and the database will be created.

primary key sql

Step2:- Afterward, Visible a new window which will be for creating a table. likewise, you can write a table name as you want in this field and also, click the “Go” button it is visible on the right side. As a result, the table will be created.

Note:- Hence, there is another field that will show a number of columns as you want. Therefore, As wed know we create table examples all the above section. So, there have customer details likewise, Id, Customer Name, Country, Year.

So, you can insert column quantity and then click the “Go” button.

primary key sql

keys in SQL

Step3:- Finally, an again visible new window with the column. Here you will do create a column. So, there are showing multiple fields for the column so insert the name of a column and column type.

furthermore, there is a more type of column type. So, if you want to text field should be select “VARCHAR” option and wants to integer values then select “INT“.

Hence, there is another field equal to that. Similarly, fields name “Length/Values” it is necessary to put the length of fields likewise, text fields name ‘255’ and INT ’15’. Here is a common value of fields and other select options like a date so on.

Also, there is another checked field option. So, that will be autoincremented to make the primary key option name is selected “A.I“.

primary key SQL

SQL Composite Key

First of all, the Composite primary key refers to more than one column is used to the primary key of the table. Even more, the foreign key will also need to insert all the columns in the composite key.

Here we describe the composite key a table refers to another table more than one column all colled to the composite key.

SQL Create Table Autoincrement

similarly, as we discuss above all sections with an example that how to create a table but there did not discuss to autoincrement. So, let start with all steps to create a table under key autoincrement.

Step1:- Here goto PHPmy admin dashboard and firstly create a database. Afterward, create a table with the name and then the next process to start to insert the all column name, type, and length.

So, there is a checkbox option namely “A.I” Autoincrement than select this option after that click the go button. Therefore, the table will be created.

As an example, I have to show the visible autoincrement checkbox section. So, you can find the below image and follow it.

Foreign Key in SQL

Hence, A foreign key in one table points to a  primary key in another table. similarly, the foreign key is used to include two tables together. So, a table primary key refers to another table that is a foreign key.

Here, as we know the primary key used by a foreign key is also knows as a parent key. Almost, the table where the primary key is from is known as a parent table( Child table ).

Therefore, the foreign key can be used to make sure that the row in one table has the corresponding row in another table. Also, a foreign key value can be null. Similarly, the primary key value can not be null.

Similarly, a foreign key cannot be unique in the column of the child table. Here, the primary key must be unique in the parent table.

Here, a customer table

2HarryUnited State2018

Also, the Department table

  • It seems like, above a customer table and there is a column of the primary key in the table namely ‘CustomerID’ in the parent table.
  • Similarly, The other department table has a column which is foreign key namely ‘CustomerID‘ in the child table.

Unique Key in SQL

Here, a unique key is a set of options fields it is identified uniquely records in a column of a table. Above all, we have to guide about keys also given an example for unique.

So, now I describe what is a unique key and how to use unique and define with database query in the table. While when we define a unique key in a particular column. So, it does not insert records only resort unique value records in the table.

Note:- First of all, the unique key null value can be accepted in the table. Also, learn more about this Woocommerce Shortcodes

CREATE TABLE Customer_table


Id integer (15) NOT NULL PRIMARY KEY,

Customer Name varchar (255),

Country varchar (255),

Number varchar (255) UNIQUE KEY,


Alter Table Add Key Oracle ( SQL drop Foreign Key )

Afterward, as we know the key is not null and we use another table for the same therefore we are using an alter table.

So, I have to give a query relative to the alter table and also show the table name that how to implement the alter table in the database.


What are a Primary Key and Foreign Key

Similarly, as we know about both key in the above all section that main different primary and foreign key. So, let start with the primary key is to identify unique records in a column of tables.

Also, the foreign key is a primary from the parent table but another table refers to this key colled to the foreign key. it cannot be unique records in the column of the child table.

Candidate Key in SQL

Here, the candidate key is can be every column of combination in a column is a unique key in the database. So, it means candidate key in a child table every field accepted and relative to primary key refers.

Also,  there can be many candidate values to identify the different columns in the database table.


Finally, We guide to all relative topics of the primary key. So, whenever if have any queries you can ask to drop your issues in the comment box.

Even more, you want to know regarding this topic follow it Relational Algebra. Also, we guide to DBMS all data structure that how to implement create the database table and Union Symbol How to work database table.

One Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts

Related Post

How to Display Image in PHP from Folder/Directory

Here, I have to show the code display image in PHP from folder. As well as, might get image from the database. After that, you might see some examples of the list of all images in folder and subfolders from the directory and folder using PHP. Display Image in Php from Folder Hence, I have […]

Read more

How to Store and Retrieve Image from database in Php

So, I am going to explain and showing you the best method to store and retrieve image from database in PHP. And about with the code of upload and fetch an image from the MySQL database using PHP. As well as, when the insert image in MySQL database then with upload image a specific folder. […]

Read more

How to Insert JSON data into MySQL using PHP

You might see every method to insert JSON data into MySQL using PHP. So, we explain about insert multiple JSON data with an example of code in PHP. Here, we implement all points step by step using PHP. Also, you can see how to store JSON data in the MySQL database with an example. How […]

Read more