Solved

Convert MySQL tables to Microsoft tables

Posted on 2014-07-22
10
357 Views
Last Modified: 2014-07-27
Hi All,

Please advise how to manually convert below mentioned MySQL tables to Microsoft SQL Server Tables.
DROP TABLE IF EXISTS `AUTH_ROLE_FUNC_AR_REL` ;

CREATE TABLE IF NOT EXISTS `AUTH_ROLE_FUNC_AR_REL` (
    `AUTH_ROLE_ID` VARCHAR(20) NOT NULL,
    `AUTH_FUNCTION_ID` VARCHAR(20) NOT NULL,
    `AUTH_ACCESSRIGHT_ID` VARCHAR(20) NOT NULL,
    `ACTIVE` TINYINT(1) NOT NULL DEFAULT true,
    INDEX `fk_ROLE_FUNC_AR_REL_AUTH_ROLE1_idx` (`AUTH_ROLE_ID` ASC),
    PRIMARY KEY (`AUTH_ROLE_ID` , `AUTH_FUNCTION_ID` , `AUTH_ACCESSRIGHT_ID`),
    INDEX `fk_AUTH_ROLE_FUNC_AR_REL_AUTH_FUNCTION1_idx` (`AUTH_FUNCTION_ID` ASC),
    INDEX `fk_AUTH_ROLE_FUNC_AR_REL_AUTH_ACCESSRIGHT1_idx` (`AUTH_ACCESSRIGHT_ID` ASC),
    CONSTRAINT `fk_ROLE_FUNC_AR_REL_AUTH_ROLE1` FOREIGN KEY (`AUTH_ROLE_ID`)
        REFERENCES `AUTH_ROLE` (`ID`)
        ON DELETE NO ACTION ON UPDATE NO ACTION,
    CONSTRAINT `fk_AUTH_ROLE_FUNC_AR_REL_AUTH_FUNCTION1` FOREIGN KEY (`AUTH_FUNCTION_ID`)
        REFERENCES `AUTH_FUNCTION` (`ID`)
        ON DELETE NO ACTION ON UPDATE NO ACTION,
    CONSTRAINT `fk_AUTH_ROLE_FUNC_AR_REL_AUTH_ACCESSRIGHT1` FOREIGN KEY (`AUTH_ACCESSRIGHT_ID`)
        REFERENCES `AUTH_ACCESSRIGHT` (`ID`)
        ON DELETE NO ACTION ON UPDATE NO ACTION
)  ENGINE=XYZ1;

Open in new window

DROP TABLE IF EXISTS `USER` ;

CREATE TABLE IF NOT EXISTS `USER` (
    `ID` VARCHAR(20) NOT NULL,
    `USERNAME` VARCHAR(50) NOT NULL,
    `PASSWORD` VARCHAR(100) NULL,
    `DISPLAYNAME` VARCHAR(60) NOT NULL,
    `PARENT_ID` VARCHAR(20) NULL DEFAULT NULL,
    `DESIGNATION` VARCHAR(45) NULL,
    `EXTERNAL` TINYINT(1) NOT NULL DEFAULT false,
    `ACTIVE` TINYINT(1) NOT NULL DEFAULT true,
    PRIMARY KEY (`ID`),
    INDEX `fk_USER_USER1_idx` (`PARENT_ID` ASC),
    CONSTRAINT `fk_USER_USER1` FOREIGN KEY (`PARENT_ID`)
        REFERENCES `USER` (`ID`)
        ON DELETE NO ACTION ON UPDATE NO ACTION
)  ENGINE=XYZ1;

Open in new window

0
Comment
Question by:sqldba2013
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 167 total points
ID: 40211318
sql server doesn't have the IF NOT EXISTS.
the `xxx` needs to be [xxx]  (or xxx, but as you have used reserved keywords, better keep [ ]  )

tinyint(x) needs to be tinyint

true  needs to be 1
false  needs to be 0

ENGINE=XYZ1   clauses needs to be removed

the rest seems like it should work as is...
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 166 total points
ID: 40211417
The IF EXISTS can be rewritten like this...
IF EXISTS (SELECT name FROM sys.tables WHERE name='AUTH_ROLE_FUNC_AR_REL') 
   DROP TABLE AUTH_ROLE_FUNC_AR_REL

CREATE TABLE AUTH_ROLE_FUNC_AR_REL
   -- The rest of the code here

Open in new window

>    PRIMARY KEY (`ID`),
Should go at the end of the ID column, like this (also, why is a varchar the primary key?
    `ID` VARCHAR(20) NOT NULL PRIMARY KEY,

Open in new window

Looks like angelIII caught the rest.
0
 

Author Comment

by:sqldba2013
ID: 40211609
Thanks.

I've modified tables and I got below error. Please correct me.
Error:
Msg 1018, Level 15, State 1, Line 7
Incorrect syntax near 'INDEX'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required.
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near 'ACTION'.
Msg 102, Level 15, State 1, Line 15
Incorrect syntax near 'ACTION'.
Msg 102, Level 15, State 1, Line 18
Incorrect syntax near 'ACTION'.
IF EXISTS (SELECT name FROM sys.tables WHERE name='AUTH_ROLE_FUNC_AR_REL') 
DROP TABLE AUTH_ROLE_FUNC_AR_REL
GO
CREATE TABLE AUTH_ROLE_FUNC_AR_REL 
(
    [AUTH_ROLE_ID] VARCHAR(20) NOT NULL,
    [AUTH_FUNCTION_ID] VARCHAR(20) NOT NULL,
    [AUTH_ACCESSRIGHT_ID] VARCHAR(20) NOT NULL,
    [ACTIVE] TINYINT NOT NULL DEFAULT 1,
    INDEX [fk_ROLE_FUNC_AR_REL_AUTH_ROLE1_idx] ([AUTH_ROLE_ID] ASC),
    PRIMARY KEY ([AUTH_ROLE_ID] , [AUTH_FUNCTION_ID], [AUTH_ACCESSRIGHT_ID]),
    INDEX [fk_AUTH_ROLE_FUNC_AR_REL_AUTH_FUNCTION1_idx] ([AUTH_FUNCTION_ID] ASC),
    INDEX [fk_AUTH_ROLE_FUNC_AR_REL_AUTH_ACCESSRIGHT1_idx] ([AUTH_ACCESSRIGHT_ID] ASC),
    CONSTRAINT [fk_ROLE_FUNC_AR_REL_AUTH_ROLE1] FOREIGN KEY ([AUTH_ROLE_ID])
    REFERENCES [AUTH_ROLE] ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION,
    CONSTRAINT [fk_AUTH_ROLE_FUNC_AR_REL_AUTH_FUNCTION1] FOREIGN KEY ([AUTH_FUNCTION_ID])
    REFERENCES [AUTH_FUNCTION] ([ID]) ON DELETE NO ACTION ON UPDATE NO ACTION,
    CONSTRAINT [fk_AUTH_ROLE_FUNC_AR_REL_AUTH_ACCESSRIGHT1] FOREIGN KEY ([AUTH_ACCESSRIGHT_ID])
    REFERENCES [AUTH_ACCESSRIGHT] ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION
);

Open in new window

Error:
Msg 1018, Level 15, State 1, Line 11
Incorrect syntax near 'INDEX'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required.
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near 'ACTION'.
IF EXISTS (SELECT name FROM sys.tables WHERE name='[USER]') 
DROP TABLE [USER]
GO
CREATE TABLE [USER] (
    ID INT NOT NULL,
    [USERNAME] VARCHAR(50) NOT NULL,
    [PASSWORD] VARCHAR(100) NULL,
    [DISPLAYNAME] VARCHAR(60) NOT NULL,
    [PARENT_ID] VARCHAR(20) NULL DEFAULT NULL,
    [DESIGNATION] VARCHAR(45) NULL,
    [EXTERNAL] TINYINT NOT NULL DEFAULT 0,
    [ACTIVE] TINYINT NOT NULL DEFAULT 1,
    PRIMARY KEY ([ID]),
    INDEX [fk_USER_USER1_idx] ([PARENT_ID] ASC),
    CONSTRAINT [fk_USER_USER1] FOREIGN KEY ([PARENT_ID])
    REFERENCES [USER] ([ID]) ON DELETE NO ACTION ON UPDATE NO ACTION
);

Open in new window

0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 167 total points
ID: 40212571
First table:

IF OBJECT_ID('AUTH_ROLE_FUNC_AR_REL') IS NOT NULL
    DROP TABLE AUTH_ROLE_FUNC_AR_REL ;

CREATE TABLE dbo.AUTH_ROLE_FUNC_AR_REL (
    AUTH_ROLE_ID varchar(20) NOT NULL,
    AUTH_FUNCTION_ID varchar(20) NOT NULL,
    AUTH_ACCESSRIGHT_ID varchar(20) NOT NULL,
    ACTIVE tinyint NOT NULL DEFAULT 1,
    PRIMARY KEY (AUTH_ROLE_ID , AUTH_FUNCTION_ID , AUTH_ACCESSRIGHT_ID),
    CONSTRAINT fk_ROLE_FUNC_AR_REL_AUTH_ROLE1 FOREIGN KEY (AUTH_ROLE_ID)
        REFERENCES AUTH_ROLE (ID)
        ON DELETE NO ACTION ON UPDATE NO ACTION,
    CONSTRAINT fk_AUTH_ROLE_FUNC_AR_REL_AUTH_FUNCTION1 FOREIGN KEY (AUTH_FUNCTION_ID)
        REFERENCES AUTH_FUNCTION (ID)
        ON DELETE NO ACTION ON UPDATE NO ACTION,
    CONSTRAINT fk_AUTH_ROLE_FUNC_AR_REL_AUTH_ACCESSRIGHT1 FOREIGN KEY (AUTH_ACCESSRIGHT_ID)
        REFERENCES AUTH_ACCESSRIGHT (ID)
        ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE NONCLUSTERED INDEX fk_ROLE_FUNC_AR_REL_AUTH_ROLE1_idx ON dbo.AUTH_ROLE_FUNC_AR_REL (AUTH_ROLE_ID ASC)
CREATE NONCLUSTERED INDEX fk_AUTH_ROLE_FUNC_AR_REL_AUTH_FUNCTION1_idx ON dbo.AUTH_ROLE_FUNC_AR_REL (AUTH_FUNCTION_ID ASC)
CREATE NONCLUSTERED INDEX fk_AUTH_ROLE_FUNC_AR_REL_AUTH_ACCESSRIGHT1_idx ON dbo.AUTH_ROLE_FUNC_AR_REL (AUTH_ACCESSRIGHT_ID ASC)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40212578
Second table:


IF OBJECT_ID([USER]) IS NOT NULL
    DROP TABLE [USER]

CREATE TABLE [USER] (
    ID varchar(20) NOT NULL,
    USERNAME varchar(50) NOT NULL,
    PASSWORD varchar(100) NULL,
    DISPLAYNAME varchar(60) NOT NULL,
    PARENT_ID varchar(20) NULL DEFAULT NULL,
    DESIGNATION varchar(45) NULL,
    [EXTERNAL] tinyint NOT NULL DEFAULT 0,
    ACTIVE tinyint NOT NULL DEFAULT 0,
    PRIMARY KEY (ID),
    CONSTRAINT fk_USER_USER1 FOREIGN KEY (PARENT_ID)
        REFERENCES [USER] (ID)
        ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE NONCLUSTERED INDEX fk_USER_USER1_idx ON [USER] (PARENT_ID ASC)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40212582
I have no idea what:

ENGINE=XYZ1;

means.  SQL Server does not have that clause directly, so you would need to verify if/what SQL Server option replaces that.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40215781
MySQL's engine feature has no relevance in MS SQL as far as I can see.

the "Engine" options are  MyISAM, Memory, InnoDB, Archive, NDB which provide different capabilities eg:
		        MyISAM	Memory	InnoDB	Archive	NDB
Storage limits	    	256TB	RAM	64TB	None	384EB
Transactions	    	No	No	Yes	No	Yes
Locking granularity	Table	Table	Row	Table	Row

Open in new window

for more see
http://dev.mysql.com/doc/refman/5.1/en/storage-engines.html
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40215802
@Scott surprised there's no suggestion on clustered indexing (to my knowledge there's no equivalent in MySQL)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40217028
In SQL Server, the PK will be the clustered index by default.  I have no data with which to suggest a different clustering key(s).
0
 

Author Closing Comment

by:sqldba2013
ID: 40223428
Thanks to all for your help/suggestion.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question