MySQL Triggers

Pagina: 1
Acties:

  • Av3ng3rtje
  • Registratie: December 2002
  • Laatst online: 05-01 22:08
Hallo,

EDIT: MySQL 5.0.24 community-nt

Ik ben op het moment een forum aan het maken voor mijn website. Voor dit forum gebruik vier tabellen:

forum_cats - hier staan alle categorieren voor de forums
forum_forums - hierin staan de forums die onder de forum_cats vallen
forum_topics - hierin komen de topics
forum_posts - hierin komen alle posts

Het probleem zit hem in de triggers, ik wil een aantal triggers aan maken voor 2 tabellen zodat wanneer men een topic insert of een post een andere tabel wordt geupdate. Het probleem doet zich voort wanneer ik een trigger wil maken met 2 queries er in, waarin beide de temp tabel wordt gebruikt.

Ik geprobeerd een delimiter toe te voegen ( | en // ) maar dan kwam hij ook weer met sql syntax errors. Als ik hem probeerde aan te maken zonder delimiters, krijg ik een error dat bv. NEW.userID niet meer bestaat omdat waarschijnlijk de "create trigger" transition al voldaan is en hij de tweede query in de trigger body gewoon als een query ziet.


Structuur tabellen:

SQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
CREATE TABLE `forum_cats` (
  `catID` bigint(20) NOT NULL auto_increment,
  `catName` varchar(64) NOT NULL,
  `catDescription` varchar(255) NOT NULL,
  PRIMARY KEY  (`catID`)
)

CREATE TABLE `forum_forums` (
  `forumID` bigint(20) NOT NULL auto_increment,
  `forumName` varchar(64) NOT NULL,
  `forumDescription` varchar(255) NOT NULL,
  `forumPosts` bigint(20) NOT NULL,
  `forumTopics` bigint(20) NOT NULL,
  `catID` bigint(20) NOT NULL,
  `userGroupAccess` text NOT NULL,
  PRIMARY KEY  (`forumID`)
)

CREATE TABLE `forum_posts` (
  `postID` bigint(20) NOT NULL auto_increment,
  `userID` bigint(20) NOT NULL,
  `Subject` varchar(128) NOT NULL,
  `Message` text NOT NULL,
  `Date` bigint(20) NOT NULL,
  `topicID` bigint(20) NOT NULL,
  PRIMARY KEY  (`postID`)
)

CREATE TABLE `forum_topics` (
  `topicID` bigint(20) NOT NULL auto_increment,
  `topicName` varchar(128) NOT NULL,
  `userID` bigint(20) NOT NULL,
  `topicPosts` bigint(20) NOT NULL,
  `topicViews` bigint(20) NOT NULL,
  `lastPost` bigint(20) NOT NULL,
  `sticky` int(11) NOT NULL,
  `locked` int(11) NOT NULL,
  `forumID` bigint(20) NOT NULL,
  PRIMARY KEY  (`topicID`)
)

CREATE TABLE `users` (
  `userID` bigint(20) NOT NULL auto_increment,
  `username` varchar(32) NOT NULL default '',
  `password` varchar(32) NOT NULL default '',
  `Email` varchar(128) NOT NULL,
  `userPosts` bigint(20) NOT NULL,
  `userAvatarID` bigint(20) NOT NULL,
  `userSignature` text NOT NULL,
  `ip` varchar(15) NOT NULL default '',
  `hash` varchar(32) NOT NULL default '',
  `usergroupID` bigint(20) NOT NULL default '0',
  `lastLogin` bigint(20) NOT NULL,
  `RealName` varchar(96) NOT NULL,
  `Age` int(11) NOT NULL,
  `Country` varchar(96) NOT NULL,
  `CityVillage` varchar(96) NOT NULL,
  `Interests` varchar(255) NOT NULL,
  PRIMARY KEY  (`userID`)
)


Triggers

SQL:
1
2
3
4
5
6
7
8
9
10
11
CREATE TRIGGER onPostInsert
AFTER INSERT ON forum_posts
FOR EACH ROW
UPDATE forum_topics SET topicPosts = topicPosts + 1 WHERE topicID = NEW.topicID;
UPDATE users SET userPosts = userPosts + 1 WHERE userID = NEW.userID;

CREATE TRIGGER onPostDelete
AFTER DELETE ON forum_posts
FOR EACH ROW
UPDATE forum_topics SET topicPosts = topicPosts - 1 WHERE topicID = OLD.topicID;
UPDATE users SET userPosts = userPosts - 1 WHERE userID = OLD.userID;

[ Voor 0% gewijzigd door moto-moi op 17-09-2006 14:43 ]


  • H@rry
  • Registratie: Maart 2001
  • Laatst online: 10-02 22:15
http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html
CREATE TRIGGER testref BEFORE INSERT ON test1
FOR EACH ROW BEGIN
INSERT INTO test2 SET a2 = NEW.a1;
DELETE FROM test3 WHERE a3 = NEW.a1;
UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
END;
probeer em zo es

en met delimiter moet je volgens mij het woord DELIMITER erbij doen

GoT a clue? Specs


  • Av3ng3rtje
  • Registratie: December 2002
  • Laatst online: 05-01 22:08
Zoals ik al aan gaf in me topicstart heb ik een delimiter gebruikt..

SQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
DELIMITER |
CREATE TRIGGER onPostInsert
BEFORE INSERT ON forum_posts
FOR EACH ROW
BEGIN
UPDATE forum_topics SET topicPosts = topicPosts + 1 WHERE topicID = NEW.topicID;
UPDATE users SET userPosts = userPosts + 1 WHERE userID = NEW.userID;
END ;
|

CREATE TRIGGER onPostDelete
BEFORE DELETE ON forum_posts
FOR EACH ROW
BEGIN
UPDATE forum_topics SET topicPosts = topicPosts - 1 WHERE topicID = OLD.topicID;
UPDATE users SET userPosts = userPosts - 1 WHERE userID = OLD.userID;
END ;
|


Error:

code:
1
2
3
4
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER |
CREATE TRIGGER onPostInsert
BEFORE INSERT ON forum_posts
FOR EACH' at line 1

[ Voor 0% gewijzigd door moto-moi op 17-09-2006 14:43 ]


  • H@rry
  • Registratie: Maart 2001
  • Laatst online: 10-02 22:15
en tjek nou es die van DELIMITER op de MYSQL site

ik zie geen DELIMITER; op het eind bij jou

GoT a clue? Specs


  • Av3ng3rtje
  • Registratie: December 2002
  • Laatst online: 05-01 22:08
SQL:
1
DELIMITER ;


Op het eind geeft nu:

code:
1
2
3
4
5
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER |

CREATE TRIGGER onPostDelete
BEFORE DELETE ON forum_posts
  FOR ' at line 1

[ Voor 90% gewijzigd door Av3ng3rtje op 17-09-2006 14:52 ]


  • Av3ng3rtje
  • Registratie: December 2002
  • Laatst online: 05-01 22:08
bump

  • pjonk
  • Registratie: November 2000
  • Laatst online: 29-12-2025
Op http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html staat een voorbeeld als je die 1 op 1 overneemt moet het toch werken. Welke MySQL versie heb je? Misschien ondersteunt die geen DELIMITER? Ik vond ook nog onderstaande post via Google Group misschien heb je daar wat aan.
DELIMITER is actually a command, not a statement. It is only of the few thing you can enter without a delimiter. I personally find it easier to code triggers and stored procedures in a text file, then use the SOURCE command or pipe syntax to execute them. I don't see anything obviously wrong with your choice of delimiter. Do remember to put a space after the keyword DELIMITER, if you omitted it then mysql will be prompting you for 'more' of your 'statement', or will treat the subsequent CREATE statement as part of the DELIMITER command. Normally a delimiter does not require whitespace before, the DELIMITER command itself is the exception.

It’s nice to be important but it’s more important to be nice


  • Av3ng3rtje
  • Registratie: December 2002
  • Laatst online: 05-01 22:08
pjonk schreef op woensdag 20 september 2006 @ 13:53:
Op http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html staat een voorbeeld als je die 1 op 1 overneemt moet het toch werken. Welke MySQL versie heb je? Misschien ondersteunt die geen DELIMITER? Ik vond ook nog onderstaande post via Google Group misschien heb je daar wat aan.

[...]
Versie: zie TS.

Ik heb exact dat voorbeeld overgenomen en ik krijg telkens de error dat hij vast loopt, als ik probeer een function aan te maken dan zegt hij ook telkens:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER //' at line 1

Lijkt net alsof DELIMITER niet aanstaan ofzo :|
Pagina: 1