Showing posts with label SQL. Show all posts

How drop unnamed column constraint in Oracle

by GarciaPL on Thursday, 16 April 2020

If constraint on column STATUS was created without a name during creating a table, Oracle will assign a random name for it. Unfortunately, we cannot modify the constraint directly.

Steps involved of dropping unnamed constraint linked to column STATUS

  1. Duplicate STATUS field into a new field STATUS2
  2. Define CHECK constraints on STATUS2
  3. Migrate data from STATUS into STATUS2
  4. Drop STATUS column
  5. Rename STATUS2 to STATUS


ALTER TABLE MY_TABLE ADD STATUS2 NVARCHAR2(10) DEFAULT 'OPEN';
ALTER TABLE MY_TABLE ADD CONSTRAINT MY_TABLE_CHECK_STATUS CHECK (STATUS2 IN ('OPEN', 'CLOSED'));
UPDATE MY_TABLE SET STATUS2 = STATUS;
ALTER TABLE MY_TABLE DROP COLUMN STATUS;
ALTER TABLE MY_TABLE RENAME COLUMN STATUS2 TO STATUS;

Git move files - stop applying CRLF against my SQL file!

by GarciaPL on Friday, 6 April 2018

Recently, I was trying to move some files, mostly .java's and.sql's, from one maven module into another one. We were just trying to merge two modules into one module, because of we thought that keeping two modules with pretty the same functionality is odd.

The main point around this movement was to preserve the history of changes made to those files in the past. Everything was fine until we tried to deploy the application on the server. Of course, I forgot to mention that we were using Flyway as a database migration tool. Unexpectedly, one of the file's checksum was not the same as the checksum saved in the database for that SQL file (flyway_schema_history table). We were astonished because of the content of the file did not change at all... but Pull Request on BitBucket was saying that there is a difference. We checked the MD5 for that file before and after the move, it was the same, but for some reason the checksum detected by Flyway was different! To be honest, we spent a lot of time trying to understand why it's happening, and then I found this post made by Richard Tuin [1].

The solution was just to revert moving of those SQL files up to the previous location and try to create a file called .gitattributes. We defined over there just a single line which was disabling CRLF for that single file for which checksum was not the same as this one stored in flyway schema history table. After moving SQL files once again, using git mv [2] command and uploading results on BitBucket, everything was fine!

The content of .gitattributes for that single file should look as below :

fileName.sql -crlf


References :
[1] Richard Tuin - How to make git ignore different line endings
[2] Git mv
[3] Git attributes












Oracle - duplicate keys found for novalidate unique constraint

by GarciaPL on Tuesday, 10 January 2017

I was trying recently to add unique constraint on few fields on Oracle. At the beginning I thought that it might be a easy task to do, but at the end some workaround was needed. So, in case of error you might get below message which indicates that Oracle found some duplicate records in your table, even though you specify NOVALIDATE flag which allows you to do not check existing data against that constraint.

ALTER TABLE MY_TABLE ADD CONSTRAINT MY_UNIQUE_CONSTRAINT UNIQUE (ID, CUSTOM_ID, VALUE, UNITS, AMOUNT, ITEM_ID, DATE) NOVALIDATE
Error report -
SQL Error: ORA-02299: cannot validate (DB.MY_UNIQUE_CONSTRAINT) - duplicate keys found
02299. 00000 - "cannot validate (%s.%s) - duplicate keys found"
*Cause:    an alter table validating constraint failed because the table has
           duplicate key values.
*Action:   Obvious

That's why we need to define simple INDEX on fields which will be used during setting UNIQUE CONSTRAINT before setting our UNIQUE CONSTRAINT.
CREATE INDEX INDEX_UNIQUE_CONSTRAINT ON MY_TABLE (ID, CUSTOM_ID, VALUE, UNITS, AMOUNT, ITEM_ID, DATE);
ALTER TABLE MY_TABLE ADD CONSTRAINT MY_UNIQUE_CONSTRAINT UNIQUE (ID, CUSTOM_ID, VALUE, UNITS, AMOUNT, ITEM_ID, DATE) NOVALIDATE;

References : [1] ENABLE NOVALIDATE validating existing data - Ask Tom [2] NOVALIDATE Constraints – No really - richardfoote

Create sequence in Oracle without dropping it

by GarciaPL on Thursday, 25 August 2016

Some of you might thinking how to create sequence for table which contains some records. You might always export all records from database, then eventually create sequence and after all import those records once again into database. Nevertheless there is a solution how to create this sequence using PL/SQL.



DECLARE
  seq_value_holder        NUMBER;
BEGIN
  -- create a sequence for YOUR_TABLE
  SELECT NVL(MAX(id), 0) + 1
  INTO seq_value_holder
  FROM YOUR_TABLE;
  EXECUTE immediate 'create sequence SEQ_YOUR_TABLE start with ' || seq_value_holder || ' increment by 1';
END;

Spring Test DBUnit - Table already exists

by GarciaPL on Saturday, 16 April 2016

I had a chance to work with Spring Test DBUnit [1] (integration between Spring testing framework and DBUnit) with few tests related with Spring Integration. Most of them use the same common text context which contains embedded database H2. Funny thing was that some tests failed, because of some tables already exist. The issue is that embedded database is not cleared between tests and it is reused within the same context. That's why you should use @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) with @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class}) like below :

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class YourTest {

}




Reference :
[1] Spring Test DBUnit

[2] Spring Framework - DirtiesContext

SQL DSL

by GarciaPL on Monday, 7 September 2015

At this moment I am looking for some Java library for SQL-DSL. I have two requirements for this library. First of all it must be able to build queries using Builder pattern. Second requirement is that it should be available in Maven central repository. I must to add that I am not looking for some bigger framework to do it, but some small tool to build those queries dynamically. I found very good library called jOOQ. Unfortunately it will apply overhead in my project. I found library called sql-dsl from kantega [2]. Unfortunately it is not available in Maven repository. Additionally I found that this library does not support distinct statement (maybe owner forgot about this...). Nevertheless there was perfect time and place for me to make pull request with hotfix on GitHub [1]. I hope that owner of this library will apply those changes and put this library in Maven repository that it will be more accessible for me and other developers.


Reference :
[1] https://github.com/GarciaPL/sql-dsl
[2] https://github.com/kantega/sql-dsl

Recruitment Tech Test #2

by GarciaPL on Saturday, 15 August 2015

Some time ago I have solved another technical test for one company. As the same before this test was performed for recruitment process.

This application is dedicated for tellers which provides functionalities like :

  1. Create account(s) - a user can create an account, associate a name with it, give it a unique account number, add a starting balance etc.
  2. Make lodgement - a user can lodge an amount into an account (balance increase)
  3. Make transfer - a user can transfer an amount from one account to another (balance transfer)
  4. View transactions - a user can view recent, or all, transactions for an account (statement)
Application was developed using Java 7 and Spring Framework 4.0.1. Below you can find out what libraries were used in application :

  • Spring Beans – 4.0.1.RELEASE
  • Spring Tx – 4.0.1.RELEASE
  • Spring Context – 4.0.1.RELEASE
  • Spring Context Support – 4.0.1.RELEASE
  • Spring Orm – 4.0.1.RELEASE
  • Spring Jdbc – 4.0.1.RELEASE
  • Spring Web – 4.0.1.RELEASE
  • Spring Web MVC – 4.0.1.RELEASE
  • Spring Test - 4.0.1.RELEASE
  • Joda Money - 0.10.0
  • Jackson Core - 2.5.0
  • Jackson Databind - 2.5.0
  • Jackson Annotations - 2.5.0
  • Javax Servlet API – 3.1.0
  • JSTL – 1.2
  • Hibernate Core - 4.3.5.Final
  • Hibernate Entitymanager - 4.3.5.Final
  • HSQLDB - 2.3.3
  • SLF4J - 1.7.8
  • Commons Logging - 1.2
  • JUnit - 4.10
  • Mockito - 1.9.5
  • Hamcrest - 1.3
  • Twitter Bootstrap - 3.3.4
  • DataTables - 1.10.7
  • jqBootstrapValidation - 1.3.6


Home screen


Create new account

Deposit money


Transfer money

Transactions


Application for tellers has also built-in Jetty web sever container which allows you run it very quickly using command mvn jetty:run in directory of project. After that application should be accessible under context http://localhost:9090/banknow/.
You can also build own WAR file using command mvn war:war in directory of project. War will be accessible under directory /target and called banknow.war.
Reference :
[1] GarciaPL Github.com BankNow
[2] Jetty
[3] HSQLDB
[4] jQuery DataTables
[5] jQuery jqBootstrapValidation

SQL Developer Freezes on Ubuntu

by GarciaPL on Wednesday, 12 November 2014

Some of you have faced the problem when SQL Developer on Ubuntu which just freeze after some idle time. More information about this issue and solution can be found under [1]. I must to admit that this solution is quite complex, so I decided to prepare some very simple tool called SQLDeveloper Killer. Below I paste some source code in Java which aims to list all processes which are alive in system and find particular process which corresponds to SQL Developer process (contains "sqldeveloper.conf" in CMD description).

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package killsqldeveloper;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 *
 * @author lciesluk
 */
public class KillSQLDeveloper {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            Process exec = Runtime.getRuntime().exec(new String[]{"ps", "-ef"});
            exec.waitFor();

            BufferedReader reader
                    = new BufferedReader(new InputStreamReader(exec.getInputStream()));

            String line = "";
            while ((line = reader.readLine()) != null) {
                if (line.contains("sqldeveloper.conf")) {
                    System.out.println(line);
                    String[] split = line.split(" ");
                    if (split.length > 2) {
                        System.out.println(split[1]);
                        if (!split[1].equals("")) {
                            Process execKill = Runtime.getRuntime().exec(new String[]{"kill", "-9", split[1]});
                            execKill.waitFor();
                        } else {
                            Process execKill = Runtime.getRuntime().exec(new String[]{"kill", "-9", split[2]});
                            execKill.waitFor();
                        }
                    }
                    break;
                }
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}


After you paste this code to your IDE, you can install very useful tool called alacarte to create Unity launcher on Ubuntu.

SQL Developer Killer


Reference : [1] Alexeymoseyev.wordpress.com - Oracle SQL Developer hangs on Linux [2] Pastebin.com - Source Code [3] Alacarte - Ubuntu menu editor

SQL Developer

by GarciaPL on Thursday, 31 July 2014

If you have difficulties with running SQL Developer (in my case 4.0.2.15.21) from Oracle in console on Linux you can see such stack trace :

# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007feb22ed8be0, pid=32116, tid=140649711318784
#
# JRE version: Java(TM) SE Runtime Environment (7.0_65-b17) (build 1.7.0_65-b17)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.65-b04 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  0x00007feb22ed8be0
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/lukasz/sqldeveloper/sqldeveloper/bin/hs_err_pid32116.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
#
/home/lukasz/sqldeveloper/sqldeveloper/bin/../../ide/bin/launcher.sh: line 1193: 32116 Przerwane               (core dumped) ${JAVA} "${APP_VM_OPTS[@]}" ${APP_ENV_VARS} -classpath ${APP_CLASSPATH} ${APP_MAIN_CLASS} "${APP_APP_OPTS[@]}"

So, fix of this problem is quite easy - go to file sqldeveloper.sh which is located in folder of your SQL Developer and add at the beginning of this file the following line :
unset GNOME_DESKTOP_SESSION_ID

SQLDeveloper.sh
SQLDeveloper.sh


Oracle DB 11g Errors Guide

by GarciaPL on Tuesday, 15 July 2014

I would like only to announce that I have created a small application in Android called Oracle DB 11g Errors Guide. This application helps you as a developer or administrator of Oracle ® Database 11g Release 1 find solutions of problems which you are faced with during your work. Each error message contains the message statement, an explanation of the probable causes and a recommended action.

More information about Oracle ® Database 11g Release 1 Error Messages you can find under hyperlink - Oracle® Database Error Messages 11g Release 1 (11.1) Documentation

Hyperlink to Google Play Store - Google Play Store GarciaPL

Remove all tables from a MySQL schema

by GarciaPL on Thursday, 6 March 2014

A few days ago I found very useful solution (thanks for Biomathematicus!) for deleting tables from a MySQL schema. I know that it could be quite difficult for some developers (also for me) to manually remove every table from schema without removing this schema itself. So, there is a two-step solution which is going to help you removing a lot of tables from SQL database.

1) List all tables in the MySQL schema :

SELECT CONCAT('drop table ',table_name,'; ')
FROM information_schema.tables
WHERE table_schema = 'yourDatabaseName';

2) Copy result and paste it to query window and execute.
drop table table1; 
drop table table2;

Reference : [1] Biomathematicus.blogspot.com Remove all tables from mysql schema

SQL Joins Explain Diagram

by GarciaPL on Friday, 25 October 2013

I must say that this one diagram of SQL Joins is one of the most memorizing image about how joins in sql works. Enjoy :)

SQL Joins
SQL Joins Explain

Reference :
[1] Codeproject.com Visual Representation of SQL Joins

ORA-01861: literal does not match format string

by GarciaPL on Thursday, 2 May 2013

In last few days I had a problem with some SQL queries in Oracle database. I won't give you this particular SQL query which has ended with error which you can find in the header of this post, but i will give solution! :)

Suppose that your SQL queries use data in format for instance YYYY-MM-DD, so before them execute below command which specifies the default date format which can be used in TO_CHAR and TO_DATE functions :

alter session  set NLS_DATE_FORMAT='YYYY-MM-DD';


Reference :
[1] Docs Oracle NLS_DATE_FORMAT

MySQL Find duplicate records

by GarciaPL on Wednesday, 5 December 2012


I used to find many times duplicates in my table in MySQL database this time. First of all, I will paste here my table structure below (yes i know that it is difficult to read) :

mysql> describe proxy_parser_statistics;
+-----------------------+---------+------+-----+---------+----------------+
| Field                 | Type    | Null | Key | Default | Extra          |
+-----------------------+---------+------+-----+---------+----------------+
| ID                    | int(11) | NO   | PRI | NULL    | auto_increment |
| file_name             | text    | NO   |     | NULL    |                |
| processing_stamp_date | date    | YES  |     | NULL    |                |
| processing_stamp_time | time    | YES  |     | NULL    |                |
| processed_records     | int(11) | YES  |     | NULL    |                |
+-----------------------+---------+------+-----+---------+----------------+

I just use this query :

SELECT a.id, a.file_name FROM proxy_parser_statistics a INNER JOIN proxy_parser_statistics b ON a.file_name = b.file_name WHERE a.id <> b.id

Results of above query :

+-----+-----------------------------+
| id  | file_name                   |
+-----+-----------------------------+
| 803 | access1211170012.merged.log |
| 804 | access1211170112.merged.log |
| 805 | access1211170212.merged.log |
| 806 | access1211170312.merged.log |
| 807 | access1211170412.merged.log |
| 808 | access1211170512.merged.log |
| 809 | access1211170612.merged.log |
| 810 | access1211170712.merged.log |
| 811 | access1211170812.merged.log |
| 812 | access1211170912.merged.log |
| 813 | access1211171012.merged.log |
| 814 | access1211171112.merged.log |
| 815 | access1211171212.merged.log |
| 816 | access1211171312.merged.log |
| 817 | access1211171412.merged.log |
| 818 | access1211171512.merged.log |
| 819 | access1211171612.merged.log |
| 820 | access1211171712.merged.log |
| 821 | access1211171812.merged.log |
| 822 | access1211171912.merged.log |
| 823 | access1211172012.merged.log |
| 824 | access1211172112.merged.log |
| 825 | access1211172212.merged.log |
| 826 | access1211172312.merged.log |
| 827 | access1211172359.merged.log |
| 727 | access1211170012.merged.log |
| 728 | access1211170112.merged.log |
| 729 | access1211170212.merged.log |
| 730 | access1211170312.merged.log |
| 731 | access1211170412.merged.log |
| 732 | access1211170512.merged.log |
| 733 | access1211170612.merged.log |
| 734 | access1211170712.merged.log |
| 735 | access1211170812.merged.log |
| 736 | access1211170912.merged.log |
| 737 | access1211171012.merged.log |
| 738 | access1211171112.merged.log |
| 739 | access1211171212.merged.log |
| 740 | access1211171312.merged.log |
| 741 | access1211171412.merged.log |
| 742 | access1211171512.merged.log |
| 743 | access1211171612.merged.log |
| 744 | access1211171712.merged.log |
| 745 | access1211171812.merged.log |
| 746 | access1211171912.merged.log |
| 747 | access1211172012.merged.log |
| 748 | access1211172112.merged.log |
| 749 | access1211172212.merged.log |
| 750 | access1211172312.merged.log |
| 751 | access1211172359.merged.log |
+-----+-----------------------------+

Reference :
[1] Table structure Pastebin.com [2] Query result Pastebin.com