开发者

Convert SQL Server query to MySQL

开发者 https://www.devze.com 2023-03-04 00:18 出处:网络
this is my simple query written in ms sql server... now i need to convert this database to mysql... suggest me the simplest way to do this...thanks in advance

this is my simple query written in ms sql server... now i need to convert this database to mysql... suggest me the simplest way to do this...thanks in advance

CREATE TABLE DEPARTMENT
(
DEPARTMENT_ID int NOT NULL PRIMARY KEY,
DEPARTMENT_NAME VARCHAR(20) NOT NULL,
DEAPRTMENT_OFFICE_NO VARCHAR(20),
)
CREATE TABLE PREVIOUS_ACADEMIC_INFO
(
ACADEMIC_ID int NOT NULL PRIMARY KEY,
DEGREE_DESCRIPTION VARCHAR(50),
CERTIFICATION VARCHAR(30),
DURATION DATETIME
)
CREATE TABLE CLASS
(
CLASS_NAME VARCHAR(20) NOT NULL PRIMARY KEY,
CLASS_STRENGTH INT NOT NULL,
)
CREATE TABLE STUDENT
(
STUDENT_REGNO  INT  NOT NULL PRIMARY KEY,
STUDENT_FIRSTNAME VARCHAR(30) NOT NULL,
STUDENT_LASTNAME VARCHAR(20) NOT NULL,
STUDENT_FATHER_NAME VARCHAR(30) NOT NULL,
STUDENT_EMAIL VARCHAR(20),
STUDENT_MOBILE_NO VARCHAR(15),
STUDENT_PHONE_NO VARCHAR(15),
STUDENT_ADDRESS VARCHAR (40),
DEPARTMENT_ID INT   CONSTRAINT DEP_1 FOREIGN KEY(DEPARTMENT_ID)
 REFERENCES DEPARTMENT(DEPARTMENT_ID),
ACADEMIC_ID INT CONSTRAINT PREVIOUS_ACADEMIC_1 FOREIGN KEY(ACADEMIC_ID)
REFERENCES PREVIOUS_ACADEMIC_INFO(ACADEMIC_ID),
CLASS_NAME VARCHAR(20) CONSTRAINT CLASS_1 FOREIGN KEY (CLASS_NAME)
REFERENCES CLASS(CLASS_NAME)
)
CREATE TABLE FACULTY
(
FACULTY_ID INT  NOT NULL PRIMARY KEY,
FACULTY_NAME VARCHAR(20) NOT NULL,
FACULTY_FATHER_NAME VARCHAR(30) NOT NULL,
FACULTY_EMAIL VARCHAR(20),
FACULTY_ADDRESS VARCHAR(40) NOT NULL,
FACULTY_MOBILE_NO VARCHAR(30),
FACULTY_PHONE_NO VARCHAR(20),
FACULTY_QUALIFICATION VARCHAR(20),
DEPARTMENT_ID INT CONSTRAINT DEPARTEMNT_2 FOREIGN KEY (DEPARTMENT_ID)
REFERENCES DEPARTMENT(DEPARTMENT_ID),
ACADEMIC_ID INT CONSTRAINT ACADEMIC_1 FOREIGN KEY(ACADEMIC_ID)
REFERENCES PREVIOUS_ACADEMIC_INFO(ACADEMIC_ID)
)
CREATE TABLE USER1
(
USERNAME VARCHAR(20) NOT NULL,
USER_PASSWORD VARCHAR(6) NOT NULL,
USER_TYPE VARCHAR(20) NOT NULL,

)
CREATE TABLE COURSE
(
COURSE_ID INT NOT NULL PRIMAR开发者_运维知识库Y KEY,
COURSE_NAME VARCHAR(20) NOT NULL,
COURSE_CREDIT_HR INT NOT NULL,
COURSE_DESCRIPTION VARCHAR(99),
)

CREATE TABLE ANNOUNCMENT
(
ANNOUNCMENT_ID int NOT NULL PRIMARY KEY,
ANNOUNCMENT_DESCRIPTION VARCHAR(50),
ANNOUNCMENT_TITLE VARCHAR(45)
)
CREATE TABLE FORUM
(
FORUM_ID int NOT NULL PRIMARY KEY,
FORUM_NAME VARCHAR(35) NOT NULL,
FORUM_DESCRIPTION VARCHAR(333),
FORUM_DISCUSSION VARCHAR(22),
POSTED_BY VARCHAR(34)
)
CREATE TABLE GRADE
(
LETTER_GRADE CHAR NOT NULL ,
PERCENTAGE FLOAT NOT NULL,
STUDENT_REGNO int CONSTRAINT STUDENT_5 FOREIGN KEY(STUDENT_REGNO)
REFERENCES STUDENT(STUDENT_REGNO),
COURSE_ID int CONSTRAINT COURSE_1 FOREIGN KEY(COURSE_ID)
REFERENCES COURSE(COURSE_ID)
)

CREATE TABLE TERM
(
TERM_START_DATE DATETIME NOT NULL,
TERM_END_DATE DATETIME NOT NULL,
student_regno int CONSTRAINT STUDENT_2 FOREIGN KEY(STUDENT_REGNO)
REFERENCES STUDENT(STUDENT_REGNO)
)

CREATE TABLE TEACHER_COURSE
(
AREA_OF_SPECILIZATION VARCHAR(30),
faculty_id int CONSTRAINT FACULTY_1 FOREIGN KEY(FACULTY_ID)
REFERENCES FACULTY(FACULTY_ID)
)


There is no easy solution - you need to convert it by hand, manually. The syntax for creating the table is more or less the same, the things you need t change are the data types, and the constraints. You can check all the data types and contraint creation syntax on the mysql reference manual, found here: http://dev.mysql.com/doc/refman/5.0/en/sql-syntax.html

EDIT (based on comment)

On a separate note, if you are looking to migrate from SQL SERVER to mySql (and not just convert the queries), try looking here: http://dev.mysql.com/tech-resources/articles/migrating-from-microsoft.html

Migration Tools
There are a wide variety of tools available to help you migrate a SQL Server or Access database to MySQL. We'll look at several different tools so you can choose the one that best suits your needs. The tools we will look at will include the following:

MSSQL2MYSQL
Microsoft DTS
SQLyog
Access Export 
Text Import/Export 
SQLYog and the Microsoft DTS wizard offer graphical interfaces that can be used with both MSSQL and Microsoft Access to import tables into MySQL. MSSQL2MYSQL is a script by Michael Kofler that can convert not only the table structure and data, but converts the index information as well. If you use Microsoft Access you may not have access to the above tools, but you can use the data export features of Access.


The basic syntax should work, but there is one gotcha though:

You need to move all of the "inline" foreign key constraints either to the end of the table or into an ALTER TABLE, as InnoDB ignores the inline constraints "attached" to a column:

Furthermore, InnoDB does not recognize or support “inline REFERENCES specifications” (as defined in the SQL standard) where the references are defined as part of the column specification. InnoDB accepts REFERENCES clauses only when specified as part of a separate FOREIGN KEY specification.

From: http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

So, this statement

CREATE TABLE GRADE
(
  LETTER_GRADE CHAR NOT NULL ,
  PERCENTAGE FLOAT NOT NULL,
  STUDENT_REGNO int CONSTRAINT STUDENT_5 FOREIGN KEY(STUDENT_REGNO)
  REFERENCES STUDENT(STUDENT_REGNO),
  COURSE_ID int CONSTRAINT COURSE_1 FOREIGN KEY(COURSE_ID)
  REFERENCES COURSE(COURSE_ID)
)

needs to become:

CREATE TABLE GRADE
(
  LETTER_GRADE CHAR NOT NULL,
  PERCENTAGE FLOAT NOT NULL,
  STUDENT_REGNO int,
  COURSE_ID int,
  CONSTRAINT STUDENT_5 FOREIGN KEY(STUDENT_REGNO) REFERENCES STUDENT(STUDENT_REGNO),
  CONSTRAINT COURSE_1 FOREIGN KEY(COURSE_ID) REFERENCES COURSE(COURSE_ID)
)

Note the slight difference in positioning the constraint definition.

0

精彩评论

暂无评论...
验证码 换一张
取 消