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.
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.
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
1 2 3 4 5 6 7 |
CREATE TABLE Students ( Stu_ID VARCHAR(20) NOT NULL , Name VARCHAR(50) NOT NULL , Age INT NOT NULL , Phone_No VARCHAR(10) NOT NULL , Address VARCHAR(100) NOT NULL , PRIMARY KEY (Stu_ID)); |
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.
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.
Step 3) Set Stu_ID
as a Primary Key, Right Click on the Stu_ID
, and then select Set Primary Key.
Step 4) Right Now Change your table Name which you want to change the name. Therefore, right click and select Save Table_1
Step 5) Enter table name which you want to change, and click on OK
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:
1 2 3 4 5 |
CREATE TABLE <Table_Name>( Column1 datatype, Column2 datatype, CONSTRAINT <Name> PRIMARY KEY (Column name); ); |
Create Table Statement
1 2 3 4 5 6 7 |
CREATE TABLE Customers ( Emd_ID INT NOT NULL , Emp_Name VARCHAR(50) NOT NULL , Age INT NOT NULL , Phone_No VARCHAR(10) NOT NULL , Address VARCHAR(100) NOT NULL , CONSTRAINT PK PRIMARY KEY (Emd_ID)); |
‘Execute’
Result: You can see that
Emd_Id
is now a Primary Key.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!