IDEA:
Having a table of item, user, assign now if I assign one item to user which the record will be save on table of assign,
table_item:
ID------INT
NAME----TEXT
COUNT---INT
table_user:
ID-------INT
NAME-----TEXT
table_assing:
ID------INT
USER----INT (user id)
ITEM----INT (item_id)
COUNT---INT (this is for subtractions from the column of COUNT table of item)
Here I want to set trigger on inserting to table (table_assing) the value of column COUNT should subtract from column of COUNT table of table_item
This is possible on PHP that I can set to query on once action but it will take lots of code if it's possible on MySQL that will be much better and fast and effective with accuracy
simple trigger after insert on table table_assign
UPDATE table_item
SET table_item.count = (table_item.count - NEW.table_assign.count)
WHERE table_item.id = table_assign.item
Something like this should work.
DELIMITER $$
USE database_name$$
CREATE TRIGGER trigger_name AFTER INSERT ON table_asign FOR EACH ROW
BEGIN
UPDATE table_item SET count=count+NEW.count WHERE id=NEW.id;
END;$$
The 'NEW.id' refers to the new row in the table 'table_asign'
Related
There are 2 Table in the Database.
tbl_second_category
tbl_third_category
I'm already passing
col_first_category_id
from
table1 tbl_second_category
to
Table2 tbl_third_category col_second_category_id
using select box control
You can see id 13 in both the Tables
when I'm passing above id, at the same time how can I pass col_second_category_id from Table1
tbl_second_category to column col_for_delete_row Table2 tbl_third_category.
Thank you
then use trigger, you may need more then one for update and delete events... for example:
CREATE TRIGGER trigger_name AFTER INSERT ON tbl_second_category
FOR EACH ROW
UPDATE tbl_third_category
SET col_for_delete_row = NEW.col_second_category_id
WHERE col_second_category_id = NEW.col_first_category_id;
This is actually a form to update the team members who work for a specific client, When i deselect a member then it's status turns to 0.
I have a table with all unique records. table consists of four columns -
first column is `id` which is unique and auto_incremented.
second column is `client_id`.
third column is `member_id`. (these second and third columns together make the primary key.)
fourth column is `current` which shows the status (default is 1.).
Now i have a form which sends the values of client_id and member_id. But this forms also contains the values that are already in the table BUT NOT ALL.
I need a query which
(i) `INSERT` the values that are not already in the table,
(ii) `UPDATE` the `current` column to value `0` which are in the table but not in the form values.
here is a screenshot of my form.
If (select count(*) from yourtable where client_id = and member_id = ) > 0 THEN
update yourtable set current = 0;
ELSE
insert into yourtable (client_id,member_id,current) values (value1,value2,value3)
First of all check if the value exists in the table or not, by using a SELECT query.
Then check if the result haven't save value so it will be inserted, else show an error .
This would be a great time to create a database stored procedure that flows something like...
select user
if exists update row
else insert new row
stored procedures don't improve transaction times, but they are a great addition to any piece of software.
If this doesn't solve your problem then a database trigger might help out.
Doing a little research on this matter might open up some great ideas!
Add below logic in your SP
If (select count(*) from yourtable where client_id = <value> and member_id = <value>) > 0 THEN
update yourtable set current = 0;
ELSE
insert into yourtable (client_id,member_id,current) values (value1,value2,value3)
if you want simple solution then follow this:
*) use select with each entry in selected team.
if select returns a row
then use update sql
else
use insert sql.
In your case member_id & client_id together makes the primary key.
So , you can use sql ON DUPLICATE KEY UPDATE Syntax.
Example:
$sql="INSERT INTO table_name SET
client_id='".$clientId."',
member_id='".$member_id."',
current='".$current."'
ON DUPLICATE KEY
UPDATE
current = '".$current."'
";
In this case when member_id & client_id combination repeats , it will automatically executes update query for that particular row.
I am using Laravel 3. I have a form, and 2 tables. One table has an AUTO INCREMENT ID and I want to give this ID to the second table. eg.
table1
ID: Auto increment
title:
text:
date:
table2
ID: Auto increment
t1_ID: table1->ID
text:
Is it possible somehow?
I think I can get the ID of the last record in table1 and add 1 to it in the controller, but maybe there is an easier way.
SOLUTION #1
This sounds like you need to use a trigger. To be safe you should use an AFTER INSERT trigger because you cannot predict if the autoincrement will increment more than once in the event of some unforeseen error during an INSERT.
Perhaps the trigger should look something like this:
DELIMITER $$
CREATE TRIGGER table1_ai AFTER INSERT ON table1 FOR EACH ROW
BEGIN
INSERT INTO table2 (t1_ID) VALUES (NEW.ID);
END; $$
DELIMITER ;
That way, the ID is safely placed in table2, knowing that the ID is in actual use in table1.
SOLUTION #2
You could retrieve the ID using the information_schema. How?
Suppose the table is mydb.table1. You can quickly get the next auto_increment value like this:
SELECT auto_increment FROM information_schema.tables
WHERE table_schema='mydb' and table_name='table1';
This could be risky to retrieve if mydb.table1 experiences high-volume writes
EPILOGUE
You should go with SOLUTION #1, since implementing the trigger would handle the ID's placement in two tables without you having to code it.
i have a contactnumber column in mysql database. In contactnumber column there are more than 20,000 entries. Now when i upload new numbers through .csv file, i dont want duplicate numbers in database.
How can i avoid duplicate numbers while inserting in database.
I initially implemented logic that checks each number in .csv file with each of the number in database.
this works but takes lot of time to upload .csv file containing 1000 numbers.
Pleae suggest how to minimize time required to upload .csv file while not uploading duplicate values.
Simply add a UNIQUE constraint to the contactnumber column:
ALTER TABLE `mytable` ADD UNIQUE (`contactnumber`);
From there you can use the IGNORE option to ignore the error you'd usually be shown when inserting a duplicate:
INSERT IGNORE INTO `mytable` VALUES ('0123456789');
Alternatively, you could use the ON DUPLICATE KEY UPDATE to do something with the dupe, as detailed in this question: MySQL - ignore insert error: duplicate entry
If your contactnumber should not be repeated then make it PRIMARY or at least a UNIQUE key. That way when a value is being inserted as a duplicate, insert will fail automatically and you won't have to check beforehand.
The way I would do it is to create a temporary table.
create table my_dateasyyyymmddhhiiss as select * from mytable where 1=0;
Do your inserts into that table.
and then query out the orphans on the between mytable and the temp table based on contactnumber
then run an inner join query between the two tables and fetch out the duplicate for your telecaller tracking.
finally drop the temporary table.
Thing that this does not address are duplicates within the supplied file (don't know if that would be an issue in this problem)
Hope this help
If you don't want to insert duplicate values in table and rather wants to keep that value in different table.
You can create trigger on table.
like this:
DELIMITER $$
CREATE TRIGGER unique_key BEFORE INSERT ON table1
FOR EACH ROW BEGIN
DECLARE c INT;
SELECT COUNT(*) INTO c FROM table1 WHERE itemid = NEW.itemid;
IF (c > 0) THEN
insert into table2 (column_name) values (NEW.itemid);
END IF;
END$$
DELIMITER ;
I would recommend this way
Alter the contactnumber column as UNIQUE KEY
Using phpmyadmin import the .csv file and check the option 'Do not abort on INSERT error' under Format-Specific Options before submitting
I have a number which is in this form : 2012-01 (2012 as current year) and 01 is just a the maximum value of a field in my database incremented by 1, and each year that number is reset to 0.
but if there are two users that try to do the same operation at the same time the value is the same for both and thus i get the same number inserted twice in my database .
I thought of creating a sequence but that requires a job that resets the sequence each year and i would prefer if there is a way to make a lock before i get the next number and
release it after an insert is done ?
Thanks.
CREATE UNIQUE INDEX index_name ON table_name (column_name);
or
ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column_name);
You don't specify where you store the field that is used as the counter. But maybe it is possible to use a SELECT FOR UPDATE statement.
Before you increment the value of your counter field by 1 you can lock that record by using a SELECT FOR UPDATE. Then update the counter.
Something like this, assuming the table has only 1 record:
SELECT *
FROM CounterTable
FOR UPDATE;
UPDATE CounterTable
SET Counter = Counter + 1;
COMMIT;
If one session (user) has done the SELECT FOR UPDATE and not yet committed or rolled back, the other session (user) doing a SELECT FOR UPDATE will block waiting to be able to get a lock. This prevents two users from getting the same number.