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,...] an index is a pointer to data in a table. Used for Not indicated for: [column_name,...] is optional and creates the index for only those columns specified. Can be single-column indexes and composite intexes [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.1. These are PL/SQL functions, packages or procedures. Running any of the following commands will place you in PL/SQL mode CREATE FUNCTION CREATE LIBRARY CREATE PACKAGE CREATE PACKAGE BODY CREATE PROCEDURE CREATE TRIGGER CREATE TYPE


1.3.1. addhere


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') 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 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 ``` 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.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 ); 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] <....> ALTER TABLE SpecialPersonnel ADD Age INTEGER Adds a new column with name and data type ALTER TABLE SpecialPersonnel DROP COLUMN Age Deletes a column specified ALTER TABLE SpecialPersonnel MODIFY COLUMN Age INTEGER [NOT NULL] NOT NULL specifies that the Age can't be null on insertion/altering ALTER TABLE SpecialPersonnel ADD CONSTRAINT MyConstraint CHECK (SecurityClearence >3) Sets a condition for the SecurityClearance that it should be more >3, otherwise it can't exist in this table (when inserted, updated..). ALTER TABLE SpecialPersonnel ADD CONSTRAINT AddPKConstraint UNIQUE (Name,PRIMARYKEY) Sets the constraint that the Name and PRIMARY KEY fields have to be UNIQUE ALTER TABLE SpecialPersonnel ADD PRIMARY KEY (PrimaryKey..); Adds a Primary Key constraint to a field ALTER TABLE SpecialPersonnel DROP CONSTRAINT MyUniquePKConstraint / MyConstraint Removes a NamedConstrained from the table

3.5. Drop

3.5.1. DROP TABLE SpecialPersonnel 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 Variables (global or local) String Integer Real Date Boolean Constants varname CONSTANT datatype := value; Procedures CREATE OR REPLACE PROCEDURE prcd_name IS BEGIN dbms_output.put_line('This is the output'); END; / 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; Functions 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;/ 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; Cursors CURSOR c_name IS SELECT * FROM employees WHERE id>0; Triggers triggers ~ stored programs which are automatically executed or fired in response to an event upon a table, view, schema or database CREATE TRIGGER budget_rules BEFORE UPDATE OF salary ON employees WHEN (NEW.salary>1.5*OLD.salary) 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; / Exceptions (User-defined) 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 Operators + | - | * | / | ** = | != | <> | ~= | > | < | >= | <= LIKE | BETWEEN | IN | IS | NULL AND | OR | NOT Conditions IF - THEN IF - THEN - ELSE IF - THEN - ELSIF CASE statemet IF - THEN (nested) Loops LOOP WHILE LOOP FOR LOOP LOOP (nested) <<others

4.2.3. Using cursors 1. OPEN cr_name; 2. FETCH cr_name INTO cr_name, cr_id; 3. CLOSE cr_name;

4.2.4. Using procedures executing procedure from a pl/sql block from the sql prompt deleting procedures DROP PROCEDURE prcd_name;

4.2.5. Using functions executing function from the pl sql block from sql prompt deleting functions 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 EXCEPTION WHEN ErrorNo_data_found THEN dbmps_output.put_line('No such entry'); Raising Exceptions IF table.var > 10 THEN RAISE ExceptionName; END IF; 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 Package Specification DECLARE variables, subprograms, etc that can be referenced from outside the package Package Body Definition CREATE the code and instructions inside the subprogram Using package pack_name.prc_name(2.4,6.8);

4.4.3. Record addhere

4.4.4. -- comments are preceded by --