On duplicate key update feature in H2 - sql

I have developed java desktop application with the use of H2(Embedded). I just have basic knowledge about database, so i simply installed H2 and create a schema name RecordAutomation and then add tables to that schema. Now i am trying to use the ON DUPLICATE KEY UPDATE feature for a specific table which is not working giving sql syntax error, i check my query i found it right, given below
INSERT INTO RECORDAUTOMATION.MREPORT
(PRODUCTID ,DESCRIPTION ,QUANTITY ,SUBTOTAL ,PROFIT )
VALUES (22,olper,5,100,260)
ON DUPLICATE KEY UPDATE SET QUANTITY = QUANTITY+5;
i search and try to solve this problem some where it is discussed like this feature does not work for non-default tables. i have no idea about default and non-default. please make help me

You need to use the MySQL mode. To do that, append ;mode=MySQL to the database URL. (This feature is not properly documented yet).
The table needs to have a primary key or at least a unique index. Complete example:
drop table MREPORT;
set mode MySQL;
create table MREPORT(PRODUCTID int primary key,
DESCRIPTION varchar, QUANTITY int, SUBTOTAL int, PROFIT int);
INSERT INTO MREPORT
(PRODUCTID ,DESCRIPTION ,QUANTITY ,SUBTOTAL ,PROFIT )
VALUES (22,'olper',5,100,260)
ON DUPLICATE KEY UPDATE QUANTITY = QUANTITY+5;

Related

Find relevant records with multiple options

I wasn't sure how I should name the Question. I try to explanin what I need to achieve.
I will get a list from our Customer where the installed software on each machin will be listed.
Example_
Machines List
Hostname SW
PC001 SW001
PC001 SW002
PC002 SW003
PC002 SW001
PC003 SW003
Software List
SW Name Status
SW001 not okay
SW002 not okay
SW003 ready
I need to have an SQL or MS-Access logik to find all Hostname which have only SW installed with Status okay
and - in adition - i need a logic to tell me what kind of SW product need to be okay to make additional Hostnames availabale with only okay Software installed.
At the moment i struggle with this question
rgds
Sebastian
At the moment i am not sure how to select the multiple rows one Hostname could have as the whole - so If i use a select statement i get all Hostname where the specified product is installed - but other installed products aren't reflected.
Well i could help you a bit, but you have to do something by yourself,
So first of all i modified your DDL code first
CREATE TABLE `machines` (
`id` int(11) NOT NULL,
`hostnames` varchar(20) NOT NULL,
`id_soft` int(11) NOT NULL
)
CREATE TABLE `software` (
`id_soft` int(11) NOT NULL,
`software_name` varchar(20) NOT NULL,
`status` varchar(20) NOT NULL
)
--
-- Indexes for table machines
ALTER TABLE `machines`
ADD PRIMARY KEY (`id`),
ADD KEY `id_soft` (`id_soft`);
--
-- Indexes for table software
ALTER TABLE `software`
ADD PRIMARY KEY (`id_soft`);
--
-- Constraints for table machines
ALTER TABLE `machines`
ADD CONSTRAINT `machines_ibfk_1` FOREIGN KEY (`id_soft`) REFERENCES
`software` (`id_soft`);
and here is your query for showing hostnames only where the status is okey or ready!
SELECT hostnames
FROM machines
LEFT JOIN software on machines.id_soft = software.id_soft
WHERE machines.id_soft = (SELECT software.id_soft from software
WHERE software.status like 'ready');
So this will definitely help you but you have to do other things alone, and only if you cant find a solution, we will be here to get help you.

sql server executing update it takes more time

I have two tables (UserTable and UserProfile) and the Structure:
create table userTable(
id_user int identity(1,1) primary key ,
Name varchar(300) not null ,
Email varchar(500) not null ,
PasswordUser varchar(700) not null,
userType int ,
constraint usertype_fk foreign key(userType) REFERENCES userType(id_type)
on delete set null
)
and userPtrofile:
create table UserProfile(
id_profile int identity(1,1) primary key ,
ClientCmpName varchar(300) null,
Clientaddress varchar(500) null,
phone varchar(50) null,
descriptionClient varchar(400) null,
img image null,
messageClient text ,
fk_user int ,
constraint fkuser foreign key(fk_user) references userTable(id_user)
on delete cascade
)
I am using SQL Server 2008.
The problem is that when I update records the executing load without executing
this is sample query:
update UserProfile set messageClient=N'010383772' where fk_user=2;
screenshot
If your concern is performance for this query:
update UserProfile
set messageClient = N'010383772'
where fk_user = 2;
Then an index will be very helpful:
create index idx_UserProfile_fkuser on UserProfile(fk_user);
This should make the query almost instantaneous.
Note: indexes can slow down inserts and other operations. This is usually not a big issue, and having indexes on foreign key columns is common.
Dumb question, why are you trying to do an update based on a [userType] value ?
update UserProfile set messageClient=N'010383772' where fk_user=2;
Don't you want to update this value on one specific [UserProfile] based on its ID (which is a Primary Key, so would be much faster)
UPDATE [UserProfile]
SET [messageClient]='010383772'
WHERE id_profile=2;
Perhaps the performance problem is due to your UPDATE attempting to update all of your [UserProfile] records with this particular UserType value...?
Or I'm missing the point of what you're trying to do (and how many records you're attempting to update).
Maybe you have alredy started a transaction (BEGIN TRANSACTION) on the table in another process (maybe another query editor page) and until you don't stop that transaction the table would not be available for updates.
Check the variable select ##trancount, or try do rollback the updates you have already made (ROLLBACK TRANSACTION).
Also check if other tables can be update without issues.
Is the query ever executed? It rather seems like a deadlock. You should
open the activity monoitor and check if your query is blocked by some process.
In that case, you should kill the blocking query.
Thank you for trying to help me
my problem fixed the problem was another query editor page because i worked with asp.net and another page i use the same record to update the same record when i stop the asp.net project then query was success

PostgreSQL force X amount of foreign key rows after insert

If I have the following tables:
CREATE TABLE projects
(
id SERIAL PRIMARY KEY,
name VARCHAR(25) NOT NULL,
);
INSERT INTO projects (name) VALUES ('Portfolio Website'), ('Client Website');
CREATE TABLE developers
(
id SERIAL PRIMARY KEY,
name VARCHAR(25) NOT NULL,
);
INSERT INTO developers (name) VALUES ('Kevin'), ('Mark'), ('Simon');
CREATE TABLE languages
(
id SERIAL PRIMARY KEY,
name VARCHAR(25) NOT NULL,
);
INSERT INTO languages (name) VALUES ('HTML'), ('PHP'), ('ASP.NET'), ('Java');
CREATE TABLE developer_languages
(
id SERIAL PRIMARY KEY,
developer INTEGER REFERENCES developers(id),
language INTEGER REFERENCES languages(id)
);
INSERT INTO developer_languages (developer, language) VALUES (1,1), (1,2), (2,1), (2,3), (3,4);
CREATE TABLE project_developers
(
id SERIAL PRIMARY KEY,
project INTEGER REFERENCES projects(id),
developer INTEGER REFERENCES developers(id)
);
The requirement is that every project has a HTML developer and a Java developer. Is there any way to create a rule that enforces this?
I know I could just have a 'html_dev' and 'java_dev' column in the projects table but this then is an extra data source if you wanted to get every developer on the project stored in these two columns and the project_developers table.
I hope my question makes sense? I'm not even sure where to start looking so I though someone might be able to point me in the right direction, thanks!
What you are trying to do is not easily solved and a constraint trigger is not the solution because you cannot create a constraint to match your problem. One solution is the following, although it is a bit artificial:
Remove the INSERT privilege from the projects table.
Create a function insert_new_project(nm text, html_dev integer, java_dev integer) which inserts the new project and two inserts in project_developers. Check that the developers indeed have the required language skills.
Remove the UPDATE privilege on project_developers. Create a BEFORE DELETE trigger on project_developers to check that if you delete a Java or HTML developer from a project there is at least one such developer remaining. (So if you want to remove a developer, first insert a replacement.)
This procedure ensures that a new project has one developer of both flavours and that when developers get removed from a project at least one of each flavour remains. Insert a new project with SELECT insert_new_project('New project', 1, 3);.

Insert and alter in one statement

I'd like to store a set of data into a database but if it's a pre-existing record, I'd like to alter it. Otherwise, create a new one. Is there a combine statement for that? (Haven't got any when googling.)
Right now, the best I have is to check if already exists and then perform one of the operations. Seems cumbersome to me.
create table Stuff (
Id int identity(1001, 1) primary key clustered,
Beep int unique,
Boop nvarchar(50))
IN MYSQL :
You may use INSERT ... ON DUPLICATE KEY UPDATE .
eg:
INSERT INTO table (a,b,c) VALUES (4,5,6)
ON DUPLICATE KEY UPDATE c=9;
For more information: http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html
MySQL uses INSERT... ON DUPLICATE KEY and MSSQL uses MERGE
MERGE is supported by Azure, and I can highly recommend this blog article on it, as a good intro to the statement
Here is a merge statement based on the schema provided...
create table #Stuff (
Id int identity(1001, 1) primary key clustered,
Beep int unique,
Boop nvarchar(50),
Baap nvarchar(50)
);
INSERT INTO #Stuff VALUES (1,'boop', 'poop');
INSERT INTO #Stuff VALUES (2,'beep', 'peep');
SELECT * FROM #STUFF;
MERGE #Stuff
USING (VALUES(1,'BeepBeep','PeepPeep')) AS TheNewThing(A,B,C)
ON #Stuff.Beep = TheNewThing.A
WHEN MATCHED THEN UPDATE SET #Stuff.Boop = TheNewThing.B, #Stuff.Baap = 'fixed'
WHEN NOT MATCHED THEN INSERT (Beep,Boop,Baap) VALUES (
TheNewThing.A, TheNewThing.B, TheNewThing.C);
SELECT * FROM #STUFF
I also found a really good SO Q which might make good further reading
yes you can easily do it using pl/sql here is sample code which will help you
http://docs.oracle.com/cd/B10501_01/appdev.920/a96624/01_oview.htm#7106

Derby DB modify 'GENERATED' expression on column

I'm attempting to alter a Derby database that has a table like this:
CREATE TABLE sec_merch_categories (
category_id int NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
packageName VARCHAR(100) NOT NULL,
name VARCHAR(100) NOT NULL,
primary key(category_id)
);
I'd like to change the category_id column to be:
category_id int NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
The only place I've seen that documents this is IBM's DB2, which advises dropping the expression, changing the integrity of the table, and adding the expression back. Is there anyway of doing this in Derby?
Thanks,
Andrew
You can create a new table with the schema you want (but a different name), then use INSERT INTO ... SELECT FROM ... to copy the data from the old table to the new table (or unload the old table and reload it into the new table using the copy-data system procedures), then use RENAME TABLE to rename the old table to an alternate name and rename the new table to its desired name.
And, as #a_horse_with_no_name indicated in the above comment, all of these steps are documented in the Derby documentation on the Apache website.