Importing sql file from phpmyadmin create error - mysql

I have successfully backup sql database. But whenever trying to import it shows some error. Like below:
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 'STORED,
`purchase_gst` decimal(10,2) NOT NULL,
`purchase_due` decimal(10,2' at line 8
Here is my code extracted from SQL file:
DROP TABLE IF EXISTS `purchase`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `purchase` (
`purchase_id` bigint(20) NOT NULL AUTO_INCREMENT,
`purchase_date` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`purchase_item` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`purchase_rate` decimal(10,2) DEFAULT NULL,
`purchase_qty` decimal(10,2) NOT NULL,
`purchase_from` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`purchase_gross` decimal(10,2) GENERATED ALWAYS AS ((`purchase_qty` * `purchase_rate`)) STORED,
`purchase_gst` decimal(10,2) NOT NULL,
`purchase_due` decimal(10,2) NOT NULL,
`purchase_tcost` decimal(10,2) NOT NULL,
`purchase_net` decimal(10,0) GENERATED ALWAYS AS (round(((`purchase_gross` + `purchase_gst`) + `purchase_tcost`),0)) STORED,
PRIMARY KEY (`purchase_id`)
) ENGINE=InnoDB AUTO_INCREMENT=372 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = #saved_cs_client */;

Please check the version of MySQl .This will work on MySQl 5.7 and above versions.

It's depends on your mySql version.
This sql syntax error shows below the MySQL 5.7 version.
Please Check this with in MySQL 5.7 version it's working with below code:
DROP TABLE IF EXISTS `purchase`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `purchase` (
`purchase_id` bigint(20) NOT NULL AUTO_INCREMENT,
`purchase_date` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`purchase_item` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`purchase_rate` decimal(10,2) DEFAULT NULL,
`purchase_qty` decimal(10,2) NOT NULL,
`purchase_from` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`purchase_gross` decimal(10,2) GENERATED ALWAYS AS (`purchase_qty` * `purchase_rate`) STORED,
`purchase_gst` decimal(10,2) NOT NULL,
`purchase_due` decimal(10,2) NOT NULL,
`purchase_tcost` decimal(10,2) NOT NULL,
`purchase_net` decimal(10,0) GENERATED ALWAYS AS (round(((`purchase_gross` +
`purchase_gst`) + `purchase_tcost`),0)) STORED,
PRIMARY KEY (`purchase_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;`enter code here`

Related

Sql constraint error references other column

I don't know a lot of mysql and have an error in my sql script. Currently running mysql 8.0.24. Does anyone know what might be the problem?
Error:
https://prnt.sc/226xk5x
Sql:
-- Dumping structure for table gtav_rp2._vehicle
CREATE TABLE IF NOT EXISTS `_vehicle` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`cid` int(11) unsigned NOT NULL,
`vin` varchar(50) NOT NULL DEFAULT '',
`type` varchar(50) NOT NULL DEFAULT '',
`size` int(11) NOT NULL,
`plate` varchar(50) NOT NULL DEFAULT '',
`model` varchar(50) NOT NULL DEFAULT '',
`name` varchar(50) DEFAULT NULL,
`garage` varchar(59) DEFAULT NULL,
`state` varchar(50) DEFAULT NULL,
`appearance` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid('appearance')),
`mods` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid('mods')),
`data` longtext DEFAULT NULL,
`damage` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid('damage')),
`degredation` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid('degredation')),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Dumping data for table gtav_rp2._vehicle: ~0 rows (approximately)
/*!40000 ALTER TABLE `_vehicle` DISABLE KEYS */;
/*!40000 ALTER TABLE `_vehicle` ENABLE KEYS */;
/*!40101 SET SQL_MODE=IFNULL(#OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IF(#OLD_FOREIGN_KEY_CHECKS IS NULL, 1, #OLD_FOREIGN_KEY_CHECKS) */;
/*!40101 SET CHARACTER
_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
Your CHECK constraint does not reference the column. Or any column, actually. By using single-quotes, you're using a string literal, not a column name.
`appearance` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL
CHECK (json_valid('appearance')),
I assume this is meant to be:
`appearance` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL
CHECK (json_valid(`appearance`)),
Use the right type of quotes for SQL identifiers, not string literals.
In addition, this CHECK constraint would be unnecessary if you used the JSON data type instead of LONGTEXT. The JSON data type already enforces that the content of the column must be valid JSON format.
MySQL's JSON data type already uses utf8mb4 character set and utf8mb4_bin collation.
So your column definition could be simply as follows:
`appearance` JSON

Mysql truncates the data

I can't upload data to mysql with csv upload.
I got this error now:
It says duplicate entry for '7'. but it is not 7 actually. it is 907 in the csv file I'm trying to upload. Mysql takes only 7 of 907 for some reason. How can i solve this problem? please. thanks.
-- phpMyAdmin SQL Dump
-- version 4.6.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Nov 04, 2018 at 11:27 AM
-- Server version: 5.7.14
-- PHP Version: 5.6.25
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `mdb`
--
-- --------------------------------------------------------
--
-- Table structure for table `ttb`
--
CREATE TABLE `ttb` (
`Week` int(11) NOT NULL,
`Date` date DEFAULT NULL,
`Sside` varchar(45) DEFAULT NULL,
`Schange` varchar(45) DEFAULT NULL,
`SRep` tinyint(4) DEFAULT NULL,
`SRepC` varchar(45) DEFAULT NULL,
`Opp` varchar(45) DEFAULT NULL,
`RuRpt` varchar(45) DEFAULT NULL,
`RaRpt` varchar(45) DEFAULT NULL,
`DuRpt` varchar(45) DEFAULT NULL,
`DaRpt` varchar(45) DEFAULT NULL,
`Dist1` varchar(15) DEFAULT NULL,
`Dist2` varchar(15) DEFAULT NULL,
`Dist3` varchar(15) DEFAULT NULL,
`Dist4` varchar(15) DEFAULT NULL,
`Dist5` varchar(15) DEFAULT NULL,
`Distin` varchar(15) DEFAULT NULL,
`Sint` varchar(15) DEFAULT NULL,
`Svar` varchar(15) DEFAULT NULL,
`Sdev` varchar(15) DEFAULT NULL,
`Smean` varchar(15) DEFAULT NULL,
`Smedian` varchar(15) DEFAULT NULL,
`Rerng` varchar(15) DEFAULT NULL,
`Bmedian` varchar(45) DEFAULT NULL,
`Opp1` varchar(45) DEFAULT NULL,
`Opp2` varchar(45) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
and this is the beginning of CSV file that I want to upload:
906,24/10/2018,14,B,,7,,,,,x,4,,,2,3,4,1,2,1,1,1,0.3,,3,1
905,17/10/2018,5,A,,5,,,R1,S1,2x,,3,,2,3,1,4,1,2,1,1,,3,3,2

I am missing a closing bracket was expected. (near ")" at position 136)

I received this error while uploading SQL to server phpMyAdmin. I did look at the answers from similar posts, but I don't think it's the same thing. It should be after -- Table structure for table wzflh_admintools_log. These are lines 127 through 146:
CREATE TABLE `wzflh_admintools_ipblock` (
`id` bigint(20) UNSIGNED NOT NULL,
`ip` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `wzflh_admintools_log`
--
CREATE TABLE `wzflh_admintools_log` (
`id` bigint(20) UNSIGNED NOT NULL,
`logdate` datetime NOT NULL,
`ip` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reason` enum('other','adminpw','ipwl','ipbl','sqlishield','antispam','wafblacklist','tpone','tmpl','template','muashield','csrfshield','badbehaviour','geoblocking','rfishield','dfishield','uploadshield','xssshield','httpbl','loginfailure','securitycode','external','awayschedule','admindir','sessionshield','nonewadmins','nonewfrontendadmins','configmonitor','phpshield') COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`extradata` longtext COLLATE utf8mb4_unicode_ci
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
I have created a database named mydb and imported your tables via the console. This is what I have:
-- phpMyAdmin SQL Dump
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE `wzflh_admintools_ipblock` (
`id` bigint(20) UNSIGNED NOT NULL,
`ip` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `wzflh_admintools_log` (
`id` bigint(20) UNSIGNED NOT NULL,
`logdate` datetime NOT NULL,
`ip` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reason` enum('other', 'adminpw', 'ipwl', 'ipbl', 'sqlishield',
'antispam', 'wafblacklist', 'tpone', 'tmpl',
'template', 'muashield', 'csrfshield',
'badbehaviour', 'geoblocking', 'rfishield',
'dfishield', 'uploadshield', 'xssshield', 'httpbl',
'loginfailure', 'securitycode', 'external',
'awayschedule', 'admindir', 'sessionshield',
'nonewadmins', 'nonewfrontendadmins', 'configmonitor',
'phpshield') COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`extradata` longtext COLLATE utf8mb4_unicode_ci
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Copy and paste this to your SQL file:
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Oct 19, 2017 at 07:46 PM
-- Server version: 10.1.16-MariaDB
-- PHP Version: 5.6.24
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `centeroakland`
--
-- --------------------------------------------------------
--
-- Table structure for table `wzflh_admintools_ipblock`
--
CREATE TABLE `wzflh_admintools_ipblock` (
`id` bigint(20) UNSIGNED NOT NULL,
`ip` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `wzflh_admintools_log`
--
CREATE TABLE `wzflh_admintools_log` (
`id` bigint(20) UNSIGNED NOT NULL,
`logdate` datetime NOT NULL,
`ip` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reason` enum('other','adminpw','ipwl','ipbl','sqlishield','antispam','wafblacklist','tpone','tmpl','template','muashield','csrfshield','badbehaviour','geoblocking','rfishield','dfishield','uploadshield','xssshield','httpbl','loginfailure','securitycode','external','awayschedule','admindir','sessionshield','nonewadmins','nonewfrontendadmins','configmonitor','phpshield') COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`extradata` longtext COLLATE utf8mb4_unicode_ci
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!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 */;
Save it then import it.

Migrating MySQL DB version 5.6 to 5.5 (Django) structure dump import fails

Locally I was working on Django project with MySQL version 5.6
I am trying to host a DB on Azure and unfortunately ClearDB support 5.5 and not yet 5.6.
I am migrating the data using the export/import functionality on MySQL Workbench.
I dump the structure of the database first and that fails in syntax not sure why.
02:09:05 Restoring C:\Users\Saher\Documents\dumps\Dumpauthmodels.sql
Running: mysql.exe --defaults-file="c:\users\saher\appdata\local\temp\tmpb3d1gg.cnf" --protocol=tcp --host=us-cdbr-azure-west-b.cleardb.com --user=b42d1da1703a84 --port=3306 --default-character-set=utf8 --comments --database=mytravelsdb < "C:\\Users\\Saher\\Documents\\dumps\\Dumpauthmodels.sql"
ERROR 1064 (42000) at line 79: 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 '(6),
`is_superuser` tinyint(1) NOT NULL,
`username` varchar(30) COLLATE utf8' at line 4
Operation failed with exitcode 1
02:09:07 Import of C:\Users\Saher\Documents\dumps\Dumpauthmodels.sql has finished with 1 errors
This is simply part of the Django table model.
I know I can simply ignore these tables and just recreate with Django migrata/syncdb commands, but I am curious what Create table syntax changed between 5.5 and 5.6 causing syntax error.
Here is the table structure in the dump from the localhost MySQL 5.6
--
-- Table structure for table `auth_user`
--
DROP TABLE IF EXISTS `auth_user`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `auth_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`password` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`last_login` datetime(6),
`is_superuser` tinyint(1) NOT NULL,
`username` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`first_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`last_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(254) COLLATE utf8_unicode_ci,
`is_staff` tinyint(1) NOT NULL,
`is_active` tinyint(1) NOT NULL,
`date_joined` datetime(6) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = #saved_cs_client */;
The error message is deceiving I had to post the query in the editor to view the error. It seems the MySQL 5.6 has Automatic Initialization and Updating for TIMESTAMP and DATETIME which is supported if you use such syntax
CREATE TABLE t1 (
ts TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
);
This is not supported in 5.5 so one needs to get rid of the parentheses after each datetime and timestamp.
Import worked now.

How do I speed up this 1.7 Second Mysql Query?

I'm writing a webapp in PHP where a list of users can be voted on. The query I'm using to pull a user from the table is quite slow. I suspect there is a much more efficient way to check if the target user has already been voted on by the active user.
SELECT *
FROM users
WHERE id NOT IN (
SELECT ratedid
FROM votes
WHERE who LIKE 12707264
)
AND picture1 NOT LIKE ''
AND cp1 < '10'
AND gender NOT LIKE 'male'
ORDER BY RAND( )
LIMIT 1
Table data as follows:
>SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
>SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `notchus_userdb`
--
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id` bigint(20) NOT NULL,
`username` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`first_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`last_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`bio` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`picture1` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`cp1` int(100) DEFAULT NULL,
`picture2` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`picture3` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`friends` blob,
`relationship_status` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`relationship_interest` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`verified` int(1) NOT NULL,
`gender` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`birthday` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`hometown` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`citylocation` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`oauth_provider` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`oauth_uid` int(11) NOT NULL,
`ratchet` int(1) DEFAULT NULL,
`boss` int(1) DEFAULT NULL,
`isadmin` int(1) DEFAULT NULL,
`views` int(10) NOT NULL,
`reviews` int(10) NOT NULL,
`email` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`isuser` int(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Server version: 5.1.70-cll
-- PHP Version: 5.3.17
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `notchus_userdb`
--
-- --------------------------------------------------------
--
-- Table structure for table `votes`
--
CREATE TABLE IF NOT EXISTS `votes` (
`uqid` int(11) NOT NULL AUTO_INCREMENT,
`value` tinyint(4) NOT NULL,
`picture` int(11) DEFAULT NULL,
`ratedid` bigint(20) DEFAULT NULL,
`comment` int(11) DEFAULT NULL,
`quote` int(11) DEFAULT NULL,
`who` bigint(20) NOT NULL,
`votedate` int(11) NOT NULL,
`control` varchar(100) NOT NULL,
PRIMARY KEY (`uqid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1084 ;
First of all, LIKE is only for doing wildcard matching. I'm surprised that you're not getting an error doing a LIKE on an integer. Then again, nothing MySQL does surprises me.
SELECT *
FROM users
WHERE id NOT IN (
SELECT ratedid
FROM votes
WHERE who = 12707264
)
AND picture1 <> ''
AND cp1 < '10'
AND gender <> 'male'
ORDER BY RAND( )
LIMIT 1
Also note that if it's OK for picture1 to be an empty string, then you shouldn't have a NOT NULL on the column, and you should store a NULL in the column where is no picture.
Your like statements don't appear to be using wildcards, I see no reason to use them here. Try changing them to equal signs. Indexing your tables could help you as well.
You don't appear to have indices on the foreign key relationships.
try:
create index who on votes (who);
create index q1 on users (picture1, cp1, gender);
Also, you appear to be confused about "like" and "=" - when comparing an integer column (e.g. CP1), use cp1 < 1 or who = 12707264.