Find relevant records with multiple options - sql

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.

Related

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

On duplicate key update feature in H2

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;

In H2 Database, add index while table creation in single query

I am trying to create table having different indexes with single query but H2 gives Error for example:
create table tbl_Cust
(
id int primary key auto_increment not null,
fid int,
c_name varchar(50),
INDEX (fid)
);
but this gives error as
Unknown data type: "("; SQL statement:
[Error Code: 50004]
[SQL State: HY004]
Due to this I have to run 2 different queries to create table with Index. First query to create table and then second query to add index with
create INDEX c_fid on tbl_Cust(fid);
Is there something wrong in my query or H2 simply does not support this creation of table with index in single query?
Interesting question. The solution is even more interesting, as it involves MySQL compatibility mode.
It's actually possible to perform the exact same command you wrote without any modification, provided you just add to your jdbc url the MySQL mode.
Example URL like this: jdbc:h2:mem:;mode=mysql
SQL remains:
create table tbl_Cust
(
id int primary key auto_increment not null,
fid int,
c_name varchar(50),
INDEX (fid)
);
Update count: 0
(15 ms)
Too bad I did not see this question earlier... Hopefully the solution might become handy one day to someone :-)
I could resolve the problem. According to
http://www.h2database.com/html/grammar.html#create_index
I modified the query. It works fine with my H2 server.
CREATE TABLE subscription_validator (
application_id int(11) NOT NULL,
api_id int(11) NOT NULL,
validator_id int(11) NOT NULL,
PRIMARY KEY (application_id,api_id),
CONSTRAINT subscription_validator_ibfk_1 FOREIGN KEY (validator_id) REFERENCES validator (id) ON UPDATE CASCADE
);
CREATE INDEX validator_id ON subscription_validator(validator_id);

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);.

Cretae Domains Table In HSQL includes the metadata of Domains Sytem View

I want to create a table named Domains in HSQL. But when i try to do so it includes 29 columns although my table definition specifies just 5.There is a Domains System View and it seems it is including these columns also in my metadata.Because of this i am not able to insert anything in my db as those unwanted columns from Domains System View also contain some non-null values.
I tried creating schema but it is not what i need as my solution.
I need to override this Domains System View or stop the metadata from picking it.
Does anyone know how can we do it?
I am stuck at it for more than few days now.
CREATE TABLE IF NOT EXISTS Domains
(
domain_id INTEGER NOT NULL,
name VARCHAR(100) NOT NULL,
description VARCHAR(500) NOT NULL,
CREATED_BY VARCHAR(8) NOT NULL,
LAST_UPDATED_BY VARCHAR(8) NOT NULL,
PRIMARY KEY (domain_id)
);