How to rename a partition name in mysql 5.6.16 - mysql

I'm using mysql 5.6.16 and now want to change a partition name. I cannot find rename partition syntax, and it is inefficient to use statement as below:
alter table name reorganize partition old into ( partition new ...).

I got it by the way of exchange pasrtition. Firstly, I exchange the data in the partition to a normal empty table, Secondly, I reorganize the empty partition to wanted name. Finally, I exhchange the data with the normal table again

Related

partitioning in MySQL : insert into partition

I come from an Apache Hive background.
In that language, you would say the below to insert into date 20220601:
insert into table db.tablename partition(date=20220601)
In MySQL; I can't get such an insert statement to work. I have been Googling & it seems it just sorts itself out?
So if I did
insert into db.tablename
select * from db.othertable
Would it automatically partition the ingested data?
I feel like I am missing something here!
If the table is partitioned, the values you insert determine which partition the row goes into. Partitioning a table requires you define the mapping, so it's always deterministic which partition a row goes into.
Therefore you don't need to tell INSERT which partition to insert the row into. It's determined automatically by the values you insert in the row.
Partitioning in MySQL is not required for a table. By default, a table is not partitioned. This is normal and sufficient in almost all cases.
Perhaps partitioning in Apache Hive is necessary and does something different from the feature called partitioning in MySQL? I don't know Apache Hive, so I can't answer that.
I suggest you read the MySQL manual chapter about partitioning if you want to learn more about it: https://dev.mysql.com/doc/refman/8.0/en/partitioning.html

SAP HANA | TRUNCATE PARTITION

Is there a way we can truncate partition in SAP HANA? As official documents only say about dropping partition?
The official documentation is correct on this topic. Table partitions can be dropped and re-created but not TRUNCATEd.
Remove all data from specific partition of table is possible
Syntax : truncate table <table_name> partition <partition_name>
Except : You cannot use truncate table <table_name> partition <partition_name> when the table object is marked for replication and is created using hash- or round-robin-based partitions. Doing so will end up in error.
Check official document by SAP : https://help.sap.com/viewer/e0d4539d39c34f52ae9ef822c2060077/16.0.3.3/en-US/ab461944bc2b1014bc78d29ab3759f49.html

Add partitions to existing table based on current date

I'd like to partition an existing table into 3 partitions. At the time of update of our solution, a powershell script will connect to the MySQL server and execute a script file.
I tried following query for adding the partitions:
Alter Table `mytable`
PARTITION BY RANGE (TO_DAYS(`TimeStart`))
(
PARTITION start VALUES LESS THAN (0),
PARTITION "from"+(curdate()+0) VALUES LESS THAN (curdate()+1),
PARTITION future VALUES LESS THAN MAXVALUE
);
column timestart (datetime(3) NOT NULL) is part of the primary key.
The partition in the middle requires specific values to make it compatible with maintenance queries run from a Windows service at some interval.
The query fails for two different reasons:
Creation of the name of the partition: I'd like to get e.g. "from20180220" when the script was executed today.
The error messsage is
Error Code: 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"+(curdate()+0) VALUES LESS THAN (curdate()+1), PARTITION future ' at line 5
Creation of the partition value. When the script is run today (Feb 19), I'd like to have an equivalent to VALUES LESS THAN (TO_DAYS('2018-2-20').
Error Code: 1064. Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed near '), PARTITION future VALUES LESS THAN MAXVALUE )' at line 5
I tried TO_DAYS(curdate())+1 also. Actually, I did not expect MySQL to have closures...
How can those errors be solved?
You're attempting to use MySQL data-manipulation-language functions like CURDATE() and TO_DAYS() in your data definition code. You Can't Do Thatâ„¢.
You need to write some sort of program to write out a little file containing your Alter Table command, then run that file in MySQL.
Explanations from O.Jones are a bit too short.
You need to use another language script, like php, perl, python ...
Then write something like would work :
Alter Table `mytable`
PARTITION BY RANGE (TO_DAYS(`TimeStart`))
(
PARTITION start VALUES LESS THAN (0),
PARTITION from".$curdate." VALUES LESS THAN TO_DAYS(".$nextdateTime."),
PARTITION future VALUES LESS THAN MAXVALUE
);

mysql alter on large table to another location?

I have a very large (900GB) table in mysql that I need to perform an alter on, but I lack the temp space necessary on my data partition to hold a temp copy of the table (or an extra copy for an online schema change). I have a newly formatted partition just mounted on my box, is there a way I can have an alter write the new table's schema and data to the new partition?

JDBI ALTER TABLE DROP PARTITION

Hey I'm having an issue with altering table partitions using JDBI. Here is an example of the query I'm trying to run:
ALTER TABLE table1 DROP PARTITION P_1
This runs fine in MySQL when dropping the partition "P_1" from the table "table1."
I've implemented it in my java code as the following:
#SqlUpdate("ALTER TABLE table1 DROP PARTITION :partition;")
public void deletePartition(#Bind("partition") String partition);
And call this function as such
deletePartition("P_1")
However, this results in the following error:
Causing: org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ''P_1'' at line 1 [statement:"ALTER TABLE table1 DROP PARTITION :partition;", located:"ALTER TABLE table1 DROP PARTITION :partition;", rewritten:"/* HiveDropBoxDBI.deletePartition */ ALTER TABLE table1 DROP PARTITION ?;", arguments:{ positional:{}, named:{partition:'P_1'}, finder:[]}]
Is this functionality not supported? Or am I missing something with my syntax?
Thanks
We need to use #Define for changing the query. #Bind is only for used for binding parameters (like some_field = :value).
#SqlUpdate("ALTER TABLE table1 DROP PARTITION :partition;")
public void deletePartition(#Bind("partition") String partition);
You can't use bind parameters for DDL such as CREATE, ALTER and DROP.
In order to drop your partition, you must concatenate the partition name onto the query instead.
However, taking a partition name and appending it straight into a SQL string which gets executed is a recipe for a security vulnerability. Consider some of the following:
'escaping' the partition name with backticks,
checking that the partition name contains only certain whitelisted characters (e.g. alphanumerics and underscores),
querying the INFORMATION_SCHEMA.PARTITIONS table to see if the partition you're trying to drop exists. (This might not be a good idea if there is a chance that the partition may be created or dropped between checking for its existence and dropping it. I don't know your application well enough to say whether this would be a problem.)
As JDBI relies on constant strings for #SqlUpdate annotations, you will not be able to use JDBI in this way to drop partitions, unless you only ever want to drop the same partition.