20
DBMS Defintion, Objective And Applications Database: Collection of interrelated data. example: Mark sheet and student records DBMS: A Database Management system is a collection of interrelated data and set of programs to access the data. Objective of DBMS: To provide a convenient environment to retrieve and store database information. Database systems can support single user and multiuser environment. Database Application: Database application is the program that is used to view, retrieve and update information stored in the database. The following are the few applications of Database. * Bank database * University database * Railway database etc., DBMS Services DBMS offers following services Data Definition: It is a method of defining data and storage. Data Maintaenance: It checks whether each record has field containing information for a particular record or not. example: In an employee table information about the employee like employee id, name, designation, salary recorded. Data Manipulation: It allows data in the database to be inserted, updated, deleted and sorted. Data Display: It helps in viewing data. Data Integrity: It ensures the accuracy of the data. Relational Database Management System[RDBMS]:An RDBMS is a computer program, which provides the user the facility to store and retrieve data in a manner consistant with a defined model called relational model. Relation -> table; tuple -> Row, Record; Cardinality -> No.of rows; Attribute -> column, field Degree -> No.of columns; Primary Key -> unique identifier Domain -> Set of legal values; Introduction to oracle9i: * Oracle9i is an Object Relational Database Management System (ORDBMS). * It offers capabilities of relational and object oriented database system. * Oracle products are based on client/server technology. * In this technology server performsall activities related to the database and the client performs activities that help the user to interact with the application. Features of oracle9i: * Database performance

DBMS Defintion

Embed Size (px)

Citation preview

DBMS Defintion, Objective And ApplicationsDatabase: Collection of interrelated data.example: Mark sheet and student records

DBMS: A Database Management system is a collection of interrelated data and set of programs to access the data.

Objective of DBMS: To provide a convenient environment to retrieve and store database information. Database systems can support single user and multiuser environment.

Database Application: Database application is the program that is used to view, retrieve and update information stored in the database. The following are the few applications of Database. * Bank database * University database * Railway database etc.,DBMS ServicesDBMS offers following servicesData Definition: It is a method of defining data and storage.

Data Maintaenance: It checks whether each record has field containing information for a particular record or not.example: In an employee table information about the employee like employee id, name, designation, salary recorded.

Data Manipulation: It allows data in the database to be inserted, updated, deleted and sorted.

Data Display: It helps in viewing data.

Data Integrity: It ensures the accuracy of the data.Relational Database Management System[RDBMS]:An RDBMS is a computer program, which provides the user the facility to store and retrieve data in a manner consistant with a defined model called relational model.

Relation -> table; tuple -> Row, Record;Cardinality -> No.of rows; Attribute -> column, fieldDegree -> No.of columns; Primary Key -> unique identifierDomain -> Set of legal values;

Introduction to oracle9i:* Oracle9i is an Object Relational Database Management System (ORDBMS).* It offers capabilities of relational and object oriented database system. * Oracle products are based on client/server technology.* In this technology server performsall activities related to the database and the client performs activities that help the user to interact with the application.Features of oracle9i:* Database performance* Ease of Management* Scalability* Security* Availability* Internet content Management* e-commerce Integration* Packaged Applications* Business Intelligence

Tools Of Oracle* SQL *Plus* PL/SQL* Forms* Reports

SQL *Plus: SQL *Plus is structured query language supported by Oracle. Through SQL *Plus we can store, retrieve, edit and run SQL commands and PL/SQL blocks. Using SQL *Plus we can perform calculations, list column definitions for any table and can also format query results in the form of a report.

PL/SQL: PL/SQL is an extension of SQL. PL/SQL block can contain any number of SQL statements integrated with flow of control statements. PL/SQL is a procedural language for the oracle server and its client. In a PL/SQL program,SQL statements are used for data manipulation and transaction processing.

Forms: Form is a graphical tool used for generating and executing forms based applications. A form basically consists of block and fields. Multiple tables can be accesed over a single form. Oracle form builder is the design component of oracle forms.

Reports: It is an application development tool of oracle for developing, executing, displaying and printing reports. We can create wide variety of reports, which have various modes. Oracle reports are powerful and easy to use.

Oracle Data typesNUMBER:NUMBER datatype is used to store fixed-point or floating-point numbers

We can specify precision, which is the total number of digits and scale which is the number of digits to the right of the decimal point.syntax: NUMBER(precision,scale)

NUMBER subtypes DEC DECIMAL INTEGER NUMERIC

CHAR:Character types allow us to store alphanumeric data, represent words and text and manipulate character strings.

CHAR: CHAR datatype is used to store fixed-length character data. represented internally depends on the database character set. The CHAR datatype an optional parameter that lets us specify a maximum length upto 32767 bytes.

syntax:char(max_length);

VARCHAR2: VARCHAR2 datatype is used to store variable-length character data.Data represented internally depends on the database character set. The varchar2datatype takes a required parameter that specifies a maximum length up to 32767bytes. syntax:varchar2(max_length);

DATE:DATE:DATE datatype is used to store fixed-length date/time values. DATE valuesinclude the time of day in seconds since midnight. The date portion defaults tothe first day of the current month, the time portion defaults to midnight. THedate function SYSDATE returns the current date and time.

Data Definition Language(DDL)The Data Definition Language is used to create, remove and alter the structure of database objects. The DDL is used for table definition and can be classified into 4 categories.(i) create table command(ii) alter table command(iii) truncate table command(iv) drop table command

Data Manipulation Language(DML)Data Manipulation commands are used to query and manipulate existing database objects. Following are some DML commands-(i) Insert(ii) select(iii) update(iv) delete

Data Control Language(DCL)Data Control Language is used to control the kind of a data access to the database and transaction control. The owner can allow other database users access to the database objects as per his/her direction.

Eg:grant, revoke are used for data access control.commit, rollback and save point are transaction control commands.

Creating table in OracleRules for table and column names* Names can be upto 30 characters.* Names must begin with an alphabet.* Names cannot contain quotes.* Names are not case sensitive.* Names can contain characters a-z, 0-9, _, $ and #.* Names cannot be reserve words.

Create table statement:The create table statement can be used to create a new table.

Syntax:CREATE TABLE tablename (column1 datatype(width), column2 datatype(width), column3 datatype(width), . . column-n datatype(width));

Example:create a table 'student' with the following fields'Roll_no', 'Name', 'class', 'DOB' and 'Address'

CREATE TABLE student (Roll_no varchar2(20), Name varchar2(20), class varchar2(20), DOB date, Address varchar2(100));Inserting Rows in a tableInsert into statementThe insert into statement is used to insert new rows into a table. we can use insert into statement in three ways.* We can insert values into the table directly by using the following syntax.

Syntax:INSERT INTO tablename VALUES(value1,value2,....value-n);

Example:insert into student values(101,'abc','BCom','03-03-90','Hyderabad');

* We can also specify the columns for which we want to insert the data.

Syntax:INSERT INTO tablename (column1,column2) values(value1,value2);

Example:insert into student(Roll_no, name) values(102,'def');

* We can add fields interactively by using the following syntax.Syntax:insert into < values('&Roll_no','&Name','&class','&DOB','&Address');

Selecting Rows from the tableSelect statement:Select statement can be used to view the data from the table.

We can use the select statement in the following situations.* To view the data of only some columns in the table.* To view the data of all columns in the table.* To view the distinct values in the table.* select statement with where clause

* syntax To view some columns:select column-1,column-2 from

Example:select name, dob, address from student;

* Syntax to view all columns:select * from ;

Example:select * from student; [here * means all]

* Syntax to view distinct values:The distinct keyword is used to select different values from the table.Means the distinct keyword doesnot display the repeated data in the table.

select distinct from ;Example:select distinct name from student;Here, repeated names wont be displayed.

* select statement with where clauseThe optional where clause has the following syntax

where Here, the has two forms

1. exp operator exp 2. Logical Operator exp Here, operators are =,=, logical operators are And,Or,Not

Example:select * from student where name='abc';

ConstraintsConstraints are rules that are used to control the invalid data entry in a column. Constraints are Primary key Foreign key Null and NotNull Unique Check DefaultPrimary KeyThe primary key has two features1. It defines the column as manadatory i.e column cannot be left blank.2. The data held across the column must be unique i.e values should not be repeated.

* A single column primary key is known as simple primary key.* Multi column primary key is known as composite primary key.

Note: We cannot define more than one primary key in a single table. We can define primary key for multi columns by using composite primary key.

Example on simple primary key:create a table 'customer' with the following fieldscust_no as primary key, name, address, city, state, pin.

create table customer(cust_no number(10) primary key, name varchar2(50), address varchar2(50), city varchar2(50), state varchar2(50), pin number(6));

Example on composite primary key:create table product(order_no number(10), prod_no varchar2(20), qty number(5), price number(8,3) primary key(order_no,prod_no));

Null:If in a record any field that is created as nullable(not having value) then oracle will replace a null value in that column. Null value is not equivalent to zero or blank. Null values can be inserted into the columns of any datatype.

Not Null:The not null constraint ensures that the users always type the value for that column becomes a manadatory column. Not null constraint can be applied at column level only.Unique Key* unique key constraint ensures that information in the columns is unique i.e value entered in unique column must not be repeated across the column(s).

* A table may have more than one unique key. Unique key may be null at the time of data entry. So, value is not manadatory in unique key column.

* We can define a single column as unique and not null both then the constraint will work as primary key.

CheckCheck constraint ensure that when data is entered, the data in the column is limited to specific values.eg: category varchar2(5) check(category in ('sc','st'));Default Constraint:At the time of table creation a default value can be assigned to a column. When the user is entering the values and leaves this column empty the oracle will auotmatically load this column with the default value. The datatype of the default value should match the datatype of the column.

eg: age number(3) default 21;

Update StatementThe update statement is used to update or change records that match a specified criteria. This is accomplished by carefully constructing a where clause.

syntax: update set columnname=newvalue, columnname1=newvalue1,... where

eg: update employee set phoneno='9884532245' where ename='abc';

AlterThe ALTER command is used to modify the structure of a table. Using ALTER command, we can add new columns, modify existing columns, and add or drop integrity constraints.

ALTER can be used to make the following changes to any tables* Add new columns* Add new integrity constraints* Modifying existing columns -Expand length -Change default -Decrease length -All values in column must be null -Change data type -All values in column must be null* Drop integrity Constraints

Alter SyntaxesTo add a new columnsyntax: alter table add(new_colname datatype(width));example: alter table employee add(address varchar2(10));

To change the width of the exisiting fieldsyntax: alter table modify(col_name datatype(width));example: alter table employee modify(address varchar2(50));

To rename the columnsyntax: alter table rename column old_col to new_col;example: alter table employee rename empname to employename;

Adding Many columns in a single alter statementsyntax: alter table add(col1 datatype(width), col2 datatype(width).. coln datatype(width));example: alter table employee add(address varchar2(20), city varchar2(20), dob date);

To add a primary keysyntax: alter table add primary key(field);example: alter table employee add primary key(empno);

To remove a primary keysyntax:alter table drop primary key;example:alter table employee drop primary key;

employee table1. Create a table 'employee' with the following fieldsEmpnovarchar26(primary key) Enamevarchar220(not null)Jobchar10Hiredatedate basic_salnumber (9,2)commnumber (7,2)dept_novarchar2 2

Q: create table employee(empno varchar2(6) primary key, ename varchar2(20) not null, job char(10), hiredate date, basic_sal number(9,2), comm number(7,2), dept_no varchar2(2));

2. Describe the table 'employee'Q: desc employee

3. Insert the following data into the above 'employee' tableempnoenamejobhiredatebasic_salcommdept_no

E0001kimManager15-dec-025000500D001

E0002BruceAnalyst24-Apr-994000400D002

E0003ArnoldClerk10-jan-012500250D004

E0004HolyfieldTester10-oct-013000300D002

E0005kellyAdmin11-apr-992000200D003

Q: insert into employee values('E0001','Kim','Manager', '15-Dec-02','5000.00','500','D001'); insert into employee values('E0002','Bruce','Analyst', '24-Apr-99','4000','400','D002'); insert into employee values('E0003','Arnold','Clerk', '10-jan-01','2500','250','D004'); insert into employee values('E0004','Holyfield','Tester', '10-oct-01','3000','300','D002'); insert into employee values('E0005','Kelly','Admin', '11-Apr-99','2000','200','D003');

Select Command And Like OperatorI. employee table1. List the names of the employee who have a salary less than Rs.3000 from employee tableQ. select ename from employee where basic_sal < 3000

3. List all the employees whose name starts with the letter 'K' from employee table.Q. select ename from employee where ename like 'K%';

4. List the employee name working in department D002, D003 from employee table.Q. select ename from employee where dept_no='D002' or dept_no='D003';

5. List all employee whose name start with 'A' and end with 'd' from employee table.Q. select ename from employee where ename like 'A%d';

6. List all managers and salesman with salary over 2500 from employee table.Q. select ename from employee where (job='Manager' or job='Sales man') and basic_sal>2500;

7. Display all the employee names in the ascending order of their date of joining from employee table.Q. select ename from employee order by hiredate asc;

8. Display all the employees in alphabetical order from employee table.Q. select ename from employee order by ename;

9. List all employee who where hired during 1999 from employee table.Q. select ename from employee where to_char(hiredate,'yy')=99;

10. List all employees whose commission is more than Rs.300 from employee table.Q. select ename from employee where comm>300;

II. client_mast table 1. Find out the names of all the clients from client_mast table. Q. select name from client_mast;

2. Retrieve all the records from client_mast table Q. select * from client_mast;

3. Retrieve all the list of names, address and city of all the clients from client_mast Q. select name,address,city from client_mast;

4. List all the clients who are staying in Florida from client_mast table Q. select name from client_mast where state="Florida";

III. Dept table 1. List the department name which is located in Noida and Rockey Creek from Dept table. Q. select dname from depttable where loc='Noida' or loc='Rocky Creek';

Update CommandI. employee table1. Change the basic salary Rs.3000 where basic salary less than 2500 from employee table Q. update employee set basic_sal=3000 where basic_sal editDeclare a number; b number; c number;Begin a:=&a; b:=&b; c:=&c; if a>b and a>c then dbms_output.put_line(a||'is greater'); elsif b>a and b>c then dbms_output.put_line(b||'is greater'); elsif c>a and c>b then dbms_output.put_line(c||'is greater'); end if; end;SQL> /Program to calculate Factorial of number.declarev_num number :=#fact number :=1;beginfor i in 1..v_numloopfact:=fact*i;end loop;dbms_output.put_line('fact of '||v_num||' is '||fact);end;/

Program to calculate Even or odd Numberdeclaren number;r number :=1;beginn:=&number;for r in 1..nloopif mod(r,2)=0 thendbms_output.put_line('Even No :' || r);end if;end loop;end;

Multiplication TableDECLAREn number := &n;prod number;BEGINfor i in 1..10 loop prod := n * i; dbms_output.put_line(n||' * '||lpad(i,2,' ')||' = '||lpad(prod,3,' '));end loop;END;

Types of Table RelationshipsA relationship works by matching data in columns usually columns with the same name in both tables. In most cases, the relationship matches the primary key from one table, which provides a unique identifier for each row, with an entry in the foreign key in the other table.

There are three types of relationships between tables. One-to-Many Relationship Many-to-Many Relationships One-to-One Relationships

One-to-Many RelationshipsA one-to-many relationship is the most common type of relationship, a row in table A can have many matching rows in table B, but a row in table B can have only one matching row in table A. For example, the publishersandtitlestables have a one-to-many relationship: each publisher produces many titles, but each title comes from only one publisher.

Many-to-Many RelationshipsIn a many-to-many relationship, a row in table A can have many matching rows in table B, and vice versa. You create such a relationship by defining a third table, called a junction table, whose primary key consists of the foreign keys from both table A and table B.

One-to-One RelationshipsIn a one-to-one relationship, a row in table A can have no more than one matching row in table B, and vice versa. A one-to-one relationship is created if both of the related columns are primary keys or have unique constraints.

One-to-one relationships: This relationship exists where a record in Table A can only have one related record in Table B, and a record in Table B can only have a single matching record in Table A For example, an MP can have only one constituency, and a constituency can have only one MP

One-to-many relationships: This relationship exists where a record in Table A can have no, one or more matching record in Table B, but a record in Table B can only have one matching records in Table A For example, a mother can have more than one child, but a child can have only one biological mother

Many-to-many relationships: This relationship exists where a record in Table A can have no, one or many matching records in Table B, and a record in Table B can have no, one or more than one matching record in Table A For example, an author can write more than one book and a book can be written by more than one author