I want to add 3 columns. There are two things that I dont know, one is how to specify a default value for each column, and then next is how to alter a table that has got spaces:
ALTER TABLE app name and url
ADD COLUMN price VARCHAR(200)
ADD COLUMN type_of_membership VARCHAR(200)
ADD COLUMN special_deal VARCHAR(200)
I get this 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 'name and url ADD COLUMN price VARCHAR(200) ADD COLUMN type_of_membership VARCH' at line 1
I guess it is because I have a tables name with spaces.
Is this how you insert default values:
ALTER TABLE app name and url
ADD COLUMN price VARCHAR(200) DEFAULT 'None'
ADD COLUMN type_of_membership VARCHAR(200) DEFAULT 'None'
ADD COLUMN special_deal VARCHAR(200) DEFAULT 'None'
UPDATE
This is what I executed:
ALTER TABLE `app name and url`
ADD COLUMN price VARCHAR(200) DEFAULT 'None',
ADD COLUMN type_of_membership VARCHAR(200) DEFAULT 'None',
ADD COLUMN special_deal VARCHAR(200) DEFAULT 'None';
and this is what I got:
'mydb.app name and url' is not BASE TABLE
The error is because it is a view..I get it now. I will have to modify the view
In Mysql, you need to escape it using backtick (grave accent)
ALTER TABLE `app name and url`
ADD COLUMN price VARCHAR(200)
ADD COLUMN type_of_membership VARCHAR(200)
ADD COLUMN special_deal VARCHAR(200)
What is the meaning of grave accent (AKA backtick) quoted characters in MySQL?
PS: Next time use only alphanumeric in your table and column name.
Use ``
You have to replace the table name with
ALTER TABLE `app name and url`
SQL Fiddle demo
Related
I am writing a migration file in typeorm I wanted to add an extra column to my existing table.
I am writing the following query
await queryRunner.query('ALTER TABLE "me" ADD COLUMN "id" VARCHAR(255) DEFAULT NULL');
with the above query, I gt the following error:
RSE_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 '"me" ADD COLUMN "id" VARCHAR(255) DEFAULT NULL' at line 1,`
I just want that my column is nullable, also I want to set Collation to utf8mb4_bin.
How do I achieve that?
There is no need to double quote around table and column name.
ALTER TABLE me ADD COLUMN id VARCHAR(255) DEFAULT NULL;
Furthermore, the id column usually is auto increment int and primary key field.So if you want to name a string field id, it maybe ambiguous.
I am trying to add multiple columns to an existing table in phpMyAdmin, but I keep getting the same error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax ...
I am writing:
ALTER TABLE `WeatherCenter`
ADD COLUMN
BarometricPressure SMALLINT NOT NULL,
CloudType VARCHAR(70) NOT NULL,
WhenLikelyToRain VARCHAR(30) NOT NULL;
I have referred to past posts on StackOverflow, and I am following the experts' recommendation, so why am I getting an error?
ALTER TABLE table_name
ADD COLUMN column_name datatype
correct syntax
ALTER TABLE `WeatherCenter`
ADD COLUMN BarometricPressure SMALLINT NOT NULL,
ADD COLUMN CloudType VARCHAR(70) NOT NULL,
ADD COLUMN WhenLikelyToRain VARCHAR(30) NOT NULL;
check syntax
You need to specify multiple ADD COLUMN
ALTER TABLE `WeatherCenter`
ADD COLUMN BarometricPressure SMALLINT NOT NULL,
ADD COLUMN CloudType VARCHAR(70) NOT NULL,
ADD COLUMN WhenLikelyToRain VARCHAR(30) NOT NULL;
You can alter a table and add multiple columns in one statement by doing it like this.
alter table WeatherCenter add column (BarometricPressure SMALLINT NOT NULL, CloudType VARCHAR(70) NOT NULL, WhenLikelyToRain VARCHAR(30) NOT NULL);
This will help you:
alter table A add first_name varchar(10),last_name varchar(10);
As you're adding columns to an existing table I don't think you're meant to declare NOT NULL in the statement. Also, you don't need to use ADD COLUMN, you can just use ADD.
ALTER TABLE WeatherCentre
ADD BarometricPressure SMALLINT,
ADD CloudType VARCHAR(70),
ADD WhenLikelyToRain VARCHAR(30);
This is from Official MySQL Documentation
ALTER TABLE tbl_name
[alter_specification [, alter_specification] ...]
[partition_options]
alter_specification:
table_options
| ADD [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
| ADD [COLUMN] (col_name column_definition,...)
Possible duplicate of alter table add MULTIPLE columns AFTER column1
alter table table_name add (product varchar(20) not null, price int(10))
this is also working fine
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' ;
What is wrong with this query?
ALTER TABLE KARGO ADD COLUMN test VARCHAR(100) NOT NULL DEFAULT t
The DEFAULT t bit is wrong.
You need to quote the default value DEFAULT 't'
The default value should be quoted as the field is varchar ,
Correct syntax :
ALTER TABLE KARGO ADD COLUMN testt VARCHAR (100) NOT NULL DEFAULT 't'
I have to add a column whose default value is not null by default to the table after particular column using Alter table.
ALTER TABLE tblechecklistrevision ADD COLUMN IWorkFlowOrder INT(10) DEFAULT NOT NULL AFTER fState;
When I Execute the query I will get the below 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 'NOT NULL AFTER fState' at line 1
You should remove DEFAULT:
ALTER TABLE tblechecklistrevision
ADD COLUMN IWorkFlowOrder INT(10) NOT NULL AFTER fState;
DEFAULT is for setting initial value to new rows where a value for that column isn't specified, when you write ...INT(10) NOT NULL what you mean is actually that that column can never contain a NULL, not only at initialization time.
If you want the default value not to equal NULL (example 0) you can do:
ALTER TABLE tblechecklistrevision
ADD COLUMN IWorkFlowOrder INT(10) NOT NULL DEFAULT 0 AFTER fState