Automatic Insert to Another Table - php

i'm working on a certain system that evaluates the performance of teachers.
Table 1:
Student (student_id,student_name,teachers,subjects)
Table 2:
Teacher (teacher id, teacher_name, subjects)
Is it possible that i can automatically insert some of data in the columns of Table 2 while inserting to the Table 1, by just one script? And can reference such that everytime a student login, he already knows who will he/she evaluates based on the subjects enrolled of the student and the teacher teaches. Did I make sense? Hoping for your response! Thanks Guys!

CREATE TRIGGER testref AFTER INSERT ON Student
FOR EACH ROW
BEGIN
INSERT INTO Teacher
(
teacher_name,
deleted_by)
VALUES
(
NEW.subject,
NEW.teacher_name );
END;

Problem 1)
can automatically insert some of data in the columns of Table 2 while inserting to the Table 1, by just one script?
You need to look at adding database triggers for that. But just from experience, I can tell you that the team I work with will never use database triggers. It tends to complicate your system and makes it difficult to maintain and find bugs. Try first to implement a method in your program that always inserts both rows in a single transaction. Then whenever you need to perform those insertions, just call that method.
Problem 2)
And can reference such that everytime a student login, he already knows who will he/she evaluates based on the subjects enrolled of the student and the teacher teaches.
Um, you may have to place that in a separate post and provide a lot more details.

Related

sync the 2 tables for certain rows

This is a question of is it possible and should i try to do it?
i have two rows in my table one named jobs and one named contacts (hope you still with me)
in jobs i have the following columns
typeofjob
id
name
phone
address
budget
due
in contacts i have the following columns
id
name
phone
address
is there a way to sync the 2 tables for certain rows?
id
name
phone
address
i ask as this would make life a lot easier for me but worried as id might conflict
and what happens if i delete a user from contacts i would only want it deleted from that row
i am very new to this and learning so if you can answer me with simplify answers that be great just treat it like your trying to explain this to your nan or something, but on the other hand she might pick this up faster than i am :)
What you want as an ID in one of the columns with an ID of a row in another table. So, you could add a column contact_id in jobs table, under the assumption that each contact could potentially spawn multiple jobs. You can then query it, such as
SELECT * FROM `jobs` WHERE `contact_id` = 'whatever'
You could also use JOINS to do more complex and efficient work (remember, the less trips to the database, the better!).

Auto erase table data in php myadmin

i'm developing a feedback form, where students will be allowed to give feedback on the particular subjects.
I have a table with 3 fields "ID, Unique No, Password", where students admission number are stored. Now here is what i want.
As soon as each students completes giving the feedback, his particular data's from the table must be deleted automatically.
please help.
This can be done with a JOIN, but I'll demonstrate a trigger here, because I mentioned it in my comment above.
I assume you've got another table where you store the students feedback data. Let's call it students_feedback and the other students_admission for this example.
Using MySQL Triggers, you assing the Database to delete the student admission data automatically ON INSERT. You'll want to use on create, because as soon as the feedback data is stored in the students_feedback table, the INSERT event is triggered.
So, for example:
CREATE TRIGGER delete_admission AFTER INSERT
ON students_feedback FOR EACH ROW
BEGIN
DELETE FROM students_admission WHERE students_admission.id=OLD.id LIMIT 1;
END;
Use whatever DELETE query you want here.
NOTE: Before MySQL 5.0.10, triggers cannot contain direct references to tables by name.
Like explained before use a trigger. Simply click on triggers and create a trigger that occurs after an INSERT in the table that records the feedback of the students. You could do something like this
I don't really agree though that using triggers is a good practice. Triggers are business logic and their logic should be implemented in your code. Separating business logic in your app and in your database makes it harder for the next developer to work on since he doesn't know where to look. The only reason that i think is viable to use them is when it is used for distributed databases to keep them updated in relation to each other.

Issue in copying rows from one table to another

I am implementing a request mechanism where the user have to approve a request. For that i have implemented a temporary table and main table. Initially when the request is added the data will be inserted to the temporary table, on approval it will be copied to the main table.
The issue is there will be more than 5k rows to be moved to the main table after approval + another 3-5 row for each row in the detail table (stores the details).
My current implementation is like this
//Get the rows from temporary table (batch_temp)
//Loop through the data
//Insert the data to the main table (batch_main) and return the id
//Get the details row from the temporary detail table (batch_temp_detail) using detail_tempid
//Loop through the data
//Insert the details to the detail table (batch_main_detail) with the main table id amount_id
//End Loop
//End Loop
But this implementation would take atleast 20k queries. Is there any better ways to implement the same.
I tried to create a sqlfiddle but was unable to create one. So i have pasted the query in pgsql.privatepaste.com
I'm sorry that I'm not familiar with PostgreSQL. My solution is in MySQL, I hope it can help since if they (MySQL & PostgreSQL) are same.
First, we should add 1 more field into your batch_main table to track the origin batch_temp record for each batch_main record.
ALTER TABLE `batch_main`
ADD COLUMN tempid bigint;
Then, on approval, we will insert 5k rows by 1 query:
INSERT INTO batch_main
(batchid, userid, amount, tempid)
SELECT batchid, userid, amount, amount_id FROM batch_temp;
So, with each new batch_main record we have its origin batch_temp record's id. Then, insert the detail records
INSERT INTO `batch_main_detail`
(detail_amount, detail_mainid)
SELECT
btd.detail_amount, bm.amount_id
FROM
batch_temp_detail `btd`
INNER JOIN batch_main `bm` ON btd.detail_tempid = bm.tempid
Done!
P/S:
I'm confuse a bit about the way you name your fields, and since I do not know about PostgreSQL and by looking into your syntax, can you use same sequence for primary key of both table batch_temp & batch_main? If you can, it's no need to add 1 more field.
Hope this help,
Simply need to update your Schema. Instead of having two tables: one main and one temporary, you should have all the data in main table, but have a flag which indicates whether a certain record is approved or no. Initially it will be set to false, and once approved it will simply be set to true and then the data can display on your website etc. That way you will not need to write the data two times, or even have to move it from one table to another
You haven't specified RDBMS you are using, but good old INSERT with SELECT in it must do the trick in one command:
insert main (field1,...,fieldN) select field1,...,fieldN from temporary

Check for value in CSV MySQL row

I am storing user ID values in a table field separated by a | (user_id1|user_id2|user_id3|user_id17).
A user ID will be added and removed from this field at certain points.
How can I check if the current users ID exists in the field or not using a query?
And it of course needs to be an exact match. Can't look for user_id1 and find user_id17.
I know I could use a SELECT query, explode the field, then use in_array but if there's a way to do it using a query it'd be better.
I guess I'll explain what I am doing: I made a forum for a small private website (7 users), but coding it for larger scale.
My table structure is pretty good: forum_categories, forum_topics, forum_posts. Using foreign keys between the tables for delete and update queries.
What I am seeking help on is to mark Topics as unread for each user. I could create a new table with topic_id & user_id, each one being a new row but that wouldn't be good with alot of users & topics.
If somebody has a better solution I am all for it. Or can prove to me that 1 row per user_id is the best way then I'll be more than willing to do that.
I think you want to track read messages, not the other way around. If you tracked unread messages, every time you add a user you'll have to add that user to every topics "unread list".
I looked into SMF like my comment suggested. They are using a separate table to track read messages.
A simple table that holds user_id and topic_id are you are need. When a user reads a topic, make sure there is a row in the table for that user.
Another reason to use a separate table. It's going to be faster to query against 2 int values in the database than to use LIKE % statements.

splitting data into multiple tables

I am building a employees page.
Some of the information goes into an 'employees' table but some of it goes into a 'availability' table that is referenced to the 'employee' table:
availability:
id / employeeid (unique id from employees table) / monday available / and on and on /
So I don't have that unique ID from the employees table until I create them.
Is it fine to do a query where I set the employee info and then a query to get the last created row in the employee table and then use the unique id from that to set the availability...
Or is that messy and should I have a create employee page and THEN a set availability page?
So basically I want to know if it is cleaner and 'better' coding to separate the two functions?
Adding to #Quassnoi's answer:
You would add the employee record, then use the MySQL LAST_INSERT_ID() function to find the autoincremented unique id for the employee record you added. You can then feed that value back into the availability INSERT statement.
More details are on the MySQL manual page at http://dev.mysql.com/doc/refman/5.1/en/example-auto-increment.html
It's important that you not use a SELECT statement (e.g. SELECT MAX(employee.id)) since there might be other uses adding records as well. LAST_INSERT_ID() is specific to your connection
Of course create employee first, availability then.
If your tables are InnoDB, you can do it in a transaction so that you can rollback the whole update if something goes wrong.
Is it fine to do a query where I set
the employee info and then a query to
get the last created row in the
employee table and then use the unique
id from that to set the
availability...
Yes, that sounds OK. If you use an autoincrement column for employeeid, you can then use mysql_insert_id() or equivalent to retrieve that last inserted id safely. Don't do SELECT MAX(employeeid) FROM ...., because you might get problems when loads of people are using it concurrently.
You can easily get the last insered record via
mysql_insert_id()
After that, you can insert an availability record for the desired employee.
Note: I would choose a framework that takes care of these issues, like Symfony or Cake.
Using the "last created row" may not always work the way that you're expecting and may complicate things in the future if there's growth or if another programmer assumes the project. If I understand what you're looking for, you should instead have 3 tables. One table for employees, one table for availability, and a third table should be used to store unique records for the association. In the association table each row will have columns for : a unique ID, the employee id, the availability id.

Categories