SQL Query To Create Table With Primary Key | Create Table Statement

Summary: In this tutorial, we will learn about SQL query to create table with Primary Key, and also we will understand the Primary Key(FK) role in DBMS. Let us understand SQL query to create table with Primary Key or Create Table Statement.

What is Primary Key in SQL?

The Primary Key(PK) is a field or a combination of fields that identify a record or data uniquely. In another word, we can say that the Primary key is a column or set of columns that are unique. i.e. every value is unique for Primary Key.

.
Important Rules For Primary Key:
Every table can have only one SQL Primary Key(PK).
All the values are unique and the PK SQL values can uniquely identify every row.
The system won’t permit inserting a row with SQL Server PK-Primary Key which already exists in the table.
Primary Key can’t be NULL.
The below diagram describes all the above points for the SQL Primary Key.

rules for primary key: create table with primary key

Create Table With Primary Key

You know very well that a primary key uniquely identifies each row table. It must contain unique values and can not contain non-NULL values. also, you know that a table can have only one primary key, which may consist of single or multiple fields. Furthermore, When multiple fields are used as a PK-primary key, they are known as composite keys.
To create a Primary key(PK) in the table, we have to use a keyword; “PRIMARY KEY ( )”

SQL Query | Create Table Statement

Create Table With Primary Key
To view whether “Stu_ID” is the primary key(PK) or not we use Describe command to view the structure of the Students Table.

How to Create Table With Primary Key in SQL Server

There are two ways:
1. SQL Server Management Studio
2. Transact-SQL (T-SQL): Create Table With SQL Query Statement

1. SQL Server Management Studio

Step 1) Right Click on the Table name. after that click on New table.
create table with primary key
Step 2) Here, enter Column Name which you want in table with Data Types and you can Allow Nulls values if you want otherwise do not allow nulls values.
Furthermore, as you can see that there are five-column such as Stu_ID, Name, Age, Phone_No, Address with different data types, and Address column can accept null values.
Table Diagram2
Step 3) Set Stu_ID as a Primary Key, Right Click on the Stu_ID, and then select Set Primary Key.
Table Diagram3
Step 4) Right Now Change your table Name which you want to change the name. Therefore, right click and select Save Table_1
Table Diagram4
Step 5) Enter table name which you want to change, and click on OK
Table Diagram5

2). Transact-SQL (T-SQL): Create Table With SQL Query Statement

The following given below is the syntax to create table with Primary Key(PK)from T-SQL

Syntax:
Furthermore, Now let’s create a Table with One Column as a SQL Primary Key(PK).

Create Table Statement
Step 1) Run the SQL Query by clicking on ‘Execute’
Table Diagram6
Result: You can see that Emd_Id is now a Primary Key.
Table Diagram7

Interesting Facts!

  • The Primary key(PK) can be a combination of multiple columns. Thus, This combination is called the Composite PK-primary key.
  • The Primary key can have a maximum of sixteen(16) columns.

Conclusion:

In this article, you learned everything concerning such as What is a Primary Key in SQL, and How to create table with Primary key. Furthermore, The primary key column always stores the unique value for each record in the table. Each table can have only one PK-Primary Key. All the values are unique which can uniquely identify or determine each row.I hope you will enjoy it!