You are on page 1of 10

CBA BASIC DATABASE OPERATIONS

Contents
1

Part A: Generic Requirements............................................................................2


1.1
Functional Requirements:.............................................................................2
1.2
NFRs:...........................................................................................................2
1.3
Primitives......................................................................................................2
1.4
Knowledge Reference..................................................................................2
2 Part B - Solution process steps............................................................................3
2.1
Process Steps for Functional Requirements.................................................3
2.1.1
Create a Table in the Database.............................................................3
2.1.2

Perform DB CRUD operation on Table(s)..............................................4

2.1.3

Perform Transaction Control on DML....................................................6

2.1.4

Create Sequence and Indexes..............................................................6

2.1.5

Alter the Table definition by adding/ removing columns, constraints......6

2.1.6

Create Master-Child relationship among Tables in Database................7

2.2
Process Steps for NFRs...............................................................................7
2.2.1
Steps for Security:.................................................................................7
2.2.2

Steps for Performance:..........................................................................8

2.2.3

Steps for Reliability:...............................................................................8

2.2.4

Steps for Maintainability:.......................................................................8

2.2.5

Steps for Portability:..............................................................................8

2.3

Test Approach - Process Steps.....................................................................8

1 Part A: Generic Requirements


A database is a collection of information that is organized so that it can easily be
accessed, managed and updated. This CBA will cover basic operations of relational
database system i.e. Create table, perform CRUD operation on the table, Create
Index, sequence and perform Transaction control operation etc.

1.1

Functional Requirements:
1. Create a Table in the Database with columns and constraint
2. Perform Basic DB CRUD operations on Table(s)
o Insert data into the table
o Retrieve data from single table,
o Modify stored data
o Delete stored data
o Sort data retrieved
o Retrieve data from multiple tables
3. Perform Transaction control on DML operations
4. Create a Sequence/ additional Index on Table
5. Alter the Table definition by adding/ removing columns, constraints
6. Create Master-Child relationship among Tables in Database
o Create a table as a child table to an existing Table
o Delete data from Master Table

1.2

NFRs:
1. Security:
Only Authorized user will be able to access the Entity
2. Performance:
Searching of any data should not take more than 3 second
3. Reliability:
Data should be stored in the table in correct format and Insertion,
update and deletion of data should not affect other data
4. Maintainability:
Creation and deletion of new table should not affect existing database
structure and data integrity
5. Portability: e
ORACLE environment should be used to create the tables.

1.3

Primitives
SQL DDL
DML
DCL
TCL

1.4

CREATE TABLE, CREATE INDEX,CREATE SEQUENCE,


NEXTVAL, ALTER TABLE, DROP TABLE,
DROP INDEX, DROP SEQUENCE,
SELECT, INSERT, UPDATE, DELETE, ORDER BY,
WHERE, IN, NOT IN, EXISTS
GRANT,REVOKE
COMMIT, ROLLBACK
AUTO COMMIT, SET

Knowledge Reference
Learning Reference:
Initial Learning

Program > PLSQL > Oracle PLSQL stream Mater ials


> Course Materials > Oracle

2 Part B - Solution process steps


2.1

Process Steps for Functional Requirements

2.1.1 Create a Table in the Database


For each table identified in the design specification, create table structure in the
database by following below steps.
Step 1. Login to the ORACLE database AND Enter into the database using USE
database command.
Step 2. Use CREATE table command to create the tables. For each table as per the
design specification,

Define the columns with data type and size. Below are some commonly used
data types in ORACLE database.

Data type
Character

Name
char(size)
varchar2(size)
Long

Numeric

Date/time
Large object

Raw
long raw
number(p,s)
Float
Integer
Int
Real
Date
timestamp (fractional
seconds precision)
Bfile
Blob
Clob
Nclob

Description
Fixed-length strings. Space padded. Max size of 2000
bytes
Variable-length string. Max size of 4000 bytes
Variable-length strings. (backward compatible) Max
size of 2GB
Variable-length binary strings. Max size of 2000 bytes
Variable-length binary strings. Max size of 2GB
precision p(1 to 38), scale s(-87 to 127)

A date between Jan 1, 4712 BC and Dec 31, 9999 AD


Includes year, month, day, hour, minute, and seconds.
File locators that point to a binary file on the server file
system (outside the database).Max size of 4GB
Stores unstructured binary large objects
Stores single-byte and multi-byte character data.
Stores unicode data.(character text)

Define not null column- prohibits a database value from being null.
The column which must contain a value and can not be null should define as
not null. For example Employee Number or Gender

Step 3. Define Audit trail column to capture the record modification detail. Column
name should be
o Created By : user name who inserted the record
o Created Date: Date & Time of Insertion (data type should be
timestamp)
o Modified By : user name who modified the record
o Modified Date: Date & Time of Modification (data type should be
timestamp)
Step 4. Define Constraints on the Table

Define the primary key- combines a NOT NULL constraint and a unique
constraint in a single declaration. That is, it prohibits multiple rows from
having same value for the primary key.
o The column/s defined as primary key can not be null or contains
duplicate value.
o Primary key value uniquely identifies a particular record in the table.
o The column/s defined as primary key can not be null.

Define the unique key- prohibits multiple rows from having the same value in
the same column or combination of columns but allows some values to be
null

Define Check Constraint - requires a value in the database to comply with a


specified condition.
o A check constraint allows you to specify a condition on each
row in a table.
o A check constraint can be defined in either a CREATE TABLE
statement or an ALTER TABLE statement.
o It can not be define on view or include subquery

Step 5. Use DESC command to check the definition of each table and know whether
the table created correctly or not.

2.1.2 Perform DB CRUD operation on Table(s)


To perform Insert, Search, Update and delete operation on the database, write SQL
statements at SQL prompt as below:
2.1.2.1 Insert data into the table
Once table is created in the database, data can be stored into the table in record
format. To insert data into the table, follow the below steps:
Step 1 Use DESC command to check the table definition.
Step 2 To insert record into the existing table, use INSERT statement.
Step 3 INSERT statements allow entering single record or multiple records into the
table.
Step 4 To insert single record define column name and corresponding value in the
INSERT statement.
Step 5 To insert multiple records, use sub query in place of direct values in the
Step 6 INSERT statement. This is applicable when data existing in one table, need
to be insert in another table.
2.1.2.2 Retrieve data from single table
To view the stored data in the table, we have to fetch it from the table. Follow the
below steps to retrieve the stored data:
Step 1 Use SELECT statement to retrieve data from the data base.
Step 2 To list all the data of a given table, uses SELECT statement without
WHERE clause.
Step 3 To list individual fields of all the record, use column name in the SELECT
statement
Step 4 To retrieve given data about an record use Primary key value in the
WHERE clause of the SELECT statement

Step 5 To filter data based on certain criteria use WHERE clause to specify the
criteria (=, IN, NOT IN,ANY/ALL etc) in SELECT statement
2.1.2.3 Modify stored data
Sometimes data stored in the database need to be modified or changed, for that we
have to update the records stored in the database. To update a record in the
database, follow the below instruction:
Step 1 UPDATE statement is use to update or modify single as well as multiple
records in a table. WHERE clause is use in the statement to specify the
condition to select the record for modification.
Step 2 WHERE clause should contain conditions like =, IN, NOT IN, ANY etc.
Step 3 Sub query can be use with EXISTS condition in the WHERE clause to
specify the condition
Step 4 Again use SELECT statement with same WHERE clause to check whether
data updated in the database or Not.
2.1.2.4 Delete stored data
To remove unwanted or wrong records from the database, delete operation is
performed on the stored data. The DELETE statement allows deleting single as well
as multiple records from the table. Follow the below instruction while deleting
record(s) from the database.
Step 1 Use condition in the WHERE clause to select a particular record from the
table for deletion.
Step 2 Do not specify the where clause if whole records need to be deleted from the
table. TRUNCATE command can be use to delete all records from the table but
the operation can not be rollback.
Step 3 EXISTS condition be use in the WHERE clause
Step 4 Again use SELECT statement with same WHERE clause to check whether
data is deleted from the database or not. Also check the data of the child table
2.1.2.5 Sort data retrieved
To view the stored data, we can use SELECT statement to fetch the data. But to
view the data in some particular order, we have to filter or sort the retrieved data.
Follow the below instruction to sort the retrieved data.
Step 1 Use OREDER BY clause in the SELECT statement to sort the result set by
a specified column
Step 2 The OREDER BY clause may specify one or more columns on which to
sort the data
Step 3 The OREDER BY clause must follow the FROM clause or WHERE clause
(if a WHERE clause is supplied)
Step 4 The OREDER BY clause sorts the records in ascending order by default.
To sort the record in descending order, use DESC keyword
2.1.2.6 Retrieve data from multiple tables
To fetch data from multiple tables use JOINS. A join is performed when two or more
tables are listed after FROM clause of the SQL statement. Use any of the below type
of joins as per the requirement.

Step 1 Inner join: It is most common type of join. It returns all rows from multiple
tables where the join condition is met.
Step 2 Outer join: This type of join returns all rows from one table and only those
rows from a secondary table where the joined fields are equal (join condition is
met).
Step 3 Left join: The LEFT JOIN keyword returns all rows from the left table
(table_name1), even if there are no matches in the right table (table_name2).
Step 4 Right join: The RIGHT JOIN keyword returns all the rows from the right table
(table_name2), even if there are no matches in the left table (table_name1).
Step 5 Full join: The FULL JOIN keyword return rows when there is a match in one
of the tables.

2.1.3 Perform Transaction Control on DML


DDL statements are by default auto commit. On successful execution of the
statement, transaction will automatically committed to the database.
Step 1 Make AUTOCOMMIT OFF by using SET command
Step 2 Use COMMIT statement after the successful execution of the sql statement
to make the transaction permanent
Step 3 Use ROLLBACK to void the transaction if required.
Step 4 To mark a point in the transaction upto which we can do rollback, use
SAVEPOINT command. It marks the current point in the processing of a
transaction and use with ROLLBACK to undo parts of transaction.

2.1.4 Create Sequence and Indexes


2.1.4.1 Create Sequence
Use Sequence to generate autonumber field. It is useful when we want to create
unique number to act as primary key.
Step 1 Use CREATE sequence syntax to create sequence and specify the
attributes like sequence name, MINVALUE, MAXVALUE, START WITH,
INCREMENT BY, CACHE
Step 2 To fetch value from the sequence, use nextval command.
Step 3 Use ALTER Sequence command to reset the counter to correct value.
Step 4 To drop a Sequence, use DROP SEQUENCE command
2.1.4.2 Create Index
Oracle automatically creates an index for each Unique key and Primary key
declaration. We can not drop these indexes. The indexes are dropped automatically
when we drop the table or the constraints.
Step 1
Step 2
Step 3
Step 4

To Create additional index on the table, use CREATE INDEX syntax.


The statement must contain the index name.
To view the indexes in the database, Select index_name from user_indexes.
To Drop an index, use DROP INDEX command.

2.1.5 Alter the Table definition by adding/ removing columns,


constraints
After creation of table in the database, sometimes it requires to be modified to
Add/remove/modify columns, constraints etc, for that we can use ALTER command.
Step 1 To Rename an existing table, use RENAME TO clause with ALETER
command
Step 2 To Add a single or Multiple column to the table, use ADD clause in the
ALTER command
Step 3 To Modify a single or Multiple column to the table, use MODIFY clause in the
ALTER command. Column data type can be modify by this command.
Step 4 To drop a column from existing table, use DROP COLUMN clause in the
ALTER command
Step 5 Modify (change size) column from existing table
Step 6 To Rename a column in the table, RENAME COLUMN clause in the ALTER
command
Step 7 To Add constraint to the table, use ADD CONSTRAINT clause in the ALTER
command.
Step 8 To Enable/Disable constraints in the existing table, use ENABLE/DISABLE
CONSTRAINTS clause in the ALTER command

2.1.6 Create Master-Child relationship among Tables in Database


A relational database is based on the separation and independence of the logical and
physical representations of the data. So physically data is stored in different table
having some relationship. The relationship may be one-to-one or one-to-many.
Use foreign key to define the Master-child relationship among tables.
2.1.6.1 Create a table as a child table to an existing Table
Step 1 Create the table (table_name2) using CREATE table command as defined in
the 1.2.1.
Step 2 To establish relationship with table (table_name1), define foreign key in
table_name2 referring primary key of table_name1.
Step 3 The foreign key column can represent entire Primary key (one to one
relationship) or is part of the primary key (one to many relationship)
2.1.6.2 Delete data from Master Table
Before deleting data from Master table, corresponding data from child table must
be deleted first.
Step 1 Use ON DELETE CASECADE and ON UPDATE CASECADE condition
with foreign key. So that whenever any record from parent table gets deleted,
corresponding record from child table will automatically delete And also if there is
any update in primary key value in master table (like change of size etc. )
corresponding foreign key in child table also get updated.
Step 2 To delete data from Master table follow, the Delete Stored Data section.

2.2

Process Steps for NFRs

2.2.1 Steps for Security:


To secure the data from any unwanted modification or loss:

Step 1 To grant access permission for a table use GRANT commands at SQL
command prompt.
Step 2 Use AUDIT COLUMNS columns in each table and it should store uses detail
who accessed the record along with timestamp, so that we can get the user
information who modified the data.

2.2.2 Steps for Performance:


To improve performance while retrieving data , use primary key / indexes in WHERE
clauses.
Step 1 Create INDEX on table to improve performance while retrieving data and use
proper search criteria and specific column name in the query.
Step 2 INDEX can be altered and dropped using ALTER and DROP command.

2.2.3 Steps for Reliability:


To maintain data reliability, follow below steps while creating a table and doing any
Transaction on the data.
Step 1 While creating the Table specify the size of the attributes as per the Table
Description.
Step 2 If any column is missing or constraints to be added use ALTER command to
add a column to the existing table and also constraint. Also constraints can be
added by using ALTER command.
Step 3 Size of column and data type can be changed by using ALTER command
along with MODIFY.
Step 4 Use DESC command and check the table definition before inserting data
intro the table.
Step 5 To prevent accidental deletion or update, write the Where clause first in the
update and delete statement.
Step 6 Transaction should not be in auto commit mode.
Step 7 Each entry in child table must refer an entry in the parent or Master table.

2.2.4 Steps for Maintainability:


To prevent redundancy and maintain consistency, Store the Master data in parent
table and corresponding data in Child table So that each referenced Id in Child table
will have unique entry in Parent or Master table. Use Foreign key in child table to
maintain relationship with Master table.

2.2.5 Steps for Portability:


Step 1 ORACLE database should be used for creation of the tables.

2.3

Test Approach - Process Steps

1. Create a Table in the Database


Step 1 Use SHOW Table command to view whether the table is created in the
database or not.
Step 2 If Table is created, then Use DESC command to check the definition of each
table.

2. Perform DB CRUD Operation


Step 1 Use Select statement to check whether data is inserted into the table or not.
Step 2 After firing update statement, use SELECT statement to check whether data
is updated or not.
Step 3 After firing delete statement, use SELECT statement to check whether data
is deleted or not.
3. Perform Transaction Control
Step 1 Make Transaction AUTOCOMMIT OFF
Step 2 Insert a record to the table using INSERT statement
Step 3 Then commit the transaction using COMMIT command
Step 4 Fetch the records from the table using SELECT statement and check
whether the record inserted in the Step 2 is exist or not.
Step 5 Then update the record using UPDATE statement
Step 6 Then execute the ROLLBACK statement
Step 7 Fetch the records from the table using SELECT statement and check
whether the record updated in the Step 5 is modified or not
4. Create a Sequence/Indexes
Step 8 Use SELECT statement to fetch seq_name.nextval from dual. If it is showing
correct data, then sequence is created correctly.
Step 9 Drop the sequence if required
Step 10 To view the indexes in the database, Select index_name from user_indexes.
Step 11 Drop the index if required
5. Alter the Table definition by adding/ removing columns, constraints
Step 1 Use DESC command to check the definition of each table after using the
ALTER command.
6. Create Master-Child relationship among Tables in Database
Step 1 Use DESC command to check the definition of the child table
Step 2 Delete data from Master table using DELETE command
Step 3 View the data in the Child table using SELECT command

You might also like