How can I increase the value of an attribute of one table on the basis of the insertion of value to an...
I have two tables.
CREATE TABLE `departments` (
department_id INT(2) NOT NULL AUTO_INCREMENT,
department_name VARCHAR(30) NOT NULL,
total_employees INT(4),
PRIMARY KEY (department_id),
UNIQUE (department_name));
CREATE TABLE `employees` (
employee_id INT(4) NOT NULL AUTO_INCREMENT,
employee_email VARCHAR(30) NOT NULL,
employee_first_name VARCHAR(30) NOT NULL,
employee_last_name VARCHAR(30) NOT NULL,
department_name VARCHAR(30) NOT NULL,
PRIMARY KEY (employee_id),
UNIQUE (employee_email),
FOREIGN KEY (department_name)
REFERENCES departments (department_name)
ON DELETE CASCADE);
Now, suppose I've a department which is HRM. 2 employees have HRM department. So, I want the value of total_employees of departments table 2 through a trigger. Would be great if I could get some suggestions of yours.
mysql sql rdbms database-trigger
|
show 5 more comments
I have two tables.
CREATE TABLE `departments` (
department_id INT(2) NOT NULL AUTO_INCREMENT,
department_name VARCHAR(30) NOT NULL,
total_employees INT(4),
PRIMARY KEY (department_id),
UNIQUE (department_name));
CREATE TABLE `employees` (
employee_id INT(4) NOT NULL AUTO_INCREMENT,
employee_email VARCHAR(30) NOT NULL,
employee_first_name VARCHAR(30) NOT NULL,
employee_last_name VARCHAR(30) NOT NULL,
department_name VARCHAR(30) NOT NULL,
PRIMARY KEY (employee_id),
UNIQUE (employee_email),
FOREIGN KEY (department_name)
REFERENCES departments (department_name)
ON DELETE CASCADE);
Now, suppose I've a department which is HRM. 2 employees have HRM department. So, I want the value of total_employees of departments table 2 through a trigger. Would be great if I could get some suggestions of yours.
mysql sql rdbms database-trigger
you don't really need to store the total number of employees in a field. You can just calculate it using a COUNT inside a query at the time when you want to know it. It's kind of a redundant piece of information. If you do really want to store it for some reason, then yes, you can write a trigger. But...what have you tried to do so far? What problem are you having? This isn't a free write-my-code or do-my-research service. You can find examples of this kind of trigger easily. There's no need for us to provide another tutorial here.
– ADyson
Nov 28 '18 at 11:09
See, I ain't expecting you to solve my problem, I'm asking politely for some suggestions.
– Bappi_SB
Nov 28 '18 at 11:13
Ok thankyou. But that wasn't clear from the way you wrote the question before. We can't read your mind. Anyway, please add that code and error information into the question itself using the "edit" button...code in comments is hard to read, and people may not notice it. Comments are just for asking for clarifications / making brief suggestions, not for adding content which is fundamental to the question itself. Thanks.
– ADyson
Nov 28 '18 at 11:13
If you not into suggesting, please ignore, I think there are people who would love to suggest.
– Bappi_SB
Nov 28 '18 at 11:14
1
in an insert trigger there is no "old"...because you're inserting a row, not updating one. There is no previous version of the row, because it never existed before. You can just use the department_id in the newly created row usingnewinstead. See stackoverflow.com/questions/25453535/…
– ADyson
Nov 28 '18 at 11:28
|
show 5 more comments
I have two tables.
CREATE TABLE `departments` (
department_id INT(2) NOT NULL AUTO_INCREMENT,
department_name VARCHAR(30) NOT NULL,
total_employees INT(4),
PRIMARY KEY (department_id),
UNIQUE (department_name));
CREATE TABLE `employees` (
employee_id INT(4) NOT NULL AUTO_INCREMENT,
employee_email VARCHAR(30) NOT NULL,
employee_first_name VARCHAR(30) NOT NULL,
employee_last_name VARCHAR(30) NOT NULL,
department_name VARCHAR(30) NOT NULL,
PRIMARY KEY (employee_id),
UNIQUE (employee_email),
FOREIGN KEY (department_name)
REFERENCES departments (department_name)
ON DELETE CASCADE);
Now, suppose I've a department which is HRM. 2 employees have HRM department. So, I want the value of total_employees of departments table 2 through a trigger. Would be great if I could get some suggestions of yours.
mysql sql rdbms database-trigger
I have two tables.
CREATE TABLE `departments` (
department_id INT(2) NOT NULL AUTO_INCREMENT,
department_name VARCHAR(30) NOT NULL,
total_employees INT(4),
PRIMARY KEY (department_id),
UNIQUE (department_name));
CREATE TABLE `employees` (
employee_id INT(4) NOT NULL AUTO_INCREMENT,
employee_email VARCHAR(30) NOT NULL,
employee_first_name VARCHAR(30) NOT NULL,
employee_last_name VARCHAR(30) NOT NULL,
department_name VARCHAR(30) NOT NULL,
PRIMARY KEY (employee_id),
UNIQUE (employee_email),
FOREIGN KEY (department_name)
REFERENCES departments (department_name)
ON DELETE CASCADE);
Now, suppose I've a department which is HRM. 2 employees have HRM department. So, I want the value of total_employees of departments table 2 through a trigger. Would be great if I could get some suggestions of yours.
mysql sql rdbms database-trigger
mysql sql rdbms database-trigger
edited Nov 28 '18 at 12:03
Bappi_SB
asked Nov 28 '18 at 11:04
Bappi_SBBappi_SB
407
407
you don't really need to store the total number of employees in a field. You can just calculate it using a COUNT inside a query at the time when you want to know it. It's kind of a redundant piece of information. If you do really want to store it for some reason, then yes, you can write a trigger. But...what have you tried to do so far? What problem are you having? This isn't a free write-my-code or do-my-research service. You can find examples of this kind of trigger easily. There's no need for us to provide another tutorial here.
– ADyson
Nov 28 '18 at 11:09
See, I ain't expecting you to solve my problem, I'm asking politely for some suggestions.
– Bappi_SB
Nov 28 '18 at 11:13
Ok thankyou. But that wasn't clear from the way you wrote the question before. We can't read your mind. Anyway, please add that code and error information into the question itself using the "edit" button...code in comments is hard to read, and people may not notice it. Comments are just for asking for clarifications / making brief suggestions, not for adding content which is fundamental to the question itself. Thanks.
– ADyson
Nov 28 '18 at 11:13
If you not into suggesting, please ignore, I think there are people who would love to suggest.
– Bappi_SB
Nov 28 '18 at 11:14
1
in an insert trigger there is no "old"...because you're inserting a row, not updating one. There is no previous version of the row, because it never existed before. You can just use the department_id in the newly created row usingnewinstead. See stackoverflow.com/questions/25453535/…
– ADyson
Nov 28 '18 at 11:28
|
show 5 more comments
you don't really need to store the total number of employees in a field. You can just calculate it using a COUNT inside a query at the time when you want to know it. It's kind of a redundant piece of information. If you do really want to store it for some reason, then yes, you can write a trigger. But...what have you tried to do so far? What problem are you having? This isn't a free write-my-code or do-my-research service. You can find examples of this kind of trigger easily. There's no need for us to provide another tutorial here.
– ADyson
Nov 28 '18 at 11:09
See, I ain't expecting you to solve my problem, I'm asking politely for some suggestions.
– Bappi_SB
Nov 28 '18 at 11:13
Ok thankyou. But that wasn't clear from the way you wrote the question before. We can't read your mind. Anyway, please add that code and error information into the question itself using the "edit" button...code in comments is hard to read, and people may not notice it. Comments are just for asking for clarifications / making brief suggestions, not for adding content which is fundamental to the question itself. Thanks.
– ADyson
Nov 28 '18 at 11:13
If you not into suggesting, please ignore, I think there are people who would love to suggest.
– Bappi_SB
Nov 28 '18 at 11:14
1
in an insert trigger there is no "old"...because you're inserting a row, not updating one. There is no previous version of the row, because it never existed before. You can just use the department_id in the newly created row usingnewinstead. See stackoverflow.com/questions/25453535/…
– ADyson
Nov 28 '18 at 11:28
you don't really need to store the total number of employees in a field. You can just calculate it using a COUNT inside a query at the time when you want to know it. It's kind of a redundant piece of information. If you do really want to store it for some reason, then yes, you can write a trigger. But...what have you tried to do so far? What problem are you having? This isn't a free write-my-code or do-my-research service. You can find examples of this kind of trigger easily. There's no need for us to provide another tutorial here.
– ADyson
Nov 28 '18 at 11:09
you don't really need to store the total number of employees in a field. You can just calculate it using a COUNT inside a query at the time when you want to know it. It's kind of a redundant piece of information. If you do really want to store it for some reason, then yes, you can write a trigger. But...what have you tried to do so far? What problem are you having? This isn't a free write-my-code or do-my-research service. You can find examples of this kind of trigger easily. There's no need for us to provide another tutorial here.
– ADyson
Nov 28 '18 at 11:09
See, I ain't expecting you to solve my problem, I'm asking politely for some suggestions.
– Bappi_SB
Nov 28 '18 at 11:13
See, I ain't expecting you to solve my problem, I'm asking politely for some suggestions.
– Bappi_SB
Nov 28 '18 at 11:13
Ok thankyou. But that wasn't clear from the way you wrote the question before. We can't read your mind. Anyway, please add that code and error information into the question itself using the "edit" button...code in comments is hard to read, and people may not notice it. Comments are just for asking for clarifications / making brief suggestions, not for adding content which is fundamental to the question itself. Thanks.
– ADyson
Nov 28 '18 at 11:13
Ok thankyou. But that wasn't clear from the way you wrote the question before. We can't read your mind. Anyway, please add that code and error information into the question itself using the "edit" button...code in comments is hard to read, and people may not notice it. Comments are just for asking for clarifications / making brief suggestions, not for adding content which is fundamental to the question itself. Thanks.
– ADyson
Nov 28 '18 at 11:13
If you not into suggesting, please ignore, I think there are people who would love to suggest.
– Bappi_SB
Nov 28 '18 at 11:14
If you not into suggesting, please ignore, I think there are people who would love to suggest.
– Bappi_SB
Nov 28 '18 at 11:14
1
1
in an insert trigger there is no "old"...because you're inserting a row, not updating one. There is no previous version of the row, because it never existed before. You can just use the department_id in the newly created row using
new instead. See stackoverflow.com/questions/25453535/…– ADyson
Nov 28 '18 at 11:28
in an insert trigger there is no "old"...because you're inserting a row, not updating one. There is no previous version of the row, because it never existed before. You can just use the department_id in the newly created row using
new instead. See stackoverflow.com/questions/25453535/…– ADyson
Nov 28 '18 at 11:28
|
show 5 more comments
2 Answers
2
active
oldest
votes
In an insert trigger there is no "old"...because you're inserting a row, not updating one. There is no previous version of the row, because it never existed before. You can just use the department_id in the newly created row using new instead. And you don't need the if because there's nothing to compare to:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row begin
update departments set total_employees=total_employees+1
where department_id=new.department_id;
end$$ delimiter ;
See also How to get inserted value for trigger
P.S. It might be safer to update this by using a count of the actual rows, rather than a simple increment. I'm worried it might go askew if lots of rows are changed at almost the same time. It may be worth testing whether this causes issues or not.
P.P.S. I assume you also plan to add UPDATE and DELETE triggers for the same purpose, if you haven't already.
One issue that I'm facing, would be great if you could give some ideas.
– Bappi_SB
Nov 28 '18 at 11:46
@Bappi_SB what issue are you referring to? If you have a new problem, please ask a new question about it. paste the question link in these comments if you'd like me to look at it. Thanks.
– ADyson
Nov 28 '18 at 11:51
I've added it over here. Would be great if you could give your view on this.
– Bappi_SB
Nov 28 '18 at 11:53
....over where?
– ADyson
Nov 28 '18 at 11:54
Over this question. Edited it.
– Bappi_SB
Nov 28 '18 at 11:54
|
show 4 more comments
You seem to want an update trigger, not an insert trigger. The insert trigger would be:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row
begin
update departments set
total_employees = total_employees + 1
where department_id = new.department_id;
end if;
end$$
delimiter ;
The update trigger would be more like:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row
begin
if not (old.employee_department <=> old.employee_department) then
update departments set
total_employees = total_employees + (case when department_id = old_department_id then -1 else 1 end)
where department_id in (old.department_id, new.department_id);
end if;
end$$
delimiter ;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53517964%2fhow-can-i-increase-the-value-of-an-attribute-of-one-table-on-the-basis-of-the-in%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In an insert trigger there is no "old"...because you're inserting a row, not updating one. There is no previous version of the row, because it never existed before. You can just use the department_id in the newly created row using new instead. And you don't need the if because there's nothing to compare to:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row begin
update departments set total_employees=total_employees+1
where department_id=new.department_id;
end$$ delimiter ;
See also How to get inserted value for trigger
P.S. It might be safer to update this by using a count of the actual rows, rather than a simple increment. I'm worried it might go askew if lots of rows are changed at almost the same time. It may be worth testing whether this causes issues or not.
P.P.S. I assume you also plan to add UPDATE and DELETE triggers for the same purpose, if you haven't already.
One issue that I'm facing, would be great if you could give some ideas.
– Bappi_SB
Nov 28 '18 at 11:46
@Bappi_SB what issue are you referring to? If you have a new problem, please ask a new question about it. paste the question link in these comments if you'd like me to look at it. Thanks.
– ADyson
Nov 28 '18 at 11:51
I've added it over here. Would be great if you could give your view on this.
– Bappi_SB
Nov 28 '18 at 11:53
....over where?
– ADyson
Nov 28 '18 at 11:54
Over this question. Edited it.
– Bappi_SB
Nov 28 '18 at 11:54
|
show 4 more comments
In an insert trigger there is no "old"...because you're inserting a row, not updating one. There is no previous version of the row, because it never existed before. You can just use the department_id in the newly created row using new instead. And you don't need the if because there's nothing to compare to:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row begin
update departments set total_employees=total_employees+1
where department_id=new.department_id;
end$$ delimiter ;
See also How to get inserted value for trigger
P.S. It might be safer to update this by using a count of the actual rows, rather than a simple increment. I'm worried it might go askew if lots of rows are changed at almost the same time. It may be worth testing whether this causes issues or not.
P.P.S. I assume you also plan to add UPDATE and DELETE triggers for the same purpose, if you haven't already.
One issue that I'm facing, would be great if you could give some ideas.
– Bappi_SB
Nov 28 '18 at 11:46
@Bappi_SB what issue are you referring to? If you have a new problem, please ask a new question about it. paste the question link in these comments if you'd like me to look at it. Thanks.
– ADyson
Nov 28 '18 at 11:51
I've added it over here. Would be great if you could give your view on this.
– Bappi_SB
Nov 28 '18 at 11:53
....over where?
– ADyson
Nov 28 '18 at 11:54
Over this question. Edited it.
– Bappi_SB
Nov 28 '18 at 11:54
|
show 4 more comments
In an insert trigger there is no "old"...because you're inserting a row, not updating one. There is no previous version of the row, because it never existed before. You can just use the department_id in the newly created row using new instead. And you don't need the if because there's nothing to compare to:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row begin
update departments set total_employees=total_employees+1
where department_id=new.department_id;
end$$ delimiter ;
See also How to get inserted value for trigger
P.S. It might be safer to update this by using a count of the actual rows, rather than a simple increment. I'm worried it might go askew if lots of rows are changed at almost the same time. It may be worth testing whether this causes issues or not.
P.P.S. I assume you also plan to add UPDATE and DELETE triggers for the same purpose, if you haven't already.
In an insert trigger there is no "old"...because you're inserting a row, not updating one. There is no previous version of the row, because it never existed before. You can just use the department_id in the newly created row using new instead. And you don't need the if because there's nothing to compare to:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row begin
update departments set total_employees=total_employees+1
where department_id=new.department_id;
end$$ delimiter ;
See also How to get inserted value for trigger
P.S. It might be safer to update this by using a count of the actual rows, rather than a simple increment. I'm worried it might go askew if lots of rows are changed at almost the same time. It may be worth testing whether this causes issues or not.
P.P.S. I assume you also plan to add UPDATE and DELETE triggers for the same purpose, if you haven't already.
answered Nov 28 '18 at 11:39
ADysonADyson
25.1k112746
25.1k112746
One issue that I'm facing, would be great if you could give some ideas.
– Bappi_SB
Nov 28 '18 at 11:46
@Bappi_SB what issue are you referring to? If you have a new problem, please ask a new question about it. paste the question link in these comments if you'd like me to look at it. Thanks.
– ADyson
Nov 28 '18 at 11:51
I've added it over here. Would be great if you could give your view on this.
– Bappi_SB
Nov 28 '18 at 11:53
....over where?
– ADyson
Nov 28 '18 at 11:54
Over this question. Edited it.
– Bappi_SB
Nov 28 '18 at 11:54
|
show 4 more comments
One issue that I'm facing, would be great if you could give some ideas.
– Bappi_SB
Nov 28 '18 at 11:46
@Bappi_SB what issue are you referring to? If you have a new problem, please ask a new question about it. paste the question link in these comments if you'd like me to look at it. Thanks.
– ADyson
Nov 28 '18 at 11:51
I've added it over here. Would be great if you could give your view on this.
– Bappi_SB
Nov 28 '18 at 11:53
....over where?
– ADyson
Nov 28 '18 at 11:54
Over this question. Edited it.
– Bappi_SB
Nov 28 '18 at 11:54
One issue that I'm facing, would be great if you could give some ideas.
– Bappi_SB
Nov 28 '18 at 11:46
One issue that I'm facing, would be great if you could give some ideas.
– Bappi_SB
Nov 28 '18 at 11:46
@Bappi_SB what issue are you referring to? If you have a new problem, please ask a new question about it. paste the question link in these comments if you'd like me to look at it. Thanks.
– ADyson
Nov 28 '18 at 11:51
@Bappi_SB what issue are you referring to? If you have a new problem, please ask a new question about it. paste the question link in these comments if you'd like me to look at it. Thanks.
– ADyson
Nov 28 '18 at 11:51
I've added it over here. Would be great if you could give your view on this.
– Bappi_SB
Nov 28 '18 at 11:53
I've added it over here. Would be great if you could give your view on this.
– Bappi_SB
Nov 28 '18 at 11:53
....over where?
– ADyson
Nov 28 '18 at 11:54
....over where?
– ADyson
Nov 28 '18 at 11:54
Over this question. Edited it.
– Bappi_SB
Nov 28 '18 at 11:54
Over this question. Edited it.
– Bappi_SB
Nov 28 '18 at 11:54
|
show 4 more comments
You seem to want an update trigger, not an insert trigger. The insert trigger would be:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row
begin
update departments set
total_employees = total_employees + 1
where department_id = new.department_id;
end if;
end$$
delimiter ;
The update trigger would be more like:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row
begin
if not (old.employee_department <=> old.employee_department) then
update departments set
total_employees = total_employees + (case when department_id = old_department_id then -1 else 1 end)
where department_id in (old.department_id, new.department_id);
end if;
end$$
delimiter ;
add a comment |
You seem to want an update trigger, not an insert trigger. The insert trigger would be:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row
begin
update departments set
total_employees = total_employees + 1
where department_id = new.department_id;
end if;
end$$
delimiter ;
The update trigger would be more like:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row
begin
if not (old.employee_department <=> old.employee_department) then
update departments set
total_employees = total_employees + (case when department_id = old_department_id then -1 else 1 end)
where department_id in (old.department_id, new.department_id);
end if;
end$$
delimiter ;
add a comment |
You seem to want an update trigger, not an insert trigger. The insert trigger would be:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row
begin
update departments set
total_employees = total_employees + 1
where department_id = new.department_id;
end if;
end$$
delimiter ;
The update trigger would be more like:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row
begin
if not (old.employee_department <=> old.employee_department) then
update departments set
total_employees = total_employees + (case when department_id = old_department_id then -1 else 1 end)
where department_id in (old.department_id, new.department_id);
end if;
end$$
delimiter ;
You seem to want an update trigger, not an insert trigger. The insert trigger would be:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row
begin
update departments set
total_employees = total_employees + 1
where department_id = new.department_id;
end if;
end$$
delimiter ;
The update trigger would be more like:
delimiter $$
create trigger employee_insertion_in_individual_department
after insert on employees for each row
begin
if not (old.employee_department <=> old.employee_department) then
update departments set
total_employees = total_employees + (case when department_id = old_department_id then -1 else 1 end)
where department_id in (old.department_id, new.department_id);
end if;
end$$
delimiter ;
answered Nov 28 '18 at 11:41
Gordon LinoffGordon Linoff
789k35314418
789k35314418
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53517964%2fhow-can-i-increase-the-value-of-an-attribute-of-one-table-on-the-basis-of-the-in%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you don't really need to store the total number of employees in a field. You can just calculate it using a COUNT inside a query at the time when you want to know it. It's kind of a redundant piece of information. If you do really want to store it for some reason, then yes, you can write a trigger. But...what have you tried to do so far? What problem are you having? This isn't a free write-my-code or do-my-research service. You can find examples of this kind of trigger easily. There's no need for us to provide another tutorial here.
– ADyson
Nov 28 '18 at 11:09
See, I ain't expecting you to solve my problem, I'm asking politely for some suggestions.
– Bappi_SB
Nov 28 '18 at 11:13
Ok thankyou. But that wasn't clear from the way you wrote the question before. We can't read your mind. Anyway, please add that code and error information into the question itself using the "edit" button...code in comments is hard to read, and people may not notice it. Comments are just for asking for clarifications / making brief suggestions, not for adding content which is fundamental to the question itself. Thanks.
– ADyson
Nov 28 '18 at 11:13
If you not into suggesting, please ignore, I think there are people who would love to suggest.
– Bappi_SB
Nov 28 '18 at 11:14
1
in an insert trigger there is no "old"...because you're inserting a row, not updating one. There is no previous version of the row, because it never existed before. You can just use the department_id in the newly created row using
newinstead. See stackoverflow.com/questions/25453535/…– ADyson
Nov 28 '18 at 11:28