Good day,
I have a table, which some columns, and BELTID set to primary key, and IS_AUTOINCREMENT set to YES.
I wish to insert a row of data inside this data without key in the BELTID, I am expect the BELDID will auto generate.
The query is as follow:
INSERT INTO mySchema.TABLE1(TYPE, ORIGINALBATCHID, MANUAL)
VALUES ('TEST', 124, 1);
I get this error:
SQLSTATE: 23505. A violation of the constraint imposed by a unique index or a unique constraint occurred.
Then I change my query to:
INSERT INTO mySchema.TABLE1(BELTID, TYPE, ORIGINALBATCHID, MANUAL)
VALUES (123, 'TEST', 124, 1);
And I am getting another error:
SQLSTATE: 428C9 A ROWID column cannot be specified as the target column of an INSERT or UPDATE.
Kindly advise on what mistake I make.
Related
I have a simple table like this:
CREATE TABLE IF NOT EXISTS myval
(
id integer NOT NULL DEFAULT nextval('myval_myval_id_seq'::regclass),
name character varying(255),
CONSTRAINT "PK_aa671c3359a0359082a84ecb801" PRIMARY KEY (id)
)
the sequence definition is:
CREATE SEQUENCE IF NOT EXISTS myval_myval_id_seq
INCREMENT 1
START 1
MINVALUE 1
MAXVALUE 2147483647
CACHE 1
OWNED BY myval.myval_id;
when I insert data along with the primary key:
INSERT INTO myval(id, name) VALUES (1, 'sdf');
INSERT INTO myval(id, name) VALUES (2, 'sdf');
INSERT INTO myval(id, name) VALUES (3, 'sdf');
INSERT INTO myval(id, name) VALUES (4, 'sdf');
then, I insert it without the PK:
INSERT INTO myval(name) VALUES ('new sdf');
it gives an error saying:
duplicate key value violates unique constraint "PK_aa671c3359a0359082a84ecb801",
DETAIL: Key (myval_id)=(1) already exists.
I expected it to start with PK value of 5 but, instead it gives an error. Can we configure postgres to skip conflicting values and generate from the closest available value to use instead of throwing an error?
The best way to avoid such conflicts is to use identity columns - in this case a GENERATED ALWAYS AS IDENTITY seems the right option.
CREATE TABLE IF NOT EXISTS myval
(
id integer GENERATED ALWAYS AS IDENTITY,
name character varying(255),
CONSTRAINT "PK_aa671c3359a0359082a84ecb801" PRIMARY KEY (id)
);
This will work like a sequence (serial), however it will fail if the user tries to manually insert a value in this column
INSERT INTO myval (id,name)
VALUES (1,'foor');
ERROR: cannot insert a non-DEFAULT value into column "id"
DETAIL: Column "id" is an identity column defined as GENERATED ALWAYS.
TIP: Use OVERRIDING SYSTEM VALUE to override.
If for whatever reason you must override this behavior in a certain INSERT statement you can do so using OVERRIDING SYSTEM VALUE, as the error message above suggests
INSERT INTO myval (id,name) OVERRIDING SYSTEM VALUE
VALUES (1,'foo');
You might be able to achieve a sequential value using serial even if the user screws things up with inserts, e.g. using trigger functions. But such an architecture is hard to maintain and imho is definitely not worth the trouble.
Demo: db<>fiddle
I need to add a row to a SQL Server table called Customers.
Here is the table design:
Here how I try to add a row to the table above:
INSERT INTO Customers (Id, Name)
VALUES (1, 'test');
But I get this error:
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with uniqueidentifier
As you can see the ID column of type uniqueidentifier. How do I add a uniqueidentifier to the column above?
The uniqueidentifier data type stores 16-byte binary values that operate as globally unique identifiers (GUIDs).
Example using:
insert into customers values('7E3E3BC1-9B15-473C-A45A-46D89689156C', 'Tomas')
insert into customers values(newid(), 'Tomas')
See more details: https://technet.microsoft.com/en-US/library/ms190215(v=sql.105).aspx
Use newid():
INSERT INTO Customers(Id, Name) VALUES (newid(), 1);
Note that this would often be done using a default value:
create table customers (
id uniqueidentifier default newid(),
. . .
);
For various technical reasons, it is not recommended to make the uniqueidentifier column a primary key -- although you can get around some of the problems using newsequentialid().. Instead, make it a unique key and have another key (typically an identity() column) as the primary key.
H2 (started with MODE=MYSQL on) supports INSERT ON DUPLICATE KEY UPDATE statement only with VALUES clause, while throws a "Unique index or primary key violation" error when using INSERT SELECT statement.
Here is an example:
-- creating a simple table
CREATE TABLE test_table1 (
id INT NOT NULL,
value VARCHAR(255) NOT NULL,
PRIMARY KEY (id))
ENGINE = InnoDB;
-- inserting a value
INSERT INTO test_table1
VALUES (1, 'test1');
-- trying to insert on duplicate key update: it works!
INSERT INTO test_table1
VALUES (1, 'test2')
ON DUPLICATE KEY UPDATE value='test2';
-- trying using INSERT SELECT: it throws Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.TEST_TABLE1(ID)"
INSERT INTO test_table1
SELECT 1, 'test2'
FROM test_table1
ON DUPLICATE KEY UPDATE value='test2';
I'm using H2 db version 1.4.192.
Is it a bug? Or is there something wrong with my code?
Thank you
On you H2 console, if you have 'HIBERNATE_SEQUENCES' table make sure to check what is the NEXT_VAL for SEQUENCE_NAME = 'default'.
In my case, I had 2 row (insert statement) in my /src/main/resources/data.sql and the NEXT_VAL was 2 which was causing problems. I changed to 3 with update statement, and it now works fine.
Is there something wrong with my code?
Yes, there is. Why are you inserting into an auto-increment column? You should be specifying the columns with non-autogenerated data. So:
INSERT INTO test_table1(value)
VALUES ('test1');
And:
INSERT INTO test_table1(value)
SELECT 'test2'
FROM test_table1
ON DUPLICATE KEY UPDATE value = VALUES(value);
Your are getting the error because ON DUPLICATE KEY resets value, but that has nothing to do with the primary key on the table.
I have an odd problem with a table, I'm inserting rows into tables, and now I am trying to test it, on one of my tests it allows an entry that it shouldn't.
My table definition:
CREATE TABLE ITEMS(
ITEM_NUMBER VARCHAR2(3)
CONSTRAINT ITEMS_ITEM_NUMBERf_PK PRIMARY KEY,
ITEM_DESCRIPTION VARCHAR2(30),
ITEM_SIZE VARCHAR2(2),
ITEM_COST NUMBER(30,0),
ITEM_QTY NUMBER(2,0),
ORDER_NUMBER VARCHAR2(4));
When I try and test the ITEM_SIZE column by putting in a number it just inserts it.
INSERT INTO ITEMS VALUES ('I32', 'Leather blazer', 'L', 14.00, 1, 'O114');
INSERT INTO ITEMS VALUES ('I33', 'Fleece vest', 'L', 28.00, 1, 'O128');
INSERT INTO ITEMS VALUES ('I34', 'Khaki pants', 'S', 22.00, 3, 'O122');
INSERT INTO ITEMS VALUES ('I35', 'Muscle Tee', 'M', 12.99, 1, 'O122');
INSERT INTO ITEMS VALUES ('I36', 'Trench Coat', 'M', 03.00, 1, 'O133');
INSERT INTO ITEMS VALUES ('I37', 'Crewneck sweater', 'XL', 07.00, 1, 'O107');
INSERT INTO ITEMS VALUES ('I38', 'Varsity Sweater', 'M', 08.99, 1, 'O108');
INSERT INTO ITEMS VALUES ('I39', 'CROOK', 'M', 12.00, 1, 'O112');
//--the tester--//
INSERT INTO ITEMS VALUES ('I49', 'bROOK', 10, 12.00, 1, 'O112');
In the statement above I insert the number 10 into ITEM_SIZE VARCHAR2(2). How can I avoid that?
As #kevinsky said, Oracle is doing implicit data conversion from number to string while you insert.
If you want to restrict the values that can be put into a column then you can add a check constraint:
alter table items add constraint items_size_chk
check (item_size in ('XXS','XS','S','M','L','XL','XXL','XXXL'));
Table ITEMS altered.
INSERT INTO ITEMS VALUES ('I49', 'bROOK', 10, 12.00, 1, 'O112');
SQL Error: ORA-02290: check constraint (SCHEMA.ITEMS_SIZE_CHK) violated
02290. 00000 - "check constraint (%s.%s) violated"
*Cause: The values being inserted do not satisfy the named check
If you want different restrictions you can change the way the check is done; for example you could have a regular expression check to exclude digits but allow any characters. In this case it seems more likely you want a list of specific values to be allowed though.
Note though that when a new valid size is added, you will have to modify the check constraint; and any of those size values will be allowed for any item.
If the valid sizes are coming from another table then you can use a foreign key constraint instead. You probably really want a list of sizes (or other attributes) that are valid for each item, so your data model might need some fleshing out.
Oracle is implicitly converting the number 10 into the varchar2 string '10'.
and here is another implicit conversion
SELECT item_size + 1 FROM items;
Result in Oracle 11g is
11
I'm not sure whether this was intended to be helpful when the parser was being built but it does cause a lot of confusion. However Oracle will not convert an alphabetical character into a number implicitly. It will fail.
Query:
INSERT INTO `job_listing_has_employer_details` (`job_listing_id`, `employer_details_id`)
VALUES (6, '5')
Error:
Cannot add or update a child row: a foreign key constraint fails (mydb.job_listing_has_employer_details, CONSTRAINT job_listing_has_employer_details_ibfk_2 FOREIGN KEY (employer_details_id) REFERENCES employer_details (id))
What does this mean? The two ID's I am inserting into the table exist.
It means it can't find '5' in the id column of the employer_details table. If there is a 5 in that column of that table, then maybe the data is numeric, so must be passed without quotes. If so, try:
INSERT INTO `job_listing_has_employer_details` (`job_listing_id`, `employer_details_id`) VALUES (6, 5)