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.
Related
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.
I have created few tables such as date table which records the date and time of the attendance recorded, student table which records details of students and status which records present, absent or late.
I want to create a interface where a teacher can record the attendance easily and where MYSQL PHP language can be used.
I have being able to echo out all the list of the students from a certain class taught by specific teacher. Now I want to let the teacher to use checkbox system to select or deselect and submit and directly update into the database.
Can you please help me how can I start this process?
If you want more information then please let me know.
I want to create a interface such as attendance recording interface where I have create a table which involves student_id as foreign key from student table. Then a status table that records present, late or absent status and a date & time record that records the date and time the record was added.
When a user selects the checkboxes of three different types and clicks Submit then the PHP will send this information to the MYSQL saying Tim is present but Ashley is late recognized with their student_id so it means it will update the student’s record however I was think of using insert query command rather than update because new records might be added everyday.
So the formula or the query could be SELECT the checkboxes and insert status of the student WHERE student_id = the chosen column.
I am struggling on the right query to work out and do I have to use JavaScript to make it work or use a PHP function?
You need to understand how POST/GET resquests work (for interfacing between HTML and PHP side), and then how to call a MySQL request from PHP. Here you have some usefull examples:
GET/POST requests:
http://www.w3schools.com/tags/ref_httpmethods.asp
Interfacing MySQL with PHP (I recommend read all sections, intro, connection to database etc)
http://www.w3schools.com/php/php_mysql_intro.asp
MySQL insertion to database (this is the 'record' part you are looking for):
http://www.w3schools.com/php/php_mysql_insert.asp
Basically what you're asking for - "How can I make a nice slick HTML/CSS interface that uses PHP/MySQL on the backend to perform these operations - CREATE, RETRIEVE, UPDATE, DELETE"
Things you can learn in sequence -
Acccessing MySQL DB from PHP and populate into HTML table, you can generate the checkboxes too.
Make the table look cool with some CSS
And communicate between the server and client side using form submissions, using GET/POST
This is just a general guidance to get you started. Do not land up using frameworks or libraries immediately if you wan't to be in this field in the long run.
Welcome to webdev. :)
quick question.
In my user database I have 5 separate tables all containing different information. 4 tables are connected by foreign key to the primary key of the first table.
I am wanting to trigger row inserts on the other 4 tables when I do an insert on the first (primary). I thought that with ON UPDATE CASCADE would do this for me but after trying it I realised it did not...I know clue is in the name ON UPDATE!!!!!
I also tried and failed at multiple triggers on the same table but found this was not possible either.
What I am planning on doing is putting a trigger on the first to INSERT on the second and then putting a trigger on the second to insert on the third......etc
Would just like to know if this is a wise thing to do or not or if I am missing a better and simpler way of doing this.
Any help/advice much appreciated.
Based on the given information, it "feels" as if there might be a flaw in the database design if each of the child tables requires a row for every single row in the parent table. There is a reason that "ON INSERT CASCADE" does not exist; it is typically not considered meaningful.
The first thought that comes to mind is that the child tables should actually be part of the parent table; it sounds as if there is a one-to-one relationship. It still may make sense to have separate tables from an organizational standpoint (and size of records), but it is something to think about.
If there is not a one-to-one relationship, then the ability to add meaningful data beyond default values to the child tables would imply there might be a bit more normalization of data required. If the only values to be added are NULLs, then one could maybe argue that there is no real point in having the record because a LEFT JOIN could produce the same results without that record.
Having said all that, if it is required, I would think that it would be better to have a single trigger on the parent table add all the records to the child tables rather than chain them in several triggers. That way the logic would be contained in a single location.
Not understanding your structure (the information you need in each of these tables is pertinent to correctly answer), I can only guess that a trigger might not be what you want to do this. If your tables have other fields beyond what is in table 1 and they do not have default values, how will you get the value for those other fields inthe trigger? Personally I would use a stored proc to insert to table1 and get the id value back from the insert and then insert to the other tables with the additonal information needed and put it all in a transaction so that if one insert fails all are rolled back.
I'm used to building websites with user accounts, so I can simply auto-increment the user id, then let them log in while I identify that user by user id internally. What I need to do in this case is a bit different. I need to anonymously collect a few rows of data from people, and tie those rows together so I can easily discern which data rows belong to which user.
The difficulty I'm having is in generating the id to tie the data rows together. My first thought was to poll the database for the highest user ID in existence, and write to the database with user ID +1. This will fail, however, if two submissions poll the database before either of them writes to it - they will each share the same user ID.
Another thought I had was to create a separate user ID table that would be set to auto-increment, and simply generate a new row, then poll that table for the id of the last row created. That also fails for the same reason as above - if two submissions create a row before either of them polls for the latest user ID, then they'll end up sharing an ID.
Any ideas? I get the impression I'm missing something obvious.
I think I'm understanding you right; I was having a similar issue. There's a super handy php function, though. After you query the database to insert a new row and auto-incrementing their user ID, do:
$user_id = mysql_insert_id();
That just returns the auto-increment value from the previous query on the current mysql connection. You can read more about it here if you need to.
You can then use this to populate the second table's data, being sure nobody will get a duplicate ID from the first one.
You need to insert the user, get the auto-generated id, and then use that id as a foreign key in the couple of rows you need to associate with the parent record. The hat rack must exist before you can hang hats on it.
This is a common issue, and to solve it, you would use a transaction. This gives you the atomic idea being being able to do more than one thing, but have it tied to either a success or fail as a package. It's an advanced db feature, and does require awareness of some more advanced programming in order to implement it in as fault-tolerant a manner as possible.
I've got an application in php & mysql where the users writes and reads from a particular table. One of the write modes is in a batch, doing only one query with the multiple values. The table has an ID which auto-increments.
The idea is that for each row in the table that is inserted, a copy is inserted in a separate table, as a history log, including the ID that was generated.
The problem is that multiple users can do this at once, and I need to be sure that the ID loaded is the correct.
Can I be sure that if I do for example:
INSERT INTO table1 VALUES ('','test1'),('','test2')
that the ids generated are sequential?
How can I get the Id's that were just loaded, and be sure that those are the ones that were just loaded?
I've thinked of the LOCK TABLE, but the users shouldn't note this.
Hope I made myself clear...
Building an application that requires generated IDs to be sequential usually means you're taking a wrong approach - what happens when you have to delete a value some day, are you going to re-sequence the entire table? Much better to just let the values fall as they may, using a primary key to prevent duplication.
based on the current implementation of myisam and innodb, yes. however, this is not guaranteed to be so in the future, so i would not rely on it.