Вы находитесь на странице: 1из 58

ASSIGNMENT -1

Q1.what is sql? What are the different types of categories of sql languages &
explain them?

Ans1. SQL (often erroneously referred to as Structured Query Language) ( )


is a database computer language designed for managing data in relational
database management systems (RDBMS), and originally based upon
Relational Algebra

• Data Definition Language (DDL) Statements


• Data Manipulation Language (DML) Statements
• Transaction Control Statements
• Session Control Statements
• System Control Statement
• Embedded SQL Statements

Data Definition Language (DDL) Statements = Data definition


language (DDL) statements let you to perform these tasks:

• Create, alter, and drop schema objects


• Grant and revoke privileges and roles
• Analyze information on a table, index, or cluster
• Establish auditing options
• Add comments to the data dictionary

The CREATE, ALTER, and DROP commands require exclusive access to the
specified object. For example, an ALTER TABLE statement fails if another
user has an open transaction on the specified table.

The GRANT, REVOKE, ANALYZE, AUDIT, and COMMENT commands do not


require exclusive access to the specified object. For example, you can
analyze a table while other users are updating the table.

Oracle Database implicitly commits the current transaction before and after
every DDL statement.

Data Manipulation Language (DML) Statements

Data manipulation language (DML) statements access and manipulate data


in existing schema objects. These statements do not implicitly commit the
current transaction. The data manipulation language statements are:

CALL
DELETE
EXPLAIN PLAN
INSERT
LOCK TABLE
MERGE
SELECT
UPDATE

The SELECT statement is a limited form of DML statement in that it can only
access data in the database. It cannot manipulate data in the database,
although it can operate on the accessed data before returning the results of
the query.

The CALL and EXPLAIN PLAN statements are supported in PL/SQL only when
executed dynamically. All other DML statements are fully supported in
PL/SQL.

Transaction Control Statements

Transaction control statements manage changes made by DML statements.


The transaction control statements are:

COMMIT
ROLLBACK
SAVEPOINT
SET TRANSACTION

All transaction control statements, except certain forms of


the COMMIT and ROLLBACK commands, are supported in PL/SQL. For
information on the restrictions, see COMMIT and ROLLBACK .

Session Control Statements

Session control statements dynamically manage the properties of a user


session. These statements do not implicitly commit the current transaction.

PL/SQL does not support session control statements. The session control
statements are:

ALTER SESSION
SET ROLE
Embedded SQL Statements

Embedded SQL statements place DDL, DML, and transaction control


statements within a procedural language program. Embedded SQL is
supported by the Oracle precompilers and is documented in the following
books:

Q2.what are different data types in oracle?

Ans the information in a datatypes is mentaines in the form of tables and


each table consists of rows and column , which store data and therefore this
data must have some data must have data type have i.e the type of data ,
which is stored is a table

The different types of data available in oracle are

Data types Description

Varchar2 Cantains variable length text strings


of up to 2000 bytes

Char Contains fixed text strings of up to


255 bytes

Number Contains numeric data

Date Contains date data

Raw Contains binary data of up to 255


bytes

Long Contains text data of up to 2


gigabytes

Long raw Contains binary data of up to


2gigabytes

Rowed Contains disk location for table rows

Blob Large binary object

Clob Large character based object

Nclob Large single or multi-byte character


based object
Bfile Large external file

Q.3what are different commands in each category of sql languages & explain
them?

Ans Defining Database Structures


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:

CREATE TABLE

ALTER TABLE

DROP TABLE

CREATE INDEX

ALTER INDEX

DROP INDEX

CREATE VIEW

DROP VIEW

These commands are discussed in detail during Hour 3, "Managing Database


Objects," Hour 17, "Improving Database Performance," and Hour 20,
"Creating and Using Views and Synonyms."

Manipulating Data
Data Manipulation Language, DML, is the part of SQL used to manipulate
data within objects of a relational database.

There are three basic DML commands:

INSERT
UPDATE

DELETE

These commands are discussed in detail during Hour 5, "Manipulating Data."

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:

SELECT

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.
The SELECT command is discussed in exhilarating detail during Hours 7
through 16.

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 Language


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:

ALTER PASSWORD

GRANT

REVOKE

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


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:

START AUDIT

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.

Transactional Control Commands


In addition to the previously introduced categories of commands, there are
commands that allow the user to manage database transactions.

• COMMIT Saves database transactions

• ROLLBACK Undoes database transactions

• SAVEPOINT Creates points within groups of transactions in which


to ROLLBACK

• SET TRANSACTION Places a name on a transaction

Transactional commands are discussed extensively during Hour 6,


"Managing Database Transactions."
ASSIGNMENT -2

Q1.Create Table employee27 with following attributes:

Employee27(empid(5),Firstname(15),lastname(15),address(50),phoneno(10),
job(10),salary(7))?

Solution 1:

Create table employee27

(empid varchar(5),

Firstname char(15),

Lastname char(15),

Address varchar(50),

Phoneno number(10),

Job char(10),

Salary number(7));
Q2.Enter 10 records in the employee27 table?

Solution 2:

Insert into employee27


values(‘e100’,’avanish’,’goel’,’rohini’,24235453,’manager’,25000);

Insert into employee27


values(‘e101’,’ford’,’kutcher’,’pitampura’,24264565,’accountant’,30000);

Insert into employee27


values(‘e102’,’amit’,’aggarwal’,’rohini’,24245453,’manager’,35000);

Insert into employee27 values(‘e103’,’john’,’smith’,’paschim


vihar’,25554453,’clerk’,15000);

Insert into employee27


values(‘e104’,’james’,’stewart’,’rohini’,24675453,’accountant’,20000);

Insert into employee27


values(‘e105’,’ashok’,’goel’,’janakpuri’,24543453,’engineer’,40000);

Insert into employee27


values(‘e106’,’rahul’,’gupta’,’vikaspuri’,’25656673,’manager’,45000);
Insert into employee27
values(‘e107’,’karan’,’jindal’,’rohini’,25655663,’accountant’,30000);

Insert into employee27


values(‘e108’,’vikas’,’goel’,’rohini’,24256553,’manager’,35000);

Insert into employee27 values(‘e109’,’rakesh’,’gupta’,’paschim


vihar’,’24565463,’accountant’,45000);

Q3.Describe the structure of employee27 table?

Solution 3:

Desc employee27:

Output:

Name NULL
TYPE

EMPNO VARCHAR(5)
FIRSTNAME CHAR(15)

LASTNAME CHAR(15)

ADDRESS VARCHAR(15)

Q4.Display the name , salary , jobs of all the employees ?

Solution:

Select firstname, lastname, salary , job , from employee27;

Output:

Firstname lastname
job salary

10anish goel
manager 45000
rahul gupta
manager 45000

vikas aggarwaal
manager 45000

Q5.Perform the Followings:

a. Display the name, salary, job of an employee whose empid = e101?

b. Display the name , salary , job of an employee whose firstname is


‘amit’

c. Display the name, salary, job of an employee whose salary is greater


than 25000?

Solution 5:

a. Select firstname, lastname, salary, job from employee27 where empid


= ‘e101’;

Output:

Firstname lastname
job salary

avnish goel
manager 45000
b.Select firstname, lastname, salary,job from employee27 where
firstname = ‘amit’

Output:

Firstname lastname
job salary

Amit goel
manager 45000

c. Select firstname , lastname , job from employee27 where salary > 25000;

Output:

Firstname lastname
job salary

avnish goel
manager 45000

rahul gupta
manager 55000

vikas aggarwaal
manager 35000

ASSIGNMENT -3

Q1.write the syntax of alters, delete and update commands?

Solution:

Alter

1. Adding a new column:

• Alter table table_name add(new_column_name datatype(size));


2. Delete an existing column:

• Alter table table_name drop column column_name;

3. Increasing size of existing column:

• Alter table table_name modify(column_name datatype(size));

Delete

1. To delete all fields:

• delete from tablename;

2. To delete with condition:

• delete from tablename where condition;

Update

• update tablename set column1 = value1 , column2 = value2 where


column3 = value3;

Q.2 Remove the data of an employee whose empid = e102?

Solution:

Delete from employee27 where empid = ‘e102’;


Q3.Update the salary of an employee from 45000 to 55000 whose job is
‘manager’ ?

Solution:

Update employee27 set salary = 45000 where job = ‘manager’;

Output:

Empno Firstname lastname address


job salary

E100 avnish goel rohini


manager 45000

E106 rahul gupta


vikaspuri manager 45000

E108 vikas aggarwaal rohini


manager 45000
Q4.Add a column ‘dept’ and ‘hire date’ in the employee table?

Solution:

Alter table employee27 add (dept number (5));


Alter table employee27 add (hiredate);

Output:

Empno Firstname lastname address job


salary Dept hiredate

E100 Avnish Goel Rohini manager


45000

E106 Rahul Gupta Vikaspuri manager


45000

E108 Vikas Aggarwaal Rohini manager


45000

E107 Varun Bagha Rohini clerk


15000
Q5.Enter 10 records for dept and hiredate column?

Solution

Insert into employee27 (dept, hiredates) values (10, 15/Aug/2010);

Insert into employee27 (dept, hiredates) values (20, 24/Jan/2010);

Insert into employee27 (dept, hiredates) values (30, 22/June/2010);

Insert into employee27 (dept, hiredates) values (40, 11/Sept/2010);

Insert into employee27 (dept, hiredates) values (50, 17/March/2010);

Insert into employee27 (dept, hiredates) values (20, 29/April/2010);

Insert into employee27 (dept, hiredates) values (30, 05/May/2010);

Insert into employee27 (dept, hiredates) values (40, 13/July/2010);


Insert into employee27 (dept, hiredates) values (20, 23/February/2010);

Insert into employee27 (dept, hiredates) values (10, 25/Nov/2010);

Q6. Increase the size of a column job to 10?

Solution:

Alter table employee27 modify (job char (10));

Output
Name

Avnish

Rahul

Vikas

Varun
ASSIGNMENT -4

Q1.Display the unique Jobs from the employee27 table?

Solution

Select distinct jobs from employees27;

Output

Jobs

Manager

Manager

Clerk

Assistant
Q2.Display the names of an employee whose First name start’s with
character ‘a’ ?

Solution

Select firstname form employee27 where firstname like’a%’;

Output:

FirstName

Rahul

Varun

Tarun
Aman

Q3.List the names of employees ending with character ‘s’ ?

Solution

Select firstname form employee27 where firstname like’s%’;

Output
FirstName

Vikas

Suhaas

Paras
Q4.List the names of the employees whose name has exactly 5 character’s?

Solution

Select firstname from employee27 where firstname like”____”

Output:

James

Vikaas

Rahul

Karan
Q.5 List the names of employees having ‘a’ as second character ?

Solution

Select firstname form employee27 where firstname like’_a%’;

Output:

FirstName

Aakash
Lakshay

Rahul

Sanchit

Q.6 List the names of employees having 2 a’s in their name ?


Solution:

output

FirstName

Karan
Q7.List the name’s, job’s, id of an employee whose salary between 25000 to
45000 ?

Solution:

Select firstname, lastname, jobs, empid from employee27 where salary


between 25000 to 45000;

Output:

Firstname lastname
job salary

avnish goel
manager 45000

rahul gupta
manager 45000
vikas aggarwaal
manager 45000

Q8.List the name’s, id of an employee whose department is either 10 and


20 ?

Solution:
Select firstname, lastname , empid from employee27 where dept as(10,20);

Output

Firstname lastname
empno

avnish goel
e101

rahul gupta
e106

vikas aggarwaal
e109
Q9.list the id of an employee who do not have the name ‘ford’, ‘james’ and
‘john’?

Solution:

Select empid from employee27 where firstname not in (‘ford’, ‘james’,


‘john’);

Output:

Empno

E102

E109

E104

E103
E108

Q10.List the names,jobs,id of an employee’s having hire date between


1/10/2005 to 1/10/2007

Solution

Select firstname, lastname, job, empid from employee27 where hiredate


between 1/Oct/2005 to 1/Oct/2007

Output:

Firstname lastname
job Empno

avnish goel
manager E101

rahul gupta
manager E109

vikas aggarwaal
manager E105
ASSIGNMENT -5

Q1. Display the first name, last name, job, salary of an employee and sort it
using multiple column on the first name and last name?

Solution:

Select firstname, last name, job, salary from employee27 order by firstname
lastname;

Output:

Firstname lastname
job Salary

avnish goel
manager 45000

rahul gupta
manager 45000

vikas aggarwaal
clerk 20000
Q2.Display the names, salary, birthdate of an employee and sort the dates
on the basis of column 3rd ?

Solution:

Select firstname,lastname,hiredate,salary from employee27 order by 3;

Output:

Firstname lastname
Hiredate Salary

avnish goel
13-Nov-09 45000

rahul gupta
10-Dec-09 45000

vikas aggarwaal
16-Jun-10 31000
ASSIGNMENT -6

Q1.Create the customer table where customer id is a primary key , customer


name is not well , customer name is not null , customer address is null ?

Solution:

Create table customer4

(cid varchar(10) primary key,

Cname char(15)not null

Caddress varchar (30)null);


Q2.Create table where department id is a primary key and department name
is a unique key ?

Solution:

Create table department4

(deptno varchar(10) primary key,

Dname char(20)unique key));


Q3.Add a primary key constraint in the employee id of employee id of
employee table ?

Solution:

Alter table employee27 modify (empid varchar(5)primary key);


Q4.Add a foreign key constraint in the dept column of employee4 table ?

Solution:

Alter table employee4 modify (empid varchar(5)foreign key references


department4);
ASSIGNMENT -7

Q1.Display the name , salary of employees and name the table as ‘emp’ ?

Solution:

Select firstname , lastname , sal from employee4 ‘emp’;


Q2.Select the first name of all the employees and nemae the column as
name ?

Solution:

Select firstname ‘name’ from employee4;


Q3.Display unique department from department table ?

Solution:

Select distinct department from department4;

Output:

Department

France

Hr.

Production
Q4.Display jobs , total salary of an employee on the limits of them jobs ?

Solution:

Select job , sum(sal) from employee4 group by side;

Output:

Sum(salary)
Job

125000 Accountant

15000 manager

14000 manager
Q5.Display the job , maximum salary of an employee whose salary is greater
than 25000 grouped on the basis of jobs and whose salary is greater then
40000

Solution:

Select job , max(sal) from employee27 where sal>25000 group by job having
sal>45000;

Output:

Job
Max(Salary)

Accountant
45000

Manager
45000
ASSIGNMENT -8

Q1.Display the current date ?

Solution:

Select sysdate from dual;

Output:
Sysdate

19-Nov-10

Q2.Display your name with 12(*) Character at the left ?


Solution:

Select lpad(‘Lakshay’,12,(*)) from dual;

Output:

LPAD(lakshay,12,*)

************lakshay
Q3.Display the length of your name ?

Solution:

Select length (‘lakshay’)from dual;

Output:

Length(laksahy)

5
Q4.Dispaly the substring of your name of length 4,starting with 3rd
character ?
Solution:

Select substr(‘lakshay’,3,4) from dual;

Output:

Substringchar

Hayp
Q5.Display the followings:

a)Remove the leading a’s from string ’aadsaggfaa’ ?

b)Remove a’s from the sides of a string ‘aadsaggfaa’?

Solution:

a) Select ltrim(‘a’ from ‘aadsaggfaa’) from dual;

Output:

Ltrm(aadsaggfaa)

Dsaggf

b) Select ltrim(both , ‘a’ from ‘aadsaggfaa’) from dual;


Output:

Ltrm(aadsaggfaa)

aadsaggf

Вам также может понравиться