This question already has answers here:
ERROR 1062 (23000): Duplicate entry '2147483647' for key 'PRIMARY'
(3 answers)
Closed 2 years ago.
yesterday I had the problem that I couldn't insert multiple values into my columns. It turned out that the problem was that my key was missing an auto_increment. I've been trying to to the same thing(inserting multiple values into columns) now on another table but for some reason it doesn't work for me. I made the same changes as I did to the other table which works now. I can't imagine what I could've missed, can anyone help me with this?
Here the table specification and error:
When you mark the PK as auto-increment you do not have to insert values to that entry anymore as it will be incremented automatically, therefore will not trigger any duplicate problems due to being different to the previous PK entries. I suggest you to add only the values of the non PK columns.
INSERT INTO tbllaptops(L_ProzTyp, L_Akku, L_MietgebuehrProTag) -> VALUES ('i56600', '8000mha', '8.5')
Related
This question already has answers here:
oracle constraints datatype
(2 answers)
Closed 2 years ago.
I am making a naive ratings platform db and I want to restrict a rating to be whole numbers from 1 to 5. To clarify I do not want to round or truncate, I want it to show a constraint violation if anything except 1,2,3,4,5 is entered.
The data type I'm using is smallint for rating. If I input 2.7, say, it truncates to 2 and proceeds to add the relation instance to the table. Which I don't want. How can I add a constraint to prevent this?
This is what the CHECK keyword is for (https://www.w3schools.com/sql/sql_check.asp)
If you had a table
CREATE TABLE Reviews (
Rating smallint CHECK (Rating IN (1,2,3,4,5)
);
This question already has answers here:
How to get the identity of an inserted row?
(15 answers)
Closed 5 years ago.
Here's what I'm trying to do: I add a record to the table. This record receives its unique ID number (identity). Then I want to use this ID in my code. How can I find out which ID number has just received a newly added record in my SQL Server database table?
Add an output parameter to your insert statement and return scope_identity(). If this isn't sufficient just search my answer, it will be in here a bunch.
This question already has answers here:
Insert, on duplicate update in PostgreSQL?
(18 answers)
How to UPSERT (MERGE, INSERT ... ON DUPLICATE UPDATE) in PostgreSQL?
(7 answers)
Closed 9 years ago.
i'm manually updating table in my DB using import functionality in PostgreSQL. Working with large number of data i came across an issue of duplicating primary keys. I am looking for a script to upload only values that do not violate primary key assumption, and those that to violate are to be ignored (not uploaded or updated).
I have already seen a code that would kind of do what i need however not quite sure if it will work for me.
Columns i am working with are:
acc_ph_id (primary_key);acc_ph_no;ph_type;ph_flag
Any suggestions will be highly appreciated as i am rather new to Postgresql in general.
Upload the table into a staging table with no constraints.
Then load the table into the full table eliminating duplicates:
insert into real_table(acc_ph_id, acc_ph_no, ph_type, ph_flag)
select distinct on (acc_ph_id) acc_ph_id, acc_ph_no, ph_type, ph_flag
from staging_table
order by acc_ph_id;
EDIT:
Oh, if the problem is the keys that already exist, then do:
insert into real_table(acc_ph_id, acc_ph_no, ph_type, ph_flag)
select distinct on (acc_ph_id) acc_ph_id, acc_ph_no, ph_type, ph_flag
from staging_table st
where not exists (select 1
from real_table rt
where rt.acc_ph_id = st.acc_ph_id
)
order by acc_ph_id;
This question already has answers here:
SQL Server, How to set auto increment after creating a table without data loss?
(7 answers)
Closed 9 years ago.
I have imported a table from one database to another, the issue is the auto increment ID has now been removed, I want to add the primary key back to the original column on my copied table and get it working again by taking the last max ID when it was working and continuing to add 1 as normal, is this possible.
Thanks P
Just Check the Table.Whether Identity is must be there..Remove the identity..So it wil start from 1
The Query
DBCC CHECKIDENT('Your Table Name', RESEED, 0)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL Server: Is it possible to insert into two tables at the same time?
Select from one table and insert to another two table
I have two tables with one-to-one relationship
according to the result of a select statement I need to insert the resultant values in those tables
I really need to avoid cursors, do you know how I could insert in both tables with one select statement , or do you know some other solution that is better in performance, the table I'm querying is expected to get really huge and looping through it would be bad
thanks in advance