What's new

Closed Sa magagaling dyan oh help naman

Status
Not open for further replies.

PHC_Jayvee

nakatapos din sa research😂🤣
Joined
Apr 14, 2018
Posts
11,351
Solutions
64
Reaction
13,372
Points
3,717
Pa help naman po kac may ginagawa ako sarili panel. Yung Id nya ay naka auto increment pero pag nag delete ako diretso pa Rin sya

Example Yung id sa database ay 1,2,3
Pag denelete ko si 3 Yung kasunod na I'd na ay 4
Gusto ko po sana kapag ang id sa database ay 1,2,3 pag denelete ko si 3, 3 padin kasunod pag nag add ako

Gusto ko po sana Wala laktaw Yung number kahit mag delete pa


Thank you sa tutulong
 
Worried kabang maubusan ng integer at ayaw mo ng walang laktaw? From data integrity point of view, hindi mo dapat "duktorin" yung sequence. Deletion is an event and should be reflected as such. Leave it as it is.
 
Ahh Ganon po ba sorry!!

Eh tanong ko nalang po pano maglagay ng consecutive number sa table
 
I found this question similar to the other thread. haha! mukang magka group kayo sa thesis ah? joke! Honestly, your question is way more rational than the other thread and its more realistic and can be done with a less of concern on data integrity. For the purpose of giving you a favor, you can do something like this:

-When adding a record, first you have to fetch for the last ID that have been inserted.
-Upon fetching, write a logic either from the frontend or backend(sql) itself that adds 1 to the fetched data(ID field).
-Then use this to insert a new record.
-Done.

How is that translated into SQL?

SQL:
DECLARE @newID varchar(10);
DECLARE @newIncrementedID int;

SET @newIncrementedID = (SELECT MAX(idnumber) FROM dbo.mytable) + 1;
SET @newID = RIGHT('00000'+ CONVERT(VARCHAR(6),@newIncrementedID),6)

The line : SET @newIncrementedID = (SELECT MAX(idnumber) FROM dbo.mytable) + 1;

You can alternatively write something like this:
SET @newIncrementedID = (SELECT TOP 1 idnumber ORDER BY idnumber DESC FROM dbo.mytable) + 1;

With this approach you have to write these codes in an T-SQL Function that returns the newly incremented ID number.

You can also write similar a simple query to just fetch the last code then do the increment logic through the frontend if you are not that familiar writing T-SQL Queries.

Happy Coding!
 
Last edited:
Worried kabang maubusan ng integer at ayaw mo ng walang laktaw? From data integrity point of view, hindi mo dapat "duktorin" yung sequence. Deletion is an event and should be reflected as such. Leave it as it is.

This is very correct. In fact as much as possible hindi mo dapat iedit ang isang Unique field like ID Number or Code Number ng isang record. It violates the data integrity and it's essential purpose for serving as record's identity. Siguro mga studyante ang mga ito kaya hindi pa nila alam yung principles at best practices.
 
Status
Not open for further replies.

Similar threads

Back
Top