Skip to main content

Creating Primary Key without Clustered Index

Create a Table;

Create table Test_tbl
(
Id int,
Name varchar (50)
)

Create a clustered index on this table before creating a primary key constraint on it;

Create Clustered Index Index_Name on Test_tbl(Name)


Now create a primary key as below;

Alter table Test_tbl add constraint Primary key (Id)

Now check the type  of primary key Using this query as below;  

Select object_name (object_id) as tablename, type_desc, Name, index_id, type from sys.indexes where object_name(object_id) = 'Test_tbl'

1. Clustered Index : 
Clustered index is created only when both the following conditions satisfy – 

  1. The data or file, that you are moving into secondary memory should be in sequential or sorted order.
  2. There should be a key value, meaning it can not have repeated values. 
     

Whenever you apply clustered indexing in a table, it will perform sorting in that table only. You can create only one clustered index in a table like primary key. Clustered index is as same as dictionary where the data is arranged by alphabetical order. 

In clustered index, index contains pointer to block but not direct data. 
 

Example of Clustered Index – 
If you apply primary key to any column, then automatically it will become clustered index. 
 

create table Student
( Roll_No int primary key, 
Name varchar(50), 
Gender varchar(30), 
Mob_No bigint );

insert into Student
values (4, 'ankita', 'female', 9876543210 );

insert into Student 
values (3, 'anita', 'female', 9675432890 );

insert into Student 
values (5, 'mahima', 'female', 8976453201 ); 

In this example, Roll no is a primary key, it will automatically act as a clustered index. 
The output of this code will produce in increasing order of roll no. 

 

Roll_NoNameGenderMob_No
3anitafemale9675432890
4ankitafemale9876543210
5mahimafemale8976453201

You can have only one clustered index in one table, but you can have one clustered index on multiple columns, and that type of index is called composite index. 

2. Non-clustered Index : 
Non-Clustered Index is similar to the index of a book. The index of a book consists of a chapter name and page number, if you want to read any topic or chapter then you can directly go to that page by using index of that book. No need to go through each and every page of a book. 

The data is stored in one place, and index is stored in another place. Since, the data and non-clustered index is stored separately, then you can have multiple non-clustered index in a table. 

In non-clustered index, index contains the pointer to data. 

 

Example of Non-clustered Index – 

create table Student
( Roll_No int primary key, 
Name varchar(50), 
Gender varchar(30), 
Mob_No bigint );

insert into Student 
values (4, 'afzal', 'male', 9876543210 );

insert into Student 
values (3, 'sudhir', 'male', 9675432890 );

insert into Student 
values (5, 'zoya', 'female', 8976453201 );

create nonclustered index NIX_FTE_Name
on Student (Name ASC); 

Here, roll no is a primary key, hence there is automatically a clustered index. 
If we want to apply non-clustered index in NAME column (in ascending order), then the new table will be created for that column. 

Output before applying non-clustered index : 
 

Roll_NoNameGenderMob_No
3sudhirmale9675432890
4afzalmale9876543210
5zoyafemale8976453201

Output after applying non-clustered index : 

 

NameRow address
Afzal3452
Sudhir5643
zoya9876

Row address is used because, if someone wants to search the data for sudhir, then by using the row address he/she will directly go to that row address and can fetch the data directly. 

Difference between Clustered and Non-clustered index : 

CLUSTERED INDEXNON-CLUSTERED INDEX
Clustered index is faster.Non-clustered index is slower.
Clustered index requires less memory for operations.Non-Clustered index requires more memory for operations.
In clustered index, index is the main data.In Non-Clustered index, index is the copy of data.
A table can have only one clustered index.A table can have multiple non-clustered index.
Clustered index has inherent ability of storing data on the disk.Non-Clustered index does not have inherent ability of storing data on the disk.
Clustered index store pointers to block not data.Non-Clustered index store both value and a pointer to actual row that holds data.
In Clustered index leaf nodes are actual data itself.In Non-Clustered index leaf nodes are not the actual data itself rather they only contains included columns.
In Clustered index, Clustered key defines order of data within table.In Non-Clustered index, index key defines order of data within index.
A Clustered index is a type of index in which table records are physically reordered to match the index.A Non-Clustered index is a special type of index in which logical order of index does not match physical stored order of the rows on disk.
The size of clustered index is large.Size of non-clustered index is comparatively smaller.
Primary Keys of the table by default are clustered index.Composite key when used with unique constraints of the table act as non-clustered index.

Comments

Popular posts from this blog

Select Names from table which have vowels

  Problem Query the list of  CITY  names from  table  which have vowels (i.e.,  a ,  e ,  i ,  o , and  u ) as both their first  and  last characters. Your result cannot contain duplicates. Input Format The  STATION  table is described as follows: Field Type ID NUMBER CITY VARCHAR2(21) STATE VARCHAR2(2) LAT_N NUMBER LONG_W NUMBER STATION where  LAT_N  is the northern latitude and  LONG_W  is the western longitude. MYSQL select distinct city from station where (city like 'a%' or city like 'e%' or city like 'i%' or city like 'o%' or city like 'u%' ) and ( city like '%a' or city like '%e' or city like '%i' or city like '%o' or city like '%u' )

Write a query that prints a list of employee names for employees in Employee having a salary greater than 2000

  Write a query that prints a list of employee names (i.e.: the  name  attribute) for employees in  Employee  having a salary greater than  2000  per month who have been employees for less than  10  months. Sort your result by ascending  employee_id . Input Format The  Employee  table containing employee data for a company is described as follows: Column Type employee_id Integer name String months Integer salary Integer where  employee_id  is an employee’s ID number,  name  is their name,  months  is the total number of months they’ve been working for the company, and  salary  is the their monthly salary. Sample Input employee_id name months salary 12228 Rose 15 1968 33645 Angela 1 3443 45692 Frank 17 1608 56118 Patrick 7 1345 59725 Lisa 11 2330 74197 Kimberly 16 4372 78454 Bonnie 8 1771 83565 Michael 6 2017 98607 Todd 5 3396 99989 Joe 9 3573 Sample Output Angela Michael Todd Joe Explanat...

SQL SERVER – Fix : Error 1702 CREATE TABLE failed because column in table exceeds the maximum of columns

  Error 1702 CREATE TABLE failed because column in table exceeds the maximum of columns SQL Server 2000 supports table with maximum 1024 columns. This errors happens when we try to create table with 1024 columns or try to add columns to table which exceeds more than 1024. Fix/Solution/WorkAround: Reduce the number of columns in the table to 1,024 or less