Professional Documents
Culture Documents
INDEX
SQL AND PL/SQL
S.NO. SIGN.
1. SQL Definition
Primary key
Foreign key
Command
Aggregate Function
Normalization of Database
PL/SQL
TABLE
1. Create employee table insert data in employee table (empno,
empname, job, doj, salary, deptno)
2. Create client table insert data into client table (clno, clname,
address, state, balance)
3. Create salesman table insert data into salesman table (slno,
sname, oddate, salary, address, city)
4. Create tabte student table insert into data student table
(rno,sname,class,msub ,feedue,percent)
QUERY
1. Create a query to display emp no & emp name for all the emp
earn more than average salary.
2. Select empno,empname,job whose date 1-jan-99 to1-dec-99
3. Display emp name,job,salary from emp1 where
empname='sheetal';
4. Display emp name,salary,dep no. work in company as an
army.
5. Display emp no. & salary from that job based upto deptno.
6.
CREATE A QUERY TO DISPLAY DEPT NO. EMP NO. OF JOB WORK IN
EMPLOYEE.
30. List all students whose percent is more than average percent.
YUGAL KISHOR SAHU BSC 3rdYEAR
SQL
Definition:- the structured query language (SQL)is a language that enables us to create and
operate on relation databases ,which are sets of related information stored in tables.
Structured query language is not case sensitive. Keywords of SQL are written in
uppercase.
Statements of SQL are dependent on text lines. We can use a single SQL statement on
one or more multiple text line.
SQL depends on tuple relational calculus and relational algebra.
Datatypes in SQL:
Datatype is used to define the type of data that will be stored in a particular field .
(1) Char (size) : this data type is used to store character string of fixed length .the size is
given to determine the number of characters we want to store for a field . the maximum
allowable field size is 255 characters .
(2) Varchar (size) or varchar2 (size) : this data type is used to store alphanumeric
data . the maximum allowable field size for this is 2000 characters.
(3) Number (P,S): this data type is used to store numbers (fixed or floating point ).
(4) Data : this data type is used to represent data & time . the standard format is ‘DD-
MON-YY’.
YUGAL KISHOR SAHU BSC 3rdYEAR
SQL COMMANDS
SQL commands are instructions . It is used to communicate with the database and perform the
specific tasks, functions and queries of data.
SQL uses certain commands like Create, Drop, Insert etc. to carry out the required tasks.
These SQL commands are mainly categorized into four categories as:-
DDL
DML
DCL
TCL
1. DML:-
DML stands for data definition language. It changes the structure of the table
like creating a table, deleting a table, altering a table etc.
Some commands that come under DDL are-
CREATE
ALTER
DROP
2. DML:-
DML stands for data manipulation language. It is used to modify the database.
Some commands that come under DML are-
INSERT
UPDATE
DELETE
3. DCL:-
DCL stands for data control language. It is used to grant and take back authority
from any database user.
Some commands that come under DCL-
GRANT
REVOKE
4. TCL:-
TCL stands for transaction control language. Some commands that come under
TCL are-
COMMIT
ROOLBACK
SAVEPOINT
CREATE TABLE: A table is basic unit of storage. It is composed of rows and columns.
To
create a table we will name the table and the columns of the table. We follow the rules to name
tables and columns:-
It must begin with a letter and can be up to 30 characters long.
It must not be duplicate and not any reserved word.
Syntax :-
YUGAL KISHOR SAHU BSC 3rdYEAR
Table created.
SELECT : The select command of sql lets you make queries on the database. A query is a
command that is given certain specified information from the database tables. It can be used to
retrieve a subset of rows or columns from one or more tables.
Syntax :-
SELECT* FROM <tablename>;
Example:-
DESCRIBE : To find information about columns like column name, their data types and
other
attributes of a table we can use DESCRIBE command.
Syntax :-
DESCRIBE tablename;
YUGAL KISHOR SAHU BSC 3rdYEAR
Example:-desc emp1;
EMPNO NUMBER(10)
EMPNAME CHAR(20)
JOB CHAR(15)
DOJ DATE
SALARY NUMBER(15)
DEPNO NUMBER(15)
ALTER TABLE : After creating a table one may have need to change the table either by
add
new columns or by modify existing columns. One can do so by using alter table command.
Syntax :-to add a column is
ALTER TABLE tablename ADD(col1 datatype,col2 datatype);
Table altered.
SQL>desc emp1;
EMPNO NUMBER(10)
EMPNAME CHAR(25)
JOB CHAR(15)
DOJ DATE
SALARY NUMBER(15)
DEPNO NUMBER(15)
YUGAL KISHOR SAHU BSC 3rdYEAR
DELETE : One can delete data fron table by using delete from statement. The delete statement
removes rows from table but it doesn’t release storage space.
Syntax:-
Syntax of delete rows from table is
DELETE FROM tablename WHERE <condition>;
Example:-
SQL>delete from emp1 where empname =’kumesh’;
row deleted.
UPDATE : The update command enables user to change the values of existing rows.
Syntax :-Syntax to update value of table is
UPDATE tablename SET col1=value1,col2=value2;
Example:-
1 row updated.
DROP TABLE : To remove the definition of oracle table, the drop table statement is used.
Syntax :-Syntax to drop table is
DROP TABLE tablename;
Commit : this command makes permanent any changes made by statement that have been
executed since the beginning of this transaction. Generally user uses this command at the end of
every session.
Keys: keys are attributes or set of attributes used to distinguish one entity from
another in an entity set.
Primary key: the primary key is the term used for the candidate key that is
chosen by the database designer as the principal means of identifying an entity.
EXAMPLE:-
Consider a student table. In the “student” table there are three attributes Stu_ID, Stu_Name
and Stu_Age.
Student
Out of these three attributes, one attribute or a set of more than one attributes can be a
primary key. Attribute Stu_Name alone cannot be a primary key as more than one students
can have same name. Attribute Stu_Age alone cannot be a primary key as more than one
students can have same age.Attribute Stu_ID alone is a primary key as each student has a
unique id that can identify the student record in the table.
Consider two tables “Student” and “Department” having attributes as stu_id, stu_name, and
course in Student table and Dept_name and stu_id in table Department.
YUGAL KISHOR SAHU BSC 3rdYEAR
Student
Department
Dept_name Stu_id
CS_Department 105
CS_Department 101
Science_Department 101
Math_Department 108
In the tables, one attribute is common that is stu_id, but it has different key constraints for both
tables. In the Student table the field stu_id is a primary key because it is uniquely identifying all
other fields of the Student table.
On the other hand, stu_id is a foreign key attribute for the Department table because it is acting
as a primary key attribute for the Student table.
It means both that both the Student and Department table are linked with one another because of
the stu_id attribute.
AGGREGATE FUNCTION
COUNT
SUM
AVG
MAX
MIN
1. COUNT FUNCTION
o COUNT function is used to Count the number of rows in a database table. It can work
on both numeric and non-numeric data types.
YUGAL KISHOR SAHU BSC 3rdYEAR
o COUNT function uses the COUNT(*) that returns the count of all the rows in a
specified table. COUNT(*) considers duplicate and Null.
FROM employees;
Total
----------
17
2. SUM Function
Sum function is used to calculate the sum of all selected columns. It works on numeric fields
only.
Syntax:-SUM([DISTINCT \ALL]n)
FROM employees;
Total
----------
691400
3. AVG function
The AVG function is used to calculate the average value of the numeric type. AVG function
returns the average of all non-Null values.
Syntax:-AVG([DISTINCT l ALL]n)
YUGAL KISHOR SAHU BSC 3rdYEAR
FROM employees;
Average
--------------
6461.83178
4. MAX Function
MAX function is used to find the maximum value of a certain column. This function determines
the largest value of all selected values of a column.
Syntax:-MAX([DISTINCT l ALL]expr)
Example:-The following example determines the highest salary in the hr.employees table:
FROM employees;
Maximum
----------
24000
5. MIN Function
MIN function is used to find the minimum value of a certain column. This function
determines the smallest value of all selected values of a column.
Syntax:-MIN([DISTINCT l ALL]expr)
Example:-The following statement returns the earliest hire date in the hr.employees table:
FROM employees;
YUGAL KISHOR SAHU BSC 3rdYEAR
Earliest
---------
13-JAN-01
NORMALIZATION OF DATABASE
Below student relation is not in 1NF because of multi-valued attribute stu_phone. It’s
decomposition into 1NF is given by “table2”.
YUGAL KISHOR SAHU BSC 3rdYEAR
Rule 1- Be in 1NF
Rule 2- Single Column Primary Key
In the above table, non primary attribute teach_age is dependent on teach-id is a proper
subset of a candidate key. Hence, it is not in 2NF.
To convert the given table into 2NF, we decompose the above table into two tables:
Teacher_Detail-
Teache_id Teach_age
25 30
47 35
83 38
Teacher_subject-
Teach_id Subject
25 chemistry
25 Biology
47 English
83 Math
83 Computer
Rule 1- Be in 2NF
Rule 2- Has no transitive functional dependencies
In the above table, Book_id determines Genre_id and Genre_id determines Genre type.
Therefore Book_id determines Genre type through Genre_id and we have transitive
functional dependency. Hence it is not in 3NF form.
To convert the above table into 3NF we decompose it into two tables.
TABLE_BOOK-
TABLE_GENRE-
Even when a database is in 3rd Normal Form, still there would be anomalies resulted if it
has more than one Candidate Key.
Example:- Let us assume there is a company where employees work in more than one
department.
YUGAL KISHOR SAHU BSC 3rdYEAR
EMPLOYEE
Emp_id→ Emp_country
The table is not in BCNF because neither Emp_dept nor Emp_id alone are keys.
To convert the above table into BCNF, we decompose it into three tables:
Emp_country-
Emp_id Emp_country
264 INDIA
264 INDIA
Emp_dept-
Emp_dept Dept_type Emp_dept_no
Designing D394 283
Testing D394 300
Stores D283 232
Developing D283 549
Emp_id Emp_dept
D394 283
D394 300
D283 232
D283 549
Emp_id → Emp_country
Now this is in BCNF because left side part of both the functional dependencies is a key.
If no database table instance contains two or more, independent and multivalued data describing
the relevant entity, then it is in 4th Normal Form.
Example:- Student
In the student relation, a student with Stu_id contains two courses, computer and math and
hobbies dancing and singing. So there is a multi – valued dependency on stu_id, which leads to
unnecessary repetition of data.
So to make the above table into 4NF, we can decompose it into two tables:
STUDENT_COURSE:
Stu_id Course
21 Computer
21 Math
34 Chemistry
74 Biology
59 Physics
STUDENT_HOBBY:
Stu_id Hobby
21 Dancing
21 Singing
34 Dancing
74 Cricket
59 Hockey
YUGAL KISHOR SAHU BSC 3rdYEAR
In the above table “class”, john takes both Computer and Math class for Sem1 but he does not
take Math class for Sem2. In this case combination of all these fields required to identify a valid
data.
So, to convert the above table into 5NF, we can decompose it into three tables P1, P2 and P3.
P1:
Semester Subject
Sem1 Computer
Sem1 Math
Sem1 Chemistry
Sem2 Math
P2:
Subject Lecturer
Computer Anshika
Computer John
Math John
Math Akash
Chemistry Praveen
Semester Lecturer
Sem1 Anshika
YUGAL KISHOR SAHU BSC 3rdYEAR
Sem1 John
Sem1 John
Sem2 Akash
Sem1 Praveen
P3:
PL/SQL
The PL/SQL programming language was developed by Oracle Corporation in the late 1980s as
procedural extension language for SQL and the Oracle relational database. Following are
certain notable facts about PL/SQL −
PL/SQL is a completely portable, high-performance transaction-processing language.
PL/SQL provides a built-in, interpreted and OS independent programming environment.
PL/SQL can also directly be called from the command-line SQL*Plus interface.
Direct call can also be made from external programming language calls to database.
PL/SQL's general syntax is based on that of ADA and Pascal programming language.
Features of PL/SQL
PL/SQL has the following features −
Advantages of PL/SQL
Syntax:
The Basic Syntax of PL/SQL programs are divided and written in logical blocks of code. Each
block consists of three sub-parts −
Declarations
1 This section starts with the keyword DECLARE. It is an optional section and
defines all variables, cursors, subprograms, and other elements to be used in
the program.
Executable Commands
This section is enclosed between the keywords BEGIN and END and it is a
2 mandatory section. It consists of the executable PL/SQL statements of the
program. It should have at least one executable line of code, which may be just
a NULL command to indicate that nothing should be executed.
Exception Handling
3 This section starts with the keyword EXCEPTION. This optional section
contains exception(s) that handle errors in the program.
Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested
within other PL/SQL blocks using BEGIN and END.
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
YUGAL KISHOR SAHU BSC 3rdYEAR
Example:-
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
The end; line signals the end of the PL/SQL block. To run the code from the SQL
command line, we may need to type / at the beginning of the first blank line after the
last line of the code.
When the above code is executed at the SQL prompt, it produces the following result −
Hello World
Table
Coding:-
SQL> create table emp1(empno number,empname char(20),job char(15),doj date,salary
number,depno number);
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
row created.
YUGAL KISHOR SAHU BSC 3rdYEAR
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
11 rows selected.
YUGAL KISHOR SAHU BSC 3rdYEAR
Q 02:- CREATE CLIENT TABLE INSERT DATA INTO CLIENT TABLE (CLNO, CLNAME,
ADDRESS , STATE, BALANCE)
Coding:-
SQL> create table t2(clno number,clname char(9),address char(9),state char(9),balance
number);
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
8 rows selected
YUGAL KISHOR SAHU Bsc 3rd year(cs)
Q 03:- CREATE SALESMAN TABLE INSERT DATA INTO SALESMAN TABLE (SLNO,
SNAME, ODDATE, SALARY , ADDRESS , CITY)
Coding:-
SQL> create table t3(sno number,sname char(9),oddate date,salary number,address
char(9),city char(20));
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
QUE4:- CREATE TABTE STUDENT TABLE INSERT INTO DATA STUDENT TABLE
(RNO,SNAME,CLASS,MSUB ,FEEDUE,PERCENT)
Coding:-
SQL> create table t4(rno number,sname char(15),class varchar(20),msub char(20),feedue
number,percent varchar(20));
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
7 rows selected
QUERY 1:-CREATE A QUERY TO DISPLAY EMP NO & EMP NAME FOR ALL THE
EMP EARN MORE THAN AVERAGE SALARY.
Coding:-
SQL> select empno,empname from emp1 where salary>(select avg(salary)from emp1);
EMPNO EMPNAME
512 vikram
124 bittu
125 manoj
126 jitendra
127 rekhu
Coding:-
SQL> select empname,job,salary from emp1 where empname='sheetal';
Coding:-
SQL> select empname,job,salary from emp1 where job='techer';
QUERY 5:- DISPLAY EMP NO . & SALARY FROM THAT JOB BASED UPTO DEPTNO.
Coding :-
11 rows selected.
YUGAL KISHOR SAHU Bsc 3rd year(cs)
QUERY 6:-CREATE A QUERY TO DISPLAY DEPT NO. EMP NO. OF JOB WORK IN
EMPLOYEE.
121 1 lecturer
102 2 techer
234 3 professor
123 4 clerk
345 5 principal
321 6 programmer
512 7 penter
124 8 police
125 9 army
126 10 electrician
127 11 engineer
YUGAL KISHOR SAHU Bsc 3rd year(cs)
Coding:-
SQL> select count(empno)from emp1;
COUNT(EMPNO)
11
Coding:-
CODING:-
1 row updated.
QUERY 10:- LIST THE CLIENT NAME WHO ARE LOCATE IN STATE CG.
CODING:-
SQL> select clname from t2 where state='cg';
CLNAME
yugal
sheetal
pravin
QUERY 11:- CHANGE THE SIZE OF CLIENT NAME FROM TABLE CLIENT.
CODING:-
SQL> alter table t2 modify(clname char(15));
Table altered.
8 rows selected.
YUGAL KISHOR SAHU Bsc 3rd year(cs)
Coding:-
SQL> create table company(rno varchar(15) primary key,clno number,name char(20));
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
YUGAL KISHOR SAHU Bsc 3rd year(cs)
221 4sheetal
223 5 yugal
224 6 sonu
225 6 rekhu
Table created.
1 row created.
221 4sheetal
YUGAL KISHOR SAHU Bsc 3rd year(cs)
Coding:-
SQL> alter table t2 add(telephone varchar(10));
Table altered.
Table altered.
SNO NUMBER
SNAME CHAR(9)
ODDATE DATE
SALARY NUMBER
ADDRESS CHAR(9)
CITY CHAR(9)
YUGAL KISHOR SAHU Bsc 3rd year(cs)
1 row deleted.
Coding:-
SQL> select count(oddate)from t3;
COUNT(ODDATE)
5
YUGAL KISHOR SAHU Bsc 3rd year(cs)
QUERY 18:- SELECT ALL SALARY FROM BOTH EMPLOYEE AND SALESMAN.
CODING:-
SQL> select salary from emp1 union all select salary from t3;
SALARY
10000
11000
12000
13000
14000
15000
16000
17000
18000
20000
25000
10000
12000
13000
14000
14000
16 rows selected.
YUGAL KISHOR SAHU Bsc 3rd year(cs)
QUERY 19:- LIST ALL STUDENT WHOSE ROLL NO BELONG THE RANGE BETWEEN
222TO444.
Coding:-
CODING:-
QUERY 21:-LIST ALL STUDENT WHOSE NAME ARE SHEETAL AND YUGAL.
CODING:-
SQL> select * from t4 where sname in('sheetal','yugal');
QUERY 22:- LIST ALL STUDENT WHOSE NAME START WITH LETTER ‘P’;
CODING:-
SQL> select * from t4 where sname like 'p%';
Query 23:-list all student whose name start with letter ‘j’ a s student of BA
with economy.
Coding:-
SQL> select * from t4 where sname like 'j%' and msub='economy';
QUERY 24:- LIST ALL STUDENT WHOSE NAME STARTED ‘P’ & HAVE A
GEOGRAPY
Coding:-
SQL> select * from t4 where sname like'p%' and msub='geograpy';
CODING:-
COUNT(RNO)
----------
2
YUGAL KISHOR SAHU Bsc 3rd year(cs)
Coding:-
SQL> select count(rno)from t4 group by msub;
COUNT(RNO)
----------
6 rows selected.
CODING:-
SQL> select*from t4 where class='BSc' and msub='maths';
CODING:-
SQL> select*from t4 where msub='physics' and feedue='11000';
CLASS .
CODING:-
SQL> select avg(percent) from t4;
AVG(PERCENT)
------------
74.5
YUGAL KISHOR SAHU Bsc 3rd year(cs)
Q30:- LIST ALL STUDENT WHOSE PERCENT IS MORE THAN AVERAGE PERCENT .
CODING:-
SQL> select * from t4 where percent>(select avg(percent) from t4);