Creating Table
create table student(
name varchar(20),
rollno int,
branch varchar(10)
);
Describe Table
describe student;
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(20)
ROLLNO NUMBER(38)
BRANCH VARCHAR2(10)
Viewing all Table
select * from tab;
This command list all the table and our created table will be visible at the bottom of the list.
Inserting into Table
insert into student values('&name', '&rollno', '&branch');
Enter value for name: Anurag
Enter value for rollno: 1
Enter value for branch: CS
old 1: insert into student values('&name', '&rollno', '&branch')
new 1: insert into student values('Anurag', '1', 'CS')
Use /
for the re-execution of the previous command.
Similarly, I have added some more student data.
Viewing Data
select * from student;
NAME ROLLNO BRANCH
-------------------- ---------- ----------
Anurag 1 CS
Abhijeet 2 IT
Anshuman 3 CS
Viewing specific field
select name, rollno from student;
NAME ROLLNO
-------------------- ----------
Anurag 1
Abhijeet 2
Anshuman 3
Conditional Query
select * from student where rollno>1;
NAME ROLLNO BRANCH
-------------------- ---------- ----------
Abhijeet 2 IT
Anshuman 3 CS
More Conditional Queries
select * from student where name='Abhijeet';
select * from student where branch='CS';
select name from student where branch='IT';
Sorting Records
select * from student order by rollno;
NAME ROLLNO BRANCH
-------------------- ---------- ----------
Anurag 1 CS
Abhijeet 2 IT
Anshuman 3 CS
Descending
select * from student order by rollno desc;
NAME ROLLNO BRANCH
-------------------- ---------- ----------
Anshuman 3 CS
Abhijeet 2 IT
Anurag 1 CS
Updating Table
Here is our table.
NAME ROLLNO BRANCH
-------------------- ---------- ----------
Anurag 1 CS
Abhijeet 2 IT
Anshuman 3 CS
Let's suppose I want to change the name of roll no 2 to Prince
update student set name='Prince' where rollno=2;
Now table data will be updated to
NAME ROLLNO BRANCH
-------------------- ---------- ----------
Anurag 1 CS
Prince 2 IT
Anshuman 3 CS
Alter Table
Altering a table means changing the old schema of the table to a new schema.
Here is our old table schema.
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(20)
ROLLNO NUMBER(38)
BRANCH VARCHAR2(10)
Add Column
In this example, I want to add a new column mobile
alter table student add(mobile number(10));
Now our table will look like this
NAME ROLLNO BRANCH MOBILE
-------------------- ---------- ---------- ----------
Anurag 1 CS
Prince 2 IT
Anshuman 3 CS
The mobile number of every student is now blank. We can update the mobile number of students using the update command.
Updating Column
alter table student modify(name varchar(50));
We have updated our name field from varchar(20) to varchar(50)
Deleting Column
alter table student drop column mobile;
Now, we have deleted the column mobile
and now our schema will look like the old schema.