Date post: | 21-Dec-2015 |
Category: |
Documents |
Upload: | sharma-divya |
View: | 78 times |
Download: | 4 times |
Q3.Create following two tables including integrity constraints using SQL Command:
Table Name : StudentField Name Data Type Field Size ConstraintsRoll No Number 8 Primary KeyName Text 20 Not NullAddress Text 50
Table Name : EnrollmentField Name Data Type Field Size ConstraintsRoll No Number 8 Foreign Key,
Primary KeyCourse Text 20 Primary KeyGrade Text 1 ‘A’,’B’,’C’,’D’,’E’
Perform the following operations using SQL Commands
(i) List all the Students enrolled for ‘BBA’ and have grade ‘A’(ii) Find the number of students enrolled in each course.
Ans:-create table student2(rollno number(8) ,name varchar2(20) NOT NULL,address varchar2(50));
Output:-
1
create table enrollment(rollnumber number(8) ,course varchar2(20),grade varchar2(1) CHECK(grade IN('A','B','C','D','E')));
Output:-
alter table student2 add constraint pk1_primarykey PRIMARY KEY(rollno);
Output:-
2
alter table enrollment add constraint pk2_primarykey PRIMARY KEY(rollnumber,course);
Output:-
alter table enrollment add constraint fk1_rollnumber FOREIGN KEY(rollnumber) REFERENCES student2(rollno);
Output:-
3
select * from student2,enrollment where course='bba' and grade='A' and rollno=rollnumber;
Output:-
select count(course) from enrollment GROUP by course;
4
Q4. Consider EMP table and perform the following using SQL Commands
(i) Add a new column called total sal to the table (ii) Update the total sal column with sal + comm.(iii) Find the employees having salary greater than average salary of the employees
working in dept 10(iv) List employee name and yearly salary and arrange the output on the basis of
yearly salary in descending order.(v) Retrieve the names of departments in ascending order and their employees in
descending order.
Ans:-
create table emp(empno number(8),dept varchar2(20),name varchar2(20),salary number(20),comm number(8));
select * from emp;
5
alter table emp add totalsal number(20);
update empset totalsal=salary+comm;
6
select name,salary from emp where salary >(select AVG(salary) from emp);
select name,salary from emp order by salary desc;
7
select name,dept from emp order by dept asc,name desc;
8
9