What is a Database Management System

A database management system (DBMS) is a collection of interrelated data and a set of programs to access those data. The collection of data, usually referred to as database, contains information relevant to an enterprise.

Database system versus File System

Keeping organizational information in file-processing system has a number of major disadvantages:

Since different programmers create the files and application programs over a long period, the various files are likely to have different formats and the programs may be written in several programming languages. Moreover, the same information may be duplicated several places (files).For example, the address and telephone no. of a particular customer may appear in a file that consists of saving-account records and in a file that consists of checking-account records. This redundancy leads to higher storage and access cost.

In addition, it may lead to inconsistency; that is, the various copies of the same data may no longer agree. For example, a changed customer address may be reflected in saving-account records but not elsewhere in the system.

Suppose that one of the bank officers needs to find out the names of all customers who live within particular postal-code area. The officer asks the data-processing department to generate such a list. Because the designers of the original system did not anticipate this request, there is no application program on hand to meet it. There is, however, an application program to generate the list of all customers. The bank officer has now two choices: either obtain the list of all customers and extract the needed information manually or ask a system programmer to write the necessary application program.

The point here is that conventional file-processing environments do not allow needed data to be retrieved in a convenient and efficient manner.

Because data are scattered in various files, and files may be in different formats, writing new application programs to retrieve the appropriate data is difficult.

The data values stored in the database must satisfy certain type of consistency constraints. For example, the balance of a bank account may never fall below a prescribed amount. Developers enforce these constraints in the system by adding appropriate code in the various application programs. However, when new constraints added, it is difficult to change the programs to enforce them.

A computer system, like any other mechanical or electrical device, is subjected to failure. In many applications, it is crucial that, if a failure occurs, the data be restored to the consistent state that existed prior to the failure. Consider a program to transfer $50 from account A to account B. If a system failure occurs during the execution of the program it is possible that the $50 was removed from the account A but was not credited to account B, resulting in an inconsistent database state. The funds transfer must be atomic- it must happen in its entirely or not all. It is difficult to ensure atomicity in a conventional file-processing system.

For the sake of overall performance of the system and faster response, many systems allow multiple users to update the data simultaneously. In such environment, interaction of concurrent updates may result in inconsistent data. Consider bank account A, containing $500. If two customers withdraw funds (say $50 and $100 respectively) from account A at the about same time, the result of the concurrent executions may leave incorrect state. To guard against this possibility, the system must maintain some form of supervision. But supervision is difficult to provide because data may be accessed by many different application programs that have not been coordinated previously.

Not every user of the database system should be able to access all the data. For example, in a banking system, payroll personnel need to see only that part of the database that has information about the various bank employees. They do not need access to information about customer accounts. But since application programs are added to the system in an ad hoc manner, enforcing such security constraints is difficult.

SQL

What Can SQL do?

SQL Statements

SELECT

Data retrieval

INSERT

UPDATE

DELETE

MERGE

Data manipulating language(DML)

CREATE

ALTER

DROP

RENAME

TRUNCATE

Data definition language(DDL)

COMMIT

ROLLBACK

SAVEPOINT

Transaction control

GRANT

REVOKE

Data control language(DCL)

Statement

Description

SELECT

Retrieves data from database

INSERT

UPDATE

DELETE

MERGE

Enter new rows, changes existing rows and removes unwanted rows from tables in the database, respectively.

CREATE

ALTER

DROP

RENAME

TRUNCATE

Set up changes and removes data structures from the tables.

COMMIT

ROLLBACK

SAVEPOINT

Manages the changes made by DML statements. Changes to the data can be grouped together into logical transactions.

GRANT

REVOKE

Gives or remove access rights to both the Oracle database and the structure with in it.

SELECT Statement

SELECT [ALL|DISTINCT] columns/expr [AS new name]

FROM table_name | view_name

[WHERE conditions]

[GROUP BY fields]

[HAVING conditions]

[ORDER BY column/integer [ASC|DESC]]

SELECT

Specifies the columns or fields to be included in the result set.

DISTINCT

Use to avoid duplicates.

FROM

Identifies the tables or views from which the data will be retrieved.

WHERE

The predicate(s) that determine which rows will be retrieved.

GROUP BY

Use to group output by the field with duplicate values and apply group functions to the grouped data.

HAVING

Use to place a condition on results of group function calculations.

ORDER BY

Determines the sequence of the rows (Default order is ascending).

SELECT ename, age, add as “Address”, sal “Salary”

 FROM emp

WHERE sal>10000

ORDER BY sal DESC;

INSERT Statement

INSERT...VALUES and INSERT...SELECT

INSERT INTO table_name

(col1, col2...)

VALUES (value1, value2...)

INSERT INTO table_name

(col1, col2...)

SELECT col1, col2...

FROM tablename

WHERE search_condition

SELECT ITEM, WORTH, REMARKS

FROM COLLECTION;

UPDATE Statement

UPDATE table_name

SET columnname1 = value1

[, columname2 = value2]...

WHERE search_condition

Update collection

Set worth = 900, item = ball

Where item = 'STRING';

DELETE Statement

DELETE FROM table_name

WHERE condition;

DELETE FROM COLLECTION

WHERE WORTH < 275;

Table

Creating a Table

CREATE TABLE [Schema].table_name

(

column_name datatype [DEFAULT expr][column_constraint]

……….

[table_constraint]

);

CREATE TABLE emp

(rollno number(5) CONSTRAINT emp_rollno_pk PRIMARY KEY,

name varchar2(20) CONSTRAINT emp_name_nn NOT NULL,

age number(2),

CONSTRAINT emp_age_chk CHECK(age>20));

Modifying a table

ALTER TABLE table_name

<ADD column_name data_type; |

MODIFY column_name data_type;>

MODIFY NAME CHAR (40);

ADD COMMENTS CHAR (80);

Removing a table

DROP TABLE table_name;

DROP TABLE team;

Generic data types

Operators in SQL

FROM PRICE;

WHERE AREACODE > 300;

WHERE AREACODE >= 300;

WHERE STATE! = 'CA';

WHERE LOCATION LIKE '%BACK%';

WHERE STATE LIKE 'C_';

WHERE STATE IN ('CA', 'CO', 'LA');

FROM TEAM

WHERE WINWORLDCUP IS NULL;

FROM PRICE

WHERE WHOLESALE BETWEEN 0.25 AND 0.75;

FROM VACATION

WHERE LASTNAME LIKE 'B%'

AND

YEARS * 12 - LEAVETAKEN > 50;

FROM VACATION

WHERE YEARS >= 5

OR

((YEARS *12)-LEAVETAKEN)/(YEARS * 12) >= 0.50;

WHERE LASTNAME NOT LIKE 'B%';

FROM SOFTBALL

UNION

SELECT NAME FROM FOOTBALL;

UNION ALL

SELECT NAME FROM FOOTBALL;

INTERSECT

SELECT * FROM FOOTBALL;

MINUS

SELECT * FROM FOOTBALL;

SQL Functions

FROM NUMBERS;

FROM NUMBERS;

FROM NUMBERS;

FROM NUMBERS;

FROM NUMBERS;

FROM CHARACTERS;

FROM CHARACTERS;

FROM CHARACTERS;

FROM CHARACTERS;

FROM CHARACTERS;

FROM CHARACTERS;

FROM CHARACTERS;

FROM CHARACTERS;

FROM CHARACTERS;

FROM TEAMSTATS;

FROM TEAMSTATS;

FROM TEAMSTATS;

FROM TEAMSTATS

WHERE HITS = MAX (HITS);

FROM TEAMSTATS;

FROM TEAMSTATS

WHERE HITS/AB < .35;

FROM TEAMSTATS;

FROM TEAMSTATS;

WHERE STARTDATE > SYSDATE;

FROM PROJECT;

FROM PROJECT;

FROM PROJECT;

FROM CONVERT;

FROM CONVERT;

Constraint

Joining Tables

Types of joins

one_table INNER JOIN another_table

ON one_table.some_field = another_table.another_field

SELECT FilmName, FirstName, LastName, State, YearReleased, YEAR (DateOfBirth) AS YearOfBirth

FROM MemberDetails INNER JOIN Films ON Films.YearReleased >= YEAR (MemberDetails.DateOfBirth)

WHERE State = ‘Golden State’ ORDER BY LastName, FirstName;

SELECT FirstName, LastName, Category.Category

FROM MemberDetails INNER JOIN FavCategory

ON MemberDetails.MemberId = FavCategory.MemberId

INNER JOIN Category

ON FavCategory.CategoryId = Category.CategoryId

ORDER BY LastName, FirstName;

SELECT Category, Street

FROM Category CROSS JOIN Location

ORDER BY Street;

SELECT MD1.FirstName, MD1.LastName, MD2.FirstName, MD2.LastName,

MD1.ZipCode, MD2.ZipCode, MD1.Street, MD2.Street

FROM MemberDetails AS MD1 INNER JOIN MemberDetails AS MD2

ON MD1.Street = MD2.Street AND MD1.ZipCode = MD2.ZipCode;

SELECT column_list

FROM left_table LEFT OUTER JOIN right_table

ON condition

SELECT column_list

FROM left_table LEFT OUTER JOIN right_table

ON condition

SELECT Location.Street, MemberDetails.Street

FROM Location LEFT OUTER JOIN MemberDetails

ON Location.Street = MemberDetails.Street;

SELECT column_list

FROM left_table RIGHT OUTER JOIN right_table

ON condition

SELECT Location.Street, MemberDetails.Street

FROM Location RIGHT OUTER JOIN MemberDetails

ON Location.Street = MemberDetails.Street;

SELECT column_list

FROM left_table FULL OUTER JOIN right_table

ON condition

SELECT Location.Street, MemberDetails.Street

FROM Location FULL OUTER JOIN MemberDetails

ON Location.Street = MemberDetails.Street;

I took assistance and consulted from the underneath named sources of information:

 Encyclopedia website.

26