MySQL SHA256 with Insert Statement - mysql

I want to create a script to fill a database for testing. How would I set a string to be hashed and the inserted into the database?
I have:
INSERT INTO `loop`.`User`
(`userID`,
`firstName`,
`lastName`,
`email`,
`password`,
`userName`,
`bio`,
`spamCount`)
VALUES
('gZvTtlPtjGRqeMBaLji3HxoKB5EZCsNL',
'Joe',
'Smith',
'test0#email.com',
SHA2('testgZvTtlPtjGRqeMBaLji3HxoKB5EZCsNL', 256),
'test0#email.com',
"TEST BIO",
0);
How do I hash the string and INSERT in same statement?

You can insert a SELECT instead of VALUES to run a function on one of the inputs:
INSERT INTO `loop`.`User`
(`userID`,
`firstName`,
`lastName`,
`email`,
`password`,
`userName`,
`bio`,
`spamCount`)
SELECT
'gZvTtlPtjGRqeMBaLji3HxoKB5EZCsNL',
'Joe',
'Smith',
'test0#email.com',
SHA2('testgZvTtlPtjGRqeMBaLji3HxoKB5EZCsNL', 256),
'test0#email.com',
"TEST BIO",
0;

If you already have the table filled by some content, you can alter it with the following :
ALTER TABLE `page` ADD COLUMN `hash` char(64) AS (SHA2(`content`, 256)) AFTER `content`
This solution will add hash column right after the content one, generates hash for existing and new records too without need to change your INSERT statement.

Related

Trying to insert data to multiple table with the last table `student` being the child table. Data is inserted to the other tables except the parent

BEGIN;
INSERT INTO `address` (`postal address`, `email`, `phone_number`) VALUES ('1527', 'tommydd#gmail.com', '0766452152');
INSERT INTO `bank` (`bank_number`, `bank_name`,`branch`) VALUES ('050002345678', 'Equity', 'Haniwa');
INSERT INTO `fees` (`perannum`, `fee_balance`) VALUES ('27000', '1500.34');
INSERT INTO `institution` (`Institution_name`, `course`, `form_study_year`, `adm_upi`, `type`) VALUES ('KU', 'Computer Science', '3rd Year', 'STT/B/01-003/2009', 'Public Day');
INSERT INTO `student` (`address_id`, `fees_id`, `bank_id`, `institution_id`, `first_name`, `middle_name`, `last_name`, `gender`, `date_of_birth`, `bc_no`, `parent`) VALUES ('LAST_INSERT_ID()', 'LAST_INSERT_ID()', 'LAST_INSERT_ID()', 'LAST_INSERT_ID()', 'Tommy', 'Gas', 'John', 'Male', '11/04/1999', '3868686', 'Both Alive');
COMMIT;

error when insert new values into database in infinityfree

When i use InfinityFree hosting, my register form doesn't work. But in localhost it's no problem.
I am trying to insert into my phpmyadmin database. The first column is the user_id column an it's an auto_increment field.
if i do this :
INSERT INTO `user` (`user_id`, `email`, `username`, `password`, `game_id`) VALUES('', '$email', '$username', '$password', '$game_id')
the error is :
Incorrect integer value: '' for column 'user_id' at row 1
and if i do this :
INSERT INTO `user` (`user_id`, `email`, `username`, `password`, `game_id`) VALUES(NULL, '$email', '$username', '$password', '$game_id')
the error is :
Data truncated for column 'game_id' at row 1
Instead of assigning a NULL value to user_id, just leave the user_id out of the query.
It should be like this:
INSERT INTO user (email, username, password, game_id) VALUES('$email', '$username', '$password', '$game_id')

MySQL INSERT and UPDATE with IF condition

I have a simple MySQL Database like:
CREATE TABLE `tab_update` (
`id` int(11) NOT NULL,
`number` int(11) NOT NULL,
`description` varchar(11) NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and some data inside:
INSERT INTO `tab_update` (`id`, `number`, `description`) VALUES ('111', '5', 'first value');
INSERT INTO `tab_update` (`id`, `number`, `description`) VALUES ('222', '5', 'first value');
INSERT INTO `tab_update` (`id`, `number`, `description`) VALUES ('333', '5', 'first value');
What I want is to:
insert data to the database, if the same 'id' does not exist already
or
update data if both below conditions are met:
a) 'id' is already in the database
b) new number value is bigger than the number stored already
What I am trying to do now is:
INSERT INTO tab_update (id, number, description) VALUES (333, 1, 'second value')
ON DUPLICATE KEY UPDATE number = GREATEST(number, VALUES(number)), description = 'second value'
this works partially - inserts data or update, however, I have a wrong behavior. If we have 'number = 1' (which is less then 5) this should not update the record.
At the moment keeps updating description, but should not.
Could you please guys help me improve this query?
update:
I am trying also:
INSERT INTO tab_update (id, number, description) VALUES (333, 6, 'second value')
ON DUPLICATE KEY UPDATE
number = GREATEST(number, VALUES(number)),
description = IF(VALUES(number) > number, VALUES(description), description)
but last line does not work as expected.

Trigger Error with MySQL

I'm doing something simple. Basically everytime a user register themself into the system, a trigger should run and copy the row which contain the ID and insert it into another table called userinfo.
Theres two table. One only contain username id and password, and another one contains foreign key linking the users.UserID and userinfo.UserID. I wish to insert new user to userinfo everytime users table is updated with new member. However the trigger keeps giving me error. Can someone tell me what am I doing wrong?
I'm open for any easier/proper way too. This is my first time playing with trigger.
CREATE TRIGGER NewUsers_Trigger AFTER insert ON users FOR EACH ROW
BEGIN INSERT INTO `pms`.`userinfo` (`UserID`, `FirstName`, `LastName`,
`Address`, `City`, `Country`, `PostCode`, `PhoneNumber`) VALUES ('2',
'test', 'test', 'test', 'test', 'test', 'test', 'test'); END
Trigger needs delimiter to be set, also you mentioned that there is a foreign key so the trigger should look like
delimiter //
CREATE TRIGGER NewUsers_Trigger AFTER insert ON users
FOR EACH ROW
BEGIN
INSERT INTO `pms`.`userinfo` (`UserID`, `FirstName`, `LastName`,
`Address`, `City`, `Country`, `PostCode`, `PhoneNumber`) VALUES (new.UserID,
'test', 'test', 'test', 'test', 'test', 'test', 'test');
END; //
delimiter ;
Note I have removed 2 with new.UserID which is the last inserted id from the user table so that same id is inserted to the other table and there is no error raised on foreign key constraints

select inside insert statmen and procedure

can i want to create store procedure that get arguments and run an select statment inside the insert statment?
i got 2 question:
1)how to get arguments on store procedure?
2)can i run select statment in insert statment like this?
DELIMITER $$
CREATE PROCEDURE `insertGuide` #m_name varchar(45) ,#m_last varchar(45) ,#addres varchar(45) ,#mphone int
BEGIN
INSERT INTO `guides` (`id`, `name`, `lastName`, `address`, `phone`)
VALUES (select max(id)+1 from seq,#m_name,#m_last,#addres,#mphone);
END
I don't understand the first question, but regarding the INSERT statement, this should work
INSERT INTO `guides` (`id`, `name`, `lastName`, `address`, `phone`)
select max(id)+1, #m_name, #m_last, #addres, #mphone
from seq;
try like this:
DELIMITER $$
DROP PROCEDURE IF EXISTS insertGuide;
CREATE PROCEDURE insertGuide (id int, name varchar(45) ,last varchar(45) ,addres varchar(45) ,phone int(12))
BEGIN
INSERT INTO `guides` (`id`, `name`, `lastName`, `address`, `phone`)
VALUES (id, name, last, addres, phone);
END
you can run select query in insert like:
INSERT INTO `guides` (`id`, `name`, `lastName`, `address`, `phone`)
SELECT MAX(id)+1, #m_name, #m_last, #addres, #mphone
FROM seq;