Showing posts with label SQL Development. Show all posts
Showing posts with label SQL Development. Show all posts

Calculation of Inventory Through The Trigger Mechanism on The Transaction Table

This time we will discuss about the computerization of inventory in the warehouse (location) is done automatically at the time the transaction is entered into a purchase transaction table, into the sales transaction table, as well as mutations in the transaction table movement of goods between warehouses (locations). The assumption of this discussion is about the stock of drugs in the hospital pharmacy, the location of the drug in some places.
For the above, the following is the syntax of SQL that is run on Microsoft SQL Server 2000 Personal Edition.
CREATE TABLE [dbo].[Master_Obat] (
    [Kode_Kelompok] [varchar] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [Kode_Obat] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
    [Nama_Obat] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [Satuan] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [Harga_beli] [money] NULL ,
    [Harga_Jual] [money] NULL ,
    [Kode_Pabrik] [varchar] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Master_Obat] ADD
    CONSTRAINT [PK_Master_Obat] PRIMARY KEY  CLUSTERED
    (
        [Kode_Obat]
    )  ON [PRIMARY]
GO
---------------------------------------------------
Master_obat tables are used to store master data medicine overall. In this table has a field that is drug code, drug name, unit, purchase price, sales price, and several other fields as needed.
---------------------------------------------------

CREATE TABLE [dbo].[Gudang] (
    [kode_gudang] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
    [Nama_gudang] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [Is_GudangUtama] [bit] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Master_Obat] ADD
    CONSTRAINT [PK_Master_Obat] PRIMARY KEY  CLUSTERED
    (
        [Kode_gudang]
    )  ON [PRIMARY]
GO
---------------------------------------------------

CREATE TABLE [dbo].[StokGudang] (
    [kode_gudang] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
    [kode_obat] [nvarchar] (8) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
    [stok] [int] NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Master_Obat] ADD
    CONSTRAINT [PK_Master_Obat] PRIMARY KEY  CLUSTERED
    (
        [Kode_gudang]
        [Kode_obat]
    )  ON [PRIMARY]
GO
----------------------------------------------
Both of the above table is used to store information drug stocks at each location.
----------------------------------------------

Next is a purchase transaction table as shown below. At the time of the purchase transaction information is stored in two tables, namely, purchase table and table purchase details.

CREATE TABLE [dbo].[Pembelian] (
    [Nomor_Pembelian] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
    [Nomor_Faktur] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [Tanggal_Pembelian] [datetime] NULL ,
    [pemasok] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Master_Obat] ADD
    CONSTRAINT [PK_Master_Obat] PRIMARY KEY  CLUSTERED
    (
        [Nomor_Pembelian]
    )  ON [PRIMARY]
GO

-------------------------------------------------------
CREATE TABLE [dbo].[Pembelian_Detail] (
    [Nomor_Pembelian] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
    [Kode_Obat] [nvarchar] (8) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
    [quantity] [int] NOT NULL ,
    [Harga_beli] [money] NULL ,
    [harga_jual] [money] NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Pembelian_Detail] ADD
    CONSTRAINT [PK_Pembelian_Detail] PRIMARY KEY  CLUSTERED
    (
        [Nomor_Pembelian],
        [Kode_Obat]
    )  ON [PRIMARY]
GO

ALTER TABLE [dbo].[Pembelian_Detail] ADD
    CONSTRAINT [FK_Pembelian_Detail_Pembelian] FOREIGN KEY
    (
        [Nomor_Pembelian]
    ) REFERENCES [dbo].[Pembelian] (
        [Nomor_Pembelian]
    )
GO

Drug stocks counting mechanism through trigger on the table purchase details, as described below. Trigger occurs when the INSERT, DELETE, and UPDATE.

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

CREATE TRIGGER tbl_PembelianDetail_Trigger1
ON dbo.Pembelian_Detail
FOR INSERT
AS
    declare @no_beli     varchar(12)
    declare @kod_obt     nvarchar(8)
      declare @qty     int
    declare @stk     int
    declare @hrg_beli    money
    declare @hrg_jual    money

    SELECT     @no_beli  = ins. nomor_pembelian,
        @kod_obt = ins.kode_obat,
        @qty = ins.quantity,
        @hrg_beli = ins.harga_beli,
        @hrg_jual = ins.harga_jual
    FROM inserted ins

    SELECT @stk = Stok
    FROM StokGudang
    WHERE (kode_gudang = (SELECT Kode_Gudang
                   FROM Gudang
                   WHERE Is_gudangUtama = 1))
         AND (kode_obat = @kod_obt)

    UPDATE StokGudang
    SET stok = @stk + @qty
    WHERE  (kode_gudang = (SELECT Kode_Gudang
                   FROM Gudang
                   WHERE Is_gudangUtama = 1))
         AND (kode_obat = @kod_obt)

    UPDATE master_obat
    SET harga_beli = @hrg_beli, harga_jual = @hrg_jual
    WHERE kode_obat = @kod_obt


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

CREATE TRIGGER tbl_PembelianDetail_Trigger2
ON dbo.Pembelian_Detail
FOR DELETE
AS
    declare @no_beli     varchar(12)
    declare @kod_obt     nvarchar(8)
      declare @qty     int
    declare @stk     int

    SELECT     @no_beli  = del. nomor_pembelian,
        @kod_obt = del.kode_obat,
        @qty = del.quantity
    FROM deleted del

    SELECT @stk = Stok
    FROM StokGudang
    WHERE  (kode_gudang = (SELECT Kode_Gudang
                   FROM Gudang
                   WHERE Is_gudangUtama = 1))
         AND (kode_obat = @kod_obt)

    UPDATE StokGudang
    SET stok = @stk - @qty
    WHERE  (kode_gudang = (SELECT Kode_Gudang
                   FROM Gudang
                   WHERE Is_gudangUtama = 1))
         AND (kode_obat = @kod_obt)

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

CREATE TRIGGER tbl_PembelianDetail_Trigger3
ON dbo.Pembelian_Detail
FOR UPDATE
AS
    declare @no_beli     varchar(12)
    declare @kod_obtIns     nvarchar(8)
    declare @kod_obtDel     nvarchar(8)
      declare @qtyIns     int
    declare @qtyDel     int
    declare @stk     int
    declare @hrg_beliIns    money
    declare @hrg_jualIns    money
    declare @hrg_beliDel    money
    declare @hrg_jualDel    money

     SELECT     @no_beli  = ins. nomor_pembelian,
        @kod_obtDel = del.kode_obat,
        @kod_obtIns = ins.kode_obat,
        @qtyDel = del.quantity,
        @qtyIns = ins.quantity,
        @hrg_beliDel = del.harga_beli,
        @hrg_beliIns = ins.harga_beli,
        @hrg_jualDel = del.harga_jual,
        @hrg_jualIns = ins.harga_jual
    FROM deleted del, inserted ins

    SELECT @stk = Stok
    FROM StokGudang
    WHERE (kode_gudang = (SELECT Kode_Gudang
                   FROM Gudang
                   WHERE Is_gudangUtama = 1))
         AND (kode_obat = @kod_obtDel)

    UPDATE StokGudang
    SET stok = @stk - @qtyDel
    WHERE (kode_gudang = (SELECT Kode_Gudang
                   FROM Gudang
                   WHERE Is_gudangUtama = 1))
         AND (kode_obat = @kod_obtDel)

    SELECT @stk = Stok
    FROM StokGudang
    WHERE (kode_gudang = (SELECT Kode_Gudang
                   FROM Gudang
                   WHERE Is_gudangUtama = 1))
         AND (kode_obat = @kod_obtIns)

    UPDATE StokGudang
    SET stok = @stk + @qtyIns
    WHERE (kode_gudang = (SELECT Kode_Gudang
                   FROM Gudang
                   WHERE Is_gudangUtama = 1))
         AND (kode_obat = @kod_obtIns)
   
    UPDATE master_obat
    SET harga_beli = @hrg_beliDel, harga_jual = @hrg_jualDel
    WHERE kode_obat = @kod_obtDel

    UPDATE master_obat
    SET harga_beli = @hrg_beliIns, harga_jual = @hrg_jualIns
    WHERE kode_obat = @kod_obtIns


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
-----------------------------------------------

MySQL Workbench for working with MySQL Servers

MySQL Workbench provides a graphical tool for working with MySQL Servers and databases. It fully supports MySQL Server versions 5.1 and above. It is also compatible with MySQL Server 5.0, but not every feature of 5.0 may be supported. It does not support MySQL Server versions 4.x.
MySQL Workbench provides three main areas of functionality:
  • SQL Development: This module enables you to create and manage connections to database servers. As well as enabling you to configure connection parameters, MySQL Workbench provides the capability to execute SQL queries on the database connections using the built-in SQL Editor. This functionality replaces that previously provided by the Query Browser standalone application.
  • Data Modeling: This module enables you to create models of your database schema graphically, you can reverse and forward engineer between a schema and a live database, and edit all aspects of your database using the comprehensive Table Editor. The Table Editor provides easy-to-use facilities for editing Tables, Columns, Indexes, Triggers, Partitioning, Options, Inserts and Privileges, Routines and Views.
  • Server Administration: This module enables you to create and administer server instances.
MySQL Workbench is available in two editions, the Community Edition and the Standard Edition. The Community Edition is available free of charge. The Standard Edition provides additional Enterprise features, such as database documentation generation, at low cost. 

MySQL Workbench Editions
The Community Edition (OSS)
The Community Edition is the foundation of all MySQL Workbench editions—versions that are currently available or those that will become available in the future. All editions of MySQL Workbench are based on the Community Edition and all future improvements to the base framework and feature set will be included in this version. The Community Edition is a full feature product that puts a powerful database management tool into the hands of the MySQL community.
The Standard Edition
The Standard Edition is a commercial extension that builds on top of the OSS Edition and adds modules and plugins, enabling an optimized work flow. The highlights of this edition are:
  • MySQL Specific Schema Validation
  • Model Validation
  • General Schema Validation
  • DBDoc
DBDoc provides the following features:
  • Document complex database schemata
  • Document all SQL object types
  • Document output available in different file formats
A comparison of edition features can be found at MySQL Workbench Developer Central

MySQL Workbench is available for the following platforms:
  • Windows
  • Linux
  • Mac OS X
Binary distributions of MySQL Workbench are available for the preceding platforms. Source code distributions are also available as a tar.gz package or an RPM package.
The following sections explain the installation process for each of these platforms.