*Note: I am not an experienced developer by any means! please be kind. *
I am trying to get a simple web development setup to work so I can work on a webapp. I have a few docker containers running for MariaDB, PHP-fpm and Nginx. Right now I can edit en see php pages on my localhost in my browser. The PDO connection to MariaDB also works. I installed DBeaver and used it to connect to my MariaDB Docker conainer, to work on my create and insert scripts for my MariaDB database.
It went well so far, last week I made my first create script and it worked just fine. I tried it again today and suddenly it’s full of errors that I can’t figure out. Nothing seems to work. I don’t think it can be a regular syntax error because I do everything the MariaDB syntax tells me to do in their documentation. I’ve been trying to solve this for a few hours now and I am getting really frustrated.
This is my create script (changed the names for the sake of anonimity and didn’t put the other tables in because the error is somewhere between the first three commands anyway):
DROP DATABASE IF EXISTS db_name;
CREATE DATABASE db_name;
USE db_name;
CREATE OR REPLACE TABLE tablename (
pk_number INT(5) AUTO_INCREMENT PRIMARY KEY,
coloumn1 VARCHAR(20) NOT NULL,
coloumn2 VARCHAR(20) NOT NULL,
coloumn3 VARCHAR(30),
coloumn4 VARCHAR(30),
coloumn5 VARCHAR(10),
coloumn6 VARCHAR(6),
coloumn7 VARCHAR(20),
coloumn8 DATE
);
When I run the SQL Script it says:
SQL Error [1064] [42000]: (conn=19) 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 ‘USE db_name;
CREATE TABLE tablename (
pk_number VARCHAR(30) PRIMAR…’ at line 2Error position: line: 1
Or:
SQL Error [1064] [42000]: (conn=18) 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 ‘USE db_name’ at line 3
I can’t figure out whether the problem is:
-With the drop or replace database command
-With the USE database command
-With the create table script
-With DBeaver or something
I thought maybe try something different, so I tried to run this:
CREATE OR REPLACE DATABASE db_name;
USE db_name;
CREATE TABLE tablename (
column1 VARCHAR(30) PRIMARY KEY,
column2 VARCHAR(30)
);
This gave the same result. I don’t know why I can’t ‘USE db_name’??
I also tried some other things like:
backticks around the table names
- ‘these ticks around the table names’
- USE DATABASE instead of USE
- Drop and create the database by hand and then run the USE and CREATE TABLE script (same result)
- Deleting all of the whitespace
- Uncommenting and deleting unnessisary text
The weirdest thing is that it all works fine if I run the queries individually (so first run drop database, then run create database, then run create table).
Why is this happening? What can I do about it?
I wonder if maybe the database doesn’t realise that it’s made the database and then concludes that it isn’t able to use that database? I don’t know how to explain that really but I used to put “GO” in between lines when I used Microsoft SQL and that used to help but I can’t use that here obviously.
Any help or tips are appreciated! Thanks for reading.
And before you comment: yes, I actually have checked tons of similiar questions and they were not helpful at all! I’ve used google and asked friends and that also didn’t help me (that’s why I am making a post).
Tamra Bronsvoort is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.