I have two tables, one temporary called Persons, the 2nd permanent called employee.
The temporary table is updated by someone every few hours and contains two fields, firstname and lastname.
The permanent table is called employee. It is our permanent record of employees and includes all of their contact information, etc. Importanly, it includes two fields firstname and lastname.
I have this query that shows me if a record in persons matches a record in employees.
SELECT T.FirstName, CASE WHEN P.FirstName IS NULL THEN 'DOES NOT EXIST' ELSE 'DOES EXIST' END
FROM employee T
LEFT JOIN Persons P ON T.FirstName = P.FirstName AND T.LastName = P.LastName
I want something to run within MySQL, on a constant basis and do 2 things:
If a name matches in firstname and lastname in the Persons table and the employee table I want to receive an email that says "Duplicate employee found". AND, I want it to add just those two fields to the employee table then remove the record from the Persons table.
If there is no match, I just want it to add those two fields to a new row in employee table and remove the row from Persons.
I know it sounds backwards but I have thought it through. I can do a query, but I need MySQL to do this automatically on a periodic basis somehow.
Would love your help.
MySQL 5.1 introduced a feature called Events, which allows you to execute a block of procedure code on a schedule (like cron).
See http://dev.mysql.com/doc/refman/5.5/en/events.html
Related
So I have the following hierarchical database structure:
Table person has columns id and some other fields.
Table car has columns id, owner (with a foreign key constraint to person.id) and some other field
Table bumpersticker has columns id, car (with a foreign key constraint to car.id) and some other fields
I want to INSERT a row in to bumpersticker and have values to populate the row. I also have a person.id value of the person trying to add the bumpersticker.
What is the best practice to ensure that the car.owner value selected from the bumpersticker.car is in fact the same person.id as I have?
I guess one obvious way is to first execute a select query, on the car table and select the car.owner and validate that this value is the same value as the id of the person trying to add the bumpersticker and then execute an insert query.
but this seems like something there must be an elegant solution to in MySQL. at least not having to do two separate queries.
Most thankful for your help!
You can insert from a SELECT query that tests if the owner matches the criteria
INSERT INTO bumpersticker (car, sticker_text)
SELECT c.id, "If you can read this you're too close"
FROM car AS c
WHERE c.id = #car_id AND c.owner = #person_id
#car_id is the ID of the car you're adding the bumpersticker for, and #person_id is the ID of the user doing the insert. If the owner ID doesn't match, the SELECT query will return no rows, so nothing gets inserted.
DEMO
I'm new to MS access. I have a few tables. Table 'Learners' contains columns such as 'LearnerID' (unique), firstname, surname, status, etc. There's another table (LearnerUpdate) that I created by importing a spreadsheet. The LearnerUpdate also contains a learnerID and their names. I need to match the learner ID and update teh LearnerUpdate table to include the 'status' taken from the 'Learners' table. How do I do it?
I'm not even sure what to google for. The Access terminology is alien to me.
Thank you
Assuming the column you want to update in the LearnerUpdate table is named 'Status', then try to execute this SQL in the Queries table:
UPDATE LearnerUpdate
INNER JOIN Learners
ON LearnerUpdate.LearnerID = Learners.LearnerID
SET LearnerUpdate.Status = Learners.Status
Sorry for the ambiguous title.
I have two tables:
table 1: mailing_email
table 2 (dynamic table but for now is): membership
table 1 contains a list of all email accounts in the database and few ancillary fields such as name. It also has a column called communicate.
communicate is basically my terminology for subscribed. Any unsubscribe link will set communicate to false.
Both mailing_email and membership have a email and communicate column.
I need to write a query where the following happens:
mailing_email.communicate gets updated to the current status of membership.communicate where mailing_email.email = membership.email. If an email exists in mailing_email which does not exist in membership, the communicate field stays the same.
How would i go about doing this the fastest possible way? Each table will have thousands of rows this sync command would run often.
MySQL offers an update join syntax:
UPDATE mailing_email
JOIN membership ON mailing_email.email = membership.email
SET mailing_email.communicate = membership.communicate
I have a table for Staff, which holds all their key data; StaffID, Name, address etc. There is also a table for Absences. The Absences table has Absence ID, Staff ID, Staff Name and Date.
Is there a way to autofill the Staff Name field in Absences when the Staff ID is entered.
So a way for the database to look up the Staff name associated with that specific ID?
Or whenever an absence is recorded, do I have to manually insert ID and Name (The problem with this is someone could accidentally put a name that doesn't match the ID)
Any ideas/help is appreciated.
Thanks in advance.
There are multiple ways to accomplish this.
Insert Trigger when new record is added you want to go to additional table and get name that can be inserted into Absences table.
Modify your application to look-up data from STAFF table and during insert add that data.
Run update statement that will get name from STAFF and place it in Absences.
This all depends on how you want to implement it. Your best alternative would be INSERT trigger. Read http://dev.mysql.com/doc/refman/5.0/en/triggers.html for how to use triggers in MySQL
I want to store user followers and following member list. Now in order to this, i am thinking to create two columns namely FOLLOWING and FOLLOWER in USER table to store comma separated values of following and followers respectively.
USER TABLE FIELDS:
userid
firstname
lastname
date_of_birth
following //in this we store multiple following_id as comma separated
follower //in this we store multiple follower_id as comma separated
Another way is to create tables namely FOLLOWER and FOLLOWING to store user's followers and following members id in it.
USER TABLE FIELDS:
userid
firstname
lastname
date_of_birth
and
FOLLOWER TABLE FIELDS:
userid
follower_id (also is an user)
and
FOLLOWING TABLE FIELDS:
userid
following_id (also is an user)
Since i am learning database designing, i don't have enough knowledge. So, here i am not getting proper idea of which way is proper? I have searched that using comma separated way is not a good idea but at the same time is it a good way to have multiple tables with NF ? Is there any drawback of using JOINS? Or is there any other effective way to deal with this scenario?
You need just two tables - one to list your users, and one to list who each user is following.
The resulting tables would be like your second proposal, except that the followers table is unnecessary because all of the required data is already in the following table - it's just keyed from the second column instead of the first. That table will need an index on both columns.
The following table whould have one row per relationship per direction. If the users are following each other, you would put two entries in the following table.
CREATE TABLE following (
userid ... NOT NULL,
following_id ... NOT NULL
);
CREATE INDEX idx_user ON following(userid);
CREATE INDEX idx_following on following(following_id);
CREATE UNIQUE INDEX idx_both ON following (userid, following_id); // prevents duplicates
To find the IDs that a particular user is following:
SELECT following_id FROM following WHERE userid = ?
or to find that user's followers:
SELECT userid FROM following WHERE following_id = ?
Use appropriate JOIN clauses if required to expand those queries to return the users' names.
None of the above. One row per follower. Normalize your data and using it will be easy. Make it an abstract mess like you're proposing and you're life will get tougher and tougher as your application grows.