I can get query for creating a specific table via:
SHOW CREATE TABLE `table_name`
If I do this for all of the tables, I can recreate the whole database by running the gathered sql queries. Is there an easier way to get the table creation script, i.e. sql export file, which created the database just by using CREATE statements?
I would solve this with mysqldump. It's a command-line tool that comes with MySQL. Here's the documentation: https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html
You run it at a command prompt, not in phpMyAdmin. You must have access to a shell environment that is either the same host where you run MySQL Server, or at least can reach that host via the network.
For example:
mysqldump --no-data mydatabase mytable
Output is produced:
--
-- Table structure for table `mytable`
--
DROP TABLE IF EXISTS `mytable`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `mytable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` int(11) DEFAULT NULL,
`c2` int(11) DEFAULT NULL,
`c3` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
(There's more output that I'm omitting.)
You can dump the structure of many tables at once this way, and also views, triggers, procedures, and so on.
If you don't use --no-data, it also includes the data in each table you dump. This makes it an effective tool for data backups. By default, it dumps data in the form of a lot of INSERT statements. So just like the CREATE TABLE statements can recreate the structure of your database if you run them against another MySQL instance, the INSERT statements can recreate your data.
Related
I have a project sent to me by a friend and i am having serious issues importing the database into my WAMP Server. I end up getting Mysql error
Error
SQL query:
--
-- Database: `drivers_endorsements`
--
-- --------------------------------------------------------
--
-- Table structure for table `admin`
--
CREATE TABLE IF NOT EXISTS `admin` (
`admin_id` int(11) NOT NULL,
`username` varchar(30) NOT NULL,
`password` varchar(12) NOT NULL,
`name` varchar(40) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3
MySQL said: Documentation
#1046 - No database selected
Firstly you have to create database manually or select the existing one in phpmyadmin (as mentioned WAMP server is used ) and then import the .sql file in it, and the database name should be same as that of used in application else will not work with desired application to which the database is linked.
It's not really a big deal! The message itself is self-explanatory. You need to select an existing database first & then try your import.
Or you could possibly add the following at the very top of your DB script that you trying to import -
CREATE DATABASE IF NOT EXISTS drivers_endorsements;
USE drivers_endorsements;
I have a db in MariaDB 10.1.25 and in this, I have many tables and 20 views.
When I try to backup my db using mysqldump, it works fine for tables but in view definitions, it fails to create a create statement like it does with tables.
The code generated is this:
--
-- Temporary table structure for view `qry_clientes`
--
DROP TABLE IF EXISTS `qry_clientes`;
/*!50001 DROP VIEW IF EXISTS `qry_clientes`*/;
SET #saved_cs_client = ##character_set_client;
SET character_set_client = utf8;
/*!50001 CREATE TABLE `qry_clientes` (
`Id` tinyint NOT NULL,
`Cliente` tinyint NOT NULL,
`Direccion` tinyint NOT NULL,
`Ciudad` tinyint NOT NULL,
`Fono` tinyint NOT NULL,
`Fax` tinyint NOT NULL,
`Email` tinyint NOT NULL,
`Ruc` tinyint NOT NULL,
`tipo` tinyint NOT NULL
) ENGINE=MyISAM */;
SET character_set_client = #saved_cs_client;
and in this there are no view definitions. I have all the privilegies grandted
Usually, in the mysqldump backup script, the views are first created as tables and then are then dropped at the bottom of the script as each view is being created.
Sometimes there is an error in this process because when a view is created there is a user used as DEFINER. This statement may fail because this user might not exist in the database.
Please verify that the view drop/create script exists at the end, write the error that you are getting (if you are getting) and run the import using the -v option for more logging.
Trying to import a MySQL database using PHP-Admin. When I try to import the file backed-up by my host, I get this error message:
SQL query:
DROP TABLE IF EXISTS `wp_commentmeta`
MySQL said: Documentation
#1046 - No database selected
I have also tried to export the database myself, then trying to import that specific file. When I do that, however, I get a completely different error message:
SQL query:
CREATE TABLE IF NOT EXISTS `wp_commentmeta` (
`meta_id` bigint(20) unsigned NOT NULL,
`comment_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`meta_value` longtext COLLATE utf8mb4_unicode_ci
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
MySQL said: Documentation
#1046 - No database selected
What can I do to resolve this?
You need to USE a database. You could edit your file and add the line at the top, something like:
USE databasename;
Some tools will let you set a default database when restoring from backup. If you haven't selected one that could be a problem.
If you haven't created a database you'll need to do that first.
You need to select which database you are importing to in your SQL. Your first statement should be:
USE DATABASE `db_name`;
You have no database selected.
Find which databases exist
SHOW DATABASES:
and then select one for use
USE `database_name`
You say that you are importing into a new host, so I would assume that you do not already have a database created;
CREATE DATABASE `database_name`
Now you can USE that database you just created; and your queries should now work.
Background Information
While doing a backup / restore with mysql, I noticed that the restored database was missing a bunch of tables.
Code:
To create the dump file, this is the command that is being used:
mysqldump --databases widgetdb --master-data -u username -ptest --add-drop-database --extended-insert > /var/test/dump.db
Then we are trying to restore the database onto a different server using the following command:
mysql --user=username--password=test widgetdb < /var/test/dbdump.db
So I've checked the dump file and I searched for a table that's missing in the restored database. This is what the code looks like:
DROP TABLE IF EXISTS `widget1`;
/*!50001 DROP VIEW IF EXISTS `widget1`*/;
SET #saved_cs_client = ##character_set_client;
SET character_set_client = utf8;
/*!50001 CREATE TABLE `widget1` (
`id` tinyint NOT NULL,
`name` tinyint NOT NULL,
`label` tinyint NOT NULL,
`objtype_id` tinyint NOT NULL,
`asset_no` tinyint NOT NULL,
`has_problems` tinyint NOT NULL,
`comment` tinyint NOT NULL
) ENGINE=MyISAM */;
SET character_set_client = #saved_cs_client;
I found the following post: MySQL dump file and commented out lines
This seems to indicate then that the commented out lines will still be run. In my case, I'm running mysql version 5.5.35 which is higher than 50001...
But the table isn't created for me when I restore the dump file.
What I've Tried So Far:
I've tried to change the command use to create the dump file so that I use the --opt option in an attempt to use the "defaults". The restored database is still missing tables.
I'm trying to import a SQL dump to another server. It fails on the first line. I'm first creating the exp_actions table and then inserting a bunch of data into it, but I get this really weird error.
SQL query:
--
-- Database: `ee_cmssite`
--
-- --------------------------------------------------------
--
-- Table structure for table `exp_actions`
--
CREATE TABLE `exp_actions` (
`action_id` INT( 4 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`class` VARCHAR( 50 ) NOT NULL DEFAULT '',
`method` VARCHAR( 50 ) NOT NULL DEFAULT '',
PRIMARY KEY ( `action_id` )
) ENGINE = MYISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT =21;
MySQL said:
#1146 - Table 'site_ee.exp_actions' doesn't exist
Why doesn't it exist? I just instructed it to be created. I'm completely baffled. I've tried with and without IF_NOT_EXISTS
If anyone else comes across this seemingly bizarre error - see https://stackoverflow.com/a/11696069 for the solution.
I had the same symptoms and the cause was the same - moving to a new machine I took the old short-cut of simply copying the databases from the mysql/data directory that I needed directly into the new machine, however some were newer InnoDb types. This causes the Create Table throws table doesn't exist error. I had to drop the database and recreate it, then import from an sql dump.
According to the SQL script, the table exists in another database:
--
-- Database: ee_cmssite
-- --------------------------------------------------------
-- Table structure for table exp_actions
try to use ee_cmssite database instead.
While the error is not clear, I think this is related to the missing USE at the beginning of the file. mysqldump doesn't add a USE statement when you dump a single db. So you should add:
USE `ee_cmssite`;