How do I alter table VARCHAR mysql Ruby on Rails? - mysql

I used the code below to change VARCHAR from (20) to (40) but no change happened in my table:
mysql> ALTER TABLE create_user modify email VARCHAR(40);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

But i use your code can accomplish.
mysql> desc create_user;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| email | varchar(40) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> Alter table create_user modify email varchar(20);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc create_user;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| email | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

As per my comment you should run commit but that also means your auto commit settings are different from the default. you may want to look into that further to prevent future headaches
https://dev.mysql.com/doc/refman/5.7/en/commit.html

Related

full text INDEX can not search the content with "," in it (mysql5.7.20, MyISAM)

I have create the full text index. (mysql 5.7.20, MyISAM)
I have modified the ft_stopword_file to '', and then restarted the server, recreated the table.
But still I can not search the word with "," in it, as below:
mysql> show create table tmp;
+-------+-----------------------------+
| Table | Create Table |
+-------+------------------------------+
| tmp | CREATE TABLE `tmp` (
`book_name` char(32) NOT NULL,
FULLTEXT KEY `book_name` (`book_name`) /*!50100 WITH PARSER `ngram` */
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 |
+-------+---------------------------------+
1 row in set (0.00 sec)
mysql> select * from tmp;
+-----------------+
| book_name |
+-----------------+
| hi,there |
+-----------------+
1 rows in set (0.00 sec)
mysql> show variables like '%ngram%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| ngram_token_size | 2 |
+------------------+-------+
1 row in set (0.01 sec)
mysql> show variables like '%stopword%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| ft_stopword_file | |
| innodb_ft_enable_stopword | ON |
| innodb_ft_server_stopword_table | |
| innodb_ft_user_stopword_table | |
+---------------------------------+-------+
mysql> select book_name from tmp where match(book_name) against('"hi,there"' in boolean mode);
Empty set (0.00 sec)
Why?

Why does `SET NAMES utf8` change the behaviour of `REPLACE(uuid(),...)` calls?

While investigating an issue with a failing migration I found out the following strange behaviour. Using SET NAMES utf8 on my client session changes the behaviour of REPLACE(uuid(),'','') calls.
mysql> select replace(uuid(),'','') from mysql.user;
+--------------------------------------+
| replace(uuid(),'','') |
+--------------------------------------+
| 4b483d57-ecdc-11e8-844f-0242ac120002 |
| 4b483d57-ecdc-11e8-844f-0242ac120002 |
| 4b483d57-ecdc-11e8-844f-0242ac120002 |
+--------------------------------------+
3 rows in set (0.00 sec)
mysql> set names utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> select replace(uuid(),'','') from mysql.user;
+--------------------------------------+
| replace(uuid(),'','') |
+--------------------------------------+
| 539c0b5c-ecdc-11e8-844f-0242ac120002 |
| 539c0b79-ecdc-11e8-844f-0242ac120002 |
| 539c0b7f-ecdc-11e8-844f-0242ac120002 |
+--------------------------------------+
3 rows in set (0.01 sec)
As you can see the generated UUID's are unique only after setting NAMES to utf8. The way I found out about SET NAMES utf8 was passing the query through MySQL Workbench.
I would greatly appreciate if someone here can explain how character sets (NAMES) influence the output of REPLACE(UUID(), ...) calls. Thanks in advance.
Update: adding a snippet to prove that the problem 1) is not with UUID() generating non-unique values and 2) relates to utf8mb4 charset
mysql> SHOW VARIABLES LIKE 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
mysql> select uuid, replace(uuid,'','') from ( select uuid() as uuid from mysql.user) tmp;
+--------------------------------------+--------------------------------------+
| uuid | replace(uuid,'','') |
+--------------------------------------+--------------------------------------+
| e41d2fe4-ed70-11e8-844f-0242ac120002 | e41d2dc2-ed70-11e8-844f-0242ac120002 |
| e41d3042-ed70-11e8-844f-0242ac120002 | e41d2dc2-ed70-11e8-844f-0242ac120002 |
| e41d309c-ed70-11e8-844f-0242ac120002 | e41d2dc2-ed70-11e8-844f-0242ac120002 |
+--------------------------------------+--------------------------------------+
3 rows in set (0.00 sec)
mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> select uuid, replace(uuid,'','') from ( select uuid() as uuid from mysql.user) tmp;
+--------------------------------------+--------------------------------------+
| uuid | replace(uuid,'','') |
+--------------------------------------+--------------------------------------+
| e9059092-ed70-11e8-844f-0242ac120002 | e9059117-ed70-11e8-844f-0242ac120002 |
| e90591a1-ed70-11e8-844f-0242ac120002 | e905923e-ed70-11e8-844f-0242ac120002 |
| e9059380-ed70-11e8-844f-0242ac120002 | e90593e1-ed70-11e8-844f-0242ac120002 |
+--------------------------------------+--------------------------------------+
3 rows in set (0.00 sec)
mysql> set names utf8mb4;
Query OK, 0 rows affected (0.00 sec)
mysql> select uuid, replace(uuid,'','') from ( select uuid() as uuid from mysql.user) tmp;
+--------------------------------------+--------------------------------------+
| uuid | replace(uuid,'','') |
+--------------------------------------+--------------------------------------+
| ef564f32-ed70-11e8-844f-0242ac120002 | ef564d0c-ed70-11e8-844f-0242ac120002 |
| ef564fa4-ed70-11e8-844f-0242ac120002 | ef564d0c-ed70-11e8-844f-0242ac120002 |
| ef565019-ed70-11e8-844f-0242ac120002 | ef564d0c-ed70-11e8-844f-0242ac120002 |
+--------------------------------------+--------------------------------------+
3 rows in set (0.00 sec)
Update 2: Adding EXPLAIN queries below to trace the actual SQL code as suggested by #qbolec. That reveals that the use of CONVERT(... using ...) is the culprit for the non-unique UUIDs. I still do not exactly understand why as this is not the behaviour I expect from CONVERT() func.
mysql> set names utf8mb4;
Query OK, 0 rows affected (0.00 sec)
mysql> explain select replace(uuid(),'','') from mysql.user;\W
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | user | NULL | index | NULL | PRIMARY | 276 | NULL | 7 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
Note (Code 1003): /* select#1 */ select replace(convert(uuid() using utf8mb4),'','') AS `replace(uuid(),'','')` from `mysql`.`user`
Show warnings enabled.
mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> explain select replace(uuid(),'','') from mysql.user;\W
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | user | NULL | index | NULL | PRIMARY | 276 | NULL | 7 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
Note (Code 1003): /* select#1 */ select replace(uuid(),'','') AS `replace(uuid(),'','')` from `mysql`.`user`
Show warnings enabled.

Is it possible to have an empty MySQL numeric field?

I want to have an empty field on a MySQL Numeric field. If I define the field to allow a NULL value it defaults to 0.00. Sometimes for this row item I prefer no value. I could probably create a different table to track these few items, but at this point I prefer a one table solution.
Because you did this with DEFAULT. Don't do that:
create table t1
( id int auto_increment primary key,
thing varchar(100) not null,
anInt NUMERIC(5,2) NULL DEFAULT 0
);
insert t1(thing) values ('fish');
select * from t1;
+----+-------+-------+
| id | thing | anInt |
+----+-------+-------+
| 1 | fish | 0.00 |
+----+-------+-------+
Works for me on mysql 5.7.12:
mysql> create table X (a double null);
Query OK, 0 rows affected (0.01 sec)
mysql> desc X;
+-------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| a | double | YES | | NULL | |
+-------+--------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> alter table X add column b int;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc X;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| a | double | YES | | NULL | |
| b | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into X (a) values (1.3);
Query OK, 1 row affected (0.00 sec)
mysql> insert into X (b) values (1);
Query OK, 1 row affected (0.00 sec)
mysql> select * from X;
+------+------+
| a | b |
+------+------+
| 1.3 | NULL |
| NULL | 1 |
+------+------+
2 rows in set (0.00 sec)

Why does the record gets inserted when commit was not issued?

On running the following statements:
set autocommit = false;
start transaction;
insert into test_table (id,name) values('6','iqbal bano');
The entry gets inserted into the database, although I didn't issue the command commit. Is this behavior natural to mysql? If yes, why is it so? To my surprise, if I run 3 statements as:
set autocommit = false;
start transaction;
insert into test_table (id,name) values('7','chitra singh');
insert into test_table (id,name) values('8',nayaara);
The third statement, returns an error (because nayaara had to wrapped inside the quotes) but, the previous insert succeeds and entry with id 7 is created into the database. What is the reason for this? Is this the way the transaction will proceed?
Note: Autocommit is set to false, as shown in the above statements.
If you set the auto-commit to false and the inserts go through, its part of the transaction but doesn't get committed to the database. If you were to restart the database the inserts would be lost; the transaction is rolledback since you did not say commit. However if you commit and then restart the records are not lost.
mysql> set autocommit=false;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test (id) values (5),(6),(7),(8);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from test;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
+------+
8 rows in set (0.00 sec)
mysql> exit
Bye
mysql> select * from test;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)
mysql> insert into test (id) values (5),(6),(7),(8);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from test;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
+------+
8 rows in set (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
+------+
8 rows in set (0.00 sec)

MySQL: Strange AUTO_INCREMENT

Table looks like:
mysql> DESC text;
+-----------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| text | varchar(255) | YES | | NULL | |
+-----------------+--------------+------+-----+-------------------+----------------+
2 rows in set (0.00 sec)
and AUTO_INCREMENT is 1:
mysql> ALTER TABLE text AUTO_INCREMENT = 1;
Query OK, 1 row affected (0.36 sec)
Records: 1 Duplicates: 0 Warnings: 0
but I get strange id like:
mysql> SELECT id FROM text;
+------------+
| id |
+------------+
| 2147483647 |
+------------+
1 row in set (0.00 sec)
What is the problem?
When you change the auto increment it is set to greatest(your_value,max(column)+ 1)
though I cant find the part in the docs which mention it, it is in the comments
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
ALTER TABLE text AUTO_INCREMENT = 1;
then check the result of
SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND TABLE_NAME = 'text';
to confirm that its not actually 1