10 Aralık 2021 Cuma

STR_TO_DATE Metodu - String'i Date Nesnesine Çevirir

Giriş
String'i Date nesnesine çevirir. Oracle'daki TO_DATE() ile aynıdır. Veri tabanlarındaki 
DB Karşılaştırmalı Tarih Fonksiyonları(Oracle-Postgresql-Mssql-Mysql) yazısı da faydalı

Örnek
Şöyle yaparız
STR_TO_DATE('07,7,2022','%d,%m,%Y')
Örnek
Şöyle yaparız
STR_TO_DATE('12/31/2011', '%m/%d/%Y')
Örnek 
Şöyle yaparız
STR_TO_DATE ('...', 'yyyy-MM-dd HH24:MI:SS')



7 Aralık 2021 Salı

INFORMATION_SCHEMA Sistem Tablosu

Giriş
Açıklaması şöyle
It's a part of the SQL-92 standard, and it's implemented by most major database engines (with the notable exception of Oracle).

Database Discovery
Bir keresinde veri tabanını keşfedip Java nesneleri haline getiren bir kod ile çalışmıştım. Adımlar şöyleydi. Burada amaç primary key ve foreign key sütunları arasında çift yönlü ilişki kurmak

1. Select foreign keys
Foreign key kullanan tabloları buluyordu.
//Foreign key of all tables.
//Key : Table name
//Value : List of column names in other tables
Map<String, List<String>> fkMap = new HashMap<>();  
2. Select default values
Her sütun için varsa default değerleri buluyordu. 
//Default column value of all tables
//Key : Table name
//Value : Column name + Default value
Map<String, Map<String, String>> defaultsMap = new HashMap<>();
3. Select all views
Tüm view'lar dolaşılıyordu. Doldurulan veri şöyleydi.
Map<String, DbTable> viewsMap
4. Select all tables
Tüm tablolar dolaşılıyordu. Doldurulan veri şöyleydi
Map<String, DbTable> tableMap
DbTable şöyle
public class DbTable {

  //database name of the table
  private String name;

  //Map of tables that contains foreign key set on this table's Public Key
  private Map<String, IDbTable> subTables;

  // Map of fields this table contains
  private Map<String, IDbField> fields = new LinkedHashMap<String, IDbField>();

  ...
}
5. Select all columns + select all foreign keys
Tüm tablo ve view'ların sütunları için nesneler yaratılıyordu. Buradaki en önemli adım her sütun nesnesi için aynı zamanda foreign key ise atıfta bulunduğu tablo ve sütunlar da atanıyordu
DbField şöyle. Yani refTableName ve refColumnsNames dolduruluyor. 
Böylece foreign key <-> primary key çift yönlü ilişkisinin bir tarafı dolduruluyor
public class DbField {

  //database name of the field
  private String name;

  //Data type mapping info for this field.
  private DATATYPE dataType;

  private boolean isNullable;

  //Default value for the field
  private String defaultValue;

  //Flag indicating if this field is part of public key
  private boolean isPrimaryKey;

  //Flag indicating if this field is foreign key
  private boolean isForeignKey;

  //Name of the table which is referenced by the field (used for FK fields)
  private String refTableName;

  //Names of columns which are referenced by the field (used for FK fields)
  private List<String> refColumnsNames;

  //Name of the PK column in the referenced table (used for FK fields)
  private String refPkColumnName;

  //Indicates position of the column in table
  private int ordinal;

  //Map of tables that contains foreign key set on this table's Public Key
  private Map<String, IDbTable> subTables;
  ...
}
6. Select all columns + select all primary keys
Tüm constraint'ler üzerinde dolaşıp primary key olanlar primary key = true olarak işaretlenir. Ayrıca foreign key ise atıfta bulunulan sütunun tablolar listesine DBTable nesnesi eklenir. Yani subTables listesine ekleniyor. 

Böylece primary key <-> foreign key çift yönlü ilişkisinin bir tarafı dolduruluyor

COLUMNS
Tablolara ait sütunları verir
Örnek - Bir kullanıcıya ait tabloların tüm sütunları
Şöyle yaparız
MySQL
SELECT table_name, column_name FROM information_schema.columns WHERE table_schema = ?

Oracle
SELECT a.table_name, a.column_name, a.data_default FROM all_tab_cols a WHERE a.owner = ?
KEY_COLUMN_USAGE
Tablolara ait Primary Key, Foreign Key gibi sütunları verir
Şöyle yaparız
MYSQL
SELECT column_name, referenced_table_name FROM information_schema.key_column_usage
  WHERE referenced_column_name IS NOT NULL AND table_name LIKE 'GSR%';


Oracle
SELECT a.column_name, a.table_name FROM user_cons_columns a,user_constraints b WHERE a.constraint_name = b.constraint_name AND a.table_name = b.table_name AND a.table_name LIKE 'GSR_%' AND b.constraint_type = 'R'
TABLES
Tablo ve view isimlerini verir
Şöyle yaparız
MYSQL
SELECT table_name, FROM information_schema.tables
 WHERE table_name LIKE 'GSR%' AND table_type = 'VIEW'
SELECT table_name, FROM information_schema.tables WHERE table_name LIKE 'GSR%' AND table_type = 'BASE TABLE'


3 Aralık 2021 Cuma

2 Aralık 2021 Perşembe

GROUP_CONCAT Metodu - Birden Fazla Satırı Birleştirebiliriz

Giriş
GROUP_CONCAT ile birden fazla satırı birleştirebiliriz.

Örnek
Şöyle yaparız
GROUP_CONCAT(
  DISTINCT CONCAT(tags.id,',',tags.displayName) 
  ORDER BY posts.id 
  SEPARATOR ';'
)
Örnek
Şöyle yaparız
SELECT employeeNumber, firstName, lastName,
  ( SELECT GROUP_CONCAT(DISTINCT CONCAT(firstName,lastName,employeeNumber) SEPARATOR ','
    FROM employees) AS concatted
FROM employees
INNER JOIN
 customers ON customers.salesRepEmployeeNumber = employeeNumber
GROUP BY employeeNumber
ORDER BY firstName , lastname;

1 Aralık 2021 Çarşamba

Vitess Consensus Algorithm

Flex Paxos denilen bir şey kullanıyor. Flexible Paxos anlamına geliyor.

Replication

Giriş
Master-Slave Replication için şunlar kullanılabilir
1. Asynchronous
2. Synchronous
3. Semi-synchronous

Master-Slave veya Master-Master Replication için şunlar kullanılabilir
1. Group Replication

 Asynchronous Replication
 Asynchronous Replication yazısına taşıdım

Synchronous Replication
Açıklaması şöyle. Master tüm replica'ların da transaction'ı commit etmesini bekler.
With fully synchronous replication, when a source commits a transaction, all replicas have also committed the transaction before the source returns to the session that performed the transaction. Fully synchronous replication means failover from the source to any replica is possible at any time. The drawback of fully synchronous replication is that there might be a lot of delay to complete a transaction.
Semi-synchronous Replication
Semi-synchronous Replication yazısına taşıdım

Group Replication
Açıklaması şöyle
Groups can operate in a single-primary mode with automatic primary election, where only one server accepts updates at a time. Alternatively, groups can be deployed in multi-primary mode, where all servers can accept updates, even if they are issued concurrently.

There is a built-in group membership service that keeps the view of the group consistent and available for all servers at any given point in time. Servers can leave and join the group and the view is updated accordingly. Sometimes servers can leave the group unexpectedly, in which case the failure detection mechanism detects this and notifies the group that the view has changed. This is all automatic.

Group Replication guarantees that the database service is continuously available. However, it is important to understand that if one of the group members becomes unavailable, the clients connected to that group member must be redirected, or failed over, to a different server in the group, using a connector, load balancer, router, or some form of middleware. Group Replication does not have an inbuilt method to do this. For example, see MySQL Router 8.0.

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