I created a database, linked it to project and even inserted some values in them. nothing seemed wrong. but after I exported the file. and later decided to import it into wamp, I get this error message:
Error
SQL query:
--
-- Database: `dimensioncon`
--
-- --------------------------------------------------------
--
-- Table structure for table `admission_form`
--
DROP TABLE IF EXISTS `admission_form`
MySQL said: Documentation
#1046 - No database selected
please what seems to be the problem? and how do I import the database back please.
use dimensioncon; --your database name
Then use your SQL query
SQL query:
DROP TABLE IF EXISTS `admission_form`
--
-- Table structure for table `admission_form`
--
first use 'Database Name';
use dimensioncon;
SQL query:
DROP TABLE IF EXISTS `admission_form`;
and now imported your sql file.
You just have to use USE dimensioncon; in the starting of your exported file
Something like
USE Your_Database_Name;
CREATE TABLE IF NOT EXISTS `admission_form` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10001 ;
--
-- Dumping data for table `admission_form`
--
INSERT INTO `admission_form` (`user_id`, `username`) VALUES (1, 'rogers63');
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 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.
I have a table and corresponding view.
CREATE TABLE IF NOT EXISTS `test` (
`id` mediumint(8) unsigned NOT NULL,
`test` tinyint(3) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `test` (`id`, `test`) VALUES (DEFAULT, 1);
INSERT INTO `test` (`id`, `test`) VALUES (DEFAULT, 2);
CREATE OR REPLACE
ALGORITHM=UNDEFINED SQL SECURITY INVOKER
VIEW `test_view` AS
SELECT `id`, `test` from `test`;
I can change table structure, for example:
ALTER TABLE `test` CHANGE `test` `test_renamed` TINYINT(3) UNSIGNED NOT NULL;
And all next queries to view will be broken:
SELECT * FROM test_view;
#1356 - View 'test_view' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
How to prevent table columns changing if view used them?
UPDATE: I have Percona Server 5.6.32-78.0
What you cannot do is tell your database system to refuse any changes to columns or tables used in views.
You can put a test in your code for the use of a table or even column in any view by writing a query on the information_schema.views table. However, it is up to you and your users to perform the test, mysql will not enforce it.
For this reason I usually use a script containing all views to recreate them after I changed the database structure. Of course sometimes one of these queries will fail after a change, but then those are the exact views you need to change.
You can then rename the new column name to the old column name or whatever you want to do to keep your old views usable.
I'm supposed to write a trigger that checks BEFORE INSERT whether the record already exists, and then take some actions. I'm stuck with this error returned on issuing an INSERT statement against Koncerty view:
Error Code: 1347. 'BazyLista3.Koncerty' is not BASE TABLE
Here's the code for view Koncerty:
CREATE VIEW Koncerty (`nazwa_klubu`, `adres_klubu`, `nazwa_zespolu`,
`ilosc_czlonkow_zespolu`, `data_wystepu`) AS
( SELECT `nazwa_klubu`, `kb`.`adres`, `nazwa_zespolu`, `zs`.`ilosc_czlonkow`,
`data_wystepu` FROM `Koncert` AS kc
INNER JOIN `Klub` AS kb ON `kc`.`nazwa_klubu` = `kb`.`nazwa`
INNER JOIN `Zespol` AS zs ON `kc`.`nazwa_zespolu` = `zs`.`nazwa` );
And my trigger, where I have this error:
DROP TRIGGER IF EXISTS `before_koncerty_insert`
DELIMITER $$
CREATE TRIGGER `before_koncerty_insert` BEFORE INSERT ON `Koncerty`
FOR EACH ROW
BEGIN
DECLARE i INT DEFAULT 0;
SELECT COUNT(*) INTO i FROM `Koncerty` WHERE
`nazwa_klubu` = NEW.`nazwa_klubu` AND
`adres_klubu` = NEW.`adres_klubu` AND
`nazwa_zespolu` = NEW.`nazwa_zespolu` AND
`ilosc_czlonkow_zespolu` = NEW.`ilosc_czlonkow_zespolu` AND
`data_wystepu` = NEW.`data_wystepu`;
IF i > 0 THEN
SIGNAL SQLSTATE '58005'
SET MESSAGE_TEXT = 'Blad! Taka krotka juz istnieje';
END IF;
END $$
DELIMITER ;
Tables
CREATE TABLE IF NOT EXISTS `Klub`
(
`nazwa` varchar(50) NOT NULL,
`adres` varchar(70) NOT NULL,
PRIMARY KEY (`nazwa`)
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `Zespol`
(
`nazwa` varchar(50) NOT NULL,
`ilosc_czlonkow` int(3) NOT NULL,
PRIMARY KEY (`nazwa`)
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `Koncert`
(
`nazwa_klubu` varchar(50) NOT NULL,
`nazwa_zespolu` varchar(50) NOT NULL,
`data_wystepu` datetime NOT NULL,
FOREIGN KEY (`nazwa_klubu`) REFERENCES `Klub`(`nazwa`),
FOREIGN KEY (`nazwa_zespolu`) REFERENCES `Zespol`(`nazwa`)
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
How do I get around this issue?
MySQL does not support triggers on views.
The way to do this task is to make Koncerty a table without keys, in order to then operate on it with trigger actions. It all worked out just as I wanted.
Also, making UNIQUE CONSTRAINTS on records in tables does the job for the unique record, because it pops up an error when trying to add anything alike.
MySQL does not support triggers on views, from MySQL reference manual:
You cannot associate a trigger with a TEMPORARY table or a view.
The error might come if the view has used the database name in it. For example if a view has used a table as <dbname>.<tablename>:
create or replace view dealer_current_stage as
select
dsc.dealer_id,
dsc.new_stage current_stage,
dsc.change_date last_stage_change_date
from
dealer_current_stage_pre dcsp
left join
risk.dealer_stage_changes dsc ON dsc.change_date = dcsp.max_stage_change_date
and dsc.dealer_id = dcsp.dealer_id
group by dsc.dealer_id;
Here, risk is a different database and its table is being used. So better do not use the separate database and table, if you have to use it then consider the privileges, etc.
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`;