MySQL Forums
Forum List  »  German

Re: Automatisch generierter Wert
Posted by: Thomas Wiedmann
Date: October 20, 2010 12:01AM

Als Idee folgendes Tabellenfragment

CREATE TABLE daten (
 col1,
 col2,
 col3,
 auto_gen_id 
);


Jetzt einen ON INSERT Datenbank Trigger auf diese Tabelle erstellen.

DELIMITER $$
CREATE TRIGGER auto_gen_id_tr
 BEFORE INSERT ON daten
  FOR EACH ROW BEGIN
    -- Berechnen der auto_gen_id aus Inhalten der anderen Spalten
    NEW.auto_gen_id = NEW.col1 + NEW.col2 + NEW.col3
  END$$
  
DELIMITER ;

http://dev.mysql.com/doc/refman/5.1/de/create-trigger.html


Erfolgt nun ein INSERT

INSERT INTO daten (col1, col2, col3) VALUES ( 1,2,3);

Wird vom dem Trigger die Spalte auto_gen_id berechnet und gefüllt.


So, ich hoffe die Lösung passt zum Problem.

Grüße
Thomas

Options: ReplyQuote


Subject
Views
Written By
Posted
2732
October 19, 2010 10:24AM
Re: Automatisch generierter Wert
1103
October 20, 2010 12:01AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.