How do i import a mysql dump to SQL Server database - mysql

Is it possible to import a mysql dump to a SQL Server database? I have some syntax problems with it.
I have looked through some articles and none of them had helped
Here is how the dump looks like
CREATE TABLE IF NOT EXISTS `search_by_vehicle` (
`id` int(11) NOT NULL auto_increment,
`vendor` varchar(255) NOT NULL,
`car` varchar(255) NOT NULL,
`year` varchar(255) NOT NULL,
`modification` varchar(255) NOT NULL,
`param_pcd` varchar(32) NOT NULL,
`param_dia` varchar(8) NOT NULL,
`param_nut` varchar(32) NOT NULL,
`param_bolt` varchar(32) NOT NULL,
`tyres_factory` text NOT NULL,
`tyres_replace` text NOT NULL,
`tyres_tuning` text NOT NULL,
`wheels_factory` text NOT NULL,
`wheels_replace` text NOT NULL,
`wheels_tuning` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO search_by_vehicle (vendor, car, modification, year, tyres_factory, tyres_replace, tyres_tuning, wheels_factory, wheels_replace, wheels_tuning, param_pcd, param_nut, param_bolt, param_dia)
VALUES( 'Jaguar','S-Type','3.0i','1998','235/50 R17','245/40 R18|245/35 R19','','7.5 x 17 ET45','8 x 18 ET40|8 x 19 ET40','','5*108','12*1.5','','63.3');

You could convert it to SQL Server syntax:
CREATE TABLE search_by_vehicle (
[id] int NOT NULL identity,
[vendor] varchar(255) NOT NULL,
[car] varchar(255) NOT NULL,
[year] varchar(255) NOT NULL,
[modification] varchar(255) NOT NULL,
[param_pcd] varchar(32) NOT NULL,
[param_dia] varchar(8) NOT NULL,
[param_nut] varchar(32) NOT NULL,
[param_bolt] varchar(32) NOT NULL,
[tyres_factory] varchar(max) NOT NULL,
[tyres_replace] varchar(max) NOT NULL,
[tyres_tuning] varchar(max) NOT NULL,
[wheels_factory] varchar(max) NOT NULL,
[wheels_replace] varchar(max) NOT NULL,
[wheels_tuning] varchar(max) NOT NULL,
PRIMARY KEY ([id])
) ;
INSERT INTO search_by_vehicle (vendor, car, modification, year, tyres_factory, tyres_replace, tyres_tuning, wheels_factory, wheels_replace, wheels_tuning, param_pcd, param_nut, param_bolt, param_dia)
VALUES( 'Jaguar','S-Type','3.0i','1998','235/50 R17','245/40 R18|245/35 R19','','7.5 x 17 ET45','8 x 18 ET40|8 x 19 ET40','','5*108','12*1.5','','63.3');
SELECT *
FROM search_by_vehicle;
LiveDemo
Using:
specific tool (for this specific example I used one of online free tool / do not recommend to use online tools when you handle sensitive data)
by hand
Keep in mind that it is not always possible to do it 1:1.
It looks like that column wheels_replace has non-atomic data ('8 x 18 ET40|8 x 19 ET40').
It could cause problems when you need to get specific value or need to join.

Related

Coverting script for older SQL version to work with 5.6

Apologies if my fundamental understanding of the issue is incorrect. I am not very experienced with SQL and am still learning.
I am attempting to generate a table for a data set and was given this script:
CREATE TABLE [dbo].[lobbying](
[uniqid] [varchar](36) NOT NULL, [registrant_raw] [varchar](110) NULL, [registrant] [varchar](50) NULL, [isfirm] [char](1) NULL,
[client_raw] [varchar](110) NULL, [client] [varchar](50) NULL,
[ultorg] [varchar](50) NULL,
[amount] [float] NULL,
[catcode] [char](5) NULL,
[source] [char] (5) NULL,
[self] [char](1) NULL,
[IncludeNSFS] [char](1) NULL,
[use] [char](1) NULL,
[ind] [char](1) NULL,
[year] [char](4) NULL,
[type] [char](4) NULL,
[typelong] [varchar](50) NULL, [affiliate] [char](1) NULL,
) ON [PRIMARY]
As you can probably tell, it doesn't work. The script was updated in 2015 so that is why I presume the issue to be the version. I tried using SQL Fiddle to figure out what was causing the issue, and found that taking the brackets out helped(which makes sense, as the tutorial I was following did not use any brackets for their tables). However, even with that, I still receive the error
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 'use char(1) NULL,
ind char(1) NULL,
year char(4) NULL,
type char(4) NULL,
typelo' at line 10
Does anybody know what the issue is here? Any help would be greatly appreciated. I've poured out about 4 hours into this project so far and have not been able to get past this roadblock.
It is enough to replace the names with brackets with backticks and also remove the brackets around the type nqames
CREATE TABLE lobbying(
`niqid`varchar(36) NOT NULL
, `egistrant_raw`varchar(110) NULL
, `egistrant` varchar(50) NULL
, `isfirm` char(1) NULL,
`client_raw` varchar(110) NULL
, `client` varchar(50) NULL,
`ultorg` varchar(50) NULL,
`amount` float NULL,
`catcode` char(5) NULL,
`source` char(5) NULL,
`self` char(1) NULL,
`IncludeNSFS` char(1) NULL,
`use` char(1) NULL,
`ind` char(1) NULL,
`year` char(4) NULL,
`type` char(4) NULL,
`typelong` varchar(50) NULL
, `affiliate` char(1) NULL
)
✓
db<>fiddle here

SQLite3 multiple primary key fields with autoincrement (rails project)

I'm trying to create a SQLite3 database for use with a Rails application.
I have the same database created already in MySQL and am trying to replicate the same in SQLite3.
The create syntax for MySQL is
CREATE TABLE `leaderboard_A` (
`league` tinyint(1) NOT NULL,
`id` bigint(10) NOT NULL,
`cut` tinyint(1) NOT NULL,
`wd` tinyint(1) NOT NULL,
`tie` tinyint(1) NOT NULL,
`pos` int(4) NOT NULL,
`pos_s` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`to_par` smallint(3) NOT NULL,
`to_par_s` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
`hole` varchar(8) COLLATE utf8_unicode_ci NOT NULL,
`round` smallint(6) NOT NULL,
`round_s` varchar(8) COLLATE utf8_unicode_ci NOT NULL,
`round_1` int(11) NOT NULL,
`round_2` int(11) NOT NULL,
`round_3` int(11) NOT NULL,
`round_4` int(11) NOT NULL,
`total` smallint(4) NOT NULL,
`tournament_id` varchar(13) COLLATE utf8_unicode_ci NOT NULL,
`tournament_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`year` smallint(4) NOT NULL,
PRIMARY KEY (`league`,`id`,`tournament_id`),
KEY `pos` (`pos`),
KEY `player_key` (`name`,`tournament_name`,`year`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I believe I need to have an ID column added in for my SQLite3 schema.
Is it possible to have an autoincrement column in my SQLite3 aswell as making other columns primary too?
I have some SQLite3 SQL written already with just a slightly different column setup as below.
CREATE TABLE IF NOT EXISTS Leaderboard (
id INTEGER,
current_position TEXT,
current_round INTEGER,
country TEXT,
is_amateur BOOLEAN,
first_name TEXT,
last_name TEXT,
name TEXT,
player_id INTEGER,
round1 INTEGER,
round2 INTEGER,
round3 INTEGER,
round4 INTEGER,
start_position TEXT,
status TEXT,
thru INTEGER,
today INTEGER,
total INTEGER,
tournament_name TEXT,
tournament_id INTEGER,
start_date datetime,
end_date datetime,
year INTEGER,
PRIMARY KEY (id, player_id, tournament_id, year)
)
My eventual solution is to read in a JSON record and update the details if it exists and if not create the record using the SQL below
INSERT OR REPLACE INTO Leaderboard (
current_position, current_round, country, is_amateur, first_name, last_name, name, player_id, round1, round2, round3, round4, start_position, status, thru, today, total, tournament_name, tournament_id, start_date, end_date, year)
VALUES
('1','4','USA','false','Jordan','Spieth','Spieth, Jordan','34046','68','67','71','69','T1','active','18','-1','-78','US Open','026','2015-06-18','2015-06-21','2015')
Any feedback, suggestions or fixes would be appreciated. New to working all this out. :)

show values for multiple ids in a column in mysql

I have a table called register, where I store user's data like username,birth-date,language.
there I offer them to add multiple languages. and I store that languages as id in database.
I have separate table for language too.
now I want to create a view that fetch all data with multiple select language.
CREATE TABLE `register` (
`index_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
`password` varchar(300) NOT NULL,
`m_status` varchar(200) NOT NULL,
`username` varchar(100) NOT NULL,
`occupation` int(5) NOT NULL,
`m_tongue` varchar(200) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
PRIMARY KEY (`index_id`)
) ENGINE=MyISAM AUTO_INCREMENT=621 DEFAULT CHARSET=latin1

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

Problems with a MySQL server change

I have three tables in MySQL:
CREATE TABLE `mlm`.`facturacion_2012_drm_base` ( `custid`
varchar(20) NOT NULL, `fecha` date NOT NULL, `docid` varchar(20)
NOT NULL, `billid` varchar(20) NOT NULL, `movimiento` varchar(20)
DEFAULT NULL, `movid` varchar(20) DEFAULT NULL, `medio_pago`
varchar(40) DEFAULT NULL, `digitos` varchar(20) DEFAULT NULL,
`monto_facturado` decimal(20,2) NOT NULL, `monto_pagado`
decimal(20,2) NOT NULL, `monto_usado` decimal(20,2) NOT NULL,
`documento` varchar(2) NOT NULL, `codigo_pago` varchar(5) DEFAULT
NULL, `desc_pago` varchar(100) DEFAULT NULL, `sociedad`
varchar(45) DEFAULT NULL, `sociedad_bonif` varchar(45) DEFAULT NULL,
KEY `billid` (`billid`), KEY `motors_no_fact`
(`custid`,`billid`,`fecha`,`documento`) USING BTREE, KEY
`facturacion` (`custid`,`fecha`,`documento`) USING BTREE )
ENGINE=MyISAM DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;
CREATE TABLE `mlm`.`facturacion_2012_drm_cortes` ( `id` bigint(20)
NOT NULL AUTO_INCREMENT, `fecha_inicial` date NOT NULL,
`fecha_final` date NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM
AUTO_INCREMENT=433 DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;
CREATE TABLE `mlm`.`facturacion_2012_drm_emitidas` ( `id`
bigint(20) NOT NULL AUTO_INCREMENT, `custid` varchar(20) NOT NULL,
`fecha_emision` date NOT NULL, `id_fechas` bigint(20) NOT NULL,
`monto` decimal(20,2) NOT NULL, `iva` decimal(20,2) NOT NULL,
`total` decimal(20,2) NOT NULL, `medio_pago` varchar(2000) NOT NULL,
`digitos` varchar(100) NOT NULL, `operaciones` int(10) NOT NULL,
`activa` varchar(2) NOT NULL, `movimiento` varchar(45) NOT NULL,
`parcialidades` varchar(100) NOT NULL, `monto_bruto` decimal(20,2)
NOT NULL, `billid` varchar(45) NOT NULL, `serie` varchar(2)
DEFAULT NULL, `folio` int(10) DEFAULT NULL, `uuid` varchar(45)
DEFAULT NULL, PRIMARY KEY (`id`), KEY `motors`
(`billid`,`id_fechas`,`activa`) ) ENGINE=MyISAM AUTO_INCREMENT=511483
DEFAULT CHARSET=latin1;
I changed the MySQL server from 5.1 (32 Bits) to 5.6 (64 Bits) and restored all my tables but I´m having problems with this query:
SELECT a.custid,
a.monto_facturado,
a.billid,
a.fecha,
b.id,
b.fecha_inicial
FROM facturacion_2012_drm_base a,
facturacion_2012_drm_cortes b
WHERE a.custid = ANY (SELECT custid
FROM facturacion_motors_pendientes
WHERE situacion = 'no facturado')
AND a.billid <> ALL (SELECT billid
FROM facturacion_2012_drm_emitidas
WHERE activa = 'SI')
AND a.fecha BETWEEN b.fecha_inicial AND b.fecha_final
AND a.documento = 'FA'
AND Year(a.fecha) = Year(Curdate())
GROUP BY a.billid
Since the server change, the query never finish, showing the message "Query is being executed..."
Anybody knows why this is happening?
At the very least you have indexing problems:
On facturacion_2012_drm_emitidas you filter by activa but do not have an index that can be used for this
On facturacion_2012_drm_base you don't have an index for fecha or documento that can be used
On facturacion_2012_drm_cortes you don't have an index for fecha_inicial or fecha_final
I don't know if you have index problems on facturacion_motors_pendientes because you did not include DDL for it.
Make sure you have the proper indexes for your query and then see what happens. What is happening now is that you have a complex join with subqueries and basically none of it is utilizing indexes, requiring you to execute full table scans on all tables involved.
You also might consider converting these tables to InnoDB, as that is the preferred table type in 5.6. This is of course unless you need certain MyISAM functionality on these tables (like full text search).