I am encountering error while updating my table field 'delete' in MySQL
CREATE TABLE IF NOT EXISTS `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`delete` varchar(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=29 ;
INSERT INTO `student` (`name`, `delete`) VALUES('newa', 'no')
UPDATE SET delete='yes
#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 'set delete-'yes'' at line 1
delete is a reserved word in mySQL.
You need to wrap the field name in backticks:
SET `delete` = .....
or, preferably, use a different field name.
Try:
UPDATE student SET `delete`='yes'
You forgot the table name between UPDATE and SET. You should have UPDATE student SET delete='yes'
Additionally, you shouldn't use reserved keywords as column names.
use `delete` instead of delete. Please do not use reserved words to name tables and columns. You might as well rename the column to is_deleted.
Related
I am using below CREATE TABLE statement
CREATE TABLE IF NOT EXISTS users (
`uuid` varchar(36) NOT NULL,
`json` varchar(MAX) NOT NULL,
PRIMARY KEY (`uuid`)
);
However I keep getting this 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
'MAX) NOT NULL,
PRIMARY KEY (uuid)
)' at line 3
Makes no sense to me.
MAX is not suported for this use, it is reserved for the MAX function. Use the equivalent number instead, check this out: Equivalent of varchar(max) in MySQL?
This will work for you. MAX is reserved keyword. Specify exact number of varchar instead of max. However, varchar(MAX) will work in SQL SERVER 2005+.
CREATE TABLE IF NOT EXISTS users (
uuid varchar(36) NOT NULL,
json varchar(21808) NOT NULL,
PRIMARY KEY (uuid)
);
FIDDLE
MAX() is a function in MySql,So if you want to declare the size to the max.please refer the below example.
CREATE TABLE IF NOT EXISTS users (
`uuid` varchar(36) NOT NULL,
`json` varchar(65535) NOT NULL,
PRIMARY KEY (`uuid`)
);
and if you calculate that 21845*3 = 65535, which wouldn't have worked anyway. Whereas 21844*3 = 65532, which does work.
mysql> CREATE TABLE foo ( v VARCHAR(21844) ) CHARSET=utf8;
Query OK, 0 rows affected (0.32 sec)
I am trying to alter a table and set a default value for a nullable column. But i get the following error.
Here is the command:
ALTER TABLE `questiontboard`.`questions`
CHANGE COLUMN `status` `status` (11) NULL DEFAULT 1 ;
Here is the error:
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 '(11) NULL DEFAULT 1' at line 2
SQL Statement:
ALTER TABLE `questionboard`.`questions`
CHANGE COLUMN `status` `status` (11) NULL DEFAULT 1
ERROR: Error when running failback script. Details follow.
ERROR 1050: Table 'question' already exists
What am i doing wrong?
You forgot the data type. Did you mean
ALTER TABLE `questiontboard`.`questions`
CHANGE COLUMN `status` `status` INT(11) NULL DEFAULT 1 ;
^^^
Your query should be this:
ALTER TABLE `questiontboard`.`questions`
CHANGE COLUMN `status` `status` int(11) NULL DEFAULT 1 ;
^^ here add int as you want the datatype
You are missing datatype of field in the query.
I got the same error when altering a table. I did the exact same thing you did (minus the code typo).
I got the error when altering a column from a SMALLINT to a varchar(n). It gives the "1050 Table already exists..." error. The error was confusing. Of course the table exists, that's why I'm trying to alter it!
In the end, I found out that the problem was that my new varchar(2) was not big enough to hold all the original smallint data. I had one row that had a 4 digit number, so varchar(2) wouldn't work. I changed it to use varchar(4), and it worked.
ALTER TABLE omiccom_wp.myTable
CHANGE COLUMN myColumn myColumn VARCHAR(2) NOT NULL DEFAULT '0' ;
I have created the customer table that has a trigger that not allow null value on the customer_id column. it looks like:
create table customer(
customer_id varchar(20) UNIQUE,
customer_name varchar(15) NOT NULL,
password varchar(10) NOT NULL,
social_number varchar(14) not null,
phone_number varchar(13) NOT NULL,
email varchar(30) NOT NULL,
address varchar(50) NOT NULL,
primary key ( social_number )
);
But, creating the following trigger causes this error.
create trigger null_checker
on customer
after insert
as
delete from customer
where customer_id is null;
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 'on customer after insert as delete from customer where customer_id is null' at line 1
What's wrong with? I could add not-null constraint using not null specifier. but I am not allowed to do it because creating the trigger is a part of my assignment. Any help would be appreciated.
You need to use right syntax:
create trigger null_checker
after insert on customer
for each row
delete from customer
where customer_id is null;
But you cannot modify the same table from trigger.
A solution, without triggers, may be is change the sql_mode to avoid null values defined in your columns.
set sql_mode = 'TRADITIONAL';
That command says to MySQL “give an error instead of a warning” when inserting an incorrect value into a column, like inserting NULL to a 'NOT NULL column.
More info: http://dev.mysql.com/doc/refman/5.0/en/sql-mode.html
I'm doing a mySQL tutorial to learn how to write sql statements. I keep getting this:
#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 ''add_delete_record' ( 'id' int(11) NOT NULL AUTO_INCREMENT, 'content' text' at line 1
This is the sql I am using:
CREATE TABLE IF NOT EXISTS 'add_delete_record' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'content' text NOT NULL,
PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
This is the exact code the tutorial gave, so I am not sure if the tutorial is just older than my version of mysql(v5.5) or if I have something tiny wrong that I am missing.
You should be using backticks(`) instead of single quotes (').
CREATE TABLE IF NOT EXISTS `add_delete_record` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
sql fiddle
Just remove all single quotes from everywhere the query will run fine.
mysql query - error: 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 'a', '1') ON DUPLICATE KEY UPDATE count=count+1' at line 1
The query that fails:
INSERT INTO tags (ip, tag, count)
VALUES ('xx.xx.xxx.xxx', 'krwiopijcy', '1')
ON DUPLICATE KEY UPDATE count=count+1;
Is there anything wrong in my query?
count is a built in function and as such, may require care to be used as such. However, as the docs say, it is perfectly fine as column name. You might want to escape it using
`count`
That being said, I had no problem with your exact query (no escaping) using this table structure:
CREATE TABLE `tags` (
`ip` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`tag` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`count` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`ip`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Your example from the comments has one apostrophe too much (behind 'umacku'), which doesn't seem to be the problem (as it is not given in the error message), but you should paste the exact queries:
INSERT INTO tags (ip, tag, count)
VALUES ('xx.xx.xxx.xxx', 'umacku'', '1')
ON DUPLICATE KEY UPDATE count=count+1;