Suppose we have class with a 100 student limit, we make a column StudentId
Column between (1-100) beyond this limit StudentId
is not generated
Create Table Class
(
StudentId Int Primary Key Identity(1,1)
StudentName Varchar(25)
)
insert into Class values('Jhon')
/* 2 ..
..
..
To 100 (Column) */
insert into Class values('Joy')
Record 101
insert into Class values('Joy')
--- When We insert 101 row a error will occur
3条答案
按热度按时间knsnq2tg1#
Add a CONSTRAINT to your
int
columngc0ot86w2#
One draw back about this is it doesn't guarantee, if the row are actually 100 if there is deletion. So you will have to do more in DO SOMETHING section
ncecgwcz3#
If you want to limit your
ClassTable
with 100 rows, you could create anafter insert
trigger. Since your column isidentity
column, you cannot rely on Id, because, you could be in a situation where you can't insert rows but you have less than 100 students. This usually occurs when you insert and delete rows. One way to solve this problem is byresetting
identity column withDBCC CHECKIDENT
command, which you do not want to be doing every time.