Is there any syntax error in this MySQL code? - mysql-error-1064

I would like to create a column in a table, with data type BLOB and attribute BINARY, however, MySQL always says that there is a syntax error in my code, I cannot see any.
My code is:
ALTER TABLE `Users` CHANGE `user_image` `user_image` BLOB BINARY NULL DEFAULT NULL;
And the error message is:
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 'BINARY NULL DEFAULT NULL' at line 1.

Modify existing column
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
Add a new column
ALTER TABLE table_name
ADD column_name datatype;

Related

Raw query to set AutoIncrement to false?

The id column in the student table is an auto incrementing one.I wanted to make that to non - autoincrementing. May i know, how can i modify the below query to work as such?
DB::statement("ALTER TABLE student SET AUTO_INCREMENT=FALSE;");
the above code shows the below error.
Illuminate\Database\QueryException
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 1: ALTER TABLE student SET AUTO_INCREMENT=FALSE;
^ (SQL: ALTER TABLE student SET AUTO_INCREMENT=FALSE;)
The correct syntax in PostgreSQL would be:
ALTER TABLE student ALTER COLUMN id DROP DEFAULT;
Where id is the serial column.
You might also want to drop the not null constraint :
ALTER TABLE student ALTER COLUMN id DROP NOT NULL;
This is an example of why posting table definitions (ddl) and Postgres version can be critical. #Zakaria is correct if the Postgres version is prior to version 10, or if version 10 or later and is still defined as serial/bigserial. However the preferred definition for version 10 and later is generated always as identity. If defined as identity you need:
alter table student alter column id drop identity.
I would not drop a not null constraint, and if it is the PK it is moot point as it will automatically be not null.

Add notnull constraint to a column of an existing table

How to add notnull constraint to a column of an existing table in which no value is yet inserted i.e. only table is created and now i want to alter the table
i am writing the following query
ALTER TABLE TABLENAME MODIFY COLUMNAME DATATYPE NOT NULL;
but oracle is throwing error "invalid alter table option"
In an alter table you only have to specify what changes
ALTER TABLE TABLENAME MODIFY COLUMNAME NOT NULL;
As you don't change the datatype, you don't have to specify it again.

Add enum type column to table

im trying to add a new column to my existing table 'Results', and it seems to be very easy but I cant see the mistake.
Here is my code:
SQL> Alter table results add column CAL ENUM('A','B');
ERROR at line 1: ORA-00904: : invalid identifier
What am I missing?
I've read this from w3 and this from java2s but cant see the difference to mine.
Thanks, and sorry for the dumb question.
OK, with an ORA- error I am assuming that this is an oracle database, and not mysql. you have both tags and you are linking to MySQL example, but the error is not a MySQL error.
Assuming that this IS an Oracle DB, then there is no native ENUM data type. You have to do this as follows: First - you add the column with a correctly defined data type, and second you create a constrained list of permitted values on that column as a check constraint.
Alter table results add (cal varchar2(1));
Alter table results add constraint chk_cal CHECK (cal in ('A','B')) ENABLE;
(EDITED to fix brackets in constraint creation line)

How to move columns in java DB Apache Derby?

I need to change specific column's position
I tried Ted Hopp's solution in Move Column in MYSQL
ALTER TABLE EMPLOYEES MODIFY COLUMN fname VARCHAR(25) AFTER password
and i got this error:
Error code -1, SQL state 42X01: Syntax error: Encountered "MODIFY" at line 1, column 22.
So any idea how to do it with Derby
Derby uses ADD COLUMN (docs):
ALTER TABLE EMPLOYEES ADD COLUMN fname VARCHAR(25)
There is no way to insert columns before or after a certain column; they are always appended.

Alter table after keyword in Oracle

ALTER TABLE testTable ADD column1 NUMBER(1) DEFAULT 0 NOT NULL AFTER column2;
Why can't I use mySql syntax in Oracle too? The above command works in MySql. Can you give me an equivalent that works?
Error report:
SQL Error: ORA-01735: invalid ALTER TABLE option
01735. 00000 - "invalid ALTER TABLE option"
I am asking if there is any way to use after clause in Oracle command that I provided?
Because SQL is a relational algebra. It doesn't care one bit about "where" columns are located within a table, only that they exist.
To get it to work in Oracle, just get rid of the after clause. The Oracle documentation for alter table is here but it boils down to:
alter table testTable
add ( column1 number(1) default 0 not null )
There is no after clause for the alter table command.
Oracle does not support adding columns in the middle of a table, only adding them to the end. Your database design and app functionality should not depend on the order of columns in the database schema. You can always specify an order in your select statement, after all.
However if for some reason you simply must have a new column in the middle of your table there is a work around.
CREATE TABLE tab1New AS SELECT 0 AS col1, col1 AS col2 FROM tab1;
DROP TABLE tab1 PURGE;
RENAME tan1New to tab1;
Where the SELECT 0 AS col1 is your new column and then you specify other columns as needed from your original table. Put the SELECT 0 AS col1 at the appropriate place in the order you want.
Afterwards you may want to run an alter table statement on the column to make sure it's the data type you desire.
Try this :
ALTER TABLE testTable ADD column1 NUMBER(1) DEFAULT 0 NOT NULL

Resources