SQL Database Help - MySQL Workbench - mysql

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

Related

Is it possible in MySql to associate inserted data to a specific user?

I just was wondering if is it possible to associate an user to the data inserted in a table.
For example:
I have a table Customers with columns customerID, name and address. Is there any method in MySql to know what user has inserted each row in the table or must I add a column userID in the table Customer to add the user in my SQL Insert statement?.
If I have only one table, it is not a proble to add the userID column. But when i have multiple tables, maybe it becomes a messy task.
Thanx in advance.
If you are trying to find out the user_id caused the insert then NO, there is no other way than you storing it explicitly likewise you already have thought of.
If there is multiple tables for which you want to store the same information; then you can probably have a separate table where you can have the user_id column and can use a AFTER INSERT TRIGGER to insert the user id in this table.
No, no such functionality is provided by MySQL. You'll have to add a column for user_id in your table(s) and insert the user id yourself.

MySQL click log by country, best approach

I'm planning to log referral clicks in mysql by the country of the IP.
Let's say my table is named referral_clicks and has the columns id, referral_id, country.
I have 2 approaches in mind now:
Create another column clicks which is set to +1 for every country / referral_id. This means that I would have to check first if the row for the specific referral_id and country already exists and if not, create it.
Insert a new row for every request. My concern here is, that the table might geht messy and too big, as might get very much referral requests.
What would be the best approach now for something like that, or is there evern a better approach?
Use INSERT ... ON DUPLICATE KEY UPDATE.
I suggest you create a table with the following columns.
id (autoincrement)
referral_id
country
clickdate
clicks
I suggest you create a unique index of (referral_id,country,clickdate).
Then, I suggest you use the following SQL each time you want to log a click:
INSERT INTO referral_clicks (referral_id, country, clickdate, clicks)
VALUES ('whatever', 'whatcountry', CURDATE(), 1)
ON DUPLICATE KEY UPDATE clicks=click+1
This will start a new row for each referral id, for each country, for each date. If the row already exists it will increment clicks for you.

Adding a database record with foreign key

Let's say there is a database with two tables: one customer table and one country table. Each customer row contains (among other things) a countryId foreign key. Let's also assume that we are populating the database from a data file (i.e., it is not an operator that is selecting a country from a UI).
What is the best practice for this?
Should one query the database first and get all ID's for all countries, and then just supply the (now known) country id's in the insert query? This is not a problem for my 'country' example, but what if there is a large number of records in the table that is being referred?
Or should the insert query use a sub query to get the country id based on the country name? If so, what if the record for the country does not exist yet and has to be added?
Or another approach? Or does it depend? :)
I would suggest using a join in your insert query to get the country id based on the country name. However, I don't know if that's something possible with every SGBD and you don't give more precision on the one you're using.

MySQL automate update of table data

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

SQL: flavours of INSERT INTO

(MySQL) I am trying to migrate a "subscription" table into 3 new tables: "product", "subscription", "actual" where "actual" is the name of the actual product, say, newsletter. The subscription has a FK reference to product and the actual has FK reference into subscription.
I know the INSERT INTO SELECT statement can copy data probably from many table to one; is there a statement to do the opposite, one to many tables for my case?
I'm not aware of an SQL statement that will do what you want. Just do several INSERT INTO SELECTs one after the other. It may be faster to do them one at a time anyway.
I think you can use three seperate insert into select statements. First you convert the product table, then the subscription where you can use an embedded select to find the id in the product table:
insert into subscription (some_column, FK_id,...)
select something, (select id from product where <your where clause>),...
and finally convert the actual table using an embedded select to get the id from the subscription table.