Professional Documents
Culture Documents
Best Practices For Writing MySQL Queries
Best Practices For Writing MySQL Queries
Best Practices For Writing MySQL Queries
MySQL queries
Tips, tricks, and best practices to apply
SQL DELETE 08
SQL REPLACE
5 tips for finding and replacing text in MySQL
USE sakila;
SELECT email,
-- Output:
MARY.SMITH@sakilacustomer.org
SELECT REPLACE('know the unknown','know ','foresee');
MARY_SMITH@sakilacustomer_org
SET @string := 'Cats are great pets and so easy to take care of. They make good companions. Having a cat around is good for children.';
SELECT REPLACE(@string,'cat','dog');
-- Output: Cats are great pets and so easy to take care of. They make good companions. Having a dog around is good for children.
-- If you capitalize the word "cat" in the query (i.e., "Cat"), the replace operation will not commence.
SELECT REPLACE(@string,'Cat','dog');
-- Output: dogs are great pets and so easy to take care of. They make good companions. Having a cat around is good for children.
02
DECLARE @string = 'Cats are great pets and so easy to take care of. They make good companions. Having a cat around is good for children.';
SELECT REPLACE(REPLACE(@string,'cat','dog'),'Cat','Dog');
-- Output: Dogs are great pets and so easy to take care of. They make good companions. Having a dog around is good for children.
UPDATE customer
-- Service script:
-- When an error of any kind occurs, the operation will roll back,
DELIMITER $$
BEGIN
DECLARE ct int;
BEGIN
ROLLBACK;
terminated';
END;
START TRANSACTION;
FROM payment p
UPDATE customer
UPDATE payment
SET avg_flag = 1
COMMIT;
END
$$
DELIMITER ;
04
CASE
START TRANSACTION;
END rating
UPDATE customer
UPDATE payment
SET avg_flag = 1
CASE
COMMIT;
END rating
ADD COLUMN summary decimal(5, 2) DEFAULT NULL,
ON c.customer_id = r.customer_id
05
-- OR
-- Create a result table with PRIMARY KEY (rental_id) or UNIQUE INDEX (rental_id):
FROM rental_double;
3 Use GROUP BY
4 Use WHERE IN
-- Create a test table with an extra column and duplicate records:
SET @id:=0;
FROM rental;
SET @id:=0;
FROM rental;
staff_id, last_update
FROM rental;
staff_id, last_update
FROM rental_double
FROM rental;
staff_id, last_update
FROM rental_double
last_update
MySQL developers
?
WHERE p.customer_id = 2
FROM staff s
DELETE FROM payment
LIMIT 3;
INNER JOIN staff s
AND s.store_id IN (1,2)
ADD INDEX data_dict_dn USING HASH (data_name);
-- Using CREATE TABLE only:
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
ADD INDEX zips((CAST(custinfo->'$.zipcode' AS UNSIGNED ARRAY)));
CREATE INDEX zips
CREATE SPATIAL INDEX idx_location ON address (location); ON customers ((CAST(custinfo->'$.zipcode' AS UNSIGNED ARRAY)));
11
9 Create a unique composite index 10 Create a composite index with a multi-valued part
ADD UNIQUE INDEX UK_address (address, phone); id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
custinfo JSON);
ALTER TABLE customers
CONSTRAINT fk_address_city FOREIGN KEY (city_id) REFERENCES city (city_id) ON DELETE RESTRICT ON UPDATE CASCADE);
12
IN i_index_name VARCHAR(128))
BEGIN
SET @indexExists = 0;
SELECT 1
INTO @indexExists
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_NAME = @tableName
14 Drop an index
SET @query = CONCAT('DROP INDEX ', @indexName, ' ON ', @tableName); -- Using ALTER TABLE:
IF @indexExists THEN
EXECUTE stmt;
DROP INDEX idx_location;
DEALLOCATE PREPARE stmt;
END IF;
-- Using DROP INDEX:
END
$$
DROP INDEX idx_location
DELIMITER; ON address;
ADD
WITH
FULLTEXT INDEX IDX_address_address (address)
PARSER NGRAM;
16 Rename an index
ALTER TABLE address
SQL formatting
It is easy to improve the readability, consistency, and standardization of your
DOWNLOAD FREE 30-DAY TRIAL code with the rich SQL formatting options offered by the Studio. Depending
on your needs, you can apply automatic, manual, or wizard-aided formatting.
14
I WANT TO TRY!
15
Helpful resources
Here are a few bonuses to help you expand your MySQL skills.
Get started with dbForge Studio for free today! DOWNLOAD FREE 30-DAY TRIAL