How to remove a sequence? - mariadb

Under mariadb-10.3.7 on an InnoDB engine, I'm trying to have the following structure:
drop sequence if exists user_account_id_seq;
create sequence user_account_id_seq start with 1 increment by 10;
drop table if exists user_account;
create table user_account (
-- id bigint(20) unsigned not null auto_increment,
id bigint(20) unsigned not null default (next value for user_account_id_seq),
version int(10) unsigned not null,
created_on datetime,
updated_on datetime,
firstname varchar(255) not null,
lastname varchar(255) not null,
password varchar(100),
password_salt varchar(50),
readable_password varchar(50),
email varchar(50) not null,
confirmed_email bit(1) not null check (confirmed_email in (0, 1)),
work_phone varchar(20),
unique key email (email),
primary key (id)
);
But it doesn't want to remove a sequence:
--------------
drop sequence if exists user_account_id_seq
--------------
--------------
commit
--------------
--------------
create sequence user_account_id_seq start with 1 increment by 10
--------------
ERROR 1050 (42S01) at line 4: Table '`useraccount`.`user_account_id_seq`' already exists
+ /usr/bin/mariadb/install/bin/mysql useraccount --protocol=tcp -h mysql -P 3306 -u root -v
--------------
I then manually tried these commands to show how puzzling this message is:
MariaDB [useraccount]> create sequence user_account_id_seq start with 1 increment by 10;
ERROR 1813 (HY000): Tablespace for table '`useraccount`.`user_account_id_seq`' exists. Please DISCARD the tablespace before IMPORT
MariaDB [useraccount]> alter table `useraccount`.`user_account_id_seq` discard tablespace;
ERROR 1146 (42S02): Table 'useraccount.user_account_id_seq' doesn't exist

Related

Not able to drop field in MariaDB table

I have table my_transitions and I am trying to delete field called myStage.
I am using this command:
alter table `my_transitions` drop `myStage`
I am seeing this error:
Key column 'myStage' doesn't exist in table
But when I list all fields using this command:
describe my_transitions
I can see
id bigint(20) unsigned NO PRI NULL auto_increment
myStage varchar(255) NO MUL NULL
updated_at timestamp YES NULL
Anyone can see if I am doing something wrong?
EDIT:
If I run show create table my_transitions;, I am getting:
CREATE TABLE `my_transitions` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`myStage` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`myStage1` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_stage_combination` (`myStage`,`myStage1`),
KEY `my_transitions_myStage` (`myStage`),
KEY `my_transitions_myStage1` (`myStage1`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
I have solved it by first deleting the unique key
ALTER TABLE my_transitions DROP INDEX unique_stage_combination;
It seems like it is not possible to delete a column if it is a part of index key in Maria DB 10.5.8.
This is a pecular bug in MariaDB. It affects MariaDB 10.5.
Demo: https://dbfiddle.uk/?rdbms=mariadb_10.5&fiddle=867204670347fa29e40bd5eb510c6956
The workaround is to drop the UNIQUE KEY that column mystage is part of first, then drop the column.
alter table my_transitions drop key unique_stage_combination, drop column mystage;
P.S.: I tested this on MySQL 8.0 and it does not require the workaround. It does drop the column, but it leaves the table with a UNIQUE KEY column on just one column mystage1, which might not be what you want.

Is there any way to not use in-memory authentication? #PreAuthorize, #Secured, #RoleAllowed don't work

Questions: 1.How to link Authority to annotations?
Intent is to use current annotations to lock some APIs: #RoleAllowed("ROLE_VIEWER","ROLE_EDITOR"),#Secured({ "ROLE_VIEWER", "ROLE_EDITOR" }),#PreAuthorize("hasRole('USER') or hasRole('ADMIN')").
#EnableGlobalMethodSecurity(prePostEnabled=true,securedEnabled=true,jsr250Enabled=true) is used already. DB is shown below. Firebase is used to generate/authenticate.
CREATE TABLE IF NOT EXISTS USER (
ID VARCHAR (50) NOT NULL PRIMARY KEY,
NAME VARCHAR (50) NOT NULL,
LOGIN VARCHAR (50) NOT NULL,
STATUS SMALLINT NOT NULL,
CREATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UPDATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT UC_USER_LOGIN UNIQUE (LOGIN));
CREATE TABLE IF NOT EXISTS AUTHORITY (
ID VARCHAR (50) NOT NULL PRIMARY KEY,
NAME VARCHAR (50) NOT NULL,
DESCRIPTION VARCHAR (255) NOT NULL,
CREATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UPDATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT UC_AUTHORITY_NAME UNIQUE (NAME));
CREATE TABLE IF NOT EXISTS USER_AUTHORITY (
ID VARCHAR (100) NOT NULL PRIMARY KEY,
USER_ID VARCHAR (50) NOT NULL REFERENCES USER (ID) ON DELETE CASCADE,
AUTHORITY_ID VARCHAR (50) NOT NULL REFERENCES AUTHORITY (ID) ON DELETE CASCADE,
CREATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UPDATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP);

SQL - how do I insert into these tables?

create table Employees
(
EmployeeID INT IDENTITY (1,1) PRIMARY KEY NOT NULL,
EmployeeName VARCHAR(255) NOT NULL,
EmployeeUsername VARCHAR(255) NOT NULL,
EmployeeEmail VARCHAR(255) NOT NULL,
GroupID INT FOREIGN KEY REFERENCES TypeOfGroup(GroupID),
Password VARCHAR(255) NOT NULL
);
create table TypeOfGroup
(
GroupID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
TypeGroup VARCHAR(255) NOT NULL,
Permission CHAR(1) NOT NULL
);
Question: I have a foreign key and wanted to make a connection with another table as I do this to insert into.
My foreign key is 'GROUPID'.
Try this...
insert into TypeOfGroup( TypeGroup, Permission) values ('hr', 1)
"this 1 is used for bit which is a data type in sql server 1 for true and 0 for false"
insert into Employees (EmployeeName, EmployeeUsername, EmployeeEmail,
GroupID, Password) values ('bruno', 'bruno', 'bruno#gmail.com', 1, 'urPassword')
and 1 in the 2nd query is primary of typeofgroup table.

Sqlite foreign key does not work

I read all the previous issues with the foreign keys. I did all the possible configurations but I can not enforce it to work.
I can insert to the nodes table even if I don't have any record in the types table. But as I know the foreign key constraint should not allow this to happen.
CREATE TABLE nodes(
id int NOT NULL PRIMARY KEY ,
ver int NOT NULL,
lock int NOT NULL,
title varchar(50) NOT NULL,
typeid int NOT NULL REFERENCES types(id),
desc text NOT NULL,
CHECK(trim(id) <> '' AND trim(ver) <> '' AND trim(lock) <>'' AND trim(title)
<> '' AND trim(typeid) <> '' AND trim(desc) <> '' )
)
and
CREATE TABLE "types"
("id" INTEGER PRIMARY KEY NOT NULL ,
"name" TEXT NOT NULL )
SQLite 3.19.3
sqlite> PRAGMA foreign_keys;
foreign_keys
------------
1
sqlite> PRAGMA foreign_keys_list;
sqlite>
I don't know why PRAGMA foreign_keys_list answer is empty.
It is solved because I used s in my command
Correct : PRAGMA foreign_key_list(nodes)
sqlite> PRAGMA foreign_key_list(nodes);
id seq table from to on_update on_delete match
---------- ---------- ---------- ---------- ---------- ---------- -------- ------------
0 0 types typeid id NO ACTION NO ACTION NONE
The syntax is PRAGMA foreign_key_list(table-name);
Remove that s after key — SQlite won't warn you if you made a typo and simply returns nothing.

My WordPress plugin can't create all the database tables I want on activation

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

Resources