The document describes how to perform various SQL operations like creating and managing users, tables, and data. It covers topics like creating and dropping users, granting privileges, creating, altering and dropping tables, performing joins, and inserting, updating, deleting and selecting data.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
40 views42 pages
Advanced Database
The document describes how to perform various SQL operations like creating and managing users, tables, and data. It covers topics like creating and dropping users, granting privileges, creating, altering and dropping tables, performing joins, and inserting, updating, deleting and selecting data.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42
Advanced database lab module
• How to create user:
Create user username identified by password default tablespace users temporary tablespace temp; For example : Create user amanuel identified by 1234 default tablespace users temporary tablespace temp; • How to grant to user: Grant all privilege to username; For example: Grant all privilege to amanuel; How to expire password: Alter user amanuel password expire; • How to lock account: Alter user username identified by password account lock; Or Alter user username account lock; For example: Alter user amanuel identified by 1234 account lock; Or Alter user amanuel account lock; • How to unlock account: Alter user username identified by password account unlock; Or Alter user username account unlock; For example: Alter user amanuel identified by password 1234 account unlock; Or Alter user amanuel account unlock; • How to expire the password and change it: alter user username identified by password account lock password expire; Or alter user username account lock password expire; For example: alter user amanuel identified by 1234 account lock password expire; Or alter user amanuel account lock password expire; When you unlock the account, password is expire and tells u to insert another password • How to drop user: Drop user username; For example: Drop user amanuel; • How to create table: Create table aman(column_name datatype,……); For example: Create table aman(emp_id number(5),emp_name varchar2(30)); • How to Alter table: Modify column: Alter table table_name modify(column_name datatype); For example: Alter table aman modify(emp_name varchar(20); Add column: Alter table table_name add(column_name datatype); For example: Alter table aman add(last_name varchar2(20)); • How to rename table name: Rename table_name to another_table_name; For example: Rename aman into amani; Drop comumn: Alter table table_name drop(column_name); For example: Alter table aman drop(emp_name); • Drop table Drop table table_name; For example: Drop table aman; • How to Insert data into table Insert into table_name(column1,column2,column3,..) values(values1,values2,values3,..); For example: Insert into aman(emp_id,emp_name,last_name) values(12,’alemayew’,’kelay’); • How to insert multiple data into table using single insert statement: Insert all into table_name(column1,column2,column3,..) values(values1,values2,values3,..) into table_name(column1,column2,column3,..) values(values1,values2,values3,..) into table_name(column1,column2,column3) values(values1,values2,values3,…)….) select * from dual; for example: Insert all into aman(emp_id,emp_name,last_name) values(12,’alemayew’,’kelay’) into aman(emp_id,emp_name,last_name) values(13,’abebe’ ,’beso‘) into aman(emp_id,emp_name,last_name) values(14,’chala’,’chube’) select * from dual; • Insertion by address: Insert into table_name values(‘&column1’,’&column2’,…); For example: Insert into aman values(‘&emp_id’,’&emp_name’,’&last_name’); • How to delete data from table: Delete from table_name where condition; For example: Delete from aman where emp_id=13; • How to update data in table: UPDATE table_name SET column_name = value [ column_name = value]... [ WHERE condition ]; For example: Update aman set emp_name=‘ababa’ where emp_id=12; • Dual function: without table For example Select 1+1 from dual; =2 Select 5*5 from dual; =25 Select abs(-10) from dual; =10 Select sysdate from dual; =current date • Dual function with table For example: for example multiple insertion • To drop primary key • ALTER TABLE Persons DROP PRIMARY KEY; • First create session privilege to new user Syntax Grant Create session to new_user; Example Grant create session to nn; • To give select,delete,insert,update privilege for another user syntax Grant select |delete |insert | update on table_name to new_user_name; Example: Grant select |delete |insert | update on emp to nn; • If u want to give full privilege for user to access the table you can use this command: syntax : Grant all on table_name to new_user; Example: Grant all on emp to nn; • If no longer allow for user to access this table you can use this command: Revoke select |delete |insert | update on table_name from new_user_name; Example: Revoke select |delete |insert | update on emp from nn; emp table: ID NAME FNAME SALARY ---------- -------------------- -------------------- ---------- • 11 ami girma 2000.25 • 15 amu baba 3000.25 Info table: INF_ID RESP ID • ---------- -------------------- ---------- • 34 manager 11 • 33 accountant 15 • 23 assist 12 Inner Join • An inner join essentially finds the intersection between the two tables. SELECT emp.Name, info. resp FROM Emp, info WHERE Emp.ID=info.ID; Or SELECT Emp.Name, info.resp FROM Emp INNER JOIN info on Emp.ID=info.ID; Self join select i.resp emp_resp,m.resp man_resp from info i,info m where m.id=i.inf_id; Full outer join The full outer join returns a result table with the matched data of two table then remaining rows of both left table and then the right table. • SELECT Emp.Name, info.resp FROM Emp full join info ON Emp.ID=info.ID; • LEFT OUTER JOIN • The LEFT OUTER JOIN returns all the rows from the first table (Emp), even if there are no matches in the second table (info). • SELECT Emp.Name, info.resp FROM Emp LEFT JOIN info ON Emp.ID=info.ID; RIGHT OUTER JOIN • The RIGHT OUTER JOIN returns all the rows from the second table (info), even if there are no matches in the first table (Emp). • SELECT Emp.Name, info.resp FROM Emp right JOIN info ON Emp.ID=info.ID; Natural join • SELECT id, name, inf_id FROM emp NATURAL JOIN info; • Create table from another table: Create table stud as(select name,fname from emp); Create table from two or more than two table Create table work as(select emp.id,emp.name,info.resp from emp,info); Subquery • Select name,fname from emp where id in(select id from info where resp=‘manager’); • select * from emp where exists (select * from info where emp.id= info.id); • select * from info where not exists (select * from emp where info.id= emp.id); Character Functions LOWER • LOWER(argument) Example • SELECT LOWER(‘STUDENT') from dual; UPPER • UPPER(argument) • Select upper(‘student’) from dual; INITCAP Initcap(argument) Select initcap(‘student’) from dual; • LPAD & RPAD • LPAD(string,len,pstring) • RPAD(string,len,pstring) • Example • SELECT LPAD(‘stud',10,‘a') from dual; • SELECT RPAD(‘stud',10,‘a') from dual; Syntax • SUBSTR(string,pos,len) Exmaple: • SELECT SUBSTR(‘student',2,3) from dual; • INSTR(string,search) • INSTR(string,search,pos,n) • Exampl: • SELECT INSTR(‘student',‘ent') from dual; • SELECT INSTR(‘studentent',‘ent‘,3,2); Syntax • LTRIM(string,rem) • RTRIM(string,rem) • Example: • SELECT LTRIM('Ooracle’,'O') from dual; • SELECT RTRIM('Ooracle’,‘e') from dual; • LENGTH(string) Example: • SELECT LENGTH('Oracle’) from dual; Syntax • TRANSLATE(string,from,to) Example: SELECT TRANSLATE('hehho',‘h','l') from dual; • syntax • REPLACE(string,search,replace) • REPLACE(string,search) • SELECT REPLACE(‘amangir',‘gir',‘uel') from dual; • SELECT REPLACE(‘amanuelgir',‘gir') from dual; •Run sql using notepad •C:\Windows\system32>cd/ •C:\>cd amiti •C:\amiti>start aa.sql •C:\amiti>sqlplus "/ as sysdba" •SQL*Plus: Release 11.2.0.1.0 Production on Wed May 20 08:22:32 2015 •Copyright (c) 1982, 2010, Oracle. All rights reserved. •Connected to: •Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production •With the Partitioning, OLAP, Data Mining and Real Application Testing options •SQL> start aa.sql •Table created. •SQL> start aa.sql •1 row created.