Question

I have a table called enrollments that looks like this:

enrollmentindex - which is table primary key
username - varchar(64)
shortname - varchar(64)
role - varcar(64)

I have to leave the enrollmentindex col, but would like to create a second primary key (compound key) using username, shortname and role.

I am having trouble creating the new compound key because of the existence of the key on enrollmentindex.

Tried:

ALTER TABLE enrollments ADD PRIMARY KEY(username,shortname,role) 

-- results in error code 1068.

ALTER TABLE enrollments DROP PRIMARY KEY, ADD PRIMARY KEY(username,shortname,role)

-- results in error code 1075

There is no data in the table.

Was it helpful?

Solution

Error Code 1068 is for Multiple Primary Keys (not allowed)

Error Code 1075 is for Incorrect Table Definition (auto_increment fields must be defined as a key). It looks like enrollmentindex is an auto_increment field, so it will need to be included in your composite primary key.

I would suggest creating a separate UNIQUE index on username,shortname,role, if that's something you want to force

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top