Modify table to add Partition syntax get error in MySQL - mysql

I tried the following code for table partitioning.
ALTER TABLE activity_log(PRIMARY KEY (`activityId`))
PARTITION BY RANGE( TO_DAYS(dated) ) (
PARTITION p20150101 VALUES LESS THAN (TO_DAYS('2015-01-01')),
PARTITION p20160101 VALUES LESS THAN (TO_DAYS('2016-01-01')),
PARTITION p20170101 VALUES LESS THAN (TO_DAYS('2017-01-01')),
PARTITION p20180101 VALUES LESS THAN (TO_DAYS('2018-01-01')),
);
ERROR
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 '(PRIMARY KEY (activityId))
PARTITION BY RANGE( TO_DAYS(dated) ) (
PART' at line 1
Existing Table Structure
CREATE TABLE `activity_log` (
`activityId` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`teamId` int(11) ,
`addedby` int(11),
`leadId` int(11),
`activity` VARCHAR(255),
`dated` datetime,
PRIMARY KEY (`activityId`),
) ENGINE=InnoDB AUTO_INCREMENT=496831 DEFAULT CHARSET=utf8;
What i am doing wrong. Please guide me.

Related

Why is the MySQL notation "UNIQUE field (field)" causing an error in MySQL 5.7.21?

I ran into a new problem today, when I was dealing with a mysql query that works on 10.1.19-MariaDB localhost, but not on MySQL 5.7.21-0ubuntu0.16.04.1-log:
CREATE TABLE testing (
pageid INT UNSIGNED NOT NULL AUTO_INCREMENT,
position SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (pageid),
UNIQUE position (position)
) ENGINE=InnoDB CHARSET=utf8
In MySQL 5.6 it works without any hickups (fiddle), however, in MySQL 5.7.21 (fiddle) it throws:
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 'position (position)) ENGINE=InnoDB CHARSET=utf8' at line 1
I figured out a solution by replacing UNIQUE position (position) with UNIQUE (position).
But I am wondering, what the underlying problem is, why it works with the other db system, and I am not sure if my solution is correct.
MySQL doesn't allow you to create an index with the same name of the column
The following code works for MySQL (fiddle)
CREATE TABLE testing (
pageid INT UNSIGNED NOT NULL AUTO_INCREMENT,
position SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (pageid),
UNIQUE idx_position (position)
) ENGINE=InnoDB CHARSET=utf8
That's why it is so important to wrap field and table names in backticks in MySQL (and their derivatives):
Try this (same query and index names, only added backticks):
CREATE TABLE `testing` (
`pageid` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`position` SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (`pageid`),
UNIQUE `position` (`position`)
) ENGINE=InnoDB CHARSET=utf8;

Why won't my query let me assign primary key?

Just a note, I'm pretty new to SQL.
I'm using MySQL and using the app SequelPro.
I receive this error when I try to make a table:
"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 '(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8' at line 4"
This is the code I'm using to make my table:
CREATE TABLE klout_scores_3 (
id INT(11) NOT NULL AUTO_INCREMENT,
score INT(11)
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
score INT(11)
You want a comma , at end of that line.
Your query is being interpreted like this:
CREATE TABLE klout_scores_3 (
id INT(11) NOT NULL AUTO_INCREMENT,
score INT(11) PRIMARY KEY
(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The (id) part is really confusing to MySQL and it's raising an error right there. Although adding a , after the INT(11) part will fix it, a better solution is to move the declaration:
CREATE TABLE klout_scores_3 (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
score INT(11)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
It's a little concerning that you have a table name like klout_scores_3, as that suggests you have N of these tables. Relational database design and database normalization
rules strongly frowns on this, you should have a singular table with some kind of ..._id column to relate the scores to whatever record that 3 identifies.

SQL Error - null auto increment

I'm doing a mySQL tutorial to learn how to write sql statements. I keep getting this:
#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 ''add_delete_record' ( 'id' int(11) NOT NULL AUTO_INCREMENT, 'content' text' at line 1
This is the sql I am using:
CREATE TABLE IF NOT EXISTS 'add_delete_record' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'content' text NOT NULL,
PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
This is the exact code the tutorial gave, so I am not sure if the tutorial is just older than my version of mysql(v5.5) or if I have something tiny wrong that I am missing.
You should be using backticks(`) instead of single quotes (').
CREATE TABLE IF NOT EXISTS `add_delete_record` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
sql fiddle
Just remove all single quotes from everywhere the query will run fine.

MySQL server version for the right syntax to use near '('id')

i get this error when i try to inport to data base
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 '('id')
) TYPE=MyISAM AUTO_INCREMENT=6' at line 4
DROP TABLE IF EXISTS `categories`;
CREATE TABLE `categories` (
`id` int(5) NOT NULL auto_increment,
`category` varchar(50) NOT NULL default '',
PRIMARY KEY ('id')
) TYPE=MyISAM AUTO_INCREMENT=6 ;
You are using ' here
PRIMARY KEY ('id')
id is in this case a string, not the column name. Use backticks instead.
PRIMARY KEY (`id`)
You have two issues with the code, remove the single quotes around the id in the primary key declaration. You can use backticks or nothing.
And change the Type=MyISAM to:
ENGINE=MyISAM
From the MySQL Docs:
The older term TYPE is supported as a synonym for ENGINE for backward compatibility, but ENGINE is the preferred term and TYPE is deprecated.
So the script will be:
DROP TABLE IF EXISTS `categories`;
CREATE TABLE `categories` (
`id` int(5) NOT NULL auto_increment,
`category` varchar(50) NOT NULL default '',
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=6 ;
See SQL Fiddle with Demo
Try this:
DROP TABLE IF EXISTS `categories`;
CREATE TABLE `categories` (
`id` int(5) NOT NULL auto_increment,
`category` varchar(50) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MYISAM AUTO_INCREMENT=6
The problems is that PRIMARY KEY ('id') should use backquotes insted of quotes
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 'FROM disc_disc WHERE disc_id=NULL' at line 1 SQL=SELECT disc_id, disc_name, FROM disc_disc WHERE disc_id=NULL
// zjistit jmeno diskuse
$disc_name = "";
$sql = sprintf("SELECT disc_id, disc_name, " .
"FROM disc_disc " .
"WHERE disc_id=%s",
quote_smart($disc_id));
$db->setQuery($sql);
$rec = $db->loadObjectList();
if ($db->getErrorNum()) {
JError::raiseWarning( 500, $db->stderr() );
}

How to create a table in MySQL 5.5 that has fulltext indexing and partitioning?

I update MySQL versition from 5.0 to 5.5. and I am new for studying mysql partition. firstly, I type:
SHOW VARIABLES LIKE '%partition%'
Variable_name Value
have_partitioning YES
Make sure that the new version support partition. I tried to partition my table by every 10 minutes, then INSERT, UPDATE, QUERY huge data into this table for a test.
First, I need create a new table, I type my code:
CREATE TABLE test (
`id` int unsigned NOT NULL auto_increment,
`words` varchar(100) collate utf8_unicode_ci NOT NULL,
`date` varchar(10) collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `index` (`words`)
)
ENGINE=MyISAM
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci
AUTO_INCREMENT=0
PARTITION BY RANGE (MINUTE(`date`))
(
PARTITION p0 VALUES LESS THAN (1322644000),
PARTITION p1 VALUES LESS THAN (1322644600) ,
PARTITION p2 VALUES LESS THAN (1322641200) ,
PARTITION p3 VALUES LESS THAN (1322641800) ,
PARTITION p4 VALUES LESS THAN MAXVALUE
);
It return alert: #1564 - This partition function is not allowed, so what is this problem? thanks.
UPDATE
Modify date into int NOT NULL, and PARTITION BY RANGE MINUTE(date) into PARTITION BY RANGE COLUMNS(date)
CREATE TABLE test (
`id` int unsigned NOT NULL auto_increment,
`words` varchar(100) collate utf8_unicode_ci NOT NULL,
`date` int NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `index` (`words`)
)
ENGINE=MyISAM
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci
AUTO_INCREMENT=0
PARTITION BY RANGE COLUMNS(`date`)
(
PARTITION p0 VALUES LESS THAN (1322644000),
PARTITION p1 VALUES LESS THAN (1322644600) ,
PARTITION p2 VALUES LESS THAN (1322641200) ,
PARTITION p3 VALUES LESS THAN (1322641800) ,
PARTITION p4 VALUES LESS THAN MAXVALUE
);
Then caused new error: #1214 - The used table type doesn't support FULLTEXT indexes
I am so sorry, mysql not support fulltext and partition at the same time.
See partitioning limitations
FULLTEXT indexes. Partitioned tables do not support FULLTEXT indexes or searches. This includes partitioned tables employing the MyISAM storage engine.
One issue might be
select MINUTE('2008-10-10 56:56:98') returns null, the reason is Minute function returns minute from time or datetime value, where as in your case date is varchar
MINUTE function returns in either date/datetime expression. Again, A partitioning key must be either an integer column or an expression that resolves to an
integer but inyour case it's VARCHAR