[SQL] - Aggregate SQL Server

May 25, 2018 |

This article just note the SQL script in the work progress.

1. Drop INDEX
* SQL Server
-- Check a index is existing or not before drop it.
IF EXISTS (SELECT * FROM sys.indexes WHERE  object_id = OBJECT_ID(N'<table_name') AND name = N'<index_name>'))
    DROP INDEX <index_name> ON <table_name>

2. Drop TABLE
* SQL Server 
-- check a table is existing or not before drop it.
IF EXISTS (SELECT * FROM sys.tables WHERE object_id = OBJECT_ID(N'table_name'))
    DROP TABLE <table_name>

3. Drop COLUMN
* SQL Server 
-- drop a column if its doesn't has any contraint.
IF EXISTS (Select 1 From sys.columns Where name = '<column_name>' And object_id = OBJECT_ID('<table_name>'))
BEGIN
  ALTER TABLE <table_name> DROP COLUMN <column_name>;
END

-- drop a column if its has contraints.
IF EXISTS (Select 1 From sys.columns Where name = '<column_name>' And object_id = OBJECT_ID('<table_name>'))
BEGIN
  DECLARE @def varchar(256);
  SELECT @def = name FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID('<table_name>') AND COL_NAME(parent_object_id, parent_column_id) = '<column_name>';

  if @def is not null
  EXEC('ALTER TABLE <table_name> DROP CONSTRAINT ' + @def);

  ALTER TABLE <table_name> DROP COLUMN <column_name>;
END


4. Drop CONSTRAINT
* SQL Server 
--drop a contraint
ALTER TABLE <table_name> DROP CONSTRAINT <constraint_name>

-- Check a column is contraint on a table. If its exist, drop and create contraint.
IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS C JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K
ON C.TABLE_NAME = K.TABLE_NAME AND C.CONSTRAINT_CATALOG = K.CONSTRAINT_CATALOG AND C.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA
AND C.CONSTRAINT_NAME = K.CONSTRAINT_NAME WHERE C.CONSTRAINT_TYPE = 'PRIMARY KEY' AND K.COLUMN_NAME = '<column_name>'
and K.TABLE_NAME = '<table_name>')
BEGIN
    declare @pkey varchar(50);
    SELECT top 1 @pkey=c.CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS C JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K
    ON C.TABLE_NAME = K.TABLE_NAME AND C.CONSTRAINT_CATALOG = K.CONSTRAINT_CATALOG AND C.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA
    AND C.CONSTRAINT_NAME = K.CONSTRAINT_NAME WHERE C.CONSTRAINT_TYPE = 'PRIMARY KEY' and K.TABLE_NAME = '<table_name>'

    if(@pkey is not null)
        exec('ALTER TABLE <table_name> DROP CONSTRAINT ' + @pkey);
    else
        set @pkey='pk_<table_name>';

    exec('ALTER TABLE <table_name> ADD CONSTRAINT ' + @pkey + ' PRIMARY KEY CLUSTERED (column_name_1, column_name_2,.., column_name_n) WITH (FILLFACTOR = 80)');
END

-- rename column_1 to column_2 and make them a part of primary key
IF EXISTS (SELECT 1 FROM sys.columns WHERE name = 'column_1' AND object_id = OBJECT_ID('table_1'))
BEGIN
  DECLARE @pk_name varchar(300);
  SELECT @pk_name = name FROM sysobjects WHERE xtype = 'PK' AND parent_obj = (object_id('table_1'));

  if @pk_name is not null
    exec('ALTER TABLE table_1 DROP CONSTRAINT ' + @pk_name);
  else
    SET @pk_name='pk_table_1';
 
  EXEC sp_rename 'table_1.column_1', 'column_2', 'COLUMN';
 
  exec('ALTER TABLE table_1 ADD CONSTRAINT ' + @pk_name + ' PRIMARY KEY CLUSTERED (column_1, column_2,..., column_n) WITH (FILLFACTOR = 80)');
END




Updating...
Read more…

[Tools] - Java VisualVM

May 12, 2018 |



Java VisualVM help us monitor resource in Java Application as show performance.
To open Java VisualVM, following the step below:

1. Go to C:\Program Files\Java\<jdk version>\bin (Java JDK 64bit )or C:\Program Files (x86)\Java\<jdk version>\bin (Java JDK x86).
Note: This tool avaible from JDK 8 or older, JDK 9 is not avaible.
2. Find and open jvisualvm.exe
3. The Java VisualJM window display

Java VisualVM


+ From Application tab > Local: Show Java Applications are running.
Note: If you open Java VisualVM with current JDK version, there are only show the Java Applications that are running on this JDK.
+ Monitor: The Java VisualVM show the resources which the Java apps is used as memory, CPU, Network,...
+ Sampler tab (Useful): The Java VisualVM take a sampler about the resources of Java App. You can use take performance your app. You will be known exactly the java classes are taken performance.
Read more…

[Java Design Pattern] [Creational Pattern] - Factory Method

May 09, 2018 |
Creational Pattern - Factory Method Pattern
Purpose: The Factory Method Pattern gives us a way encapsulate the instancetiation of concrete types. Instead you delcare new direct object, you use Factory class to create a new object.

Factory Method concept UML. (Source: Software Architecture Design Patterns in Java.pdf)





Example 1:


IVehicle
package com.designpattern.creational.factorymethod;

public interface IVehicle {
    void run();
}

Vehicle
package com.designpattern.creational.factorymethod;

public class Vehicle implements IVehicle {
   
    private String color;
   
    @Override
    public void run() {
        System.out.println("Not Run");
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

Bike
package com.designpattern.creational.factorymethod;

public class Bike extends Vehicle {
   
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("Bike is running...");
    }

}

Car
package com.designpattern.creational.factorymethod;

public class Car extends Vehicle {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("Car is running...");
    }
}

Motobike

package com.designpattern.creational.factorymethod;

public class Motobike extends Vehicle {
   
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("Motobike is running...");
    }
}

VechicleFactory
package com.designpattern.creational.factorymethod;

public class VehicleFactory {

    public static Vehicle createVehicle(String s) {
        Vehicle vehicle = null;
       
        if ("bike".equals(s)) {
            vehicle = new Bike();
        } else if ("motobike".equals(s)) {
            vehicle = new Motobike();
        } else if ("car".equals(s)) {
            vehicle = new Car();
        } else {
            vehicle = new Vehicle();
        }

        return vehicle;

    }
}

VehicleMain
package com.designpattern.creational.factorymethod;

public class VehicleMain {
    public static void main(String[] args) {
        Vehicle bike =   VehicleFactory.createVehicle("bike");
        bike.run();
      
        Vehicle motobike = VehicleFactory.createVehicle("motobike");
        motobike.run();
      
        Vehicle car = VehicleFactory.createVehicle("car");
        car.run();

    }
}

Result:
Bike is running...
Motobike is running...
Car is running...



Read more…