You are on page 1of 31

Database Concepts

What is Database? A database can be defined as a collection of coherent ,meaningful data. A set of inter-related data is known as database e.g Address book is Dbase and postal addresses in that book is the data that fills the database.

Database Management system: A computer based record keeping system which consists of software for processing a collection of inter-related data.

Def Of DBA: The database is centrally managed by a person known as the database administrator or the DBA. Responsibilities of DBA:
Initially Studies the system Accordingly decides the types of data to be used. Decides structures to be used to hold the data and the relationships between the data structures. Ensures the security of the database. DBA usually controls access to the data through the user codes and passwords and by restricting the views or operations that the user can perform on the database.

Characteristics of DBMS
Control of data Redundancy Sharing of data Maintenance of integrity Support for transaction Control and Recovery Data independence Availability of Productivity Tools Control over security Hardware independence

DBMS Models 1. Hierarchical 2.Network 3.Relational

Relational DBMS The relational model presents an orderly ,predictable and intuitive approach to organizing,manipulating and viewing data. Relational data consists of relations. A relation is a two dimensional table with special properties. A relational table consists of a set of named columns and an arbitrary number of rows. A domain is a set of values that may appear in one or more columns. Each column or attribute is associated with a domain. E.g Dept table Dept No Dname 1 Accounts 2 Purhase Loc Pune Bombay

row or tuple

Column or attribute

Properties Of Relational Structures

Entries of attributes are single valued Entries of attributes are of the same kind No two rows are identical The order of attributes is unimportant The order of rows is unimportant Every column can be uniquely identified.

DBMS V/S RDBMS

DBMS

RDBMS

Relationship between two tables or Relationship between two tables or files are maintained programmatically files can be specified at the time of table creation. Does not support Client/Server architecture Does not support Distributed databases In DBMS no security of data Most of the RDBMS supports Client/Server architecture Most of the RDBMS supports Distributed databases In RDBMS there are multiple levels of security 1.Logging in at O/S level 2.Command level 3.Object level Many tables are grouped in one database

Each table is given an extension

Naming Conventions

DBMS Field Record File

RDBMS Column,Attribute Row,tuple,Entity Table,Relation, Entity Class

Normalization :
Normalization is the process of putting things right ,making them normal. Normalization is the process that helps analyst or database designers to design table structures for an application. Normalization is carried out for following reasons: 1. 2. 3. 4. 5. To structure the data between tables so that data maintenance is simplified. To allow data retrieval at optimal speed. To simplify data maintenance through updates,inserts and deletes. To reduce the need to restructure tables as new application requirements arise To improve the quality of design for an application by rationalization of table data.

Normalization is technique that: 1. 2. 3. Decomposes data into two-dimensional tables. Eliminates any relationships in which table data does fully depend upon the primary key of a record. Eliminates any relationship that contains transitive dependencies.

First Normal Form:


When a table is decomposed into two-dimensional tables with all repeating groups of data eliminated , the table is said to be in its first normal form. A table is in 1st NF if: There are no repeating groups. All the key attributes are defined All are dependent on a primary key.

To understand the application of normalization to table data the following table structure will be taken as an example: Table: EmpProj

Field
Project Number Project Name Employee Number Employee Name Rate Category Hourly Rate

Key -------

Type

1-n 1-n 1-n 1-n

After Converting this data to its 1NF :

Field
Project Number Project Name Employee Number Employee Name Rate Category Hourly Rate

Key
Primary key

Primary key

Second Normal Form: A table is said to be in 2nd NF when each record in the table is in the 1NF and each column in the record is fully dependent on its primary key. The steps to convert a table to its 2Nf: 1.find and remove fields that related to the only part of the key. 2.Group the removed items in the another table 3.Assign the new table with the key I.e. part of a whole composite key. To convert the table into the 2nd NF remove and place these fields in a separate table, with the key being that part of the original key are dependent on.

After Converting to 2NF :


Field Project Number Employee Number Key Primary key Primary key

Field Project Number Project Name

Key Primary key --

Field Employee Number Employee Name Rate Category Hourly rate

Key Primary key ----

Third Normal Form: Table data is said to be in 3NF when all transitive dependencies are removed from this data.
A general case of transitive dependencies : A,B,C are three columns in table: If C is related to B If B is related to A. Then C is indirectly related A This is when transitive dependency exists. To Convert such data into 3NF remove this transitive dependency bu splitting each relation into two separate relations.This means that columns A,B,C must be placed in separate tables,which are linked using a Foreign key.

After Converting to 3NF:


Empproj Field Project Number Employee Number Emp Field Employee Number Employee Name Rate Category Key Primary key -Foreign Key Field Rate Category Hourly rate Rate Key Primary key -Key Primary key Primary key Proj Field Project Name Key --

Project Number Primary key

What is SQL ?
SQL or Structured Query Language is the set of commands, which is used for interacting with relational Databases. ANSI has standardized the commands and relational databases conform to this standard.Most RDBMS provide extensions to it to make the life of the application programmer easier. Oracle has modified SQL to support ORDBMS features. Components Of SQL : SQL can be broadly classified into four components. 1. Data Definition Language (DDL) :This component is used for defining and destroying objects in your application.

2. Data Manipulation Language (DML):This is used to modify the data according to our needs . 3. Data Control Language (DCL):This is used for defining your security for your application objects like tables etc. 4. Data Query Language (DQL):This is used to retrieve the data as per requirement.

Examples of DDL,DML,DCL,DQL DDL:       Create - To create objects in database Alter - Alters the structure of the database

Drop - deletes object from database Truncate remove all records from table , including all spaces allocated for the records are removed Commit gives users access privileges to database. Revoke withdraw access privileges given with the GRANT command.

DML:     Insert Insert data into table Update Updates existing data within a table. Delete Deletes all records from a table, the space for record is remain Lock table control concurrency

DCL: Commit save work done Savepoint Identify a point in a transaction to which you can later roll back Rollback restore database to original since the last commit Grant / Revoke Grant or take back permission to and from the oracle user DQL: Select Retrieve .data from the database. Benefits of SQL : Non-procedural Language processes sets of records rather than just one record at a time. Provides automatic navigation to the data. 2.Language For All users System Administrator Database Administrator

Security Administrator Application Programmers End-Users

3. Unified Language Data Types Supported in SQL: 1. Char(n) : To store Fixed Length string , max length =255 bytes e.g. Name char(15) 2. Varchar2(n) : To store variable length string max length = 2000 bytes . E.g. Description varchar2(1000 3. Long(n) :To store variable length string .max length= 2GB .e.g. Synopsis Long(5000) 4. Number(p,s) : To store numerical data . Range 1E-129 to 9.99E125 , max digits = 38 e.g. Salary number(9,2)

5. Date : To store date . Range 01/01/4712 BC to 31/12/4712 AD. Both date and time are stored. Requires 8 bytes . E.g. Hiredate date 6. Raw(n) : To store data in binary format such as signature ,photograph . Max size=255 bytes 7.Long Raw(n) : Same as Raw. Max size = 2GB. Oracle has added New Data types to support object extensions. They are Varrays, Nested Tables, REFS and LOBS.

Data Constraints All Business World run on business data being gathered , stored and analyzed . Business Managers determine a set of business rules that must be applied to their data prior to it being stored in the database/table to ensure its integrity. Such Rules have to be enforced on data stored. Only data , which satisfies the condition set , should be stored for future analysis . If the data gathered fails to satisfy the condition set, it must be rejected . This ensures data stored in a table will be valid , and have integrity. Business Rules that are applied to data are completely System dependant. Business Rules , which are enforced on data being stored in a table are called Constraints.

There are Two Types Of Data Constraints that can be applied to data being inserted in Oracle table. I/O Constraints Input / output Constraints - determines the speed at which data can be inserted or extracted from oracle table. Business Rule Constraint I/O Constraints:  Primary Key Foreign Key Unique Key Business Rule Constraint Null Value Constraint Check Constraint Default Value Constraint

Primary Key : Primary key is a column or set of columns that uniquely identifies a row . Its main purpose is the Record uniqueness. Primary Key Will not allow duplicate values and null values. Primary key is not compulsory but it is recommended. Only one primary key is allowed per table , and can be formed by combining maximum 16 columns Primary Key helps to identify one record from another record and also helps in relating tables with another. Syntax: <column name> <datatype> (<size>) PRIMARY KEY Or PRIMARY KEY(<column name>,<column name>)

Foreign Key : A Foreign key is a column or columns whose values are derived from the Primary key or Unique Key of some other table. Foreign Key represents relationships between tables. Foreign key constraint can be specified on child but not on parent. Parent record can be delete provided no child record exists Master table can not be updated if child record exists If the ON DELETE CASCADE option is set , a DELETE operation in the Master table will trigger a DELETE operation for corresponding records in all detail table. If the ON DELETE SET NULL option is set , a DELETE operation in the Master table will set the values held by the foreign key of the detail tables to null.

Syntax:
<Column name > <data type> (<size>) REFERENCES <Table Name> [(<column name>)] [ON DEELETE CASCADE ] Or FOREIGN KEY (<column name > [, <column name>]) REFERENCES <Table Name> [(<column name>,<column name>) Or CONSTRAINT <constraint name> FOREIGN KEY (<column name > [, <column name>]) REFERENCES <Table Name> [(<column name>,<column name>) ON DELETE SET NULL

Unique Key The Unique column constraint permits entries of NULL into the column .
Unique Key will not allow duplicate values A table can have more than one Unique Key which is not possible in Primary key Unique Index is created automatically. Unique Key can combine up to 16 columns in composite Unique key. Syntax: <column name> < Data type> (<size>) UNIQUE Or UNIQUE (<column name1>,<coumn name2>)

Check Constraint:
Business rules can be implemented in Oracle by using CHECK constraint. Check constraint can be bound to a column or a table using the CREATE or ALTER command. The Check constraint must be satisfied for the write operation to succeed.

A check constraint has the following limitations: The condition must be a Boolean expression that can be evaluated using the values in the row being inserted or updated. The condition can not contain subqueries or sequences. The condition can not include the SYSDATE,UID,USER,USRNV SQL functions. Syntax: <column name> <data type>(<size>) CHECK (<logical expression>)

Null Value Concept:


Often there may be records in a table that do not have values for every field. This could be because the information is not available at the time of data entry or because the field is not applicable in every case. If the column was created as NULLABLE , Oracle will place a NULL value in the column in the absence of a user-defined value. A NULL value is different from a blank or a zero. A NULL value can be inserted into columns of any data type. Now , an empty string is treated as a null value in oracle.

NOT NULL Constraint : When a column is defined as NOT NULL ,then that column becomes mandatory
column. NOT NULL column constraint ensures that a table column can not be left empty. Syntax: <column name> <data type> (<size>) NOT NULL

Default Constraint: At the time of table creation a default value can be assigned to a column. When a record is loaded into the table, and the column is left empty , the Oracle engine will automatically load this column with the default value specified. The data type of the default value should match the data type of the column. The DEFAULT clause can be used to specify a default value fro a column. Syntax: <column name> <data type>(<size>) DEFAULT <value>

Example for all Data Constraint 1. Constraint at column level Create table emp ( eno varchar2(10) Primary key, Ename varchar2(20) Not Null, Addr1 varchar2(30) Not Null, Addr2 varchar2(30), Sal number(10,2) Not Null, Comm number(10,2) Default 0, Deptno number(2) references dept(deptno) , Check(eno like C%) );

2. Constraint at table level


Create table emp ( eno varchar2(10) , Ename varchar2(20) Not Null, Addr1 varchar2(30) Not Null, Addr2 varchar2(30), Sal number(10,2) Not Null, Comm number(10,2) Default 0, Deptno number(2) , Check(eno like C%), Constraint PK_emp Primary Key(empno), Constraint FK_deptno references dept(deptno) );

You might also like