Home » Programming » Databases

Copy a Table in MySQL/MariaDB – How To, With Examples

Mysql MariaDB Copy Table

This article will show you the BEST way to copy a table, with or without the data in it, in MySQL and MariaDB. Copying the Table with All Data in MySql The following code will copy a table including all data using the CREATE TABLE LIKE statement: CREATE TABLE new_table_name LIKE database_name.old_table_name; INSERT new_table_name SELECT * FROM database_name.old_table_name; There are other methods, some single line, but this is probably the best and simplest one. Why did I choose this method? Because it copies the table … Read more

Home » Programming » Databases

Merge Tables in MySQL (UNION/MERGE TABLES) – Tutorial

MySQL Merge Tables

There are one of two things you might be looking for – merging two MySQL tables, or the MERGE TABLE syntax – this article explains both, with examples. Merging Tables in MySQL (Moving Data From One Table Into Another) A note before I get to the code – if you’re moving data from one table to another, you should take into account the contents of the data and ensure that it will still be valid when merged with the second data set. This is particularly important if … Read more

Home » Programming » Databases

Show Privileges in MySQL/MariaDB using SHOW GRANTS, With Examples

MySQL Show Privileges

This article will show you, with examples, how to show what database privileges users have in MySQL and MariaDB. List All Users To show the privileges for a user, you need to be able to query the user’s name. Here’s how to generate a list of all users on a MySQL server: SELECT user FROM mysql.user; …and here’s how to list all users, with the host they are allowed to connect from: SELECT user, host FROM mysql.user; Both of the above queries pull information from … Read more

Home » Programming » Databases

The MySQL/MariaDB ‘SHOW INDEX’ Statement, With Examples

MySQL SHOW INDEX

This article shows you how to use the MySQL SHOW INDEX statement to list the details of indexes and keys in a table. To use the SHOW INDEX statement, you will need to know which database and table you wish to view index information for. Looking to list the databases and tables on your system? What Are Indexes? Indexes are a tool that allows a database to quickly lookup data in a table for a certain column. Usually, when searching a table, the database software must read every row in … Read more

Home » Programming » Databases

The MySQL/MariaDB ‘DISTINCT’ Statement, With Examples

MySQL Distinct

The MySQL DISTINCT operator returns only unique values from a column from a database table; duplicates are not returned.  Here’s how to use it. The DISTINCT operator can be useful in many scenarios; for example, you may want to generate a list of countries you have shipped to, or you may want to provide a drop-down menu containing unique product options for the user to select. Example Usage of MySQL DISTINCT Examples will use the following table: table_orders: order_id shipping_country product_category 1 Australia soap 2 China … Read more

Home » Programming » Databases

The MySQL (and Maria DB) TRUNCATE Command, With Examples (And Warning!)

MySQL Truncate

MySQL (and its functional equivalent, MariaDB) have two things named TRUNCATE – so watch out which one you want to use or suffer dire consequences! This article outlines the difference and usages of the MySQL TRUNCATE function and TRUNCATE TABLE statement – they share similar names but do very different things. TRUNCATE Function – Truncating a Number to a Certain Number of Decimal Places First, the TRUNCATE function. The TRUNCATE function reduces the number of decimal places for a given number. MySQL TRUNCATE Function Syntax TRUNCATE(number, decimals) Note that: number is a number decimals is an integer … Read more

Home » Programming » Databases

The MySQL LIMIT and OFFSET Clauses [with Examples]

MySQL LIMIT and OFFSET Clauses

This tutorial covers limiting the number of results from a MySQL database query using the LIMIT clause and skipping results using the OFFSET clause. This is especially useful when wanting to paginate results in web applications – spreading them out over several pages by retrieving a subset of the database results. Examples Examples will use the following table and data: table_people id name 1 Bob 2 Ted 3 Jim 4 Pip 5 Zak 6 Tim LIMIT Here’s how to limit the results to 2 records using LIMIT: SELECT * FROM table_people LIMIT … Read more

Home » Programming » Databases

MySQL CASE Statement and CASE Operator – Difference and Examples

MySQL CASE Statement and CASE Operator

There are two things called CASE in MySQL (and, by extension, the compatible MariaDB). The CASE Operator, for use in queries. The CASE Statement, for use in stored programs and procedures. Both have similar syntax and the same purpose. Both MySQL CASE Operator and Statement provide control over the results based on a set of conditions. Examples in this article will use the following test data: table_people id name 1 Bob 2 Ted 3 Jim 4 Pip 5 Zak 6 Tim CASE Operator The CASE operator is used … Read more

Home » Programming » Databases

MySQL vs MariaDB vs MongoDB vs PostgreSQL vs SQLite vs MS SQL – Which to Choose?

Which database to Choose

There is a lot of weird-sounding language around databases. Most of it centers on the names of the popular database software which is available. It can make it tough to choose. Each strangely named database solution offers features making it suitable for different tasks. This article should shed some light on what’s what. SQL Is a Language SQL (pronounced ESS-QUE-ELL or sequel depending on which side of the raging debate you side with). It stands for Structured Query Language. It is not a piece of software – it’s the … Read more

Home » Programming » Databases

How To Check your MySQL (Or MariaDB) Version [Easy]

Check MySQL Version

Knowing which version of MySQL you are running is vital when making sure your code is compatible and that you are using supported features. Here’s how to find out which you are running. For example, older versions of MySQL lack features for handling JSON data and modern character sets (including emojis!), so targeting the right MySQL version or making sure your MySQL version is up to date is pretty important if you want to support these popular functions! MariaDB is a drop-in replacement for MySQL, so … Read more