lets say i have table like this (new_table) (all field is not-null constraints):
id name km_unit
1 honda 1000
2 toyota 2000
3 bmw 1000
4 wuling 1500
i want to update the table with insert with this query:
insert into new_table(id,km_unit) values
(1,20000),
(2,20000),
(3,200000),
(4,200000)
ON CONFLICT (id)
DO
update SET km_unit = EXCLUDED.km_unit
but it return error like this :
null value in column "name" violates not-null constraint
the question is how to update the existing km_unit field if the id is same with values that i inserted?
can i update the table without writing name field in the values?
It seems you don't actually want to insert anything, so use an UPDATE statement:
update new_table dl
set km_unit = v.km_unit
from (
values
(1,20000),
(2,20000),
(3,200000),
(4,200000)
) as v(id, km_unit)
where v.id = dl.id
NOT NULL constraint
When the NOT NULL constraint is defined for a column, a row containing the null value in that column cannot be added, nor can a row be updated so as to set the null value in that column. A column for which the NOT NULL constraint is defined must have a definite value in every row. An attempt to set the null value in such a column results in a constraint violation.
In your case it means the column NAME should have a value.
Related
I have a column 'name' in student table. I need to add NOT NULL constraint on this column. But I get SQL error saying cannot add null constraint since the existing rows in the table has null values in the column. How would I add a null constraint along with default value in a single alter statement. Below is my query.
alter table Student alter column name nvarchar NOT NULL;
SQL Server does not make this easy. I think the only way is to set the existing values to the default that you want. Then change the column to have a default value and not null:
-- Get rid of the existing `NULL` values
update students set name = '' where name is null;
-- Add the NOT NULL constraint
alter table students
alter column name varchar(255) not null;
-- Add a default value
alter table students
add constraint df_t_name default '' for name ;
Here is what it looks like in practice.
But I get SQL error saying cannot add null constraint since the existing rows in the table has null values in the column.
Have you tried overwriting the existing NULL values?
You can't have a constraint on a column when existing values would violate that constraint. You need to make your table compliant first.
Hi I'm trying to insert values in the last column for my table from another table but am getting error ERROR:
null value in column "name" violates not-null constraint DETAIL:
Failing row contains (ddf1caf0-26c2-49e1-8a73-64227eae1f50, null,
null, null, null, null, 2532).
I suspect that you want to update the column subsystem of the table software_instances with values of the column sub of the table temp_subsystem:
update software_instances si
set subsystem = ts.sub
from temp_subsystem ts
where ts.module = si.module
there are two solutions to this issue
make sure the value you are selecting have the name value populated.
Add where name!=null in the select query
or
alter the software_instance table , to accept null values for name column.
ALTER TABLE SOFTWARE_INSTANCES ALTER COLUMN NAME DROP NOT NULL
how to add not null column in existing table and then insert values in that column ??
in sql...
If you want to add a NOT NULL column you must specify a DEFAULT:
ALTER TABLE YourTable
ADD SomeColumn INT NOT NULL
CONSTRAINT DF_YourTable_SomeColumn DEFAULT(0);
Other possibility is to add it with NULL, add your data and alter it to NOT NULL later (see ALTER TABLE)
EDIT: Your comment about "how to insert values"...
This depends very much in your needs. If you want to set all rows to the same value it is:
UPDATE YourTable SET SomeColumn=0;
I want to add a column with the same value across the rows to a table. I use this:
ALTER TABLE dbo.MyTable
ADD MyColumnName VARCHAR(20) NULL
DEFAULT 'A201412'
WITH VALUES
It almost works fine but it creates some stupid constraint to a column. Other existing columns do not have any constraints. How to create a column without a constraint?
That "stupid" constraint is what the DEFAULT keyword is really used for.
Using DEFAULT when you create a column means that that value will be used when no value is specified in an INSERT for the column, for example
CREATE TABLE test (
a Int
, b Varchar(5) DEFAULT 'foo'
);
INSERT INTO test(a) VALUES (1)
will generate the row
a | b
1 | foo
instead of the row
a | b
1 | NULL
As Gordon already said, don't add the DEFAULT, then use an UPDATE to put the initial value in the new column
ALTER TABLE dbo.MyTable ADD MyColumnName VARCHAR(20);
UPDATE dbo.MyTable SET MyColumnName = 'A201412'
Just don't add the default clause. Simply use:
ALTER TABLE dbo.MyTable ADD MyColumnName VARCHAR(20);
I have a table defined like this:
CREATE TABLE wp_master (
gid integer NOT NULL DEFAULT nextval('wp_master_gid_seq'::regclass),
name character varying(80),
....
type integer DEFAULT 4,
CONSTRAINT p_key PRIMARY KEY (gid),
);
I want to insert data into the table from another table so I
insert into wp_master ( name, .... type) select "NAME", ...., 1 from ."Tiri2011";
but I get the error:
ERROR: duplicate key value violates unique constraint "p_key"
DETAIL: Key (gid)=(2) already exists.
Why is postgres trying to put anything into the gid field when I have explicitly not included it in the list of columns? I assumed that gid pick up its value from the sequence.
Russell
Is is trying to insert the next value of the wp_master_gid_seq sequence. Declaring an id column as serial (auto-increment) will create a sequence which has a stored value of the last inserted id which was auto-incremented. If at anytime you inserted a gid value manually, it bypassed the sequence and the autoincrement function may become broken, because the sequence value did not get updated accordingly.
The easiest way to fix it is to change the value of the sequence to the (max gid value of your table) + 1. Just execute this once and you should be ok
select setval('wp_master_gid_seq', coalesce((select max(id)+1 from wp_master), 1), false)