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

Dbms Week-1, 2

Uploaded by

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

Dbms Week-1, 2

Uploaded by

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

WEEK - 1

Aim: Demonstrating Data Definition Commands, Data Manipulation Commands for


inserting and deleting of data from Tables
Description:
Table: Tables are database objects that contain all the data in a database objects
that contain all the data in a database.
 In tables, data is logically organized in a row and column format similar to a
spreadsheet.
 Each row represents a unique record, and each column represents a field in
the record.
Data Definition Language(DDL):
DDL is used to store the information of metadata like the number of tables and
schemas, their names, indexes, columns in each table,etc.
Data Manipulation Language(DML):
DML is used for accessing and manipulating data in a database.
Data Definition Commands:(used in this question)
DDL statement for creating a table-

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

Creating a table from a table-

Syntax-
CREATE TABLE TABLENAME(columnname data_type, columnname data_type,
………);
Example:
CREATE TABLE STUDENT(NAME VARCHAR2(17),ROLLNO VARCHAR2(7));

Insertion operation:
The insert command is used for inserting one or more rows into a database table
with specified table column values. The first DML command executed immediately
after a table creation is the insert statement.

Insertion of data into tables-

Syntax-
INSERT INTO tablename [(columnname, columnname, ………)] values(expression,
expression);
Example:
INSERT INTO STUDENT[(NAME VARCHAR2(17),ROLLNO VARCHAR2(7))]
values(“Arjun”,”21131a1262”);

Inserting data into a table from another table:


Syntax-
INSERT INTO tablename SELECT columnname, columnname, ……. FROM
tablename;
Example:
INSERT INTO STUDENT SELECT NAME FROM STUDENT;

Insertion of selected data into a table from another table:

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

Example:

INSERT INTO STUDENT SELECT NAME,ROLLNO FROM EMPLOYEE WHERE


NAME= “priya”;

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 ;

Example:

delete from student;

Deletion of specified number of rows

Syntax:

delete from tablename where search condition;

Example:

delete from student where NAME = “Arjun” ;


Create table employee:

SQL> create table employee(EMPNO NUMBER(3), EMPNAME VARCHAR2(8),


JOININGDATE DATE, SALARY NUMBER(8,2), CITY VARCHAR2(15));
Insert values into table employee:

SQL> insert into employee values(101,'KEYUR','01-05-02',12000,'RAJKOT');

SQL> insert into employee values(102,'HARDIK','02-12-04',14000,'AHMEDABAD');

SQL> insert into employee values(103,'KAJAL','03-14-06',15000,'BARODA');

SQL> insert into employee values(104,'BHOOMI','06-23-05',12500,'AHMEDABAD');

SQL> insert into employee values(105,'HARDIK','02-15-04',14000,'RAJKOT');

SQL> desc employee;


SQL>select *from employee;

Create table student:

SQL> create table student(ENROLLMENTNO VARCHAR2(10) PRIMARY KEY,


NAME VARCHAR2(25), BIRTHDATE DATE,CPI NUMBER(6,2));
Insert values into table student:
SQL> insert into student values(1105301106,'VIPUL','01-10-90',7.5);
SQL> insert into student values(1105301108,'MANAN','12-22-92',7);
SQL> insert into student values(1105301123,'KARTIK','09-03-89',5.25);
SQL> insert into student values(1105301145,'VIKRAM','05-06-94',6);
SQL> insert into student values(1105301189,'RAHUL','12-07-93',4.5);
SQL> desc student;
SQL> select *from student;
COMMANDS:

CREATE TABLE deposit:

SQL> create table deposit(ACTNO VARCHAR2(5),CNAME VARCHAR2(18),BNAME


VARCHAR2(18),AMOUNT NUMBER(8,2) ,ADATE DATE);

INSERT VALUES INTO TABLE deposit:

SQL> insert into deposit values('100','ANIL','VRCE',1000,'03-01-95');

SQL> insert into deposit values('101','SUNIL','AJNI',5000,'01-04-96');

SQL> insert into deposit values('102','MEHUL','KAROLBAGH',3500,'11-17-95');

SQL> insert into deposit values(‘104’,’MADHURI’,’CHANDI’,1200,’12-17-95’);

SQL> insert into deposit values ('105','PRMOD','M.G.ROAD',3000,'03-27-96');

SQL> insert into deposit values ('106','SANDIP','ANDHERI',2000,'03-31-96');

SQL> insert into deposit values ('107','SHIVANI','VIRAR',1000,'09-05-95');

SQL> insert into deposit values ('108','KRANTI','NEHRU PLACE',5000,'07-02-95');

SQL> insert into deposit values ('109','MINU','POWAI',7000,'08-10-95');

Describe deposit:

desc deposit;
O/P:
List all data from table deposit:

select * from deposit;

Inserting data into a table from another table:

insert into deposit select ACTNO,CNAME,BNAME,AMOUNT,ADATE from deposit;

Insertion of selected data into a table from another table:

insert into deposit select ACTNO,CNAME,BNAME,AMOUNT,ADATE from deposit


where CNAME='ANIL';

Deletion of specified number of rows:

delete from deposit where CNAME=’ANIL’;

Deletion of all rows:

delete from deposit;

truncate table deposit;

Deletion of Table:

drop table student;


9 row(s) inserted.

2 row(s) inserted.

1 row(s) deleted.

8 row(s) deleted.

Table truncated.

Table dropped.
WEEK-2 Date:

Aim: Data Manipulation Commands for updating and retrieving of data from Tables
and Transaction Control statements.
Description:
Data Manipulation Language(DML):
DML ( Data Manipulation Language) Data manipulation is

 The retrieval of information stored in the database.


 The insertion of new information into the database.
 The deletion of information from the database.
 The modification of information stored by the appropriate data
model. There are basically two types.
 Procedural DML:- require a user to specify what data
are needed and how to get those data.
 Non Procedural DML : require a user to specify what
data are needed without specifying how to get those
data.

Updating the content of a table:


In creation situation we may wish to change a value in table without changing all
values in the tuple . For this purpose the update statement can be used.

Syntax:
update table name
set columnname = expression, columnname =expression…… 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;

Selecting a data set from table data-


Syntax-
SELECT columnname, columnname FROM tablename WHERE searchcondition;

Altering the contents of database:

The ALTER TABLE statement is used to add, delete, or modify columns in an existing
table.The ALTER TABLE statement is also used to add and drop various constraints
on an existing table.
ALTER TABLE - ADD Column

It is used to add a column in a table.

Syntax: ALTER TABLE tablename ADD columnname datatype;

ALTER TABLE - DROP Column

It is used to delete a column in a table.

Syntax: ALTER TABLE tablename DROP COLUMN columnname;

ALTER TABLE - Rename Column

It is used to rename a column in a table.

Syntax: ALTER TABLE tablename RENAME COLUMN oldname to newname;

ALTER TABLE - Modify Column

It is used to change the data type of a column in a table.

Syntax: ALTER TABLE tablename MODIFY columnname datatype;


COMMANDS:

Give account no and amount of depositors.

SELECT ACTNO,AMOUNT FROM deposit;

SELECT CNAME FROM deposit WHERE AMOUNT >4000;

Add two more column CITY VARCHAR2(20) and PINCODE NUMBER(6) to


student table :
ALTER TABLE student ADD (CITY VARCHAR2(20),PINCODE NUMBER(6));

Change the data type of PINCODE column to varchar2(6).


ALTER TABLE student MODIFY (PINCODE VARCHAR2(6));

Rename column PINCODE to PCODE in student table:

ALTER TABLE student RENAME COLUMN PINCODE TO PCODE;


Delete column PINCODE from the student table:
ALTER TABLE student DROP COLUMN PINCODE;

Change name of table student to STUDENT_MASTER.


RENAME student TO STUDENT_MASTER;

Delete the table student:


DROP TABLE student;

Assign 5.5 as CPI to STUDENT_MASTER with EnrollmentNo equal to


1105301189.
UPDATE STUDENT_MASTER SET CPI = 5.5 WHERE ENROLLMENTNO = 1105301189;
Update city of all employee having Ahmedabad to Surat.
UPDATE employee SET CITY = ‘SURAT’ WHERE CITY = ‘AHMEDABAD’;

Give 10% increment in salary to each employee.


UPDATE employee SET SALARY =SALARY*0.1+SALARY;

Update city of EmpNo 1 to NULL.


UPDATE employee SET CITY=NULL WHERE EMPNO=1;

Find out the employee who does not belongs to ‘Baroda’ city.
SELECT * FROM employee WHERE CITY != ‘BARODA’;

List all record of student table.


SELECT * FROM student;
Display all record of student with CPI more than 6.
SELECT * FROM student WHERE CPI > 6;

Display EnrollmentNo and Name of student table.


SELECT ENROLLMENTNO,NAME FROM student;

Display Name and CPI of student with CPI more than 5.


SELECT NAME,CPI FROM student WHERE CPI > 5;

Delete the records of student table with CPI less than 6.


DELETE FROM student WHERE CPI < 6;

Delete all records from student table.


TRUNCATE TABLE student;

Delete all the records of employee table having salary less than or equal to
14000.
DELETE FROM employee WHERE SALARY <= 14000;

You might also like