How to set a name for not null constraint in MariaDB - 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.

Related

MariaDB constraint is incorrectly formed although columns are of the same type

I've got a problem while creating (altering) foreign key.
I have two tables in my DB (created via flyway migrations):
connector (migration)
create table if not exists connector
(
id char(36) not null,
# other fields omitted
primary key (id)
) DEFAULT CHARACTER SET 'utf8mb4'
COLLATE 'utf8mb4_unicode_520_ci';
connector_preset (migration)
CREATE TABLE IF NOT EXISTS connector_preset
(
id CHAR(36) NOT NULL,
# other fields omitted
PRIMARY KEY (id)
);
I want to create a link between connector and connector_preset, so I created another migration like this:
ALTER IGNORE TABLE `connector`
# `connector_preset`.`id`
ADD COLUMN IF NOT EXISTS `preset_id` CHAR(36),
ADD CONSTRAINT `fk_connector_preset_id` FOREIGN KEY (`preset_id`) REFERENCES `connector_preset` (`id`);
but it fails with the following error:
SQL State : HY000
Error Code : 1005
Message : (conn=4) Can't create table `test`.`connector` (errno: 150 "Foreign key constraint is incorrectly formed")
Location : db/migration/...
Line : 34
Statement : ALTER IGNORE TABLE `connector`
# `connector_preset`.`id`
ADD COLUMN IF NOT EXISTS `preset_id` CHAR(36),
ADD CONSTRAINT `fk_connector_preset_id` FOREIGN KEY (`preset_id`) REFERENCES `connector_preset` (`id`),
The columns seem to be of the same type. Also, for some reason it works in local k8s cluster (10.3.29-MariaDB), but fails in integration tests (testcontainers, MariaDB 10.6.11). Also fails in GH Actions which use 10.3.29, which is strange since it's working locally.
UPD: If I set mariadb version in testcontainers to 10.3.29 - it still fails.
UPD: Tried altering connector_preset table to use the same charset and collation:
ALTER IGNORE TABLE `connector_preset` DEFAULT CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_520_ci';
Still doesn't work
Table connector was created with character set utf8mb4 and collation utf8mb4_unicode_520_ci, while connector_preset was created with servers default character set and collation, which obviously differs.
So you have to convert your table to the right character set.
Your attempt to change it with ALTER TABLE ... DEFAULT CHARACTER SET 'utf8mb4' sets the default character set for the table, but not for the columns. Instead you have to convert it with:
ALTER TABLE connector_preset CONVERT TO CHARSET utf8mb4 COLLATE utf8mb4_unicode_520_ci

i'm unsure as to why i'm unable to run this sql script [duplicate]

This question already has answers here:
MySQL error 1064 syntax but everything seems fine
(3 answers)
Closed 3 years ago.
created this with my sql and unsure what is wrong with the script
CREATE TABLE IF NOT EXISTS `restaurant`.`restaurant` (
`_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`added_date` VARCHAR(30) NULL,
`tele_number` VARCHAR(8) NULL,
`about` TEXT(1024) NULL,
`average_rating` INT NULL,
`price` VARCHAR(10) NULL,
`opening_hour` VARCHAR(255) NULL,
`restaurantcol` VARCHAR(45) NULL,
PRIMARY KEY (`_id`),
UNIQUE INDEX `_id_UNIQUE` (`_id` ASC) VISIBLE,
UNIQUE INDEX `tele number_UNIQUE` (`tele_number` ASC) VISIBLE)
ENGINE = InnoDB
error given:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' UNIQUE INDEX tele number_UNIQUE (tele_number ASC) VISIBLE) ENGINE = InnoD' at line 12
The error messages of MySQL contain the part of the query that has not been parsed because the last parsed token is the one that produces the error. This means the word before " UNIQUE INDEX tele number_UNIQUE ..." is the problem. That word is VISIBLE.
The VISIBLE keyword has been introduced in MySQL 8.0 and you probably use an earlier version.
By default, the indexes are VISIBLE. You can safely drop this keyword from the query. On MySQL 8 you will get the same result as when you use it. On MySQL 5 the concept of invisible indexes does not exist, all indexes are visible.
I see now in the error message that you are not using MySQL but MariaDB. MariaDB is a fork of MySQL 5 that is compatible with MySQL up to some point. You will find many small differences here and there.

why is there error 1064 in my script when i executed it

MySQL
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') ENGINE = InnoDB' at line 7
CREATE TABLE IF NOT EXISTS `game_review`.`users` (
`user_id` INT NOT NULL AUTO_INCREMENT,
`email_address` VARCHAR(45) NOT NULL,
`password` VARCHAR(6) NOT NULL,
`username` VARCHAR(20) NOT NULL,
PRIMARY KEY (`user_id`),
UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC) VISIBLE)
ENGINE = InnoDB;
I expected this to execute properly since I forward engineered it with my ER diagram that I created, but it gives me the error message above.
MariaDB does not support invisible indices, so the VISIBLE and INVISIBLE keywords are not used. Indices are already visible to the optimizer by default, so you could just use:
UNIQUE INDEX user_id_UNIQUE (user_id)
But, a primary key column should already be unique, so you can probably just not even include the unique index.
Side note: MySQL 8+ does support invisible indices, see here, but your MariaDB version seems to not support them.

MariaDB 10.1 Truncate table cascade syntax error

When i try to execute the following sql statemant MariaDB give an error:
SQL: TRUNCATE $table CASCADE;
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CASCADE' at line 1 (SQL: TRUNCATE wortmann_products CASCADE;)
is cascade removed in MariaDB or is there something wrong whit the sql statement?
If you are looking to cascade out deletes and updates then the definition has to be stored when the table is created or altered, and will apply to the keys (or FK's in associated tables) allowing child records to be deleted in associated table when parent record is deleted. There are plenty of resources on this, I have listed one here: https://mariadb.com/kb/en/the-mariadb-library/foreign-keys/ - Taken from this resource is the example below:
CREATE TABLE author (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL
) ENGINE = InnoDB;
CREATE TABLE book (
id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(200) NOT NULL,
author_id SMALLINT UNSIGNED NOT NULL,
CONSTRAINT `fk_book_author`
FOREIGN KEY (author_id) REFERENCES author (id)
ON DELETE CASCADE
ON UPDATE RESTRICT
) ENGINE = InnoDB;

Unknown sql(ite) syntax error

Why is the following invalid, i'm at a loss here?
'CREATE TABLE IF NOT EXISTS Mons(id int PRIMARY KEY NOT NULL AUTO_INCREMENT, keyword VARCHAR(255) NOT NULL);'
From node.js's sqlite3, in coffeescript as:
db.parallelize( () ->
db.run('CREATE TABLE IF NOT EXISTS Mons(id int PRIMARY KEY NOT NULL AUTO_INCREMENT, keyword VARCHAR(255) NOT NULL);')
)
Exact error:
Error: SQLITE_ERROR: near "AUTO_INCREMENT": syntax error
at Error (native)
The syntax for an autoincrement primary key in sqlite is
INTEGER PRIMARY KEY AUTOINCREMENT
and not
int PRIMARY KEY NOT NULL AUTO_INCREMENT
If you omit the AUTOINCREMENT keyword, you'll get slightly different autoincrement behavior.
Using AUTO_INCREMENT isn't the best idea unless you want unique keys for the lifetime of your db. If you delete a record, that id can never be used again. This constraint will slow your database down and use more memory so if you don't need unique keys, I would suggest dropping this as PRIMARY KEY already has a constraint to resist any identical primary keys.

Resources