So, I have this one column in my table that gets filled by a trigger when a new entry is inserted.
CREATE TABLE `users` (
`idusers` int(11) NOT NULL AUTO_INCREMENT,
`uid` char(64) DEFAULT NULL,
`uname` varchar(80) NOT NULL,
`password` char(128) NOT NULL,
`mail` varchar(120) NOT NULL,
PRIMARY KEY (`idusers`),
UNIQUE KEY `uname_UNIQUE` (`uname`),
UNIQUE KEY `mail_UNIQUE` (`mail`),
UNIQUE KEY `uid_UNIQUE` (`uid`)
);
DELIMITER $$
TRIGGER `flask`.`users_BEFORE_INSERT` BEFORE INSERT ON `users` FOR EACH ROW
BEGIN
set new.uid = sha2(new.idusers, 256);
END$$
DELIMITER ;
The problem now is that when I try to add a new row (only have a test one yet because of the error), in the trigger the value of new.idusers is somehow always 0 instead of the current auto_increment value.
What do I need to change in my trigger code so that the value used for generating the uid is the actual id and not always 0?
Since idusers is an AUTO_INCREMENT field, its value is known only after the record is inserted in the table.
Use an AFTER INSERT trigger instead of a BEFORE INSERT trigger, and update the newly inserted record:
DELIMITER $$
CREATE TRIGGER `flask`.`users_AFTER_INSERT` AFTER INSERT ON `users` FOR EACH ROW
BEGIN
UPDATE users SET uid = sha2(NEW.idusers, 256) WHERE idusers = NEW.idusers;
END $$
DELIMITER ;
Related
I create two tables sequence and sequence_test,the definitions are follows:
create table sequence
(
id bigint unsigned auto_increment comment 'id'
primary key
)comment 'sequence' collate = utf8mb4_unicode_ci;
create table sequence_test
(
id bigint unsigned auto_increment comment 'id'
primary key,
se_id bigint unsigned not null comment 'trigger'
)
comment 'test' collate = utf8mb4_unicode_ci;
then I create a MySQL trigger to generate the se_id value when insert row into sequence_test table ,the trigger definition:
create trigger id_generator
before insert
on sequence_test for each row
begin
if new.se_id is null then
insert into sequence value ();
set new.se_id=(select last_insert_id() from sequence);
end if;
end;
then when I insert table, I get error like [21000][1242] Subquery returns more than 1 row,and why?
I have known the answer, here
set new.se_id=(select last_insert_id() from sequence);
use
set new.se_id=last_insert_id()
last_insert_id() will return the last insert value
I've a table tbl_bonus. I want to update the status of bonus when point==used_point. Want to update the status to used at that time. otherwise the status will remain unused.
This is the table.
CREATE TABLE `tbl_bonus` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`point` INT(5) NOT NULL,
`used_point` INT(5) DEFAULT NULL,
`status` ENUM('unused','used') NOT NULL,
PRIMARY KEY (`id`),
);
This is the trigger I'm trying to run.
DELIMITER $$
CREATE TRIGGER xyz BEFORE UPDATE ON tbl_bonus FOR EACH ROW
BEGIN
IF (new.used_point <=> new.point) THEN
SET new.status = 'used':
END IF:
END $$
DELIMITER :
How shall I write the trigger that will suffice my requirement? Will appreciate any sort of help.
I need to automatically update the value of AdoptionDate every time AdopterID receives a value different from NULL. I tried to do this using the code below, but it gives me Error Code: 1362.
DROP SCHEMA IF EXISTS `Assignment`;
CREATE SCHEMA `Assignment` DEFAULT CHARACTER SET utf8;
USE `Assignment`;
DROP TABLE IF EXISTS `Dog`;
CREATE TABLE `Dog` (
`DogID` INT NOT NULL AUTO_INCREMENT,
`Race` VARCHAR(32) NOT NULL,
`RescueDate` DATE NOT NULL,
`AdoptionDate` DATE DEFAULT NULL,
`AdopterID` INT(11) DEFAULT NULL,
PRIMARY KEY (`DogID`)
);
DELIMITER //
CREATE TRIGGER CustomTrigger AFTER UPDATE ON `Dog`
FOR EACH ROW
BEGIN
IF NEW.`AdopterID` <=> NULL THEN
SET NEW.`AdoptionDate` = NOW();
END IF;
END; //
DELIMITER ;
I am trying to make a table where the primary key is auto generated UUID every time there's an INSERT event. But the problem is when I try to execute an INSERT query skipping the primary key column like this:
INSERT INTO `tablename` (`reference`) VALUES ('hey');
No row is inserted and no errors. Just "0 rows inserted..."
Here's my table and the trigger. Thank you.
CREATE TABLE `tablename` (
`uuid` char(36) NULL,
`reference` varchar(100) NOT NULL,
PRIMARY KEY (uuid)
);
DELIMITER ;;
CREATE TRIGGER before_insert_tablename
BEFORE INSERT ON tablename
FOR EACH ROW
BEGIN
IF new.uuid IS NULL THEN
SET new.uuid = uuid();
END IF;
END
;;
DELIMITER ;
You CAN NOT make a Primary key as NULL
Just change it to NOT NULL
`uuid` char(36) NOT NULL,
The primary key for a table represents the column or set of columns that you use in your most vital queries. It has an associated index, for fast query performance. Query performance benefits from the NOT NULL optimization, because it cannot include any NULL values.
Reference
I'm a MySQL newbie, I just discovered that it doesn't support assertions.
I got this table:
CREATE TABLE `guest` (
`ssn` varchar(16) NOT NULL,
`name` varchar(200) NOT NULL,
`surname` varchar(200) NOT NULL,
`card_number` int(11) NOT NULL,
PRIMARY KEY (`ssn`),
KEY `card_number` (`card_number`),
CONSTRAINT `guest_ibfk_1` FOREIGN KEY (`card_number`) REFERENCES `member` (`card_number`)
)
What I need is that a member can invite maximum 2 guests.
So, in table guest I need that a specific card_number can appear maximum 2 times.
How can I manage it without assertions?
Thanks.
This definitly smells of a BEFORE INSERT trigger on the table 'guest':
DELIMITER $$
DROP TRIGGER IF EXISTS check_guest_count $$
CREATE TRIGGER check_guest_count BEFORE INSERT ON `guest`
FOR EACH ROW BEGIN
DECLARE numguests int DEFAULT 0;
SELECT COUNT(*) INTO numguests FROM `guest` WHERE card_number=NEW.card_number;
if numguests>=2 THEN
SET NEW.card_number = NULL;
END IF;
END;
$$
DELIMITER ;
This basically looks up the current guest count, and if it is already >=2 sets card_number to NULL. Since card_number is declared NOT NULL, this will reject the insert.
Tested and works for me on MySQL 5.1.41-3ubuntu12.10 (Ubuntu Lucid)