I'm wondering if it's possible to change only comment on a column in mysql without change the column definition data like name, type and key.
Example:
ALTER TABLE `user` CHANGE `id` `id` INT(11) COMMENT 'id of user'
Can i change only the comment is this example ?
Thanks in adavance.
According to the MySQL specification, you must repeat the entire column definition if you want to redefine the comment:
ALTER TABLE user MODIFY id INT(11) COMMENT 'id of user';
Related
Hello I need to edit a database to add new columns. But I encountered a little problem, because I need to add comments to these new columns and I also need to add them after a certain column in the exist table.
Is this the correct syntax? :
ALTER TABLE `table`
ADD COLUMN `new` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '...' AFTER `secondlastcolumn`;
Or would this be correct? :
ALTER TABLE `table`
ADD COLUMN `new` VARCHAR(50) NOT NULL DEFAULT '' AFTER `secondlastcolumn` COMMENT '...';
Or is there another way of doing this?
I couldn't find a answer with an ADD COLUMN, only for modifying columns.
ALTER TABLE Statement defines ADD COLUMN definition as
ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name]
CREATE TABLE Statement defines COMMENT is a part of column_definition.
So COMMENT then AFTER.
I have the following column
`Equipamento_Solucao_id` VARCHAR(32) NOT NULL,
I would like it to be
`Equipamento_Solucao_id` VARCHAR(32) DEFAULT NULL,
How can I do this without changing my database model, that is, with a sql query?
You would use an alter table statement. A typical method would be:
alter table t alter column Equipamento_Solucao_id VARCHAR(32) DEFAULT NULL;
You could also look through the system tables on your database, find the not-null constraint, and then drop it specifically.
Can someone please tell me why this happens. The original problem is as always much more complex, but I've created a simple test case to reproduce the issue. First you need a table like so:
CREATE TABLE `test` (
`test_a` int(11) unsigned NOT NULL AUTO_INCREMENT,
`test_b` int(11) DEFAULT NULL,
PRIMARY KEY (`test_a`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Then you need to try to alter the table with this query:
ALTER TABLE `test`
CHANGE COLUMN `test_b` `new_test_b` INT(11) NOT NULL AFTER `test_a`,
CHANGE COLUMN `test_a` `new_test_a` INT(11) NOT NULL AUTO_INCREMENT FIRST;
and you get this result:
Unknown column 'test_a' in 'test'
I don't get it. It works fine when you do each alteration separately but if you do them together it blows up.
Addendum...
After studying this a bit, i figured out a few things. I took a guess that perhaps the compiler (or preprocessor or something) was evaluating the comma separated alter statements in reverse order, there-by changing the test_a column name before it got the the test_b (which would make the 'AFTER test_a' part not make sense.. This turned out to be wrong in testing because if you reverse the order of the statements like so:
ALTER TABLE `test
CHANGE COLUMN `test_a` `new_test_a` INT(11) NOT NULL AUTO_INCREMENT FIRST,
CHANGE COLUMN `test_b` `new_test_b` INT(11) NOT NULL AFTER `test_a`;
You end up with the same result.
Next I took the assumption that certain types of operations have precedence in a statement like this. I assumed that all CHANGE COLUMN operations must be taking place before any column ordering operations such as 'AFTER test_a' If this was the case, then it would make sense to reference the new column name in the ordering operation, like so:
ALTER TABLE `test`
CHANGE COLUMN `test_b` `new_test_b` INT(11) NOT NULL AFTER `new_test_a`,
CHANGE COLUMN `test_a` `new_test_a` INT(11) NOT NULL AUTO_INCREMENT FIRST;
This worked. So that must be the answer. My question i guess has evolved now to what is the order of precedence of the various types of operations.
Note, sorry I am not going to mark any of the current answers correct as they do not actually answer the question, they simply offer alternative ways to do the same thing (or state the obvious).
You can do it this way:
ALTER TABLE `test`
CHANGE COLUMN `test_b` `new_test_b` INT(11) NOT NULL AFTER `test_a`;
ALTER TABLE `test`
CHANGE COLUMN `test_a` `new_test_a` INT(11) NOT NULL AUTO_INCREMENT FIRST;
I create my borrowed table that keeps which users borrowed which books records of a library application.
I forgot to set not null When i create userID and bookID fields,
how i can add this feature ti this two fields?
I try this but failed:
Alter table borrowed set not null (userID,bookID);
In mysql you can add a costraint in an existing column with the command MODIFY:
ALTER TABLE borrowed MODIFY userID INT(11) NOT NULL;
ALTER TABLE borrowed MODIFY bookID INT(11) NOT NULL;
You can do so by using the CHANGE syntax separated by comma for multiple columns
ALTER TABLE `borrowed`
CHANGE `userID` `userID` INT(11) NOT NULL,
CHANGE `bookID` `bookID` INT(11) NOT NULL;
How do I change my column's default value from None to something else? For example, I want my dates to have a default value of 0000-00-00 if I don't specify one when I create the row.
I understand this in phpMyAdmin, but I'm not sure how to do it via command prompt.
I also understand how to do this when adding a column. But all of my columns are made and have data in some of them.
ALTER TABLE table1 ADD COLUMN foo INT DEFAULT 0;
From searching, I found this line, but I'm not sure if that's what I want?
ALTER TABLE foobar_data MODIFY COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';
Use ALTER TABLE to CHANGE or MODIFY the DEFAULT value of column. Check this link ALTER TABLE SYNTAX
ALTER TABLE `tableName` CHANGE `columnName` `columnName` DATE DEFAULT '0000-00-00';
ALTER TABLE `tableName` MODIFY `columnName` DATE DEFAULT '0000-00-00';
try this
ALTER TABLE foobar_data CHANGE COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';
user CHANGE to alter a existing column
see Link
In my case, it worked
ALTER TABLE `table_name` CHANGE `col_name` `col_name_again` VARCHAR(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'anydefault_text';
Hope it helps you too.