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

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)

SHOW INDEXES

Örnek
Şöyle yaparız
mysql> SHOW INDEXES FROM employee2 \G;
*************************** 1. row ***************************
        Table: employee2
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
      Visible: YES
   Expression: NULL
1 row in set (0.00 sec)

SET GLOBAL

Örnek
Şöyle yaparız
mysql> SET GLOBAL validate_password.policy = 0;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

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 co...