- --Create Student
- if object_id('Student') is null
- create table Student(id tinyint identity(1,1) ,Name varchar(20),Marks int)
- --If we observe above Create table statement the Id column is identity column and its data type is tinyint.
- We know that tinyint accepts 0 to 255 numbers range.
- --insert data into Student table.
- Declare @start int=1
- while (@start<=256)
- begin
- insert into Student(Name,Marks) values('Rakesh',500)
- set @start=@start+1
- end
- --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.
- (1 row(s) affected)
- (1 row(s) affected)
In this code we will learn about Identity column in SQL-SERVER which is automatically insert the value in identity column whenever new record gets inserted into the table.
