This kind of sad but I've been at it a while and I just can't seem to figure this statement out, google searches turn up similar questions but I haven't successfully applied the solutions.
I have a table of music, and every time I insert a song into it(each row is a song) I want to insert the song into a table of clean music if it is flagged as clean. I'm using mysql.
use music;
CREATE TRIGGER cache_clean_music BEFORE INSERT ON music
FOR EACH ROW
if new.clean then
insert into clean_music values (new.artist, new.album, new.song, new.filename, new.clean);
end if;
The Error I get is
ERROR 1064 (42000) at line 3: 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 '' at line 4
and here is a description of the music table, the clean_music table is exactly the same
+----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+----------------+
| artist | varchar(100) | YES | | NULL | |
| album | varchar(100) | YES | | NULL | |
| song | varchar(100) | YES | | NULL | |
| filename | varchar(100) | YES | | NULL | |
| clean | tinyint(1) | YES | | NULL | |
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
+----------+---------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
If the two tables are identical (or almost), you probably do not need triggers (and all their mess) at all.
You may use a VIEW instead of a table (with duplicate data) for cache_clean_music:
CREATE VIEW cache_clean_music AS
SELECT artist
, album
, song
, filename ---- and possibly other fields you need
, id
FROM music
WHERE clean ;
Adding an index on music.clean would be a good idea in this case.
Does it help if you wrap a BEGIN...END around things?
CREATE TRIGGER ...
FOR EACH ROW BEGIN
IF ...
....
END IF;
END
The error - ERROR 1064 (42000) at line 3: 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 '' at line 4 - means that the values in some of your input params isn't correct, perhaps there is some mismatching single quote. Can you display your query or the value in the NEW. variables?
Also, once you have fixed that error, your query will also return another error that "the column count doesn't match value count". And that will be because your table has 6 columns but your INSERT has only 5. Mention the columns in your INSERT query and it should be fine, like:
insert into clean_music (artist, album, song, filename, clean) values (new.artist, new.album, new.song, new.filename, new.clean);
Your clue is this
" check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4"
Do you know what version of mysql server you are running?
Did you check the manual to make sure that the command you have written is allowed in that version?
Related
I have been trying to alter a table to include a date column with default value of CURDATE() but MySQL is constantly throwing syntax error. Now, I have checked syntax for altering a table from several sources but I believe I do not have any syntax error. When I remove the default value part, the query runs fine but for some reason it cannot add a default value for the date column. I don't know why that is the case.
The code:
mysql> describe test;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| col1 | int | YES | | 0 | |
| col2 | varchar(100) | YES | | hello | |
| col3 | varchar(5) | YES | | T | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.02 sec)
mysql> ALTER TABLE test ADD COLUMN col4 DATE DEFAULT CURDATE();
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 'CURDATE()' at line 1
mysql>
Edit: My MySQL version: 8.0.31
I think it has to be like this now:
ALTER TABLE test ADD COLUMN col4 DATE DEFAULT (CURRENT_DATE);
Note the parenthesis, or (curdate())
I am looking for a way to convert the result of a query to a string.
The query can be
DESC entries;
or
SHOW COLUMNS FROM entires;
For example, both yield the same result if I execute them in the CLI which is:
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(45) | YES | | NULL | |
| content | text | YES | | NULL | |
| time | int(11) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
However, I need this table as one string. It is NOT an option for me to get his information out of the information_schema.columns table because this information is not present there. It is further necessary to achieve this on the same query. Using PHP or another language is also not an option.
I have tried various things but none of them have been successful so far.
These queries all resulted in an error:
SELECT group_concat(Field) FROM (SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS FROM entries LIMIT 1);
SELECT Field from (SHOW COLUMNS FROM entries);
SELECT 1 from (SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS) FROM entries;
I have tried the same with desc entries; but with the same result.
I always get 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 'show columns
from entries)' at line 1
Neither Google nor the manual could tell me how to achieve this, maybe I was looking for the wrong phrases. Any help is highly appreciated.
I got tables like below and I am trying to INSERT data.
mysql> desc subscribers
-> ;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | smallint(6) | NO | PRI | NULL | auto_increment |
| email | varchar(32) | NO | | NULL | |
| comment | text | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+
Data insert
INSERT INTO `subscribers` VALUES (1, ‘someone#gmail.com’, ‘thanks’);
But It gives me an error.
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 '#gmail.com’, ‘ thanks’)' at line 1
Does anybody see an error??
You are surrounding your strings to be inserted with some sort of apostrophe instead of a single quote. Use this instead (and copy it verbatim from this answer):
INSERT INTO `subscribers`
VALUES (1, 'someone#gmail.com', 'thanks');
Your problem is the curly single quote. But, to prevent future errors, you should always include the column list in the insert:
INSERT INTO `subscribers`(id, email, comment)
VALUES (1, 'someone#gmail.com', 'thanks');
In fact, a natural way to create the table is using an auto-incremented id. If you did then, then the INSERT would look like:
INSERT INTO `subscribers`(email, comment)
VALUES ('someone#gmail.com', 'thanks');
The id would be assigned automatically.
I'm trying to load up a simple CSV file using this tutorial. My CSV includes the list of nationalities, of which here is a sample:
1,Afghan,2015-10-14 17:56:39
Meanwhile, my table definitions is:
+-------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+-------------------+-----------------------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Nationality | varchar(255) | NO | UNI | NULL | |
| Time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+--------------+------+-----+-------------------+-----------------------------+
I'm running the following command:
BULK INSERT meta_nationality FROM '/home/benjamin/Downloads/Nationalities.csv' WITH (fieldterminator = ',', rowterminator ='\n');
This seems to be right, however I get an error message pointing to a syntax issue:
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 'bulk insert...
According to the documentation the syntax error refers to the use of a reserved word, but I wouldn't know which one it is in this case.
Could you help me figure out my way through this? Thanks.
Since you're using MySQL you'll want to use a different syntax. You should be aware that the tutorial you're following is for MSSQL.
LOAD DATA INFILE "/home/benjamin/Downloads/Nationalities.csv"
INTO TABLE meta_nationality
COLUMNS TERMINATED BY ','
LINES TERMINATED BY '\n'
BULK INSERT isn't a command.
You were probably looking for LOAD DATA INFILE _____ INTO TABLE _____
Please refer MySQL tutorials instead of SQL server.
LOAD DATA INFILE
command will help you
Table structure is:
mysql> DESC groups;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| PKey | varchar(64) | NO | PRI | NULL | |
| group_name | varchar(64) | YES | | NULL | |
| Region | varchar(128) | NO | | NULL | |
| Role | varchar(128) | NO | | NULL | |
| parent_group | varchar(64) | NO | MUL | NULL | |
+--------------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
When i am executing this Trigger , i'm having a compilation error
DELIMITER $$
CREATE
TRIGGER `group_before_delete` BEFORE DELETE
ON `groups`
FOR EACH ROW BEGIN
IF old.parent_group=old.PKey THEN
UPDATE `Error: deletion RootGroup is prohibited!`;
ELSE
UPDATE groups
SET parent_group=old.parent_group
WHERE parent_group=old.Pkey;
END IF;
END$$
DELIMITER ;
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 ';
ELSE
t_group=old.parent_group
t_group=old.PKey;
END IF;' at line 6
mysql> DELIMITER ;
Can you tell me what i'm missing here ??
You get an error in your code because the syntax of your UPDATE statement is not valid.
The links you give are 4 and 5 years old!
Since the SIGNAL statement is available in MySQL since version 5.5.0 (released 3 years ago), this is really not a good idea to use the hacks described in these 2 webpages. Instead, use the SIGNAL statement.
Note: From the comments we learn that the OP is not using MySQL 5.5, so SIGNAL is not available.
In your IF statement, the following is not a valid SQL statement:
UPDATE `Error: deletion RootGroup is prohibited!`;
This should be this:
IF old.parent_group=old.PKey THEN
UPDATE `Error: deletion RootGroup is prohibited!` set x=1;
ELSE
UPDATE groups
SET parent_group=old.parent_group
WHERE parent_group=old.PKey;
END IF;
I have never done things this way. It is a bit of an ugly way of doing it. But if it works, what the heck.