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' ;
Related
This is the code
CREATE TABLE `church` (
`ID` int(10) UNSIGNED NOT NULL,
`StudentID` int(11) NOT NULL,
`semesterID` int(11) NOT NULL,
`attendedWed` int(11) NOT NULL,
`attendedFri` int(11) NOT NULL,
`attendedSabM` int(11) NOT NULL,
`attendedSabE` int(11) NOT NULL,
`ChurchScore` double(10,2) GENERATED ALWAYS AS ((((((`attendedWed` + `attendedFri`) + `attendedSabM`) + `attendedSabE`) * 100) / 60)) STORED
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
This is the error
Operation failed: There was an error while applying the SQL script to
the database. Executing: ALTER TABLE citizenshipgroup3.church
CHANGE COLUMN ChurchScore ChurchScore DOUBLE(10,2) NULL DEFAULT
attendedWed ;
ERROR 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 'attendedWed' at line 2 SQL Statement: ALTER TABLE
citizenshipgroup3.church CHANGE COLUMN ChurchScore
ChurchScore DOUBLE(10,2) NULL DEFAULT attendedWed
Your syntax is fine. The problem is that MySQL does not support generated columns until 5.7. You are presumably using an earlier version.
Probably the simplest solution is to use a view for the calculation.
The error appears at the time of ALTER TABLE, when you are trying to modify the column ChurchScore setting the DEFAULT value to an expression involving another Column attendedWed.
Also, your error message seems to be originating from MariaDB, not MySQL.
From Mariadb Documentation:
From MariaDB 10.2.1 you can use most functions in DEFAULT. Expressions
should have parentheses around them. If you use a non deterministic
function in DEFAULT then all inserts to the table will be replicated
in row mode. You can even refer to earlier columns in the DEFAULT
expression:
CREATE TABLE t1 (a int DEFAULT (1+1), b int DEFAULT (a+1));
CREATE TABLE t2 (a bigint primary key DEFAULT UUID_SHORT());
So you need to ensure couple of things:
Upgrade your MariaDB version to 10.2.1 and above. Preferably, upgrade to current latest version (it is 10.3+ right now).
You need to use parentheses around the expression specified in the DEFAULT clause.
So the ALTER TABLE statement would look like:
ALTER TABLE `citizenshipgroup3`.`church`
CHANGE COLUMN `ChurchScore` `ChurchScore` DOUBLE(10,2) NULL
DEFAULT (attendedWed) ;
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 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
Is it possible to insert columns into a MySQL table??
I've created a table and named it "my_table" - I do not understand, why MySQL does not eats my syntax...
INSERT INTO "my_table"(
"item" char(1) NOT NULL DEFAULT '',
"price" int(10) NOT NULL DEFAULT '3000',
"level" int(10) NOT NULL DEFAULT '1000',
"super" char(1) NOT NULL DEFAULT '',
"play" char(1) NOT NULL DEFAULT ''
)
Error message:
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 '"my_table"( "item" char(1) NOT NULL DEFAULT '', "price" i' at line 1
So what's wrong with my syntax ?
If you're trying to add columns to an already created table you must use ALTER.
ALTER TABLE my_table ADD item char(1) NOT NULL DEFAULT '';
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
http://www.techiecorner.com/560/mysql-how-to-add-column-to-existing-table/
As the documentation says quite clearly, INSERT is for inserting rows of data, not altering the schema.
Look at ALTER instead.
And table/field names are delimited with backticks, not quotation marks.
I am using migration toolkit for the migration but i am getting these errors in the process of migration
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
Incorrect string value: '\xEF\xBF\xBDs d...' for column 'MESSAGE' at row 5
0 row(s) transferred.
For the fixing the first error i got something here http://terrencemiao.com/Webmail/msg00949.html
but i am not getting the second error what it is and why is it there how to fix it also suggest me some better ideas for fixing the first one if there any apart from what mentioned in the link
USE `MyDB`
Creating tables ...
Creating table MyTable...
DROP TABLE IF EXISTS `MyTable`
Creating table MyTable ...
SET NAMES UTF8;
CREATE TABLE `MyTable` (
`PrimaryKey` INT(10) NOT NULL AUTO_INCREMENT,
`FK_QUESTION_ID` INT(10) NOT NULL,
`ANSWER` LONGTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL
PRIMARY KEY (`PK_ID`)
)
ENGINE = INNODB
i am getting error for answer column
*Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause*
This is right, you should not create more then one such fields.
Incorrect string value: '\xEF\xBF\xBDs d...' for column 'MESSAGE' at row 5 0 row(s) transferred.
Possible encoding error, try to run 'SET NAMES UTF8;' before inserting data
Try this statement,
CREATE TABLE `MyTable` (
PK_ID INT(11) NOT NULL AUTO_INCREMENT,
FK_QUESTION_ID INT(11) NOT NULL,
ANSWER LONGTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
PRIMARY KEY (`PK_ID`)
)
ENGINE = INNODB;
You missed a comma and it was wrong field name. Be careful with migration toolkit. Check generated field types, for example if you do not need 4GB text values, you could use simple VARCHAR instead of LONGTEXT.