Thursday, November 19, 2009

Read Consistency

Read Consistency :
  • Read consistency guarantees a consistent view of the data at all times.
  • Changes made by one user do not conflict with changes made by another user.
  • Read consistency ensures that on the same data :
  • Readers do not wait for writers
  • Writers do not wait for readers
Implementation of Read Consistency :

Wednesday, November 18, 2009

State of the Data Before COMMIT or ROLLBACK

  • The previous state of the data can be recovered.
  • The current user can review the results of the DML operations by using the SELECT statement.
  • Other users cannot view the results of the DML statements by the current user.
  • The affected rows are locked; other users cannotchange the data in the affected rows.
State of the Data After COMMIT :
  • Data changes are made permanent in the database.
  • The previous state of the data is permanently lost.
  • All users can view the results.
  • Locks on the affected rows are released; those rows are available for other users to
    manipulate.
  • All savepoints are erased.
Committing Data :

Make the changes :
  • BEGIN
  • DELETE FROM emp WHERE empno = 7090;
  • INSERT INTO dept VALUES (290, 'Corporate Tax', NULL);
Commit the changes :
  • COMMIT;
State of the Data After ROLLBACK :
Discard all pending changes by using the ROLLBACK statement :
  • Data changes are undone.
  • Previous state of the data is restored.
  • Locks on the affected rows are released.
  • BEGIN;
  • DELETE FROM copy_emp;
  • DELETE 20
  • ROLLBACK ;
  • Rollback

Tuesday, November 17, 2009

Implicit Transaction Processing

Controlling Transactions :



An automatic commit occurs under the following circumstances :
  • DML is issued
  • DDL statement is issued
  • DCL statement is issued
  • Normal exit from psql, without explicitly issuing COMMIT or ROLLBACK statements
  • An automatic rollback occurs under an abnormal termination of psql or a system failure.
System Failures :
  • When a transaction is interrupted by a system failure, the entire transaction is automatically rolled back. This prevents the error from causing unwanted changes to the data and returns the tables to their state at the time of the last commit. In this way, the PostgreSQL server protects the integrity of the tables.

Monday, November 16, 2009

Database Transactions

A database transaction consists of one of the following :
  • DML statements that constitute one consistent change to the data
  • One DDL statement
  • One data control language (DCL) statement
  • PostgreSQL commit automatically when you perform a transaction.
  • You can explicitly start a transaction using BEGIN keyword.
  • Transaction that are explicitly started using BEGIN end with one of the following events:
    • A COMMIT or ROLLBACK statement is issued.
    • The user exits psql.
    • The system crashes.
Advantages of COMMIT and ROLLBACK Statements :
With COMMIT and ROLLBACK statements, you can :
  • Ensure data consistency
  • Preview data changes before making changes permanent
  • Group logically related operations

Controlling Transactions :

Sunday, November 15, 2009

TRUNCATE Statement

  • Removes all rows from a table, leaving the table empty and the table structure intact
  • Is a data definition language (DDL) statement rather than a DML statement; cannot easily be undone
  • Syntax : TRUNCATE TABLE table_name;
  • Example : TRUNCATE TABLE copy_emp;

Saturday, November 14, 2009

DELETE Statement

You can remove existing rows from a table by using the DELETE statement :
  • DELETE [FROM] table [WHERE condition];
Deleting Rows from a Table :
Specific rows are deleted if you specify the WHERE clause :
  • DELETE FROM dept WHERE dname = 'Finance';
All rows in the table are deleted if you omit the WHERE clause :
  • DELETE FROM copy_emp;
Deleting Rows Based on Another Table :
  • Use subqueries in DELETE statements to remove rows from a table based on values from another table :
  • DELETE FROM emp WHERE deptno = (SELECT deptno FROM dept WHERE dname =‘SALES’);

Friday, November 13, 2009

UPDATE Statement Syntax

Modify existing rows with the UPDATE statement :
  • UPDATE table SET column = value [, column = value, ...] [WHERE condition];
  • Update more than one row at a time (if required).
Updating Rows in a Table :

Specific row or rows are modified if you specify the WHERE clause :
  • UPDATE emp SET deptno = 40 WHERE empno = 7654;
All rows in the table are modified if you omit the WHERE clause :
  • UPDATE copy_emp SET deptno = 110;
Updating Two Columns with a Subquery :

  • Update employee 7654’s job and sal to match that of employee 7698
UPDATE emp SET job = (SELECT job FROM emp WHERE empno = 7698), sal = (SELECT sal FROM emp WHERE empno = 7698) WHERE empno = 7654;

Thursday, November 12, 2009

DML - Inserting Special Values

Inserting Special Values :
  • The CURRENT_DATE function records the current date and time.
  • INSERT INTO emp (empno, ename,hiredate, job, sal, comm, mgr,deptno) VALUES (1132, 'Louis',CURRENT_DATE, 'AC_ACCOUNT', 7000, NULL, 7768, 70);
Inserting Specific Date Values :

Add a new employee :
  • INSERT INTO emp (empno, ename,hiredate, job, sal, comm, mgr,deptno) VALUES ( 1114,'Den', TO_DATE('FEB 3, 1999', 'MON DD, YYYY'), 'AC_ACCOUNT', 3600, NULL, 7900, 40);
Verify your addition :
  • Select*from emp;
Creating a Script :
  • Open vi editor using vi command
  • Type in your insert command
  • Save your file using :wq filename
  • Use psql –f option to execute the stored file in training database.
  • E.g
    ./psql –f in.sql –d training –U postgres –p 5432
Copying Rows from Another Table :

Write your INSERT statement with a subquery :
  • INSERT INTO sales_reps(id, name, sal, comm)
    SELECT empno, ename, sal, comm
    FROM emp WHERE job=‘SALESMAN’;
  • Do not use the VALUES clause.
  • Match the number of columns in the INSERT clause to those in the subquery.

Wednesday, November 11, 2009

DML Statements

Manipulating Data using DML

Data Manipulation Language :
A DML statement is executed when you :
  • Add new rows to a table
  • Modify existing rows in a table
  • Remove existing rows from a table
  • A transaction consists of a collection of DML statements that form a logical unit of work.

INSERT Statement Syntax :
Add new rows to a table by using the INSERT statement :
  • INSERT INTO table [(column [, column...])] VALUES (value [, value...])[,(value [,value..])];
  • With this syntax, only multiple rows are inserted at a time.
Inserting New Rows :
  • Insert a new row containing values for each column.
  • List values in the default order of the columns in the table.
  • Optionally, list the columns in the INSERT clause.
  • Enclose character and date values in single quotation marks.
  • INSERT INTO dept(deptno, dname, loc) VALUES (50, 'Public Relations',’EDMONTON’);
Inserting Rows with Null Values :
Implicit method :
  • Omit the column from the column list.
  • INSERT INTO dept (deptno, dname ) VALUES (60, 'Purchasing');
Explicit method :
  • Specify the NULL keyword in the VALUES clause.
  • INSERT INTO dept VALUES (70, 'Finance', NULL);

Followers