gpt4 book ai didi

mysql - Oracle和MySQL的代码形式区别

转载 作者:行者123 更新时间:2023-11-29 05:50:33 67 4
gpt4 key购买 nike

我正在通过 http://www.sqlines.com/online 将 Oracle 中的代码转换为 MYSQL。代码在 Oracle 中完全正确(创建的表和数据没有任何错误消息),但是当我复制代码并粘贴到 MYSQL 时出现错误消息:

SQL Error [1005] [HY000]: Can't create table universityddl.faculty (errno: 150 "Foreign key constraint is incorrectly formed").

我不明白是什么原因,因为我是 DBMS 的新手。如果有人可以提供帮助,我将不胜感激。

Oracle 中的代码:

CREATE TABLE Faculty ( 
FacNo CHAR(11) NOT NULL,
FacFirstName VARCHAR(30) NOT NULL,
FacLastName VARCHAR(30) NOT NULL,
FacCity VARCHAR(30) NOT NULL,
FacState CHAR(2) NOT NULL,
FacDept CHAR(6) NULL,
FacRank CHAR(4) NULL,
FacSalary DECIMAL(10,2) NULL,
FacSupervisor CHAR(11) NULL,
FacHireDate DATE NULL,
FacZipCode CHAR(10) NOT NULL,
CONSTRAINT FacultyPK PRIMARY KEY (FacNo),
CONSTRAINT SupervisorFK FOREIGN KEY (FacSupervisor) REFERENCES Faculty
);

CREATE TABLE Course (
CourseNo CHAR(6) NOT NULL,
CrsDesc VARCHAR(50) NOT NULL,
CrsUnits INTEGER NULL,
CONSTRAINT CoursePK PRIMARY KEY (CourseNo)
);

CREATE TABLE Offering (
OfferNo INTEGER NOT NULL,
CourseNo CHAR(6) NOT NULL,
OffTerm CHAR(6) NOT NULL,
OffYear INTEGER NOT NULL,
OffLocation VARCHAR(30) NULL,
OffTime VARCHAR(10) NULL,
FacNo CHAR(11) NULL,
OffDays CHAR(4) NULL,
CONSTRAINT OfferingPK PRIMARY KEY (OfferNo),
CONSTRAINT CourseFK FOREIGN KEY (CourseNo) REFERENCES Course,
CONSTRAINT FacultyFK FOREIGN KEY (FacNo) REFERENCES Faculty
);

CREATE TABLE Student (
StdNo CHAR(11) NOT NULL,
StdFirstName VARCHAR(30) NOT NULL,
StdLastName VARCHAR(30) NOT NULL,
StdCity VARCHAR(30) NOT NULL,
StdState CHAR(2) NOT NULL,
StdZip CHAR(10) NOT NULL,
StdMajor CHAR(6) NULL,
StdClass CHAR(2) NULL,
StdGPA DECIMAL(3,2) NULL,
CONSTRAINT StudentPk PRIMARY KEY (StdNo)
);

CREATE TABLE Enrollment(
OfferNo INTEGER NOT NULL,
StdNo CHAR(11) NOT NULL,
EnrGrade DECIMAL(3,2) NULL,
CONSTRAINT EnrollmentPK PRIMARY KEY (OfferNo,StdNo),
CONSTRAINT OfferingFK FOREIGN KEY (OfferNo) REFERENCES Offering
ON DELETE CASCADE,
CONSTRAINT StudentFK FOREIGN KEY (StdNo) REFERENCES Student
ON DELETE CASCADE
);

INSERT INTO Faculty VALUES ('543210987','VICTORIA','EMMANUEL','BOTHELL','WA','MS','PROF',120000.0,NULL,'15/Apr/1998','98011-2242');

INSERT INTO Faculty VALUES ('654321098','LEONARD','FIBON','SEATTLE','WA','MS','ASSC',70000.00,'543210987','1/May/1996','98121-0094');

INSERT INTO Faculty VALUES ('098765432','LEONARD','VINCE','SEATTLE','WA','MS','ASST',35000.00,'654321098','10/Apr/1997','98111-9921');

INSERT INTO Faculty VALUES ('765432109','NICKI','MACON','BELLEVUE','WA','FIN','PROF',65000.00,NULL,'11/Apr/1999','98015-9945');

INSERT INTO Faculty VALUES ('876543210','CRISTOPHER','COLAN','SEATTLE','WA','MS','ASST',40000.00,'654321098','1/Mar/2001','98114-1332');

INSERT INTO Faculty VALUES ('987654321','JULIA','MILLS','SEATTLE','WA','FIN','ASSC',75000.00,'765432109','15/Mar/2002','98114-9954');

INSERT INTO Course VALUES ('FIN300','FUNDAMENTALS OF FINANCE',4);

INSERT INTO Course VALUES ('FIN450','PRINCIPLES OF INVESTMENTS',4);

INSERT INTO Course VALUES ('FIN480','CORPORATE FINANCE',4);

INSERT INTO Course VALUES ('IS320','FUNDAMENTALS OF BUSINESS PROGRAMMING',4);

INSERT INTO Course VALUES ('IS460','SYSTEMS ANALYSIS',4);

INSERT INTO Course VALUES ('IS470','BUSINESS DATA COMMUNICATIONS',4);

INSERT INTO Course VALUES ('IS480','FUNDAMENTALS OF DATABASE MANAGEMENT',4);

INSERT INTO Offering VALUES (1111,'IS320','SUMMER',2010,'BLM302','10:30:00',NULL,'MW');

INSERT INTO Offering VALUES (1234,'IS320','FALL',2009,'BLM302','10:30:00','098765432','MW');

INSERT INTO Offering VALUES (2222,'IS460','SUMMER',2009,'BLM412','13:30:00',NULL,'TTH');

INSERT INTO Offering VALUES (3333,'IS320','SPRING',2010,'BLM214','8:30:00','098765432','MW');

INSERT INTO Offering VALUES (4321,'IS320','FALL',2009,'BLM214','15:30:00','098765432','TTH');

INSERT INTO Offering VALUES (4444,'IS320','WINTER',2010,'BLM302','15:30:00','543210987','TTH');

INSERT INTO Offering VALUES (5555,'FIN300','WINTER',2010,'BLM207','8:30:00','765432109','MW');

INSERT INTO Offering VALUES (5678,'IS480','WINTER',2010,'BLM302','10:30:00','987654321','MW');

INSERT INTO Offering VALUES (5679,'IS480','SPRING',2010,'BLM412','15:30:00','876543210','TTH');

INSERT INTO Offering VALUES (6666,'FIN450','WINTER',2010,'BLM212','10:30:00','987654321','TTH');

INSERT INTO Offering VALUES (7777,'FIN480','SPRING',2010,'BLM305','13:30:00','765432109','MW');

INSERT INTO Offering VALUES (8888,'IS320','SUMMER',2010,'BLM405','13:30:00','654321098','MW');

INSERT INTO Offering VALUES (9876,'IS460','SPRING',2010,'BLM307','13:30:00','654321098','TTH');

INSERT INTO Student VALUES ('123456789','HOMER','WELLS','SEATTLE','WA','98121-1111','IS','FR',3.00);

INSERT INTO Student VALUES ('124567890','BOB','NORBERT','BOTHELL','WA','98011-2121','FIN','JR',2.70);

INSERT INTO Student VALUES ('234567890','CANDY','KENDALL','TACOMA','WA','99042-3321','ACCT','JR',3.50);

INSERT INTO Student VALUES ('345678901','WALLY','KENDALL','SEATTLE','WA','98123-1141','IS','SR',2.80);

INSERT INTO Student VALUES ('456789012','JOE','ESTRADA','SEATTLE','WA','98121-2333','FIN','SR',3.20);

INSERT INTO Student VALUES ('567890123','MARIAH','DODGE','SEATTLE','WA','98114-0021','IS','JR',3.60);

INSERT INTO Student VALUES ('678901234','TESS','DODGE','REDMOND','WA','98116-2344','ACCT','SO',3.30);

INSERT INTO Student VALUES ('789012345','ROBERTO','MORALES','SEATTLE','WA','98121-2212','FIN','JR',2.50);

INSERT INTO Student VALUES ('876543210','CRISTOPHER','COLAN','SEATTLE','WA','98114-1332','IS','SR',4.00);

INSERT INTO Student VALUES ('890123456','LUKE','BRAZZI','SEATTLE','WA','98116-0021','IS','SR',2.20);

INSERT INTO Student VALUES ('901234567','WILLIAM','PILGRIM','BOTHELL','WA','98113-1885','IS','SO',3.80);

INSERT INTO Enrollment VALUES (1234,'123456789',3.30);

INSERT INTO Enrollment VALUES (1234,'234567890',3.50);

INSERT INTO Enrollment VALUES (1234,'345678901',3.20);

INSERT INTO Enrollment VALUES (1234,'456789012',3.10);

INSERT INTO Enrollment VALUES (1234,'567890123',3.80);

INSERT INTO Enrollment VALUES (1234,'678901234',3.40);

INSERT INTO Enrollment VALUES (4321,'123456789',3.50);

INSERT INTO Enrollment VALUES (4321,'124567890',3.20);

INSERT INTO Enrollment VALUES (4321,'789012345',3.50);

INSERT INTO Enrollment VALUES (4321,'876543210',3.10);

INSERT INTO Enrollment VALUES (4321,'890123456',3.40);

INSERT INTO Enrollment VALUES (4321,'901234567',3.10);

INSERT INTO Enrollment VALUES (5555,'123456789',3.20);

INSERT INTO Enrollment VALUES (5555,'124567890',2.70);

INSERT INTO Enrollment VALUES (5678,'123456789',3.20);

INSERT INTO Enrollment VALUES (5678,'234567890',2.80);

INSERT INTO Enrollment VALUES (5678,'345678901',3.30);

INSERT INTO Enrollment VALUES (5678,'456789012',3.40);

INSERT INTO Enrollment VALUES (5678,'567890123',2.60);

INSERT INTO Enrollment VALUES (5679,'123456789',2.00);

INSERT INTO Enrollment VALUES (5679,'124567890',3.70);

INSERT INTO Enrollment VALUES (5679,'678901234',3.30);

INSERT INTO Enrollment VALUES (5679,'789012345',3.80);

INSERT INTO Enrollment VALUES (5679,'890123456',2.90);

INSERT INTO Enrollment VALUES (5679,'901234567',3.10);

INSERT INTO Enrollment VALUES (6666,'234567890',3.10);

INSERT INTO Enrollment VALUES (6666,'567890123',3.60);

INSERT INTO Enrollment VALUES (7777,'876543210',3.40);

INSERT INTO Enrollment VALUES (7777,'890123456',3.70);

INSERT INTO Enrollment VALUES (7777,'901234567',3.40);

INSERT INTO Enrollment VALUES (9876,'124567890',3.50);

INSERT INTO Enrollment VALUES (9876,'234567890',3.20);

INSERT INTO Enrollment VALUES (9876,'345678901',3.20);

INSERT INTO Enrollment VALUES (9876,'456789012',3.40);

INSERT INTO Enrollment VALUES (9876,'567890123',2.60);

INSERT INTO Enrollment VALUES (9876,'678901234',3.30);

INSERT INTO Enrollment VALUES (9876,'901234567',4.00);

转换后的代码(到 MY SQL):

CREATE TABLE Faculty ( 
FacNo CHAR(11) NOT NULL,
FacFirstName VARCHAR(30) NOT NULL,
FacLastName VARCHAR(30) NOT NULL,
FacCity VARCHAR(30) NOT NULL,
FacState CHAR(2) NOT NULL,
FacDept CHAR(6) NULL,
FacRank CHAR(4) NULL,
FacSalary DECIMAL(10,2) NULL,
FacSupervisor CHAR(11) NULL,
FacHireDate DATE NULL,
FacZipCode CHAR(10) NOT NULL,
CONSTRAINT FacultyPK PRIMARY KEY (FacNo),
CONSTRAINT SupervisorFK FOREIGN KEY (FacSupervisor) REFERENCES Faculty
);

CREATE TABLE Course (
CourseNo CHAR(6) NOT NULL,
CrsDesc VARCHAR(50) NOT NULL,
CrsUnits INTEGER NULL,
CONSTRAINT CoursePK PRIMARY KEY (CourseNo)
);

CREATE TABLE Offering (
OfferNo INTEGER NOT NULL,
CourseNo CHAR(6) NOT NULL,
OffTerm CHAR(6) NOT NULL,
OffYear INTEGER NOT NULL,
OffLocation VARCHAR(30) NULL,
OffTime VARCHAR(10) NULL,
FacNo CHAR(11) NULL,
OffDays CHAR(4) NULL,
CONSTRAINT OfferingPK PRIMARY KEY (OfferNo),
CONSTRAINT CourseFK FOREIGN KEY (CourseNo) REFERENCES Course,
CONSTRAINT FacultyFK FOREIGN KEY (FacNo) REFERENCES Faculty
);

CREATE TABLE Student (
StdNo CHAR(11) NOT NULL,
StdFirstName VARCHAR(30) NOT NULL,
StdLastName VARCHAR(30) NOT NULL,
StdCity VARCHAR(30) NOT NULL,
StdState CHAR(2) NOT NULL,
StdZip CHAR(10) NOT NULL,
StdMajor CHAR(6) NULL,
StdClass CHAR(2) NULL,
StdGPA DECIMAL(3,2) NULL,
CONSTRAINT StudentPk PRIMARY KEY (StdNo)
);

CREATE TABLE Enrollment(
OfferNo INTEGER NOT NULL,
StdNo CHAR(11) NOT NULL,
EnrGrade DECIMAL(3,2) NULL,
CONSTRAINT EnrollmentPK PRIMARY KEY (OfferNo,StdNo),
CONSTRAINT OfferingFK FOREIGN KEY (OfferNo) REFERENCES Offering
ON DELETE CASCADE,
CONSTRAINT StudentFK FOREIGN KEY (StdNo) REFERENCES Student
ON DELETE CASCADE
);

INSERT INTO Faculty VALUES ('543210987','VICTORIA','EMMANUEL','BOTHELL','WA','MS','PROF',120000.0,NULL,'15/Apr/1998','98011-2242');

INSERT INTO Faculty VALUES ('654321098','LEONARD','FIBON','SEATTLE','WA','MS','ASSC',70000.00,'543210987','1/May/1996','98121-0094');

INSERT INTO Faculty VALUES ('098765432','LEONARD','VINCE','SEATTLE','WA','MS','ASST',35000.00,'654321098','10/Apr/1997','98111-9921');

INSERT INTO Faculty VALUES ('765432109','NICKI','MACON','BELLEVUE','WA','FIN','PROF',65000.00,NULL,'11/Apr/1999','98015-9945');

INSERT INTO Faculty VALUES ('876543210','CRISTOPHER','COLAN','SEATTLE','WA','MS','ASST',40000.00,'654321098','1/Mar/2001','98114-1332');

INSERT INTO Faculty VALUES ('987654321','JULIA','MILLS','SEATTLE','WA','FIN','ASSC',75000.00,'765432109','15/Mar/2002','98114-9954');

INSERT INTO Course VALUES ('FIN300','FUNDAMENTALS OF FINANCE',4);

INSERT INTO Course VALUES ('FIN450','PRINCIPLES OF INVESTMENTS',4);

INSERT INTO Course VALUES ('FIN480','CORPORATE FINANCE',4);

INSERT INTO Course VALUES ('IS320','FUNDAMENTALS OF BUSINESS PROGRAMMING',4);

INSERT INTO Course VALUES ('IS460','SYSTEMS ANALYSIS',4);

INSERT INTO Course VALUES ('IS470','BUSINESS DATA COMMUNICATIONS',4);

INSERT INTO Course VALUES ('IS480','FUNDAMENTALS OF DATABASE MANAGEMENT',4);

INSERT INTO Offering VALUES (1111,'IS320','SUMMER',2010,'BLM302','10:30:00',NULL,'MW');

INSERT INTO Offering VALUES (1234,'IS320','FALL',2009,'BLM302','10:30:00','098765432','MW');

INSERT INTO Offering VALUES (2222,'IS460','SUMMER',2009,'BLM412','13:30:00',NULL,'TTH');

INSERT INTO Offering VALUES (3333,'IS320','SPRING',2010,'BLM214','8:30:00','098765432','MW');

INSERT INTO Offering VALUES (4321,'IS320','FALL',2009,'BLM214','15:30:00','098765432','TTH');

INSERT INTO Offering VALUES (4444,'IS320','WINTER',2010,'BLM302','15:30:00','543210987','TTH');

INSERT INTO Offering VALUES (5555,'FIN300','WINTER',2010,'BLM207','8:30:00','765432109','MW');

INSERT INTO Offering VALUES (5678,'IS480','WINTER',2010,'BLM302','10:30:00','987654321','MW');

INSERT INTO Offering VALUES (5679,'IS480','SPRING',2010,'BLM412','15:30:00','876543210','TTH');

INSERT INTO Offering VALUES (6666,'FIN450','WINTER',2010,'BLM212','10:30:00','987654321','TTH');

INSERT INTO Offering VALUES (7777,'FIN480','SPRING',2010,'BLM305','13:30:00','765432109','MW');

INSERT INTO Offering VALUES (8888,'IS320','SUMMER',2010,'BLM405','13:30:00','654321098','MW');

INSERT INTO Offering VALUES (9876,'IS460','SPRING',2010,'BLM307','13:30:00','654321098','TTH');

INSERT INTO Student VALUES ('123456789','HOMER','WELLS','SEATTLE','WA','98121-1111','IS','FR',3.00);

INSERT INTO Student VALUES ('124567890','BOB','NORBERT','BOTHELL','WA','98011-2121','FIN','JR',2.70);

INSERT INTO Student VALUES ('234567890','CANDY','KENDALL','TACOMA','WA','99042-3321','ACCT','JR',3.50);

INSERT INTO Student VALUES ('345678901','WALLY','KENDALL','SEATTLE','WA','98123-1141','IS','SR',2.80);

INSERT INTO Student VALUES ('456789012','JOE','ESTRADA','SEATTLE','WA','98121-2333','FIN','SR',3.20);

INSERT INTO Student VALUES ('567890123','MARIAH','DODGE','SEATTLE','WA','98114-0021','IS','JR',3.60);

INSERT INTO Student VALUES ('678901234','TESS','DODGE','REDMOND','WA','98116-2344','ACCT','SO',3.30);

INSERT INTO Student VALUES ('789012345','ROBERTO','MORALES','SEATTLE','WA','98121-2212','FIN','JR',2.50);

INSERT INTO Student VALUES ('876543210','CRISTOPHER','COLAN','SEATTLE','WA','98114-1332','IS','SR',4.00);

INSERT INTO Student VALUES ('890123456','LUKE','BRAZZI','SEATTLE','WA','98116-0021','IS','SR',2.20);

INSERT INTO Student VALUES ('901234567','WILLIAM','PILGRIM','BOTHELL','WA','98113-1885','IS','SO',3.80);

INSERT INTO Enrollment VALUES (1234,'123456789',3.30);

INSERT INTO Enrollment VALUES (1234,'234567890',3.50);

INSERT INTO Enrollment VALUES (1234,'345678901',3.20);

INSERT INTO Enrollment VALUES (1234,'456789012',3.10);

INSERT INTO Enrollment VALUES (1234,'567890123',3.80);

INSERT INTO Enrollment VALUES (1234,'678901234',3.40);

INSERT INTO Enrollment VALUES (4321,'123456789',3.50);

INSERT INTO Enrollment VALUES (4321,'124567890',3.20);

INSERT INTO Enrollment VALUES (4321,'789012345',3.50);

INSERT INTO Enrollment VALUES (4321,'876543210',3.10);

INSERT INTO Enrollment VALUES (4321,'890123456',3.40);

INSERT INTO Enrollment VALUES (4321,'901234567',3.10);

INSERT INTO Enrollment VALUES (5555,'123456789',3.20);

INSERT INTO Enrollment VALUES (5555,'124567890',2.70);

INSERT INTO Enrollment VALUES (5678,'123456789',3.20);

INSERT INTO Enrollment VALUES (5678,'234567890',2.80);

INSERT INTO Enrollment VALUES (5678,'345678901',3.30);

INSERT INTO Enrollment VALUES (5678,'456789012',3.40);

INSERT INTO Enrollment VALUES (5678,'567890123',2.60);

INSERT INTO Enrollment VALUES (5679,'123456789',2.00);

INSERT INTO Enrollment VALUES (5679,'124567890',3.70);

INSERT INTO Enrollment VALUES (5679,'678901234',3.30);

INSERT INTO Enrollment VALUES (5679,'789012345',3.80);

INSERT INTO Enrollment VALUES (5679,'890123456',2.90);

INSERT INTO Enrollment VALUES (5679,'901234567',3.10);

INSERT INTO Enrollment VALUES (6666,'234567890',3.10);

INSERT INTO Enrollment VALUES (6666,'567890123',3.60);

INSERT INTO Enrollment VALUES (7777,'876543210',3.40);

INSERT INTO Enrollment VALUES (7777,'890123456',3.70);

INSERT INTO Enrollment VALUES (7777,'901234567',3.40);

INSERT INTO Enrollment VALUES (9876,'124567890',3.50);

INSERT INTO Enrollment VALUES (9876,'234567890',3.20);

INSERT INTO Enrollment VALUES (9876,'345678901',3.20);

INSERT INTO Enrollment VALUES (9876,'456789012',3.40);

INSERT INTO Enrollment VALUES (9876,'567890123',2.60);

INSERT INTO Enrollment VALUES (9876,'678901234',3.30);

INSERT INTO Enrollment VALUES (9876,'901234567',4.00);

最佳答案

1) 正如 Nick 所回答的,外键语法需要修复。有 5 个外键声明需要更改:

CREATE TABLE Faculty ( 
...
CONSTRAINT SupervisorFK FOREIGN KEY (FacSupervisor) REFERENCES Faculty(FacNo)
);

CREATE TABLE Offering (
...
CONSTRAINT CourseFK FOREIGN KEY (CourseNo) REFERENCES Course(CourseNo),
CONSTRAINT FacultyFK FOREIGN KEY (FacNo) REFERENCES Faculty(FacNo)
);

CREATE TABLE Enrollment(
....
CONSTRAINT OfferingFK FOREIGN KEY (OfferNo) REFERENCES Offering(OfferNo)
ON DELETE CASCADE,
CONSTRAINT StudentFK FOREIGN KEY (StdNo) REFERENCES Student(StdNo)
ON DELETE CASCADE
);

CONSTRAINT CourseFK FOREIGN KEY (CourseNo) REFERENCES Course(CourseNo)

代替:

CONSTRAINT CourseFK FOREIGN KEY (CourseNo) REFERENCES Course

2) 在插入数据时,您应该预料到处理日期时会出现问题。您正在为数据类型 date(即 Faculty.FacHireDate)的列提供 MySQL 值,例如 '1/Mar/2001':它将不接受。日期的默认格式是 yyyy-mm-dd

一种解决方案是在从 Oracle 提取数据时转换格式,方法是使用 TO_CHAR(FacHireDate, 'yyyy-mm-dd'),或者可能通过更改 NLS parameter NLS_DATE_FORMAT .否则,您可以使用 MySQL function STR_TO_DATE()将字符串翻译成日期:

INSERT INTO Faculty VALUES (
'543210987',
'VICTORIA',
'EMMANUEL',
'BOTHELL',
'WA',
'MS',
'PROF',
120000.0,
NULL,
STR_TO_DATE('15/Apr/1998', '%d/%b/%Y'),
'98011-2242'
);

通过这些修复,脚本在 this MySQL DB Fiddle 中运行良好.

关于mysql - Oracle和MySQL的代码形式区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54817992/

67 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com