Mysql Error Only When Run Via a Bash Script - mysql

So I am very new to databases, but I have done some research and I thought that I had figured everything out that I needed but I am running into an error when I try to run my script.
This is my script
mysql -h portal-rds -u $user --password=$mysqlpw <<QUERY_INPUT
CREATE DATABASE IF NOT EXISTS $DATABASE
use $DATABASE
CREATE TABLE IF NOT EXISTS backUpFiles (fileName VARCHAR(20), archiveId VARCHAR(500), checkSum VARCHAR(100), glacierVault VARCHAR(100), timeStamp date);
INSERT INTO backUpFiles
VALUES ('$archive_file_name', '$archiveID', '$CURRENTVAULT', '$checkSum', CURDATE());
QUERY_INPUT
The Error I am recieving is
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'use glacier
CREATE TABLE IF NOT EXISTS backUpFiles (fileName VARCHAR(20), archiveId VARCHAR(500), checkSum VARCHAR(100), glacierVault VARCHAR(100), timeStamp date);
Query OK, 0 rows affected, 1 warning (0.00 sec)
I then decided to manually log into mysql and run this commands and they worked fine besides this one warning which shouldn't break anything (I believe)
mysql> show warnings;
+-------+------+------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------+
| Note | 1050 | Table 'backupfiles' already exists |
+-------+------+------------------------------------+
1 row in set (0.28 sec)

You are missing the ; as the statement separator after the CREATE DATABASE statement.
The statement should look like this:
mysql -h portal-rds -u $user --password=$mysqlpw <<QUERY_INPUT
CREATE DATABASE IF NOT EXISTS $DATABASE; <---- were missing
use $DATABASE
CREATE TABLE IF NOT EXISTS backUpFiles (fileName VARCHAR(20), archiveId VARCHAR(500), checkSum VARCHAR(100), glacierVault VARCHAR(100), timeStamp date);
INSERT INTO backUpFiles
VALUES ('$archive_file_name', '$archiveID', '$CURRENTVAULT', '$checkSum', CURDATE());
QUERY_INPUT

You missed ;(semicolon) for the first two sql statements.
CREATE DATABASE IF NOT EXISTS $DATABASE;
use $DATABASE;

Related

Problems with mysql CREATE TABLE syntax

I try to create table in mysql with the following command:
CREATE TABLE keys (id INT(10), key VARCHAR(100));
and it always gives me an error like this:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'keys (id INT(10), key VARCHAR(100))' at line 1`
So both the table name keys and the field key are reserved in the Mysql namespace. If you choose a different table name (e.g. keys_tbl) and rename the second field something like key_id, your code will work.
You should be very carefully with table and columns naming, you might face a lot of problems in the future.
I suggest find names that are not MySQL reserved words
If you still want to keep the names you should put in in backticks.
mysql> CREATE TABLE keys (id INT(10), key VARCHAR(100));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'keys (id INT(10), key VARCHAR(100))' at line 1
mysql> CREATE TABLE `keys` (id INT(10), `key` VARCHAR(100));
Query OK, 0 rows affected, 1 warning (0.97 sec)
mysql> show create table keys;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'keys' at line 1
mysql>
mysql>
mysql> show create table `keys`;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| keys | CREATE TABLE `keys` (
`id` int DEFAULT NULL,
`key` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
Note that you have to use backticks when using the table

trying to UPDATE foreign key in two tables joined on 4 columns

I have two tables:
mysql> show create table named \G
*************************** 1. row ***************************
Table: named
Create Table: CREATE TABLE `named` (
`table_name` varchar(40) NOT NULL,
`table_pk` int NOT NULL,
`name_type` varchar(6) NOT NULL,
`naml` varchar(200) DEFAULT NULL,
`namf` varchar(45) DEFAULT NULL,
`namt` varchar(10) DEFAULT NULL,
`nams` varchar(10) DEFAULT NULL,
`named_only_pk` int DEFAULT NULL,
PRIMARY KEY (`table_name`,`table_pk`,`name_type`),
KEY `naml` (`naml`(20)),
KEY `naml_2` (`naml`,`namf`,`namt`,`nams`),
KEY `named_only_pk` (`named_only_pk`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
1 row in set (0.01 sec)
mysql> show create table named_only \G
*************************** 1. row ***************************
Table: named_only
Create Table: CREATE TABLE `named_only` (
`pk` int DEFAULT NULL,
`naml` varchar(200) DEFAULT NULL,
`namf` varchar(45) DEFAULT NULL,
`namt` varchar(10) DEFAULT NULL,
`nams` varchar(10) DEFAULT NULL,
KEY `naml` (`naml`,`namf`,`namt`,`nams`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
1 row in set (0.01 sec)
The named table as 30M rows and the named_only table has 2M rows.
The named_only table has a unique constraint on (naml,namf,namt,nams).
The named table came from other tables. The named_only table was filled by:
replace into named_only (naml,namf,namt,nams) select naml,namf,namt,nams from named;
I set the pk values in named_only after the rows are added with:
update named cross join (select #pk:=0) as init set named.pk=#pk:=#pk+1;
I am trying to set up named_only_pk in named as a foreign key to the pk in named_only.
I know a query that will work. I can do:
update named n1, named_only n1 set n1.named_only_pk = n2.pk where
n1.naml = n2.naml and n1.namf = n2.namf and n1.namt = n2.namt and n1.nams = n2.nams;
This is not on a powerhouse machine, but my laptop.
But it seems as though this is going to take a week. I create indexes of (naml,namf,namt,nams) on both tables. Still was taking a while with no indication of how long it would take.
I tried different variations of selects with limits and could not get anything to work.
I tried exporting the named_only table and using that (and awk) to build SQL insert statements but this seems error-prone.
But is this the only way to do this operation in small enough bunches that it does not try to suck up all the memory in the known universe?
Any suggestions?
ADDED 2020/06/16:
Creating an insert trigger did not work. I created:
DELIMITER $$
CREATE TRIGGER named_only_after_insert
AFTER INSERT ON `named_only` for each row
begin
update named n1 set n1.only_pk = NEW.pk
where n1.naml = NEW.naml and
n1.namf = NEW.namf and
n1.namt = NEW.namt and
n1.nams = NEW.nams;
END$$
DELIMITER ;
I tried to insert 10 rows (making the op quicker?) and got:
mysql> insert into named_only (naml,namf,namt,nams) select naml,namf,namt,nams from named where only_pk is NULL limit 10;
ERROR 1442 (HY000): Can't update table 'named' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Sigh. Ok. What if it is coming from a third table, a copy of named?
mysql> insert into named_only (naml, namf, namt, nams) select naml, namf, namt, nams from named2 where pk < 10;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
10 is too many?
mysql> insert into named_only (naml, namf, namt, nams) select naml, namf, namt, nams from named2 where pk < 2;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
Obviously not. There is no other query running, so nothing else is holding a lock on the table.
And I am now doing this in a AWS large Ubuntu image. So my laptop is not the issue.
Well, I think I will have to go outside SQL for this. Which seems lame, but O well.
ADDED LATER:
Yes, it was solvable, but not using SQL.
So here it is:
% echo "select naml,namf,namt,nams,pk from named_uniq;" | mysql -u root -p -B --skip-column-names ca_sos_20200615 > n1.txt
% echo "select naml,namf,namt,nams,table_name,table_pk,name_type from named;" | mysql -u root -p -B --skip-column-names ca_sos_20200615 > n2.txt
% cat n1.txt n2.txt | sort > n3.txt
% awk 'BEGIN{FS="\t"}{if (NF == 4) fk=$4; if (NF == 7) print $5"\t"$6"\t"$7"\t"$1"\t"$2"\t"$3"\t"$4"\t"fk}' n3.txt > n4.txt
Theoretically I can just rename n4.txt to named and import it. Nope. Nothing so easy, because that is a big insert.
So:
% split --lines=10000 --suffix-length=7 n4.txt n4_
/bin/ls -1 n4_* | awk '{print "echo \""$0"\"; cp "$0" named; echo \"load data local infile '\''/home/ubuntu/named'\'' into table named;\" | mysql -u root --password=root --local-infile ca_sos_20200615"}' | /bin/sh 2>/dev/null
And all that in under 3 hours.
Now, how can this be done with SQL?

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax?

I want to make a table and this error comes up. What do I have to fix?
mysql> create database mytest;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytest |
| performance_schema |
| phpmyadmin |
+--------------------+
5 rows in set (0.00 sec)
mysql> use mytest;
Database changed
mysql> create table price(NAME varchar2(60), PRICE number(10));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'varchar2(60), PRICE number(10))' at line 1
there are more errors in your statement:
1) you must use backticks if you use keywords as field names like name
2) there is no datatype VARCHAR2 use VARCHAR instead
3) there is no datatype NUMBER use INT instead:
CREATE TABLE price (
`NAME` VARCHAR(60),
`PRICE` INT(10)
);
sample
mysql> CREATE TABLE price (
-> `NAME` VARCHAR(60),
-> `PRICE` INT(10)
-> );
Query OK, 0 rows affected (0,03 sec)
mysql>
Replace number(10) by INTEGER(10). There is no data type named number in mysql.

MySQL - Table does not exist and can not be created

MySQL 5.5.46 strange error: Table does not exist and can not be created.
Please, can anyone help?
drop table t_example;
Returns: drop table t_example Error Code: 1051. Unknown table 't_example' 0.000 sec
CREATE TABLE t_example(
`id_example` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`des_example` VARCHAR(45) NOT NULL,
`id_example` int unsigned NULL,
PRIMARY KEY (`id_example`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
Returns: Error Code: 1050. Table 't_example' already exists
select * from t_example;
Returns: Error Code: 1146. Table 't_example' doesn't exist
I'm using:
UBUNTU 14.04 LTS;
mysql Ver 14.14 Distrib 5.5.46, for debian-linux-gnu (x86_64) using readline 6.
I'm using root user.
Tried:
mysql> REPAIR TABLE t_example;
+----------------+--------+----------+--------------------------------------+
| Table | Op | Msg_type | Msg_text |
+ ---------------+--------+----------+--------------------------------------+
| mydb.t_example | repair | Error | Table 'mydb.t_example' doesn't exist |
| mydb.t_example | repair | status | Operation failed |
+----------------+--------+----------+--------------------------------------+
2 rows in set (0.00 sec)
Tried too: sudo mysqladmin flush-tables ... Also not solved the problem!
REAL EXAMPLE:
mysql> use flexible;
Database changed
mysql> select * from st_fin_centro_custo;
ERROR 1146 (42S02): Table 'flexible.st_fin_centro_custo' doesn't exist
mysql> CREATE TABLE st_fin_centro_custo(
-> `cod_centro_custo` INT UNSIGNED NOT NULL AUTO_INCREMENT,
-> `des_centro_custo` VARCHAR(45) NOT NULL,
-> PRIMARY KEY (`cod_centro_custo`))
-> ENGINE = InnoDB
-> DEFAULT CHARACTER SET = utf8
-> COLLATE = utf8_general_ci;
ERROR 1050 (42S01): Table '`flexible`.`st_fin_centro_custo`' already exists
mysql> drop table st_fin_centro_custo;
ERROR 1051 (42S02): Unknown table 'st_fin_centro_custo'
mysql>
Tried get from MySQL information_schema:
mysql> SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_TYPE
FROM information_schema.tables
where table_name like 'st_fin_centro_custo';
Empty set (0.00 sec)
mysql>
Note: creating the table with another name works fine.
Thanks a lot!
First select your data base schema by using use command<use schema>.
Then run DROP TABLE IF EXISTS t_example;
After that try to create your table
CREATE TABLE t_example(
`id_example` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`des_example` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id_example`))
ENGINE = INNODB
DEFAULT CHARACTER SET = UTF8
COLLATE = UTF8_GENERAL_CI;
Also don't use duplicate column name.
Another solution is :
Change table name in the create table query, execute it and then rename the table.
Then, you can also drop this table and after it create it without getting error.
It's possible your table is corrupted.
Try REPAIR TABLE t_example;
First of all, try to restart mysqld. If it do not help, try to create this table into another database. Then copy newly created *.frm file to the target database folder. Restart mysqld one more time. Now try to get access/drop/create this table in proper DB.
Edit: How about to drop database and recreate if from the scratch? I hope it's not a production system.

Errors when importing database in phpmyadmin

When i try to import my DB i get this error. I don't know what I should do, please help.
--
-- Database: oddjobexchange
--
-- ---------------------------------------------------- --
--
TABLE structure FOR TABLE admin --
CREATE TABLE IF NOT EXISTS admin (
Email varchar( 30 ) NOT NULL ,
Password varchar( 35 ) NOT NULL ,
PRIMARY KEY ( Email )
) ENGINE = InnoDB DEFAULT CHARSET = latin1;
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Table structure for table admin
CREATE TABLE IF NOT EXISTS admin (
`Em' at line 10
help!
There needs to be a space after the first two dashes. Otherwise it is not a valid comment:
------------------------------------------------------ --
should be:
-- ---------------------------------------------------- --
Just need to add the follwing to top of file.
CREATE DATABASE databasename;
USE databasename;