db2

An overview of databases using SQL / PLSQL / PostgreSQL.

Get Started. It's Free
or sign up with your email address
Rocket clouds
db2 by Mind Map: db2

1. *

1.1. INDEX

1.1.1. e.g: CREATE [UNIQUE] INDEX index_name ON table_name [column_name1,...]

1.1.1.1. an index is a pointer to data in a table.

1.1.1.1.1. Used for

1.1.1.1.2. Not indicated for:

1.1.1.2. [column_name,...] is optional and creates the index for only those columns specified. Can be single-column indexes and composite intexes

1.1.1.3. [UNIQUE] is optional and creates unique values within the index . Helps with performance and integrity

1.1.2. e.g: DROP INDEX intex_name

1.2. STORED PROCEDURES

1.2.1. These are PL/SQL functions, packages or procedures. Running any of the following commands will place you in PL/SQL mode

1.2.1.1. CREATE FUNCTION

1.2.1.2. CREATE LIBRARY

1.2.1.3. CREATE PACKAGE

1.2.1.4. CREATE PACKAGE BODY

1.2.1.5. CREATE PROCEDURE

1.2.1.6. CREATE TRIGGER

1.2.1.7. CREATE TYPE

1.3. DESCRIBE

1.3.1. addhere

2. MANIPULACAO - DML

2.1. Insert

2.1.1. e.g: INSERT INTO Employees [(EmployeeID,LastName,FirstName,...)] VALUES (14, 'Tenzin', 'Gyatso', 07/06/1935, 'dl14.pic' , 'The 14th Dalai Lama')

2.1.1.1. Specifying the field names before the VALUES is [optional], but useful when you only need to insert to certain fields and leave the others NULL. Otherwise, you input values in the order they appear in the table structure

2.2. Update

2.2.1. e.g: UPDATE Employees SET LastName='Gyatso', FirstName='Tenzin' [WHERE] EmployeeID=14

2.2.1.1. WHERE is not mandatory, but if not present it will evaluate to 1 and UPDATE the whole table only with 'Tenzin Gyatso'. That's not usualy something that you want

2.3. Delete

2.3.1. ```SQL e.g: DELETE FROM Employees [WHERE] EmployeeID=14 ```

2.3.1.1. WHERE is not mandatory, but if not present it will evaluate to 1 and DELETE the whole table records. That's not usualy something that you want

3. DEFINICAO - DDL

3.1. Create objects (tables, views, triggers, store procedures), change the structure of databases, delete objects altogether or just its contents.

3.2. CREATE ROLE role_chosen_name [identified by parola_mea_secreta]

3.3. Create

3.3.1. CREATE TABLE SpecialPersonnel( Name CHAR(25) NOT NULL, SecurityClearence CHAR (1) NOT NULL, LastLogin TIME DateOfEmployment DATE, PRIMARY KEY( EmployeeID ) UNIQUE );

3.3.1.1. Tells the database you want to create a table with the specified FieldNames and DataTypes. NOT NULL is not mandatory but it specifies that the field can't ever be NULL. The same with UNIQUE, they can both be used on the same fieldname

3.3.2. CREATE [OR REPLACE] VIEW view_name AS SELECT column_name from table_name WHERE condition

3.4. Alter

3.4.1. ALTER TABLE SpecialPersonnel [ADD / DROP COLUMN / MODIFY COLUMN / MODIFY / ADD CONSTRAINT / DROP CONSTRAINT / DROP CONSTRAINT / DROP PRIMARY KEY] <....>

3.4.1.1. ALTER TABLE SpecialPersonnel ADD Age INTEGER

3.4.1.1.1. Adds a new column with name and data type

3.4.1.2. ALTER TABLE SpecialPersonnel DROP COLUMN Age

3.4.1.2.1. Deletes a column specified

3.4.1.3. ALTER TABLE SpecialPersonnel MODIFY COLUMN Age INTEGER [NOT NULL]

3.4.1.3.1. NOT NULL specifies that the Age can't be null on insertion/altering

3.4.1.4. ALTER TABLE SpecialPersonnel ADD CONSTRAINT MyConstraint CHECK (SecurityClearence >3)

3.4.1.4.1. Sets a condition for the SecurityClearance that it should be more >3, otherwise it can't exist in this table (when inserted, updated..).

3.4.1.5. ALTER TABLE SpecialPersonnel ADD CONSTRAINT AddPKConstraint UNIQUE (Name,PRIMARYKEY)

3.4.1.5.1. Sets the constraint that the Name and PRIMARY KEY fields have to be UNIQUE

3.4.1.6. ALTER TABLE SpecialPersonnel ADD PRIMARY KEY (PrimaryKey..);

3.4.1.6.1. Adds a Primary Key constraint to a field

3.4.1.7. ALTER TABLE SpecialPersonnel DROP CONSTRAINT MyUniquePKConstraint / MyConstraint

3.4.1.7.1. Removes a NamedConstrained from the table

3.5. Drop

3.5.1. DROP TABLE SpecialPersonnel

3.5.1.1. Deletes the TABLE from the database. It notifies by confirming 'Query OK'. If we use DESC SpecialPersonnel afterwards, we get an error saying it doesn't exist

4. PL/SQL Language overview

4.1. Declarations

4.1.1. Optional section where variables, cursors, subprograms and other elements are defined to be used later on in the program

4.1.2. DECLARE

4.1.2.1. Variables (global or local)

4.1.2.1.1. String

4.1.2.1.2. Integer

4.1.2.1.3. Real

4.1.2.1.4. Date

4.1.2.1.5. Boolean

4.1.2.2. Constants

4.1.2.2.1. varname CONSTANT datatype := value;

4.1.2.3. Procedures

4.1.2.3.1. CREATE OR REPLACE PROCEDURE prcd_name IS BEGIN dbms_output.put_line('This is the output'); END; /

4.1.2.3.2. CREATE PROCEDURE prc_name (i IN DOUBLE, f IN INTEGER h IN OUT FLOAT) IS BEGIN IF i<f THEN h:=f; ELSE h:=i;

4.1.2.4. Functions

4.1.2.4.1. CREATE FUNCTION F_Counter (i IN FLOAT, j IN OUT DOUBLE) RETURN INTEGER IS count_var INTEGER:=0; BEGIN SELECT count(*) INTO count_var FROM employees WHERE emp_id>90; RETURN count_var; END;/

4.1.2.4.2. CREATE OR REPLACE F_Sentence (i IN INTEGER) RETURN VARCHAR2(20) IS word_var; BEGIN IF i>10 word_var='LessThan 10' ELSE IF i<20 word_var='Less than 20' ELSE word_var='More'; RETURN word_var; END;

4.1.2.5. Cursors

4.1.2.5.1. CURSOR c_name IS SELECT * FROM employees WHERE id>0;

4.1.2.6. Triggers

4.1.2.6.1. triggers ~ stored programs which are automatically executed or fired in response to an event upon a table, view, schema or database

4.1.2.6.2. CREATE TRIGGER budget_rules BEFORE UPDATE OF salary ON employees WHEN (NEW.salary>1.5*OLD.salary)

4.1.2.6.3. create or replace trigger emp_bid before insert or delete FOR EACH ROW on employee referencing new as new3r old as old3r begin if to_char(sysdate,'Dy') in ('Sat','Sun') then raise_application_error(-29999,'No create/delete employees on weekend!'); end if; end; /

4.1.2.7. Exceptions (User-defined)

4.1.2.7.1. customError EXCEPTION;

4.2. Executable Commands

4.2.1. Mandatory section, consists of PL/SQL statements of the program. Must have at least one executable line of code (NULL included). Between Begin and End

4.2.2. BEGIN and END

4.2.2.1. Operators

4.2.2.1.1. + | - | * | / | **

4.2.2.1.2. = | != | <> | ~= | > | < | >= | <=

4.2.2.1.3. LIKE | BETWEEN | IN | IS | NULL

4.2.2.1.4. AND | OR | NOT

4.2.2.2. Conditions

4.2.2.2.1. IF - THEN

4.2.2.2.2. IF - THEN - ELSE

4.2.2.2.3. IF - THEN - ELSIF

4.2.2.2.4. CASE statemet

4.2.2.2.5. IF - THEN (nested)

4.2.2.3. Loops

4.2.2.3.1. LOOP

4.2.2.3.2. WHILE LOOP

4.2.2.3.3. FOR LOOP

4.2.2.3.4. LOOP (nested)

4.2.2.3.5. <<others

4.2.3. Using cursors

4.2.3.1. 1. OPEN cr_name;

4.2.3.2. 2. FETCH cr_name INTO cr_name, cr_id;

4.2.3.3. 3. CLOSE cr_name;

4.2.4. Using procedures

4.2.4.1. executing procedure

4.2.4.1.1. from a pl/sql block

4.2.4.1.2. from the sql prompt

4.2.4.2. deleting procedures

4.2.4.2.1. DROP PROCEDURE prcd_name;

4.2.5. Using functions

4.2.5.1. executing function

4.2.5.1.1. from the pl sql block

4.2.5.1.2. from sql prompt

4.2.5.2. deleting functions

4.2.5.2.1. DROP FUNCTION F_Counter

4.3. Exception Handling

4.3.1. Optional section that contains exceptions that handle program errors. Between Begin and End

4.3.2. BEGIN and END

4.3.2.1. EXCEPTION

4.3.2.1.1. WHEN ErrorNo_data_found THEN dbmps_output.put_line('No such entry');

4.3.2.2. Raising Exceptions

4.3.2.2.1. IF table.var > 10 THEN RAISE ExceptionName; END IF;

4.3.2.2.2. IF table.string = NULL THEN RAISE customError; END IF;

4.4. others

4.4.1. to run code from sql command line you might need to type / at the end of the program, on a new line

4.4.2. Package

4.4.2.1. Package Specification

4.4.2.1.1. DECLARE variables, subprograms, etc that can be referenced from outside the package

4.4.2.2. Package Body Definition

4.4.2.2.1. CREATE the code and instructions inside the subprogram

4.4.2.3. Using package

4.4.2.3.1. pack_name.prc_name(2.4,6.8);

4.4.3. Record

4.4.3.1. addhere

4.4.4. -- comments are preceded by --