INSERT OR IGNORE conversion sqlite to mariadb - sqlite

I'm trying to convert my SQlite code to Mariadb but i'm stuck with a query for a crossed table between two another tables
The purpose of the table is to add ID from the other table but only if they don't exists
I have two tables
Table COMPUTERS
ID(primary) / NETBIOS(unique) / IP / SOFTWARE_STAT / COPY_STAT / AV_STAT
ex : 1 PC1 192.168.1.1 KO KO 0
Table SOFTWARES
ID(primary) / NAME(unique)
ex : 1 ADOBE
And the cross table
Table INSTALL
ID (primary)/ COMPUTER_ID / SOFTWARE_ID / FAIL
1 1 1 0
My SQLITE code below is working
INSERT OR IGNORE INTO INSTALL (COMPUTER_ID,SOFTWARE_ID)
SELECT
(SELECT ID FROM COMPUTERS WHERE IP = '192.168.1.1'),
(SELECT ID FROM SOFTWARES WHERE NOM = 'ADOBE')
WHERE NOT EXISTS
(SELECT
COMPUTER_ID,SOFTWARE_ID FROM INSTALL
WHERE
COMPUTER_ID = (SELECT ID FROM COMPUTERS WHERE IP = '192.168.1.1')
AND
SOFTWARE_ID = (SELECT ID FROM SOFTWARES WHERE NOM = 'ADOBE')
)
I've tried that
INSERT INTO INSTALL (COMPUTER_ID,SOFTWARE_ID)
SELECT
(SELECT RowID FROM COMPUTERS WHERE IP='192.168.1.1'),
(SELECT RowID FROM SOFTWARES WHERE NOM='ADOBE')
WHERE NOT EXISTS
(SELECT COMPUTER_ID,SOFTWARE_ID FROM INSTALL
WHERE
COMPUTER_ID = (SELECT RowID FROM COMPUTERS WHERE IP='192.168.1.1')
AND
SOFTWARE_ID = (SELECT RowID FROM SOFTWARES WHERE NOM='ADOBE'));
Without success
Someone have an idea ? Thanks in advance.

INSERT OR IGNORE --> INSERT IGNORE
However, you have a redundancy: (1) IGNORE, and (2) NOT EXISTS. That is, you both verify that it is not a dup and you ignore errors if it is.
Otherwise, the Sqlite code should work as is in MariaDB. What went wrong? Did you get an error?
MariaDB has no RowID.

Related

Insert Query executing on local DB but not on server

I'm facing a strange error. I have a 5.5.5-10.1.20-MariaDB install on my local mac (brew) and a 5.5.52-MariaDB on my prod server (centos7). My local DB content is a copy from my server DB. I've executed this query on local:
## CREATE DIRECT RELATION BETWEEN JOURNAL AND PUBLICATION
INSERT INTO journal_publication (journal_id, `publication_id`) (
select issues.journal_id as journal_id, publications.id as publication_id from issues
join publications on issues.id = publications.`issue_id`
where publications.id Not In (select distinct publication_id from journal_publication)
);
It works fine and takes only less than a second to execute.
Now when I try the exact same query on my prod server, the query is never ending and takes all CPUs. Moreover, I've tried to EXPLAIN the query, it works fine on my local:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY issues index PRIMARY issues_journal_id_foreign 5 NULL 70993 Using index; Using temporary
1 PRIMARY publications ref publications_issue_id_foreign publications_issue_id_foreign 5 pubpeer.issues.id 1 Using where; Using index
2 MATERIALIZED journal_publication index NULL PRIMARY 8 NULL 143926 Using index
Whereas the same query on my Prod returns an error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO journal_publication (journal_id, `publication_id`)
(select issues.j' at line 2
Again, the content of the two DBs are identical, primary keys and indexes are set equally. For the record, when I try and execute this query:
select issues.journal_id as journal_id, publications.id as publication_id from issues
join publications on issues.id = publications.`issue_id`
where publications.id Not In (select distinct publication_id from journal_publication;
either on local or prod takes only a second.
Have you got any clue or process I could follow to help me understand these differences?
Thanks.
Xavier
MariaDB server versions < 10.0 only support EXPLAIN SELECT
MariaDB server versions >= 10.0 support additionally EXPLAIN
UPDATE, EXPLAIN INSERT and EXPLAIN DELETE
Please note that the version string 5.5.5-10.1.20-MariaDB means MariaDB 10.1.20, the 5.5.5 prefix is required since MySQL replication would break, since it supports only 1 digit numbers for the major version.
See also EXPLAIN UDATE/INSERT/DELETE in MySQL and MariaDB

FROM keyword not found in oracle 11g

I am using Oracle 11g client.
SELECT DISTINCT TOP 3 Salary FROM employees ORDER BY Salary DESC;
Giving error
FROM keyword not found where expected.
Can anyone please help?
In Oracle11g i guess you need to use DISTINCT like this:
SELECT * FROM (SELECT DISTINCT Salary FROM employees ORDER BY Salary DESC) WHERE rownum < 4
Here rownum works like TOP provided with the condition < 4 or you can pass rownum <= 3

bug in mysql count function: zero results with two conditions

I'm working with a LAMP server (it's a bananapi) with MYSQL 5.6 server installed on it. I have a table with several fields.
From mysql command line, if I run:
select * from tbl_name where field1=val1 and field2=val2
it returns N results.
If I run instead:
select COUNT(*) from tbl_name where field1=val1 and field2=val2
it returns 0..
In particular, field1 and field2 are foreign key values to other two separate tables. All tables are InnoDB.
It's a bug???
are you enabled fulltextsearch on the table?
i have same problem and fixed by this trik!
select count(*) from (select * from tbl_name where field1=val1 and field2=val2) as t

sqlite count rows of tables identified by subquery

I want to get the count of rows in every table in a Sqlite3 database. I want to avoid writing out a longhand query. I can get the list of tables like this:
SELECT name FROM sqlite_master WHERE type='table'
and I would like to use it in a subquery like this:
select count (*) from (SELECT name FROM sqlite_master WHERE type='table');
but would just return the total rows in the subquery, which isn't what I want.
How can I write a query that will list each table along with their counts?
I have seen dynamic SQL for this kind of thing but I don't think SQLite has that.
I have written a bash loop to do this but I would prefer to do it as a single query
for t in $(sqlite3 data.db "SELECT name FROM sqlite_master WHERE type='table'"); do
echo -n "$t = "; sqlite3 data.db "SELECT COUNT(*) FROM $t;"
done
Ideas appreciated
I used the following shell syntax to blindly get counts from tables in a database I was debugging.
db="database.db"
for i in `sqlite3 "$db" "SELECT name FROM sqlite_master WHERE
type='table';"`; do echo -n $i\ \=\ ; sqlite3 "$db" "SELECT
COUNT(*) FROM $i;"; done
cols = 0
sqlite_sequence = 0
datacols = 17
hits = 0
host = 0
document = 0
admin = 2
comments = 0
If you want to know these values for debugging purposes, look at the output of the sqlite3_analyzer tool.
If you want to use these values in your program, you have to generate the queries dynamically in your program.

Converting SQL Server to Oracle

In my project, I have a database in SQL which was working fine. But now I have to make the application support oracle db too.
Some limitations I found out was that in Oracle, there is no bit field and the table name cannot be greater than 30 char. Is there any other limitation that I need to keep in mind.
Any suggestion from past experience will be helpful.
If I recall correctly from my earlier Oracle days:
there's no IDENTITY column specification in Oracle (you need to use sequences instead)
you cannot simply return a SELECT (columns) from a stored procedure (you need to use REF CURSOR)
of course, all stored procs/funcs are different (Oracle's PL/SQL is not the same as T-SQL)
The SQL ISNULL counterpart in Oracle is NVL
select ISNULL(col, 0)...
select NVL(col, 0)...
You will also struggle if you attempt to select without a from in Oracle. Use dual:
select 'Hello' from DUAL
Bear in mind also, that in Oracle there is the distinction between PL/SQL (Procedural SQL) and pure SQL. They are two distinct and separate languages, that are commonly combined.
Varchar in Oracle Databases called
varchar2 is limited to 4000
characters
Oracles concept of temporary tables is different, they have a global redefined structure
by default sort order and string compare is case-sensitive
When you add a column to a select *
Select * from table_1 order by id;
you must prefix the * by the table_name or an alias
Select
(row_number() over (order by id)) rn,
t.*
from table_1 t
order by id;
Oracle doesn't distinguish between null and '' (empty string). For insert and update you ca use '', but to query you must use null
create table t1 (
id NUMBER(10),
val varchar2(20)
);
Insert into t1 values (1, '');
Insert into t1 values (2, null);
Select * from t1 where stringval = 0; -- correct but empty
Select * from t1 where stringval is null; -- returns both rows
ORACLE do not support TOP clause. Instead of TOP you can use ROWNUM.
SQL Server: TOP (Transact-SQL)
SELECT TOP 3 * FROM CUSTOMERS
ORACLE: ROWNUM Pseudocolumn
SELECT * FROM CUSTOMERS WHERE ROWNUM <= 3

Resources