如何将此 MS SQL 脚本重写为 MySQL 脚本?
我正在尝试学习Java 教程。
作者编写了使用 MS SQL 的教程。我想按照使用 MySQL 的教程进行操作。我不完全确定如何翻译使用“IDENTITY”、“CONSTRAINT”和“CLUSTERED”的 MS SQL 脚本,如下所示:
CREATE TABLE [event_person] (
[event_id] [int] NOT NULL,
[person_id] [int] NOT NULL,
CONSTRAINT [PK_event_person] PRIMARY KEY CLUSTERED
(
[event_id] ASC,
[person_id] ASC
)
)
CREATE TABLE [events] (
[id] [int] IDENTITY(1,1) NOT NULL,
[dt] [datetime] NULL,
[name] [nvarchar](50) NULL,
CONSTRAINT [PK_events] PRIMARY KEY CLUSTERED
(
[id] ASC
)
)
CREATE TABLE [people] (
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NOT NULL,
CONSTRAINT [PK_people] PRIMARY KEY CLUSTERED
(
[id] ASC
)
)
这是我所能得到的:
CREATE TABLE event_person (
event_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
person_id int NOT NULL
);
CREATE TABLE events (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
dt datetime NULL,
name nvarchar(50) NOT NULL);
CREATE TABLE people (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name nvarchar(50) NOT NULL);
..但我担心省略的代码会导致功能丢失,甚至与教程的其余部分不兼容。
我应该有更好的方法来写这个吗?
I'm trying to work my way through a Java Tutorial.
The author wrote the tutorial to work with MS SQL. I'd like to follow the tutorial using MySQL. I'm not completely sure how to translate the MS SQL script which uses "IDENTITY", "CONSTRAINT", and "CLUSTERED" as you'll see below:
CREATE TABLE [event_person] (
[event_id] [int] NOT NULL,
[person_id] [int] NOT NULL,
CONSTRAINT [PK_event_person] PRIMARY KEY CLUSTERED
(
[event_id] ASC,
[person_id] ASC
)
)
CREATE TABLE [events] (
[id] [int] IDENTITY(1,1) NOT NULL,
[dt] [datetime] NULL,
[name] [nvarchar](50) NULL,
CONSTRAINT [PK_events] PRIMARY KEY CLUSTERED
(
[id] ASC
)
)
CREATE TABLE [people] (
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NOT NULL,
CONSTRAINT [PK_people] PRIMARY KEY CLUSTERED
(
[id] ASC
)
)
This is as far as I have been able to get with it:
CREATE TABLE event_person (
event_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
person_id int NOT NULL
);
CREATE TABLE events (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
dt datetime NULL,
name nvarchar(50) NOT NULL);
CREATE TABLE people (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name nvarchar(50) NOT NULL);
... but I'm concerned that the omitted code will result in lost functionality and even incompatibility with the rest of the tutorial.
Is there a better way I should write this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
event_person
表的主键在两个脚本中并不相同。 SQL Server 示例使用由两个字段组成的复合主键。这不能在列规范中定义,因此我们需要使用单独的PRIMARY KEY
子句:另请注意,没有
nvarchar
MySQL 中的数据类型。您需要使用列字符集:另外,请注意,在上面的示例中,我们没有为主键约束命名。虽然这可能无关紧要,但您可能需要按如下方式指定名称:
The primary keys for the
event_person
tables are not equivalent in the two scripts. The SQL Server example is using a composite primary key, made up of two fields. This cannot be defined in the column specification, so we need to use a separatePRIMARY KEY
clause:Also note that there is no
nvarchar
data type in MySQL. You would need to use a column character set:In addition, note that in the above example we did not give names to the primary key constraints. While this is probably irrelevant, you may want to give names as follows:
+1 @Daniel Vassallo 的答案,还有这些提示:
Microsoft 使用方括号来分隔标识符,而 MySQL 使用反引号。这两个数据库都可以设置为使用 ANSI SQL 标准标识符分隔符(双引号)的模式。在 MySQL 中,不要分隔数据类型名称(例如
[int]
)。IDENTITY 特定于 Microsoft(以及 Sybase、fwiw)。在 MySQL 中,使用 AUTO_INCRMENT。您还可以使用 SERIAL,它是 BIGINT UNSIGNED AUTO_INCRMENT 的简写形式。
MySQL 中不支持 CLUSTERED 关键字。主键始终是 MySQL 的 InnoDB 存储引擎中的聚集索引(除非您有充分的理由不这样做,否则应该使用它)。另外,您不需要为 MySQL 中的索引声明 ASC 或 DESC。
+1 to @Daniel Vassallo's answer, and also these tips:
Microsoft uses square brackets to delimit identifiers, whereas MySQL uses back-ticks. Both databases can be put into a mode to use ANSI SQL standard identifier delimiters, which are double-quotes. In MySQL, don't delimit data type names (e.g.
[int]
).IDENTITY is specific to Microsoft (and Sybase, fwiw). In MySQL, use AUTO_INCREMENT. You can also use SERIAL which is shorthand for BIGINT UNSIGNED AUTO_INCREMENT.
There's no support for a CLUSTERED keyword in MySQL. The primary key is always a clustered index in MySQL's InnoDB storage engine (which you should use unless you have a good reason not to). Also you don't need to declare ASC or DESC for indexes in MySQL.