Change Column If Exists - mariadb

Apparently MariaDB supports IF EXISTS in conjunction with CHANGE for `ALTER TABLE queries (https://mariadb.com/kb/en/library/alter-table/) however the detailed documentation does not imply this.
Here are a couple of example queries for a test table:
ALTER TABLE `test` CHANGE COLUMN `col2` `col2` INT(1) NOT NULL AFTER `col1`;
ALTER TABLE `test` CHANGE COLUMN `col2` `col2` INT(2) NOT NULL AFTER `col1`;
However when I add IF EXISTS MariaDB throws a sytax error:
ALTER TABLE `test` CHANGE COLUMN `col2` IF EXISTS `col2` INT(1) NOT NULL AFTER `col1`;
Yes, apparently this is not part of the SQL standard (and in my scenario I do not have to be concerned with that or compatibility with MySQL). However I am interested in a more explicit yes or no in support for IF EXISTS specifically in conjunction with CHANGE and if so how the proper syntax would look? Locally I'm using MariaDB 10.2.6.

Incorrect
ALTER TABLE `test` CHANGE COLUMN `col2` IF EXISTS `col2` INT(1) NOT NULL AFTER `col1`;
Incorrect
ALTER TABLE `test` CHANGE COLUMN `col2` `col2` IF EXISTS INT(1) NOT NULL AFTER `col1`;
Correct
ALTER TABLE `test` CHANGE COLUMN IF EXISTS `col2` `col2` INT(1) NOT NULL AFTER `col1`;
The IF EXISTS must immediately follow CHANGE COLUMN.

Related

increase the max character limit for the index keys in mariaDB

On Nextcloud/social we have this problem:
Primary index name on "oc_social_a2_cache_documts" is too long.
see: https://github.com/nextcloud/social/issues/850
Is there a way to increase the limit for the character length of the index?
Update:
The table structure is
REATE TABLE `oc_social_a2_cache_documts` (
`id_prim` varchar(128) COLLATE utf8_bin NOT NULL,
`id` varchar(1000) COLLATE utf8_bin DEFAULT NULL
PRIMARY KEY (`id_prim`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
It works, If I rename the table to a shorter name with one character less: just oc_social_a2_cache_docums
But that is not a solution here, since it seems to have worked before in older versions I am not sure, how the problem could only occur now with the latest update???

How to set a name for not null constraint in MariaDB

i tried to set names for constraints in MariaDB like i used to do in oracle but i got error.
This my code:
"CREATE TABLE TBLPROFILES(
ID_PROFILE INT(3) UNSIGNED AUTO_INCREMENT,
PROFILE_NAME VARCHAR(10) CONSTRAINT NN_PROFILES_PROFILE_NAME NOT NULL,
CONSTRAINT UQ_PROFILES_PROFILE_NAME UNIQUE (PROFILE_NAME),
CONSTRAINT PK_PROFILES_ID_PROFILE PRIMARY KEY (ID_PROFILE));"
This is the error:
"ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server ver
sion for the right syntax to use near 'CONSTRAINT NN_PROFILES_PROFILE_NAME NOT NULL,
CONSTRAINT UQ_PROFILES_PROFILE_NAM' at line 3"
There are a thousand syntax differences between Oracle and MySQL. You must consult the MySQL reference manual.
CREATE TABLE TBLPROFILES( -- ok
ID_PROFILE INT(3) UNSIGNED AUTO_INCREMENT, -- ok
PROFILE_NAME VARCHAR(10) NOT NULL, -- note 1
UNIQUE (PROFILE_NAME), -- note 2
PRIMARY KEY (ID_PROFILE)); -- note 2
Note 1: NOT NULL is not really a "constraint"; it is part of the column definition.
Note 2: I provided a simpler syntax. (Possibly the Oracle syntax would also work; I don't know.)
Other notes:
INT(3) -- the 3 is ignored unless you have ZEROFILL. An INT is always a 32-bit number. (I think this differes from Oracle.)
All-caps is not normally used in MySQL for table and column names. (But it does work, and such are always case-folded.)
Disclaimer: No, I don't know exactly how many differences there are.

How does sqlite ON CONFLICT clause work in a column definition?

I have the following example:
sqlite> create table FILES (FILENAME VARCHAR(1024) PRIMARY KEY NOT NULL ON CONFLICT IGNORE);
With this, I gather from the documentation that an insert that violates this primary key would be ignored.
But that's not happening, the 2. insert gives an error.
sqlite> insert into files values ('fileA');
sqlite> insert into files values ('fileA');
Error: UNIQUE constraint failed: FILES.FILENAME
So, how does ON CONFLICT IGNORE work in the above table, what is its purpose ?
(Note - I know I can also run insert or ignore into files values ('fileA');, and that will be ignored, but the question is about the column definition).
The conflict resolution clause applies to the NOT NULL constraint, so it would ignore only NULL values.
To ignore duplicates, add ON CONFLICT IGNORE directly after the PRIMARY KEY constraint.

MariaDB: ALTER TABLE syntax to add a FOREIGN KEY?

what'S wrong with the following statement?
ALTER TABLE submittedForecast
ADD CONSTRAINT FOREIGN KEY (data) REFERENCES blobs (id);
The error message I am getting is
Can't create table `fcdemo`.`#sql-664_b` (errno: 150 "Foreign key constraint is incorrectly formed")
This works for me on MariaDB 10.1.8:
CREATE TABLE `submittedforecast` (
`id` INT(11) NOT NULL,
`data` INT(11) NOT NULL,
PRIMARY KEY (`id`),
INDEX `data` (`data`)
) ENGINE=InnoDB;
CREATE TABLE `blobs` (
`id` INT(11) NOT NULL,
`content` BLOB NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
ALTER TABLE submittedForecast
ADD CONSTRAINT FOREIGN KEY (data) REFERENCES blobs (id);
Can you give your MariaDB version number and a complete example including the CREATE TABLE statements for submittedForecast and blobs?
No idea if you already solved this but make sure both engines and collations match between tables (e.g: latin1 to latin1 and InnoDB to InnoDB).
I was having the same issue and by switching these I managed to get it working.
I was getting the same issue and after looking at my table structure, found out that my child table's foreign key clause was not referencing the primary key of parent table. Once i changed it to reference to primary key of parent table, the error was gone.
I had the same error and is actually pretty easy to solve, you have name the constraint, something like this should do:
ALTER TABLE submittedForecast ADD CONSTRAINT `fk_submittedForecast`
FOREIGN KEY (data) REFERENCES blobs (id)
If you would like more cohesion also add at the end of the query
ON DELETE CASCADE ON UPDATE RESTRICT
This could also be a different fields error so check if the table key and the foreign key are of the same type and have the same atributes.
I had the same problem, too. When checking the definition of the fields, I noticed that one field was defined as INT and the other as BIGINT. After changing the BIGINT type to INT, I was able to create my foreign key.

Sybase null datetime

I am attempting to insert a row into a table while leaving two datetime columns null.
Error (for both columns):
The column column_name in table conflict does not allow null values.
The only constraint the table table has is a primary key constraint. The constraint is not on the datetime columns.
Is it possible to have a null datetime in Sybase?
You could try casting the NULL value as a datetime datatype and see if that works.
For example:
INSERT INTO tbl_conflict (date_column) VALUES (CAST(NULL AS datetime))
I did not realize that Sybase defaulted columns to not null. I need to set the columns as null.

Resources