Identity column in SQL-SERVER

  1. --Create Student  

  2. if object_id('Student'is null  
  3. create table Student(id tinyint identity(1,1) ,Name varchar(20),Marks int)  
  4.   
  5. --If we observe above Create table statement the Id column is identity column and its data type is tinyint.  
  6.             We know that tinyint accepts 0 to 255 numbers range.  
  7. --insert data into Student table.  

  8. Declare @start int=1  
  9. while (@start<=256)  
  10. begin  
  11. insert into Student(Name,Marks) values('Rakesh',500)  
  12. set @start=@start+1  
  13. end  

  14. --In the above while loop we are trying to insert same record in 256 time. The identity column automatically supplied value. The 1-255 records gets inserted successfully.256 record get an error because of tinyint accepts 0-255 records only.  
  15. (1 row(s) affected)  
  16.   
  17. (1 row(s) affected)