Anda di halaman 1dari 29

LAB

(DATABSE
ADMINISTRATION)
INTERACTIVE SQL-PART 1
MODIFYING THE STRUCTURE
OF TABLES
•The structure of the table can be modified by the using the “ALTER
TABLE” command.
•ALTER TABLE allows changing the structure of an existing table.
•It is possible to:

1. Add new columns


2. Delete columns
3. Create or destroy indexes
4. Change the data type of an existing column
5. Rename the column or table itself
SYNTAX
ALTER TABLE EMPLOYEE

ADD (BILL_NO NUMBER(5) NOT NULL);

Output:

Table altered
MODIFYING EXISTING
COLUMN
Syntax:

ALTER TABLE <TABLE NAME>

MODIFY (<COLOUMNNAME> <NEW DATA TYPE>

(<NEW SIZE>));

Example:
ALTER TABLE CUSTOMER MODIFY (FIRSTNAME CHAR(20));

Output:
Table altered
RENAMING TABLES
The syntax to rename table is:

RENAME <TABLE NAME> TO <NEW TABLE NAME>

Example:
RENAME BUSINESS TO INCOME;

Output:
Table renamed
DELETE OPERATIONS
The DELTETE command deletes rows from the table that satisfies the
condition provided by its where clause, and returns the number of records
deleted.

If a DELETE statement without a WHERE clause is issued then, all rows are
deleted.

The verb DELETE in SQL is used to remove either:

All the rows from a table


OR
Removal of All Rows
Syntax:
• DELETE FROM <Tablename>

Example:
• DELETE FROM CUSTOMERS;

Output:
• 16 rows deleted
Removal of specific row

Synatx:
DELETE FROM <TABLE NAME> WHERE <CONDITION>;

Example:
DELETE FROM BILLS WHERE BILLDT<‘01-Aug-2000’;

Output:
2 rows deleted
USING THE BETWEEN
OPERATOR
1. In order to select data that is within a range of values, the “BETWEEN” operator is used.

2. The BETWEEN operator allows the selection of rows that contain values within a
specified lower and upper limit.

3. The range specified after the word between is inclusive.

4. The lower value must be specified first.

5. You can use the WHERE clause and the AND operator.
LIST THE NAME AND SALARY OF ALL THE
EMPLOYEES WITH SALARIES GREATER THAN OR
EQUAL TO $1000 AND LESS THAN OR EQUAL TO $
5,000.

SQL> SELECT ename, sal


2 FROM emp
3 WHERE sal BETWEEN 1000 AND 1500;

ENAME SAL
---------- ---------
MARTIN 1250
TURNER 1500
WARD 1250
ADAMS 1100
MILLER 1300
USING THE IN OPERATOR
1. The IN operator helps reduce the need to use multiple OR conditions.
2. It is used to test for values in a specified list.

Example

Display employee number, name, salary, and manager’s employee number of all the employees whose manager’s
employee number is 7902, 7566, or 7788.

SQL> SELECT empno, ename, sal, mgr


2 FROM emp
3 WHERE mgr IN (7902, 7566, 7788);
EMPNO ENAME SAL MGR
--------- ---------- --------- ---------
7902 FORD 3000 7566
7369 SMITH 800 7902
7788 SCOTT 3000 7566
7876 ADAMS 1100 7788

In this query, you could obtain the same answer by


using the condition WHERE mgr = 7902 OR mgr =
7566 OR mgr = 7788.
USING THE LIKE OPERATOR
1. The LIKE operator allows comparison of one string value with another
string value, which is not identical in all aspects.

2. This can be achieved by using the wild card characters.

3. Two wild card characters that are available are:

% … allows finding a match for any string of any length.

- … allows finding a match for any single character.


EXAMPLE
List the employee name from the EMP table for any employee
whose name begins with an “S.”
SQL> SELECT ename
2 FROM emp
3 WHEREename LIKE 'S%';

The SELECT statement above returns the employee name from the
EMP table for any employee whose name begins with an “S.” Note
the uppercase “S.” Names beginning with an “s” will not be
returned.
USING THE LIKE OPERATOR
You can combine pattern-matching characters.

Example:
List the employees whose name have the second character as A.
SQL> SELECT ename
2 FROM emp
3 WHEREename LIKE '_A%';

ENAME
----------
MARTIN
JAMES
WARD
USING THE IS NULL OPERATOR
1. The IS NULL operator tests for values that are null.
2. A null value means the value is unavailable, unassigned, unknown, or inapplicable.
3. Therefore, you cannot test with (=) because a null value cannot be equal or unequal to
any value.

SQL> SELECT ename, mgr


2 FROM emp
3 WHERE mgr IS NULL;

ENAME MGR
---------- ---------
KING

The slide example retrieves the name and manager of all employees who do not
have a manager.
SORTING DATA IN A TABLE
1. Oracle allows data from a table to be viewed in a sorted order.
2. The rows retrieved from the table will be sorted in either
ascending or descending order depending on the condition
specified in the SELECT statement.
3. Sort rows with the order by clause.

• ASC: ascending order, Default (optional)


• DESC: descending order

The ORDER BY clause comes last in the SELECT statement


SYNTAX

The syntax for viewing data in a sorted order is as follow:

SELECT * FROM <TABLENAME>

ORDER BY <COLUMN NAME1> , <COLUMNNAME2>

<[SORT ORDER]>;

SQL> SELECT ename, job, deptno, hiredate


2 FROM emp
3 ORDER BY hiredate;

ENAME JOB DEPTNO HIREDATE


---------- --------- --------- ---------
SMITH CLERK 20 17-DEC-80
ALLEN SALESMAN 30 20-FEB-81
...
14 rows selected.
Sorting in Descending Order

SQL> SELECT ename, job, deptno, hiredate


2 FROM emp
3 ORDER BY hiredate DESC;

ENAME JOB DEPTNO HIREDATE


---------- --------- --------- ---------
ADAMS CLERK 20 12-JAN-83
SCOTT ANALYST 20 09-DEC-82
MILLER CLERK 10 23-JAN-82
JAMES CLERK 30 03-DEC-81
FORD ANALYST 20 03-DEC-81
KING PRESIDENT 10 17-NOV-81
MARTIN SALESMAN 30 28-SEP-81
...
14 rows selected.
SORTING BY COLUMN ALIAS
You can use a column alias in the ORDER BY clause. The slide
example sorts the data by annual salary.
SQL> SELECT empno, ename, sal*12 annsal
2 FROM emp
3 ORDER BY annsal;

EMPNO ENAME ANNSAL


--------- ---------- ---------
7369 SMITH 9600
7900 JAMES 11400
7876 ADAMS 13200
7654 MARTIN 15000
7521 WARD 15000
7934 MILLER 15600
7844 TURNER 18000
...
14 rows selected.
SORTING BY MULTIPLE
COLUMNS

1. You can sort query results by more than one column.

2. The sort limit is the number of columns in the given table.

3. In the ORDER BY clause, specify the columns, and separate the column
names using commas.

4. If you want to reverse the order of a column, specify DESC after its
name.

5. You can order by columns that are not included in the SELECT clause.
EXAMPLE

DISPLAY NAME AND SALARY OF ALL EMPLOYEES.


ORDER THE RESULT BY DEPARTMENT NUMBER
AND THEN DESCENDING ORDER BY SALARY.

SQL> SELECT ename, deptno, sal


2 FROM emp
3 ORDER BY deptno, sal DESC;
LAB TASKS
1. Create the tables as described below:

Table Name: CLIENT_MASTER (used to store client information)


Column Name Data Type Size Attributes
CLIENTNO CHAR 6 Primary key/first letter must
start with ‘C’
NAME CHAR 20 Not Null
ADDRESS CHAR 30
CITY CHAR 15
PINCODE NUMBER 8
STATE CHAR 15
BALANCEDUE NUMBER 10
Table Name: SALES_ORDER (used to store client’s order)

Column Name Data Type Size Attributes


ORDERNO CHAR 6 Primary key / first letter must start with ‘O’
CLIENTNO CHAR 6
ORDERDATE DATE Not Null
DELIVERYADDR CHAR 25
ESS
SALESMANNO CHAR 6
DELIVERYTYPE CHAR 1 Delivery: part (P) / full(F)
BILL CHAR 1
DELIVERYDATE DATE Cannot be less than Order date
Values ( ‘In Process’ , “Fulfilled’ ,
ORDERSTATUS CHAR 10 ‘BackOrder’ , ‘ Cancelled’ )
2. INSERT THE FOLLOWING DATA INTO THEIR
RESPECTIVE TABLES

Data for CLIENT_MASTER table

CLIENTNO NAME CITY PINCODE STATE BALANCE


DUE
C00001 Aisha Khan Lahore 450001 PUNJAB 15000
C00002 Hamid Ali Larkana 780001 SINDH 0
C00003 Aiman Raza Lalamusa 500057 PUNJAB 5000
C00004 Sara Khan Karachi 560001 SINDH 0
C00005 Hassan Ali Islamabad 560050 FEDERAL 2000
DATA FOR SALES_ORDER TABLE

ORDE CLIE ORDERD SALES DELIVE BIL DELIVE ORDER


R NT ATE MANN RY L RY STATUS
NO NO O TYPE DATE
O1900 C0000 12-June-04 S00001 F N 20-July- In Process
1 1 02
O1900 C0000 25-June- 04 S00002 P N 27-June- Cancelled
2 2 02
04686 C0000 18-Feb-04 S00003 F Y 20-Feb- Fulfilled
5 3 02
01900 C0000 03-Apr-08 S00004 F Y 07-Apr- Fulfilled
3 1 02
04686 C0000 20-May-09 S00005 P N 22-May- Cancelled
6 4 02
01900 C0000 24-May-05 S00006 F N 26-July- In Process
8 5 02
LAB TASKS
1. Delete from CLIENT_MASTER where the column state holds the value “Federal”.

2. Add a column called “TELEPHONE” of data type ‘Number’ and ‘Size’ = 10 to the
CLIENT_MASTER table.

3. Change the name of the CLIENT_MASTER table to “CLIENT_INFORMATION”.

4. List the name of all clients having ‘a’ as the second letter in their names.

5. List the clients who stay in a city whose first letter is ‘L’.

6. List all clients who stay in ‘Lahore’ or ‘Karachi’.

7. List all clients whose Balancedue is greater than 100000.


LAB TASKS

8. List the order information for CLIENTNO ‘C00001’ and ‘C00002’.

9. List the name, city and state of clients who are not in the state of ‘SINDH’.

10. Perform sorting in CLIENT_MASTER table to display latest order first.

Anda mungkin juga menyukai