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

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

Related

Create table mysql with TIMESTAMPDIFF

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;

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.

SQL syntax error, Can't find it?

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''comments' ( 'id' int(10) unsigned NOT NULL AUTO_INCREMENT, 'article_i' at line 1
CREATE TABLE IF NOT EXISTS 'comments' (
'id' int(10) unsigned NOT NULL AUTO_INCREMENT,
'article_id' int(10) NOT NULL,
'comment' varchar(45) NOT NULL,
'time' datetime NOT NULL,
'name' varchar(45) NOT NULL,
'email' varchar(45) NOT NULL,
PRIMARY KEY ('id'),
KEY 'fk_comments_article'('article_id')
);
Does anyone see the syntax error?
Use backticks instead of single qoutes .Single qoutes are used for string literals.
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`article_id` int(10) NOT NULL,
`comment` varchar(45) NOT NULL,
`time` datetime NOT NULL,
`name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
PRIMARY KEY (`id`),
KEY fk_comments_article(`article_id`)
);
backticks ` are used to enable identifires be used a column name / table name if they happen to be Keywords in MySQL .
It is the recommended way as it is highly unlikely that we know all the keywords beforehand and may end up using one of keyword as our name for column/table like you have done for column time in your CREATE satement .
But you should avoid using keywords known to you as identifires.
Remove the single quotes. Try this:
CREATE TABLE IF NOT EXISTS comments (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
article_id int(10) NOT NULL,
comment varchar(45) NOT NULL,
time datetime NOT NULL,
name varchar(45) NOT NULL,
email varchar(45) NOT NULL,
PRIMARY KEY (id),
KEY 'fk_comments_article'(article_id)
);
or try with back ticks:
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`article_id` int(10) NOT NULL,
`comment` varchar(45) NOT NULL,
`time` datetime NOT NULL,
`name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
PRIMARY KEY (`id`),
KEY fk_comments_article(`article_id`)
);
You are getting this error because when you write 'id' then it is treated as a string not a column which you intend
Remove Single quotes, tested below query in SQL Fiddle:
CREATE TABLE IF NOT EXISTS comments (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
article_id int(10) NOT NULL,
comment varchar(45) NOT NULL,
time datetime NOT NULL,
name varchar(45) NOT NULL,
email varchar(45) NOT NULL,
PRIMARY KEY (id),
KEY fk_comments_article(article_id)
);
Remove '
CREATE TABLE IF NOT EXISTS comments (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
article_id int(10) NOT NULL,
comment varchar(45) NOT NULL,
time datetime NOT NULL,
name varchar(45) NOT NULL,
email varchar(45) NOT NULL,
PRIMARY KEY (id),
KEY fk_comments_article(article_id)
);
Replace all of the single quotes ' with back ticks or simply remove the single quotes.
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`article_id` int(10) NOT NULL,
`comment` varchar(45) NOT NULL,
`time` datetime NOT NULL,
`name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_comments_article`(`article_id`)
);
You have used single quotes which is worng and used for string only not for enclosing column names. Removethat and use backticks since you have some keywords like TIME, NAME:
Try:
CREATE TABLE IF NOT EXISTS `comments` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`article_id` INT(10) NOT NULL,
`comment` VARCHAR(45) NOT NULL,
`time` DATETIME NOT NULL,
`name` VARCHAR(45) NOT NULL,
`email` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
KEY fk_comments_article(`article_id`)
);

Equivalent of MSSQL IDENTITY Column in MySQL

What is the equivalent of MSSQL IDENTITY Columns in MySQL? How would I create this table in MySQL?
CREATE TABLE Lookups.Gender
(
GenderID INT IDENTITY(1,1) NOT NULL,
GenderName VARCHAR(32) NOT NULL
);
CREATE TABLE Lookups.Gender
(
GenderID INT NOT NULL AUTO_INCREMENT,
GenderName VARCHAR(32) NOT NULL
);
CREATE TABLE `Persons` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`LastName` varchar(255) NOT NULL,
`FirstName` varchar(255) DEFAULT NULL,
`Address` varchar(255) DEFAULT NULL,
`City` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=latin1;
This above example uses the AUTO_INCREMENT syntax. You can specify a starting offset specific to the table.
The Increment, however, has to be set globally.
SET ##auto_increment_increment=10;
You can also set a global default for the offset like follows:
SET ##auto_increment_offset=5;
To view your current values, type SHOW VARIABLES LIKE 'auto_inc%';

How do I remove a uniqueness constraint from a MySQL table?

I created a table in a MySQL database via the following:
CREATE TABLE `newsubscriptions_orderspecification` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`display_name` varchar(256) NOT NULL,
`sub_def_id` integer UNSIGNED NOT NULL,
`source_code_id` integer UNSIGNED NOT NULL,
`order_code_id` integer UNSIGNED NOT NULL,
`rate` numeric(5, 2) NOT NULL,
`type` varchar(4) NOT NULL,
`region` varchar(4) NOT NULL,
`term` varchar(4) NOT NULL,
`buyer_type` varchar(4) NOT NULL,
`is_active` bool NOT NULL,
UNIQUE (`sub_def_id`, `buyer_type`, `rate`, `is_active`)
)
;
How can I remove the uniqueness constraint?
use this:
ALTER TABLE `newsubscriptions_orderspecification` DROP INDEX `sub_def_id`