On Mon, 10 May 2004 11:15:25 -0300
"Ronan Lucio" <[EMAIL PROTECTED]> wrote:

> Is the Primary Key Column mandatory?
> 
> Supposing:
> If I have two tables: Clients and Cars, and a third table
> Clients_R_Cars, that is a relationship between Clients and Cars.
> 
> I only need to know what cars the clients have.
> So, I just need to two columns "CliCar_ClientsID" and
> "CliCar_CarsID", the will be my index keys.
> 
> Even thus do I need to create a Primary Key Column "CliCar_ID"?

It is not "mandatory" to have a primary key, but you will have to handle duplicate 
rows in your front end program.  Having primary keys is generally a good idea.  In the 
situation that you are talking about, you can have a composite primary key (a primary 
key with more than one colunm) in your Clients_R_Cars table that is (Client_ID, 
Car_ID).  This says that in your Clients_R_Cars table no two rows can have the same 
Client_Id AND Car_ID.  Not sure if this is what you want or not.  

CREATE TABLE Clients_R_Cars (
   Client_ID INT NOT NULL,
   Car_ID INT NOT NULL,
   PRIMARY KEY (Client_ID, Car_ID)
);

If you really wanted to do this the relational way you would use InnoDB (or BDB) and 
use foreign keys as well.

CREATE TABLE Clients_R_Cars (
   Client_ID INT NOT NULL,
   Car_ID INT NOT NULL,
   INDEX client_id_ind (Client_ID),
   INDEX car_id_ind (Car_ID),
   FOREIGN KEY (Client_Id) REFERENCES Client(Client_Id) ON DELETE CASCADE ON UPDATE 
CASCADE,
   FOREIGN KEY (Car_Id) REFERENCES Car(Car_Id) ON DELETE CASCADE ON UPDATE CASCADE,
   PRIMARY KEY (Client_ID, Car_ID)
) TYPE=InnoDB;

Josh

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to