Table already exist sql - mysql

When trying to import database
Error
SQL query:
CREATE TABLE `wp_ihrss_plugin` (
`Ihrss_id` int(11) NOT NULL,
`Ihrss_path` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`Ihrss_link` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`Ihrss_target` varchar(50) NOT NULL,
`Ihrss_title` varchar(500) NOT NULL,
`Ihrss_order` int(11) NOT NULL,
`Ihrss_status` varchar(10) NOT NULL,
`Ihrss_type` varchar(100) NOT NULL,
`Ihrss_extra1` varchar(100) NOT NULL,
`Ihrss_extra2` varchar(100) NOT NULL,
`Ihrss_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=MyISAM DEFAULT CHARSET=utf8
MySQL said: Documentation
#1050 - Table 'wp_ihrss_plugin' already exists
and 5 more tables ; when try to drop one it show that there is another one

One possible approacj to deal with this would be to drop and recreate the object. For instance:
DROP TABLE IF EXISTS `foo`;
CREATE TABLE `foo` ( ... );

There are some duplicate tables present in your database dump.
Check the sql file from which you are trying to import

Related

How to generate the table schema from INFORMATION_SCHEMA in MySQL?

I trying to generate table structure using INFORMATION_SCHEMA in MySQL.
I need the same output as
SHOW CREATE TABLE Mytablename;
My intention is to generation create table script for list of tables in mysql.
please help. I need to take table scripts for 100 tables. like below
CREATE TABLE `customer_list` (
`ID` smallint(5) unsigned NOT NULL DEFAULT '0',
`name` varchar(91) DEFAULT NULL,
`address` varchar(50) NOT NULL,
`zip_code` varchar(10) DEFAULT NULL,
`phone` varchar(20) NOT NULL,
`city` varchar(50) NOT NULL,
`country` varchar(50) NOT NULL,
`notes` varchar(6) NOT NULL DEFAULT '',
`SID` tinyint(3) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
SHOW CREATE TABLE your_table_name;
The SQL query to get the table structure from the INFORMATION SCHEMA is as follows, where the database name is "dbname" and the table name is "Mytablename":
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dbname'
AND TABLE_NAME = 'Mytablename';

Export from localhost to remote on the same LAN

I want to export one of my database to my Raspberry. I tried through PHPMyadmin, but on import I got this errormessage:
SQL query:
--
-- Database: `leltar`
--
-- --------------------------------------------------------
--
-- Table structure for table `eventlog`
--
CREATE TABLE `eventlog` (
`ID` int(50) NOT NULL,
`event` varchar(50) COLLATE utf8_hungarian_ci NOT NULL,
`productbc` varchar(50) COLLATE utf8_hungarian_ci DEFAULT NULL,
`uname` varchar(50) COLLATE utf8_hungarian_ci NOT NULL,
`datetime` datetime(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_hungarian_ci;
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_hungarian_ci' at line 16
What's the matter?
Your datetime field has fractional seconds specified: datetime(1).
This feature is probably not supported by the target mysql version (< v5.7), hence the error message.
You either need to upgrade your target mysql version to support fractional seconds or you need to remove the fractional seconds from the export file, both from the data definition and the data itself.
CREATE TABLE `eventlog` (
`ID` int(50) NOT NULL,
`event` varchar(50) COLLATE utf8_hungarian_ci NOT NULL,
`productbc` varchar(50) COLLATE utf8_hungarian_ci DEFAULT NULL,
`uname` varchar(50) COLLATE utf8_hungarian_ci NOT NULL,
`datetime` DATETIME NOT NULL
) ;
Remove the "(1)" after the word "datetime" because it needs no length (sorry, it's not "length" it is a fractional seconds specification). The following should work:
CREATE TABLE `eventlog` (
`ID` int(50) NOT NULL,
`event` varchar(50) COLLATE utf8_hungarian_ci NOT NULL,
`productbc` varchar(50) COLLATE utf8_hungarian_ci DEFAULT NULL,
`uname` varchar(50) COLLATE utf8_hungarian_ci NOT NULL,
`datetime` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_hungarian_ci;

Mysql i get Error when i try to edit after insert data

this is my one of database table structure and after insert some fields as row into that i can't update columns and i get error:
CREATE TABLE `channels` (
`id` int(11) NOT NULL,
`channelName` varchar(30) COLLATE utf8_persian_ci NOT NULL,
`channelLink` varchar(50) COLLATE utf8_persian_ci NOT NULL,
`channelDescription` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`channelImageFileName` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`channelAvatarFileName` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`channelState` tinyint(1) NOT NULL,
`channelType` enum('channels','userCreatedChannels') COLLATE utf8_persian_ci NOT NULL,
`channelOwnerUserId` int(11) NOT NULL,
`fileServerUrlId` int(11) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `channels`
--
ALTER TABLE `channels`
ADD PRIMARY KEY (`id`);
Error and notice:
This table does not contain a unique column. Features related to the grid edit, checkbox, Edit, Copy and Delete links may not work after saving.
Error
UPDATE `channels` SET `channelState` = '1' WHERE LIMIT 1
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LIMIT 1' at line 1
id of this table is unique and A_I, how can i resolve this problem?

XAMPP's PHPMyAdmin Ignores AUTO_INCREMENT Attribute When Exporting the Table

The problem that I am going to tell is for all the tables in the DB, When I dump my DB it ignores AUTO_INCREMENT attribute. My actual table is as the following:
CREATE TABLE IF NOT EXISTS `departments` (
`departmentid` int(11) NOT NULL AUTO_INCREMENT,
`chairid` int(11) NOT NULL,
`department_name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`image` varchar(128) NOT NULL DEFAULT 'default_department.png',
`url` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1'
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
But if I dump the table using PHPMyAdmin it does not add auto_increment which I mentioned before.
The output of the exported .sql file's content is here:
CREATE TABLE IF NOT EXISTS `departments` (
`departmentid` int(11) NOT NULL, -- AUTO_INCREMENT is missing
`chairid` int(11) NOT NULL,
`department_name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`image` varchar(128) NOT NULL DEFAULT 'default_department.png',
`url` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1'
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
I especially checked that if auto_increment is disabled in CREATE TABLE options but no, it is not.
This was fixed in phpMyAdmin version 4.5.0.1 (Sep 2015):
https://github.com/phpmyadmin/phpmyadmin/issues/11492
I supposed it ignores auto increment but I explore file and at the bottom of sql I found that it altered to auto increment somewhere else

Is .sql file different for MySQL and SQL?

I have a .sql file and I want to load it into MySQL database. I don't know from which database (MySQL or MS-SQL) it was created.
Now, I am trying to import that file into MySQL database. It is showing errors while importing and executing that file.
Q1. So, my question is whether the .sql file generated from MySQL and MS-SQL are different?
Note: I am using SQLYog software (graphical interface for MySQL) for importing the file.
Here is the ERROR:
Query:
CREATE TABLE ads (
id bigint(20) NOT NULL auto_increment,
city_id int(11) NOT NULL,
type text collate utf8_bin NOT NULL,
town text collate utf8_bin NOT NULL,
address text collate utf8_bin NOT NULL,
price text collate utf8_bin NOT NULL,
info text collate utf8_bin NOT NULL,
link text collate utf8_bin NOT NULL,
hasImage int(11) NOT NULL,
language varchar(2) collate utf8_bin NOT NULL,
time_added varchar(255) collate utf8_bin NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1
Error occured at:2009-09-08 17:41:01
Line no.:35
Error Code: 1050 - Table 'ads' already exists
Query:
CREATE TABLE ads (
id bigint(20) NOT NULL auto_increment,
city_id int(11) NOT NULL,
type text collate utf8_bin NOT NULL,
town text collate utf8_bin NOT NULL,
address text collate utf8_bin NOT NULL,
price text collate utf8_bin NOT NULL,
info text collate utf8_bin NOT NULL,
link text collate utf8_bin NOT NULL,
hasImage int(11) NOT NULL,
language varchar(2) collate utf8_bin NOT NULL,
time_added varchar(255) collate utf8_bin NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1
Error occured at:2009-09-08 17:41:21
Line no.:35
Error Code: 1050 - Table 'ads' already exists
Query:
CREATE TABLE ads (
id bigint(20) NOT NULL auto_increment,
city_id int(11) NOT NULL,
type text collate utf8_bin NOT NULL,
town text collate utf8_bin NOT NULL,
address text collate utf8_bin NOT NULL,
price text collate utf8_bin NOT NULL,
info text collate utf8_bin NOT NULL,
link text collate utf8_bin NOT NULL,
hasImage int(11) NOT NULL,
language varchar(2) collate utf8_bin NOT NULL,
time_added varchar(255) collate utf8_bin NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1
Error occured at:2009-09-08 17:41:35
Line no.:35
Error Code: 1050 - Table 'ads' already exists
Query:
CREATE TABLE ads (
id bigint(20) NOT NULL auto_increment,
city_id int(11) NOT NULL,
type text collate utf8_bin NOT NULL,
town text collate utf8_bin NOT NULL,
address text collate utf8_bin NOT NULL,
price text collate utf8_bin NOT NULL,
info text collate utf8_bin NOT NULL,
link text collate utf8_bin NOT NULL,
hasImage int(11) NOT NULL,
language varchar(2) collate utf8_bin NOT NULL,
time_added varchar(255) collate utf8_bin NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1
Error occured at:2009-09-08 17:42:07
Line no.:35
Error Code: 1050 - Table 'ads' already exists
Query:
1 Stanford University 6700 http://www.orkut.co.in
.
.
.
The file extension ".sql" is essentially meaningless: it's just there so that you know what the file is. It will just be a plain text file containing SQL, which you can open in Notepad. Therefore, there's no special "mysql" or "sql server" extensions.
The errors you're getting there "Table 'ads' already exists" are because you're trying to create a table which already exists in the database. (Did you read the error?) You have a few options:
Change the SQL to this:
CREATE TABLE IF NOT EXISTS ads ( id bigint(20) ...
Change the SQL to this:
DROP TABLE IF EXISTS ads;
CREATE TABLE ads (id bigint(20) ...
Clear out all the tables in the DB first.
MySQL and Microsoft SQL Server do, unfortunately, implement different dialects of SQL. So, the answer to your question :
Q1. So, my question is whether the
.sql file generated from MySQL and
MS-SQL are different?
is "yes, quite possiby". However, the specific error you're showing appears to be due strictly to a "Create table" statement on line 35 being executed over and over and over again, and that can't depend on such dialect differences, so the causes have to be other ones (such as bugs or subtle format differences in what that "SQLYog" program, which I'm not familiar with, expects as its input).
It depends on which software exported this file. Also check in the file's header which software did it and analyze the syntax to determine where this file belongs to. Note also that SQL is microsoft sql server.
Yes. SQL varies greatly from database to database. While there is a SQL standard which most databases support much of, every database has numerous, incompatible nonstandard features and functions above and beyond that specified by the standard.
There are also many migration docs avaiable:
MSSQL => MySQL:
http://dev.mysql.com/tech-resources/articles/migrating-from-microsoft.html
MySQL => MSSQL:
http://technet.microsoft.com/en-us/library/cc966396.aspx
There are two obvious things in your CREATE TABLE statement that MSSQL doesn't support which jump out immediately:
ENGINE=InnoDB
AUTO_INCREMENT=1
Those are both MySQL-specific statements.
It looks like your script is being executed multiple times (as the line number where the error occurs is the same each time). The error also indicates that you're trying to define a table that already exists, which may be from the first time this script ran. You may want to look into how the script is being run to see why it may be executing multiple times.
The SQL itself looks fine for MySQL. It wouldn't run with SQL Server.