Experiment 1
Performing Insertion, Deletion, Modifying, Altering, Updating and Viewing
records based on conditions
Aim:
To implement Data Definition Language (DDL) and Data Manipulation Language (DML)
using SQL Commands.
Concepts Involved:
Data Definition Language (DDL):
A DDL is a language used to define data structures within a database. It is typically
considered to be a subset of SQL, the Structured Query Language. A Data Definition Language has a
pre-defined syntax for describing data. For example, to build a new table using SQL syntax, the
CREATE command is used, followed by parameters for the table name and column definitions. The
DDL can also define the name of each column and the associated data type. Once a table is created, it
can be modified using the ALTER command. If the table is no longer needed, the DROP command
will delete the table.
So the commands used are
1. Create
2. Alter
3. Drop
4. Truncate.
Data Manipulation Language (DML):
A data manipulation language (DML) is a family of computer languages including
commands permitting users to manipulate data in a database. This manipulation involves inserting
data into database tables, retrieving existing data, deleting data from existing tables and modifying
existing data. DML is mostly incorporated in SQL databases.
The functional capability of DML is organized in manipulation commands like SELECT,
UPDATE, INSERT INTO and DELETE FROM, as described below:
SELECT: This command is used to retrieve rows from a table. The select syntax is SELECT
[column name(s)] from [table name] where [conditions]. Select is the most widely used DML
command in SQL.
UPDATE: This command modifies data of one or more records. An update command syntax
is UPDATE table name SET column name = value where [condition].
INSERT: This command adds one or more records to a database table. The insert command
syntax is INSERT INTO table name [column(s)] VALUES [value(s)].
DELETE: This command removes one or more records from a table according to specified
conditions. Delete command syntax is DELETE FROM table name where [condition].
Pre Lab Exercises:
DDL Commands:
1. Create Table
It is used to create a table
Syntax:
Create table tablename (column_name1 data_ type constraints,column_name2 data_
type constraints …)
SQL> create table depart (dno number (10),dname varchar2(10),primary key(dno));
Table created.
SQL>desc depart;
Name Null? Type
----------------------------------------- -------- ----------------------------
DNO NOT NULL NUMBER (10)
DNAME VARCHAR2 (10)
SQL> create table emp(eno number(10),ename varchar2(10),dno number(10),sal
number(10),jobid varchar2(10),mgrid varchar2(10),foreign key(dno) references
depart(dno));
Table created
SQL>descemp;
Name Null? Type
----------------------------------------- -------- ----------------------------
ENO NUMBER (10)
ENAME VARCHAR2 (10)
DNO NUMBER (10)
SAL NUMBER (10)
JOBID VARCHAR2 (10)
MGRID VARCHAR2 (10)
2. Alter Table
Alter Command is used to
o Add a new column,
o Modify the existing column definition,
o To include or drop integrity constraint.
Syntax: alter table tablename add/modify (attribute datatype(size));
ADD:
SQL> alter table empadd(primary key(eno),addr varchar2(10));
Table altered.
SQL>descemp;
Name Null? Type
----------------------------------------- -------- ----------------------------
ENO NOT NULL NUMBER (10)
ENAME VARCHAR2 (10)
DNO NUMBER (10)
SAL NUMBER (10)
JOBID VARCHAR2 (10)
MGRID VARCHAR2 (10)
ADDR VARCHAR2 (10)
SQL> alter table empadd(phno number(5));
Table altered.
SQL>descemp;
Name Null? Type
----------------------------------------- -------- ----------------------------
ENO NOT NULL NUMBER (10)
ENAME VARCHAR2 (10)
DNO NUMBER (10)
SAL NUMBER (10)
JOBID CHAR (20)
MGRID VARCHAR2 (10)
ADDR VARCHAR2 (10)
PHNO NUMBER (5)
3. Modify
SQL> alter table emp modify (jobid char);
Table altered.
SQL>descemp;
Name Null? Type
----------------------------------------- -------- ----------------------------
ENO NOT NULL NUMBER (10)
ENAME VARCHAR2 (10)
DNO NUMBER (10)
SAL NUMBER (10)
JOBID CHAR (20)
MGRID VARCHAR2 (10)
ADDR VARCHAR2 (10)
PHNO VARCHAR2 (10)
SQL> alter table emp modify (jobid char (20));
Table altered.
SQL>descemp;
Name Null? Type
----------------------------------------- -------- ----------------------------
ENO NOT NULL NUMBER (10)
ENAME VARCHAR2 (10)
DNO NUMBER (10)
SAL NUMBER (10)
JOBID CHAR (20)
MGRID VARCHAR2 (10)
ADDR VARCHAR2 (10)
PHNO VARCHAR2 (10)
SQL> alter table emp modify (jobid char (5));
alter table emp modify(jobid char(5))
ERROR at line 1:
ORA-01441: cannot decrease column length because some value is too big
4. Drop
SQL> alter table empdrop(phno);
Table altered.
SQL>descemp;
Name Null? Type
---------------------------------------- -------- ----------------------------
ENO NOT NULL NUMBER (10)
ENAME VARCHAR2 (10)
DNO NUMBER (10)
SAL NUMBER (10)
JOBID CHAR (20)
MGRID VARCHAR2 (10)
ADDR VARCHAR2 (10)
SQL> alter table emp drop (addr);
Table altered.
SQL>descemp;
Name Null? Type
----------------------------------------- -------- ----------------------------
ENO NOT NULL NUMBER (10)
ENAME VARCHAR2 (10)
DNO NUMBER (10)
SAL NUMBER (10)
JOBID CHAR (20)
MGRID VARCHAR2 (10)
DROP TABLE
It will delete the table structure provided the table should be empty.
SQL> drop table emp;
Table dropped.
SQL>descemp;
ERROR:
ORA-04043: object emp does not exist
DML Commands:
1. Insert Commands:
SQL > Create Table Cust(cname varchar2(15), cid number(5), caddr char(10), caccno
number(5), cacctype varchar2(10), cbalance float, Primary key(cid), unique(cname),
unique(caccno), check(cbalance>=1000));
SQL>desccust;
Name Null? Type
----------------------------------------- -------- ----------------------------
CNAME VARCHAR2 (15)
CID NOT NULL NUMBER (5)
CADDR CHAR (10)
CACCNO NUMBER (5)
CACCTYPE VARCHAR2 (10)
CBALANCE FLOAT (126)
SQL>insert into custvalues('Anitha',01,'Chennai',1001,'savings',15000);
1 row created.
SQL>insert into custvalues('Shriram',02,'Pondy',1002,'savings',25000);
1 row created.
SQL> insert into custvalues('Chamundi',03,'Salem',1003,'fd',36200);
1 row created.
SQL> insert into cust values ('&cname', &cid, '&caddr', &caccno, '&cacctype',
&cbalance);
Enter value for cname: Subha
Enter value for cid: 04
Enter value for caddr: Salem
Enter value for caccno: 1009
Enter value for cacctype: 5000
Enter value for cbalance: 5000
Old 1: insert into custvalues('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance)
New 1: insert into custvalues('Subha',04,'Salem',1009,'RD',5000)
1 row created.
SQL>insert into cust values ('&cname', &cid, '&caddr', &caccno, '&cacctype',
&cbalance);
Enter value for cname: Madhan
Enter value for cid: 4
Enter value for caddr: Salem
Enter value for caccno: 1004
Enter value for cacctype: checkings
Enter value for cbalance: 5000
old 1: insert into cust values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance)
new 1: insert into cust values('Madhan',4,'Salem',1004,'checkings',5000)
1 row created.
SQL> insert into cust values ('&cname', &cid, '&caddr', &caccno, '&cacctype',
&cbalance);
Enter value for cname: Subha
Enter value for cid: 5
Enter value for caddr: Trichy
Enter value for caccno: 1005
Enter value for cacctype: checkings
Enter value for cbalance: 10000
old 1: insert into cust values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance)
new 1: insert into cust values('Subha',5,'Trichy',1005,'checkings',10000)
1 row created.
SQL> insert into cust values ('&cname', &cid, '&caddr', &caccno, '&cacctype',
&cbalance);
Enter value for cname: Jayashree
Enter value for cid: 6
Enter value for caddr: Pondy
Enter value for caccno: 1006
Enter value for cacctype: fd
Enter value for cbalance: 15000
old 1: insert into cust values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance)
new 1: insert into cust values('Jayashree',6,'Pondy',1006,'fd',15000)
1 row created.
SQL>insert into cust values ('&cname', &cid, '&caddr', &caccno, '&cacctype',
&cbalance);
Enter value for cname: Sridharan
Enter value for cid: 7
Enter value for caddr: Kanchi
Enter value for caccno: 1007
Enter value for cacctype: fd
Enter value for cbalance: 22000
old 1: insert into cust values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance)
new 1: insert into cust values('Sridharan',7,'Kanchi',1007,'fd',22000)
1 row created.
2. Select Commands:
SQL>select * from cust;
CNAME CID CADDR CACCNO CACCTYPE CBALANCE
--------------- ---------- ---------- ---------- ---------- -----------------------------------
Anusha 1 Chennai 1001 savings 15000
Shriram 2 Pondy 1002 savings 25000
Chamundi 3 Salem 1003 fd 36200
Madhan 4 Salem 1004 checkings 5000
Subha 5 Trichy 1005 checkings 10000
Jayashree 6 Pondy 1006 fd 15000
Sridharan 7 Kanchi 1007 fd 22000
7 rows selected.
3. Update Commands:
SQL>update cust set caccno=1111 where cname='Chamundi';
1 row updated
SQL> select * from cust;
CNAME CID CADDR CACCNO CACCTYPE CBALANCE
--------------- ---------- ---------- ---------- ---------- --------------------------------------
Anusha 1 Chennai 1001 savings 15000
Shriram 2 Pondy 1002 savings 25000
Chamundi 3 Salem 1111 fd 36200
Madhan 4 Salem 1004 checkings 5000
Subha 5 Trichy 1005 checkings 10000
Jayashree 6 Pondy 1006 fd 15000
Sridharan 7 Kanchi 1007 fd 22000
7 rows selected.
4. Delete Commands:
SQL>delete from cust where cacctype='fd';
3 row deleted
SQL>select * from cust;
CNAME CID CADDR CACCNO CACCTYPE CBALANCE
--------------- ---------- ---------- ---------- ---------- -------------------------------------
Anusha 1 Chennai 1001 savings 15000
Shriram 2 Pondy 1002 savings 25000
Madhan 4 Salem 1004 checkings 5000
Subha 5 Trichy 1005 checkings 10000
4 rows selected.