如何将此 MS SQL 脚本重写为 MySQL 脚本?

发布于 2024-09-25 15:07:43 字数 1224 浏览 6 评论 0原文

我正在尝试学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送舟行 2024-10-02 15:07:43

event_person 表的主键在两个脚本中并不相同。 SQL Server 示例使用由两个字段组成的复合主键。这不能在列规范中定义,因此我们需要使用单独的 PRIMARY KEY 子句:

CREATE TABLE `event_person` (
  `event_id` int NOT NULL,
  `person_id` int NOT NULL,
  PRIMARY KEY (`event_id`, `person_id`)
);

另请注意,没有 nvarchar MySQL 中的数据类型。您需要使用列字符集

CREATE TABLE `events` (
  `id` int NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `dt` datetime,
  `name` varchar(50) CHARACTER SET ucs2
);

:另外,请注意,在上面的示例中,我们没有为主键约束命名。虽然这可能无关紧要,但您可能需要按如下方式指定名称:

CREATE TABLE `event_person` (
  `event_id` int NOT NULL,
  `person_id` int NOT NULL,
  CONSTRAINT `pk_event_person` PRIMARY KEY (`event_id`, `person_id`)
);

CREATE TABLE `events` (
  `id` int NOT NULL AUTO_INCREMENT,
  `dt` datetime,
  `name` varchar(50) CHARACTER SET ucs2,
  CONSTRAINT `pk_ events ` PRIMARY KEY (`id`)
);

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 separate PRIMARY KEY clause:

CREATE TABLE `event_person` (
  `event_id` int NOT NULL,
  `person_id` int NOT NULL,
  PRIMARY KEY (`event_id`, `person_id`)
);

Also note that there is no nvarchar data type in MySQL. You would need to use a column character set:

CREATE TABLE `events` (
  `id` int NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `dt` datetime,
  `name` varchar(50) CHARACTER SET ucs2
);

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:

CREATE TABLE `event_person` (
  `event_id` int NOT NULL,
  `person_id` int NOT NULL,
  CONSTRAINT `pk_event_person` PRIMARY KEY (`event_id`, `person_id`)
);

CREATE TABLE `events` (
  `id` int NOT NULL AUTO_INCREMENT,
  `dt` datetime,
  `name` varchar(50) CHARACTER SET ucs2,
  CONSTRAINT `pk_ events ` PRIMARY KEY (`id`)
);
属性 2024-10-02 15:07:43

+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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文