0% found this document useful (0 votes)
17 views

DBMS Lab File-1

The document provides examples of SQL commands for creating tables, inserting and retrieving data from tables, modifying table structures by adding or dropping columns, applying integrity constraints, performing joins, grouping data, using subqueries, creating indexes and views, and basics of PL/SQL programming including conditional control and GOTO statements.

Uploaded by

bacos64547
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

DBMS Lab File-1

The document provides examples of SQL commands for creating tables, inserting and retrieving data from tables, modifying table structures by adding or dropping columns, applying integrity constraints, performing joins, grouping data, using subqueries, creating indexes and views, and basics of PL/SQL programming including conditional control and GOTO statements.

Uploaded by

bacos64547
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Practical #01

Objective:-
Create tables and specify the Questionnaires in SQL.

Creating a table-

Syntax-
Create table tablename
(columnname datatype(size), columnname datatype(size));

Creating a table from a table-

Syntax-
CREATE TABLE TABLENAME
[(columnname, columnname, ………)]
AS SELECT columnname, columnname ......... FROM tablename;

OUTPUT:-

1
Practical #02
Objective:
Performing insertion, deletion, modification, altering and updating operations on the records
based on conditions.

Insertion of data into tables-


Syntax-
INSERT INTO tablename
[(columnname, columnname, ………)] Values(expression, expression);

Inserting data into a table from another table:


Syntax-
INSERT INTO tablename
SELECT columnname, columnname,
……. FROM tablename;

Insertion of selected data into a table from another table:


Syntax-
INSERT INTO tablename
SELECT columnname, columnname……..
FROM tablename
WHERE columnname= expression;

Retrieving of data from the tables-


Syntax-
SELECT * FROM tablename;

The retrieving of specific columns from a table-


Syntax-
SELECT columnname, columnname,
…. FROM tablename;

Elimination of duplicates from the select statement-


Syntax-
SELECT DISTINCT columnname, columnname FROM tablename;

2
Selecting a data set from table data-
Syntax-
SELECT columnname, columnname FROM tablename WHERE searchcondition;

OUTPUT:-

3
Practical #03

Objective:
To Manipulate the Operations on the table.

Updating the content of a table:


Update table name
Set columnname = expression, columnname
=expression…… Where columnname = expression;

Deletion Operation:-
A delete query is expressed in much the same way as Query. We can delete whole tuple
(rows) we can delete values on only particulars attributes.
Deletion of all rows
Syntax:
Delete from tablename :
Deletion of specified number of rows:

Delete from table name Where search condition ;


Computation in expression lists used to select data

+ Addition - Subtraction
* multiplication ** exponentiation
/ Division () Enclosed operation

Renaming columns used with Expression Lists: -

Syntax:
Select column name
result_columnname, Columnname
result_columnname, From table name;

Oracle functions:

Syntax:
Avg ([distinct/all]n)
Min return minimum value of expr.

Syntax:
MIN((distinct/all )expr)
Count Returns the no of rows where expr is not null

4
Syntax:
Count ([distinct/all)expr]
Count (*) Returns the no rows in the table, including duplicates and those with nulls.
Max Return max value of expression.

Syntax:

Max ([distinct/all]expr) Sum Returns sum of values of n

Syntax:
Sum ([distinct/all]n)
Sorting of data in table

Syntax:
Select columnname,
columnname From
table
Order by columnname;

OUTPUT:-

5
Practical #04

Objective:-
To Implement the structure of the table Modifying the Structure of Tables.

Adding new columns:

Syntax:
ALTER TABLE tablename
ADD (newcolumnname newdatatype (size));

Modifying existing table

Syntax:
ALTER TABLE tablename:
MODIFY (newcolumnname newdatatype (size));

Removing/Deleting Tables
Following command is used for removing or deleting a table.

Syntax:
DROP TABLE tablename:

Defining Integrity constraints in the ALTER TABLE command-

Add PRIMARY KEY-

Syntax:
ALTER TABLE tablename
ADD PRIMARY KEY (columnname);

Add FOREIGN KEY-


Syntax:
ALTER TABLE tablename
ADD CONSTRAINT constraintname
FOREIGN KEY(columnname) REFERENCES tablename;
Dropping integrity constraints in the ALTER TABLE command:

6
DROP the PRIMARY KEY-
Syntax:
ALTER TABLE tablename DROP PRIMARY KEY

DROP FOREIGN KEY-


Syntax:
ALTER TABLE tablename
DROP CONSTRAINT constraintname;

OUTPUT:-

7
Practical #05
Objective:-
To Implement the restrictions on the table.

Null Value Concepts:-

Syntax:
Create table tablename
(columnname data type (size) not null ……)

Primary Key:

Syntax:
primary key as a column constraint Create table tablename
(columnname datatype (size) primary key,….)
Primary key as a table constraint
Create table tablename
(columnname datatype (size), columnname datatype(
size)… Primary key (columnname,columnname));

Default value concept:

Syntax:
Create table tablename
(columnname datatype (size) default value,….);

Foreign Key Concept :

Syntax :
Create table table name
(columnname datatype (size) references another table name);

Foreign key as a table constraint:

Syntax :
Create table name
(columnname datatype
(size)…. primary key (columnname);
foreign key (columnname)references table name);

8
Check Integrity Constraints:

Syntax:
Create table tablename
(columnname datatype (size) CONSTRAINT
constraintname) Check (expression) );

OUTPUT:-

9
Practical #06
Objective:-
To implement the concept of Joins

1.Cartesian product:-
Consider two table student and course Select B.*,P.* FROM student B, course P;

2.INNER JOIN:-
Cartesian product followed by selection Select B.*,P.* FROM student B, Course P WHERE
B.course # P.course # ;

3.LEFT OUTER JOIN:


LEFT OUTER JOIN = Cartesian product + selection but include rows from the left table
which are unmatched pat nulls in the values of attributes belonging to the second table Exam:
Select B.*,P* FROM student B left join course p ON B.course # P.course #;

4.RIGHT OUTER JOIN:


RIGHT OUTER JOIN = Cartesian product + selection but include rows from right table
which are unmatched Exam: Select B.*,P.* From student B RIGHT JOIN course P B.course#
= P course # ;

5.FULL OUTER JOIN Exam:


Select B.*,P.* From student B FULL JOIN course P On B.course # = P course # ;

OUTPUT:-

10
Practical #07

Objective:- To implement the concept of grouping of Data.

Grouping Data From Tables:

Syntax:
SELECT columnname, columnname FROM tablename
GROUP BY columnname;

At times it is useful to state a condition that applies to groups rather than to tuples.
For example, we might be interested in only those branches where the average account
balance is more than 1200. This condition does not apply to a single tuple, rather it applies
to each group constructed by the GROUP BY clause. To express such Questionary, we use
the having clause of SQL. SQL applies predicates in the having may be used.

Syntax:
SELECT columnname, columnname
FROM tablename
GROUP BY columnname; HAVING searchcondition;

OUTPUT:-

11
Practical #08

Objective:- To implement the concept of SubQueries.

SubQueries:-

Example: -
Creating clientmaster table from oldclient_master, table Create table client_master
AS SELECT * FROM oldclient_master;

Output:
Records only in Query one + records only in Query two + A single set of records with is
common in the both Queries.

Syntax:
SELECT columnname, columname FROM tablename 1
UNION
SELECT columnname, columnname From tablename2;

Intersect Clause:

Output =A single set of records which are common in both Queries Syntax:
SELECT columnname, columnname FROM tablename 1
INTERSECT
SELECT columnname, columnname FROM tablename
2;

MINUS CLAUSE:-
SELECT columnname, columnname FROM tablename ;
MINUS
SELECT columnname, columnname FROM tablename ;
OUTPUT:-

12
Practical #09
Objective:-
To implement the concept of Indexes and views.

Creating an Index for a table:-

Syntax:
(Simple)
CREATE INDEX index_name ON tablename(colu mn name);
Composite Index:-
CREATE INDEX index_name
ON tablename(columnname,columnname);
Creating an UniQuestion Index:-
CREATE UNIQUESTION INDEX
indexfilename ON tablename(columnname);

Creation of Views:-

Syntax:-
CREATE VIEW viewname AS SELECT columnname,columnname FROM tablename
WHERE columnname=expression_list;

OUTPUT:-

13
Practical # 10

Objective:-
To implement the basics of PL/SQL.

SET SERVER OUTPUT ON:

Example:

Write the following code in the PL/SQL block to display message to


user DBMS_OUTPUT.PUT_LINE(„Display user message‟);

Conditional control in PL/SQL-

Syntax:
IF <condition> THEN
<Action> ELSEIF<condition>
<Action> ELSE
<Action> ENDIF;

The GOTO statement:


The goto statement allows you to change the flow of control within a PL/SQL Block.

14
OUTPUT:-

19 21 8 b is maximum 21

15

You might also like