Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't Create Trigger using values from other tables

Status
Not open for further replies.

TheFitz

Programmer
Dec 18, 2003
140
0
0
GB
Hi all,

I'm trying my hand at some MySQL. Although I am a seasoned programmer, I have never touched MySQL and I am struggling to create a new trigger using phpMyAdmin.

In short, there are 3 tables, "tblusers" "tblprofile" and "tblfamily". Assuming a user is logged in and they want to create a new profile. Each profile will contain family members. The first family member should be the current user logged in. I am trying to create an after insert trigger on the profile table that takes the "FRIENDLY NAME" from the user table and inserts a new row into the family table, yet the code is throwing the 1064 error.

My code is below:

Code:
BEGIN

DECLARE MYNAME VARCHAR(30);

SET MYNAME = SELECT USR_FRIENDLY_NAME 
             FROM tblusers 
             WHERE USR_ID = ( SELECT PRO_USR_ID 
                              FROM tblprofiles 
                              WHERE PRO_ID = NEW.PRO_ID);

INSERT INTO tblfamily (FAM_PRO_ID, FAM_NAME, FAM_DOB,      FAM_DATE_UPDATED, FAM_USER_UPDATED)
               VALUES (NEW.PRO_ID, MYNAME,   '2017-01-01', NOW(),            1);

END

I'm sure this will be easily fixed, but Google doesn't seem to be my friend today.

Thanks for any help you can give


Fitz
Did you know, there are 10 types of people in this world:
* Those who understand binary
and
* Those who Don't!!
 
and I am struggling to create a new trigger using phpMyAdmin."

Are your end users going to be using phpMyAdmin, a MySQL command prompt or some other tool that allows them to generate queries and they have appropriate permissions to the database?

If not, I don't quite understand why you are. Surely you should be writing/building a GUI that handles user input and then inserts the data into the tables as appropriate.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
ChrisHirst said:
Are your end users going to be using phpMyAdmin, a MySQL command prompt or some other tool that allows them to generate queries and they have appropriate permissions to the database?

A trigger is a conditional set of instructions stored in the Database, it does not rely on the method of input to the DB. When a condition is met in the database, i.e a table is changed, an insert statement is going to be run, a field is updated etc... the trigger fires and does things to the data. Creating it on phpmyadmin or directly through mysql is irrelevant to the end user. The end user does not need to use the same tool that was used to create the trigger for it to fire.

The Trigger will fire when the condition is met in the database.

Creating triggers is part of creating a gui that handles user input, its just kept on the database side.






----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
vacunita said:
A trigger is a conditional set of instructions stored in the Database, it does not rely on the method of input to the DB


Yes I already know this, I am attempting to find out exactly what the OP is trying to do. Reading the post it would appear that the OP is actually trying to create, the fact that this same question is in the PHP forum, a fact you already are aware of, suggests that the REAL solution lies elsewhere. Therefore, I would like to know more.



Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
But then I fail to understand the relevance of what they are using to create the trigger. A trigger is independent of the tool used to create it.

I'm not versed in trigger creation, so can't answer directly, but perhaps a better question would be: What is the exact text of the 1064 error?

At least that way we know exactly what MYSQL is complaining about.

Questioning the reasons behind the creation of the trigger or how its being created itself does not really help the OP.

Best to address the actual error they are receiving first.

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Hey all,

@Chris, yes, I am planning to have a user input, probably in PHP - also need to learn this.

For the moment, I am just setting up the DB.

In short, assume a 1-many relationship between users and profiles and profiles and family.

A user creates a new record in profiles (via a frontend insert), I want the trigger to pickup data from the related user record and insert it as the first record in the family table for that profile.

Am I making any sense?

I've managed to get the trigger on a new user to create a new profile record so I believe I understand the insert process correctly. I think my issue is return data into the MYNAME variable? but I could be wrong. . .

Thanks for your help,


Fitz
Did you know, there are 10 types of people in this world:
* Those who understand binary
and
* Those who Don't!!
 
Regarding the OP, according to (I'm not too familiar with MySQL triggers, so I looked it up), a trigger is not the same thing as a stored procedure, so there's no need for BEGIN... END. So... the syntax would be something like:
SQL:
CREATE TRIGGER tblprofile_After_Insert
AFTER INSERT ON tblprofile
FOR EACH ROW
INSERT INTO tblfamily (FAM_PRO_ID, FAM_NAME, FAM_DOB,      FAM_DATE_UPDATED, FAM_USER_UPDATED)
SELECT
[indent]NEW.PRO_ID,
(SELECT USR_FRIENDLY_NAME 
[indent]FROM tblusers [/indent]
[indent]WHERE USR_ID = ( SELECT PRO_USR_ID[/indent] 
[indent]FROM tblprofile[/indent] 
[indent]WHERE PRO_ID = NEW.PRO_ID)) AS MYNAME,[/indent]
'2017-01-01' AS DOB,
NOW() AS Date_Updated,
1 AS USER_Updated
[/indent];

(not tested.)

...also, in your code, you had "FROM tblprofiles" (plural), when you listed the table name as tblprofile in your question. :) I also recommend just testing the "SELECT" portion of the insert statement in your MySQL client, if it throws errors.

Sorry about the multiple edits; I found a few issues with my code and had to fix.

Katie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top