Note that there are some explanatory texts on larger screens.

plurals
  1. POTriggers and multi-column Primary Keys
    primarykey
    data
    text
    <p>I have a books table and a music table. Both of these tables have a Product_ID as their primary key. I also have a products table that has a Product_ID column and a type column (0 for books, 1 for music) where the Product_Id and Type columns are the primary key for the table. In phpMyAdmin, these 2 columns are underlined and SHOW COLUMNS for the table shows Type and Product_Id marked as primary keys.</p> <p>The problem I'm having is with my trigger. There are 2 triggers, with a couple of differences to handle the different tables. The music trigger for example executes this statement:</p> <pre><code> INSERT INTO products SET Type=1, Product_Id=NEW.Product_ID, Title=NEW.Album, Price=0 </code></pre> <p>The books trigger is the same, except type=0 and NEW.Album is replaced with NEW.Title. When I'm running an INSERT query now though, after some data has been inserted and deleted, I'm getting a duplicate primary key error. The problem is that the MySQL database is only reading the Product_ID column as a primary key, so when its trying to insert a duplicate there, it spits out this error even though it the value in the Type column is different. I'm stumped here.</p> <p>The products table</p> <pre><code>Product_Id int(11) - PK Title int(11) Price double Type int(11) - PK NewBool tinyint(1) </code></pre> <p>The music Table</p> <pre><code>Product_ID int(11) - PK Artist varchar(32) Album varchar(32) Genre varchar(32) Year int(11) Length double Rating double NumRatings int(11) Studio varchar(64) </code></pre> <p>The books table</p> <pre><code>Product_ID int(11) - PK ISBN bigint(20) Title text Author text Year int(11) Genre text Pages int(11) Publisher text Edition int(11) Rating double NumRatings int(11) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload