Convert MySQL tables to Microsoft tables

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

sqldba2013Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
sqldba2013Author Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott PletcherSenior DBACommented:
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
Scott PletcherSenior DBACommented:
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
Scott PletcherSenior DBACommented:
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
PortletPaulfreelancerCommented:
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
PortletPaulfreelancerCommented:
@Scott surprised there's no suggestion on clustered indexing (to my knowledge there's no equivalent in MySQL)
0
Scott PletcherSenior DBACommented:
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
sqldba2013Author Commented:
Thanks to all for your help/suggestion.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.