I have following block of code in MySql:
DROP TABLE IF EXISTS `account.info`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `account.info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`account_id` int(11) NOT NULL,
`year_id` int(11) NOT NULL,
`school_id` int(11) NOT NULL,
PRIMARY KEY (`id`,`account_id`,`year_id`,`school_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7177 DEFAULT CHARSET=utf8;
Its giving me error on first line as:
ERROR 1103 (42000) at line 56: Incorrect table name 'account.info'
What is wrong in it?
Please help me.
From http://dev.mysql.com/doc/refman/5.1/en/identifiers.html:
"Before MySQL 5.1.6, database and table names cannot contain “/”, “\”, “.”, or characters that are not permitted in file names."
Which is why I gave this answer:
You can't use dots in table names. Dots are used to separate database names and table names (and column names). You could try using `account`.`info` if your database name is account and the table name is info. If the table name is supposed to be account.info, you should change that to something else, like account_info. I don't agree with some of the other answers: Quoting never hurts, if done properly.
Since 5.1.6 you can use whatever you please, as shown by #eggyal and others.
As documented under Schema Object Names:
Before MySQL 5.1.6, database and table names cannot contain “/”, “\”, “.”, or characters that are not permitted in file names.
Incidentally, had you wanted to create a table called info within a database called account, then note that as documented under Identifier Qualifiers:
If any components of a multiple-part name require quoting, quote them individually rather than quoting the name as a whole. For example, write `my-table`.`my-column`, not `my-table.my-column`.
try this:
DROP TABLE IF EXISTS account.info;
dont use ` when using dots.
Or quote both db name and table name
DROP TABLE IF EXISTS `account`.`info`;
You seem to want to create and first drop a table in the database account with the name info. If so, do it like this:
DROP TABLE IF EXISTS `account`.`info`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `account`.`info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`account_id` int(11) NOT NULL,
`year_id` int(11) NOT NULL,
`school_id` int(11) NOT NULL,
PRIMARY KEY (`id`,`account_id`,`year_id`,`school_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7177 DEFAULT CHARSET=utf8;
You're specifying a table called account.info and not a table called info in the account db. Quote each part separately:
DROP TABLE IF EXISTS `account`.`info`;
If you are trying to make a table called account.info then older versions of MySQL wont allow a . in a table name.
Related
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 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.
I have three column(ID,Receipt_no,Name) in my table.ID is primary key auto increment so ID will start from 1, Same think i have to set on receipt_no so it will also start from 1.Is it possible?
I am using phpmyadmin.
Thanks in advance.
-- Table structure for table `temp`
--
CREATE TABLE IF NOT EXISTS `temp` (
`Id` int(11) NOT NULL,
`Receipt_no` int(11) NOT NULL,
`Name` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `temp`
ADD PRIMARY KEY (`Id`), ADD UNIQUE KEY `Receipt_no` (`Receipt_no`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `temp`
--
ALTER TABLE `temp`
MODIFY `Id` int(11) NOT NULL AUTO_INCREMENT;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
Create a db for safe testing:
create schema Hybreeder;
use Hybreeder;
Schema:
CREATE TABLE `temp` (
`Id` int(11) AUTO_INCREMENT PRIMARY KEY,
`Receipt_no` int(11) NOT NULL,
`Name` varchar(20) NOT NULL,
UNIQUE KEY `unq_Receipt_no` (`Receipt_no`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- The following table would be useful system wide for all
-- your special incrementor needs whatever they may be
-- Especially great for those looking for Prefixes to
-- add to PK's like an id such as ABC00001
create table sequences
( id int auto_increment primary key,
sectionType varchar(200) not null,
nextSequence int not null,
unique key(sectionType)
) ENGINE=InnoDB;
-- Prime it with some "sections" (we had to call them something)
insert sequences (sectionType,nextSequence) values
('Chassis',1),('Engine Block',1),('Carburetor',1),('Receipt',1001);
Stored Proc:
DROP PROCEDURE if exists getNextSequence;
DELIMITER $$ -- Note: delete this line for PHPMyAdmin (you don't need it)
CREATE PROCEDURE getNextSequence(p_sectionType varchar(200))
BEGIN
-- pass in as a parameter the "section" for next inc, such as "Chassis"
START TRANSACTION;
SELECT nextSequence into #mine_to_use from sequences where sectionType=p_sectionType FOR UPDATE;
UPDATE sequences set nextSequence=nextSequence+1 where sectionType=p_sectionType;
COMMIT; -- get and release INTENTION LOCK ASAP
SELECT #mine_to_use as yourSeqNum; -- return as a 1 column, 1 row resultset
END;
$$ -- Note: delete this line for PHPMyAdmin (you don't need it)
DELIMITER ; -- Note: delete this line for PHPMyAdmin (you don't need it)
Your client program will call the stored proc and process the result set getting the next num to use such as:
call getNextSequence("Receipt");
+------------+
| yourSeqNum |
+------------+
| 1001 |
+------------+
call it again for the heck of it:
call getNextSequence("Receipt");
+------------+
| yourSeqNum |
+------------+
| 1002 |
+------------+
It now has a 1 row 1 column result set with the column name yourSeqNum.
Let's pseudocode call that NNNNN as a variable.
INSERT temp(`Receipt_no`,`Name`) VALUES (NNNNN,'Fred'); -- again this is pseudocode
Id is the AUTO_INCREMENT column so we skip it in the column list above. It gets dealt with automatically by MySQL.
Why is it pseudocode? Because there is no talk here about what your front-end language is such as PHP, Python, Java, to know how you processed that result set to get the variable NNNNN. And I am not writing everything!
Your task for all that follow is to merely tweek the part above that gets that sequence number into a variable and use it in the INSERT statement.
Cleanup:
DROP SCHEMA Hybreeder;
Now there are those that would say this whole thing looks silly, for NNNNN is always 1000 greater than Id so what is the point? If you had multiple consumers of the sequence table for the receipt section, say other processes or other companies, then that would not be the case.
Please follow along the narrative over Here for more technical aspects that I glossed over above.
I have the following table structure:
DROP TABLE IF EXISTS `tblusers`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `tblusers` (
`UserID` int(5) NOT NULL AUTO_INCREMENT,
`ContactPersonID` int(5) NOT NULL,
`NameOfUser` varchar(70) NOT NULL,
`LegalForm` varchar(70) DEFAULT NULL,
`Address` varchar(70) DEFAULT NULL,
`City` varchar(50) DEFAULT NULL,
`Postal` int(8) DEFAULT NULL,
`Country` varchar(50) DEFAULT NULL,
`VatNum` int(10) DEFAULT NULL,
`Username` varchar(30) NOT NULL,
`Password` varchar(20) NOT NULL,
`Email` varchar(40) NOT NULL,
`Website` varchar(40) DEFAULT NULL,
`IsSeller` bit(1) DEFAULT NULL,
`IsBuyer` bit(1) DEFAULT NULL,
`IsAdmin` bit(1) DEFAULT NULL,
`Description` text,
PRIMARY KEY (`UserID`),
KEY `ContactPersonID` (`ContactPersonID`),
CONSTRAINT `tblusers_tblpersons` FOREIGN KEY (`ContactPersonID`) REFERENCES `tblpersons` (`PersonID`)
) ENGINE=InnoDB AUTO_INCREMENT=87 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = #saved_cs_client */;
Then once I create a user from the UI of my application, I have to manually set the very first admin, and this is the only time I am doing this directly from the DB, all the rest is envisioned to be done from the UI (granting admin privileges):
UPDATE `tblusers` SET `IsAdmin`='1' WHERE `UserID`='79';
but then I get:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
UPDATE `trace`.`tblusers` SET `IsAdmin`='1' WHERE `UserID`='79';
ERROR 1406: 1406: Data too long for column 'IsAdmin' at row 1
SQL Statement:
UPDATE `trace`.`tblusers` SET `IsAdmin`='1' WHERE `UserID`='79'
Which doesn't make sense because I am doing the exact same thing on other machines and it works like a charm. The only difference is that in this scenario I have mysql 5.7 server whereas I have 5.6 versions on the machines that this does work.
I tried the following solution but it didn't work for me. Besides that, the my.ini file is unchanged in the 5.6 machine where it does work.
Downgrading to 5.6 is out of the question. I need a real solution here please.
isadmin is a column of type bit and you are storing a value of type varchar in it which is of larger size than bit. modify query as follows:-
UPDATE `tblusers` SET `IsAdmin`=b'1' WHERE `UserID`='79';
IsAdmin has the datatype of bit(1), yet you are assigning the string '1' to it. Indicate that you are assigning a bit value to it by preceeding the '1' with b or use 0b format:
UPDATE `tblusers` SET `IsAdmin`=b'1' WHERE `UserID`='79';
or
UPDATE `tblusers` SET `IsAdmin`=0b1 WHERE `UserID`='79';
The reason for this behaviour is probably that strict_all_tables or strict_trans_tables setting is enabled on the v5.7 mysql server:
Strict mode controls how MySQL handles invalid or missing values in
data-change statements such as INSERT or UPDATE. A value can be
invalid for several reasons. For example, it might have the wrong data
type for the column, or it might be out of range. A value is missing
when a new row to be inserted does not contain a value for a non-NULL
column that has no explicit DEFAULT clause in its definition. (For a
NULL column, NULL is inserted if the value is missing.) Strict mode
also affects DDL statements such as CREATE TABLE.
The BIT data type is used to store bit values. A type of BIT(M) enables storage of M-bit values. M can range from 1 to 64.
UPDATE tblusers SET IsAdmin=b'1' WHERE UserID='012';
UPDATE tblusers SET IsAdmin=b'0' WHERE UserID='012';
I had the same problem when I synchronized the Model's table from MySQL Workbench to the MySQL server which had old tables with data. the data of old column types is longer than the new column types. (for example: the old column type is char[43] but the new column type is binary[32] so the new column type can't contain all of the old data)
my solution: drop the old table and then synchronized new Model with the old database
I've been searching for a conversion tool or a list of things to change when switching from mySQL to SQLite. However, the conversion tools are, as far as I found, converting an already existing mySQL database to a SQLite database.
I don't have the made the mySQL database yet, I only have the create statements. So is there either a list of changes to make to the create statements, or a converter that changes the mySQL create statements to SQLite create statements?
My sql looks like this:
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL';
CREATE SCHEMA IF NOT EXISTS `pyMS` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `pyMS` ;
-- -----------------------------------------------------
-- Table `pyMS`.`msrun`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `pyMS`.`msrun` (
`msrun_id` INT NOT NULL ,
`description` VARCHAR(250) NOT NULL ,
PRIMARY KEY (`msrun_id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `pyMS`.`feature`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `pyMS`.`feature` (
`feature_id` VARCHAR(40) NOT NULL ,
`intensity` DOUBLE NOT NULL ,
`overallquality` DOUBLE NOT NULL ,
`quality` DOUBLE NOT NULL ,
`charge` INT NOT NULL ,
`content` VARCHAR(45) NOT NULL ,
`msrun_msrun_id` INT NOT NULL ,
PRIMARY KEY (`feature_id`, `msrun_msrun_id`) ,
UNIQUE INDEX `id_UNIQUE` (`feature_id` ASC) ,
INDEX `fk_feature_msrun1` (`msrun_msrun_id` ASC) ,
CONSTRAINT `fk_feature_msrun1`
FOREIGN KEY (`msrun_msrun_id` )
REFERENCES `pyMS`.`msrun` (`msrun_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
[...]
I dont think you will find a list of changes like "replace A with B".
What you can do is look what these migration scripts do and come up with your own list.
For example, take the one on this link
if you get commands like sed 's/ smallint([0-9]*) / integer /g' | you can see what's being done (sed is a string replacement unix command).
Its just a matter of going through the scripts.