Lab 05 - Database Systems
Lab 05 - Database Systems
Items Description
Course Title Database System
Lab Title Introduction to Oracle 11g and SQL Select Statement
Duration 3 Hours
Operating System Windows Operating System/Oracle 10g
/Tool/Language
Objective To get familiar with oracle 10g and SQL select statement
Relational Database Management are composed of objects or relations . They are managed by
operations and governed by data integrity constraints
SQL is a special-purpose language used to define, access, and manipulate data. SQL is
nonprocedural, meaning that it describes the necessary components (i.e., tables) and desired
results without dictating exactly how those results should be computed. Every SQL
implementation sits atop a database engine, whose job it is to interpret SQL statements and
determine how the various data structures in the database should be accessed to accurately and
efficiently produces the desired outcome.
The SQL language includes two distinct sets of commands: Data Definition Language (DDL) is
the subset of SQL used to define and modify various data structures, while Data Manipulation
Language (DML) is the subset of SQL used to access and manipulate data contained within the
data structures previously defined via DDL. DDL includes numerous commands for handling
such tasks as creating tables, indexes, views, and constraints, while DML is comprised of just
five statements:
SQL is the language used to communicate with the server to access, manipulate and control data.
Using structured query language, one can communicate with the oracle server. SQL has
following advantages
o Efficient
o Easy to learn and use
o Functionally complete (retrieve , define and manipulate data in the tables)
The following sections discuss the basic categories of commands used in SQL to perform various
functions. These functions include building database objects, manipulating objects, populating
database tables with data, updating existing data in tables, deleting data, performing database
queries, controlling database access, and overall database administration.
The main categories are
o DDL (Data Definition Language)
o DML (Data Manipulation Language)
o DQL (Data Query Language)
o DCL (Data Control Language)
o Data administration commands
o Transactional control commands
Data Definition Language, DDL, is the part of SQL that allows a database user to create and
restructure database objects, such as the creation or the deletion of a table.
Some of the most fundamental DDL commands discussed during following hours include the
following:
o CREATE TABLE
o ALTER TABLE
o DROP TABLE
o DROP VIEW
o CREATE INDEX
o ALTER INDEX
o DROP INDEX
o CREATE VIE
Manipulating Data
Data Manipulation Language, DML, is the part of SQL used to manipulate data within objects
of a relational database.
Selecting Data
Though comprised of only one command, Data Query Language (DQL) is the most concentrated
focus of SQL for modern relational database users. The base command is as follows:
o SELECT
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
This command, accompanied by many options and clauses, is used to compose queries against a
relational database. Queries, from simple to complex, from vague to specific, can be easily
created.
A query is an inquiry to the database for information. A query is usually issued to the database
through an application interface or via a command line prompt.
Data control commands in SQL allow you to control access to data within the database. These
DCL commands are normally used to create objects related to user access and also control the
distribution of privileges among users. Some data control commands are as follows:
o ALTER PASSWORD
o GRANT
o REVOKE
o CREATE SYNONYM
You will find that these commands are often grouped with other commands and may appear in a
number of different lessons throughout this book.
Data administration commands allow the user to perform audits and perform analyses on
operations within the database. They can also be used to help analyze system performance. Two
general data administration commands are as follows:
o START AUDIT
o STOP AUDIT
Do not get data administration confused with database administration. Database administration
is the overall administration of a database, which envelops the use of all levels of commands.
Database administration is much more specific to each SQL implementation than are those core
commands of the SQL language.
In addition to the previously introduced categories of commands, there are commands that allow
the user to manage database transactions.
Introduction of Oracle
Oracle is a database management system tool used for database management. Oracle corporation
produces products and services to meet relational database management system needs. It is object
relational database management system. It has the full capabilities and functionality of a
relational database , plus the feature of an object database. It includes several features for
improved performances and functionality of online transaction processing applications
To start SQL Command Line from the operating-system command prompt, enter the following:
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
When prompted, enter the username and password of the user account (schema) that you want
to access in the local database. For example, enter HR for the username and
my_hr_passwordfor the password when prompted.
You can also include the username and password when you start SQLCommand Line.For
example:
Projection: Choose the columns in a table that you want returned by your query. You can choose
as few as many columns of the table as you require
Selection: choose the rows in a table that you want returned by a query. You can use various
criteria to restrict the rows that you see.
Joining: Bring togather data that is stored in different tables by creating a link between them
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
To view that which tables are accessed to the user which will help in query
This output gives the attribute name, specify this attribute is null or not and also display the data
type of the attribute
This output looks just like the code in the example. Notice that columns 1 and 3 in the output
statement are right-justified and that column 2 is left-justified. This format follows the alignment
convention in which numeric data types are right-justified and character data types are left-
justified.
The asterisk (*) in select * tells the database to return all the columns associated with the given
table described in the FROM clause. The database determines the order in which to return the
columns.
In the query example given below, each column name is listed in the SELECT clause. The order
in which the columns are listed is the order in which they will appear in the output. Notice both
the commas that separate the column names and the space between the final column name and the
subsequent clause (in this case FROM). The output would look like this:
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
The output is identical because only the format of the statement changed. Now that you have
established control over the order of the columns, you will be able to specify which columns you
want to see.
Suppose you do not want to see every column in the database. You used SELECT * to find out
what information was available, and now you want to concentrate on the check number and the
amount. You type
Now you have the columns you want to see. Notice the use of upper- and lowercase in the query.
It did not affect the result.
If you look at the original table,EMP, you see that some of the data repeats. For example, if you
looked at the salary column using
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
Notice that the amount 1250, 3000 is repeated. What if you wanted to see how many different
amounts were in this column? Try this:
Notice that only twelve rows are selected. Because you specified DISTINCT, only one instance
of the duplicated data is shown, which means that one less row is returned. ALL is a keyword that
is implied in the basic SELECT statement. You almost never see ALL because SELECT <Table>
and SELECT ALL <Table> have the same result.
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
SYNTAX:
WHERE <SEARCH CONDITION>
SELECT, FROM, and WHERE are the three most frequently used clauses in SQL. WHERE
simply causes your queries to be more selective. Without the WHERE clause, the most useful
thing you could do with a query is display all records in the selected table(s). For example:
Character strings and date values are enclosed by single quotation marks. Character values are
case-sensitive, and date values are format-sensitive. The default date format is DD-MON-RR.
This simple example shows how you can place a condition on the data that you want to retrieve.
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
Arithmetic Expressions
Create expressions with number and date data by using arithmetic operators.
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
In the above query, employee name, their original salary and salary plus increment of 3000 is
displayed
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
A column alias renames a column heading. It is useful with calculations immediately follows the
column name (There can also be the optional AS keyword between the column name and alias.)
It requires double quotation marks if it contains spaces or special characters or if it is case-
sensitive
In the query above, giving the ename an alias Name, and sal as Salary and results show that it
displays these names as a heading
Concatenation Operator:
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
Literal String:
A literal is a character, a number, or a date that is included in the SELECT statement. Date and
character literal values must be enclosed by single quotation marks. Each character string is
output once for each row returned.
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
Note: For every query or step also add screen shot in the lab report.
LAB TASK 1
1. Do the following statements return the same or different output:
SELECT * FROM DEPT;
select * from dept;
2. The following queries do not work. Why not?
a. Select *
b. Select * from emp
c. Select empno ename FROM emp;
3. Which of the following SQL statements will work?
a. select *
from salgrade;
b. select * from salgrade;
c. select * from salgrade
4. There are four coding errors in this statement. Can you identify them?
SELECT empno, ename
Sal X 12 ANNUAL SALARY
FROM emp;
5. Show the structure of the emp table. Select all data from the table.
6. Show the structure of the bonus table. Select all data from the table
7. Create a query to display the empno, hiredate, salary from the employee table
8. Create a query to display the unique manager id from the employee table
9. Create a query to display the empno, hiredate, salary and rename column as Emp #,
Joining Date, Salary from the employee table
10. Create a query to display all the data from the emp table. Separate each column by a
comma
11. Create a query that display the salary of employee with increment of 10%.
12. Display the employee name concatenated with the job tile and hires date and names the
column “Employee Details”
13. Create a query that display the employee details of all employees whose designation is
“CLERK”
14. Create a query that display the location of the department “OPERATIONS”
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
LAB TASK 2
Q1: For the following Logical Schema write DDL to create Registration Database.
Q2: Using alter query add an attribute in the table Enrolled. Name of the attribute is session. It
will contain values like Fall2020, Spring2020, Fall2021 etc.
Q3: write query to delete table Student. Report what happens when query is executed. What steps
should you take to make that happen.
References: