Create table mysql with TIMESTAMPDIFF - mysql

I am converting some tables from my system that used sql server now to mysql, and am having a question in mysql how can I create a table with TIMESTAMPDIFF
Sql Server Table:
CREATE TABLE [dbo].[login_user](
[idx] [int] IDENTITY(1,1) NOT NULL,
[client_id] [int] NOT NULL,
[Login] [datetime] NULL,
[Logout] [datetime] NULL,
[Time] AS (datediff(second,[Login],[Logout])),
Mysql Table:
CREATE TABLE IF NOT EXISTS `login_user` (
`idx` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`client_id` INT(11) UNSIGNED NOT NULL DEFAULT '0',
`login` DATETIME NULL,
`Logout` DATETIME NULL,
`Time` AS TIMESTAMPDIFF(second,`login`,`Logout`),
PRIMARY KEY (`idx`)
) ENGINE=MyISAM;
The table in mysql is not being created, because of the TIMESTAMPDIFF, any tips on how to work this in the creation of the table?
my mysql is version 5.7.20
Image Error:

The following syntax works on MySQL 5.7:
CREATE TABLE IF NOT EXISTS `login_user` (
`idx` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`client_id` INT(11) UNSIGNED NOT NULL DEFAULT '0',
`login` DATETIME NULL,
`Logout` DATETIME NULL,
`Time` INT(11) AS (TIMESTAMPDIFF(second,`login`,`Logout`)),
PRIMARY KEY (`idx`)
) ENGINE=MyISAM;

Related

CONCAT not working in mysql56, need help running below sql

So I am using MySQL56, with nodejs. When trying to create the table below, I get an error for the uniqueId line. the error i get is:
CONCAT is not valid at this position.
CREATE TABLE IF NOT EXISTS `tickets` (
`id` INTEGER NOT NULL auto_increment ,
`title` VARCHAR(255),
`uniqueId` VARCHAR(255) DEFAULT concat(Test_string, `id`) UNIQUE,
`imageUrl` VARCHAR(255) NOT NULL,
`description` VARCHAR(255) NOT NULL,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NOT NULL,
PRIMARY KEY (`id`)) ENGINE=InnoDB;

Mysql Query is not giving good performance

I have a fairly simple query in MySQL but it is taking around 170 minutes to execute.
Can anyone help me here? I am tired of applying indexes on various keys but no benefit.
Update
H20_AUDIENCE_ADDRESS_LOG L
Join
TEMP_V_3064446579 T
Using
( ZS_AUDIENCE_ID, ZS_SOURCE_OBJECT_ID, ZS_ADDRESS_TYPE_ID )
Set
ZS_ACTIVE_PERIOD_END_DT = '2015-08-14 15:05:48',
ZS_IS_ACTIVE_PERIOD = False
Where
ZS_IS_ACTIVE_PERIOD = True
And
L.ZS_ADDRESS_ID <> T.ZS_ADDRESS_ID
And
T.ZS_SOURCE_TIMESTAMP > L.ZS_SOURCE_TIMESTAMP;
Creates:
CREATE TABLE `H20_AUDIENCE_ADDRESS_LOG` (
`ZS_AUDIENCE_ADDRESS_LOG_ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`ZS_AUDIENCE_ID` bigint(20) unsigned NOT NULL,
`ZS_SOURCE_OBJECT_ID` int(10) unsigned NOT NULL,
`ZS_INSERT_DT` datetime NOT NULL,
`ZS_ADDRESS_TYPE_ID` tinyint(3) unsigned NOT NULL,
`ZS_ADDRESS_ID` bigint(20) unsigned NOT NULL,
`ZS_SOURCE_TIMESTAMP` datetime NOT NULL,
`ZS_ACTIVE_PERIOD_START_DT` datetime DEFAULT NULL,
`ZS_ACTIVE_PERIOD_END_DT` datetime DEFAULT NULL,
`ZS_IS_ACTIVE_PERIOD` bit(1) DEFAULT NULL,
`ZS_ACTIVE_PRIORITY_PERIOD_START_DT` datetime DEFAULT NULL,
`ZS_ACTIVE_PRIORITY_PERIOD_END_DT` datetime DEFAULT NULL,
`ZS_IS_ACTIVE_PRIORITY_PERIOD` bit(1) DEFAULT NULL,
PRIMARY KEY (`ZS_AUDIENCE_ADDRESS_LOG_ID`),
KEY `IX_H20_AUDIENCE_ADDRESS_LOG` (`ZS_AUDIENCE_ID`,`ZS_SOURCE_OBJECT_ID`,`ZS_ADDRESS_TYPE_ID`,`ZS_ADDRESS_ID`),
KEY `IX_ADDRESS_ID` (`ZS_ADDRESS_ID`,`ZS_IS_ACTIVE_PERIOD`)
) ENGINE=InnoDB AUTO_INCREMENT=22920801 DEFAULT CHARSET=utf8;
CREATE TABLE `TEMP_V_3064446579` (
`ZS_AUDIENCE_ID` bigint(20) unsigned NOT NULL,
`ZS_SOURCE_OBJECT_ID` int(10) unsigned NOT NULL,
`ZS_ADDRESS_TYPE_ID` tinyint(3) unsigned NOT NULL,
`ZS_ADDRESS_ID` bigint(20) unsigned NOT NULL,
`ZS_SOURCE_TIMESTAMP` datetime NOT NULL,
UNIQUE KEY `IX_TEMP_V_3064446579` (`ZS_AUDIENCE_ID`,`ZS_SOURCE_OBJECT_ID`,`ZS_ADDRESS_TYPE_ID`,`ZS_ADDRESS_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Both tables circa 3m records
Something like this should work:
UPDATE
`H20_AUDIENCE_ADDRESS_LOG` `L`
SET
`ZS_ACTIVE_PERIOD_END_DT` = '2015-08-14 15:05:48',
`ZS_IS_ACTIVE_PERIOD` = False
WHERE
`ZS_IS_ACTIVE_PERIOD` = True AND
EXISTS (
SELECT
1
FROM
`TEMP_V_3064446579` `T`
WHERE
`L`.`ZS_ADDRESS_ID` <> `T`.`ZS_ADDRESS_ID` AND
`T`.`ZS_SOURCE_TIMESTAMP` > `L`.`ZS_SOURCE_TIMESTAMP`
LIMIT 1
);
(The ZS_ makes the SQL hard to read; suggest removing it.)
In TEMP_V_3064446579, change UNIQUE to PRIMARY.
Change
KEY `IX_H20_AUDIENCE_ADDRESS_LOG` (`ZS_AUDIENCE_ID`,`ZS_SOURCE_OBJECT_ID`,
`ZS_ADDRESS_TYPE_ID`,`ZS_ADDRESS_ID`)
to
KEY `IX_H20_AUDIENCE_ADDRESS_LOG` (`ZS_AUDIENCE_ID`,`ZS_SOURCE_OBJECT_ID`,
`ZS_ADDRESS_TYPE_ID`,`ZS_ADDRESS_ID`,
`ZS_SOURCE_TIMESTAMP`)
If you have a new enough version, please provide EXPLAIN UPDATE .... If not, please provide EXPLAIN SELECT ... where the SELECT is derived from the UPDATE, but without the SET.

MySql to MSSQL conversion

I have this query in mysql:
DROP TABLE IF EXISTS `ARTICLES`;
CREATE TABLE `ARTICLES` (
`ART_ID` int(11) NOT NULL,
`ART_ARTICLE_NR` varchar(66) NOT NULL,
`ART_SUP_ID` smallint(6) DEFAULT NULL,
`ART_DES_ID` int(11) DEFAULT NULL,
`ART_COMPLETE_DES_ID` int(11) DEFAULT NULL,
`ART_PACK_SELFSERVICE` smallint(6) DEFAULT NULL,
`ART_MATERIAL_MARK` smallint(6) DEFAULT NULL,
`ART_REPLACEMENT` smallint(6) DEFAULT NULL,
`ART_ACCESSORY` smallint(6) DEFAULT NULL,
`ART_BATCH_SIZE1` int(11) DEFAULT NULL,
`ART_BATCH_SIZE2` int(11) DEFAULT NULL,
PRIMARY KEY (`ART_ID`),
KEY `ART_SUP_ID` (`ART_SUP_ID`),
KEY `ART_DES_ID` (`ART_DES_ID`),
KEY `ART_COMPLETE_DES_ID` (`ART_COMPLETE_DES_ID`),
KEY `ART_ARTICLE_NR` (`ART_ARTICLE_NR`(18))
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I want the MSSQL version of this. I also read about the SSMA converter.
IF OBJECT_ID('ARTICLES','U') IS NOT NULL
DROP TABLE ARTICLES;
GO
CREATE TABLE ARTICLES
(
ART_ID INT NOT NULL PRIMARY KEY,
ART_ARTICLE_NR varchar(66) NOT NULL,
ART_SUP_ID INT ,
ART_DES_ID INT ,
ART_COMPLETE_DES_ID INT ,
ART_PACK_SELFSERVICE INT ,
ART_MATERIAL_MARK INT ,
ART_REPLACEMENT INT ,
ART_ACCESSORY INT ,
ART_BATCH_SIZE1 INT ,
ART_BATCH_SIZE2 INT
);
GO
/*
KEY ART_SUP_ID (ART_SUP_ID),
KEY ART_DES_ID (ART_DES_ID),
KEY ART_COMPLETE_DES_ID (ART_COMPLETE_DES_ID),
KEY ART_ARTICLE_NR (ART_ARTICLE_NR(18))
*/
For these indexes you will need to create them in separate SQL Statements. for example SQL Server equivalent of "KEY ART_SUP_ID (ART_SUP_ID)" will be
CREATE INDEX IX_SomeIdexName
ON ARTICLES (ART_SUP_ID)
GO
... and so on for each key statement in your mysql statement.

mysql best practice to concentrate data from multiple tables with different table design

My database contains around 20 tables that holds a user's information. For example it has
Personal : hold user personal info
Documents : uploaded files
Activities :
Etc..
Every table contains a user_Id column for wiring them together ( one to many relationship), along with different table specific columns and constraints.
My question is how should I fetch all data for a single user from all these tables ?
Currently when ever I load user , application need to do
Select * from table1 where user_Id = x;
Select * from table2 where user_Id = x;
Select * from table3 where user_Id = x;
..etc
Since I'm using php (oop) its not a bad thing as every table has its own model that retrieve it. Yet I'm worried about performance as I currently run over 20 queries every time I load page. And since these data are very dynamically updated. Caching isn't helping much.
So what is the best methodology to fix this ?
example of table structures
CREATE TABLE IF NOT EXISTS `documents` (
`id` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`collection` text NOT NULL,
`photo_date` timestamp NULL DEFAULT NULL,
`gallery` varchar(50) NOT NULL,
`cover` int(1) DEFAULT NULL,
`upload_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
CREATE TABLE IF NOT EXISTS `problemlist` (
`id` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`visit_id` int(10) unsigned NOT NULL,
`pt_id` int(10) unsigned NOT NULL,
`problem` varchar(200) NOT NULL,
`severity` int(2) unsigned NOT NULL,
`note` text,
`solved` int(1) unsigned NOT NULL,
`datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
CREATE TABLE IF NOT EXISTS `visits` (
`id` int(10) unsigned NOT NULL,
`pt_id` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`visit_date` timestamp NULL DEFAULT NULL,
`visit_end` timestamp NULL DEFAULT NULL,
`complain` varchar(250) DEFAULT NULL,
`dx` varchar(200) DEFAULT NULL,
`note` text,
`stats` enum('booked','waitting','finished','noshow','canceled','inroom') NOT NULL,
`deleted` int(1) DEFAULT NULL,
`booked_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`arrived_at` timestamp NULL DEFAULT NULL,
`started_at` timestamp NULL DEFAULT NULL,
`checkout_at` timestamp NULL DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=212 ;

Convert MySQL *.sql scripts(statements) to MS SQL SERVER *.sql script(statements)

How to convert MySQL *.sql scripts(statements) to MS SQL SERVER *.sql script(statements)?
For example, here is the MySQL sql statements:
DROP TABLE IF EXISTS `qs_ad_category`;
CREATE TABLE `qs_ad_category` (
`id` smallint(5) unsigned NOT NULL auto_increment,
`alias` varchar(100) NOT NULL,
`type_id` int(10) unsigned NOT NULL,
`categoryname` varchar(100) NOT NULL,
`admin_set` tinyint(3) unsigned NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM ;
DROP TABLE IF EXISTS `qs_admin`;
CREATE TABLE `qs_admin` (
`admin_id` smallint(5) unsigned NOT NULL auto_increment,
`admin_name` varchar(40) NOT NULL,
`email` varchar(40) NOT NULL,
`pwd` varchar(32) NOT NULL,
`pwd_hash` varchar(30) NOT NULL,
`purview` TEXT NOT NULL,
`rank` varchar(32) NOT NULL,
`add_time` int(10) NOT NULL,
`last_login_time` int(10) NOT NULL,
`last_login_ip` varchar(15) NOT NULL,
PRIMARY KEY (`admin_id`)
) TYPE=MyISAM ;
Sure I cannot execute this script in MS SQL SERVER2012, because some syntax is different. Is there any quick way to convert? Or the best way to do it is still manually rewrite a MS SQL version?
Try this:
use master;
go
if db_id('name_of_the_database')is not null
begin
drop database name_of_the_database;
end
go
create database name_of_the_database;
go
use name_of_the_database;
go
if object_id ('qs_ad_category')is not null
begin
drop table qs_ad_category;
end
create table qs_ad_category
(
id smallint check (id between 1 and 5) NOT NULL IDENTITY(1, 1) primary key,
alias varchar(100) NOT NULL,
type_id int check (type_id between 1 and 10) NOT NULL,
categoryname varchar(100) NOT NULL,
admin_set tinyint check(admin_set between 1 and 3) NOT NULL default '0'
);
go