P
P
paralolik2020-12-19 08:47:42
PostgreSQL
paralolik, 2020-12-19 08:47:42

How would this SQL syntax for PSTGRESQL look like in MARIADB?

I am new to databases, writing a program that runs on MariaDB.

There is SQL written for postgree, but I can't figure out how it should look like in maria

CREATE TABLE images (
       image_id SERIAL PRIMARY KEY,
       filename TEXT NOT NULL UNIQUE
);

CREATE TABLE tags (
       tag_id SERIAL PRIMARY KEY,
       tag    TEXT NOT NULL UNIQUE
);

CREATE TABLE images_tags (
       PRIMARY KEY (tag_id, image_id),
       image_id INTEGER NOT NULL REFERENCES images (image_id),
       tag_id   INTEGER NOT NULL REFERENCES tags (tag_id)
);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2020-12-22
@paralolik

This solution works in MariaDB:

CREATE TABLE images (
  image_id INTEGER AUTO_INCREMENT PRIMARY KEY, 
  filename VARCHAR(255) NOT NULL UNIQUE
);

CREATE TABLE tags (
  tag_id INTEGER AUTO_INCREMENT PRIMARY KEY, 
  tag VARCHAR(255) NOT NULL UNIQUE
);

CREATE TABLE images_tags (
  image_id INTEGER NOT NULL, 
  tag_id INTEGER NOT NULL, 
  PRIMARY KEY (tag_id, image_id), 
  FOREIGN KEY (tag_id) REFERENCES tags(tag_id),
  FOREIGN KEY (image_id) REFERENCES images(image_id)
);

Here you can test SQL queries

M
Melkij, 2020-12-19
@melkij

SERIAL => INT AUTO_INCREMENT
text cannot be unique in mysql/mariadb. You have to choose a prefix by which to count the uniqueness. For example, make varchar(maximum allowed number of characters) instead of text.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question