One place for hosting & domains

      August 2022

      Hreflang Tags: Your Gateway to International SEO

      Continually working to optimize your website for search engine visibility will ensure that new users can find your website on the web. There are a range of techniques you can use to improve your website’s SEO rankings for a national audience, however when trying to target audiences in multiple countries or visitors who speak more than one language, you’ll need to follow a few additional rules — one of which involves adding hreflang tags to your website.

      Search engines read hreflang tags to ensure that your pages are indexed correctly for a defined international audience, and that those users are directed to the correct versions of your site. Fortunately, there are a handful of relatively simple, easy methods you can use to insert and leverage these attributes on your website.

      In this post, we’ll introduce you to hreflang tags and explain why they’re important. Then we’ll walk you through how you can implement them on your website. Let’s jump in!

      An Introduction to Hreflang Tags

      First introduced by Google in 2011, the hreflang attribute specifies the language of the content on a page.

      You can use hreflang tags to target specific countries. They are handy for websites offering content in multiple languages.

      Hreflang tags are added to the <head> section of your web pages’ HTML code. Each tag should specify the language and country for a particular page.

      For instance, the tags might look similar to the following:

      <link rel="alternate" hreflang="x-default" href="">
      <link rel="alternate" hreflang="en" href="">
      <link rel="alternate" hreflang="de" href="">

      In the above example, the first line is the default page for users who don’t speak any of the languages specified. The second and third lines are for English and German speakers, respectively. The hreflang attribute specifies the language, and the href attribute specifies the URL.

      It’s important to note that hreflang tags are only used by search engines — they won’t be visible to users. Furthermore, hreflang tags are just one type of tag that can improve your website’s SEO and User Experience (UX). Other common tags include title tags and meta descriptions.

      Also, keep in mind that hreflang tags and canonical tags are not the same. A canonical tag is an HTML element that tells search engines which version of a page to index.

      A hreflang tag, on the other hand, tells search engines which version of a page to serve to users in different countries. Canonical tags solve duplicate content issues. By contrast, hreflang tags ensure that users who speak different languages are served the correct page versions.

      Why Hreflang Tags Are Important

      Hreflang tags are important for a handful of reasons. They affect both the UX and SEO of your website.

      Benefits of Using Hreflang Tags

      • Ensure that your pages are indexed correctly.
      • Direct users to the correct versions of your site.
      • Lead to higher traffic levels and more conversions.
      • Organize your website.
      • Localize your content for global users.
      • Prevent competition between alternate web pages.

      In addition, hreflang tags can improve your Click-Through Rate (CTR) from Search Engine Result Pages (SERPs). Users who see your site is available in their languages are more likely to click on it. This can lead to higher traffic levels and, ultimately, more conversions.

      How to Use Hreflang Tags (3 Technical Methods)

      There are three main methods for implementing the hreflang attribute on your website. Below, we’ll walk you through each one.

      However, regardless of your chosen method, it’s important to understand three basic elements of hreflang tags. First, the language attribute must be in a two-letter country code known as the ISO 639-1 format. ISO is short for International Organization for Standardization. This value consists of one language, and can be combined with an (optional) region.

      You can use the ISO 3166-1 region codes to specify a region. It’s important not to get the two confused or use them interchangeably. For example, the code for the Greek language is “el”, but the code for the Greece region is “gr”.

      The second rule is that each URL needs to have return links pointing to the canonical versions. For instance, if you have 40 languages, you would have hreflang links for 40 URLs.

      Finally, you need self links for your hreflang tag implementation to work. This means that each language version must also list itself using a self-referential hreflang tag, which the page points back to itself.

      Now, here are three methods for using hreflang tags!

      1. Use HTML Tags in <head> Sections

      As we mentioned earlier, one of the ways to implement hreflang tags on your website is to insert them in HTML tags. This is often the quickest and easiest method.

      However, this process can be time-consuming if you have a long list of languages. You would need to link each variation to every other variation. Additionally, WordPress would have to make multiple database calls to generate these links.

      Ultimately, this could slow down your site. Therefore, if you have a larger website or want to create a long list of languages, you might want to use the sitemap method instead (see Method 3).

      To use this HTML tag method, you must insert your hreflang tags into the <head> section of each of your pages. For instance, if you wanted to add English and Spanish versions for the United States version of your site, you would add the following code:

      <link rel="alternate" hreflang="x-default" href="">
      <link rel="alternate" hreflang="en" href="">
      <link rel="alternate" hreflang="es" href="">

      In the above example, the first line refers to the default page. The second and third are for English and Spanish speakers.

      2. Insert Hreflang Tags in Your HTTP Headers

      If you want to add hreflang tags in non-HTML pages, such as PDFs, you won’t have HTML code to place the tags in. When this is the case, you can use your HTTP headers instead.

      The code would look something like the following:

      <http://example.pdf>; rel="alternate";hreflang="x-default",
      <http:/example.pdf>; rel="alternate";hreflang="en",
      <http://example.pdf>; rel="alternate";hreflang="es",
      <http://example.pdf>; rel="alternate";hreflang="de"

      In this example, you’re adding variants for English, Spanish, and German. Each of the respective versions must be placed in the headers of each PDF file.

      Get Content Delivered Straight to Your Inbox

      Subscribe to our blog and receive great content just like this delivered straight to your inbox.

      3. Add Hreflang Tags to Your Sitemap

      The third method for implementing hreflang tags is using XML sitemap markup. This approach will let you add the hreflang attributes for all your site’s pages in one place.

      It can also help you avoid slowing down your page loading speed (which might happen if you place the tags in the head section of the pages). Plus, changing a sitemap can be significantly easier than modifying each page’s <head> tag.

      The sitemap method is similar to the HTML <head> tag method, except the xhtml:link attribute adds the hreflang annotations to each URL. Following the xhtml:link attribute at the front of your URL, you would add the alternative page versions, so your markup would look similar to the following:

      <xhtml:link rel=”alternate” hreflang=”en-us” href=”x-default”
      <xhtml:link rel=”alternate” hreflang=”en” href=”” />
      <xhtml:link rel=”alternate” hreflang=”es” href=”” />

      Here, you’ll see that each URL has a hreflang attribute and return links to the other URLs. A separate <url> element should be created for every URL. Each element must include a <loc> that indicates the page URL. The URLs also need a <xhtml:link rel=”alternate” hreflang=”supported_language-code”> element that lists each alternative version of the page.

      You can add all of the necessary tags to one file. While the order of the elements doesn’t matter, we recommend keeping them uniform, because this layout will make it easier to check for mistakes. You can submit your new sitemap to Google Search Console when you’re done.

      Tools That Generate Hreflang Tags Automatically

      The time it takes to generate and implement your hreflang tags can vary depending on how many versions you want to create and which method you use. However, a handful of tools can simplify and speed up the process. Let’s take a look at some of the most popular ones!

      Weglot Translate Plugin

      Weglot is a popular and reliable translation tool that can add hreflang Google tags and markup to your website:

      The Weglot Translate WordPress plugin

      Weglot can be a helpful solution if you’re a beginner and unfamiliar with working with code. It automatically identifies href tags in your code during translation, and changes the page header links.

      Hreflang Checker

      It’s crucial to always double-check your hreflang tags to ensure that they’re working correctly after you’ve placed them. The Hreflang Checker tool provided by Weglot can help simplify this process:

      The Hreflang Checker tool

      To use it, just copy and paste the URL that you want to check into the text field (make sure to include “http://” or “https://”) and select a search engine bot to emulate. Then click on the Test URL button. It will display a search results page showing your tags’ status.

      XML Sitemap Generator

      If you want to implement hreflang tags using the sitemap method, you can utilize the Hreflang XML Sitemap Generator Tool. It was created by Erudite, a digital marketing agency:

      The Erudite Hreflang Sitemap tool

      After creating an account using your email address, simply tell the tool where to send your sitemap and upload your CSV file, including a column for each language. Then Erudite’s tool will automatically generate an XML sitemap.

      Hreflang Tags Generator

      If you need help generating the link elements for your hreflang tags, you can use the Hreflang Tags Generator:

      the Hreflang Tags Generator tool

      This tool, created by Aleyda Solis, can streamline creating tags for your multi-language or multi-country website. You can generate these tags by either adding the (up to 50) URLs to tag in the given form, or uploading them via a CSV file. Again, you’ll need to make sure that there is one column for the URLs.

      Ahrefs Google Sheets Template

      Another tool that you can use is the Google Sheets template provided by Ahrefs:

      The Hreflang planner Google Sheets template

      Under the Setup tab, select your site’s language-locale (default language), then choose up to four additional variations. For instance, you could pick English as the Default language-locale, followed by Spanish, German, Chinese, and Russian as the alternatives.

      Next, under the URLs tab, you’ll find five columns, each with its own header cells that correspond to the language you chose in the Setup tab. There should also be an X-Default column.

      Now paste the URLs into the respective cells. Then, under the Results tab, you’ll find an auto-generated code for your XML sitemap. You can copy and paste everything in the A column into an XML document and upload it via Google Search Console.

      Tips and Best Practices for Using Hreflang Tags

      Now that you understand the basics of hreflang tags and their implementation, let’s discuss some tips for using them. Below are some key best practices to follow!

      Make Sure the Tags Are Bidirectional

      Hreflang tags operate in pairs, meaning that they are bidirectional. When you add a hreflang tag to an English page pointing to a Spanish version, you also need to ensure that the Spanish variant returns with a hreflang tag pointing to the English version.

      This setup tells search engines the relationship is in agreement, and that you control both pages. If two pages don’t point to each other, Google will ignore the tags.

      Specify the Default Page for Users Who Don’t Speak Any of the Specified Languages

      Specifying the default page for users who don’t speak any of your set languages is important. It will ensure that visitors are directed to the correct version of your site.

      You can do this by adding a tag with the language code “x-default”. It would look something like:

      <link rel=”alternate” hreflang=”x-default” href=”” />

      The default page will be used in situations where Google is unable to extract the language or region for users when it’s not specified or listed. Instead, the x-default page will ask users which language they prefer, then point them to the appropriate alternate version.

      Use Absolute URLs

      Absolute URLs are complete URLs that include the domain name. They are also the preferred type of URL to use with hreflang tags. They are less likely to be affected by changes on your website, and make it easier for search engines to index your pages correctly.

      It’s important to make sure that your hreflang tags contain absolute URLs. In other words, the code should look like “” rather than “”.

      Make Sure Your Hreflang Tags Are Valid and Correctly Formatted

      As we mentioned earlier, the correct ISO language and region codes must be used when creating your hreflang link attributes. Otherwise, you may encounter a message in Google Search Console informing you that your site doesn’t have any hreflang language tags.

      Remember to use the ISO language codes for language attributes and the region codes for geographical locations. For instance, while “kr” is for the region of South Korea, “ko” is the code for the Korean language.

      Keep Your Hreflang Tags Up To Date

      As you continue adding content and pages to your website, keeping up with your hreflang tags becomes more critical.

      Let’s say you add a new domain for a particular country. Then you’ll need to add the appropriate hreflang tags to your existing pages, and check to ensure that they point to the new domain.

      The same applies if you delete any language versions of your website. If you remove a language, you’ll need to delete or replace the hreflang tags pointing to it. Pointing to missing or incorrect URLs can hurt both your UX and SEO.

      Monitor Your Website for Errors

      Just as essential as keeping your hreflang tags updated is consistently monitoring your website for errors. This process includes checking your site’s source code to make sure that all necessary tags are present.

      You can use a tool such as Google Search Console to monitor your website more easily. This platform will help you verify whether your pages are being indexed correctly.

      Optimize Your Website for International Audiences

      Using hreflang tags can improve the SEO and UX of your website for international audiences. These attributes can help your site reach users in different countries, and ensure that the correct content is served in their native or preferred languages.

      As we discussed in this article, there are three methods you can use to implement hreflang tags:

      1. Add the attributes to the <head> section of each page.
      2. Place them in the HTTP headers of non-HTML content pages.
      3. Put the tags in your XML sitemap, so that all the attributes are in one place.

      Do you need help optimizing your multilingual website? Check out our DreamHost Pro Services to learn how we can take your site to the next level!

      International SEO Made Easy

      We take the guesswork (and actual work) out of growing your website traffic with SEO.

      shared hosting

      Source link

      How to Translate a Website Quickly & Easily

      It probably won’t surprise you to learn that English is the most common language on the web. However, it’s far from the only one. In fact, nearly half of all internet users speak another language. This means that if you’re not translating your website, you may be losing out on potential conversions and revenue.

      Fortunately, translating your content is not as difficult as you might think. Plus, you don’t need to be multilingual to do it! Today, it’s easier than ever to build a website and make it available in other languages.

      In this post, we’ll discuss why you might want to create a multilingual site. Then, we’ll provide you with a list of solutions for translating your website quickly and easily, including using web browsers, online translator tools, plugins, and more. Let’s jump in!

      An Introduction to Website Translation

      When you translate a website, you essentially create a new version of the site in a different language. For instance, our DreamHost site is also available in Spanish.

      This process can be done manually, by hiring a professional translator. Alternatively, you might use an automatic translation tool.

      There are a few things to keep in mind when translating a website. First, you’ll want to make sure that all of the content on the site is translated accurately. This includes the text as well as any images or other multimedia elements.

      Additionally, it’s important to consider the overall design of the site. The goal is to create a version of the site that is easy for users to navigate and understand, regardless of their device or native language.

      You’ll also need to consider your site’s Search Engine Optimization (SEO). The translated pages should contain the right keywords so that users can easily find them when looking for information in their language.

      It’s worth noting that website translation is not the same as website localization. Localization is a more comprehensive process. It involves translating the site content and adapting the site’s design and functionality to better meet the needs of users in a specific region.

      Why You Might Want to Translate Your Website

      There are a number of reasons why you might want to translate your website. If you have an international audience, translating your site can help you cast a wider reach and better serve your users. It can also be a good way to expand into new markets.

      A multilingual site can help increase your conversions. Your website visitors will be more likely to convert if your site content is in a language that they understand.

      Translating your site can also show users that you are committed to inclusivity. It provides a good User Experience (UX) for everyone, regardless of their language. Making your site more accessible can help you build trust with your visitors and make them more likely to do business with you.

      Finally, translating your site can help improve your SEO. If you optimize your site for keywords in different languages, you can attract more traffic from users who are searching for articles in their native languages.

      Get Content Delivered Straight to Your Inbox

      Subscribe to our blog and receive great content just like this delivered straight to your inbox.

      How to Translate Your Website Quickly and Easily (10+ Methods)

      Translating an entire website can be time-consuming and costly, especially if it contains a lot of pages. Fortunately, there are a handful of tools that can help simplify the process. There are also multiple methods that you can use.

      Let’s look at 10+ ways to translate your website quickly and easily, with step-by-step instructions.

      Using Major Web Browsers

      Most web browsers enable you to translate your site in just a few clicks. Plus, this feature is normally available for free.

      However, the quality of the translation may not be very good. Therefore, it’s important to proofread the translated site carefully before making it live.

      Let’s look at how to translate your website using different browsers.

      1. Google Chrome

      Translating your site in Google Chrome is quick and easy. To start, click on the three dots in the upper right-hand corner and select Settings:

      Google Chrome settings

      Next, navigate to Advanced and select Languages and inputs > Languages. At the bottom, you’ll see an option that says Offer to translate pages that aren’t in a language you read. Click to enable this setting:

      The Offer Google Translate option in Chrome

      Now you can translate your website into foreign languages. Visit the web page you want to translate, then right-click and select Translate to English:

      The option to translate language in Chrome

      In the popup that appears, click on the three dots and select Choose another language:

      Choosing another language to translate in Chrome

      Then, you can choose an option from the dropdown menu. When you’re done, click on Translate. The web page will automatically be translated.

      2. Mozilla Firefox

      Unlike Chrome, Firefox doesn’t have a built-in translation feature. Instead, you’ll need to use an extension.

      A popular option is To Google Translate:

      The Firefox To Google Translate extension

      Select the Add to Firefox button to install the add-on. On the Options screen, you can choose specific languages:

      The To Google Translate options

      You can also leave the Translate Text field set to Auto to translate in different languages. When you’re done, select Save.

      Now, when you visit a web page that you want to translate, you can right-click on it and select Translate this page. This will open Google Translate.

      Another option is to change your Firefox settings. You can do this by clicking on the menu in the top right and navigating to Settings > Language:

      The Firefox language settings

      Choose the language you want. Then select the OK button.

      3. Microsoft Edge

      If you use Microsoft Edge, you can access a built-in translation tool similar to the one in Chrome. Open your site and click on the Translate icon in the address bar:

      Translating a web page in Microsoft Edge

      A popup will appear. Under Translate to, select the language from the drop-down menu and click on Done.

      4. Safari 

      Your options for translating a website in Apple Safari will depend on your device and your browser version. For example, you may be able to access the Translate button in the Smart Search field.

      If you’re using Safari on your iPhone, you can select the aA button in the address bar. This will open a menu where you can find and select the option to Translate to English.

      Using Online Translator Tools

      There are plenty of online translator tools that you can use. Many of them are free.

      These tools use Artificial Intelligence (AI) to translate a page into another language. The process is often quick.

      Unlike human translation, these tools won’t account for linguistic nuances. Therefore, you may need to check the translations to make sure they’re accurate.

      5. Google Translate

      Google Translate is one of the most popular online translator tools. You can use it to translate individual pages or your entire site into different languages:

      The Google Translate website

      On the main page, click on the Websites tab and enter your URL in the text field. Select the native language from the options on the left, then choose the new language you want to translate the content into. Next, click on Translate:

      Translating a website in Google Translate

      You should now be able to see the translated version of your site. If you want to change it to another language, you can select your preference from the drop-down menu.


      Another popular online translation tool you can use is

      The homepage

      It offers over 30 different languages and lets you use your voice or keyboard to input text. Then, you can read the translated text or listen to the audio version.

      The unedited versions of machine translation are free to use. You can also leverage the professionally edited translations for $0.07 per word.

      7. is a useful online translator that lets you choose from more than 50 languages:

      The website

      Start by entering your text in the provided box. Then, select the preferred language and click on the Translate button.

      When it’s finished, you can view the written text or select the sound icon to listen to it. You can also use the word and character count tools to keep track of the length of your content.

      Using WordPress Translation Plugins

      If you’re a WordPress user, you can also use plugins to translate your website. Let’s look at some popular options.

      8. Weglot

      Weglot is a robust solution that automatically translates your website into more than 100 languages:

      The Weglot website

      The service incorporates both human and machine translation solutions. You can use automatic translations, then manually edit your text for clarity. You’ll also have access to professional website translators.

      After you sign up for a Weglo account, you will receive a confirmation email with your API key. Then, you can install the Weglot Translate plugin on your WordPress site:

      The Weglot WordPress plugin

      After activating the plugin, navigate to Weglot and enter your API key. Next, select your Original Language (such as English) and set the Destination Languages:

      The Weglot plugin settings

      You can also choose specific pages to exclude from translation. When you’re done, click on the Save button. Now, when you view your site on the front end, you should see a language widget that lets you translate the page.

      The plugin is free. However, you can upgrade to a premium plan to access more features.

      9. Polylang

      Polylang is another WordPress plugin that enables you to translate your site:

      The Polylang plugin

      You can use it to set the language for each post and create translated versions in your WordPress editor. Once you install and activate the plugin, you can access its settings by navigating to Languages:

      The Polylang plugin settings

      The plugin is free. For more advanced features, you can upgrade to a pro plan.

      10. WPML

      WPML is one of the most popular translation plugins for WordPress:

      The WPML website

      This tool is intuitive and easy to use. However, it’s mostly focused on manual translation. Once installed and activated, you can assign the Translator role to specific users.

      The plugin supports more than 40 languages. It also lets you generate language variants.

      Bonus Option: Translate Your Website Using Custom Translation Services

      At this point, we’ve discussed a range of translation tools that you can use, including online translators and WordPress plugins. However, if you’re looking for high-quality translations, you might consider using a professional service such as Gengo or

      The website

      Of course, you’ll have to pay for this service. However, you can have peace of mind knowing that your translations are being handled by experts in those languages.

      This option can save you a lot of time and energy in the long run. You can contact the translators to learn more about their services and prices.

      Translate Your Website Today

      Not all of your site visitors are fluent in English. To avoid losing out on conversions, you might want to consider translating your content into different languages.

      As we discussed in this post, there are various methods you can use. For instance, you can use an online tool such as Google Translate or install a WordPress plugin like Weglot.

      Are you looking for additional ways to enhance your website? Check out our DreamHost Pro Services to learn more about our professional web design services!

      DreamHost Makes Web Design Easy

      Our designers can create a gorgeous website from SCRATCH to perfectly match your brand and vision — all coded with WordPress so you can manage your content going forward.

      custom website design

      Source link

      A Primer on SQL Transactions

      What are SQL Transactions?

      A SQL transaction is a grouping of one or more SQL statements that interact with a database. A transaction in its entirety can commit to a database as a single logical unit or rollback (become undone) as a single logical unit. In SQL, transactions are essential for maintaining database integrity. They are used to preserve integrity when multiple related operations are executed concurrently, or when multiple users interact with a database concurrently.

      In this Guide

      This guide demonstrates:

      Why are SQL Transactions Necessary?

      A database application has to account for every possible failure scenario while writing and reading from a database. Without SQL transactions, application code that protects database integrity would be complex and expensive to develop and maintain. With SQL transactions, application code and database maintenance can be simplified.

      Understanding SQL Transactions with an Example

      Consider a financial brokerage that maintains a database for its clients. The brokerage is required to generate a quarterly financial statement for each of its clients. The financial statement reports the statement balance, current values of holdings, and any transactions that occurred during the quarter. To generate the statement, you may need to process the following steps:

      1. Loop through each client’s transaction history account to ascertain and calculate each of the transactions that occurred during the quarter (both purchases and sales).
      2. Calculate each client portfolio’s total return for the quarter, along with year-to-date returns.
      3. Calculate each client’s taxable and non-taxable returns for the quarter.
      4. Insert a record into a statements table to record the prior three steps.
      5. Update the current portfolio holdings’ values and investment totals for the quarter in a quarterly_values table.
      6. Update the statement balance in an accounts table.

      Many read and write operations are required for the above steps. There are a number of scenarios where data integrity could be violated, including:

      • A transaction that falls within a reported quarter is backed out or changed after calculations have already been made

      • One of the updates noted above fails after we have already inserted a record into the statements table

      • The total statement balance cannot be updated

      Without SQL transactions, application code would need to account for every possible permutation of errors that could occur in the database. With SQL transactions, all of the above statements can be contained in a transaction block that either succeeds or fails.

      SQL Transaction Delimiter Syntax


      The implementation of transactions is very similar in concept across most database implementations, but the syntax can vary slightly.

      Typically, the beginning of a transaction in a SQL Server command line is defined using the BEGIN TRANSACTION statement:

      BEGIN TRANSACTION NameOfTransaction;

      In MySQL, the syntax is slightly different, but has the same meaning:


      Commit Transaction Syntax

      If a database application determines that all of the changes for a transaction have succeeded, the application can use the COMMIT TRANSACTION statement. This commits those changes to the database, and it is placed at the end of a block of statements. In SQL Server, the following command is used to commit the transaction:


      Alternatively, you can also use the below command. The following command can also be used in MySQL.


      Roll Back Transaction Syntax

      If a database application determines that a change in a transaction has failed, the application can use the ROLLBACK statement. This statement can effectively de-commit any statements that have already been executed since the beginning of the transaction. In SQL Server and MySQL, the following command is used to roll back a transaction:


      Database Transaction Examples

      To demonstrate the mechanism behind transaction processing, consider the example of a simple database named School. One of the tables in the database is named Course and is defined as follows:

      The Course table is created using the below SQL command:

      CREATE TABLE Course(
        CourseId   SMALLINT    NOT NULL;
        CourseName VARCHAR(40) NOT NULL

      The following examples demonstrate different ways to use SQL transactions from the command line.

      Example 1: Commit a Transaction

      The example below obtains the largest CourseId value from table Course and adds 1 to it. It then inserts a row into the Course table and commits the transaction. Before committing, if any part of the CourseAdd transaction fails to execute, then none of the transactions can be processed. That means if the Select or Insert statement fails in some capacity, the entire transaction is null and void.

      SELECT @CrsId = MAX(CourseId) + 1
      FROM Course;
      INSERT Course VALUES (@CrsId, 'Biology 101');

      Example 2: Roll Back a Transaction

      In the example below, although rows are manually inserted into the Course table, all the Insert statements are wrapped into a transaction. That way, if a transaction fails to execute, you can roll back the entire transaction using the following MySQL syntax:

      INSERT Course VALUES (1, 'Biology 101');
      INSERT Course VALUES (2, 'Computer Science 101');

      From the above MySQL syntax, you have ensured that the Insert statements have not committed (inserted) the data into the Course table until a Commit command is received. By issuing a Rollback statement, you have effectively undone the two prior Insert statements, and not committed either of these two rows to the database.

      Example 3: Combining Commit and Rollback in a Transaction

      The example below combines the ability to both commit and rollback transactions in the same transaction code block.

      BEGIN TRANSACTION InsertCourse;
      SELECT @CrsId = MAX(CourseId) + 1
      FROM Course;
      INSERT Course VALUES (@CrsId, 'Biology 101');
      IF (SELECT COUNT(CourseName)
         FROM Course
         WHERE CourseName="Biology 101") > 1
      END IF;

      The MySQL code above inserts a row in the Course table with the next highest CourseId. Before committing the transaction, the code checks if there are more than one rows where the CourseName is Biology 101. If true, the transaction is not committed to the database. At this point, the transaction rolls back and the code segment aborts from further processing. Otherwise, if the new row is the first instance of a CourseName of Biology 101, then the transaction proceeds and is committed to the database.

      More Benefits of Using Transactions

      When should you use transactions? Should you always use transactions?

      The simple answer is yes. This is especially true when you are dealing with multiple groups of statements. In a transaction, all of the statements in a sequence of statements must succeed for the associated data to be committed to the database. A failure of a component within the transaction necessitates a rollback.

      The use of transactions is also beneficial to protect against database failure conditions, including power failures, server crashes, disk drive failure, and database software crashes. In the event of one of these failures, if there are transactions that have not yet been committed, database integrity is maintained. Without transactions, any atomic statements that are applied to the database remain intact, regardless of whether associated statements have been executed. This may result in a data integrity issue.


      SQL transaction logic is a fundamental mechanism for ensuring database integrity and minimizing error handling logic that is required in a multi-user database application. The use of transactions in SQL environments guarantees accuracy and completeness.

      Source link