Tuesday, October 14, 2008

Bath After A Brazilian

Creating a Trigger in MySQL



In this short article will show how to create a trigger (or trigger) in MYSQL to tell us which user deleted a record in a table manufacturers in our computer database (see database item of this notebook).

Although the 1 am will realize this manual in the afternoon because I was working with a partner on this and I realized that mysql support triggers easily perform insert default records as

-

CURRENT_USER ( )
=> Insert the user who performed the surgery for free, - CURRENT_DATE () => Insert the date performed the surgery for free, - CURRENT_TIME () => Insert the time it was performed the surgery for free, => Insert the date and time it was performed the surgery for free,
But inserting records such as the code or name for the record to delete (or update) with a trigger requires more skill I hope to be clarified now.


NOTE:
MYSQL does not support more than 1 trigger in the same table;

Hands to work wing Creating tables (note: the database can call computer or the name they wish)

SET SQL code sql_mode = "NO_AUTO_VALUE_ON_ZERO"
CREATE TABLE `articles` (
CODIGOART `` int (11) NOT NULL, `NAME

` varchar (100) default NULL, `PRICE

` int (11) default NULL, `CODFRABICANTE ` int (11) NOT NULL,

PRIMARY KEY (`CODIGOART`, `CODFRABICANTE`), KEY `


CODFRABICANTE` (`CODFRABICANTE`)

) ENGINE = InnoDB DEFAULT CHARSET = latin1;

INSERT INTO `articles`
(CODIGOART ``, `name`, `PRICE`, `` CODFRABICANTE)
VALUES (100, 'MOUSE', 4230, 5),
(101, ' DELL Notebook ', 439223, 3),
(102,' Tower 50 DVD + R ', 7110, 1),
(104,' Base Notebook ', 22514, 8),
(105 , "Computer Assembled PIV ', 178214, 10), (106,' Multimedia Keyboard PS / 2 ', 4491, 6),

(107,' Aspire 5610 Notebook ', 466223, 4),

(108, 'Laser Printer', 12591, 7)
(109, 'Headphone and microphone Gamer', 18923, 9),
(110, 'USB notebook lamp', 9000, 5),
(111, 'mouse pad', 891, 10) ,
(112, 'Key PAD', 2691, 1),
(112, 'Screen Cleaner', 4500, 8),
(113, 'Backpack Notebook', 29723, 6);
CREATE TABLE `manufacturers` (
`CODE` int (11) NOT NULL, `NAME

` varchar (100) default NULL,

PRIMARY KEY (`CODE`)
) ENGINE = InnoDB DEFAULT CHARSET = latin1;
INSERT INTO `manufacturers` (`CODE`, `name`) VALUES

(1, 'National Isumo and co. ")

(3,' Dell Computer Inc. '), (2,' HP '),

(4,' International Steel '),
(5,' Genius'), (6, 'PC Tronix'),

(7, 'Sony'),

(8, 'Kensington'),
(9, 'Logitech'),
(10, 'National Importing');

ALTER TABLE `articles`
articulos_ibfk_1 `ADD CONSTRAINT` FOREIGN KEY (`CODFRABICANTE`) REFERENCES `manufacturer` (`CODE`) ON DELETE CASCADE ON UPDATE CASCADE;

Well we already have 2 related tables (manufacturers and articles) now create a new table we will call changes.
CREATE TABLE `changes' (
` id `int (11) NOT NULL,` nombre_art

`varchar (50) default NULL,
` user `varchar (50) default NULL,


`DATE` TIMESTAMP (0) NOT NULL DEFAULT '0000-00-00 00:00:00 ', PRIMARY KEY (`id`)
)
ENGINE = InnoDB DEFAULT CHARSET = latin1;


This table will store the item id, name, user, date and time to delete a table record manufacturers. Building


Trigger CREATE TRIGGER `` informatica. Fabricantes_before_ins_tr `` BEFORE DELETE ON `computing`. `Manufacturers`

FOR EACH ROW BEGIN INSERT INTO
SET id = old.CODIGO changes, nombre_art = old.Nombre, username = CURRENT_USER (), date = CURRENT_TIMESTAMP;
END;


In this case I used it
old
is a delete

in the table but if it were a
update or insert to use the alias New
.

Now we can only eliminate any record of the table manufacturers and reviewing changes in the table as has been almacenda information.

Note: To create trigger must have super user attributes database engine.
(If it fails check the semicolons and Blogger did remove some special characters to upload the information.)

more information in wikipedia x Jlara.

0 comments:

Post a Comment