Converting SQL Server Create script to MySQL - mysql

I have the following SQL Server Create Script. Can anyone help me convert this to MySQL.
I'm not sure what the bit defining the Primary Key is doing.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE IF NOT EXISTS `webdb`.`UserSessions`(
UniqueID VARCHAR(50) NOT NULL,
UserID VARCHAR(255) NOT NULL,
TokenExpires DATETIME NOT NULL,
LastSeen DATETIME NOT NULL,
LoggedOut bit NOT NULL,
CONSTRAINT [PK_EndedSessions] PRIMARY KEY CLUSTERED
(
UniqueID ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

CREATE TABLE IF NOT EXISTS `webdb`.`UserSessions`
(
UniqueID VARCHAR(50) NOT NULL PRIMARY KEY,
UserID VARCHAR(255) NOT NULL,
TokenExpires DATETIME NOT NULL,
LastSeen DATETIME NOT NULL,
LoggedOut bit NOT NULL
);

Related

Using number of leaves taken field i need to calculate number of remaining leaves,loss of pay and leave taken in total

USE [ctsdev]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_Leavetbl](
[Emp_id] [int] IDENTITY(1,1) NOT NULL,
[Month] [varchar](15) NULL,
[Year] [varchar](4) NULL,
[Rec_Name] [varchar](50) NULL,
[CompanyName] [varchar](30) NULL,
[Task] [varchar](20) NULL,
[NoOfDaysLeavTakn] [int] NOT NULL,
[NoOfAvailblLeav] [int] NULL,
[LossofPay] [int] NULL,
[LeaveTakndates] [varchar](200) NULL,
[TtlLeavTakn] [int] NULL,
CONSTRAINT [PK_tbl_Leavetbl] PRIMARY KEY CLUSTERED
(
[Emp_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
This is my table design.When the employee fills the number of leaves taken field ,with that value i need to find the remaining available leaves,like in an year only 12 leaves is permitted,and the total leaves he taken till to this month also have to display.
Is it possible through a procedure or any function ,could you please suggest some idea regarding this

SQL statement with square brackets AUTO_INCREMENT error

My new VPN accepts only sql statements with square brackets hence I edited the traditional sql query to:
CREATE TABLE admin (
[id] int(11) NOT NULL AUTO_INCREMENT,
[name] varchar(128) NOT NULL,
[email] varchar(64) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2;
But it shows this error:
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near
'AUTO_INCREMENT'
Basically the server is windows azure and I am using Microsoft SQL server management studio to execute sql queries.
CREATE TABLE [dbo].[admin](
[id] [int](11) IDENTITY(1,1) NOT NULL,
[name] [varchar](128) NULL,
[email] [varchar](64) NULL,
CONSTRAINT [PK_admin] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
In sql server should be created auto increment to use IDENTITY not AUTO_INCREMENT. Like this
CREATE TABLE admin (
[id] int(11) IDENTITY(1,1) NOT NULL ,
[name] varchar(128) NOT NULL,
[email] varchar(64) NOT NULL,
PRIMARY KEY (id)
)
More detail to refer this link http://www.w3schools.com/sql/sql_create_table.asp
CREATE TABLE [dbo].[admin](
[ID] [int]IDENTITY(1,2) NOT FOR REPLICATION NOT NULL,
[name] [char](128) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[email] [varchar](64) NOT NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Import sql syntax, Windows SQL Server and Apache Mysql

I have below sql export from Microsoft SQL Sever and I tried to import into Mysql running by Apache in osx. I got an Sql syntax error from line 1. I am new and I actually have never seen the sql using [ ] as quote fot string.
May I ask is the sql different from Sql Server in Windows and the
Mysql in Apache?
If I want to import below database into mysql, what should I check
and look at?
Thank you very much for your adverses!!
Best regards,
USE [boatexpress]
GO
/****** Object: Schema [boatexpress] Script Date: 07/22/2013 14:15:07 ******/
CREATE SCHEMA [boatexpress] AUTHORIZATION [boatexpress]
GO
/****** Object: Table [dbo].[waybill111] Script Date: 07/22/2013 14:15:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[waybill111](
[id] [int] IDENTITY(1,1) NOT NULL,
[slAAECode] [nvarchar](100) NULL,
[slProductName] [ntext] NULL,
[slRecName] [nvarchar](50) NULL,
[slRecMobi] [nvarchar](50) NULL,
[slRecAddress] [nvarchar](255) NULL,
[slZipCode] [nvarchar](50) NULL,
[slweight] [float] NULL,
[ktype] [nvarchar](50) NULL,
[worth] [nvarchar](150) NULL,
[adminid] [int] NULL,
[addtime] [date] NULL,
[insurance] [nvarchar](50) NULL,
CONSTRAINT [PK_waybill] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
The syntax from SQL to MySQL is different. If you want to create above table use this syntax.
Create the database
CREATE SCHEMA `boatexpress` ;
Create the table
CREATE TABLE boatexpress.waybill111 (
`id` int PRIMARY KEY AUTO_INCREMENT NOT NULL,
`slAAECode` nvarchar(100) NULL,
`slProductName` text NULL,
`slRecName` nvarchar(50) NULL,
`slRecMobi` nvarchar(50) NULL,
`slRecAddress` nvarchar (255) NULL,
`slZipCode` nvarchar(50) NULL,
`slweight` float NULL,
`ktype` nvarchar(50) NULL,
`worth` nvarchar(150) NULL,
`adminid` int NULL,
`addtime` date NULL,
`insurance` nvarchar(50) NULL);
Datatypes like ntext doesnt exists in MySQL. I just renamed it to TEXT. Also [] are not used to wrap columns but ` is. For datatypes you dont need to enclose them at all.
Is this what you are looking for?
Also, your SQL code is actually trying to use boatexpress before creating it.

SQL Server 2008 Foreign Key Constraint Error

I am creating a database with series of tables ddls, and in all are 13 tables. I am getting a foreign key constraint error when I run the ddl for TBNSOR_VICTIM table. The ddl was able to create the first fk constraint but I get an error creating the second fk constraint. Can somebody help me out or point me to what am missing in the scripts? This is the error am getting after running the ddl scripts
Msg 1776, Level 16, State 0, Line 2
There are no primary or candidate keys in the referenced table 'dbo.TBNSOR_OFFENSE' that match the referencing column list in the foreign key 'RD09RD03'
CREATE TABLE [dbo].[TBNSOR_OFFENSE](
[RD03_RC27SEQ_NBR] [int] NOT NULL,
[FK_RD01_RC17SID] [int] NOT NULL,
[RD03_RC27OFFENSE_DESC] [varchar] (100) NULL,
[RD03_STATUTE] [char](6) NULL,
[RD03_RC27PREDATOR_IND] [char] (1) NULL,
[RD03_RC27CONVICTION_DT] [date] NULL,
[RD03_RC27CONVICT_CITY] [varchar] (20) NULL,
[RD03_RC27CONVICT_STATE] [char] (2) NULL,
[RD03_RC27CONVICT_COUNTY] [varchar] (20) NULL,
[RD03_RC27CITY] [varchar] (20) NULL,
[RD03_RC27COUNTY] [varchar] (20) NULL,
[RD03_RC27OFFENSE_STATE] [char] (2) NULL,
[RD03_RC27OFFENSE_DATE] [date] NULL,
[RD03_RC27CONFIN_REL_DT] [date] NULL,
[RD03_RC27PP_RELEASE_DT] [date] NULL,
CONSTRAINT [CJ38ID03] PRIMARY KEY CLUSTERED
(
[RD03_RC27SEQ_NBR] ASC,
[FK_RD01_RC17SID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[TBNSOR_OFFENSE] WITH CHECK ADD CONSTRAINT [RD03RD01] FOREIGN KEY([FK_RD01_RC17SID])
REFERENCES [dbo].[TBNSOR_PHYSCSC] ([RD01_RC17SID])ON DELETE CASCADE
GO
ALTER TABLE [dbo].[TBNSOR_OFFENSE] CHECK CONSTRAINT [RD03RD01]
GO
CREATE TABLE [dbo].[TBNSOR_VICTIM](
[RD09_RC28SEQ_NUM] [int] NOT NULL,
[FK_RD01_RC17SID] [int] NOT NULL,
[FK_RD03_RC27SEQ_NBR] [int] NOT NULL,
[RD09_RC28SEX] [char] (1) NULL,
[RD09_RC28RACE] [char] (1) NULL,
[RD09_RC28AGE] [char](2) NULL,
[RD09_RC28HIGH_AGE] [char] (2) NULL,
[RD09_RC28LOW_AGE] [char] (2) NULL,
CONSTRAINT [CJ38ID09] PRIMARY KEY CLUSTERED
(
[RD09_RC28SEQ_NUM] ASC,
[FK_RD01_RC17SID] ASC,
[FK_RD03_RC27SEQ_NBR] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[TBNSOR_VICTIM] WITH CHECK ADD CONSTRAINT [RD09RD01] FOREIGN KEY([FK_RD01_RC17SID])
REFERENCES [dbo].[TBNSOR_PHYSCSC] ([RD01_RC17SID])ON DELETE CASCADE
GO
ALTER TABLE [dbo].[TBNSOR_VICTIM] CHECK CONSTRAINT [RD09RD01]
GO
ALTER TABLE [dbo].[TBNSOR_VICTIM] WITH CHECK ADD CONSTRAINT [RD09RD03] FOREIGN KEY([FK_RD03_RC27SEQ_NBR])
REFERENCES [dbo].[TBNSOR_OFFENSE]([RD03_RC27SEQ_NBR])ON DELETE CASCADE
GO
ALTER TABLE [dbo].[TBNSOR_VICTIM] CHECK CONSTRAINT [RD09RD03]
GO
You have primary key set up on two columns ([RD03_RC27SEQ_NBR] and [FK_RD01_RC17SID]) in [CJ38TBNSOR_OFFENSE] table, but your referencing just one of them.
It should be:
REFERENCES [cj38].[CJ38TBNSOR_OFFENSE]([RD03_RC27SEQ_NBR],[FK_RD01_RC17SID])

MSSMS 2008 rs error

This is my current table.
USE [PS_GameData]
GO
/****** Object: Table [dbo].[Guilds] Script Date: 04/04/2012 11:58:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Guilds](
[RowID] [int] IDENTITY(1,1) NOT NULL,
[GuildID] [int] NOT NULL,
[GuildName] [varchar](30) NOT NULL,
[MasterUserID] [varchar](12) NOT NULL,
[MasterCharID] [int] NOT NULL,
[MasterName] [varchar](30) NOT NULL,
[Country] [tinyint] NOT NULL,
[TotalCount] [smallint] NOT NULL,
[GuildPoint] [int] NOT NULL,
[Del] [tinyint] NOT NULL,
[CreateDate] [datetime] NOT NULL,
[DeleteDate] [datetime] NULL,
CONSTRAINT [PK_Guilds] PRIMARY KEY CLUSTERED
(
[GuildID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Guilds] ADD CONSTRAINT [DF_Guilds_Del] DEFAULT ((0)) FOR [Del]
GO
I am trying to change it to this.
USE [PS_GameData]
GO
/****** Object: Table [dbo].[Guilds] Script Date: 09/29/2011 06:36:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Guilds](
[RowID] [int] IDENTITY(1,1) NOT NULL,
[GuildID] [int] NOT NULL,
[GuildName] [varchar](30) COLLATE Chinese_PRC_Stroke_CI_AI NOT NULL,
[MasterUserID] [varchar](12) COLLATE Chinese_PRC_Stroke_CI_AI NOT NULL,
[MasterCharID] [int] NOT NULL,
[MasterName] [varchar](30) COLLATE Chinese_PRC_Stroke_CI_AI NOT NULL,
[Country] [tinyint] NOT NULL,
[TotalCount] [smallint] NOT NULL,
[GuildPoint] [int] NOT NULL,
[Del] [tinyint] NOT NULL CONSTRAINT [DF_Guilds_Del] DEFAULT (0),
[CreateDate] [datetime] NOT NULL,
[DeleteDate] [datetime] NULL,
CONSTRAINT [PK_Guilds] PRIMARY KEY CLUSTERED
(
[GuildID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
This is my error:
Msg 2714, Level 16, State 6, Line 2
There is already an object named 'Guilds' in the database.
Msg 1781, Level 16, State 1, Line 2
Column already has a DEFAULT bound to it.
Msg 1750, Level 16, State 0, Line 2
Could not create constraint. See previous errors.
I have already dropped table Guilds and tried to create another table named guilds and insert the script but the script stays the same or it says Guilds is already in database.
Try explicitly dropping the table at the beginning of your new DDL:
USE [PS_GameData]
GO
if exists(
select 1
from sys.objects o
inner join sys.schemas s on s.schema_id=o.schema_id
where o.[type]='U'
and s.name='dbo'
and o.name='Guilds'
)
drop table [dbo].[Guilds];
go
/****** Object: Table [dbo].[Guilds] Script Date: 09/29/2011 06:36:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Guilds](
[RowID] [int] IDENTITY(1,1) NOT NULL,
[GuildID] [int] NOT NULL,
[GuildName] [varchar](30) COLLATE Chinese_PRC_Stroke_CI_AI NOT NULL,
[MasterUserID] [varchar](12) COLLATE Chinese_PRC_Stroke_CI_AI NOT NULL,
[MasterCharID] [int] NOT NULL,
[MasterName] [varchar](30) COLLATE Chinese_PRC_Stroke_CI_AI NOT NULL,
[Country] [tinyint] NOT NULL,
[TotalCount] [smallint] NOT NULL,
[GuildPoint] [int] NOT NULL,
[Del] [tinyint] NOT NULL CONSTRAINT [DF_Guilds_Del] DEFAULT (0),
[CreateDate] [datetime] NOT NULL,
[DeleteDate] [datetime] NULL,
CONSTRAINT [PK_Guilds] PRIMARY KEY CLUSTERED
(
[GuildID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO