Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Foreign keys must point at a column of an other table (which has to be the same (ex.: INT(11) - INT(11)). When your tables are created, you can add a foreign key by using</p> <pre><code>ALTER TABLE mytable ADD FOREIGN KEY (myfkey) REFERENCES myothertable(parentkey) </code></pre> <p>Now if we apply it to your structure:</p> <pre><code>DROP TABLE items; DROP TABLE brands; DROP TABLE products; CREATE TABLE IF NOT EXISTS `brands` ( `brandId` int(11) NOT NULL AUTO_INCREMENT, `brandName` varchar(30) NOT NULL, `brandLogo` varchar(100) NOT NULL, `brandDescription` text NOT NULL, PRIMARY KEY (`brandId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ; INSERT INTO `brands` (`brandId`, `brandName`, `brandLogo`, `brandDescription`) VALUES (1, 'Samsung', '', 'Manufacturer of ...'), (2, 'Apple', '', 'American high-tech company ...'); CREATE TABLE IF NOT EXISTS `items` ( `itemId` int(11) NOT NULL AUTO_INCREMENT, `generalProductId` int(11) NOT NULL, `itemPurchasedPrice` double NOT NULL, `itemDateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `itemDescription` text, `itemBrand` int(11) NOT NULL, `itemBoughtFromPlace` int(11) NOT NULL, `itemBoughtFromUser` int(11) NOT NULL, `itemConditionNew` tinyint(1) NOT NULL DEFAULT '1', `itemBeingSold` tinyint(1) NOT NULL DEFAULT '1', `itemSellPrice` double DEFAULT NULL, PRIMARY KEY (`itemId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; CREATE TABLE IF NOT EXISTS `products` ( `productId` int(11) NOT NULL AUTO_INCREMENT, `productName` varchar(200) NOT NULL, `productTimeAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `productDescription` text, `productBrand` int(11) NOT NULL, `productFirstAddedFrom` int(11) NOT NULL, `productAvatar` varchar(100) DEFAULT NULL, PRIMARY KEY (`productId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ; INSERT INTO `products` (`productId`, `productName`, `productTimeAdded`, `productDescription`, `productBrand`, `productFirstAddedFrom`, `productAvatar`) VALUES (3, 'Samsung Galaxy SII', '2013-10-26 07:46:08', 'The Samsung Galaxy S II is a .....', 1, 1, NULL), (4, 'iPhone 5', '2013-10-26 07:46:08', 'The iPhone 5 is a ....', 1, 2, NULL); ALTER TABLE items ADD FOREIGN KEY(generalProductId) REFERENCES products(productId) ON DELETE CASCADE ON UPDATE CASCADE; ALTER TABLE products ADD FOREIGN KEY(productBrand) REFERENCES brands(brandId) ON DELETE CASCADE ON UPDATE CASCADE; ALTER TABLE items ADD FOREIGN KEY(itemBrand) REFERENCES product(productBrand) ON DELETE CASCADE ON UPDATE CASCADE; </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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