سؤال

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.

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top