2011-05-10 99 views
0

這是我在ms sql server中編寫的簡單查詢...現在我需要將此數據庫轉換爲mysql ...建議我以最簡單的方式執行此操作...提前致謝轉換SQL Server查詢到MySQL

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 PRIMARY 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) 
) 
+4

有人有一個大寫鎖定迷戀... – Blindy 2011-05-10 14:02:18

+0

我沒有看到有什麼顯然不會與MySQL的工作。那麼,我認爲MySQL ISAM引擎不支持「引用」,但它仍然接受create語句,但它並沒有真正實施它。 (或者它支持引用,而不是「on update/on delete」子句?我忘記了。)我錯過了某些東西當然有可能。你有錯誤嗎?有什麼問題? – Jay 2011-05-10 14:17:17

+0

任何人都可以請告訴我是否有任何自由軟件支持這....或者我可以導入數據從SQL服務器到MySQL ...因爲我沒有足夠的時間... – james 2011-05-10 14:40:26

回答

1

的基本語法應該工作,但是有一個問題,但:

您需要將所有的「內聯」爲橫生鍵約束任一表中的末端或到ALTER TABLE,如InnoDB的忽略直列約束「附接」到柱:

此外,InnoDB的不能識別或支持「內聯參考規範」(如SQL標準中定義的那樣),其中引用被定義爲列規範的一部分。 InnoDB只有在作爲單獨的FOREIGN KEY規範的一部分指定時才接受REFERENCES子句。

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

所以,這種說法

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 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) 
) 

注意在定位約束定義略有不同。

3

有沒有簡單的解決方案 - 你需要手動轉換它,手動。創建表的語法或多或少是相同的,你需要改變的是數據類型和約束。您可以檢查MySQL參考手冊中的所有數據類型和contraint創作語法,發現這裏:http://dev.mysql.com/doc/refman/5.0/en/sql-syntax.html

EDIT(基於評論)

在一個單獨的說明,如果你正在尋找從SQL Server遷移到MYSQL(不只是轉換查詢),嘗試尋找這裏: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. 
+0

可否請你告訴我有沒有任何支持這個免費軟件....或者我可以導入數據從SQL Server到MySQL ... – james 2011-05-10 14:39:47