Vous êtes sur la page 1sur 9

Assignment-1

1 Create the following table : STUDENT and display


structure
Column Name
RegNo
RollNo
Name
Address
PhoneNo
YearOfAdm
DeptCode
Year
BirthDate

Data Type
Varchar2
Number
Varchar2
Varchar2
Number
Number
Varchar2
Number
Date

Size
6
6
10
15
10
4
4
1

Constraints
Not null
Not null
Not null
Not null
Not null
Not null
Not null
Not null

create table student4161


(RegNo varchar2(6) NOT NULL,
RollNo number(6) NOT NULL,
Name varchar2(10) NOT NULL,
Address varchar2(15) NOT NULL,
PhoneNo number(10),
YearOfAdm number(4) NOT NULL,
DeptCode varchar2(4) NOT NULL,
Year number(1) NOT NULL,
BirthDate Date NOT NULL);
2

Insert the following data in the student table.

RegNo

RollNo

Name

Address

PhoneNo

YearOfAdm

012301

123001

Ashish

Jadavpur

012315

123015

Kamal

Kasba

012424

124024

Ipsita

Kaikhali

012250

122050

Anita

Hooghly

2476189
2
2442498
7
2573960
8
3671969
5

012344
012357

123044
123057

Biplab
Samik

Howrah
Barasat

2542674
2

Year

BirthDate

2003

DeptCod
e
CSE

01-Jun-81

2003

CSE

19-Sep-81

2004

CSE

15-Aug-82

2002

IT

22-Dec-80

2003
2003

IT
IT

3
3

03-Jan-82
15-Jul-81

012419

124019

Srija

Garia

2475565
5
2475330
6

012427

124027

Saibal

Garia

012236

122036

Santanu

012349

123049

Gita

DumDu
m
Kasba

2442868
2

2004

EE

25-Oct-82

2004

ECE

22-Mar-83

2002

ECE

11-Dec-80

2003

MCA

14-Apr-81

insert into student4161


values(012301,123001,'Ashish','Jadavpur',24761892,2003,'CSE',3,'01-Jun-81');
insert into student4161
values(012315,123015,'Kamal','Kasba',24424987,2003,'CSE',3,'19-Sep-81');
insert into student4161
values(012424,124024,'Ipsita','Kaikhali',25739608,2004,'CSE',2,'15-Aug-82');
insert into student4161
values(012250,122050,'Anita','Hooghly',36719695,2002,'IT',4,'22-Dec-80');
insert into student4161
values(012344,123044,'Biplab','Howrah','',2003,'IT',3,'03-Jan-82');
insert into student4161
values(012357,123057,'Samik','Barasat',25426742,2003,'IT',3,'15-Jul-81');
insert into student4161
values(012419,124019,'Srija','Garia',24755655,2004,'EE',2,'25-Oct-82');
insert into student4161
values(012427,124027,'Saibal','Garia',24753306,2004,'ECE',2,'22-Mar-83');
insert into student4161
values(012236,122036,'Santanu','DumDum','',2002,'ECE',4,'11-Dec-80');
insert into student4161
values(012349,123049,'Gita','Kasba',24428682,2003,'MCA',3,'14-Apr-81');

3. Display all records of students


select * from student4161;
REGNO
ROLLNO NAME
BIRTHDATE

ADDRESS

PHONENO YEAROFADM DEPT YEAR

------ ---------- ---------- --------------- ---------- ---------- -------------------------------------------------- --------12301


123001 Ashish
Jadavpur
24761892
01-JUN-81

2003

CSE

12315
123015
19-SEP-81

Kamal

Kasba

24424987

2003

CSE

12424
124024
15-AUG-82

Ipsita

Kaikhali

25739608

2004

CSE

12250
122050
22-DEC-80

Anita

Hooghly

36719695

2002

IT

12357
123057
15-JUL-81

Samik

Barasat

25426742

2003

IT

12419
124019
25-OCT-82

Srija

Garia

24755655

2004

EE

12427
124027
22-MAR-83

Saibal

Garia

24753306

2004

ECE

12349
123049
14-APR-81

Gita

Kasba

24428682

2003

MCA

12344
123044
03-JAN-82

Biplab

Howrah

23345678

2003

IT

12236
122036
11-DEC-80

Santanu

DumDum

2002

ECE

4. Display name, address and year of admission of each student


SQL> select Name,Address,YearOfAdm from student4161;
NAME
ADDRESS
YEAROFADM
---------- --------------- ---------Ashish
Jadavpur
2003
Kamal
Kasba
2003
Ipsita
Kaikhali
2004
Anita
Hooghly
2002
Samik
Barasat
2003

Srija
Garia
Saibal
Garia
Gita
Kasba
Biplab
Howrah
Santanu DumDum

2004
2004
2003
2003
2002

10 rows selected.

5. List the name and year of students who are in Computer Science.
SQL> select Name,Year from student41612
where DeptCode='CSE';
NAME
YEAR
---------- ---------Ashish
3
Kamal
3
Ipsita
2
6. List the names and departments of students belonging to 3rd year.
SQL> select Name,DeptCode from student4161
2 where Year=3;
NAME
DEPT
---------- ---Ashish
CSE
Kamal
CSE
Samik
IT
Gita
MCA
Biplab
IT
7. Display names of students with a as
the second letter in their names.

SQL> select Name from student4161


2 where Name like '_a%';
NAME
---------Kamal
Samik

Address LIKE 'H%' means the first character must be H,


but the rest of the string can be anything.
Perry% matches any string beginning with Perry.

%idge% matches any string containing idge as


a substring, for example, Perryridge, Rock Ridge,
Mianus Bridge, and Ridgeway. Address LIKE
'%Glasgow%' means a sequence of characters of any
length containing Glasgow.
_ _ _ matches any string of exactly three characters.

Address LIKE 'H_ _ _ ' means that there must be


exactly four characters in the string, the first of which
must be an H.
_ _ _% matches any string of at least three characters.

Address LIKE '%e' means any sequence of characters,


of length at least 1, with the last character an e.
Address NOT LIKE 'H%' means the first character cannot
be an H.

Saibal
Santanu
For patterns to include the special pattern characters (that is, % and), SQL allows the
specification of an escape character. The escape character is used immediately
before a special pattern character to indicate that the special pattern character is to
be treated like a normal character.

Example1
To check for the string '15%', we can use the predicate:
LIKE '15#%' ESCAPE '#' matches the string 15%
Example2
like ab#%cd% escape \ matches all strings beginning with ab%cd.
Example3
like ab\\cd% escape \ matches all strings beginning with ab\cd.

8. Display names of students in alphabetical order.


SQL> select Name from student4161
2 Order by Name asc;
NAME
---------Anita
Ashish
Biplab
Gita
Ipsita
Kamal
Saibal
Samik
Santanu
Srija
10 rows selected.
9. Display names and addresses of students who took
admission in the year 2004.

SQL> select Name,Address from student4161


where YearOfAdm=2004;
NAME
ADDRESS
---------- --------------Ipsita
Kaikhali
Srija
Garia
Saibal
Garia

10. List the names of students who do not have a phone number.
SQL> select Name from student41612
where PhoneNo is NULL;
NAME
---------Biplab
Santanu

Delete operations.
a) remove all rows
delete from <tablename>;
b) removal of a specified row/s
delete from <tablename> where <condition>;

Assignment-2

1) Delete the name of a student whose roll no, year and department code is given
delete from student4161
where RollNo=122050 AND Year=4 AND DeptCode='IT';
2) Display the number of students in each department.
SQL> select DeptCode, count(*) from student41612 group by DeptCode;
DEPT COUNT(*)
---- ---------CSE
3
ECE
2
EE
1
IT
2
MCA
1

SELECT COUNT (DISTINCT SALARY)


FROM EMPLOYEE;
SELECT sno, COUNT(salary)
FROM staff;

SELECT COUNT(*) AS count


FROM property_for_rent
WHERE rent > 350;
SELECT COUNT(DISTINCT pno) AS count
FROM viewing
WHERE date BETWEEN 'l-May-98' AND '31-May-98';

select branch-name, count (distinct customer-name)


from depositor, account
where depositor.account-number = account.accountnumber
group by branch-name

Updating the contents of a


table.
a) updating all rows

select count (*)


from customer

Syntax:
Update <tablename> set
<col>=<exp>,<col>=<exp>;

SELECT COUNT (*)


FROM EMPLOYEE,DEPARTMENT
WHERE DNO=DNUMBER AND DNAME='Research';

b) updating seleted records.

3) Change the address of a student whose roll no and name is given.


Update student4161
set Address='Birati' where RollNo=124027;

update account
set balance = balance * 1.05

Syntax:
Update <tablename> set
<col>=<exp>,<col>=<exp>
where <condition>;

case
when pred1 then result1
when pred2 then result2
...
when predn then resultn
else result0
end

update account
set balance = balance * 1.05
where balance >= 1000
update account
set balance = case
when balance <= 10000 then balance *
1.05
else balance * 1.06
end

UPDATE
SET
WHERE
PROJECT
PLOCATION = 'Bellaire', DNUM = 5
PNUMBER=10;

UPDATE staff
SET position = 'Manager', salary =
18000
WHERE sno = 'SGI4';

Modifying the structure of tables.

4) Add the college phone number (25739607) to each of these students.


a)add new columns

SQL> Alter table student4161


2 add ColPhone number(10);

Alter table <tablename> add(<new col><datatype(size),<new


col>datatype(size));
alter table emp add(sal number(7,2));
b) Dropping a column from a table.
Alter table <tablename> drop column <col>;
alter table emp drop column sal;

Table altered.
SQL> Update student4161
2 set ColPhone=25739607;
9 rows updated.
2.

5) Change the size of column Name to 15 characters.

Alter table student4161


modify Name varchar2(15);

6) Add a column MarksObtained (number) to the student table.

7) Insert values against marks column.


update student4161
set Marks=99 where DeptCode='CSE';
update student4161
set Marks=80 where DeptCode='IT';
update student4161
set Marks=85 where DeptCode='ECE';
update student4161
set Marks=70 where DeptCode='EE';
update student4161
set Marks=75 where DeptCode='MCA';
SQL> Select Name,Marks from student4161;
NAME
MARKS
--------------- ---------Ashish
99
Kamal
99
Ipsita
99
Biplab
80

c) Modifying existing columns.


Alter table <tablename>
modify(<col><newdatatype>(<newsize>));
alter table emp modify(ename varchar2(15));

Samik
Srija
Saibal
Santanu
Gita

80
70
85
85
75

9 rows selected.
8) Drop column MarksObtained from table student.
Alter table student4161
drop(Marks);

9) Add constraint primary key to the column RegNo of table student.


Alter table student4161
add primary key (RegNo);
10) Add check constraints to the column year of student table. (year should be entered within 1,2,3,4)
SQL> Alter table student4161
2 add check(Year>=1 and Year<=4);
Table altered.
11) Add check constraints against BirthDate column where the birthdate should be within 1st june1980 and
31st may 1983.

Alter table student4161


add check(BirthDate BETWEEN date '1980-06-01' AND date '1983-05-01');

Vous aimerez peut-être aussi