mySQL query to postgreSQL - mysql

I am new to PostgreSQL. while following one article which is on MySQL I am facing one challenge that the mySQL query is throwing error in PostgreSQL. Can anyone tel me the solution for this.
MySQL query is
CREATE TABLE "sessions" (
"id" varchar(128)
CHARACTER SET utf8mb4
COLLATE utf8mb4_bin NOT NULL,
"expires" int(11) unsigned NOT NULL,
"data" text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
PRIMARY KEY ("id")
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
What will be the similar query for PostgreSQL

I'm not sure how important the binary collations are for what you are doing. But this will probably work for you:
CREATE TABLE sessions (
id varchar(128) primary key,
expires int NOT NULL check(expires >= 0),
data text
) ;

Related

Replace the statement ON DUPLICATE KEY UPDATE

MySQL version 5.7, ONLY_FULL_GROUP_BY is enabled
Please tell me an effective way to solve the following problem: it is necessary to make an entry in the table only if there are no matches in the record for several fields. If there is a matching record, then we update it with new data.
The ON DUPLICATE KEY UPDATE statement does not suit me, because the type and uuid_session fields are not unique (for example, there may be several records with different type, but with the same uuid_session).
Here is an example of a fake-query to better understand my question:
INSERT INTO cameraStatus(time, uuid_session)
VALUES ('2022-12-14 16:01:00', '01234567-8901-2345-6789-012345678901')
ON DUPLICATE KEY UPDATE (type, uuid_session) = ("WORK", "55555555-8901-2345-6789-012345678901");
My table:
CREATE TABLE `cameraStatus` (
`id` int NOT NULL,
`camera_id` int NOT NULL DEFAULT '0',
`time` timestamp NOT NULL DEFAULT '2021-12-31 21:00:00',
`type` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'INFO',
`message` mediumtext COLLATE utf8_unicode_ci,
`uuid_session` varchar(36) CHARACTER SET utf8mb3 COLLATE utf8_unicode_ci DEFAULT '01234567-8901-2345-6789-012345678901'
)

MySQL Default Value as Expression is Wrong

I am running MySQL 8.0.17 and trying to add a default value to a column definition, specifically a JSON column.
create table `test` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `notes` json default ('[]')) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
The query executes fine, but when I look at the table structure the default value is listed as "(_utf8mb4'[]')".
This even happens if I set a default value for a VARCHAR field, but enclose the string in parentheses.
I've also tried using the MySQL JSON_ARRAY() function.
create table `test` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `notes` json default (JSON_ARRAY())) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
but this puts the "(json_array())" as a string as the default.
I feel like I'm doing this write based on the documentation. Is this a bug? or am I missing something?
Thanks!

The size of a table does not match the records in MySQL, AWS RDS

I have a problem with a MySQL table, the table has only 385 records that if I export them via Workbench the file is 894 KB in size. The problem is that the Data length is 10 GB when I try to inspect the table.
Note: In the table when making query only show 385 records.
The only solution to restore the size of the table that I have used is to drop the table and import it again.
I hope and you can help me to verify if it is a MySQL error or it is an attack or I don't know why the behavior is due.
The MySQL server is in an AWS RDS.
I have read the next article: https://docs.aws.amazon.com/es_es/AmazonRDS/latest/UserGuide/MySQL.KnownIssuesAndLimitations.html
CREATE TABLE `logs` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
`file` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL,
`status` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT 'yes',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=130204 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

arabic word changed to ???? after inserting into Mysql DB

when I insert data in this table, my query is
INSERT INTO urdu_word (word) VALUES ('Abdelali Abou Dher (عبد العالي ابو ذر)') ON DUPLICATE KEY UPDATE word='Abdelali Abou Dher (عبد العالي ابو ذر)' word value replace to like ???
My table structure is:
CREATE TABLE `urdu_word` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`word` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `word` (`word`),
KEY `idx_aml_word_status` (`word`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
I also tried to chage table structure to utf8_unicode_ci but same problem facing
CREATE TABLE `aml_word` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`word` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `word` (`word`),
KEY `idx_aml_word_status` (`word`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
mysql version 5.6
when insert query run in mysql command line then it inserted well in urdu but when I inserted through code using mybatis ORM then create problem.
The default character set for MySQL at (mt) Media Temple is latin1,
with a default collation of latin1_swedish_
So you need to change your callation
Try with this query
ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;
Followed by this tutorial
https://mediatemple.net/community/products/dv/204403914/default-mysql-character-set-and-collation#gs

#1075 MySQL Error

So i am just a beginner in all this php stuff. I know just the basics, and when i setting up the settings for my new table, I met the problem #1075. Before, i created one, almost similar to this one, and i don't see the differenc. Can you say me where is the problem and explain what is happening?
CREATE TABLE `try`.`testing` ( `id` INT NOT NULL AUTO_INCREMENT , `date` DATE NOT NULL , `text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , `text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ) ENGINE = MyISAM;
here is the code of my SQL Preview. I use phpMyAdmin, obviously.
Please, help me.
Thank, you)
Try this
CREATE TABLE `testing` (
`id` INT NOT NULL AUTO_INCREMENT,
`date` DATE NOT NULL,
`text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MYISAM ;
You have to declare your AUTO_INCREMENT field as a primary key or a key. So you have to add PRIMARY KEY (id) or KEY (id) to your CREATE TABLE statement:
CREATE TABLE `try`.`testing` (
`id` INT NOT NULL AUTO_INCREMENT,
`date` DATE NOT NULL ,
`text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`) -- as primary key
KEY (`id`) -- or as key
) ENGINE = MyISAM;
Please also check:
https://stackoverflow.com/a/8114994/3647441
https://stackoverflow.com/a/14087703/3647441
For an autoincrement field you should have some sort of index associated with it. eg: primary key which is missing
Try This.
CREATE TABLE `try`.`testing` (
`id` INT NOT NULL AUTO_INCREMENT,
`date` DATE NOT NULL ,
`text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
KEY (`id`)
) ENGINE = MyISAM;
https://dev.mysql.com/doc/refman/5.6/en/example-auto-increment.html