17 Temmuz 2024 Çarşamba

CREATE EVENT - Scheduled Task İçindir

Giriş
Açıklaması şöyle
Starting from MySQL 9.0.0, the following Event syntax is available:

CREATE EVENT (create event)
ALTER EVENT (modify event)
DROP EVENT (delete event)

EVENT statements do not support the use of placeholder parameters (?).
CREATE EVENT
İmzası şöyle
CREATE EVENT event_name
ON SCHEDULE AT or EVERY + specified interval DO SQL_statement
Örnek
Şöyle yaparız
CREATE EVENT myevent
    ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
    DO
      UPDATE myschema.mytable SET mycol = mycol + 1;

MySQL Sürümleri

Bazı bilgiler burada
MySQL 8 - April 19, 2018
Altı yıl aradan sonra 
MySQL 9 - July 2, 2024

4 Aralık 2023 Pazartesi

Soft Delete

Giriş
Açıklaması şöyle
When using the soft delete mechanism on the database, you might run into a situation where a record with a unique constraint was deleted and you want to reuse the unique value.
Virtual Columns
Açıklaması şöyle
If your database does not have partial indexes, e.g. MySQL, you could use a different approach with virtual columns.

Considering that in most SQL databases, the UNIQUE constraint ignores NULL values, we can use a composite key in the UNIQUE constraint that includes a nullable column that indicates if a record was deleted.

To do so, we can add a virtual column not_archived that gets its value from the soft delete column.
Şöyle yaparız
ALTER TABLE users
  ADD not_archived BOOLEAN
  GENERATED ALWAYS AS (IF(deleted_at IS NULL, 1, NULL)) VIRTUAL;
Açıklaması şöyle
This will result in a field that gets automatically updated according to the deleted_at column. It will have the value 1 when the record is not deleted (deleted_at=NULL), else it will be NULL. Now we just need to add it to the UNIQUE constraint. Remember to drop the old constraint.
Şöyle yaparız
ALTER TABLE users
ADD CONSTRAINT UNIQUE (email, not_archived);


18 Eylül 2023 Pazartesi

UUID metodu

Giriş
Açıklaması şöyle
For instance, the UUID() MySQL function returns a version 1 UUID number.

And the Java UUID.randomUUID() function returns a version 4 UUID number.

4 Eylül 2023 Pazartesi

29 Ağustos 2023 Salı

JDBC DatabaseMetaData.getTables metodu

Giriş
Bir makale burada. MySQL açısından bir veri tabanı şeklen şöyle
db 			<-- Ben yarattım
information_schema
mysql                   <-- MySQL kurulumu ile geliyor
performance_schema
sys
Açıklaması şöyle
The MySQL installation comes with default databases. One of the database is named 'mysql'.

db veri tabanını yaratmak için şöyle yaptım
CREATE TABLE IF NOT EXISTS db.myworker (...);
Örnek - catalog değeri geç
Elimizde şöyle bir kod olsun
try (Connection connection = ...;
  ResultSet tables = connection.getMetaData().getTables( connection.getCatalog(),
        null,
        null,
        new String[]{"TABLE", "VIEW"})) {
  ...
}
1. Eğer bağlantı şöyle ise yani veri tabanı belirtilmemişse 
jdbc:mysql://localhost:3306/
connection.getCatalog() çağrısı "" döner
connection.getSchema() çağrısı null döner
ResultSet te boş geliyor


2. Eğer bağlantı şöyle ise yani veri tabanı belirtilmişse
jdbc:mysql://localhost:3306/db
connection.getCatalog() çağrısı db döner
connection.getSchema() çağrısı null döner
ResultSet dolu gelir ve içinde sadece db veri tabanında yarattığım tabloları görürüm

Örnek - catalog null yani catalog değeri geçme
Elimizde şöyle bir kod olsun. Bu sefer catalog yerine null geçtim. 
try (Connection connection = ...;
  ResultSet tables = connection.getMetaData().getTables( null,
        null,
        null,
        new String[]{"TABLE", "VIEW"})) {
  ...
}
1. Eğer bağlantı şöyle ise yani veri tabanı belirtilmemişse 
jdbc:mysql://localhost:3306/
connection.getCatalog() çağrısı "" döner
connection.getSchema() çağrısı null döner
ResultSet  dolu gelir ve içinde sys ve db dahil her şeyi görürüm. Çünkü catalog değeri olarak null geçtim

2. Eğer bağlantı şöyle ise yani veri tabanı belirtilmişse
jdbc:mysql://localhost:3306/db
connection.getCatalog() çağrısı db döner
connection.getSchema() çağrısı null döner
ResultSet  dolu gelir ve içinde sys ve db dahil her şeyi görürüm. Çünkü catalog değeri olarak null geçtim

21 Ağustos 2023 Pazartesi

EXPLAIN ANALYZE

JSON Çıktı
Açıklaması şöyle
Starting from MySQL 9.0.0, it is now possible to save the JSON output of the EXPLAIN ANALYZE command into a user variable using the following new syntax:
EXPLAIN ANALYZE FORMAT=JSON INTO @variable select_stmt
Açıklaması şöyle
The INTO clause is only supported with FORMAT=JSON; FORMAT must be explicitly specified. This form of EXPLAIN ANALYZE also supports optional FOR SCHEMA or FOR DATABASE clauses.


Table Scan
Full Scan anlamına gelir. 
Örnek
Şöyle yaparız. id alanında index veya primary key olmadığı için tüm tabloyu tarar
mysql> explain analyze select * from employee1 where id = 3456;
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN                                                                                                                                                                                               |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Filter: (employee1.id = 3456)  (cost=1989 rows=1965) (actual time=5.24..29.3 rows=1 loops=1)
    -> Table scan on employee1  (cost=1989 rows=19651) (actual time=0.0504..27.3 rows=20000 loops=1)
 |
+--------------------------------------------------------------------------------------------------------
Index lookup
Örnek
Tablo için bir indeks yaratalım
CREATE INDEX index1 ON employee1 (FirstName);
Şöyle yaparız. FirstName alanında index olduğu için tüm indeksi tarar
mysql> explain analyze select * from employee1 where FirstName = 'user-13456';
+-------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN                                                                                                                             |
+-------------------------------------------------------------------------------------------------------------------------------------+
| -> Index lookup on employee1 using index1 (FirstName='user-13456')  (cost=0.35 rows=1) (actual time=0.0594..0.0669 rows=1 loops=1)
 |
+-------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

LIMIT ve Covering Index + Subquery

Örnek Şöyle yaparız SELECT t1.name, t1.age, t1.gender, t1.create_time FROM student as t1    INNER JOIN     (SELECT id FROM student ORDER BY ...