0

Can i insert multiple values from different columns in one?

i have:

ref | alt | couple_refalt
--------------------------
 A     C       AC ( i want)
 A     G       AG         Etc...

Is there a simple way?

I tried with:

INSERT INTO refalt(couple_refalt)
SELECT ref||alt
FROM refalt
WHERE ref='A';

Is it correct?

But it gives me the error:

null value in column violates not-null constraint

Postgres want a value for each colum, why can't i update or insert into specific column?

7
  • 1
    You're on the wrong track... Never, ever store data as comma separated items. It will only cause you lots of trouble! Commented Aug 11, 2016 at 10:35
  • Ok Thanks i'll chage the way i want to store it, but the SQL is it correct? Commented Aug 11, 2016 at 10:41
  • To achieve what you want you may replace (ref,alt) by ref || alt. But jarlh is right - it is bad design. Commented Aug 11, 2016 at 10:41
  • The correct syntax is insert into t1 (c1, c2, ...) select ca, cb, ... from t2 ... Commented Aug 11, 2016 at 10:48
  • Seems correct but here's a error: null value in column violates not-null constraint Error row contains: (null, null, AG ) Commented Aug 11, 2016 at 10:48

2 Answers 2

1

Storing comma separated value is not the SQLish way to store values. What you seem to want is a computed column. Postgres does not support that directly. One method is to declare a view:

create view v_refault
    select r.*, ref || ',' || alt
    from refault;

Other possibilities are:

  • Define a trigger to maintain the value.
  • Concatenate the values at the application level.
  • Use a function-based method to emulate a computed column.
Sign up to request clarification or add additional context in comments.

Comments

0

In order to insert two values into one column, you need to concatenate them. In postgresql the syntax is the following.

SELECT ref::text || ', ' || alt::text FROM refalt

If you want more details, here is the string documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.