SQL dump to table like in phpmyadmin - php

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

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.

MySQL rejects column with ON DELETE SET NULL but not ON DELETE CASCADE

I have a simple MySQL database with some foreign keys on certain table columns. Some of the foreign key columns are set to ON DELETE SET NULL, and it seems to work fine. However, for one of the tables, I can't configure ON DELETE SET NULL - I can only configure ON DELETE CASCADE.
The initial tables look like this:
CREATE TABLE sessions(
session_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(50) NOT NULL,
UNIQUE KEY uk_sessions(name))
COLLATE utf8_unicode_ci;
CREATE TABLE blocks(
block_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(50) NOT NULL,
UNIQUE KEY uk_blocks(name))
COLLATE utf8_unicode_ci;
CREATE TABLE edot(
edah_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(50) NOT NULL,
UNIQUE KEY uk_edot(name))
COLLATE utf8_unicode_ci;
CREATE TABLE campers(
camper_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
edah_id int,
FOREIGN KEY fk_edah_id(edah_id) REFERENCES edot(edah_id)
ON DELETE SET NULL
ON UPDATE CASCADE,
session_id int,
FOREIGN KEY fk_session_id(session_id) REFERENCES sessions(session_id)
ON DELETE SET NULL
ON UPDATE CASCADE,
first varchar(50) NOT NULL,
last varchar(50) NOT NULL,
email varchar(50) NOT NULL,
needs_first_choice bool DEFAULT 0,
active bool NOT NULL DEFAULT 1)
COLLATE utf8_unicode_ci;
The above all works fine. The problem comes when I try to do this:
CREATE TABLE block_instances(
block_id int NOT NULL,
FOREIGN KEY fk_block_id(block_id) REFERENCES blocks(block_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
session_id int,
FOREIGN KEY fk_session_id(session_id) REFERENCES sessions(session_id)
ON DELETE SET NULL
ON UPDATE CASCADE,
PRIMARY KEY pk_block_instances(block_id, session_id))
COLLATE utf8_unicode_ci;
This returns
ERROR 1005 (HY000): Can't create table 'chugbot_db.block_instances' (errno: 150)
The error goes away if I change the second ON DELETE SET NULL to ON DELETE CASCADE:
CREATE TABLE block_instances(
block_id int NOT NULL,
FOREIGN KEY fk_block_id(block_id) REFERENCES blocks(block_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
session_id int,
FOREIGN KEY fk_session_id(session_id) REFERENCES sessions(session_id)
ON DELETE **CASCADE**
ON UPDATE CASCADE,
PRIMARY KEY pk_block_instances(block_id, session_id))
COLLATE utf8_unicode_ci;
works fine.
Can anyone tell me what is wrong with using ON DELETE SET NULL in the block_instances table here? It seems exactly the same as the ON DELETE SET NULL in the campers table.
This turned out to be a bug in MySQL:
http://bugs.mysql.com/bug.php?id=80052

Foreign Key constraints in MySQL

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.

SQL Syntax Error (phpMyAdmin)

What's wrong with this SQL statement:
ALTER TABLE `tbl_issue`
ADD CONSTRAINT `FK_issue_requester`
FOREIGN KEY (`requester_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
I have tables called tbl_issue and tbl_user within a database called trackstar_dev.
phpMyAdmin said:
#1005 - Can't create table 'trackstar_dev.#sql-1a4_9d' (errno: 121) (Details...)
The most common reason for this error is that the foreign key constraint have the same name as in another table. Foreign keys' names must be unique in the database (not just on table level). Do you have requester_id in another table in your database?
you will get this message if you're trying to add a constraint with a name that's already used somewhere else, c you will get this message if you're trying to add a constraint with a name that's already used somewhere else . change it and it will be ok :)
I had this same problem. Drop all the tables in your database and then use the SQL code below to recreate them. I assume you are following the example in the agile web development with Yii 1.1 and php 5
- Disable foreign keys
SET FOREIGN_KEY_CHECKS = 0 ;# MySQL returned an empty result set (i.e. zero rows).
-- Create tables section -------------------------------------------------
-- Table tbl_project
CREATE TABLE IF NOT EXISTS `tbl_project` (
`id` INTEGER NOT NULL auto_increment,
`name` varchar(128) NOT NULL,
`description` text NOT NULL,
`create_time` DATETIME default NULL,
`create_user_id` INTEGER default NULL,
`update_time` DATETIME default NULL,
`update_user_id` INTEGER default NULL,
PRIMARY KEY (`id`)
) ENGINE = InnoDB
;# MySQL returned an empty result set (i.e. zero rows).
-- DROP TABLE IF EXISTS `tbl_issue` ;
CREATE TABLE IF NOT EXISTS `tbl_issue`
(
`id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar(256) NOT NULL,
`description` varchar(2000),
`project_id` INTEGER,
`type_id` INTEGER,
`status_id` INTEGER,
`owner_id` INTEGER,
`requester_id` INTEGER,
`create_time` DATETIME,
`create_user_id` INTEGER,
`update_time` DATETIME,
`update_user_id` INTEGER
) ENGINE = InnoDB
;# MySQL returned an empty result set (i.e. zero rows).
-- DROP TABLE IF EXISTS `tbl_user` ;
-- Table User
CREATE TABLE IF NOT EXISTS `tbl_user`
(
`id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
`email` Varchar(256) NOT NULL,
`username` Varchar(256),
`password` Varchar(256),
`last_login_time` Datetime,
`create_time` DATETIME,
`create_user_id` INTEGER,
`update_time` DATETIME,
`update_user_id` INTEGER
) ENGINE = InnoDB
;# MySQL returned an empty result set (i.e. zero rows).
-- DROP TABLE IF EXISTS `tbl_project_user_assignment` ;
-- Table User
CREATE TABLE IF NOT EXISTS `tbl_project_user_assignment`
(
`project_id` Int(11) NOT NULL,
`user_id` Int(11) NOT NULL,
`create_time` DATETIME,
`create_user_id` INTEGER,
`update_time` DATETIME,
`update_user_id` INTEGER,
PRIMARY KEY (`project_id`,`user_id`)
) ENGINE = InnoDB
;# MySQL returned an empty result set (i.e. zero rows).
-- The Relationships
ALTER TABLE `tbl_issue` ADD CONSTRAINT `FK_issue_project` FOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an empty result set (i.e. zero rows).
ALTER TABLE `tbl_issue` ADD CONSTRAINT `FK_issue_owner` FOREIGN KEY (`owner_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an empty result set (i.e. zero rows).
ALTER TABLE `tbl_issue` ADD CONSTRAINT `FK_issue_requester` FOREIGN KEY (`requester_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an empty result set (i.e. zero rows).
ALTER TABLE `tbl_project_user_assignment` ADD CONSTRAINT `FK_project_user` FOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an empty result set (i.e. zero rows).
ALTER TABLE `tbl_project_user_assignment` ADD CONSTRAINT `FK_user_project` FOREIGN KEY (`user_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an empty result set (i.e. zero rows).
-- Insert some seed data so we can just begin using the database
INSERT INTO `tbl_user`
(`email`, `username`, `password`)
VALUES
('test1#notanaddress.com','Test_User_One', MD5('test1')),
('test2#notanaddress.com','Test_User_Two', MD5('test2'))
;# 2 rows affected.
-- Enable foreign keys
SET FOREIGN_KEY_CHECKS = 1 ;# MySQL returned an empty result set (i.e. zero rows).

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.

Categories