You are on page 1of 8

Database Queries

Rename:
syntax: rename <table-name> to <new table name>
example: rename student to student_reocord
Display the table structure:
Syntax: Desc <table-name>
example: desc student;
Update:
syntax: update <table> set <attribute>= where <attribute>= ;
Delete:
syntax: delete from <table> where <condition>
Tested Queries:
Qus: Create a query to display all the data from employee table consisting
emp_id,emp_name,e-mail,phno,job_id separate each column by a comma name the o/p
column as output
Query: select emp_id||,||emp_name||,||e_mail||,||phno||,||job_id as
output from employee ;
Alter table:
syntax: alter table <tablename> add <attributes> <data-type>
alter table <tablename> drop <attributes> <data-type>
alter table <tablename> modify <attributes> <data-type>
Tested Queries:
Qus: Add primary key (college, roll_no) to student master table.
Query: alter table student_master add primary key (college,m roll_no);
Qus: Modify employee table to allow for larger emp_name & then confirm your
modification.
Query: alter table modify emp_name varchar2(50);
Insert Query:
Qus: enter the tuples into department table with data from dept-record table.
Include columns dept_id , dept_name.
Query: insert into <table_name> select <column_name> from <given table>
Truncate:
syntax: truncate table <table_name>
example: truncate table student;
Drop:
syntax: Drop table <tb_name>
example: drop table student;

Database Constraints:
Unique: unique constraint defined at the table lable:
create table student(
roll_no varchar2(10),
subject varchar 2(20),
marks varchar2(10),
unique (roll_no) );

Not null:
Schema: Employee (id,name,DOB,gender)
Query: create table Employee(
emp_id varchar2(30),
emp_name varchar2(30),
emp_dob date not null,
gender varchar2(4) );

Basic Query Structure

select attrib1,attrib2,..attrib n from <table> where cond1cond n
Qus: Student table:
S_id S_name DOB
A1 Xyz 01 jan 94
B1 abc 10 nov 92
C1 wxy 21 nov 21
Query: select a1 from student;
S_id S_name DOB
A1 Xyz 01 jan 94

Foreign key constraint defined with ON DELETE:
create table student1(
roll_no number (10),
sub varchar2 (10),
marks number (12),
grade char (1),
foreign key (roll_no) referneces student (roll_no) on delete set null );


Foreign key constraint defined with ON UPDATE CASCADE:
create table student1(
roll_no number (10),
sub varchar2 (10),
marks number (12),
grade char (1),
primary key (sub),
foreign key (grade) referneces grade (grade) on update cascade );
Check:
Qus: Take employee table having attribs (emp_id,salary,commisson) where salary or
commission should be greater than 0.
Query: create table employee(
emp_id varchar (2),
salary varchar2 (10),
comm varchar2(12),
primary key (emp_id),
check (salary>0 or comm>0) );
Schema: Employee(id,name,dob,gender)
Query: create table employee (
emp_id varchar2(30) primary key,
emp_name varchar2(20),
emp_dob date,
emp_gen varchar2(5) );
Primary key constraitnt defined at table level:
create table Employee(
emp-id number(12),
emp_name varchar2(23),
emp_dob date,
emp_gen varchar2(30) primary key (emp_id) );
Foreign Key: foreign key represents relation between two tables.
Foreign key constraint defined at the column lavel:
Qus: create a table student1(roll_no,subject,marks) where roll_no is foreign key.
Query: create table student1(
roll_no varchar2(10) foreign key references student (roll_no),
subject varchar2 (10),
marks varchar2 (12) );

Foreign key constraint defined at the Table lavel:
Qus: create a table student detail having attribs roll_no,subject,marks,grade.
Query: create table student (
roll_no varchar2(10),
subject varchar2 (10),
marks varchar(100)
grade char(1),
foreign key references student(roll_no) );

Data Definition Language
Defining the database schema:
create table table_name(
attrib1 datatype1,
attrib2 datatype2,
attrib3 datatype3,
. attrib n datatype n);
Example: Employee(id,name,dob,gender)
create table Employee(
emp_id number(10),
emp_name varchar2(30),
emp_dob date,
emp_gender varchar2(10) );
Add a new tuple in a table:
insert into student values (001,Shubham,21,27-Apr=1992,male);

Special Operators
IN:
Qus: write select query where chosen departments are the dept_no 2 and 3.
Query: select department_no from employee where dept_no not IN 2 and 3;
Between:
Qus: Display Emp_name and salary for all employee whose salary is not in the range of
1000 and 50,000 .
Query: select e_name,salary from employee where not salary between 10000 and
50000;
Like:
Qus: List the employee whose name starts with A .
Query: select e_name from employee where e_name like A%;
Qus: List Employee whose name have D as second character followed by any character.
Query: select e_name from employee where e_name like _D%;
Group by:
syntax: select <attrib_name> from <table>group by condition
Qus: Write a query to display no of people with the same job.
Query: select count(*),job-id from employee group by job_id;
Order by:
Syntax: select * from <table> order by <column-name>;
Example: select * from employee order by emp_id;
Qus: Display manager_id and salary of the lowest paid employee for that manager .
Exclude any one whose manager is no known .Exclude any groups where min. salary
is 60000 or less sort the o/p in desc. order of salary.
Query: select man_id,min(salary)
from employee where man_id is not null
group by man_id
having min(salary)>60000
order by min(salary) desc;
Granting system privileges:
syntax: grant privilege1 [,privilege2]
to username1 [,username2];
Example: grant all on Student_record to shubham;
Granting object privilege:
syntax: grant objectprvilege [(columnnames)]|ALL
on objectname
to user|role|public
[WITH GRANT OPTION];
Revoking privilege:
syntax: revoke privilege1 [,privilege2,]\ALL
on object name
from users\role\PUBLIC
[CASCADE CONSTRAINTS];

You might also like