Modify your Database Records with the REPLACE SQL Keyword

normalization in sqlA normalized database makes life simple. Whether for work or for play, these wonders of technology organize our lives in more ways than we may realize. We can store just about anything in a database. We just have to set up the right tables to store the data, and then use the right SQL statements to retrieve them again. Unfortunately, like any tool, databases have their limits. We can’t store everything under the sun in them. However, we can store most of the things we need in a database, and use the replace SQL keyword when we need something different.

Brush up on  the fundaments of SQL at Udemy

The Replace SQL Function

The REPLACE SQL keyword lets you change something. The REPLACE SQL function lets you change how your data will be displayed. By far the most common use of the keyword, you will most likely come across it in your database programming career.

Available with nearly every database system, REPLACE lets you replace one string with another. This new string will be an updated version of the old string with some substring switched with another string.

In computer speak; strings are just sequences of characters. These characters can include the letters of the alphabets, numbers, and any punctuation mark. We use string to let our programs speak to us or display passages, words, or phrases. For instance, this entire blog article was created using strings. Substrings are strings contained within other strings. The string “a string” is a substring of the string “This is a string.”

Computers store strings as character arrays in their memory, and this lets us change these as we see fit. The REPLACE SQL function takes advantage of this system.

REPLACE takes in three strings as arguments and returns a string as a result.

REPLACE(string1, string2, string3)

String1 is our original string. String2 is the subtring we want replaced. String3 is the string that will replace string2 is the resultant string. This resultant string is represented by the REPLACE function itself. The following example shows how the REPLACE function works.

Let’s say your company has a database table called Geography that contains its major markets and their geographical location.

Table Geography

Region_NameMarket
EastBoston
EastNew York
WestLos Angeles
WestSan Diego

This table works great until we need to use different region names for a report. Instead of changing out table data, we can use the REPLACE SQL function to convert our region names to match the report requirements as we need them.

SELECT REPLACE(Region_Name, ‘ast’, ‘astern’) Region1 FROM Geography;

Result:

Region1
Eastern
Eastern
West
West

I wrote the SQL commands in capital letters per international convention, but you can use lower case letters as well.

select replace(Region_Name, ‘ast’, ‘astern’) Region1 from Geography;

That’s all you need to know to use the REPLACE SQL function. You give it the string you need changed along with the change you need made, and it will output the string with the changes. You can even use it in INSERT and UPDATE statements to translate your database table into another table to change its information.

INSERT Reports (Region_Name, Market) VALUES (REPLACE(Geography.Region_Name, ‘ast’, ‘astern’), Geography.Market);

UPDATE Geography SET Region_Name = REPLACE(Region_Name, ‘ast’, ‘astern’);

As REPLACE does not change your original string, you can play with it without causing any damage to your database. Feel free to play with it so you can become familiar with how it works and when to use it.

Learn how to use the INSERT and UPDATE SQL statements at Udemy

The REPLACE SQL Statement

You probably never have to use the REPLACE SQL statement. It’s a system-specific extension to the standard SQL language used with MySQL databases and other similar systems.

REPLACE lets you modify your database records by replacing the current records with new ones. Its syntax looks very similar to the syntax of the INSERT command statement.

REPLACE INTO table_name (column list) VALES (column values);

Column list and column values are comma-separated lists of your table column names and your record’s data respectively. Like with INSERT, the column list is optional, but you will have to explicitly include values for every column in the table in the values list in their proper order.

REPLACE INTO company VALUES (2, NOW(), NOW(), ‘Joe\’s Steak House’);

The REPLACE SQL statement does two things. It first checks to see if the record you want replaced already exists in the table. If it finds a match, the statement deletes the old record and then inserts the new record information in its place. It adds a new record if it can’t find anything.

As you can see, the REPLACE SQL statement is a DELETE statement and an INSERT statement in a single SQL statement. In fact, you need both the INSERT and DELETE privilege in order to even use REPLACE.

You might be asking why you would ever need such statement, especially if you can just use the UPDATE statement instead. The answer lies in copying one table to another.

As a database, MySQL is the leader in web application database solutions. Its simple streamlined processes make it best for these high volume, high performance environments.  However, most good programmers test their programs on test platforms before they make them public. This means your company’s database may have test table set aside for these development tests. If your new website software needs be tested on live data, you can use the REPLACE statement to copy data from your production table to your test table without additional work. You just use the appropriate SELECT statement as the column values list.

REPLACE INTO test_table VALUES SELECT * FROM products_table;

You can even use the REPLACE SQL function inside the embedded SELECT statement.

REPLACE INTO test_table VALUES SELECT *, REPLACE(Region_name, ‘ast’, ‘astern’) FROM company_network;

In either case, the REPLACE SQL keyword signifies that something is being replaced with something else. You may never have to use either forms, but you should never confuse the two. The REPLACE SQL statement works like an INSERT statement with built-in validation checks. The REPLACE SQL function transforms a string into a new one by replacing a substring with another string. Both exist to make your database development as simple as possible.

Learn how to create web applications with PHP programming and MySQL at Udemy