List:General Discussion« Previous MessageNext Message »
From:Martin Edelius Date:March 22 1999 3:58pm
Subject:SV: How to make a joint primary key of two or more fields
View as plain text  
>it says i have to use PRIMARY KEY(index_col_name, ...), so does the
collumns i use for the joint primary have to be indexes?


I did this:
- Created table test
- Addad three columns, col1, col2, and col3, all ints NOT NULL
- Added indexes (for all three columns):  alter table test add index
index_col1 (col1);
- Made a multiple key out of the three columns: alter table test add
primary key pri_key (col1, col2, col3);

See below for a dump. Do you get this far?

mysql> show columns from test;  [Here I've just created the individual
indexes]
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1  | int(11) |      | MUL | 0       |       |
| col2  | int(11) |      | MUL | 0       |       |
| col3  | int(11) |      | MUL | 0       |       |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.02 sec)

mysql> alter table test add primary key pri_key (col1, col2, col3);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from test;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1  | int(11) |      | PRI | 0       |       |
| col2  | int(11) |      | PRI | 0       |       |
| col3  | int(11) |      | PRI | 0       |       |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.01 sec)



Best regards,
Martin Edelius
Spirex Digital Design

--------------------------------
www: http://www.spirex.se
Mail: martin.edelius@stripped
Phone: +46-31-514651, 0708-113711
Fax: +46-31-514331
S-422 43 Hisings Backa
--------------------------------



Thread
SV: How to make a joint primary key of two or more fieldsMartin Edelius22 Mar