Cant get my trigger to work [closed] - sql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
CREATE TRIGGER noOfBooks
AFTER INSERT ON BooKLoan
FOR EACH ROW
BEGIN
UPDATE Book SET noOfLoan=noOfLoans + 1
WHERE Bookloan.bookTitle= :new.bookTitle;
END;
/
(adding one to the noOfLoans column for a book title in table Book, after each time that book title is entered into a new loan row in table BookLoan)
Can anyone help me to the solution please ?

Making some wild assumptions here that you want to increment Book.noOfLoans every time an associated BookLoan record is inserted, There are at least 2 issues in your code:
UPDATE Book
SET noOfLoan=noOfLoan + 1
WHERE Book.bookTitle = :new.bookTitle;
noOfLoan or noOfLoans but not both
Since the trigger is on Bookloan, and it seems you want to update Book, you'll need to filter on Book.bookTitle, not Bookloan (since the new pseudo row is already a Bookloan row)

Related

Is there a faster way to add new column data with hundreds of rows? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I have a decent-sized table 'countries' with about 10 columns. I recently added a new column to the table 'GDP' and I am entering the information per row using
UPDATE countries
SET GDP=(value)
WHERE name = (country);
I have 200 countries in this table. Is there a better way to update this information?
This project is from Khan Academy. We are asked to select a database and run queries on it. The data was pulled from https://gist.github.com/pamelafox/ad4f6abbaac0a48aa781
I am just making sure that there isn't a more efficient way of adding this information to the existing table 'countries' where the primary key is the name of the country.

How does one query a database? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
How does one get the first name info from a Users table, which has a first name column?
First of all welcome to the community. I suggest you read the guidelines on how to properly ask a question here and you might want to write a title that is relevant to your question.
Now to the question:
Assuming tblUsers is a table in your database and 'first_name' is a column in your table, you can use SELECT followed by the column you want to select.
Lastly, add FROM to select which table you want to select from.
So in your case it would be:
SELECT first_name FROM tblUsers;
You can read more about SELECT here

SQL update field based on conditions of another field [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Super noob here.
I want to update column AI_Amount to 0 whenever column Delete_Field = C.
When a customer leaves the delete_field gets updated to a C but the AI_Amount field will not zero out. How do I go about doing this?
I tried using the update clause but it says I don't have permission. I don't want to update the actual table just the query results.
I don't want to update the actual table just the query results.
You seem to want select, not update:
select
delete_field,
case when delete_field = 'C' then 0 else ai_amount end as ai_amount,
-- other columns here
from mytable

Make SQL query fail on trying to delete non-existent record [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Is there standard SQL syntax for causing query to fail when a query tries to delete a non-existent entity? E.g.
DELETE FROM entity WHERE id = 501;
does not fail, even when there is no entity with id = 501. Can it be done in cross-database way?
Depending on environment you can get deleted row count, where you can tread 0 as error.
Or you can have a trigger that raises an exception if no rows deleted.
Here is one way you can handle this in Oracle:
BEGIN
DELETE FROM test
WHERE id = 4;
IF (SQL%NOTFOUND) THEN
dbms_output.put_line('It is already gone');
END IF;
END;
/
Here is a small demo.

Block pl/sql whit HR db [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
a Create a PL / SQL block that shows ids not assigned to employees.
b. Create a PL / SQL block where you place the department's id and return the name of the head of that department.
c. Create a PL / SQL block where you place the id of the employee and return the names of the charges it has occupied in the past.
the ponit A ITS DONE ---- all the id's are full so it does not show results.
enter image description here
the point B dont work
enter image description here
As mentioned in my comment:
Looks the dbms_output is not enabled in your session. Click on the plus sign at the end and choose the schema and then re-run the block. See below circled in red.