SQL Script to insert a object - sql

I am working on a homework assignment and this is the question:
Write a script that attempts to insert a new division named “Guitars” into the Division table. If the
insert is successful, the script should display this message:
SUCCESS: Record was inserted.
If the update is unsuccessful, the script should display a message something like this:
FAILURE: Record was not inserted.
Error 2627: Violation of UNIQUE KEY constraint
'UQ__Division__5A98D48EAD30FDC4'. Cannot insert duplicate key in object
'dbo.Division'. The duplicate key value is (Guitars).
I honestly have no Idea where to start, just looking for some help. If you are going to give the full answer please explain a little if you can so I can actually learn. Thanks

Related

ORA-02049 after many failed inserts

Can you please help me to identify what's wrong. I have a table into which I perform a really big select insert from another table. the first table is from a dblink. After a few unsuccessful tries and tests (my query was wrong then) it started throwing the said ora and doesn't allow me to insert even a single row. I can select from there though and no data is lost. How can i start inserting there again?

Query running is too long when I delete some records in Postgres

When I run this line of code.
DELETE from monthlyevaluatedbudgettable where budgetforyear = 2018;
It was only saying in the status bar Query is Running. and I think that It will not end up, so I will cancel it, and then this warning post in the messages of the logs of error:
ERROR: canceling statement due to user request
CONTEXT: SQL statement "DELETE FROM ONLY "public"."monthlyadjustedbudgettable" WHERE $1 OPERATOR(pg_catalog.=) "budgetid""
********** Error **********
ERROR: canceling statement due to user request
SQL state: 57014
Context: SQL statement "DELETE FROM ONLY "public"."monthlyadjustedbudgettable" WHERE $1 OPERATOR(pg_catalog.=) "budgetid""
The relationship of monthlyadjustedbudgettable is ON DELETE CASCADE on monthlyevaluatedbudgettable. Can someone tell me what would be the problem ?
I have 182,095 records in both of the table. their relationship is one-to-one.
You should first try using EXPLAIN on your current query to see what is happening in detail.
My hunch as to why your delete query is so slow is that you have an ON DELETE CASCADE constraint on monthlyadjustedbudgettable. This means that for each record in monthlyevaluatedbudgettable a check must be made in monthlyadjustedbudgettable to see if any records there need to be removed as well. Since there is no index on that table, a full table scan likely is happening. Given that you have about 200K records in each table, this might be prohibitively large in terms of time needed.
There is a quick fix you can try. You can add an index on the foreign key column in monthlyadjustedbudgettable:
CREATE UNIQUE INDEX budget_idx ON monthlyadjustedbudgettable (budgetid);
This assumes that the foreign key column in monthlyadjustedbudgettable is called budgetid.

Insert statement attempted to include a duplicate key

Trying to run
INSERT INTO BOOKING_EXTRAS (BOOKING_ID, EXTRAS_, EXTRAS_PRICE) VALUES ('1','Phone call: 1.80','1.8');
in Oracle SQL Developer. Ive had it running but when I close it, then reopen it I get this error:
Error starting at line 1 in command:
INSERT INTO BOOKING_EXTRAS (BOOKING_ID, EXTRAS_, EXTRAS_PRICE) VALUES ('1','Phone call: 1.80','1.8')
Error report:
SQL Error: ORA-00001: unique constraint (COURSEWORK_XE.BOOKING_EXTRAS_PK) violated
00001. 00000 - "unique constraint (%s.%s) violated"
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
*Action: Either remove the unique restriction or do not insert the key.
how would I fix this? Its happening to every table I run!
You need to either clear the tables or insert new information, a database doesn't want duplicate rows because that makes it impossible to find the correct rows later.
In addition, if that BOOKING_ID (currently serves as primary key if I guess right) doesn't actually mean something for you, you can set it as AUTO INCREMENT in your schema, then afterwards you don't need to insert a value for BOOKING_ID, the system will automatically find a value which is not duplicate for you. This might save you a lot effort.
INSERT INTO BOOKING_EXTRAS (EXTRAS_, EXTRAS_PRICE) VALUES ('Phone call: 1.80','1.8');
If your unique column is being populated by a sequence, check to make sure that your sequence has the same 'last value' as your highest-value unique column.
I just encountered a problem where the sequence had a 'last value in' that was much lower than the highest value in my unique column. The DBA ran a script to update the sequence to what the number should have been, and my error went away.
Now to find out why the sequence was so wrong...

Avoid duplicate rows in phonegap database

I have a database with a single column and create it like this
function populateDB(tx){
tx.executeSql('CREATE TABLE IF NOT EXISTS RESULT(result, UNIQUE(result))');
}
however, when I want to write objects into the table, I get the error:
could not execute statement (19 constraint failed)
The error only appears when I add the NOT NULL and PRIMARY KEY to the set-Up. Without it, it keeps writing duplicates in my database (I fill the database with data from a webservice on startup of a phonegap iOS application, so I want to catch duplicate rows on database level)
At first I had only "PRIMARY KEY" in it, and because the error says something about constraints, I was assuming the primary key has to be "NOT NULL", adding it to the code, I still got the same error.
how can I solve this. thanks in advance
edit: Maybe it´s important for you to know that I write stringyfied JSON Objects into the results column.
edit: I insert the object in this function. Basically I do it one by one:
this.save = function(){
db.transaction (function(transaction){
object = $.toJSON(self);
object = encodeURI(object);
transaction.executeSql('INSERT INTO RESULT (result) VALUES ("'+object+'")');
},
errorCB,
successCB
);
}
edit: I updated my set up table code. The error doesnt occur on every row, but the objects are actually unique.
"IF NOT EXISTS" is the catch here. You get constraint failed when you use conditions like "IF NOT EXISTS" or "REPLACE", "IGNORE" etc in SQLite queries.
Suppose, you're trying to create a table that already exists, then the constraint (here not creating the table again if it already exist) fails and hence the query fails (which you actually want - you dont want to duplicate the tables right?) and gives you the error constraint failed.
You may get a similar error on INSERT or REPLACE, INSERT or IGNORE, etc...

Cannot insert duplicate key row in object 'dbo.TitleClient' with unique index 'XAK1TitleClient'

Ever since I cleaned the data on the SQL Database I've been getting this issue, whereas on the unclean database the issue does not happen. When I run my stored procedure (huge procedure) it returns:
General SQL error. Cannot insert duplicate key row in object 'dbo.TitleClient' with unique index 'XAK1TitleClient'. Cannot insert the value NULL into column 'id_title', table 'Database.dbo.TitleCom'; column does not allow null, insert fails.
Is it possible that I deleted data from a table that causes this? Or is that impossible?
Does dbo.TitleClient have an identity column? You might need to run
DBCC CHECKIDENT('dbo.TitleClient')
I'm guessing that the first message
Cannot insert duplicate key row in
object 'dbo.TitleClient' with unique
index 'XAK1TitleClient'
is because the seed value is out of synch with the existing table values and the second error message
Cannot insert the value NULL into
column 'id_title', table
'Database.dbo.TitleCom' column does
not allow null, insert fails.
Comes from a failed attempt at inserting the result of scope_identity from the first statement.
How cleanly did you "clean" the data?
If some tables still have data, that might be causing a problem.
Especially if you have triggers resulting in further inserts.
For you to investigate further.
Take the body of your stored proc, and run it bit-by-bit.
Eventually, you'll get to the actual statement producing the error.
Of course if you aren't inserting into dbo.TitleClient at this point, then it's certainly a trigger causing problems.
Either way: Now you can easily check the data inserted earlier in your proc to figure out the root cause.