Of course you can create a PI and a PK on the same table, your example only fails because you try to create two PKs.
CREATE TABLE pk_test
(col1 INTEGER NOT NULL
,col2 INTEGER NOT NULL
,col3 INTEGER NOT NULL
,PRIMARY KEY(col1,col2)
)
PRIMARY INDEX (col3);
*** Table has been created.
*** Total elapsed time was 1 second.
SHOW TABLE pk_test;
*** Text of DDL statement returned.
*** Total elapsed time was 1 second.
CREATE SET TABLEpk_test ,NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
col1 INTEGER NOT NULL,
col2 INTEGER NOT NULL,
col3 INTEGER NOT NULL,
PRIMARY KEY ( col1 ,col2 ))
PRIMARY INDEX ( col3 );
SEL IndexNumber, IndexType, UniqueFlag FROM dbc.IndicesV
WHERE DatabaseName = DATABASE AND TableName = 'pk_test'
ORDER BY IndexNumber, ColumnPosition;
*** Query completed. 3 rows found. 3 columns returned.
*** Total elapsed time was 1 second.
IndexNumber IndexType UniqueFlag
----------- --------- ----------
1 P N
4 K Y
4 K YAnd a PK can be dropped if it's not the UPI of that table), but only when it's a named constraint, just don't ask me why :-)
CREATE TABLE pk_test
(col1 INTEGER NOT NULL
,col2 INTEGER NOT NULL
,col3 INTEGER NOT NULL
,CONSTRAINT pk PRIMARY KEY(col1,col2)
)
PRIMARY INDEX (col3);
ALTER TABLE pk_test DROP CONSTRAINT pk;
Dieter
↧