Automatic id assign - vb.net

i'm a student and im having problems using the automatic increment because when i delete a row it will continue to increment. explaining:
i want to increment id automaticly
so:
id name age
1 michael 18
2 katy 17
3 jack 20
now i delete row 3 and when i click in the button new it'll go to the id 4 instead of id 3
i'v tried rows.count and refresh the textbox but nothing
some adicional info
ds= dataset
maxrows = ds.Tables("virtualtable").Rows.Count
idcliTextBox.Text = maxrows
how do i make it set id to the real last row?

It is the correct behavior and it is not a problem. Usually the autoincrement columns in a database are never reset to accomodate for empty holes caused by deletion of previous inserted records.
The autoincrement column is usually used as primary key to uniquely identify a single record in your table.
Suppose that your table represents students where the ID field value is used as foreign key for another table examresults. In this table you store the exam result of your students. Your student Katy (2) has two records in the examresults table for the graduation in math and geography.
If you delete the record with ID=2 from the table students and the related records from examresults changing the record for Jack from 3 to 2 means that you need to change also the related records for examresults of Jack. This is very impractical and useless if you think about it.

Related

Handling CustomField Insert - Which option is efficient and easier to maintain

We have the following Table structure:
User Table
UserId CompanyId FullName Email
1 1 Alex alex#alex.com
2 1 Sam sam#sam.com
3 2 Rohit rohit#rohit.com
CustomField Table
CustomFieldId CompanyId Name Type
1 1 DOB Datetime
2 1 CompanySize Number
3 2 LandingPage Text
CustomFieldValue Table
UserId CustomFieldId DatetimeValue NumberValue TextValue
1 2 01-01-2020
1 2 10
1 3 Home
2 1
2 2 20
2 3 Product
Please consider the following facts:
There are millions of users in a particular CompanyId
When displaying a particular user in the UI we need to show all the Custom Fields that an end customer can fill up.
How to handle CustomFieldValues table in this case? We are considering the following options
When a new CustomField row is created for a particular CompanyId have a After Insert Trigger to create all corresponding rows in CustomFieldValue table for all users.
This I think would have an initial cost of creating so many rows for each Custom Field in the CustomFieldValue Table. (This may also lock up the table and users of the application would have to wait till all the inserts are done).
Same issue for deleting all CustomFieldValue rows when a CustomField row is deleted from a Company
But easier for UI and backend developers as they don't need to worry about whether a CustomFieldValue doesn't have an entry for a Custom Field that has been created for a Company
Don't create CustomFieldValue rows when a CustomField is added to the Company. Create the CustomFieldValue whenever user fills up the relevant input field in the UI view
This would have negligible insert cost and users would not have to wait for insert or delete to complete in CustomFieldValue table for all the users in a particular company.
The downside is that developers would have to make sure that relevant CustomFields are displayed in the frontend even though no relevant records yet exist in the CustomFieldValue table.
On each Custom Field input update by the end user, the developers would have to first check if a corresponding CustomFieldValue row exits, if so - store the updated value, if not - create the CustomFieldValue row.
Kindly suggest a solution which is efficient and easier to maintain.

Continue with Id order values after delete some rows

I accidentally deleted some rows from a table, it's just 4 rows, but now when I try to insert the same values again the Id values are different, it's like it's remembering the Id values that had the rows I deleted before and now every time I insert a row the value generated for Id is not the next to the existent sequence... For example:
Id Name
1 Peter
2 Luis
3 Charles
4 John
Let's say I deleted rows with Name value Peter, Luis, Charles and John. When I try to insert the same names again it inserts them but with a different Id..
Id Name
1 Peter
5 Luis
6 Charles
7 John
I can not change the Identity value manually to the old values.. Which is the best solution in this case?
The reason is that auto-incrementing Id's don't automatically reset. This is by design, as you may have the same values references in other tables, which you may not want to delete (although it creates orphaned rows).
The method of resetting the auto-incrementing value differs between databases. For instance, in MySQL, you can run:
ALTER TABLE myTable SET AUTO_INCREMENT=1;
If you AUTOINCREMENT the Primary Key then you can't change the sequence with which you have started.Even if you insert the same name again it does not matter.It will continue from the next number with which it previously ended.
But if you want to give it a number then don't AUTOINCREMENT it,INSERT primary key value like other values you have inserted in the table.
I ended up setting identity_insert to on and entering the Ids manually with the names and then changed the table back to how it was with identity_insert off

How can I restrict a column to have only a fixed number of duplicate values

Lets say I have a table tblPerson with below values. And In Name field I don't want the names to get repeated more than 2 times.
ID Name
1 JOHN HONAY
2 PETER CAM
3 JOHN HONAY
So If I try to insert a new row in tblPerson with Name as "JOHN HONAY". It should throw error. How can I achieve this. Can I do something during the creation of table itself?
I think of
On insert/ on update Triggers to check how many times the value is exists in database
Make your BL (Business logic) check for the duplicate

Deleting record from sql table and update sql table

I am trying to delete a record in sqlite. i have four records record1, record2, record3, record 4
with id as Primary Key.
so it will auto increment for each record that i insert. now when i delete record 3, the primary key is not decrementing. what to do to decrement the id based on the records that i am deleting.
i want id to be 1,2,3 when i delete the record 3 from the database. now it is 1,2,4. Is there any sql query to change it. I tried this one
DELETE FROM TABLE_NAME WHERE name = ?
Note: I am implementing in xcode
I don't know why you want this but I would recommend leaving these IDs as is.
What is wrong with having IDs as 1,2,4?
Also you can potentially break things (referential integrity) if you use these ID values as foreign keys somewhere else.
Also please refer to this page to get a better understanding how autoincrement fields works
http://sqlite.org/autoinc.html
The sense of auto increment is always to create a new unique ID and not to fill the gaps created by deleting records.
EDIT
You can reach it by a special table design. There are no deleted records but with a field "del" marked as deleted.
For example, with a "select ... where del> 0" will find all active records.
Or place without the "where" all the records, then the ID's remain unaffected. To loop through an array with "if del = 0 continue". Thus, the array is always in consecutive order.
It's very flexible. Depending on the select ... you get.
all active records
all the deleted records
all records

Copy data from one field to another on every row

So I've got:
id number
1 0
2 0
3 0
Is there a sql statement to copy everything from id into number?
I'm about to write a php scrip to select, then update every row. My SQL knowledge is pretty basic, but I'm sure there's a smart guy way to to do this:
Background: The id used to be a number that was displayed in the app and was unique. That number is no longer unique with some new features I'm adding--so I need to move it to a field that isn't unique while maintaining the integrity of the old.
You can use an update statement and reference the columns. Just do the following:
update mytable set number = id
That sets number equal to id on each row. Enjoy!