Showing posts with label Data manipulation. Show all posts
Showing posts with label Data manipulation. Show all posts

Truncate vs Delete vs Drop: A Side-by-Side Comparison

When working with databases in SQL, there are several ways to remove data from a table or delete the table itself. However, not all of these methods are created equal, and it's important to understand the differences between them. In this post, we'll compare three common SQL statements for removing data or tables: TRUNCATE, DELETE, and DROP.

StatementDescriptionSpeedLoggingStorage SpaceRollback
TRUNCATERemoves all data from a table, but leaves the table structure intact.FastestNo loggingUses minimal storage spaceCannot be undone
DELETERemoves rows from a table one by one.Slower than TRUNCATELogs each row deletionUses more storage spaceCan be undone
DROPDeletes the entire table, including all data and the table structure.SlowestNo loggingUses maximum storage spaceCannot be undone

TRUNCATE

The TRUNCATE statement removes all data from a table, but leaves the table structure intact. Here's an example:

TRUNCATE TABLE my_table;

This statement is much faster than using the DELETE statement to remove data from the table because it doesn't log each row deletion, and it doesn't use as much storage space. However, it cannot be undone, and it requires the user to have the DROP privilege.

DELETE

The DELETE statement is used to remove rows from a table one by one. Here's an example:

DELETE FROM my_table WHERE id = 123;

This statement is slower than TRUNCATE, but it's more flexible. You can use it to delete specific rows based on criteria, or you can delete all rows in a table if you don't include a WHERE clause. It also allows you to roll back the changes if needed.

DROP

The DROP statement deletes the entire table, including all data and the table structure. Here's an example:

DROP TABLE my_table;

This statement is the most destructive, and it cannot be undone. It's useful if you need to completely remove a table and its data from a database. However, you should be very careful when using this statement, as it permanently removes all data in the table.

Conclusion

In conclusion, TRUNCATE, DELETE, and DROP statements are all used to remove data from a SQL table or delete the table itself. However, each statement has its own use case and should be used carefully. Understanding the differences between these statements will help you choose the right one for your specific use case and avoid making any costly mistakes.

References

Filtering PHP Arrays Made Easy with array_filter()

Introduction

PHP is a popular server-side scripting language used for web development. It provides a rich set of built-in functions and features that make it easy to manipulate arrays, among other data structures. One such function is array_filter(), which allows you to filter the elements of an array based on a condition or set of conditions. In this blog, we'll take a closer look at how array_filter() works and provide some sample code to demonstrate its use.

What is array_filter()?

array_filter() is a built-in PHP function that allows you to filter the elements of an array based on a callback function. The callback function determines which elements to keep in the filtered array by returning a boolean value. If the function returns true, the element is included in the filtered array; if it returns false, the element is excluded.

The syntax for array_filter() is as follows:

array_filter(array $array [, callable $callback [, int $flag = 0 ]]) : array

The first argument is the array that you want to filter. The second argument is an optional callback function that will be used to filter the array. The third argument is an optional flag that can be used to modify the behavior of the function.

Sample Code

Let's take a look at some sample code to see array_filter() in action. Suppose we have an array of numbers and we want to filter out all of the odd numbers. We can use array_filter() to do this as follows:

$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9); 
$even_numbers = array_filter($numbers, function($number) { 
    return $number % 2 == 0; 
}); 
print_r($even_numbers);

In this example, we define an array of numbers and then use array_filter() to create a new array called $even_numbers that contains only the even numbers from the original array. We do this by passing in a callback function that checks whether each number is even using the modulus operator (%). If the number is even, the function returns true and the number is included in the filtered array.

Now suppose we have an array of names and we want to filter out all of the names that start with the letter "J". We can use array_filter() again as follows:

$names = array("John", "Jane", "Jim", "Bob", "Sue"); 
$non_j_names = array_filter($names, function($name) { 
    return substr($name, 0, 1) != "J"
}); 
print_r($non_j_names);

In this example, we define an array of names and then use array_filter() to create a new array called $non_j_names that contains only the names that do not start with the letter "J". We do this by passing in a callback function that checks the first letter of each name using the substr() function. If the first letter is not "J", the function returns true and the name is included in the filtered array.

References

For more information about array_filter(), check out the PHP documentation:

Here are some book references on PHP that cover array_filter() in depth:

  1. "PHP 7 in easy steps" by Mike McGrath - Chapter 9 covers PHP arrays and includes a section on array_filter() with clear examples.

  2. "PHP for the Web: Visual QuickStart Guide" by Larry Ullman - Chapter 7 covers PHP arrays and includes a section on array_filter() with detailed examples and explanations.

  3. "Modern PHP: New Features and Good Practices" by Josh Lockhart - Chapter 5 covers PHP arrays and includes a section on array_filter() with practical examples and best practices.

  4. "PHP and MySQL Web Development" by Luke Welling and Laura Thomson - Chapter 5 covers PHP arrays and includes a section on array_filter() with examples and explanations.

These books are great resources for learning more about PHP arrays and how to use array_filter() effectively.