Home > DeveloperSection > Forums > What exactly is happening when we make a field a primary key
Anupam Mishra

Total Post:135

Points:949
Posted on    February-08-2016 11:39 PM

 Database Database  Database Design  Database Performance  Database Security 
Ratings:


 1 Reply(s)
 392  View(s)
Rate this:
Hi Everyone,
I want know, when we make any field a primary key in a table then what is happening internally in database.
Please can anyone give me a solution.
Thank you.



Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    February-09-2016 10:35 PM

Primary Key:  Primary key is a key that is selected as the primary key. Only one key within an entity is selected to be the primary key. This is the key that is allowed to migrate to other entities to define the relationships that exist among the entities. When the data model is instantiated into a physical database, it is the key that the system uses the most when accessing the table, or joining the tables together when selecting data.[WIKI]  When we creating a primary key in a table such as below:

SQL> CREATE TABLE STUDENTS (  
ID INT                           NOT NULL,  
NAME VARCHAR (20) NOT NULL,  
AGE INT    NOT NULL,  
ADDRESS CHAR (25),  
PRIMARY KEY (ID)  
);  
Here ID is a primary key. It will internally  create a clustered index in a database.
A clustered index will be created for that specific field.A clustered index sorts and stores the data rows in the table based on the index key values. Therefore only one clustered index can be created on each table because the data rows themselves can only be sorted in one order. A clustered index sorts and stores the data rows in the table based on the index key values. Therefore only one clustered index can be created on each table because the data rows        
themselves can only be sorted in one order.
 To create a different clustered index on the same table, change the Create as Clustered property setting on the existing clustered index before creating the second index.

Don't want to miss updates? Please click the below button!

Follow MindStick