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?
The primary keys for the
event_persontables 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 KEYclause:Also note that there is no
nvarchardata 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: