I insert 2 records in the table at the same time, which has the ID field AUTOINCREMENT. can be assigned to the second record id before? so that when I delete a record using the id are deleted both?
If want two different rows with the same ID and the ID field has AUTOINCREMENT enabled you cannot give them the same ID. That'd be a bad idea for several reasons.
I think what you should rather do is rethink your database and/or table structure. I'd propose you put the rows in different tables and make one of the rows depend on the other. Then you can make them have the same ID in their tables and have one ID to delete both rows in the different tables.
Related
I have some kind of this table.
The question is what is the best way to create this kind of table?
Should I create for each item one table is it possible to create only one table??
Updated: See comments under #Emil.
You should have 1 tables as #Emil has suggested.
This should look like, soemthing like
_id, sort, grade, diameter, length, price1_dol, price1_euros, price2_dol, price2_euros, final,
Note: I have split up prices columns up - so you have price1_dol, price1_euros, price2_dol, price2_euros.
It is indeed possible to make this data into just one table. The columns sort and grade seem to uniquely identify one row so together they might make up a candidate key. If so you could use those as your primary key, or create a new integer column that you use as the primary key.
You should definitely not create one table per item. The database schema should never change with normal use. Only when you add, remove or change the type of data you have in your database should you consider changing the schema. Otherwise you should design and normalize your database in such a way that it's possible to grow the data only by inserting new rows, not new tables.
As per question..because when I read here
create a indexed column in sqlite
it seems to say that we need to create indexes manually... but when I read here
http://www.android-app-market.com/sqlite-optimization-in-android-programming-sqlite-optimization-in-android-apps.html
it says indexes are created automatically for every unique column.
it says indexes are created automatically for every unique column
No, it says that indexes are created automatically for every UNIQUE column. Here, the author is not using capitalization just to be funny. It is referring to the UNIQUE keyword that can be applied to a column in a CREATE TABLE statement, indicating that all values in the column must be unique compared to all other values in that column.
Most columns in SQL tables are not UNIQUE. For some of those, if you are using them in query constraints, you will want to create indexes.
Indexes are automatically created for unique columns, as in columns that cannot have duplicate values, not "every column." You should still create indexes as necessary depending on how you query your data.
I am writing an app that controls many tables in a database. I am using the AUTOINCREMENT method of sqlite. The items corresponding to the AUTOINCREMENT '_id' collumn will be having their unique ids as the values in the '_id column'
Is it advisable to use this method to assign unique ids to every item. Because i donot know what will happen to the numbering system if I write a method to remove certain rows from the table.
I mean that if once the table is created with certain entries and then the user decides to remove certain rows, will the autoincrement column re-number itself (which will lead to wrong ids being assigned to the corresponding items) or not.
Deleting rows from a table will not reset the auto increment ID. That is, suppose you have created rows 1, 2 and 3. If you delete 2, the next row you insert will still have an ID of 4, i.e, you would now have rows 1, 3, 4 in the database.
If I have a SQLite table where I delete one row in the middle of it, it removes that row, but the numbering is off. example, removed row 4
id name
1 A
2 B
3 C
5 E
6 F
How can I reset the numbering so that it the key auto incremented key ID is continuous with out the missing 4 like in the table above?
one idea is to delete all the key row ids and re insert them in a for loop. not sure if I should try that as it is auto increment. the other possibility is to make it not auto increment, and use loops to put the numbers in when the table is crated.
You already answered your own question: It's an AUTOINCREMENT field, so you can't do that. Furthermore, this is not how ids should be used in SQL anyway. They are to uniquely identify a row, and if you change the id, then you're breaking that, as well as any references there might possibly be to that id.
Say my SQLite Databate has 2 columns, the first being an auto-incrementing ID and the 2nd being some string. Say right now it's
1 random
2 jellybean
3 ImTired
if I were to delete entry 2, it would then be
1 random
3 ImTired
What I want is a way to make it so when you delete entry 2, it turns it into
1 random
2 ImTired
I thought about updating the entries to shift them all down one and delete the last one, but even if it worked(in my case, it deleted all of my entries, but whatever...), and even if I did get it to
1 random
2 ImTired
the next time I create a new entry, it'll be entry 4. I don't think this necessary to my app, but it seriously bugs me.
The ID column on your DB is working as a Primary Key, which is a column or group of columns used to uniquely identify a row. Once you set a Primary Key on a row you shouldn't change it, else you risk losing the consistency of the DB. For instance, suppose you later create another table that references the rows in your first table. That reference will be made using the Primary Key, and if you later change it your data won't make sense anymore.
If you wanted the ID column to keep changing just to reflect the number of rows in your table you can solve that problem with other methods. For instance. SQL offers a COUNT operator that will return the number of rows in your table:
SELECT COUNT(*) FROM Table_name;