Dbms Experi
Dbms Experi
03
AIM: To execute the DATA DEFINITION LANGUAGE (DDL) commands.
Data Definition Language (DDL) is a computer language for defining data structures. The term
was first introduced in relation to the Codasyl database model, where the schema of the database
was written in a Data Definition Language describing the records, fields, and "sets" making up
the user Data Model.
The DDL part of SQL permits database tables to be created or deleted. It also defines indexes
(keys), specifies links between tables, and imposes constraints between tables. The most
important DDL statements in SQL are:
CREATING A TABLE
Syntax:
Create table <table name> (column 1 datatype 1,., column n datatype n);
Explanation:
Create command is used to create a table in the database.
Example:
SQL> create table student (VT number, name varchar (10));
OUTPUT:
Table created.
DESC COMMAND
Syntax:
desc <table name>;
Explanation:
This command is used to view the structure of the table.
Example:
SQL> desc student;
OUTPUT:
NAME NULL? TYPE
VT NUMBER
NAME VARCHAR (10)
ALTERING A TABLE
Explanation:
The structure of the table can be changed using this command like add new column, change the
width of a datatype, change the datatype of a column.
1. MODIFY
SYNTAX:
alter table <table name> modify (column datatype, );
EXAMPLE:
SQL> alter table student modify (name varchar (20));
OUTPUT:
Table altered.
2. ADD / DROP
SYNTAX:
alter table <table name> add/drop (column datatype, );
EXAMPLE:
SQL> alter table student add (dept varchar (20));
SQL> alter table student drop (addr);
OUTPUT:
Table altered.
TRUNCATING A TABLE
EXAMPLE:
SQL> truncate table student;
OUTPUT:
Table truncated.
DROPPING A TABLE
SYNTAX:
drop table <table name>;
EXPLANATION:
This command is used to remove a table from the database.
EXAMPLE:
SQL> drop table student;
OUTPUT:
Table dropped.
RESULT:
Thus, the data definition language commands were executed and their outputs were verified.
Experiment No .04
AIM: To execute the DATA MANIPULATION LANGUAGE (DML) commands.
The Data Manipulation Language (DML) is used to retrieve, insert and modify database
information. These commands will be used by all database users during the routine operation of
the database. Let's take a brief look at the basic DML commands:
The query and update commands form the DML part of SQL:
INSERT COMMANDS
SYNTAX:
insert into <table name> values (value1, value2,, value n);
EXPLANATION:
This insert command is used to add rows to a table in the database.
EXAMPLE:
SQL> insert into student values (4169, rameeze);
OUTPUT:
1 row created.
SYNTAX:
insert into <table name> values (&value1, &value2,, &value n);
EXPLANATION:
This insert command is used to add multiple rows to a table in the database.
EXAMPLE:
SQL> insert into student values (&vt, &name);
OUTPUT:
Enter value for vt: 4169
Enter value for name: rameeze
Old 1: insert into student values (&vt, &name)
New 1: insert into student values (4169, rameeze)
1 row created.
SELECT COMMANDS
SYNTAX:
select * from <table name>;
EXPLANATION:
This select command is used to retrieve all the values stored in the table.
EXAMPLE:
SQL> select * from student;
OUTPUT: VT NAME
SYNTAX:
select <field name> from <table name>;
EXPLANATION:
This select command is used to retrieve the particular field values stored in the table.
EXAMPLE:
SQL> select name from student;
OUTPUT: NAME RAMEEZE
SYNTAX:
select <field name> from <table name> where <search condition>;
EXPLANATION:
This select command is used to retrieve the particular field values, stored in the table, which
satisfy a required condition.
EXAMPLE:
SQL> select name from student where vt=4169;
OUTPUT: NAME RAMEEZE
UPDATE COMMAND
SYNTAX:
update <table name> set column1=expression, column 2=expression, ,
column n=expression where <search condition>;
EXPLANATION:
This command is used to update (changing values in) one or two columns of a row in a table.
Specific rows can be updated based on some condition.
EXAMPLE:
SQL> update student set name=md rameeze where vt =4169;
OUTPUT: 1 row updated.
SQL> select * from student;
VT NAME 4169 MD RAMEEZE
DELETE COMMAND
SYNTAX:
delete from <table name> where <search condition>;
EXPLANATION:
This command is used to delete a row from a table.
EXAMPLE:
SQL> delete from student where vt =4169;
OUTPUT: 1 row deleted.
Experiment No .05
AIM: Execution of SQL Queries, Aggregate Functions and Nested Queries.
1. SELECT
Example: select name from cust;
Output: NAME hayes jones blake
2. WHERE
Example: select city from cust where name= hayes;
Output: CITY Harrison
3. FROM
Example: select name, city from cust;
Output:
NAME CITY
hayes harrison
jones redwood
blake downtown
4. RENAME
Example: select borrower.loan_no as loan_id from borrower;
Output: LOAN_ID
5. STRING OPERATIONS
(a) % .
Example: select name from cust where city like har%;
Output:
NAME
hayes
smith
(b) _ .
6. ORDERING OF TUPLES
Example: select * from loan order by amount asc;
Output: LOAN NO BRANCH AMOUNT
7. UNION
Example: (select name from depositor) union (select name from borrower);
Output:
NAME
adams
britto
hayes
jones
smith
8. INTERSECTION
Example: (select name from depositor) intersect (select name from borrower);
Output: NAME
hayes
jones
9. AGGREGATE FUNCTIONS
(a) AVERAGE.
Example: select avg (balance) from account;
Output: AVG (BALANCE)
487.5
(b) MINIMUM.
Example: select min (balance) from account;
Output: MIN (BALANCE)
350
(c) MAXIMUM.
Example: select max (balance) from account;
Output: MAX (BALANCE)
700
(d) TOTAL.
Example: select total (balance) from account;
Output: SUM (BALANCE)
1950
(e) COUNT.
Example: select count (balance) from account;
Output: COUNT (BALANCE)
4
Example: select name from depositor where depositor.name in (select name from
cust);
Output: NAME
hayes
jones
RESULT:
Thus, the queries in SQL were executed and the outputs were verified.