class(stunum: number,stuname:varchar2,stubranch:varchar2,stumarks)
Apply the following constraints to the class table while creating the table.
· stunum is unique – use UNIQUE constraint
· stuname should not be a null value—use NOT NULL constraint
· stubranch should contain only cse,ece,eee,civil,mechanical(i.e, the column should not allow other than these branches)-use CHECK consrtaint
· stumarks should not exceed 100—use CHECK constraint
1. SQL> create table class(stunum number(10) unique,
2 stuname varchar2(15) not null,
3 stubranch varchar2(10) check ( stubranch in('cse','ece','eee','civil','mechanical')),
4 stumarks number(3) check(stumarks < 100));
Table created.
2. SQL>desc class;
Name Null? Type
----------------------------------------- -------- -------------------------------------------
STUNUM NUMBER(10)
STUNAME NOT NULL VARCHAR2(15)
STUBRANCH VARCHAR2(10)
STUMARKS NUMBER(3)
3. SQL> insert into class values(&stunum,'&stuname','&stubranch',&stumarks);
Enter value for stunum: 1
Enter value for stuname: abhi
Enter value for stubranch: cse
Enter value for stumarks: 90
old 1: insert into class values(&stunum,'&stuname','&stubranch',&stumarks)
new 1: insert into class values(1,'abhi','cse',90)
1 row created.
SQL> /
Enter value for stunum: 2
Enter value for stuname: balu
Enter value for stubranch: ece
Enter value for stumarks: 90
old 1: insert into class values(&stunum,'&stuname','&stubranch',&stumarks)
new 1: insert into class values(2,'balu','ece',90)
1 row created.
SQL> /
Enter value for stunum: 3
Enter value for stuname: chandra
Enter value for stubranch: cse
Enter value for stumarks: 85
old 1: insert into class values(&stunum,'&stuname','&stubranch',&stumarks)
new 1: insert into class values(3,'chandra','cse',85)
SQL> /
Enter value for stunum: 4
Enter value for stuname: deva
Enter value for stubranch: cse
Enter value for stumarks: 90
old 1: insert into class values(&stunum,'&stuname','&stubranch',&stumarks)
new 1: insert into class values(4,'deva','cse',90)
1 row created.
SQL> /
Enter value for stunum: 5
Enter value for stuname: eswar
Enter value for stubranch: eee
Enter value for stumarks: 85
old 1: insert into class values(&stunum,'&stuname','&stubranch',&stumarks)
new 1: insert into class values(5,'eswar','eee',85)
1 row created.
SQL> /
Enter value for stunum: 6
Enter value for stuname: naresh
Enter value for stubranch: ece
Enter value for stumarks: 80
old 1: insert into class values(&stunum,'&stuname','&stubranch',&stumarks)
new 1: insert into class values(6,'naresh','ece',80)
1 row created.
SQL> /
Enter value for stunum: 7
Enter value for stuname: ganesh
Enter value for stubranch: mechanical
Enter value for stumarks: 80
old 1: insert into class values(&stunum,'&stuname','&stubranch',&stumarks)
new 1: insert into class values(7,'ganesh','mechanical',80)
1 row created.
SQL> /
Enter value for stunum: 8
Enter value for stuname: mahesh
Enter value for stubranch: civil
Enter value for stumarks: 80
old 1: insert into class values(&stunum,'&stuname','&stubranch',&stumarks)
new 1: insert into class values(8,'mahesh','civil',80)
1 row created.
SQL> /
Enter value for stunum: 9
Enter value for stuname: jagadesh
Enter value for stubranch: civil
Enter value for stumarks: 80
old 1: insert into class values(&stunum,'&stuname','&stubranch',&stumarks)
new 1: insert into class values(9,'jagadesh','civil',80)
1 row created.
SQL> /
Enter value for stunum: 10
Enter value for stuname: karthik
Enter value for stubranch: cse
Enter value for stumarks: 85
old 1: insert into class values(&stunum,'&stuname','&stubranch',&stumarks)
new 1: insert into class values(10,'karthik','cse',85)
1 row created.
4. SQL> select *
2 from class;
STUNUM STUNAME STUBRANCH STUMARKS
---------- --------------- ---------- ------------------------- ------------------------
1 abhi cse 90
2 balu ece 90
3 chandra cse 85
4 deva cse 90
5 eswar eee 85
6 naresh ece 80
7 ganesh mechanical 80
8 mahesh civil 80
9 jagadesh civil 80
10 karthik cse 85
10 rows selected.
5. SQL> select stunum, stuname
2 from class
3 where stubranch='cse';
STUNUM STUNAME
---------- ----------------------
1 abhi
3 chandra
4 deva
10 karthik
6. SQL> select *
2 from class
3 where stumarks>85;
STUNUM STUNAME STUBRANCH STUMARKS
--------------- ------------------- ----------------- ---------------------
1 abhi cse 90
2 balu ece 90
4 deva cse 90
7. SQL> alter table class
2 add(age number(2), location varchar2(10));
Table altered.
8. SQL> select *
2 from class;
STUNUM STUNAME STUBRANCH STUMARKS AGE LOCATION
------------ ---------------- ------------------------ ------------------ ---------- -------------------
1 abhi cse 90
2 balu ece 90
3 chandra cse 85
4 deva cse 90
5 eswar eee 85
6 naresh ece 80
7 ganesh mechanical 80
8 mahesh civil 80
9 jagadesh civil 80
10 karthik cse 85
10 rows selected.
9. SQL> update class
2 set location='nandyal'
3 where stubranch='cse';
4 rows updated.
SQL> update class
2 set location='kurnool'
3 where stubranch='ece';
2 rows updated.
SQL> update class
2 set location='kadapa'
3 where stubranch='eee';
1 row updated.
SQL> update class
2 set location='allagadda'
3 where stubranch='civil';
2 rows updated.
SQL> update class
2 set location='proddatur'
3 where stubranch='mechanical';
1 row updated.
10. SQL> update class
1 set age=19;
10 rows updated.
11. SQL> select *
2 from class;
STUNUM STUNAME STUBRANCH STUMARKS AGE LOCATION
------------ --------------- -------------------- ------------------- ---------- --------------
1 abhi cse 90 19 nandyal
2 balu ece 90 19 kurnool
3 chandra cse 85 19 nandyal
4 deva cse 90 19 nandyal
5 eswar eee 85 19 kadapa
6 naresh ece 80 19 kurnool
7 ganesh mechanical 80 19 proddatur
8 mahesh civil 80 19 allagadda
9 jagadesh civil 80 19 allagadda
10 karthik cse 85 19 nandyal
10 rows selected.
12. SQL> select *
2 from class
3 where location='kadapa';
STUNUM STUNAME STUBRANCH STUMARKS AGE LOCATION
----------- --------------- ------------------- ----------------- ----------- -----------------
5 eswar eee 85 19 kadapa
13. SQL> delete class
2 where location='allagadda';
2 rows deleted.
14. SQL> alter table class
2 drop column age;
Table altered.
15. SQL> insert into class values(3,'knreddy','cse','nandyal');
insert into class values(3,'knreddy','cse','nandyal')
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.SYS_C003997) violated
16. SQL> select *
2 from class;
STUNUM STUNAME STUBRANCH STUMARKS LOCATION
------------ --------------- ------------------- ------------------- ----------------
1 abhi cse 90 nandyal
2 balu ece 90 kurnool
3 chandra cse 85 nandyal
4 deva cse 90 nandyal
5 eswar eee 85 kadapa
6 naresh ece 80 kurnool
7 ganesh mechanical 80 proddatur
10 karthik cse 85 nandyal
8 rows selected.