Professional Documents
Culture Documents
YASHDBMS
YASHDBMS
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)
QUERY
1. Create a query to display emp no & emp name for all the emp earn
more than average salary.
5. Display emp no. & salary from that job based upto deptno.
6. Create a query to display dept no. emp no. of job work inemployee
10. List the client name who are locate in state CG.
12. Create the table in reference key to the emp relation toforeign key.
13. Add a colum called ‘telephone’ of datatype varchar2 and size 10 to the
client table
19. List all Student whose roll no belong the range between 222to444
21. List all student whose name are sheetal and yugal
22. list all student whose name start with letter ‘p’;
23. list all student whose name start with letter ‘j’ a s student of BA with
economy.
24. List all student whose name started ‘p’ & have a geograpy
28. List all the student whose main subject is physics and feedue is 11000.
30. List all students whose percent is more than average percent.
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’.
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 :-
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;
Example:-desc emp1;
Name Null? Type
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)
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:-
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
102 kumesh 24
103 bhupal 28
104 chandu 29
105 mohend 29
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.
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.
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)
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;
Earliest
---------
13-JAN-01
NORMALIZATION OF DATABASE
9340514609
3 Riya Punjab
Below student relation is not in 1NF because of multi-valued attribute stu_phone. It’s
decomposition into 1NF is given by “table2”.
4 Suresh Punjab
Rule 1- Be in 1NF
Rule 2- Single Column Primary Key
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
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
2 2 Sports 14.99
3 1 Gardening 10.00
4 3 Travel 12.99
5 2 Sports 17.99
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-
1 1 25.99
2 2 14.99
3 1 10.00
4 3 12.99
5 2 17.99
TABLE_GENRE-
1 Gardening
2 Sports
3 Travel
Example:- Let us assume there is a company where employees work in more than one
department.
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
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
21 Computer Dancing
21 Math Singing
34 Chemistry Dancing
74 Biology Cricket
59 Physics Hockey
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
A table is in 5th Normal Form only if it is in 4NF and it cannot be decomposed into any number
of smaller tables without loss of data.
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
P3:
Semester Lecturer
Sem1 Anshika
Sem1 John
Sem1 John
Sem2 Akash
Sem1 Praveen
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
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;
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
Table created.
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
7 rows selected.
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),balancenumber);
Table created.
SQL> insert into t2 values(1,'yugal','piproud','cg',10000);
1 row created.
1 row created.
SQL> insert into t2 values(6,'khoman','nawapara','delhi',23000);
1 row created.
1 row created.
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.
SQL> insert into t3 values(1,'rajeev','20-jan-1999',10000,'piproud','raiour');
1 row created.
SQL> insert into t3 values(2,'yugal','20-nov-1999',12000,'kurra','dhamtari');
1 row created.
SQL> insert into t3 values(3,'pravin','11-aug-1998',13000,'kumhari','raipur');
1 row created.
SQL> insert into t3 values(4,'neeraj','12-aug-1999',14000,'gobra','rajim');
1 row created.
1 row created.
SQL> select * from t3;
Q.4:- 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),feeduenumber,percent varchar(20));
Table created.
SQL> insert into t4 values(111,'sheetal','BSc','cs',8000,80);
1 row created.
SQL> insert into t4 values(222,'yugal','11th','commerce',9000,76);
1 row created.
SQL> insert into t4 values(333,'neeraj','BSc','maths',10000,70);
1 row created.
SQL> insert into t4 values(444,'rajeev’,’BSc’,’physics’,11000,60);
1 row created.
1 row created.
values(888,'jitendra','BA','economy',14000,77); 1 row
created.
7 rows selected
Query 1:-Create a query to display emp no & emp name for all theemp earn
more than average salary.
Coding:-
SQL> select empno,empname from cs where salary>(select avg(salary)from emp1);
EMPNO EMPNAME
---------- --------------------
321 rohit
Coding:-
SQL> select empname,job,salary from emp1 where empname='gopesh';
no rows selected
Query 5:- Display emp no. & salary from that job based upto deptno.
Coding :-
7 rows selected.
Query 6:-Create a query to display dept no. emp no. of job work inemployee.
121 1 lecturer
121 1 lecturer
102 2 techer
234 3 professor
123 4 clerk
345 5 principal
321 6 programmer
7 rows selected.
COUNT(EMPNO)
------------
Query8:-List the name of employee having ‘r’ as first letter their name.
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.
SQL> select * from t2;
8 rows selected.
Query 12:-Create the table in reference key to the emp relation to foreign
key.
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.
SQL> select * from company;
221 4sheetal
223 5 yugal
224 6 sonu
225 6 rekhu
Table created.
1 row created.
221 4sheetal
Table altered.
SNO NUMBER
SNAME CHAR(9)
ODDATE DATE
SALARY NUMBER
ADDRESS CHAR(9)
CITY CHAR(9)
1 row deleted.
Query 17:- Count the total no. of odd date from salesman.
Coding:-
SQL> select count(oddate)from t3;
COUNT(ODDATE)
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.
Query 19:- List all Student whose roll no belong the range between 222 to444.
Coding:-
SQL> select * from t4 where rno between '222' and '444';
Query20 List all student whose roll no are not in (999,666,444)- Coding:-
Query 21:-List all student whose name are sheetal and yugal.Coding:-
Query 22:- list all student whose name start with letter ‘j’;Coding:-
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';
COUNT(RNO)
----------
3
1
1
2
Query 26:-Count total no. of student in subject base.
Coding:-
SQL> select count(rno)from t4 group by msub;
COUNT(RNO)
----------
1
1
1
1
1
1
6 rows selected.
Query 28:- list all student of where main subject physics & feedue 11000
Coding:-
SQL> select*from t4 where msub='physics' and feedue='11000';
AVG(PERCENT)
------------
74.5
Q30:- list all student whose percent is more than average percent .
Coding:-
SQL> select * from t4 where percent>(select avg(percent) from t4);