Can you write SQL DELETE code the same as INSERTING a value? - sql

Randomly decided to dive into some SQL reading.
Can I write a DELETE statement just like a INSERT statement?
example
INSERT TABLES table (column1, column2)
VALUES ('cell1', 'cell2);
DELETE FROM table (column1, column2)
WHERE ('value', 'value');
instead of
DELETE FROM table
WHERE column1 = 'value', column2 = 'value';

Neither of those statements is valid. You can write:
DELETE FROM table
WHERE column1 = 'value' and column2 = 'value';
Some databases allow:
DELETE FROM table
WHERE (column1, column2) = ('value', 'value');

Related

postgresql conditionnal insert values

I need a SQL which do following work.
INSERT INTO table_1 (column1, column2, column3) VALUES (:1,:2, :3)
This insert will only happen when another condition is TRUE, the
condition is:
SELECT 1 FROM table_2 WHERE column_time = 'time_stamp'
Table_1 and table_2 dont have any relation. the INSERT values to table_1 should only happen if the query on Table_2 return TRUE(1)
So i try to write my SQL as following
INSERT INTO table_1 (column1, column2, column3) VALUES (:1,:2, :3)
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING
However, this SQL not work as Postgresql looks dont like the INSERT combine with WHERE condition.
BTW: the values (:1, :2:, :3) are bind array values. the final SQL looks like this:
INSERT INTO table_1 (column1, column2, column3) VALUES ('ca_1','cb_1', 'cc_1') ('ca_2','cb_2', 'cc_2') ... ('ca_n','cb_n', 'cc_n') WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'my_time_stamp') ON CONFLICT DO NOTHING
Really need help thanks.
You need to select those values.
This would work in Postgres:
INSERT INTO table_1 (column1, column2, column3)
SELECT :1, :2, :3
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING
Alternatively:
INSERT INTO table_1 (column1, column2, column3)
SELECT x.*
FROM (VALUES (:1, :2, :3)) AS x(column1, column2, column3)
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING

How to skip exception raised in INSERT statement?

When I run the following SQL code:
INSERT INTO table
(column1, column1)
SELECT column1, column2
FROM other_table
WHERE column2 = 'foo'
ON CONFLICT DO NOTHING;
I get
PG::CheckViolation: ERROR:
new row for relation "table" violates
check constraint "tables_column1_valid_first_char"
How can I skip rows violating that check constraint?
I tried
INSERT INTO table
(column1, column1)
SELECT column1, column2
FROM other_table
WHERE column2 = 'foo'
ON CONFLICT ON CONSTRAINT tables_column1_valid_first_char DO NOTHING;
But it fails with
PG::WrongObjectType: ERROR:
constraint in ON CONFLICT clause has no associated index
The constraint looks as following:
ADD CONSTRAINT tables_column1_valid_first_char
CHECK (SUBSTRING(text, 1,1) ~* '^[a-zàâçéèêëîïôûùüÿæœ]');
ON CONFLICT only deals with unique constraints, it doesn't work for check constraints (or foreign key constraints).
You need to exclude those rows by applying a WHERE clause:
INSERT INTO table (column1, column1)
SELECT column1, column2
FROM other_table
WHERE column2 = 'foo'
and SUBSTRING(column1, 1,1) ~* '^[a-zàâçéèêëîïôûùüÿæœ]'
ON CONFLICT DO NOTHING;

INSERT INTO subtract 2 values

Is it possible for an INSERT query to subtract 2 values you have entered to create a 3rd value that can then be inserted into a table - if that makes sense...
e.g.
INSERT INTO table1 (column1, column2, column3)
VALUES ('50', '25', column1 - column2)
INSERT INTO table1 (column1, column2, column3)
(select ('50', '25', column1 - column2) from table1 where conditions)
This is a sample query! hope it helps!
Convoluted:
INSERT INTO table1 (column1,column2,column3)
select column1,column2,column1-column2
from
(select 50 as column1,
25 as column2
) t
Since you can't reference other columns from the same SELECT clause, you have to do it as a subquery. I've also switched to using int literals rather than strings, because I can't make subtraction make sense in my head otherwise.
You could also do it using a Table Value Constructor:
INSERT INTO table1 (column1,column2,column3)
select column1,column2,column1-column2
from
( VALUES (50, 25)
) AS t (column1, column2);
As indicated in my comment though, if the relationship should always hold, I'd build table1 as:
CREATE TABLE table1 (
column1 int not null,
column2 int not null,
column3 as column1 - column2
--More columns
)
Because that way, the column3 value is always correct.
You can create function that subtracts values and use this function in insert. This is the right way to do such things:
INSERT INTO table1 (column1, column2, column3)
(select ('50', '25', your_function() ) from table1 where conditions)
/
Using the "INSERT INTO" would do this:
INSERT INTO Table1Name (column1, column2, column3,)
(select 'X', 'Y', X - Y as Z)
Here is a link to SQL Authority with more examples of INSERT INTO
Another method would to be add a trigger to the table, where on insert of data, the third column would be updated with the difference of the first two columns.

insert into with select + new information

I'm trying to write a sql query that can copy specific columns from a table and insert it in the same table + extra information for the other columns
Simply copying certain information would be something like this:
INSERT INTO table (column1, column2)
SELECT column1, column2
FROM table
WHERE columnx = 'some value'
But I need to also insert some new information in column3. How can I do that?
I have the information that will go in "column3", I don't have to get it from an other table or source.
This is for a repeat appointment where basically all the information is the same except for date, planner and appointment_id.
If you know what the values are . . .
INSERT INTO table (column1, column2, column3)
SELECT column1, column2, <value for column3>
FROM table
WHERE columnx = 'some value'

Sql Query Create a new Table

Can anyone offered syntax for a sql query that takes an already created table and inserts some of the data into a new table with a new id(primary key)? So far I tried this
SELECT ((ID INT IDENTITY(1,1) PRIMARY KEY), column1, column2, column3)
INTO table_name
FROM table_name
I keep coming up with a syntax error.
SELECT column1, column2, column3
INTO table_name_new
FROM table_name_old
use like this
INSERT INTO newtable (column1, column2, column3)
SELECT (column1, column2, column3)
FROM table_name
INSERT INTO new_table
(Column1, Column2 . . . )
SELECT Column1, Column2 FROM old_table
Don't include the identity column (the primary key with the auto-generated numbers) in the insert list.
Just make it like below, Aa new id will be automatically created while inserting the record
insert into table1 (col1,col2,col3) select col1,col2,col3 from table2;
According to the documentation on MSDN, the IDENTITY property will transfer through with a SELECT INTO clause, but constraints will not. That means you can create a new table with an IDENTITY column if you include the original IDENTITY column from your source table, but the column in the new table won't be a primary key.
The sane way to do this is to use ALTER TABLE after the table is created, as #Kai has suggested.