MySQL error 1064 on INSERT of DATE - mysql

I have a table named "calendar" that was created with this SQL statement in a PHP program.
$sql="CREATE TABLE calendar (
mdate DATE,
PRIMARY KEY(mdate),
special CHAR(20),
director CHAR(20),
dealer CHAR(20),
meeting CHAR(20))";
When I tried to insert data into this table I received an error 1064 from MySQL. In order to determine where the error was, I copied the INSERT statement from my PHP program and used constants instead of variables. This was the INSERT statement:
INSERT INTO TABLE calendar (mdate, special, director, dealer, meeting) VALUES('2013-05-01','Special Game','Director','Dealer','Meeting');
Here is the error message I received:
#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 calendar (mdate, special, director, dealer, meeting) VALUES('2013-05-01','' at line 1
I assume that the error involves the "mdate" column. I tried several formats for the date with no change in the error message. I reviewed the MySQL manual 9.1.3. Date and Time Literals and it appears that YYYY-MM-DD is a valid format for DATE columns. I also checked 9.3. Reserved Words to make sure that none of my column names were reserved words.
I had originally planned to use LOAD DATA INLINE to update my table but it turns out that statement needs FILE authority and the host of my web site was unwilling to grant that authority.
This seems too simple to be a problem. I hope that someone can see something that I am not seeing.

You must not use the keyword TABLE in the INSERT sentence. It should be like this:
INSERT INTO calendar (mdate, special, director, dealer, meeting)
VALUES('2013-05-01','Special Game','Director','Dealer','Meeting');

calendar is already table you dont need to say TABLE
INSERT INTO calendar (mdate, special, director, dealer, meeting)
VALUES('2013-05-01','Special Game','Director','Dealer','Meeting');

Related

Insert data partially into a table

I am trying to insert into my table something with partial data and leaving the rest default but somehow it keeps saying wrong syntax
My query is like this:
INSERT INTO day
(1030, 1100, date, tech)
VALUES ('356-635-3633', '356-635-3633', '2019-04-07', 'Thy')
#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 '1030, 1100, date, tech) VALUES('356-635-3633', '356-635-3633', '2019-04-07', 'Th' at line 1
A query like this works though:
INSERT INTO day (date, tech) VALUES ('2019-04-07', 'Thy')
The datatype for all those columns are varchar(30)
You would need to quotes these all-digits identifiers, using backticks;
INSERT INTO day
(`1030`, `1100`, date, tech)
VALUES ('356-635-3633', '356-635-3633', '2019-04-07', 'Thy')
From the documentation:
Identifiers may begin with a digit but unless quoted may not consist solely of digits.
Demo on DB Fiddle (you can uncomment the original column list to generate the error).
NB: day and date correspond to names of MySQL functions. Using them like you do do not generate error, but it would still be a good idea to surround them with backticks a well, just to avoid any ambiguity, hence:
INSERT INTO `day`
(`1030`, `1100`, `date`, tech)
VALUES ('356-635-3633', '356-635-3633', '2019-04-07', 'Thy')

How do I insert brackets into a column name of my SQL table?

I am currently trying to insert brackets into the column name of my table. However, that results in an error when I run my script.
The format of my table in my script previously reads:
cursor.execute("CREATE TABLE IF NOT EXISTS table (date date, voltage decimal (2,2))")
I then made changes to this part of the script to add brackets to my table column name. It now reads:
cursor.execute("CREATE TABLE IF NOT EXISTS table (date date, voltage(V) decimal (2,2))")
After adding the brackets i.e. (V), the script fails to run.
The error I get is:
SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(V) decimal (2,2))' at line 1
How can I add brackets to the column name without obtaining an error?
If you want to use special characters in the name of a database, table, or column, put the name in backticks.
CREATE TABLE IF NOT EXISTS table (
date date,
`voltage(V)` decimal (2,2)
)
You'll also need to use the backticks in all queries that refer to the column, so it will probably annoy all your other programmers.
See When to use single quotes, double quotes, and backticks in MySQL for more information about quoting in MySQL.

How to check a string while entering it in MySQL DB for characters in the beginning by DEFAULT?

I want to create a table where i want to list some books on different subject. I want to include first letter of the topic in the book id. For example, books on mathematics will contain 'M', literature will contain 'C'.
I had the idea to use something like
CREATE TABLE book (id VARCHAR(5) DEFAULT LIKE 'M%', book_name VARCHAR(10))
But, it's showing error.
Static analysis:
2 errors were found during analysis.
A comma or a closing bracket was expected. (near "'M%'" at position
46)
Blockquote
Unexpected beginning of statement. (near "10" at position 71)
SQL query:
CREATE TABLE book (id VARCHAR(5) DEFAULT LIKE 'M%', book_name VARCHAR(10))
MySQL said: Documentation
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 'LIKE 'M%', book_name VARCHAR(10))' at line 1
The query usage of default is wrong. You can have only fixed text in default.
Also I am not sure if you want text ending with M in ID or book_name, but looking at the query I am assuming it is id.
You need to use constraint to control the values allowed for id. Correct query with constraint will be as below
CREATE TABLE book (id VARCHAR(5), book_name VARCHAR(10), CONSTRAINT check_book_name CHECK (id like '%M'))
Next thing is
I want to include first letter of the topic in the book id.
to do that you can only add constraint as check but you can control this only through your insert statement.
My suggestion formulate the data set. Then simply create table. Based on your dataset add constraint on the table. Add pre-insert validations. This pre-insert validation would be along with the app or code executing the insert statement.

select inside insert with more data to be inserted

I'm facing a problem with select inside insert statements.
I've take a look at the questions similar to this but still the query is not working.
first, I'm working with MySQL version 5.6.24 with engine InnoDB, and I'm trying to insert this row:
INSERT INTO form (SELECT course_name FROM course WHERE course_id ='1' ), '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','not bad','bad')
I want the first column to be retrieved (which is only one value), but not the other.
I've tried many syntax formats, with VALUES, with semicolon, with more parentheses, etc... but non work. Here is the error:
#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 ' '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','no' at line 1
Thanks.
Use an INSERT-SELECT:
INSERT INTO form
SELECT course_name, '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','not bad','bad'
FROM course
WHERE course_id = '1'

MySQL syntax error regarding WHERE NOT EXISTS

I have a Chef recipe for creating Unix user IDs and deploying them across multiple nodes, to guarantee uniqueness and prevent devs from having to track them themselves. They merely name the application and it is granted a unique ID if an ID for that application doesn't already exist. If one does, it is simply returned to the script and user accounts are created on the webservers with the appropriate value.
I have a mysql database with a single table, called application_id_table which has two columns, id and application_name. id is autoincrementing and application name cannot be null (and must be unique).
Removing the Ruby from my script and making a couple of substitutions, my sql looks like this:
INSERT INTO application_id_table(application_name) VALUES('andy_test')
WHERE NOT EXISTS (select 1 from application_id_table WHERE
application_name = 'andy_test');
when run, I receive the syntax parsing 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 'WHERE NOT EXISTS (select 1 from
application_id_table WHERE application_name = 'a'
I recall seeing that the values statement does not allow a where clause but I don't wish to use a select statement to populate the values as I'm populating them from variables supplied from within Ruby/Chef. Anyone have an idea how to accomplish this?
You want to use insert . . . select:
INSERT INTO application_id_table(application_name)
SELECT aname
FROM (SELECT 'andy_test' as aname) t
WHERE NOT EXISTS (select 1 from application_id_table ait WHERE ait.application_name = t.aname);
You should be able to plug your variable directly into the select statement, the same you would would with the VALUES statement.
Try this:
INSERT INTO application_id_table(application_name)
select 'andy_test'
WHERE NOT EXISTS (select 1 from application_id_table WHERE application_name = 'andy_test');