IT

Personal notes on software development.
For Java technologies check my dedicated site

Pages

Index

This is the parent page for database info.
Child pages include:


Database procedural languages (ex. PL/SQL and T-SQL):

SQL is designed for a specific purpose: to query data contained in a relational database. SQL is a set-based, declarative query language, not an imperative language like C or BASIC. However, there are extensions to Standard SQL which add procedural programming language functionality, such as control-of-flow constructs.
  • SQL: is a query language to operate on sets. It is more or less standardized  and used by almost all relational database management systems: SQL Server, Oracle, MySQL, PostgreSQL, DB2, Informix, etc.
  • PL/SQL is a proprietary procedural language used by Oracle
  • T-SQL is a proprietary procedural language used by Microsoft in SQL Server.
  • Like PL/SQL and T-SQL there are many other proprietary procedural languages. Check the wikipedia procedural languages list.
Procedural languages are designed to extend the SQL's abilities while being able to integrate well with SQL.

They can be used for example to write stored procedures: pieces of code residing on the server to manage complex business rules that are hard or impossible to manage with pure set-based operations. I.e.: they are used to include logic in the database.

Database jargon

  • DDL (Data Definition Language) statements: manages table and index structure. The most basic items of DDL statements are:
     - CREATE;
     - ALTER;
     - RENAME;
     - DROP;
     - TRUNCATE;
  • DML (Data Manipulation Language) Statements: is the subset of SQL used to add, update and delete data ex:
     - INSERT;
     - UPDATE;
     - DELETE;
     - MERGE;
  • DCL (Data Control Language) statements: used to create roles/permissions to control access to database by securing it, ex:
     - GRANT (authorizes one or more users to perform an operation or a set of operations on an object);
     - REVOKE (eliminates a grant, which may be the default grant);
  • TCL (Transactional Control Language): used to manage different transactions occurring within a database. This statements wrap DML operations, ex:
     - COMMIT;
     - ROLLBACK;
     - SAVEPOINT;
  • DQL (Data Query Language): basically used to query data from table...
     - SELECT;

Query Optimization

Use the Execution Plan of your DBMS to determine how your queries are being executed.
If you're new to this try googling something like: "execution plan mysql".

TODO: complete this section


DataBase Auditing

Auditing is monitoring and recording all user actions on a database. Among others you can base auditing on individual actions, such as database backup, change of user logins, insert (etc.) or on combination of factors. [1]

Why Audit? [1]

Though auditing has become a popular buzzword, many DBAs and developers still do not appreciate the requirement for auditing. Properly implemented, auditing can achieve the below:
  1. Ensure accountability for all actions -  in particular server, database, schema, and table operations.
  2. Discourage users from inappropriate actions due to their accountability.
  3. Assist the investigation of  suspicious activity.
  4. Notify an auditor of the actions of an unauthorized user.
  5. Monitor and gather data about specific database activities.
  6. Detect problems with an authorization or access control implementation.
  7. Ensure compliance with legal requirements, such as: Sarbanes-Oxley Act; Health Insurance Portability and Accountability Act (HIPAA); International Convergence of Capital Measurement and Standards: a Revised Frame work; Japan Privacy Law; European Union Directive of Privacy and Electronic Communications; Data Protect Act (UK);
Related Articles:
[1] - Auditing in SQL Server 2008
[2] - SQL Server Audit Articles
[3] - Understanding SQL Server Audit


TODO

AUTO_INCREMENT

References: Using AUTO_INCREMENT (MySQL Manual)

Auto-increment allows a unique number to be generated when a new record is inserted into a table.
CREATE TABLE Persons
(
P_Id int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)

By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record.
To let the AUTO_INCREMENT sequence start with another value, use the following SQL statement:

ALTER TABLE Persons AUTO_INCREMENT=100
To insert a new record into the "Persons" table, we will not have to specify a value for the "P_Id" column (a unique value will be added automatically):
INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen')
Alter a table column to be AUTO_INCREMENT
-- previous column was:
-- id_livro int not null,
ALTER TABLE livro MODIFY COLUMN id_livro INT NOT NULL AUTO_INCREMENT;


ALTER TABLE


References: ALTER TABLE Syntax

Examples:
ALTER TABLE Persons AUTO_INCREMENT=100
ALTER TABLE livro MODIFY COLUMN id_livro INT NOT NULL AUTO_INCREMENT;
TODO
TODO

Unique

ref: CREATE TABLE Syntax, When should I use a unique constraint instead of a unique index?, SQL UNIQUE Constraint, MySQL INDEXS, Differences between index, primary, unique, fulltext? MySQL

You can either use a UNIQUE CONSTRAINT or a UNIQUE INDEX: under the hood a unique constraint is implemented the same way as a unique index (because an index is needed to efficiently fulfill the requirement to enforce the constraint).
So for a database that supports both features the choice of which to use will often come down to preferred style and consistency. But to make it more obvious:
  • If you are planning to use the index as an index (i.e. your code may rely on searching/sorting/filtering on that field to be quick) I would explicitly use a unique index;
  • Likewise if you are only needing to enforce uniqueness as a business rule rather than the field needing to be searched or used for sorting then I'd use the constraint

Using a UNIQUE CONSTRAINT:

Ex1:
CREATE TABLE Book
(
P_Id int NOT NULL AUTO_INCREMENT,
Title varchar(255) NOT NULL,
ISBN varchar(255),
PRIMARY KEY (P_Id),
UNIQUE(ISBN)
)
Ex2:
To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple columns, use the following SQL syntax:
CREATE TABLE Book
(
P_Id int NOT NULL AUTO_INCREMENT,
Title varchar(255) NOT NULL,
ISBN varchar(255),
PRIMARY KEY (P_Id),
CONSTRAINT uc_Book UNIQUE(P_Id,ISBN)
)

Using a UNIQUE INDEX:

Ex:
CREATE TABLE Book
(
P_Id int NOT NULL AUTO_INCREMENT,
Title varchar(255) NOT NULL,
ISBN varchar(255),
PRIMARY KEY (P_Id),
UNIQUE INDEX (ISBN)
)
You can also create the index separately:
Syntax:
CREATE UNIQUE INDEX index_name
ON table_name ( column1, column2,...);

TODO
code
TODO
code
TODO
code
TODO
code
TODO
code

Attention when calling other batch files

Note that when a windows batch file issues a DOS command it will get back the control when that command finishes execution BUT it does not get the control again if it calls another batch file.

Ex. (incorrect):
dir
someOtherBatchFile.bat
dir
The last "dir" command wont be executed because the batch file wont get the control after "someOtherBatchFile.bat"  finishes execution.
The solution is using the call keyword:
Ex. (correct):
dir
call someOtherBatchFile.bat
dir

Variables

Ex.
::text
set someVar=hello
::the current folder
set batchFolder=%CD%
::a folder
set someFolder=C:\Programming\

::access the vars
ECHO %someVar%
cd %batchFolder%
dir
cd %someFolder%
dir

Pause

The pause command will wait for the user to press any key before the batch file proceeds.
Just write "pause" in the batch file.

Comments

::This is a comment

Print text

@ECHO OFF
ECHO HELLO WORLD

Batch Files examples

A batch file that calls DOS and ant commands to build and deploy a java project:
cls
@ECHO OFF
set batchFolder=%CD%
set FolderBookSearchXML=C:\Programming\ISFINAL\trab1\novo1\BookSearch
set FolderWebService=C:\Programming\ISFINAL\trab2\Trabalho2\Webservice
set FolderWSClient=C:\Programming\ISFINAL\trab2\Trabalho3\WebClient
::==================================== BookSearchXML ===========================
ECHO *
ECHO ******* CRIAR JAR DO BookSearchXML *******
ECHO *

cd %FolderBookSearchXML%
::call ant clean
call ant jar -q
ECHO ******* COPIAR JAR PARA O WEBSERVICE *******
xcopy dist\*.jar %FolderWebService%\lib\ /Y/F
ECHO ----- proximo passo: actualizar webservice -----
pause

::====================================== Webservice ===========================
ECHO *
ECHO ******* GERAR ARTEFACTOS DO WEBSERVICE *******
ECHO *

cd %FolderWebService%
call ant wsgen-BookSearchWS -q

ECHO ******* DEPLOY WEBSERVICE *******
call ant run-deploy -q
ECHO ----- proximo passo: actualizar cliente do webservice -----
pause

::================================ Cliente do webservice ========================
ECHO *
ECHO ******* GERAR ARTEFACTOS DO CLIENTE DO WEBSERVICE *******
ECHO *
cd %FolderWSClient%\src\java
::wsimport -verbose -keep http://localhost:8080/BookSearchWebservice/BookSearchWS?wsdl
wsimport -keep http://localhost:8080/BookSearchWebservice/BookSearchWS?wsdl
del mywspackage\*.class

ECHO ******* DEPLOY DO CLIENTE DO WEBSERVICE *******
cd %FolderWSClient%
::call ant clean
call ant run-deploy -q

::return to starting directory
cd %batchFolder%


A great tutorial: PL/SQL Tutorial

As the name implies, PL/SQL supports programming language features like conditional statements, iterative statements.
The programming constructs are similar to how you use in programming languages like Java and C++.

PL/SQL Structure:

DECLARE 
      Variable declaration 
BEGIN 
     Program Execution 
EXCEPTION 
      Exception handling
END;

Declare variables:

Syntax:
  • variable_name datatype [NOT NULL := value ];
    Dataypes:
    • Number (n,m);
    • Char (n);
    • Varchar2 (n)
    • Date;
    • Long;
    • Long raw;
    • Raw;
    • Blob;
    • Clob;
    • Nclob;
    • Bfile;
    Ex.:
    salary number(4);
    dept varchar2(10) NOT NULL := “HR Dept”; 
    Global vs Local variables:
    DECLARE
      myGlobalVar number; 
     BEGIN 
      myGlobalVar := 100;  
      DECLARE 
       myLocalVar number; 
    ...
    NOTE: You can also use %type to declare variables and constants (see "Records").

    Declare Constants:

    Syntax:
    • constant_name CONSTANT datatype := VALUE;  
    Ex.:
    • salary_increase CONSTANT number (3) := 10; 
    NOTE: You can use also %type to declare variables and constants (see "Records"). 

    Records

    Records are composite datatypes, which means it is a combination of different scalar datatypes like char, varchar, number etc. A record can be visualized as a row of data. It can contain all the contents of a row.

    Multiples ways of declaring records:
    Syntax Usage
    TYPE record_type_name IS RECORD (column_name1 datatype, column_name2 datatype, ...); Define a composite datatype, where each field is scalar.
    col_name table_name.column_name%type; Dynamically define the datatype of a column based on a database column.
    record_name record_type_name; Declare a record based on a user-defined type.
    record_name table_name%ROWTYPE; Dynamically declare a record based on an entire row of a table. Each column in the table corresponds to a field in the record.


    Multiple ways you can assign values to and from a record:
    Syntax Usage
    record_name.col_name := value; To directly assign a value to a specific column of a record.
    record_name.column_name := value; To directly assign a value to a specific column of a record, if the record is declared using %ROWTYPE.
    SELECT col1, col2 INTO record_name.col_name1, record_name.col_name2 FROM table_name [WHERE clause]; To assign values to each field of a record from the database table.
    SELECT * INTO record_name FROM table_name [WHERE clause]; To assign a value to all fields in the record from a database table.
    variable_name := record_name.col_name; To get a value from a record column and assigning it to a variable.


    Conditional statements

    Syntax:
    IF condition 1 
    THEN 
     statement 1; 
     statement 2; 
    ELSIF condtion2 THEN 
     statement 3; 
    ELSE 
     statement 4; 
    END IF;
    

    Loops

    There are three types of loops in PL/SQL:
    • Simple Loop (similar to Java Do While)
    • While Loop (similar to Java While)
    • For Loop  (similar to Java For)
    Simple Loop
    Syntax:
    LOOP 
         statements; 
          EXIT; 
           {or EXIT WHEN condition;}
    END LOOP;
    
    Ex:
    While Loop
    Syntax:
    WHILE <condition>
    LOOP statements;
    END LOOP;

    Ex:
    WHILE monthly_value <= 4000
    LOOP
         monthly_value := daily_value * 31;
    END LOOP;
    
    For Loop
    Syntax:
    FOR counter IN [reverse] lowest_val..highest_val
      LOOP statements; 
    END LOOP; 
    Ex.:
    for i in 1..1000 loop 
         insert into a values(i,i*2);
    end loop;
    
    for i in reverse 1..1000 loop 
        insert into a values(i,i*2);
    end loop; 
    

    Records

    A record can be visualized as a row of data. It can contain all the contents of a row. Records are composite datatypes, which means it is a combination of different scalar datatypes like char, varchar, number etc.  Each scalar data types in the record holds a value.
    NOTE: When you are creating a record, you are just creating a datatype, similar to creating a variable. You need to assign values to the record to use them.

    Declaration syntax:
    • TYPE record_type_name IS RECORD
      (first_col_name column_datatype,
      second_col_name column_datatype, ...);
      NOTE: If a field is based on a column from database table, you can define the field_type as follows:
      col_name table_name.column_name%type;
    • If all the fields of a record are based on the columns of a table, we can declare the record as follows:
      record_name table_name%ROWTYPE;

    Access syntax:
    Different ways you can assign values to and from a record:
    Syntax Usage
    record_name.col_name := value; To directly assign a value to a specific column of a record.
    record_name.column_name := value; To directly assign a value to a specific column of a record, if the record is declared using %ROWTYPE.
    SELECT col1, col2 INTO record_name.col_name1, record_name.col_name2 FROM table_name [WHERE clause]; To assign values to each field of a record from the database table.
    SELECT * INTO record_name FROM table_name [WHERE clause]; To assign a value to all fields in the record from a database table.
    variable_name := record_name.col_name; To get a value from a record column and assigning it to a variable.


    Triggers

    A trigger is a pl/sql block structure which is fired when a DML statements like Insert, Delete, Update is executed on a database table. A trigger is triggered automatically when an associated DML statement is executed.

    Syntax:
    CREATE [OR REPLACE ] TRIGGER trigger_name 
     {BEFORE | AFTER | INSTEAD OF } 
     {INSERT [OR] | UPDATE [OR] | DELETE} 
     [OF col_name] 
     ON table_name 
     [REFERENCING OLD AS o NEW AS n] 
     [FOR EACH ROW] 
     WHEN (condition)  
     BEGIN 
       --- sql statements  
     END; 

    NOTE:
    Triggers can be fired in two different ways: by statement or by row.
    For example and update statement that updates many rows of a table can fire a trigger only one time (statement level trigger) or one time for each row updated (row level trigger).
    To specify this you use the [FOR EACH ROW] - This clause is used to determine whether a trigger must fire when each row gets affected ( i.e. a Row Level Trigger) or just once when the entire sql statement is executed (i.e.statement level Trigger).








    Ex.:
    CREATE or REPLACE TRIGGER price_history_trigger 
    BEFORE UPDATE OF unit_price 
    ON product 
    FOR EACH ROW 
    BEGIN 
    INSERT INTO product_price_history 
    VALUES 
    (:old.product_id, 
     :old.product_name, 
     :old.supplier_name, 
     :old.unit_price); 
    END; 
    
     CREATE or REPLACE TRIGGER After_Update_Row_product 
     AFTER  
     insert On product 
     FOR EACH ROW 
     BEGIN 
     INSERT INTO product_check 
     Values('After update, Row level',sysdate); 
     END; 
    

    Cursors

    There are 2 types of cursors:
    • Implicit cursors:
      These are created by default when DML statements like, INSERT, UPDATE, and DELETE statements are executed. They are also created when a SELECT statement that returns just one row is executed.
      Oracle provides few attributes called as implicit cursor attributes to check the status of DML operations. The cursor attributes available are %FOUND, %NOTFOUND, %ROWCOUNT, and %ISOPEN.
    • Explicit cursors:They must be created when you are executing a SELECT statement that returns more than one row. Even though the cursor stores multiple records, only one record can be processed at a time, which is called as current row. When you fetch a row the current row position moves to next row.
      Both implicit and explicit cursors have the same functionality, but they differ in the way they are accessed.

    Implicit Cursor example:
    DECLARE var_rows number(5);
    BEGIN
     UPDATE employee 
     SET salary = salary + 1000;
     IF SQL%NOTFOUND THEN
     dbms_output.put_line('None of the salaries where updated');
     ELSIF SQL%FOUND THEN
     var_rows := SQL%ROWCOUNT;
     dbms_output.put_line('Salaries for ' || var_rows || 'employees are updated');
     END IF; 
    END; 
    
    

    Explicit Cursor example:
    Ex1 (source)
    DECLARE 
        /* DECLARANDO O CURSOR DE PRODUTOS */
        CURSOR C_PRODUTOS IS 
            SELECT * FROM TB_PRODUTOS;
            
        /* DECLARANDO UMA VARIAVEL QUE SERA O REGISTRO DA TABELA */
        REG_PRODUTO C_PRODUTOS%ROWTYPE;
        
    BEGIN
        /* ABRE CURSOR */
        OPEN C_PRODUTOS
        
        LOOP
            /* LÊ UM REGISTRO DO CURSOR */
            FETCH C_PRODUTOS INTO REG_PRODUTO;
            
            /* ABANDONA O LOOP CASO SEJA O FINAL DO CURSOR */
            EXIT WHEN C_PRODUTOS%NOTFOUND;
            
            /* 
            AQUI SERA INSERIDO O CODIGO QUE IRA MANIPULAR OS DADOS COMO: 
                - INSERIR EM OUTRA TABELA, 
                - FAZER ALGUM CALCULO, 
                - ETC. 
            */
        
        END LOOP;
        
        /* FECHA O CURSOR */
        CLOSE C_PRODUTOS
    END;
    

    TODO

    TODO

    TODO

    XSL

    Documentation and tutorials


    What is XSL?

    • XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents.
    • XSLT stands for XSL Transformations. You can use XSLT to transform XML documents into other formats, like XHTML or other XML documents.
    XSL is more than a Style Sheet Language, XSL consists of three parts:
    • XSLT - a language for transforming XML documents (so, XSLT is just a part of XSL). XSLT is a language for transforming XML documents into XHTML documents or to other XML documents.
    • XPath - a language for navigating in XML documents. XPath is a language for navigating in XML documents.
    • XSL-FO - a language for formatting XML documents

    What is XSLT?

    • XSLT stands for XSL Transformations
    • XSLT is the most important part of XSL
    • XSLT transforms an XML document into another XML document (or even to another type of XML doc like XHTML);
    • XSLT uses XPath to navigate in XML documents
    • XSLT is a W3C Recommendation
    XSLT is the most important part of XSL. XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element.
    With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more.
    XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents.

    XSLT 2.0

    This new version has many new features like the new element
    xsl:for-each-group
    to group information.
    There was no grouping feature on V1.0 and this was usually done using what became known has "Muenchian Grouping" technique. This article demonstrates the Muenchian Grouping and the new way of doing it using xsl:for-each-group.

    XSLT support

    Browser support:

    XSLT processor implementations:
    There are a couple of XSLT processors available for many platforms like:
     This book quote has instructions on how to install this XSLT processors.

    XSLT tools:
    • XSLT Debugger for NetBeans (official documentation with GUI tutorial on how to use it) - seems to use the Xalan XSLT processor so only has support for XSLT 1.0;
    • Altova's XMLSpy, a fast XML Editor with support for XSLT 2.0 - includes a powerful XSLT debugger for troubleshooting and perfecting XSLT 1.0 and 2.0 stylesheets.

    XSLT examples



    XSL-FO ("Extensible Stylesheet Language Formatting Objects")

    (aka XSLFO or by its parent name "XSL")

    What is XSL-FO?

    • XSL-FO is about formatting XML data for output to screen, paper or other media.
    • XSL-FO stands for Extensible Stylesheet Language Formatting Objects
    • XSL-FO is based on XML
    • XSL-FO is a W3C Recommendation
    • XSL-FO is now formally named XSL (or you can think of it has a part of XSL)

    XSL-FO examples


    Disciplina: IRC (Introdução ás Redes de Computadores)
    Recursos: