insertSelective allows us to insert only into specified columns, but isn't it the same as insert, when we keep an object's attributes null which we do not want to insert into the columns?
For example, if a User
class User{
Integer id;
String name;
String address;
}
What is the difference between insert(User{null,"Jack",null} and insertSelective(User{null,"Jack",null}?
The difference between insert() and insertSelective() is that insert() will insert all columns specified in the object, including null values, while insertSelective() will only insert non-null values.
So, in your example, if you used insert(User{null,"Jack",null}), it would insert a new row with null values for the id and address columns. If you used insertSelective(User{null,"Jack",null}), it would only insert a new row with the non-null value of "Jack" for the name column.
Related
I get an error trying to insert data into my tables ... but I don't know why?
Syntax is correct.
column "population" is of type integer but expression is of type record
create table states(name varchar(25), population int );
create table countries(name varchar(25), population int );
insert into states values (('tn',54945),('ap',2308));
select name from states;
insert into countries values (('india',3022),('america',30902));
select * from countries;
There are extra parentheses around the tuples of values to insert, which turns the whole thing to a single record of records.
Instead:
insert into countries(name, population) values ('india',3022),('america',30902);
Im trying to write a stored procedure in which I can upsert a row even if one of the values in the key is null. I've read the documentation and found that Postgres doesn't work with comparing equality of null values.
I've also read other forum posts and noticed that what I want to achieve can be done through a partial index. I'm able to successfully get a constraint violation, however my "on conflict" never gets hit when i pass in a value that has a null birthday.
I want to be able to pass in a null birthday and update an ID for a person even if their birthday is null.
(
id bigint not null,
name text,
birthday date
);
I create an index and partial index so that it allows birthday to be null
CREATE UNIQUE INDEX name_birthday_key on people (name, birthday);
CREATE UNIQUE INDEX birthday_null_key on people (name) where birthday is null;
create or replace procedure store_person(_identifier bigint, _name character varying, _birthday date)
language plpgsql
as
$$
begin
insert into people (
id, name, birthday
)
values (
_identifier, _name, _birthday
)
on conflict (name, birthday)
do update
set
id = _identifier
where people.birthday = _date and people.name = _name;
end
$$;
if I run:
call public.store_person(1, 'Bob', '1955-01-09')
call public.store_person(2, 'Bob', '1955-01-09')
i successfully see that the only row in the DB is Bob with an ID of 2.
however, if i run
call public.store_person(3, 'Joe', null)
call public.store_person(4, 'Joe', null)
the only row i get is ID 3. the second insert for ID 4 never updates the existing row. I do get a violation error but the "on conflict" update never is hit.
can someone point me in the right direction of how to do this?
The CONFLICT doesn't match because NULLis not equal to NULL. This is not a PostgreSQL thing, it's defined in SQL standard.
Use something like COALESCE(birthday, '0001-01-01') when inserting your data, that will match; and remove the partial index.
Your code has an error, in DO UPDATE...WHERE: there's nothing named _date, should be _birthday.
Mysql database has a table(id bigint auto_increment primary key, xx), and springboot code follows:
#Insert({"insert into table(xx) " +
"values (#{xx}"})
#Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
int save(Data data);
We all known, our insert sql language does not contail id field, when we specify useGeneratedKeys=true, keyProperty="id",then we invoke save method, the object data return the id value(which success to insert into the database and return valid id value).
But insert into sql only return affected rows, why return valid data.id value?
The distribute system how to ensure the unique id from mybatis returned if mybatis has not second query id from mysql?
How the mybatis realize it?
Does it query the id after insert into sql??
So does the insert into sql has two request database(first is insert into, second is query id value)?
Table only has String column as primary column EMPLOYEE_ID how to partition it.
val destination = spark.read.options(options).jdbc(options("url"), options("dbtable"), "EMPLOYEE_ID", P00100001, P00100005000000, 10, new java.util.Properties()).rdd.map(_.mkString(","))
Is there any other way to Read JDBC table and process it.
It is not possible. Only integer columns can be used here. If your database supports some variant of rowid, which is integer or can be casted to integer, you can extract it in a query (pseudocode):
(SELECT CAST(rowid AS INTEGER), * FROM TABLE) AS tmp
My table plugins has just two columns: ID and DTYPE. I would like to store a String in DTYPE column. ID should increment automatically and be returned by insert method. For that purpose I created manually "id_sequence" like this:
CREATE SEQUENCE id_sequence
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
OWNED BY plugin.id;
This is my mapper.xml file:
<insert id="insert" useGeneratedKeys="true" parameterType="String">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
SELECT CAST(nextval('id_sequence') as INTEGER)
</selectKey>
INSERT INTO plugins (ID, DTYPE)
VALUES ( #{id}, #{plugin})
RETURNING ID;
</insert>
and corresponding mapper.java:
int insert(String plugin);
Problem statement: 1) I cannot get a proper value of ID (it is null), 2) I get an error
SQL: INSERT INTO public.ucpoplugin (ID, DTYPE) VALUES ( ?, CORRECT_STRING)
Cause: org.postgresql.util.PSQLException: FEHLER: Column »id« is of type bigint, but the sentence is has Type character varying.
I recomend to use auto increment on that id column and then do not pass just string as parameter, but pass whole object. After insert that property will be filled.
See: http://edwin.baculsoft.com/2010/12/beginning-mybatis-3-part-3-how-to-get-tables-generated-ids/
Or: Returning values from MyBatis <insert> mapped methods