Foreign Key constraints in MySQL - php

I am a newbie to PHP. I have been given the code snippet below as homework:
CREATE TABLE `admin_log` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`statusdate` DATETIME DEFAULT NULL,
`type` INT(11) DEFAULT NULL
PRIMARY KEY (`id`)
) ENGINE=MYISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin
Why will it not be possible to set up any foreign key constraints using this table?
I have done some research on Google and I cant find a reason why foreign key constraints are not possible. Please help

You can do that like this :
CREATE TABLE `admin_log` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`statusdate` DATETIME DEFAULT NULL,
`type` INT(11) DEFAULT NULL,
INDEX (type),
FOREIGN KEY (type)
REFERENCES type(id)
ON UPDATE CASCADE ON DELETE RESTRICT,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=latin
Notice the engine INNODB insetad of MYISAM which doesn't permit foreign key.
Or using MySQLAdmin in the "structure" tab, click on the "relationals view" link below the table description.

Even it has been mentioned on comment before -- just to make it more prominent: MyISAM is not supporting foreign keys. So you will need to change the engine of your table to e.g. INNODB if possible.

Related

SQLSTATE[HY000]: General error: 1025 Error on rename error when dropping foreign key in Doctrine Migration

I am seeing the error below when I run the following query inside a Doctrine migration:
ALTER TABLE crmpicco_course_version DROP FOREIGN KEY FK_C060B146DE13F470
Migration 20151209153121 failed during Execution.
Error An exception occurred while executing
'ALTER TABLE crmpicco_course_version DROP FOREIGN KEY FK_C060B146DE13F470':
SQLSTATE[HY000]: General error:
1025 Error on rename of './crmpicco_dev/crmpicco_course_version'
to './crmpicco_dev/#sql2-77c-b0a' (errno: 152)
This is the table I am trying to change:
CREATE TABLE `crmpicco_course_version` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`course_id` int(11) NOT NULL,
`updated_by_id` int(11) DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`start_date` datetime DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `IDX_C060B146896DBBDE` (`updated_by_id`),
KEY `IDX_C060B146DE13F470` (`course_id`),
CONSTRAINT `FK_C060B146896DBBDE` FOREIGN KEY (`updated_by_id`) REFERENCES `crmpicco_user` (`id`),
CONSTRAINT `FK_C060B146DE13F470` FOREIGN KEY (`course_id`) REFERENCES `crmpicco_course` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
What is preventing me from dropping this foreign key successfully?
When I run SHOW ENGINE INNODB STATUS I get the following:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
151209 16:25:42 Error IN dropping of a FOREIGN KEY CONSTRAINT of TABLE "crmpicco_dev"."crmpicco_course_version",
IN SQL command
ALTER TABLE crmpicco_course_version DROP FOREIGN KEY FK_C060B146DE13F470
Cannot find a CONSTRAINT WITH the given id "FK_C060B146DE13F470".
After endless dropping and recreating of my local database I found that this was caused by Doctrine creating the same ALTER statement more than once and also in the wrong order.
I had to change the statements around manually to make sure I migrated data from my old table to my new table before creating the new foreign key constraint on the new table. Without this change I was getting the error above and others.

SQL dump to table like in phpmyadmin

I am developing a backup/restore app in yii. I need a mysql command to dump a table like phpmyadmin does, it does not wrap the foreign keys inside table definition, but put is as "ALTER TABLE" command.
--
-- Table structure for table `sales_detail`
--
CREATE TABLE IF NOT EXISTS `sales_detail` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`salesFk` int(11) NOT NULL,
`productFk` int(11) NOT NULL,
`quantity` decimal(15,3) NOT NULL,
`unitPrice` decimal(15,3) NOT NULL,
`comUnitPrice` decimal(15,3) NOT NULL DEFAULT '0.000',
`minUnitPrice` decimal(15,3) NOT NULL DEFAULT '0.000',
PRIMARY KEY (`id`),
KEY `productFk` (`productFk`),
KEY `salesFk` (`salesFk`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=378 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `sales_detail`
--
ALTER TABLE `sales_detail`
ADD CONSTRAINT `sales_detail_ibfk_1` FOREIGN KEY (`productFk`) REFERENCES `product` (`id`) ON UPDATE CASCADE,
ADD CONSTRAINT `sales_detail_ibfk_2` FOREIGN KEY (`salesFk`) REFERENCES `sales` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Thanks in advance

Create table mysql

I'am new to mysql and i want to know what KEY (not primary key) means in the query below :
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`username` varchar(150) NOT NULL default '',
`email` varchar(100) NOT NULL default '',
`password` varchar(100) NOT NULL default '',
`activation` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `idx_name` (`name`),
KEY `username` (`username`),
KEY `email` (`email`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=65 ;
also this line ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=65 ;
A KEY is an index: it's just like a library index: quicker finding for the values of that colum. You want this for joining and searching.
ENGINE=MyISAM
Means the engine you use (this is the default). If you need foreign keys for example, then you might want InnoDB.
DEFAULT CHARSET=utf8
Is the default character set.
AUTO_INCREMENT=65 ;
Means that currently the auto-increment value is at 65.
KEY is a synonym for INDEX. See database index if you're not familiar with them.
KEY is normally a synonym for INDEX.
ENGINE=MyISAM
It means the storage engine for your table. There are two types of storage engines in MySQL: transactional and non-transactional. The default engine is InnoDB as of MySQL 5.5.5 ( MyISAM before 5.5.5).
DEFAULT CHARSET=utf8
Character set support that enables you to store data using a variety of character sets.
AUTO_INCREMENT=65
It means currently the value of auto_increment is 65 for primary key id.
More information:-
https://dev.mysql.com/doc/refman/5.7/en/create-table.html

ON DELETE CASCADE not working in MySQL

I am using the following SQL to create a table named app_info:
CREATE TABLE IF NOT EXISTS `app_info` (
`_id` int(11) NOT NULL AUTO_INCREMENT,
`app_name` varchar(50) DEFAULT NULL,
`app_owner` varchar(50) DEFAULT NULL,
`last_update` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
I am using the following SQL to create a table named tab_info:
CREATE TABLE `myDB`.`tab_info` (
`_id` INT NOT NULL AUTO_INCREMENT ,
`app_id` INT NOT NULL ,
`tab_title` VARCHAR(15) NOT NULL ,
PRIMARY KEY (`_id`) ,
UNIQUE INDEX `app_id_UNIQUE` (`app_id` ASC) ,
INDEX `app_tab_key` (`app_id` ASC) ,
CONSTRAINT `app_tab_key`
FOREIGN KEY (`app_id` )
REFERENCES `myDB`.`app_info` (`_id` )
ON DELETE CASCADE
ON UPDATE CASCADE);
But when I delete data from primary key table, the orphaned rows in the foreign key table are not being deleted automatically. Does anyone know what the problem could be?
The MyISAM storage engine doesn't support foreign key constraints. The constraint is parsed but silently ignored.
To fix your problem use the InnoDB engine instead (for both tables).
CREATE TABLE ( ... ) ENGINE = InnoDB ... ;
Instead of dropping your tables and recreating them you can also change the storage engine:
ALTER TABLE myDB.app_info ENGINE = InnoDB;
ALTER TABLE myDB.tab_info ENGINE = InnoDB;
After changing the engine you will need to add the foreign key constraint again.

delete main row and all children mysql and php

I have inherited a PHP project and the client is wanting to add some functionality to their CMS, basically the CMS allows them to create some news, all the news starts with the same content, and that is saved in one table, the actually news headline and articles are saved in another table, and the images for the news are saved in another, basically if the base row for the news is deleted I need all the related rows to be deleted, the database is not setup to work with foreign keys so I cannot use cascade deletion, so how can I delete the all the content I need to, when I only what the ID of the base news row is?
Any help would be very helpful I am sorry I cannot give you much more help, here is this the original SQL of tables scheme if that helps?
--
-- Table structure for table `mailers`
--
CREATE TABLE IF NOT EXISTS `mailers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`mailer_title` varchar(150) NOT NULL,
`mailer_header` varchar(60) NOT NULL,
`mailer_type` enum('single','multi') NOT NULL,
`introduction` varchar(80) NOT NULL,
`status` enum('live','dead','draft') NOT NULL,
`flag` enum('sent','unsent') NOT NULL,
`date_mailer_created` int(11) NOT NULL,
`date_mailer_updated` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
-- --------------------------------------------------------
--
-- Table structure for table `mailer_content`
--
CREATE TABLE IF NOT EXISTS `mailer_content` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`headline` varchar(60) NOT NULL,
`content` text NOT NULL,
`mailer_id` int(11) NOT NULL,
`position` enum('left','right','centre') DEFAULT NULL,
`created_at` int(10) NOT NULL,
`updated_at` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;
-- --------------------------------------------------------
--
-- Table structure for table `mailer_images`
--
CREATE TABLE IF NOT EXISTS `mailer_images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(150) NOT NULL,
`filename` varchar(150) NOT NULL,
`mailer_id` int(11) NOT NULL,
`content_id` int(11) DEFAULT NULL,
`date_created` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
It is worth noting that the schema cannot be changed nor can I change to the DB to MYISAM so that I can use foreign keys.
Add foreign key to table mailer_content
FOREIGN KEY (mailer_id)
REFERENCES mailers(id)
ON DELETE CASCADE
Add foreign key to table mailer_images
FOREIGN KEY (content_id)
REFERENCES mailer_content(id)
ON DELETE CASCADE
http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
It is worth noting that the schema cannot be changed nor can I change to the DB to MYISAM so that I can use foreign keys.
Why can't the schema be changed? You designed the app, didn't you? Even if you didn't, adding the proper keys is just a matter of adding the right indexes and then altering the right columns. #Michael Pakhantosv's answer has what looks to be the right bits of SQL.
Further, it's InnoDB that does foreign keys, not MyISAM. You're fine there already.
If you could change the schema, making the appropriate IDs actual, real Foreign Keys and using ON DELETE CASCADE would work. Or maybe triggers. But that's just asking for it.
Now, for some reason, ON DELETE CASCADE isn't liked very much around here. I disagree with other people's reasons for not liking it, but I don't disagree with their sentiment. Unless your application was designed to grok ON DELETE CASCADE, you're in for a world of trouble.
But, given your requirement...
basically if the base row for the news is deleted I need all the related rows to be deleted
... that's asking for ON DELETE CASCADE.
So, this might come as a shock, but if you can't modify the database, you'll just have to do your work in the code. I'd imagine that deleting a news article happens in only one place in your code, right? If not, it'd better. Fix that first. Then just make sure you delete all the proper rows in an appropriate order. And then document it!
If you can not change the schema then triggers are not an option.
InnoDB supports transactions, so deleting from two tables should not be an issue, what exactly is your problem?
P.S. It would be worth noting which version of the server are you using.

Categories