SQL

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 18

SQL> create table student1 2 (stud_no number(5), 3 stud_name char(10), 4 stud_class char(10), 5 stud_address char(15)); Table created.

SQL> desc student1; Name Null? Type ----------------------------------------- -------- ---------------------------STUD_NO NUMBER(5) STUD_NAME CHAR(10) STUD_CLASS CHAR(10) STUD_ADDRESS CHAR(15) SQL> insert into student1 values('&stud_no','&stud_name','&stud_class','&stud_address'); Enter value for stud_no: 1 Enter value for stud_name: Ramesh Enter value for stud_class: BCA TY Enter value for stud_address: latur old 1: insert into student1 values('&stud_no','&stud_name','&stud_class','&stud_address') new 1: insert into student1 values('1','Ramesh','BCA TY','latur') 1 row created. SQL> / Enter value for stud_no: 2 Enter value for stud_name: rahul Enter value for stud_class: BCA TY Enter value for stud_address: pune old 1: insert into student1 values('&stud_no','&stud_name','&stud_class','&stud_address') new 1: insert into student1 values('2','rahul','BCA TY','pune') 1 row created. SQL> select * from student1;

STUD_NO STUD_NAME ------------------1 Ramesh 2 rahul SQL> delete student1 2 where stud_name='rahul'; 1 row deleted. SQL> select * from student1; STUD_NO ---------1 3 4

STUD_CLASS ---------BCA TY BCA TY

STUD_ADDRESS --------------latur pune

STUD_NAME ---------Ramesh sonali rajesh

STUD_CLASS ---------BCA TY BCA TY BCA TY

STUD_ADDRESS --------------latur ambajogai pune

SQL> update student1 2 set stud_address ='latur' 3 where stud_name='rajesh'; 1 row updated. SQL> select * from student1; STUD_NO ---------1 3 4 STUD_NAME ---------Ramesh sonali rajesh STUD_CLASS STUD_ADDRESS ----------------------BCA TY latur BCA TY ambajogai BCA TY latur

SQL> alter table student1 2 add age number(5); Table altered. SQL> select * from student1; STUD_NO STUD_NAME STUD_CLASS STUD_ADDRESS AGE --------------------------------------------------1 Ramesh BCA TY latur 3 sonali BCA TY ambajogai

rajesh

BCA TY

latur

SQL> update student1 2 set age =20 3 where stud_name='Ramesh'; 1 row updated. SQL> update student1 2 set age =19 3 where stud_name='sonali'; 1 row updated. QL> update student1 2 set age =19 3 where stud_name= 'rajesh' ; row updated. QL> select * from student1; STUD_NO STUD_NAME STUD_CLASS STUD_ADDRESS AGE -------------------------------------------------1 Ramesh BCA TY latur 20 3 sonali BCA TY ambajogai 19 4 rajesh BCA TY latur 19 SQL> select * from student1; STUD_NO STUD_NAME STUD_CLASS STUD_ADDRESS AGE ---------- ---------- ---------- --------------- ---------1 Ramesh BCA TY latur 20 3 sonali BCA TY ambajogai 19 4 rajesh BCA TY latur 19

SQL> alter table student1 2 drop column age; Table altered. SQL> select * from student1; STUD_NO STUD_NAME STUD_CLASS STUD_ADDRESS ---------- ---------- ---------- --------------1 Ramesh BCA TY latur 3 sonali BCA TY ambajogai 4 rajesh BCA TY latur SQL> desc student1; Name -----------------STUD_NO STUD_NAME STUD_CLASS STUD_ADDRESS Null? -------Type ----------------------NUMBER(5) CHAR(10) CHAR(10) CHAR(15)

SQL> alter table student1 2 modify stud_name varchar(20); Table altered. SQL> desc student1; Name Null? ----------------------------STUD_NO STUD_NAME STUD_CLASS STUD_ADDRESS SQL> select * from stud1; ROLL_NO ---------1 1 NAME ---------rajesh rajesh Type ----------------------NUMBER(5) VARCHAR2(20) CHAR(10) CHAR(15) ADDRE ----latur latur

SQL> drop table stud1; Table dropped. SQL> select * from stud; ROLL_NO ---------1 2 3 4 1 2 6 rows selected. SQL> truncate table stud; Table truncated. SQL> select * from acc; NAME ---------jon ramesh rajesh mangash rohini sonal ACC_NO ---------20 40 60 80 100 120 BALANCE ---------20000 300000 200000 70000 10000 20000 NAME ---------rajesh ramesh ravi radhika rajesh rahul ADDRESS ---------latur pune chakur latur latur solapur

6 rows selected. SQL> RENAME acc TO ACCOUNT; Table renamed. SQL> create table sale 2 (p_no number(10)primary key, 3 name char(10) 4 vendor char(10));

Table created.

SQL> insert into sale values('&p_no','&name','vendor'); Enter value for p_no: 23455 Enter value for name: rajesh old 1: insert into sale values('&p_no','&name','vendor') new 1: insert into sale values('23455','rajesh','vendor') 1 row created. SQL> / Enter value for p_no: 23455 Enter value for name: rahul old 1: insert into sale values('&p_no','&name','vendor') new 1: insert into sale values('23455','rahul','vendor') insert into sale values('23455','rahul','vendor') SQL> create table nullex 2 (cust_no number(10)not null, 3 name char(10)); Table created.

SQL> insert into nullex(name)values('rajesh'); insert into nullex(name)values('rajesh') * ERROR at line 1: ORA-01400: cannot insert NULL into ("SCOTT"."NULLEX"."CUST_NO") SQL> insert into nullex values(2,'ram'); 1 row created. SQL> create table unqex 2 (cust_no number(10)unique, 3 name char(10)); Table created. SQL> insert into unqex values(1,'satish');

1 row created. SQL> create table chexam 2 (cust_no number(10), 3 name char(10)check(name=upper(name))); Table created. SQL> create table fee 2 (rollno number(5), 3 class char(7), 4 fees number(7), 5 foreign key(rollno)references stud4(rollno)); Table created. SQL> select * from acc; NAME ---------jon ramesh rajesh mangash rohini sonal ACC_NO ---------20 40 60 80 100 120 BALANCE ---------20000 300000 200000 70000 10000 20000

6 rows selected. MIN(): Returns a minimum value of expression. SYNTAX:- MIN( [< DISTINCT>|<ALL>]<EXPR>) EX:SQL> select min(balance) "MIN" from acc; MIN ---------10000

MAX():Returns the maximum value of expression. SYNTAX:- MAX( [< DISTINCT>|<ALL>]<EXPR>) SQL> select max(balance)"MAX" from acc; MAX ---------300000

AVG():
Returns an average value of n, ignoring null values in column. SYNTAX:- AVG( [< DISTINCT>|<ALL>]<n>) EXAMPLE: SQL> select avg(balance)"Average"from acc; Average ---------103333.333 SQL> select count(acc_no)"COUNT" from acc; COUNT ---------6 SQL> select sum(balance)"TOTAL"from acc; TOTAL ---------620000 SQL> select abs(-15) ABSOLUTE from dual; ABSOLUTE ---------SQL> select upper('computer')"UPPER" from dual; UPPER --------

COMPUTER SQL> select initcap('COMPUTER SCIENCE')"INIT" from dual; INIT ---------------Computer Science SQL> select length('Computer Science')"LENGHT" from dual; LENGHT ---------16 SQL> select LTRIM('LATUR','TUR')"LTRIM" from dual; LTRIM ----LATUR SQL> select RTRIM('COCSIT','SIT')"RTRIM" from dual; RTR --COC SQL> select ADD_MONTHS('31-jan-09',3)"MONTH" from dual; MONTH --------30-APR-09 SQL> select last_day(sysdate) from dual; LAST_DAY( --------31-JUL-09 SQL> select last_day(sysdate) from dual; LAST_DAY( --------31-JUL-09 SQL> select months_between('01-feb-08','01-jan-08')from dual;

MONTHS_BETWEEN('01-FEB-08','01-JAN-08') SQL> select next_day('31-jan-08','thursday')from dual; NEXT_DAY( --------07-FEB-08 SQL> select to_char(sysdate,'dd-month-yy')from dual; TO_CHAR(SYSDATE --------------23-july -09 SQL> select to_number('444')from dual; TO_NUMBER('444') ---------------444 SQL> select to_date('06/07/08','dd-mm-yy')from dual;
TO_DATE(' --------06-JUL-08

SQL> select * from stud; ROLL_NO ---------1 2 3 NAME --------rajesh ramesh ravi ADDRESS ---------latur pune chakur

SQL> select * from mark; ROLL_NO MARKS ------------------1 50 2 80 3 90 SQL> select roll_no from stud 2 union 3 select roll_no from mark;

ROLL_NO ---------1 2 3 4 SQL> select roll_no from stud 2 minus 3 select roll_no from mark; ROLL_NO ---------4

SQL> select roll_no from stud 2 intersect 3 select roll_no from mark; ROLL_NO ---------1 2 3 SQL> select * from product; PROD_ID ---------101 102 103 104 PROD_NAME ---------floppy monitor mouse cd drive PRICE QTY ---------- ---------2000 3 3000 5 500 5 1000 3

SQL> select prod_id,prod_name,price*qty from product; PROD_ID PROD_NAME PRICE*QTY

---------- 101 102 103 104

--------floppy monitor mouse cd drive

---------6000 15000 2500 3000

SQL> select prod_id,prod_name,price*qty+500-50/10 from product; PROD_ID PROD_NAME ------------------101 floppy 102 monitor 103 mouse 104 cd drive SQL> select * from product 2 where price>500 and price<4000; PROD_ID ---------101 102 104 PROD_NAME ---------floppy monitor cd drive PRICE*QTY+500-50/10 ------------------6495 15495 2995 3495

PRICE ---------2000 3000 1000

QTY ---------3 5 3

SQL> select * from product 2 where price>=3000 OR price<600; PROD_ID ---------- 102 103 PROD_NAME --------monitor mouse PRICE ---------3000 500 QTY ---------5 5

SQL> select *from product 2 where NOT(price=400 OR price=2000); PROD_ID ---------102 103 104 PROD_NAME ---------monitor mouse cd drive PRICE --------3000 500 1000 QTY ---------5 5 3

SQL> select * from acct_mast; VERIEMP_NO ELP_NAME -----------------1 rajesh 2 ramesh SQL> create INDEX idxveriemp_no ON ACCT_MAST(veriemp_no); Index created. SQL> DROP index idxveriemp_no; Index dropped. SQL> select * from account; NAME ---------jon ramesh rajesh mangash rohini sonal ACC_NO --------20 40 60 80 100 120 BALANCE ---------20000 300000 200000 70000 10000 20000

6 rows selected. SQL> create view acc as select * from account; View created.

SQL> select * from acc; NAME ---------- jon ACC_NO --------20 BALANCE -------20000

ramesh rajesh mangash rohini sonal

40 60 80 100 120

300000 200000 70000 10000 20000

6 rows selected. SQL> create view acc1 as select name from account; View created. SQL> select * from acc1; NAME ---------jon ramesh rajesh mangash rohini sonal 6 rows selected. SQL> create sequence addr_seq increment By 1 start with 1 2 minvalue 1 maxvalue 999 cycle; Sequence created. SQL> select addr_seq .nextval from dual; NEXTVAL ---------1 SQL> drop sequence addr_seq; Sequence dropped. SQL> select * from customer;

CUST_NAME ---------jonson smith hayes turner jones lindsay 6 rows selected.

ACCOUNT_NO ---------101 102 122 305 222 217

SQL> select * from depositor; DEP_NAME ---------jonson smith hayes turner jones linsay 6 rows selected. SQL> select customer.cust_name,account_no,depositor.dep_name,loan_no 2 from customer,depositor 3 where cust_name=dep_name; CUST_NAME ---------jones jonson smith ACCOUNT_NO ---------222 101 102 DEP_NAME ---------jones jonson smith LOAN_NO ---------93 17 23 LOAN_NO ---------17 23 15 14 93 16

SQL> create table area1(radius number(5), 2 area number(14,2)); Table created.

SQL> ed Wrote file afiedt.buf 1 declare 2 pi constant number(4,2) := 3.14; 3 RAdius number(5); 4 AR number(14,2); 5 begin 6 radius :=3; 7 while radius<=7 8 loop 9 AR := pi *power(RAdius,2); 10 insert into area1 values(RAdius,AR); 11 radius := radius +1; 12 End loop; 13* end; SQL> / PL/SQL procedure successfully completed. SQL> select * from area1; RADIUS AREA ---------- ---------3 28.26 4 50.24 5 78.5 6 113.04 7 153.8 PL/SQL procedure successfully completed. SQL> select * from emp11; ENO ---------101 5 NAME ---------sheetal ashwini SALARY ---------21000 25000

SQL> create table stud 2 (rno number(10),name char(10),city char(10)); Table created. SQL> desc stud; Name Null? Type ----------------------------------------- -------- ---------------------------RNO NUMBER(10) NAME CHAR(10) CITY CHAR(10) SQL> create table s1 2 (rno number(4),name char(10),city char(10),operation char(10),sdate date); Table created. SQL> create trigger test1 2 after 3 update or delete on stud 4 for each row 5 declare 6 oper char(8); 7 begin 8 if updating then 9 oper:='update'; 10 end if; 11 if deleting then 12 oper:='delete'; 13 end if; 14 insert into s1 15 values(:old.rno,:old.name,:old.city,oper,sysdate); 16 end; 17 / Trigger created. SQL> select * from stud;

RNO ---------1 2

NAME ---------Ajinkya Aditi

CITY ---------Latur pune

You might also like