Lab2_Prepared by TA.Jaber Taradeh

advertisement

TA. Jaber Taradeh Title: Database Management System Lab

Lab Two

EX.NO:2 IMPLEMENTATION OF DML AND DCL

COMMANDS

OBJECTIVES:

1.

The retrieval of information that stored in the database.

2.

The insertion of the new information into the database.

3.

The deletion of the information from the database.

4.

The modification of the information stored by appropriate data model, there are basically two types:

Procedural DML: Require a user to specify what data are needed and how to get those data.

Non Procedural DML: Require a user to specify what data are needed

Without specifying how to get those data.

DML COMMANDS:

1.

Insert Command this is used to add one or more rows to a table. The values are separated by commas and the data types char and date are enclosed in apostrophes. The values must be entered in the same order as they are defined.

SYNTAX

Insert into <Table name> (clo1, col2, col3…) Values (val1, val2, val3…)

EX.

insert into dept (deptno,deptname) values (1,'HR');

EX.

insert into emp (empno,ename,job,deptno) values (1,'Jaber','Manager',1);

EX.

insert into emp (empno,ename,job,deptno) values (2,’Ahmad,’Admin’,1);

2.

Select Commands It is used to retrieve information from the table. It is generally referred to as querying the table. We can either display all columns in a table or only specify column from the table.

 SYNTAX:

SELECT * FROM table_name;

SELECT * FROM table_name WHERE column_name <val>;

EX.SELECT empno,ename,job,deptno FROM emp

WHERE name=’Jaber’;

7 | P a g e Manual Of Database Lab

TA. Jaber Taradeh Title: Database Management System Lab

3.

Update Command It is used to alter the column values in a table. A single column may be updated or more than one column could be updated.

 SYNTAX:

Update table name Set column_name = expression, column_name = expression, …… .

Where column_name = expression ;

Example : update emp set ename='Mohammad' where empno=1;

4.

Delete command after inserting row in a table we can also delete them if required. The delete command consists of a Form clause followed by an optional where clause.

SYNTAX:

Delete All Rows: Delete from table name;

Deletion of specified number of rows:

Delete from table name

Where condition ;

Example : delete from emp where empno=1;

UNDERSTANDING NULL VALUES:

How does a database represent a value that is unknown? The answer is to use a

Special value, known as a null value. A null value is not a blank string—it is a distinct

Value. A null value means the value for the column is unknown.

When you select a column that contains a null value, you see nothing in that column.

You saw this (or rather, didn’t see it!).

You can also check for null values using the IS NULL clause in a SELECT statement

SYNTAX

SELECT * FROM table_name WHERE column_name IS NULL;

EXAMPLE: update emp set ename=NULL where empno=1;

SELECT empno,ename,job,deptno FROM emp WHERE ename IS NULL;

Since null values don’t display anything, how do you tell the difference between a

Null value and a blank string if you retrieve all the rows?

The answer is to use one of Oracle’s built-in functions: NVL (). NVL () allows you to

Convert a null value into another value, which you can actually read.

NVL () accepts two parameters: a column (or more generally, any expression that

Results in a value), and the value that should be substituted if the first parameter is

Null.

SYNTAX:

SELECT column_name, column_name, column_name, NVL

(column_name, 'Unknown phone number') AS column_name

8 | P a g e Manual Of Database Lab

TA. Jaber Taradeh Title: Database Management System Lab

FROM table_name;

EXAMPLE:

SELECT empno,ename,job,deptno, nvl(ename,'Name Not Found')

FROM emp;

DISPLAYING DISTINCT ROWS

The DISTINCT keyword is used to suppress the duplicate rows.

SYNTAX:

SELECT DISTINCT column_name FROM table_name;

 EXAMPLE:

SELECT DISTINCT ename FROM emp;

SORTING ROWS USING ORDER BY CLAUSE

 SYNTAX:

SELECT column_name, column_name, column_name

FROM table_name Order by column_name;

Example: insert into emp(empno,ename,job,deptno) values (10,’Basel’,'CTO',1); select * from emp order by ename;

By default, the ORDER BY clause sorts the columns in ascending order (lower values appearfirst). You can use the DESC keyword to sort the columns in descending order (higher valuesappear first). You can also use the ASC keyword to explicitly specify an ascending sort—as Imentioned, this is the default, but you can still specify it.

ARITHMETIC OPERATION

Operator

+

-

*

/

Description

Addition

Subtraction

Multiplication

Division

SYNTAX :

SELECT column_name, column_name arithmetic operation

FROM table_name;

EXAMPLES:

SELECT 2*6 FROM emp;

Alter table emp add(salary number(20)); update emp set salary=500 where empno=1; update emp set salary=400 where empno=4; update emp set salary=1500 where empno=10;

9 | P a g e Manual Of Database Lab

TA. Jaber Taradeh Title: Database Management System Lab

SELECT (salary)+150 FROM emp;

FILTERING ROWS USING THE WHERE CLAUSE

o

Using Comparison Operators

There are many other comparison operators that you can use in a WHERE

Clause besides the equality operator. The following table lists the comparison

Operators.

Operator

=

Description

Equal

<> or !=

<

>

<=

>=

ANY

Not equal

Less than

Greater than

Less than or equal

Greater than or equal

Compares one value with any value in a list

Compares one value with all values in a list ALL o

Using the SQL Operators

Operator

LIKE

IN

BETWEEN

Description

Matches patterns in strings

Matches lists of values

Matches a range of values

IS NULL

IS NAN

Matches null values

New for Oracle10 g . Matches the NaN special value, which means “not a number”

IS INFINITE New for Oracle10 g . Matches infinite BINARY_FLOAT and BINARY_DOUBLE values

You can also use the NOT operator to reverse the meaning of LIKE, IN,

BETWEEN,

And IS NULL:

NOT LIKE

NOT IN

NOT BETWEEN

IS NOT NULL

IS NOT NAN

IS NOT INFINITE

The following sections cover the LIKE, IN, and BETWEEN operators.

10 | P a g e Manual Of Database Lab

TA. Jaber Taradeh

11 | P a g e

Title: Database Management System Lab

Using the LIKE Operator

You use the LIKE operator in a WHERE clause to see if any of the character strings in a text column match a pattern that you specify.

You specify patterns using a combination of normal characters and the following two wildcard characters:

Underscore character ( _ ) Matches one character in a specified position

Percent character (%) Matches any number of characters

 beginning at the specified position

SYNTAX:

SELECT * FROM table_name WHERE column_name LIKE

'_string%';

OR

SELECT * FROM table_name WHERE column_name NOT

LIKE '_string%';

Using the IN Operator

You can use the IN operator in a WHERE clause to select only those rows whose column value is in a list that you specify.

SYNTAX:

SELECT * FROM table_name WHERE column_name IN

(text|Num, text|Num, text|Num,…….);

OR

SELECT * FROM table_name WHERE column_name NOT IN

(text|Num, text|Num, text|Num,…….);

Using the BETWEEN Operator

You use the BETWEEN operator in a WHERE clause to select rows whose columnvalue is inclusive within a specified range.

Syntax:

SELECT * FROM table_name WHERE column_name BETWEEN expression AND expression;

OR

SELECT * FROM table_name WHERE column_name NOT

BETWEEN expression AND expression;

Manual Of Database Lab

TA. Jaber Taradeh o

Logical Operators

Title: Database Management System Lab

Operator x AND y x OR y

NOT x

Description

Returns true when both x and y are true

Returns true when either x or y is true

Returns true if x is false, and returns false if x is true

SYNTAX:

SELECT * FROM table_name WHERE column_name comparison operator expression LOGICAL OPERATORS column_name comparison operator expression;

Understanding Operator Precedence

If you combine AND and OR in the same expression, the AND operator takes precedence over the OR operator (which means it’s executed first). The comparison operators take precedence over

AND. Of course, you can override these using parentheses.

12 | P a g e Manual Of Database Lab

TA. Jaber Taradeh Title: Database Management System Lab

ASSIGNMENT #2

Suppose you have the following tables answer the below questions

Q .1 change the selling price of (1.44 floppy drive) to 1150.00

Q.2 delete the records with the client 0001 from the client table.

Q.3 change the city of the client number '0005' to Bombay.

Q.3 change the bal_due of the client number '0001' to 1000.

Q.4 find the products whose selling price is more than 1500.

Q.5 list all the clients who are located in Bombay.

Q.6 display information of the client’s number 0001 and 0002.

Q.7 find the list of all clients who stay in the city 'Bombay' or city 'Delhi' or 'Madars'.

Q.8 find the products whose selling price is greater than 2000 and less than or equal to 5000.

Q.9 retrive the clients who stay in a city whose second letter is 'a'.

Q.10 retrive all the clients having 'a' as the second letter in their names.

Q.11 list all products in sorted order of their description.

Q.12 list products description and sell price and add 2 to the sell price.

Q.13 list the clients whose state is null.

Q.14 list all products whose cost price are between 3000 and 11200.

13 | P a g e Manual Of Database Lab

Download