I have a table that looks like this (using MariaDB 10.2.36):
CREATE TABLE `eg_nat202109` (
`id` int(10) unsigned DEFAULT NULL,
`log_id` bigint(20) unsigned NOT NULL,
`log_device_ip` int(10) unsigned DEFAULT NULL,
`log_device_ipv6` varchar(64) DEFAULT '',
`dev_id` int(10) unsigned DEFAULT NULL,
`time_stamp` int(10) unsigned DEFAULT NULL,
`dev_name` varchar(64) DEFAULT NULL,
`log_type` int(10) unsigned DEFAULT NULL,
`sub_type` varchar(20) DEFAULT NULL,
`ip_protocol` tinyint(4) DEFAULT NULL,
`app_protocol` tinyint(4) DEFAULT NULL,
`user_name` varchar(64) DEFAULT NULL,
`ori_src_ip` int(10) unsigned DEFAULT NULL,
`ori_src_ipv6` varchar(64) DEFAULT '',
`ori_dst_ip` int(10) unsigned DEFAULT NULL,
`ori_dst_ipv6` varchar(64) DEFAULT '',
`ori_src_port` int(10) unsigned DEFAULT NULL,
`Ori_dest_port` int(10) unsigned DEFAULT NULL,
`rep_src_ip` int(10) unsigned DEFAULT NULL,
`rep_src_ipv6` varchar(64) DEFAULT '',
`rep_dst_ip` int(10) unsigned DEFAULT NULL,
`rep_dst_ipv6` varchar(64) DEFAULT '' COMMENT,
`rep_src_port` int(10) unsigned DEFAULT NULL,
`rep_dst_port` int(10) unsigned DEFAULT NULL,
`ori_vrfid` int(10) unsigned DEFAULT NULL,
`rep_vrfid` int(10) unsigned DEFAULT NULL,
`ori_input_bytes` bigint(20) unsigned DEFAULT NULL,
`rep_input_bytes` bigint(20) unsigned DEFAULT NULL,
`die_time` int(10) unsigned DEFAULT NULL,
`create_time` int(10) unsigned DEFAULT NULL,
`date_time` datetime NOT NULL,
`l_vrfname` varchar(64) DEFAULT NULL,
`g_vrfname` varchar(64) DEFAULT NULL,
`mac` bigint(20) unsigned DEFAULT NULL,
`sn` varchar(64) DEFAULT NULL,
`extendChar1` varchar(64) DEFAULT NULL,
`extendChar2` varchar(64) DEFAULT NULL,
`log_device_mac` varchar(32) DEFAULT NULL,
KEY `index_user_name` (`user_name`),
KEY `index_ori_src_ip` (`ori_src_ip`),
KEY `index_ori_dst_ip` (`ori_dst_ip`),
KEY `index_rep_dst_ip` (`rep_dst_ip`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
PARTITION BY RANGE COLUMNS(`date_time`)
(PARTITION `part2021092900` VALUES LESS THAN ('2021-09-29 01:00:00') ENGINE = InnoDB,
PARTITION `part2021092901` VALUES LESS THAN ('2021-09-29 02:00:00') ENGINE = InnoDB,
PARTITION `part2021092902` VALUES LESS THAN ('2021-09-29 03:00:00') ENGINE = InnoDB,
PARTITION `part2021092903` VALUES LESS THAN ('2021-09-29 04:00:00') ENGINE = InnoDB,
PARTITION `part2021092904` VALUES LESS THAN ('2021-09-29 05:00:00') ENGINE = InnoDB,
PARTITION `part2021092905` VALUES LESS THAN ('2021-09-29 06:00:00') ENGINE = InnoDB,
PARTITION `part2021092907` VALUES LESS THAN ('2021-09-29 08:00:00') ENGINE = InnoDB,
PARTITION `part2021092908` VALUES LESS THAN ('2021-09-29 09:00:00') ENGINE = InnoDB,
PARTITION `part2021092909` VALUES LESS THAN ('2021-09-29 10:00:00') ENGINE = InnoDB,
PARTITION `part2021092910` VALUES LESS THAN ('2021-09-29 11:00:00') ENGINE = InnoDB,
PARTITION `part2021092911` VALUES LESS THAN ('2021-09-29 12:00:00') ENGINE = InnoDB,
PARTITION `part2021092912` VALUES LESS THAN ('2021-09-29 13:00:00') ENGINE = InnoDB,
PARTITION `part2021092913` VALUES LESS THAN ('2021-09-29 14:00:00') ENGINE = InnoDB,
PARTITION `part2021092914` VALUES LESS THAN ('2021-09-29 15:00:00') ENGINE = InnoDB,
PARTITION `part2021092915` VALUES LESS THAN ('2021-09-29 16:00:00') ENGINE = InnoDB,
PARTITION `part2021092916` VALUES LESS THAN ('2021-09-29 17:00:00') ENGINE = InnoDB,
PARTITION `part2021092917` VALUES LESS THAN ('2021-09-29 18:00:00') ENGINE = InnoDB,
PARTITION `part2021092918` VALUES LESS THAN ('2021-09-29 19:00:00') ENGINE = InnoDB,
PARTITION `part2021092919` VALUES LESS THAN ('2021-09-29 20:00:00') ENGINE = InnoDB,
PARTITION `part2021092920` VALUES LESS THAN ('2021-09-29 21:00:00') ENGINE = InnoDB,
PARTITION `part2021092921` VALUES LESS THAN ('2021-09-29 22:00:00') ENGINE = InnoDB,
PARTITION `part2021092922` VALUES LESS THAN ('2021-09-29 23:00:00') ENGINE = InnoDB,
PARTITION `part2021092923` VALUES LESS THAN ('2021-09-30 00:00:00') ENGINE = InnoDB,
PARTITION `part2021093000` VALUES LESS THAN ('2021-09-30 01:00:00') ENGINE = InnoDB,
PARTITION `part2021093001` VALUES LESS THAN ('2021-09-30 02:00:00') ENGINE = InnoDB,
PARTITION `part2021093002` VALUES LESS THAN ('2021-09-30 03:00:00') ENGINE = InnoDB,
PARTITION `part2021093003` VALUES LESS THAN ('2021-09-30 04:00:00') ENGINE = InnoDB,
PARTITION `part2021093004` VALUES LESS THAN ('2021-09-30 05:00:00') ENGINE = InnoDB,
PARTITION `part2021093005` VALUES LESS THAN ('2021-09-30 06:00:00') ENGINE = InnoDB);
I'm loading data use program from a file has 2,0000 data, the sql like this
load data local infile '" + fileName + "' replace into table "
+ tableName + " fields terminated by '\t' " + cols + "
and then it run about minutes, the thread seen not work, so I dump the stack information, it show like that
"NatLogPool-5" #158 prio=5 os_prio=0 tid=0x0000560eee487800 nid=0xc5 runnable [0x00007f68dfabb000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:101)
at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:144)
at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:174)
- locked <0x0000000087f869a8> (a com.mysql.jdbc.util.ReadAheadInputStream)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3011)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3472)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3462)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3905)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2491)
- locked <0x0000000087f6e8f8> (a com.mysql.jdbc.JDBC4Connection)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2449)
at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:845)
- locked <0x0000000087f6e8f8> (a com.mysql.jdbc.JDBC4Connection)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:745)
at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95)
at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java)
at org.springframework.jdbc.core.JdbcTemplate$1ExecuteStatementCallback.doInStatement(JdbcTemplate.java:406)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:376)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:415)
so I check the running state of a data base, it's block on reading file, and then it runing normal, it over and over again
I can't embed images now, you can see this situation easy by navicat server monitoring function.
I try other version, such as 10.2、10.2.40、10.3.31 even MySQL 8, them work well.
note: I use data base with docker redirectly.
Is this a bug in version 10.2.36?
I took the part of the code from the project, https://github.com/huangminwen/load-file-test, it only modify the database config in the yml file, I try 3 method on this test
load file way
by stream way
batch insert way
when you run this test code, please exeute the sql file, and the put the eg_nat202109-1632813678309-588228.tmp file on /opt/inap/filedata/audit/loadfile path, you can put it anywhere actually, but should modify the code com.study.storage.NatLogSaveThread#FILE_PATH at the same time.
It's about 15mins you can see the block on reading file.
Related
As posted on another forum, when upgrading XWiki from v7.0.1 to v13.10.9, a non-critical database table xwikistatsvisit, for the user visiting statistics, was preventing the after-upgrade migrations. It contained over seven million records and sized 3GB in total. As a workaround, we had to delete all records in the table, but the SQL command delete from table xwikistatsvisit took over two hours.
I have verified from ER diagram that the table is stand-alone without any foreign key referring to or from other tables. And the database is MariaDB v10.9.2 installed on the same host.
The host under test is a medium virtual machine with SSD, 4 CPUs of Intel i9 and 8GB of RAM, running MariaDB v10.9.2. Also, the hypervisor needs to enable “PAE/NX” and “Nested VT-x/ADM-V” for higher performance; otherwise, the computing task will get stuck forever.
My Questions:
Why the SQL command took so long? Is there any way to proceed faster? E.g. disabling the keys and restrictions, etc.; but I am unfamiliar with this area.
I will highly appreciate any hints or suggestions.
The definition of the table xwikistatsvisit:
--
-- Table structure for table `xwikistatsvisit`
--
DROP TABLE IF EXISTS `xwikistatsvisit`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `xwikistatsvisit` (
`XWV_ID` bigint(20) NOT NULL,
`XWV_NUMBER` int(11) DEFAULT NULL,
`XWV_NAME` varchar(255) NOT NULL,
`XWV_CLASSNAME` varchar(255) DEFAULT NULL,
`XWV_IP` varchar(255) NOT NULL,
`XWV_USER_AGENT` longtext NOT NULL,
`XWV_COOKIE` longtext NOT NULL,
`XWV_UNIQUE_ID` varchar(255) NOT NULL,
`XWV_PAGE_VIEWS` int(11) DEFAULT NULL,
`XWV_PAGE_SAVES` int(11) DEFAULT NULL,
`XWV_DOWNLOADS` int(11) DEFAULT NULL,
`XWV_START_DATE` datetime DEFAULT NULL,
`XWV_END_DATE` datetime DEFAULT NULL,
PRIMARY KEY (`XWV_ID`),
KEY `XWVS_END_DATE` (`XWV_END_DATE`),
KEY `XWVS_UNIQUE_ID` (`XWV_UNIQUE_ID`),
KEY `XWVS_PAGE_VIEWS` (`XWV_PAGE_VIEWS`),
KEY `XWVS_START_DATE` (`XWV_START_DATE`),
KEY `XWVS_NAME` (`XWV_NAME`),
KEY `XWVS_PAGE_SAVES` (`XWV_PAGE_SAVES`),
KEY `XWVS_DOWNLOADS` (`XWV_DOWNLOADS`),
KEY `XWVS_IP` (`XWV_IP`),
KEY `xwv_user_agent` (`XWV_USER_AGENT`(255)),
KEY `xwv_classname` (`XWV_CLASSNAME`),
KEY `xwv_number` (`XWV_NUMBER`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = #saved_cs_client */;
The DELETE command deletes all the matching rows one at a time. If you want to delete all the records from such a big table, TRUNCATE should be a lot faster, since it empties the full table at once.
TRUNCATE TABLE xwikistatsvisit;
Difference between DELETE and TRUNCATE.
I have a problem.
I created a MySQL schema with the MySQL Workbench.
But there is a error message.
A comma or closing bracket was expected. (near "VISIBLE" at position 288)
Unexpected start of statement. (near "`ShopID`" at position 330)
Unrecognised statement type. (near "ASC" at position 339)
As I said I created the scheme via the MySQL workbench. But what is wrong ?
Note: PK = Primary Key; FK = Foreign Key.
Fact table:
Contaminants (TimeID (PK/FK), ShopID (PK/FK), FoodID (PK/FK), substance, quantityPerOunce)
Dimension tables:
Time (TimeID (PK), dayNr, dayName, weekNr, monthNr, year)
Food (FoodID (PK), foodName, brand, foodType)
Place (ShopID (PK), name, street, region, country)
CREATE TABLE IF NOT EXISTS `mydb`.`Contaminants` (
`TimeID` INT NOT NULL,
`ShopID` INT NOT NULL,
`FoodID` INT NOT NULL,
`substance` VARCHAR(45) NULL,
`quantityPerOunce` INT NULL,
PRIMARY KEY (`TimeID`, `ShopID`, `FoodID`),
UNIQUE INDEX `TimeID_UNIQUE` (`TimeID` ASC) VISIBLE,
UNIQUE INDEX `ShopID_UNIQUE` (`ShopID` ASC) VISIBLE,
UNIQUE INDEX `FoodID_UNIQUE` (`FoodID` ASC) VISIBLE,
CONSTRAINT `TimeID`
FOREIGN KEY (`TimeID`)
REFERENCES `mydb`.`Time` (`TimeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `ShopID`
FOREIGN KEY (`ShopID`)
REFERENCES `mydb`.`Shop` (`ShopID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FoodID`
FOREIGN KEY (`FoodID`)
REFERENCES `mydb`.`Food` (`FoodID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `mydb`.`Food` (
`FoodID` INT NOT NULL,
`foodName` VARCHAR(45) NULL,
`brand` VARCHAR(45) NULL,
`foodType` VARCHAR(45) NULL,
PRIMARY KEY (`FoodID`))
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `mydb`.`Shop` (
`ShopID` INT NOT NULL,
`INT` VARCHAR(45) NULL,
`street` VARCHAR(45) NULL,
`region` VARCHAR(45) NULL,
`country` VARCHAR(45) NULL,
PRIMARY KEY (`ShopID`))
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `mydb`.`Time` (
`TimeID` INT NOT NULL,
`dayNr` INT NULL,
`dayName` VARCHAR(45) NULL,
`weekNr` INT NULL,
`monthNr` VARCHAR(45) NULL,
`year` INT NULL,
PRIMARY KEY (`TimeID`))
ENGINE = InnoDB
Mariadb 10.3
MariaDB [demo]> show create table people;
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| people | CREATE TABLE `people` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`address_id` int(11) DEFAULT NULL,
`civility_id` int(11) DEFAULT NULL,
`marital_status_id` int(11) DEFAULT NULL,
`language_id` int(11) DEFAULT NULL,
`country_id` int(11) DEFAULT NULL,
`import_timestamp` int(11) DEFAULT NULL WITHOUT SYSTEM VERSIONING,
PRIMARY KEY (`id`),
FULLTEXT KEY `indexPeopleMultiWords` (`lastname`,`lastname_of_birth`,`firstname`)
) ENGINE=InnoDB AUTO_INCREMENT=5818 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci WITH SYSTEM VERSIONING
PARTITION BY SYSTEM_TIME
(PARTITION `p_hist` HISTORY ENGINE = InnoDB,
PARTITION `p_cur` CURRENT ENGINE = InnoDB) |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.001 sec)
But when I try to drop SYSTEM VERSIONING, I got this message error :
MariaDB [nemo]> ALTER TABLE people DROP SYSTEM VERSIONING;
ERROR 4131 (HY000): Can not DROP SYSTEM VERSIONING for table `people` partitioned BY SYSTEM_TIME
Question:
How to drop system versioning in my case ? Thank you
ref: dbfiddle
PARITITION BY SYSTEM TIME is a system versioned construct.
Per the error message you received, you need to remove the partitioning in order to removing the system versioning:
ALTER TABLE people REMOVE PARTITIONING
ALTER TABLE people DROP SYSTEM VERSIONING
I'm trying to create some deployment tools and I don't want to use BTEQ. I've been trying to work with the Teradata.Client.Provider in PowerShell but I'm getting syntax errors on the creation of a table.
[Teradata Database] [3706] Syntax error: expected something between
';' and the 'IF' keyword.
SELECT * FROM DBC.TablesV WHERE DatabaseName = DATABASE AND TableName = 'MyTable';
IF ACTIVITYCOUNT > 0 THEN GOTO EndStep1;
CREATE MULTISET TABLE MyTable ,
NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
MyColId INTEGER GENERATED ALWAYS AS IDENTITY
(START WITH 1
INCREMENT BY 1
MINVALUE 0
MAXVALUE 2147483647
NO CYCLE)
NOT NULL,
MyColType VARCHAR(50) NULL,
MyColTarget VARCHAR(128) NULL,
MyColScriptName VARCHAR(256) NULL,
MyColOutput VARCHAR(64000) NULL,
isMyColException BYTEINT(1) NULL,
ExceptionOutput VARCHAR(64000) NULL,
MyColBuild VARCHAR(128) NULL,
MyColDate TIMESTAMP NOT NULL
)
PRIMARY INDEX PI_MyTable_MyColLogId(MyColLogId);
LABEL EndStep1;
I would rather not use BTEQ as I've not found it has worked well in other deployment tools we have created and requires a bit of hacks. Is there anything I can use that would avoid using that tool?
What Parse error?
The CREATE will fail due to double INTEGER in MyColId and VARCHAR(max) in ExceptionOutput, it's an unknown datatype in Teradata.
I am trying to create a plugin in WP and I need to create some tables with it. The problem is that when I use collation utf8_general_ci, the tables creation stops and does not create the 5th and the 6th table. The other tables execute normally. I can't find what's wrong.
<?php
/*
Plugin Name: UniTimetable
Plugin URI:
Description: A plugin to create timetable for lessons and classrooms
Version: 1.0
Author: Fotis Kokkoras, Antonis Roussos
Author URI: https://www.linkedin.com/pub/antonis-roussos/47/25b/9a5
License: GPLv2
*/
register_activation_hook( __FILE__, 'utt_install' );
function utt_install(){
$sql = "
CREATE TABLE IF NOT EXISTS `wp_utt_periods` (
`periodID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`year` YEAR NOT NULL COMMENT 'το έτος - έτσι θα μπορεί να κρατηθεί και ιστορικό σε βάθος χρόνου',
`semester` ENUM('Ε','Χ') NOT NULL COMMENT 'Εαρινό, Χειμερινό',
PRIMARY KEY (`periodID`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `wp_utt_subjects` (
`subjectID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(64) NOT NULL COMMENT 'το επίσημο όνομα του μαθήματος',
`type` ENUM('Θ','Ε','ΑΠ') NOT NULL COMMENT 'Θεωρία, Εργαστήριο, Άσκηση-Πράξη',
`semester` TINYINT UNSIGNED NOT NULL COMMENT 'το εξάμηνο σπουδών στο οποίο απευθύνεται το μάθημα',
`is_enabled` TINYINT(1) NOT NULL DEFAULT 1 COMMENT 'εαν το μάθημα είναι ενεργό - η εφαρμογή θα προβάλει μόνο τα ενεργά μαθήματα',
PRIMARY KEY (`subjectID`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `wp_utt_groups` (
`groupID` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'μοναδικό μέσω ευρετηρίου UQ - για χρήση ως FKey στον Lessons',
`periodID` INT UNSIGNED NOT NULL COMMENT 'FKey από Periods',
`subjectID` INT UNSIGNED NOT NULL COMMENT 'FKey από Subjects',
`groupNo` TINYINT UNSIGNED NOT NULL COMMENT 'ο αριθμός της ομάδας (εργαστηριακής, κτλ)',
PRIMARY KEY (`periodID`, `subjectID`, `groupNo`),
INDEX `fk_Groups_Periods_idx` (`periodID` ASC),
INDEX `fk_Groups_Subject1_idx` (`subjectID` ASC),
UNIQUE INDEX `groupID_UNIQUE` (`groupID` ASC),
CONSTRAINT `fk_Groups_Periods`
FOREIGN KEY (`periodID`)
REFERENCES `wp_utt_periods` (`periodID`)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT `fk_Groups_Subjects`
FOREIGN KEY (`subjectID`)
REFERENCES `wp_utt_subjects` (`subjectID`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `wp_utt_teachers` (
`teacherID` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`surname` VARCHAR(35) NOT NULL,
`name` VARCHAR(35) NULL,
PRIMARY KEY (`teacherID`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `wp_utt_classrooms` (
`classroomID` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(35) NOT NULL,
`type` ENUM('Δ','Ε') NOT NULL COMMENT 'Διαλέξεων, Εργαστηρίου',
`is_available` TINYINT(1) NOT NULL DEFAULT 1,
PRIMARY KEY (`classroomID`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `wp_utt_lessons` (
`groupID` INT UNSIGNED NOT NULL COMMENT 'FKey από Groups',
`classroomID` SMALLINT UNSIGNED NOT NULL COMMENT 'FKey από Classrooms',
`teacherID` SMALLINT UNSIGNED NOT NULL COMMENT 'FKey από Teachers',
`datetime` DATETIME NOT NULL COMMENT 'το date part καθορίζει την ημερομηνία και το time part την ώρα',
PRIMARY KEY (`groupID`, `classroomID`, `teacherID`, `datetime`),
INDEX `fk_Lesson_Classrooms1_idx` (`classroomID` ASC),
INDEX `fk_Lesson_Teachers1_idx` (`teacherID` ASC),
UNIQUE INDEX `datetime_classroom_UNIQUE` (`datetime` ASC, `classroomID` ASC),
UNIQUE INDEX `datetime_teacher_UNIQUE` (`datetime` ASC, `teacherID` ASC),
UNIQUE INDEX `datetime_group_UNIQUE` (`datetime` ASC, `groupID` ASC),
CONSTRAINT `fk_Lessons_Classrooms`
FOREIGN KEY (`classroomID`)
REFERENCES `wp_utt_classrooms` (`classroomID`)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT `fk_Lessons_Teachers`
FOREIGN KEY (`teacherID`)
REFERENCES `wp_utt_teachers` (`teacherID`)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT `fk_Lessons_Groups1`
FOREIGN KEY (`groupID`)
REFERENCES `wp_utt_groups` (`groupID`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
?>
dbDelta is notoriously fussy about how SQL is formatted. According to the Codex:
You must put each field on its own line in your SQL statement.
You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
You must not use any apostrophes or backticks around field names.
Field types must be all lowercase. SQL keywords, like CREATE TABLE and UPDATE, must be uppercase