SQLite, SQL: Using UPDATE or INSERT accordingly - sql

Basically, I want to insert if a given entry (by its primary key id) doesn't exist, and otherwise update if it does. What might be the best way to do this?

Does sqllite not have REPLACE command ? Or ON CONFLICT REPLACE ?

Related

SQL integrity checks different on update and on insert

I have a SQL CHECK CONSTRAINT which checks values base on a column value (located on a different TABLE -> so FK is not an option).
Basically something like:
--Function
CREATE FUNCTION [dbo].[func_CHECK_ID_EXISTS]
(
if ...
return 1
else return 0
)
--CHECK CONSTRAINT
ALTER TABLE [dbo].[MY_TABLE]
ADD CONSTRAINT CHECK_ID_EXISTS
CHECK ([dbo].[func_CHECK_ID_EXISTS](MyColumn)=1);
This works perfectly.
Now I need this to evolve.
On an update, I would need to make the same integrity check than before (based on MyTable2.A).
But on an insert, I need to make an integrity check based on another column (based on MyTable2.B).
As far as I know, neither the SQL function func_CHECK_ID_EXISTS neither the constraint CHECK_ID_Eenter code hereXISTS knows if the element is being inserted or updated right?
So the check constraint does not seem to be an option anymore...
I had in mind to make an alternate solution with an update trigger and an insert trigger. Indeed I can set different conditions into each trigger.
But although the insert trigger can check the inserted element via the INSERTED pseudo table, I think I do not have this possibility for the UPDATE trigger.
I mean there is no UPDATED pseudo table is this correct?
So is there a possibility to compare the columns of the updated element within the update trigger?

How can I do INSERT IF NOT EXISTS using Apache Impala?

Does anyone know if there is a way to do an
INSERT IF NOT EXISTS
in Apache Impala?
I know about INSERT OVERWRITE but it does not suit the use cases I am working on.
Thank you.
Impala doesn't support that, at least when using HDFS, since a primary key would be needed. If you are able to use Impala+Kudu, which has primary key support, INSERT IF NOT EXISTS could be implemented by inserting and ignoring the errors.

update table without using update statement

can anyone tell that how to update some records of a table without using update statement. it is possible using select statement.
I don't think you can update the table without update statement.
It is not possible with a select statement.
You can delete a row and insert the same row + your changes which is in many ways like an update, but will cause lots of trouble with foreign keys.
Oh, and your DBA might kill you.
You can use
REPLACE INTO tablename(primary key, ...{rest of the columns in the table})
VALUES(the same primary key, new values );
This will delete the previous row and insert a new row with the same primary key and updated column values. Not so much worthwhile, but maybe there is some other way.
It depends what tools you are using and what you actually want to achieve.
There are libraries which allow you to update data you got from a select statement. (eg. ORM's like NHibernate, I think ADO.NET also). These libraries are writing the update statements for you
You can use functions or triggers which change data when you just perform a select statement. In these functions or trigger, you still have an update statement.
For security reasons, you have to make sure that nobody injects an update statement into your select statement. So it is not just save to only perform a select statement.
how to update some records of a table
without using update statement.
Use a MERGE statement.
it is possible using select statement.
Logically, an update is a delete and an insert: INSERT INTO..SELECT to a staging table, modifying the data as appropriate, then DELETE then INSERT INTO..SELECT from staging table.
On the off chance you were asking how this happened when a module ran a select statement it created, then you need to read up on SQL injection. You cannot do an update without an update statment of some kind (includiing not only update but doing delete and then insert or useiing merge) and the user must have update permission on a table, but you can add an update to a select statement that is dymanically created if you haven't correctly parametized it to avoid SQL injection.

Mysql on update and default

I want to have a column on a table that is automatically updated when the row is updated, this column is a soundex version of another column, hypothetically, something like this:
CREATE TABLE `test` (`title` VARCHAR(255), `title_soundex` VARCHAR(255) DEFAULT SOUNDEX(`title`) ON UPDATE SOUNDEX(`title`));
Something like this is even possible? Is there any other approaches to make this happen solely on mysql without updating the code that uses this table ?
Yes, it is possible, but not using constraints.
You would need to create an on update trigger on the test table that would check if the title has been updated, and if so, update the title_soundex field.

null values in mySQL

I've got a new website moved to my server. This website uses PHP and MySQL, and is built very poorly.
The problem is - it needs non-null values inserted into new records where ever I don't specify a value, though the default value is null.
I've been told it has been done on the previous server, but they have no idea how. I'd be glad to receive some help on this.
You could update the default values of the fields of your database to prevent problems using:
ALTER TABLE `table` ALTER `field` SET DEFAULT 'value'
More information on ALTER TABLE for specific fields and parameters can be found in the documentation.
You need to add default values for the columns, either recreate the tables with defaults or alter the table definitions.
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
http://dev.mysql.com/doc/refman/5.1/en/create-table.html