How to convert MS SQL queries to MySQL queries - mysql

I have a MSSQL queries file(.sql), now I need to convert it to MYSQL queries.
Please help me. The script like this:
CREATE TABLE [dbo].[Artist](
[ArtistId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](120) NULL,
PRIMARY KEY CLUSTERED
(
[ArtistId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

If you want to convert the DDL by hand, then you can do this by building up rules on a case by case basis, e.g. as follows:
[] need to be replaced with backticks
IDENTITY(1,1) can be replaced with AUTO_INCREMENT
Most of the ANSI options and Device settings
can be ignored (these seem to be present only because the table has
been rescripted)
w.r.t. dbo, MySQL doesn't implement schemas in the same way as SQL Server - you will either need to separate schemas into databases, or drop the schema, or mangle the schema name into the tablename (e.g. as a Prefix)
This will leave you with something like the following:
CREATE TABLE `Artist`(
`ArtistId` int NOT NULL AUTO_INCREMENT,
`Name` nvarchar(120) NULL,
PRIMARY KEY CLUSTERED
(
`ArtistId` ASC
)
);
Fiddle here
However, it is usually much easier to do this migration with a migration tool - search for the section on How to Transition from SQL Server to MySQL

Related

In SQL Server How to create full text index with TYPE COLUMN option

I have a table definition as below
CREATE TABLE [dbo].[Dialogs](
[ID] [int] IDENTITY(1,1) NOT NULL,
[DiscussionID] [int] NOT NULL,
[ApprovedByUserID] [int] NULL,
[AddedByUserID] [int] NULL,
[Text] [nvarchar](max) NULL,
[ApprovalStatus] [int] NULL,
[ApprovedOn] [datetime] NULL,
[AddedOn] [datetime] NOT NULL,
CONSTRAINT [PK_dbo.Dialogs] 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]
Text column contains html entered by user. i want to create full text index on Text column, i also need support for html filter so that when any user type <div>,<p> or any other html tag then it do not return any results.
To create index i use below SQL
CREATE FULLTEXT INDEX ON [Dialogs]
(
[Text] TYPE COLUMN '.html'
)
KEY INDEX [PK_dbo.Dialogs]
ON AOPRDefault;
but SQL Server throws error
Incorrect syntax near '.html'.
can any one please give a example of how to specify TYPE COLUMN option when creating full text index.
Firstly, the problem is that you should be referring to a column where you have '.html' rather than a literal, so you might have something like:
-- ADD COMPUTED COLUMN TO STORE FILE TYPE
ALTER TABLE dbo.Dialogs ADD FileExtension AS '.html';
CREATE FULLTEXT INDEX ON dbo.Dialogs ([Text] TYPE COLUMN FileExtension)
KEY INDEX [PK_dbo.Dialogs] ON AOPRDefault;
However you are misunderstanding the purpose of the TYPE COLUMN property, according to the documentation:
TYPE COLUMN type_column_name
Specifies the name of a table column, type_column_name, that is used to hold the document type for a varbinary(max) or image document. This column, known as the type column, contains a user-supplied file extension (.doc, .pdf, .xls, and so forth). The type column must be of type char, nchar, varchar, or nvarchar.
Specify TYPE COLUMN type_column_name only if column_name specifies a varbinary(max) or image column, in which data is stored as binary data; otherwise, SQL Server returns an error.
Note
At indexing time, the Full-Text Engine uses the abbreviation in the type column of each table row to identify which full-text search filter to use for the document in column_name. The filter loads the document as a binary stream, removes the formatting information, and sends the text from the document to the word-breaker component. For more information, see Configure and Manage Filters for Search
Since your index is on a text column this is not applicable and the create index statement would return an error. Even if you were storing the html document as binary data, then it would still not work as you intended, what you are after is html parsing, which is a separate issue from full text indexing.

Cannot create a row of size xxx which is greater than the maximum row size of 8060

I have seen this question asked quite a few times and most of them end with a logical explanation. My table doesn't seem to be anywhere near the maximum row size.
My Dev Server is SQL 2008 Express Edition
This is my table definition. I have one varchar(max) column and the rest of my columns should be tiny. The "Notes" filed didn't contain much text, only a few characters.
CREATE TABLE [dbo].[PegBoard](
[ID] [int] IDENTITY(1,1) NOT NULL,
[OwnersCorporationID] [int] NOT NULL,
[PegNumber] [int] NOT NULL,
[DepositRequired] [bit] NOT NULL,
[KeyRegister] [bit] NOT NULL,
[Notes] [varchar](max) NULL,
[Locked] [bit] NOT NULL,
[NonLoanable] [bit] NULL,
[DepositAmount] [decimal](10, 2) NULL,
[LastReviewedDate] [datetime] NULL,
CONSTRAINT [PK_PegBoard] PRIMARY KEY NONCLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [PegBoard_PN_Cnst] UNIQUE NONCLUSTERED
(
[PegNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
When I insert a row I receive the following message.
Error:System.Data.SqlClient.SqlException: Cannot create a row of size
8066 which is greater than the allowable maximum row size of 8060.
The statement has been terminated.
I have seen a similar warning when I added new columns to the table but it didn't seem to cause a failure until now.
Any ideas what could cause this problem and what I might try to fix it.
Thanks in Advance
David
Update
Here is the structure of my Audit Table if that helps.
The trigger itself is quite complicated as it was generated from some code I found that does all tables automatically.
CREATE TABLE [dbo].[Audit](
[AuditID] [int] IDENTITY(1,1) NOT NULL,
[Type] [char](1) NULL,
[TableName] [varchar](128) NULL,
[PrimaryKeyField] [varchar](1000) NULL,
[PrimaryKeyValue] [varchar](1000) NULL,
[FieldName] [varchar](128) NULL,
[OldValue] [varchar](1000) NULL,
[NewValue] [varchar](1000) NULL,
[UpdateDate] [datetime] NULL,
[UserName] [varchar](128) NULL
) ON [PRIMARY]
It is a common misunderstanding, that using VARCHAR(MAX) is a good idea in any cases... If you really have to deal with strings larger than 8000 bytes you could think about VARBINARY(MAX) or XML.
Read this: https://www.simple-talk.com/sql/database-administration/whats-the-point-of-using-varchar(n)-anymore/
But it may work: Read this: why row insert above 8053 bytes not giving error when it should because max allowed row limit is 8060
Another problem with VARCHAR(MAX) is, that in some statements the implicitly used data type is the "normal" varchar and you need extra casts:
Read this: https://stackoverflow.com/a/33031838/5089204
Conclusio: If you do not expect really large text it's better to use a VARCHAR(XX)definition.

Add new columns while generating scripts in sql server 2008

I' trying to generate the scripts for ma DB in Sql Server 2008.. and i'm able to do that, the scripts generated is :
USE [Cab_Booking]
GO
/****** Object: Table [dbo].[User] Script Date: 05/19/2013 10:33:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[User]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[User](
[U_Id] [int] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](50) NOT NULL,
[Password] [nvarchar](50) NOT NULL,
add column new int not null,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[U_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]
END
GO
What should i do if i need to add a new column in my table through scripts...
I know this sounds easy...But, i dont know what am i missing...
thanks..
Right click on the table name on SQL Server Management Studio and select "Design". Then add a column using designer, but don't save. Right click anywhere on table designer and select "Generate Change Script". Now you have the script required to add new column to table. This method also works for removing columns, changing data types, etc.

data flow from multiple source to single destinantion in SSIS

I need to import the data from the multiple distributed database ( around 70 ) to the single source table .So how is it possible through SSIS 2008
Assuming that you can run the same query against each of the 70 source servers, you can use a ForEach Loop with a single Data Flow Task. The source connection manager's ConnectionString should be an expression using the loop variables.
Here's an example reading the INFORMATION_SCHEMA.COLUMNS view from multiple DBs. I created the following tables on my local instance:
<!-- language: lang-sql -->
CREATE TABLE [MultiDbDemo].[SourceConnections](
[DatabaseKey] [int] IDENTITY(1,1) NOT NULL,
[ServerName] [varchar](50) NOT NULL,
[DatabaseName] [varchar](50) NOT NULL,
CONSTRAINT [PK_SourceConnections] PRIMARY KEY CLUSTERED
(
[DatabaseKey] 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
CREATE TABLE [MultiDbDemo].[SourceColumns](
[ColumnKey] [int] IDENTITY(1,1) NOT NULL,
[ServerName] [varchar](50) NOT NULL,
[DatabaseName] [varchar](50) NOT NULL,
[SchemaName] [varchar](50) NOT NULL,
[TableName] [varchar](50) NOT NULL,
[ColumnName] [varchar](50) NOT NULL,
CONSTRAINT [PK_SourceColumns] PRIMARY KEY CLUSTERED
(
[ColumnKey] 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
This is the control flow for the SSIS package:
The Source_AdoDotNet connection manager's ConnectionString property is set to the following expression:
SQL_GetSourceList's SQLStatement property is SELECT ServerName, DatabaseName FROM MultiDbDemo.SourceConnections, and the ResultSet is mapped to the User::SourceList variable.
The ForEach Loop task is configured thusly:
Note that the ADO object source variable is set to the User::SourceList variable populated in the SQL_GetSourceList task.
And the data flow looks like this:
ADO_SRC_SourceInfo is configured thusly:
The next effect of all this is that, for each database listed in the SourceConnections table, we execute the query SELECT LEFT(TABLE_SCHEMA, 50) AS SchemaName, LEFT(TABLE_NAME, 50) AS TableName, LEFT(COLUMN_NAME, 50) AS ColumnName FROM INFORMATION_SCHEMA.COLUMNS and save the results in the SourceColumns table.
You will still need 70 destination components. Simply specify the same table in all of them.

A Fast Way to select Large Amount of Data in SQL Server

this is the schema of my table:
CREATE TABLE [dbo].[ClassifiedDataStore_MasterTesting]
(
[PK_ID] [uniqueidentifier] NOT NULL,
[FK_SubCategory_Master] [int] NULL,
[FK_IntegratedWeb_Master] [int] NULL,
[FK_City_Master] [int] NULL,
[Title] [nvarchar](max) NULL,
[Description] [varchar](max) NULL,
[Url] [nvarchar](max) NULL,
[DisplayUrl] [varchar](max) NULL,
[Date] [datetime] NULL,
[ImageURL] [nvarchar](max) NULL,
[Price] [decimal](18, 2) NULL,
[Fetch_Date] [datetime] NULL,
[IsActive] [bit] NULL,
[record_id] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_ClassifiedDataStore_Master2] PRIMARY KEY CLUSTERED
(
[PK_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]
Wow, all those MAX columns... do you really need MAX for URLs and titles? Do you really need the PK to be a GUID?
Since most systems are I/O bound, one key to good performance (in addition to sensible indexing and only pulling the data you need) is to fit as much data onto each page as possible. With all these LOB columns storing potentially 2GB of data each, every page fetch is going to be a little bit of a nightmare for SQL Server. Strongly recommend considering trimming some of these data types where possible, e.g.
use an IDENTITY column instead of a GUID if feasible - why have both?
for any INTs that FK to lookups that will always have < 32K rows, use SMALLINT
for any INTs that FK to lookups that will always have < 255 rows, use TINYINT
use in-row storage (and not MAX types) for things like title and URL
you can shave a few bytes by using < 18 digits for price - doubt you will have classified items worth $1,000,000,000,000+
if < minute accuracy is not needed for Date/Fetch_Date, use SMALLDATETIME
if < day accuracy is not needed for Date/Fetch_Date, use DATE
(I also find it odd that you need Unicode/nvarchar for title, but not for description, and you need Unicode/nvarchar for URL/ImageURL, but not DisplayURL. Can you explain the rationale there? I'll give a hint: if the title can contain Unicode then it is reasonable to assume that the title could also be mentioned in the description, so it should also support Unicode. And all of your URLs are probably just fine supporting only varchar; I don't recall ever seeing a URL with Unicode characters in it (these are typically URL-encoded).)
Consider using data compression if you are on Enterprise Edition or better. Again, since most systems are I/O bound, we are happy to pay a slight CPU penalty compressing/decompressing data in order to fit it onto fewer pages, this will greatly reduce the time required to perform heavy read operations against the table.
When discussing performance it's always important to know what kind of queries will be most frequent for your table.
Your searches will be filtered using what columns? Title and Date?
Suppose that most of your queries start by filtering your table by: Date then by Title.
You should create a not unique clustered index using Date in first place and then Title.
Why is that? Because then your records are stored physically sequentially by that order making the searches much faster and that is why you can just have one clustered index per table.
Check this explanantion out