Adjusting some of the table columns to support emojis on mysql 8. I successfully managed to update character set and collation to unicode utf8mb4 and utf8mb4_unicode_520_ci
When I test it with plain Insert and select queries I can able to see that emojis are successfully inserted. Problem happens when I try to use existing stored procedure to modify column it fails. Regardless of the client calls SP Scala app, Datagrip or mysql client I get same error Incorrect string value: '\xF0\x9F\x98\x85\xF0\x9F...' for column 'data'
I tried several things such as setting charset runtime before running SP and also inside the SP, adjusted SP input parameters with charset as well with no luck. Sharing stored procedure here as well to show whether I'm missing something or not but running out of ideas what can be wrong.
DROP PROCEDURE IF EXISTS deploy_scenario;
DELIMITER $$
CREATE PROCEDURE deploy_scenario (
IN scenarioId INT,
IN datax LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci,
IN creationDate DATETIME
)
BEGIN
DECLARE currentVersion INT;
SET CHARACTER SET utf8mb4;
# Get current version
SELECT IFNULL((SELECT version from scenario_version where scenario_id = scenarioId ORDER BY version DESC LIMIT 1),0)
INTO currentVersion;
# Increase current version
SET currentVersion = currentVersion + 1;
INSERT INTO scenario_version
(scenario_id, version, data, creation_date) VALUES
(scenarioId, currentVersion, datax, creationDate);
INSERT INTO engine_scenario VALUES (scenarioId,currentVersion,datax,creationDate)
ON DUPLICATE KEY UPDATE data = datax,deployed_version= currentVersion, last_op_date=creationDate;
UPDATE scenario SET status='DEPLOYED' WHERE id = scenarioId;
SELECT currentVersion;
END $$
DELIMITER ;
Here is the column details if It helps;
Related
I am learning to write MySQL stored procedures and I have encountered some difficulties. Here I have two stored procedures:
First stored procedure:
CREATE PROCEDURE sp1 (IN `username` TEXT, OUT `user_id` INT)
BEGIN
DECLARE rowcount INT;
SELECT count(`User ID`) INTO rowcount FROM user WHERE `Username`=username;
SET user_id = rowcount;
END|
Second stored procedure:
CREATE PROCEDURE sp2 (IN `doc_id` INT, IN `content` LONGTEXT)
BEGIN
UPDATE doc SET `Content`=content WHERE `Doc ID`=doc_id;
END|
(Delimiter is |.)
Question:
I observe that the result of the first stored procedure is the same as calling SELECT count(`User ID`) FROM user;. However, the second stored procedure does its job and gets the content updated with the new content.
So why does the first stored procedure treat `Username` and username as equal identifiers but the second stored procedure treats `Content` and content as different identifiers? The two identifiers in both cases are the same except the capitalization of the first letter.
I figure it out after reading the official MySQL documentation about the scope of local variables.
It states that:
A local variable should not have the same name as a table column. If an SQL statement, such as a SELECT ... INTO statement, contains a reference to a column and a declared local variable with the same name, MySQL currently interprets the reference as the name of a variable.
I want to update a MySQL database schema (with MySQL code) but I am unfortunately not sure of the state of the tables, as they are distributed..
Let's say some 'clients' have a table called "user" with a schema like
name VARCHAR(64) NOT NULL
password VARCHAR(64) NOT NULL
I want to add an email column, but it's possible that they already have an email column (depending on their installation version).
How can I run a command that ensures that there is a email column and does nothing if it's already there? Keep in mind I would be doing this for many tables that are more complex.
I know I could be creating temp tables and re-populating (and will if it's the only solution) but I figure there might be some kind of CREATE or UPDATE table command that has "oh you already have that column, skip" logic.
You can try like this:
DELIMITER $$
CREATE PROCEDURE Alter_MyTable()
BEGIN
DECLARE _count INT;
SET _count = ( SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'user' AND
COLUMN_NAME = 'email');
IF _count = 0 THEN
ALTER TABLE user
ADD COLUMN email varchar(512);
END IF;
END $$
DELIMITER ;
or rather make it a generic stored procedure like this:
create procedure AddColumnIfDoesntExists(
IN dbName tinytext,
IN tableName tinytext,
IN fieldName tinytext,
IN fieldDef text)
begin
IF NOT EXISTS (
SELECT * FROM information_schema.COLUMNS
WHERE column_name=fieldName
and table_name=tableName
and table_schema=dbName
)
THEN
set #ddl=CONCAT('ALTER TABLE ',dbName,'.',tableName,
' ADD COLUMN ',fieldName,' ',fieldDef);
prepare stmt from #ddl;
execute stmt;
END IF;
end;
//
delimiter ';'
If the column already exists the ALTER TABLE ADD COLUMN statement will throw an error, so if you are thinking that you might lose data because of trying to add a column that already exists that won't be the case, if any you need to handle error. See add column to mysql table if it does not exist
There are also resources telling you how to deal with these with store procedures, etc. See MySQL add column if not exist.
Hope it helps.
I'm new to SQL but I'm trying to minimize code duplication. I am working on a game whose sever will use MySQL C++ connector. I will also host a website that will access the same database. They both need to authenticate users. Ideally, I want to write a stored procedure that takes in a username and a hash, then returns a boolean if the name/password match.
I'm not too bad at writing queries, but stored proceedures are new territory.
Could someone give me a simple MySQL stored procedure example using a dummy table like this:
Table Player
Column PlayerID (int, primary key, not null, auto increment)
Column PlayerUsername (varchar 25, not null)
Column PlayerPassword (varchar 25, not null)
How I might create a stored procedure that takes in a name and password, and returns true if there is at least 1 record where the username and password match the parameter one.
Once I see the syntax for stored procedures I should be able to create my own. My needs are pretty basic.
Something like this
DELIMITER //
DROP FUNCTION IF EXISTS MY_FUNCTION//
create function MY_FUNCTION(name varchar(255) CHARACTER SET utf8,
pwd varchar(255) CHARACTER SET utf8, )
RETURNS tinyint(1)
READS SQL DATA SQL SECURITY INVOKER
BEGIN
declare result tinyint(1);
SELECT if(count(*)>0,1,0) INTO result
FROM users
WHERE users.name=name and users.pwd=pwd;
RETURN result;
END//
DELIMITER ;
I am trying to create a user-defined function in MySQL in order to generate unique numbers. As the auto_increment feature of MySQL just increments its seed by one, I need to have such a function to handle fields that need to be incremented by more than one. Here is my SQL script:
DELIMITER $$
CREATE FUNCTION `getUniqueID`(
id_type CHAR(1)
) RETURNS INT(10)
BEGIN
DECLARE run INT(10);
START TRANSACTION;
SELECT unique_gen_id INTO #run FROM tbl_unique_seed WHERE id_type = #id_type;
UPDATE tbl_unique_seed SET unique_gen_id = (unique_gen_id + 1) WHERE id_type = #id_type;
COMMIT;
RETURN run;
END$$
DELIMITER ;
I do not have much experience in MySQL, but creating this kind of functions in SQL Server is quite easy. It will be nice of you to help me figure out a solution for this issue. Currently, I am facing some syntax errors. The most basic ones relate to the syntax of my transaction and the select statement.
See the docs here: http://dev.mysql.com/doc/refman/5.0/en/replication-options-master.html#sysvar_auto_increment_increment
You can change the value of auto_increment_increment, which is normally 1.
I have a stored procedure that it works correctly on localhost but when i tried to execute that on my vps server, i give an empty result.
CREATE PROCEDURE `sp_contest_selectContestId`(
IN _uniquetitle VARCHAR(300))
BEGIN
SELECT `id`
FROM `contest`
WHERE
`uniquetitle` = _uniquetitle
LIMIT 0, 1
;END
When i use this part without using procedure with the same data to test, i have not any problem:
SELECT `id`
FROM `contest`
WHERE
`uniquetitle` = _uniquetitle
LIMIT 0, 1
I was using UTF-8 data. My tables was UTF8 but not my database. database was latin_swedish.
I change my database collation to UTF8 and then import my data again. problem solved.
I had to set mydatabase collation to UTF-8 befor insert or import any things.