I'm working on db2. I got one table, which contains some data:
Data | Value | Lang
To Data column of this table, was added unique constraint. However, now application CAN contain many rows for the same date. So, I removed constraint, but, after reorganization of table, restart of server it still throws exceptions when I want to add more than one row with the same Data.
Unfortunately I didn't create this table at first place, and I can't remove data from it :/
Anyone has any idea what's going on?
Thanks for you time!
Okay, it looks like there was index on this column also.
To check indexes:
select * from SYSIBM.SYSINDEXES where TBNAME= 'TABLE_NAME';
To drop index:
drop index INDEX_NAME;
Now it works! :) Quite easy.
Related
I am peer reviewing a code.
I found lots of Delete statement without conditions where Developers are removing data from table and inserting fresh data.
public void deleteAll() throws Exception {
String sql = "DELETE FROM ERP_FND_USER";
entityManager.createNativeQuery(sql, FndUserFromErp.class).executeUpdate();
LOG.log(Level.INFO, "TERP_FND_USER all data deleted");
}
Shall i make it standard to always use Truncate when delete all data as Truncate is more efficient when delete all? (or shall i be suspicious that in future a condition will come and we would need to change statement)?
I think rollbacking thing also not implemented in code i.e not transactional .
Truncating a table means we have no way of recovering the data, once done.
I believe DELETE is a better option in this case, given that we are expecting the table size is not very big.
If we are expecting a table size to be very big in terms of volume of data we are planning to store, then even in that case I recommend to use to DELETE given that we do not want to delete tables without any conditions in such cases.
Also if we are using a table only for the session of the java program I believe we can use a TEMP table instead of main table, that will help you to not DELETE it explicitly and it will be purged once the session is over.
Truncate should only be used when you are absolutely sure of DELETING the entire table and you have no intention of recovering it at all.
There is no strict answer.
There are several differences between DELETE and TRUNCATE commands.
In general, TRUNCATE works faster - the reason is evident: it is unconditional and does not perform search on the table.
Another difference is the identity: TRUNCATE reseeds the table identity, DELETE does not.
For example, you have the users table with column ID defined as identity and column Name:
1 | John Doe
2 | Max Mustermann
3 | Israel Israeli
Suppose you delete user with ID=3 via the DELETE command (with WHERE clause or not - does not even matter). Inserting another user will NEVER create a user with ID=3, most probably the created ID will be 4 (but there are situations when it can be different).
Truncating the table will start the identity from 1.
If you do not worry about identity and there are no foreign keys which may prevent you from deleting records - I would use TRUNCATE.
Update: Dinesh (below) is right, TRUNCATE is irreversible. This should be also taken into consideration.
You should use TRUNCATE if you need to reset AUTO_INCREMENT fields.
DELETE of all rows will not.
Other difference is performance, TRUNCATE will be faster than DELETE all row.
Either TRUNCATE or DELETE will remove definitively rows,
contrary to what was mentioned in another answer, except if DELETE is execute inside a TRANCACTION which is ROLLBACK. But if TRANSACTION is commited, no recover is possible.
I need something which I dont know if it's possible to achieve. Basically I'll be adding new rows of information from java to a database with the table structure , ex:
Number | Color | Size
0 | Red | Big
1 | Green | Small
2 | Yellow| Medium
I'm using java and I'll only input the Color and Size, and I would like to know if it's possible to create a trigger that will store the variable Number(id) on the database side, and increment it each time I do an Insert of a new row into the db. I was thinking doing something like, " INSERT INTO table ((null),'Red', 'Big'), and then the database would update the value with the proper Number.
Also should be possible to bare with fact that some rows can be deleted, but it won'shouldn't affect anything, example: if I have ID's 0 , 1 ,2 and I delete 1, next row should still be 3.
Another thing, I'm using Sybase SQL Anywhere 12 to do this.
You should use autoincrement column in your database.
See this.
http://www.sqlines.com/sybase-asa/autoincrement_identity
As #Gordon Linoff said ...
Identity columns are doing that, for example ...
create table T1 (ID int identity(1,1), Name nvarchar(100))
In this case you would go ...
insert into T1 (Name) values ('John')
So, you would insert Name 'John' and DB itself would give him ID 1
Next time you do insert, ID 2 would be set ... and so on and on ..
Identity(1,1) - it means start from 1 and increment it by 1 on new insert
Thing about this is that once number is taken, there is no going back, so if you have IDs 1, 2, 3 .. and delete ID 3 .. on next insert ID will go up to 4, it will not populate "missing number"
there are several solutions that satisfy your requirements but those are different in several aspects and you should decide to select the best one.
some solutions are exists in DB context. (for example #Gregory answer),
but some other solutions are independent of the DB type and specific features. it means that you implement your solution independent of your db type and you could change your db (oracle, sql-server, my-sql , ...) and there is no need to change your java code.
in jpa there are three Sequence Strategies to solving this problem with #GeneratedValue.
1) Table sequencing: you use a separate table for this purpose in your db. this table hold the proper ids for other tables that have auto-increment-columns with this strategy.
2) Sequence objects: you use a sequence object in your db and jpa handle it. Sequence objects are only supported in some databases, such as Oracle, DB2, and Postgres.
3) Identity sequencing: uses special IDENTITY columns in the database to allow the database to automatically assign an id to the object when its row is inserted. Identity columns are supported in many databases, such as MySQL, DB2, SQL Server, Sybase, and PostgreSQL. Oracle does not support IDENTITY columns but it is possible to simulate them using sequence objects and triggers.
if you want to be independent from your db type, I recommend you to use "table strategy" in jpa.
see Java Persistence/Identity and Sequencing for details.
you asked:
I would like to know if it's possible to create a trigger that will
store the variable Number(id) on the database side, and increment it
each time I do an Insert of a new row into the db.
yes, you could use trigger, but as i mentioned there are some simpler solutions rather it.
Also should be possible to bare with fact that some rows can be
deleted, but it won'shouldn't affect anything
in jpa solutions the deleted ids don't be used in next usage, but if you implement your own solution you could use them.
I hope this answer help you.
I have eclipse version 3.4.1 for BIRT Reporting,
I need to avoid page break between two detail rows of a table.I didn't found "avoid" option in page break properties.
My table showing data in two detail rows so may Table can be break at the end not in between two detail rows.
Example:
Employee No/Employee Name
--------------------------
Detail row 1 [employee_No]
Detail row 2 [employee_Name]
In above, I need to avoid page break between employee_No and employee_Name
Thanks in Advance.
The easiest solution if your design criteria will allow it; is to use a table with 2 detail columns. Place your employee number in the left column and the employee name in the right column, this way there is no row to break, everything is on the same row.
The harder way but it will allow you keep the existing format and control page breaks between employee name and number is to use a list element with a table inside it.
In this example I have a list with a table (actually two tables) in the detail line of the list. The table(s) each return only one occurrence per primary key (in my case a ticket ID, in yours an employee ID). You can get as fancy as you want with table (I have extra columns for spacing, and two columns of data & Two columns of labels.)
You can then avoid page breaks in your table.
I built this report a couple years ago, and have not done anything similar for a while, so all the details of making it work are not fresh in my mind, but I don't recall it being overly difficult to figure out.
I know this is quite old, but with versions 2.5 and following, you can define where in your layout do you want to avoid page breaks.
To do so, select an element in your layout, go to property editor -> page break and select "avoid" in the desired position.
Keep in mind that, depending on your layout, you probably need to set "avoid" in different point, for example after "first rows" and before "second rows" of each record in the question example.
Im having duplicate rows in a table like the printscreen
I read the solution was selecting the columns and check the option: "supress duplicates" (as printscreen) but this is not working as well.
Does anybody has an ideia of how to fix this problem? Thanks in advance
In BIRT, Suppress Duplicates just makes sure the value in the column in not repeated and hence you could see Blank or rows with no values.
And the other reason why we get multiple rows is because there are those many rows with same values.
To avoid blank rows we can make use of hide condition. Steps for the same:
Select the detail row of the table:
Detail Row Selection
In Property Editor -> Properties -> Visibility -> check on Hide Element and add
hidden condition in the pop up window
Row Visibility condition
Visibility condition could be like this :
if(row["Col1"]=="" && row["Col2"]=="" && row["Col3"]==""){true}
If "" doesn't work in the condition, replace it with null
Hope this works.
Thank You.
If you are getting duplicate rows in your table, where it looks like all the values are identical. You probably have one of these issues
A join to a second data set that causes a second row of data to be created, where the filed with two values is not displayed on your report table. = If that field is not required remove it from your dataset.
There are duplicate rows of data in your raw data source = use something like 'select distinct/unique' in your SQL to return only a single row.
If you only have a single data source with only unique rows, what you are displaying may not show a field with two distinct values, but the table is bound to the data that does have two values. Suppress dupes is not going to work because there are not any dupes. = Either display the field with multiple values or clean up your data pull so there is no longer duplicates where they are not intended.
Im programming a program in java and i have a database in a JTable just like the ones below. I wanted to know if it is possible to refresh the primaryID location from 1 on the GUI interface form one when a row is deleted? for example below the LoactionID is deleted for London and added again with an id 4. Is this possible?
Im using SQL in java
To answer your question, yes it is possible.
There is no good reason for you to do this though, and I highly recommend you don't do this.
The only reason to do this would be for cosmetic ones - the database doesn't care if records are sequential, only that they relate to one another consistently. There's no need to "correct" the values for the database's sake.
If you use these Id's for some kind of numbering on the UI (cosmetic reason):
Do not use your identity for this. Separate the visual row number, order or anything else from the internal database key.
If you REALLY want to do this,
Google "reseeding or resetting auto increment primary ID's" for your sql product.
Be aware for some solutions if you reset the identity seed below values that you currently have in the table, that you will violate the indentity column's uniqueness constraint as soon as the values start to overlap
Thanks Andriy for mentioning my blindly pasting a mysql solution :)
Some examples:
ALTER TABLE table_name ALTER COLUMN auto_increment_column_name RESTART WITH 8 Java DB
DBCC CHECKIDENT (mytable, RESEED, 0)
Altering the sequence