I have a simple One-to-One relationship between table TICKETINFO and TICKETINFO_REMARK.
TICKETINFO
TICKETINFOID pk, REMARK varchar(128), TICKETDATE timestamp
and
TICKETINFO_REMARK
TICKETINFOID fk, REMARK varchar(128)
and TICKETINFOID will be foreign key from TICKETINFO table and have to populate the REMARK field of TICKETINFO_REMARK along with the REMARK field of TICKETINFO for the corresponding TICKETINFOID.
For 1 TICKETINFOID there will be one REMARK.
The insertion is working fine.
Now my problem is:
My requirement is that I am not supposed to update any record in TICKETINFO_REMARK due to some performance issue. I need to delete the record first and insert it with the same TICKETINFOID and the new REMARK.
for example:
TICKETINFO table has:
**TICKETINFOID** : 1
**REMARK** : ABC
**TICKETDATE** :2012-06-12
TICKETINFO_REMARK has:
**TICKETINFOID** : 1
**REMARK** : ABC
Now I want to change the REMARK in TICKETINFO_REMARK to "XYZ123"
So i have to delete the entry from TICKETINFO_REMARK and reinsert it which which look like this
**TICKETINFOID** : 1
**REMARK** : XYZ123
what will be the code to do it?
please help!!!!
First you should select a record form "TICKETINFO_REMARK" table with TICKETINFOID.If it already found you should delete this record from "TICKETINFO_REMARK" table and then insert new record with this TICKETINFOID.It is only logic for your requirement.If you want to get sample code, please tell me which technology did you use for your CRUD operation for example JPA, HIBERNATE, IBATIS OR JDBC etc.
Related
I have a Table name called employee_Details with Columns
EMP_ID NAME VECH_NO SALARY
1 A 1234 100
2 B 12345 200
I Construct JSON array using Java. with structure
{["EMP_ID":1,"NAME":Y,"VECH_NO":4587,"SALARY":1500],["EMP_ID":3,"NAME":Z,"VECH_NO":4007,"SALARY":1800]}
I need to update full record where EMP_ID exists on table else insert as new record. There is any in build function available in postgreSQL to achieve this. I refer this https://www.postgresql.org/docs/9.3/static/functions-json.html but didn't get solution
First of all, your JSON is malformed, I guess the intended was:
[
{"EMP_ID":1,"NAME":"Y","VECH_NO":4587,"SALARY":1500},
{"EMP_ID":3,"NAME":"Z","VECH_NO":4007,"SALARY":1800}
]
Assuming the following table definition:
CREATE TEMP TABLE employee_details(
"EMP_ID" integer primary key,
"NAME" text,
"VECH_NO" integer,
"SALARY" numeric
);
And a sample data (just to show the update):
INSERT INTO employee_details VALUES(1, 'X', 123, 123);
You can first use json_array_elements to make each array one row and json_populate_record to get each value as the original table type, like this:
SELECT r.*
FROM
json_array_elements('[{"EMP_ID":1,"NAME":"Y","VECH_NO":4587,"SALARY":1500},{"EMP_ID":3,"NAME":"Z","VECH_NO":4007,"SALARY":1800}]') AS a(element),
json_populate_record(NULL::employee_details, a.element) AS r;
With that, you can simple use INSERT ... ON CONFLICT UPDATE:
INSERT INTO employee_details("EMP_ID", "NAME", "VECH_NO", "SALARY")
(
SELECT r."EMP_ID", r."NAME", r."VECH_NO", r."SALARY"
FROM
json_array_elements('[{"EMP_ID":1,"NAME":"Y","VECH_NO":4587,"SALARY":1500},{"EMP_ID":3,"NAME":"Z","VECH_NO":4007,"SALARY":1800}]') AS a(element),
json_populate_record(NULL::employee_details, a.element) AS r
)
ON CONFLICT ("EMP_ID") DO
UPDATE SET
"NAME" = EXCLUDED."NAME",
"VECH_NO" = EXCLUDED."VECH_NO",
"SALARY" = EXCLUDED."SALARY"
;
The ON CONFLICT clause only works on version 9.5 or higher. Before that you have to use some tricks with loop and retry or writable common table expression (although that has race-condition issues); in any case it is a good reason to upgrade if you are on older versions.
I got 3 tables :
Actor : ID, Name
Movie : ID, Title
Cast : Movie_ID, Actor_ID
Now I want to JOIN those 3 tables to get actor's list for each movie. It is simple SQL query, but it gets harder with Hibernate.
First of all, I created Cast table like this :
CREATE TABLE Cast (
Movie_id int PRIMARY KEY,
Actor_id int PRIMARY KEY,
FOREIGN KEY (Movie_id) REFERENCES Movie(ID),
FOREIGN KEY (Actor_id) REFERENCES Actor(ID)
)
Then, in Netbeans, I chose New -> Hibernate Mapping Files and POJO's from Database, I added ACTOR, MOVIE and CAST tables and IDE generated 4 instead of 3 classes : Actor.java, Movie.java, Cast.java and CastId.java. Class CastId.java seems to be well mapping of Cast table and Cast.java includes only CastId field and getters and setters.
I understand that this is the way Netbeans handles tables which keys have multiple attributes - but why , how does it help me ? Let's say that I want to SELECT * from Cast table which should return list of pairs(Movie_ID, Actor_ID). I can't make it, beacuase when running HQL command
from Cast
it gets only some single ID's - it's beacase Cast has only one field : CastId. On the other hand Netbeans generates CastId but doesn't CastId.hbm.xml, so I can't run 'from CastId' either. Am I missing something ? I'm newbie at Hibernate and would like to understand how this should be done.
In my database I have table "Announcement" with fields:
id int,
text varchar,
sent_date date.
Also table "Group" with fields:
id int,
name varchar.
Each announcement submitted to any number of groups(from 1 to all). How to store this relation in database?
I'm using MySQL and Hibernate in java web project.
Create a third table (I call it announcement_group) with two columns:
announcement_id REFERENCES Announcement(id)
group_id REFERENCES Group (id)
Read more about Foreign Keys from the documentation here http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
In your query you will want to use a
SELECT group_id WHERE announcement_id=<your id here> from announcement_group;
Till recent time i was using hibernate #Entity annotation to map to database tables.All the primary keys are annotated with #GeneratedValue(strategy = GenerationType.IDENTITY)
I got a scenario where i need to create new schema + migrate data from old schema into new schema.(with few column changes like drop, length and type)
After successful migration of data to new schema tables when i try to insert data using Application its throwing an exception
[ERROR] util.JDBCExceptionReporter DB2 SQL Error: SQLCODE=-803, SQLSTATE=23505, SQLERRMC=1; _NewSchema_._TableName_ , DRIVER=3.51.90
I believe that application is trying to insert rows again with Primary key value starting from 1 because same application is working fine with empty tables.
I want data rows to be inserted with its primary key value as highest value of existing rows primary key .
Any help will be thank full :)
Yes you can do that by altering the table. Alter the table and set starting index for identity column in DB2.
Suppose maximum rows for TBALE_A is 50 and name of identity column is TABLE_ID
ALTER TABLE TBALE_A ALTER COLUMN TABLE_ID
RESTART WITH 51
Your guess is correct, here is my solution, execute the following SQL to give the ID column a specified start position, then your application will work fine.
alter table TABLE_NAME alter column ID set GENERATED BY DEFAULT RESTART WITH 10000;
Hope to help you :)
In case of generation type , IDENTITY, you should look for identity column to be auto incemental.
#GeneratedValue(strategy = GenerationType.IDENTITY) required primary key column to be auto incremental.
Am using JPA into my application and then the when insert different objects the database use sequential ids.
For Example:
If i have table member and table user , when inserting object of type user will take id = 1 then add object into table member will take id = 2 .
The problem is : i imported a .sql file into the database.
Then when i insert a new record from my application it cause an exception because it use ids already added.
How can i solve this problem ?
You can set the start value for AUTO_INCREMENT as
ALTER TABLE tbl AUTO_INCREMENT = 100;
See also:
3.6.9. Using AUTO_INCREMENT