Replace function usage in mysql

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff. # The result can only be seen as that all data in replace is strings. (In my opinion only) # updatemytablesetnamereplace (name, Wang erxiao, Wang Tiezhu); # This method cannot be passed # updatemytablesetcountreplace (cou

Welcome to the Linux community forum and interact with 2 million technical staff> enter # For such a result, it can only be seen that all data in replace is strings. (In my opinion only) # update mytable set name = replace (name, Wang erxiao, Wang Tiezhu); # This method cannot be used # update mytable set count = replace (cou

Welcome to the Linux community forum and interact with 2 million technicians>

# For such a result, it can only be seen that all data in replace is strings. (Personal opinion only)

# Update mytable set name = replace (name, Wang erxiao, Wang Tiezhu); # This method cannot be used

# Update mytable set count = replace (count, '20160301', '20160301 ');

Mysql> update mytable set count = replace (count, '20140901', '20160901 ');

Query OK, 3 rows affected (0.00 sec) # It is indeed the expected result

Rows matched: 11 Changed: 3 Warnings: 0

# Therefore, data in replace is only a string

Mysql> update mytable set count = replace (count, '20140901', '20160901 ');

Query OK, 6 rows affected (0.00 sec) # prove the result again

Rows matched: 11 Changed: 6 Warnings: 0

# In summary, replace can be used to replace multiple data in a field or different fields.

# In mysql, replace can act as insert within a certain range, and the syntax is similar

# Replace into mytable (name, count) values ('raffles ', '70'), ('Rio Tinto', '123 ');

Mysql> replace into mytable (name, count) values ('raffles ', '70'), ('Rio Tinto', '123 ');

Query OK, 2 rows affected (0.00 sec)

Records: 2 Duplicates: 0 Warnings: 0

# What if the database has similar data?

Replace into mytable (name, count) values ('laifu', '70 ');

Mysql> replace into mytable (name, count) values ('laifu', '70 ');

Query OK, 1 row affected (0.00 sec)

# Here we will look at the index of the table. It seems that the index is the default ID.

Mysql> show index from mytable;

+ --------- + ------------ + ---------- + -------------- + ------------- + ----------- + ----

--------- + ---------- + -------- + ------ + ------------ + --------- +

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Car

Dinality | Sub_part | Packed | Null | Index_type | Comment |

+ --------- + ------------ + ---------- + -------------- + ------------- + ----------- + ----

--------- + ---------- + -------- + ------ + ------------ + --------- +

| Mytable | 0 | PRIMARY | 1 | id | A |

14 | NULL | BTREE |

+ --------- + ------------ + ---------- + -------------- + ------------- + ----------- + ----

--------- + ---------- + -------- + ------ + ------------ + --------- +

1 row in set (0.00 sec)

# According to the document, if there is only one index, replace is equivalent to insert.

# Remember that I have created two tables. Now we switch to the mytab table with less data for Operation Demonstration:

Mysql> show tables;

+ ---------------- +

| Tables_in_mydb |

+ ---------------- +

| Mytab |

| Mytable |

+ ---------------- +

2 rows in set (0.00 sec)

# The default index is id.

Mysql> show index from mytab;

+ ------- + ------------ + ---------- + -------------- + ------------- + ----------- + ------

------- + ---------- + -------- + ------ + ------------ + --------- +

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardi

Nality | Sub_part | Packed | Null | Index_type | Comment |

+ ------- + ------------ + ---------- + -------------- + ------------- + ----------- + ------

------- + ---------- + -------- + ------ + ------------ + --------- +

| Mytab | 0 | PRIMARY | 1 | id | A |

3 | NULL | BTREE |

+ ------- + ------------ + ---------- + -------------- + ------------- + ----------- + ------

------- + ---------- + -------- + ------ + ------------ + --------- +

1 row in set (0.00 sec)

# Now I need to add an index to it (I am in trouble here. It seems that I am not very familiar with index building. I am ashamed of myself ).

# An error occurred while trying to create multiple indexes. Write it here for the moment...

Writing this is not about not going to work, but about the schedule of the company's specific tasks for next week.

[1] [2]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.