Generate SQL Partitioning Statements

Enter the number of partitions:




Generated SQL Partitioning Statements:

<script>
	const form = document.querySelector('form');
	const sqlTextarea = document.getElementById('sql');

	form.addEventListener('submit', (event) => {
		event.preventDefault();

		const numPartitions = parseInt(document.getElementById('partitions').value);
		if (isNaN(numPartitions) || numPartitions < 1 || numPartitions > 100) {
			alert('Please enter a valid number of partitions between 1 and 100.');
			return;
		}

		let sql = `CREATE TABLE my_table (

id INT NOT NULL, value VARCHAR(50), PRIMARY KEY (id) )`;

		if (numPartitions > 1) {
			sql += `\nPARTITION BY RANGE(id) (`;

			const partitionSize = Math.floor(100 / numPartitions);
			for (let i = 1; i <= numPartitions; i++) {
				const start = (i - 1) * partitionSize;
				const end = i * partitionSize - 1;
				if (i === numPartitions) {
					sql += `\n  PARTITION p${i} VALUES LESS THAN (${end + 1})`;
				} else {
					sql += `\n  PARTITION p${i} VALUES LESS THAN (${end + 1}),`;
				}
			}

			sql += `\n)`;
		}

		sqlTextarea.value = sql;
	});
</script>
html输入框输入一个分表的数量可以生成SQL的分表语句注意使用SQL语言。

原文地址: https://www.cveoy.top/t/topic/bjNQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录