Dbms Lab New
Dbms Lab New
INDEX
1
DATABASE MANAGEMENT SYSTEMS
AIM:
To create a DDL to perform creation of table, alter, modify and drop column.
0
DDL COMMANDS
1. The Create Table Command: - it defines each column of the table uniquely. Each column
has minimum of three attributes, a name , data type and size.
Syntax:
Syntax:
Syntax:
Syntax:
2
DATABASE MANAGEMENT SYSTEMS
Syntax:
Syntax:
7. Destroying tables.
Syntax:
3
DATABASE MANAGEMENT SYSTEMS
CREATION OF TABLE:
SYNTAX:
EXAMPLE:
Table created.
1 row created.
1 row created.
OUTPUT:
Table created.
1 row created.
SYNTAX:
EXAMPLE:
OUTPUT:
ID NAME GAME
1 Mercy Cricket
1 Mercy cricket
2 Sharmi Tennis 19
SYNTAX:
EXAMPLE:
OUTPUT:
MODIFY
desc student;
Id Number(6)
Name Varchar(20)
Game Varchar(25)
Age Number(4)
5
DATABASE MANAGEMENT SYSTEMS
DROP:
EXAMPLE:
SQL>Table dropped.
TRUNCATE TABLE
DESC
Name VarChar(15)
CONSTRAINTS:
Create table tablename (column_name1 data_ type constraints, column_name2 data_ type
constraints …)
Example:
Create table Emp ( EmpNo number(5), EName VarChar(15), Job Char(10) constraint un
unique, DeptNo number(3) CONSTRAINT FKey2 REFERENCES DEPT(DeptNo));
Create table stud (sname varchar2(20) not null, rollno number(10) not null,dob date not null);
6
DATABASE MANAGEMENT SYSTEMS
DOMAIN INTEGRITY
CHECK CONSTRAINT
Example:
Create table student (regno number (6), mark number (3) constraint b check (mark
>=0 and mark <=100));
ENTITY INTEGRITY
Example:
Example:
Queries:
7
DATABASE MANAGEMENT SYSTEMS
Solution:
1. Understand create table syntax.
2. Use the create table syntax to create the said tables.
3. Create primary key constraint for each table as understand from logical table structure.
Ans:
SQL> create table emp(empno number(6),ename varchar2(20)not null,job varchar2(10) not
null, deptno number(3),sal number(7,2));
Table created.
Solution:
1. Learn alter table syntax.
2. Define the new column and its data type.
3. Use the alter table syntax.
Ans: SQL> alter table emp add(experience number(2));
Table altered.
Q3: Modify the column width of the job field of emp table.
Solution:
1. Use the alter table syntax.
2. Modify the column width and its data type.
Ans: SQL> alter table emp modify(job varchar2(12));
Table altered.
SQL> alter table emp modify(job varchar(13));
Table altered.
Solution:
1. Understand create table syntax.
2. Decide the name of the table.
3. Decide the name of each column and its data type.
4. Use the create table syntax to create the said tables.
5. Create primary key constraint for each table as understand from logical table structure.
Ans:
SQL> create table dept(deptno number(2) primary key,dname varchar2(10),loc
varchar2(10));
8
DATABASE MANAGEMENT SYSTEMS
Table created.
Q5: create the emp1 table with ename and empno, add constraints to check the empno value
while entering (i.e) empno > 100.
Solution:
1. Learn alter table syntax.
2. Define the new constraint [columns name type]
3. Use the alter table syntax for adding constraints.
Ans:
SQL> create table emp1(ename varchar2(10),empno number(6) constraint
check(empno>100));
Table created.
Q7: Truncate the emp table and drop the dept table
Solution:
QUESTIONS
1. Define DDL
2. What are constraints?
3. Categories of SQL Command.
4. Difference between truncate and drop.
5. Define primary and referential integrity.
RESULT:
9
DATABASE MANAGEMENT SYSTEMS
AIM;
To study the various DML commands and implement them on the database.
DML COMMANDS
DML commands are the most frequently used SQL commands and is used to query
and manipulate the existing database objects. Some of the commands are Insert, Select,
Update, Delete.
Insert Command This is used to add one or more rows to a table. The values are separated by
commas and the data types char and date are enclosed in apostrophes. The values must be
entered in the same order as they are defined.
Select Commands It is used to retrieve information from the table. It is generally referred to
as querying the table. We can either display all columns in a table or only specify column
from the table.
Update Command It is used to alter the column values in a table. A single column may be
updated or more than one column could be updated.
Delete command After inserting row in a table we can also delete them if required. The delete
command consists of a from clause followed by an optional where clause.
1 row created.
Q2: Insert more than a record into emp table using a single insert command.
10
DATABASE MANAGEMENT SYSTEMS
1 row created.
1 row created.
1 row created.
Q3: Update the emp table to set the salary of all employees to Rs15000/- who are working as
ASP
1 Mathi AP 1 10000
11
DATABASE MANAGEMENT SYSTEMS
1 Mathi AP 1 10000
Q4: Create a pseudo table employee with the same structure as the table emp and insert rows
into the table using select clauses.
Table created.
EMPNO NUMBER(6)
DEPTNO NUMBER(3)
SAL NUMBER(7,2)
Mathi AP
Arjun ASP
Gugan ASP
Karthik Prof
Akalya AP
suresh lect
6 rows selected.
12
DATABASE MANAGEMENT SYSTEMS
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
5 Akalya AP 1 10000
6 suresh lect 1 8000
6 rows selected.
Q7: List the records in the emp table orderby salary in ascending order.
Ans: SQL> select * from emp order by sal;
EMPNO ENAME JOB DEPTNO SAL
1 Mathi AP 1 10000
5 Akalya AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
Q8: List the records in the emp table orderby salary in descending order.
Ans: SQL> select * from emp order by sal desc;
EMPNO ENAME JOB DEPTNO SAL
13
DATABASE MANAGEMENT SYSTEMS
1 Mathi AP 1 10000
4 Akalya AP 1 10000
1 Mathi AP 1 10000
3 Gugan ASP 1 15000
5 Akalya AP 1 10000
Q10: Display deptno from the table employee avoiding the duplicated values.
Solution:
1. Use SELECT FROM syntax.
2.Select should include distinct clause for the deptno.
Ans: SQL> select distinct deptno from emp;
DEPTNO
14
DATABASE MANAGEMENT SYSTEMS
3. BUILT IN FUNCTIONS
DATE & TIME FUNCTION
07-APR-10
07-APR-10
07-JUL-10
30-APR-10
27-APR-10
NEXT_DAY(
15
DATABASE MANAGEMENT SYSTEMS
13-APR-10
NUMERIC FUNCTION
16
24
34
15
-1
16
DATABASE MANAGEMENT SYSTEMS
ABS(-70)
70
MATH FUNCTION:
ABS(45)
45
POWER(10,12)
1.000E+12
MOD(11,5)
EXP(10)
22026.466
17
DATABASE MANAGEMENT SYSTEMS
SQRT(225)
15
18
DATABASE MANAGEMENT SYSTEMS
Q1: Display all employee names and salary whose salary is greater than minimum salary of
the company and job title starts with ‗M‘.
Solution:
2. Use like operator to match job and in select clause to get the result.
Ans: SQL> select ename,sal from emp where sal>(select min(sal) from emp where job like
'A%');
ENAME SAL
-----------------------
Arjun 12000
Gugan 20000
Karthik 15000
Q2: Issue a query to find all the employees who work in the same job as Arjun.
1 Mathi AP 1 10000
4 Karthik AP 1 15000
SQL> select ename from emp where job=(select job from emp where ename='Arjun');
ENAME
Arjun
Gugan
19
DATABASE MANAGEMENT SYSTEMS
SET OPERATORS
QUERIES:
Q1: Display all the dept numbers available with the dept and emp tables avoiding duplicates.
Solution:
Ans: SQL> select deptno from emp union select deptno from dept;
DEPTNO
12
30
40
Q2: Display all the dept numbers available with the dept and emp tables.
Solution:
Ans: SQL> select deptno from emp union all select deptno from dept;
DEPTNO
20
DATABASE MANAGEMENT SYSTEMS
1
2
30
40
9 rows selected.
Q3: Display all the dept numbers available in emp and not in dept tables and vice versa.
Solution:
Ans: SQL> select deptno from emp minus select deptno from dept;
DEPTNO
12
SQL> select deptno from dept minus select deptno from emp;
DEPTNO
30
40
21
DATABASE MANAGEMENT SYSTEMS
5 .VIEWS
Q1: The organization wants to display only the details of the employees those are ASP.
Solution:
Ans: SQL> create view empview as select * from emp where job='ASP';
View created.
Q2: The organization wants to display only the details like empno, empname, deptno,
deptname of the employees. (Vertical portioning)
View created.
DEPT TABLE
EMP TABLE
EMPVIEW VIEW
EMPVIEW1 VIEW
View dropped.
Q3: Issue a query to display information about employees who earn more than any employee
in dept 1.
Ans: SQL> select * from emp where sal>(select max(sal) from emp where empno=1);
1 Mathi AP 1 10000
2 Arjun ASP 2 12000
3 Gugan ASP 2 20000
4 Karthik AP 1 15000
SQL> select * from dept;
23
DATABASE MANAGEMENT SYSTEMS
Q4: Display the employee details, departments that the departments are same in both the emp
and dept. Solution: 1. Use select from clause.
2. Use equi join in select clause to get the result.
Ans: SQL> select * from emp,dept where emp.deptno=dept.deptno;
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAME LOC
NON-EQUIJOIN
Q5: Display the employee details, departments that the departments are not same in both the
emp and dept. Solution: 1.Use select from clause. 2. Use non equi join in select clause to get
the result.
24
DATABASE MANAGEMENT SYSTEMS
Q6: Display the Student name and grade by implementing a left outer join.
Ans: SQL> select stud1.name,grade from stud1 left outer join stud2 on
stud1.name=stud2.name; Name Gra
john s raj
s sam a sharin a smith null RIGHTOUTER-JOIN
Q7: Display the Student name, register no, and result by implementing a right outer join.
Ans: SQL> select stud1.name, regno, result from stud1 right outer join stud2 on stud1.name
= stud2.name;
1 sindu 90 95 185
3 arul 90 90 180
FULLOUTER- JOIN
Q8: Display the Student name register no by implementing a full outer join.
Ans: SQL> select stud1.name,regno from stud1 full outer join stud2 on
(stud1.name=stud2.name);
25
DATABASE MANAGEMENT SYSTEMS
Name Regno
john 101
raj 102
sam 103
sharin 104
SELFJOIN
ENAME
Arjun
Gugan
Karthik
Mathi
Q10: Display the details of those who draw the salary greater than the average salary.
Ans: SQL> select distinct * from emp x where x.sal >= (select avg(sal) from emp); EMPNO
DCL COMMANDS
The DCL language is used for controlling the access to the table and hence securing the
database. DCL is used to provide certain privileges to a particular user. Privileges are rights
to be allocated.The privilege commands are namely, Grant and Revoke.The various
privileges that can be granted or revoked are, Select Insert Delete Update References Execute
All.
GRANT COMMAND: It is used to create users and grant access to the database. It requires
database administrator (DBA) privilege, except that a user can change their password. A user
can grant access to their database objects to other users.
REVOKE COMMAND: Using this command , the DBA can revoke the granted database
privileges from the user.
TCL COMMAND
26
DATABASE MANAGEMENT SYSTEMS
Queries:
Their schemas are as follows , Departments ( dept _no , dept_ name , dept_location );
Employees ( emp_id , emp_name , emp_salary );
Q1: Develop a query to grant all privileges of employees table into departments table
Ans: SQL> Grant all on employees to departments;
Grant succeeded.
Q2: Develop a query to grant some privileges of employees table into departments table
Ans: SQL> Grant select, update , insert on departments to departments with grant option;
Grant succeeded.
Q3: Develop a query to revoke all privileges of employees table from departments table
Ans: SQL> Revoke all on employees from departments; Revoke succeeded.
Q4: Develop a query to revoke some privileges of employees table from departments table
Ans: SQL> Revoke select, update , insert on departments from departments;
Revoke succeeded.
Savepoint created.
Mathi AP 1 10000
27
DATABASE MANAGEMENT SYSTEMS
Akalya AP 1 10000
Mathi AP 1 10000
Commit complete.
RESULT
Thus the DML, DCL,TCL commands was performed successfully and executed.
28
DATABASE MANAGEMENT SYSTEMS
6.IMPLEMENTATION OF TRIGGERS
Table created.
3 begin
4 :new.tot:=:new.m1+:new.m2+:new.m3;
5 :new.avrg:=:new.tot/3;
7 :new.result:='pass';
8 else
9 :new.result:='Fail';
10 end if;
11 end;
12 /
Trigger created.
1 row created.
29
DATABASE MANAGEMENT SYSTEMS
Table created.
SQL>
3 when(new.age>100)
4 begin
5 RAISE_APPLICATION_ERROR(-20998,'INVALID ERROR');
6* end;
SQL> /
Trigger created.
1 row created.
ERROR at line 1:
RESULT:
Table created.
1 row created.
1 row created.
ID FIRST_NAME
101 Nithya
102 Maya
31
DATABASE MANAGEMENT SYSTEMS
3 begin
5 end insert_num;
6/
Procedure created.
ID FIRST_NAME
101 Nithya
102 Maya
103 SCOTT
2 return number is
3 i number(10);
4 f number:=1;
5 begin
7 f:=f*i;
8 end loop;
9 return f;
10 end;
32
DATABASE MANAGEMENT SYSTEMS
11 /
Function created.
FACT(2)
RESULT:
33
DATABASE MANAGEMENT SYSTEMS
SQL> declare
2 salary number;
3 bonus number;
4 begin
5 salary:=&sa;
6 if salary>5000 then
7 bonus:=salary*0.5;
8 else
9 bonus:=0;
10 end if;
11 dbms_output.put_line(bonus);
12 end;
13 /
old 5: salary:=&sa;
new 5: salary:=10000;
5000
34
DATABASE MANAGEMENT SYSTEMS
SQL> declare
2 a number;
3 b number;
4 i number;
5 begin
6 i:=#
7 a:=i;
8 b:=0;
9 while a>0
10 loop
11 b:=b+power(mod(a,10),3);
12 a:=trunc(a/10);
13 end loop;
14 if b=i then
16 else
18 end if;
19 end;
20 /
old 6: i:=#
35
DATABASE MANAGEMENT SYSTEMS
new 6: i:=123;
SQL> /
old 6: i:=#
new 6: i:=407;
SQL> declare
2 a number;
3 b number;
4 i number;
5 n number;
6 s number;
7 begin
8 a:=&ulimit;
9 b:=&llimit;
10 n:=&n;
12 s:=i*n;
36
DATABASE MANAGEMENT SYSTEMS
13 dbms_output.put_line(i||'*'||n||'='||s);
14 end loop;
15 end;
16 /
old 8: a:=&ulimit;
new 8: a:=1;
old 9: b:=&llimit;
new 9: b:=10;
3*5=15
4*5=20
5*5=25
6*5=30
7*5=35
8*5=40
9*5=45
10*5=50
RESULT:
37
DATABASE MANAGEMENT SYSTEMS
STEPS:
1. Create a database for payroll processing which request the using SQL
4. Click add button and select oracle in ORA home 90, click finish
5. A window will appear given the data source home as oracle and select TNS source name as
lion and give the used id as SWTT
7. The above procedure must be follow except the table , A select the table as salary
8. Write appropriate Program in form each from created in VB from each from created in VB
form project.
38
DATABASE MANAGEMENT SYSTEMS
39
DATABASE MANAGEMENT SYSTEMS
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
End Sub
Private Sub delte_Click()
Adodc1.Recordset.Delete MsgBox "Record Deleted"
If Adodc1.Recordset.EOF = True
Then Adodc1.Recordset.MovePrevious
End If
End Sub
Private Sub exit_Click()
Unload Me
End Sub
Private Sub main_Click()
Form1.Show
End Sub
Private Sub
modify_Click()
Adodc1.Recordset.Update
End Sub
Output:
40
DATABASE MANAGEMENT SYSTEMS
RESULT:
Thus payroll system was designed and implemented successfully.
41
DATABASE MANAGEMENT SYSTEMS
42
DATABASE MANAGEMENT SYSTEMS
End Sub
SOURCE CODE FOR FORM 2
Private Sub CLEAR_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub
DELETE_Click()
Adodc1.Recordset.DELETE MsgBox "record deleted"
Adodc1.Recordset.MoveNext If Adodc1.Recordset.EOF = True Then
Adodc1.Recordset.MovePrevious
End If
End Sub
Private Sub EXIT_Click()
Unload Me
End Sub
Private Sub
HOME_Click()
Form1.Show
End Sub
Private Sub
INSERT_Click() Adodc1.Recordset.AddNew
End Sub
Private Sub
TRANSACTION_Click()
Form3.Show
End Sub
Private Sub UPDATE_Click() Adodc1.Recordset.UPDATE MsgBox "record updated
successfully"
End Sub
SOURCE CODE FOR FORM 3
Private Sub ACCOUNT_Click()
Form2.Show
End Sub
Private Sub CLEAR_Click()
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub
DEPOSIT_Click()
Dim s As String s = InputBox("enter the amount to be deposited")
Text2.Text = Val(Text2.Text) + Val(s) A = Text2.Text MsgBox "CURRENT BALANCE IS
Rs" + Str(A) Adodc1.Recordset.Save Adodc1.Recordset.UPDATE
43
DATABASE MANAGEMENT SYSTEMS
End Sub
Private Sub
EXIT_Click()
Unload Me
End Sub
Private Sub
HOME_Click()
Form1.Show End
Sub Private Sub
WITHDRAW_Click()
Dim s As String s = InputBox("enter the amount to be deleted")
Text2.Text = Val(Text2.Text) - Val(s) A = Text2.Text MsgBox "current balance is Rs" +
Str(A)
Adodc1.Recordset.Save
Adodc1.Recordset.UPDATE
End Sub
44
DATABASE MANAGEMENT SYSTEMS
Result:
Thus the banking system was designed and implemented successfully.
45
DATABASE MANAGEMENT SYSTEMS
STEPS:
4. Click add button and select oracle in ORA home 90, click finish
5. A window will appear given the data source home as oracle and select TNS source name as
lion and give the used id as SWTT
7. The above procedure must be follow except the table , A select the table as library
8. Write appropriate Program in form each from created in VB from each from created in VB
form project.
46
DATABASE MANAGEMENT SYSTEMS
UNIQUE (username) );
47
DATABASE MANAGEMENT SYSTEMS
CREATE TABLE Librarian( eid INTEGER, ID INTEGER NOT NULL, Pay REAL,
48
DATABASE MANAGEMENT SYSTEMS
(79916, 'Steven Jensen', '93 Sunny Glen Ln, Garfield Heights, OH 44125',
(93265, 'David Bain', '4356 Pooh Bear Lane, Travelers Rest, SC 29690',
49
DATABASE MANAGEMENT SYSTEMS
50
DATABASE MANAGEMENT SYSTEMS
51
DATABASE MANAGEMENT SYSTEMS
52
DATABASE MANAGEMENT SYSTEMS
53
DATABASE MANAGEMENT SYSTEMS
(SELECT COUNT(*)
FROM BookMedia BM
FROM Book B
WHERE B.title LIKE '%<user input>%' AND B.author LIKE '%<user input>%' AND
/* Find all copies of a book (used for placing holds or viewing detailed
information). */
nvl((SELECT SI.name
FROM Stored_In SI
54
DATABASE MANAGEMENT SYSTEMS
(SELECT COUNT(*)
FROM VideoMedia VM
FROM Video V
WHERE V.title LIKE '%<user input>%' AND V.year <= <user input> AND V.year <= <user
input>
AND V.director LIKE '%<user input>%' AND V.rating >= <user input>;
/* Find all copies of a video (used for placing holds or viewing detailed
information). */
nvl((SELECT SI.name
FROM Stored_In SI
SELECT S.description
nvl((SELECT MAX(H.queue)
55
DATABASE MANAGEMENT SYSTEMS
FROM Hold H
UPDATE Hold
FROM Card CR
UNION
56
DATABASE MANAGEMENT SYSTEMS
UNION
SELECT SUM(CR.fines)
FROM Card CR
/* *\
\* */
INSERT INTO Customer(ID, name, addr, DOB, phone, username, password) VALUES
(<user input>, <user input>, <user input>, <user input>, <user input>,
/* Find a customer */
nvl((SELECT 'Librarian'
FROM Librarian L
FROM Customer C
57
DATABASE MANAGEMENT SYSTEMS
INSERT INTO Card(num, fines, ID) VALUES ( <user input>, 0, <user input>);
UPDATE Media
FROM Hold H
UPDATE Stored_In
WHERE H.queue = 1 AND H.name = <user input> AND H.media_id = <user input> AND
(<user input>, <user input>, <user input>, <user input>, <user input>,
<user input>);
(<user input>, <user input>, <user input>, <user input>, <user input>);
UPDATE Card
60
DATABASE MANAGEMENT SYSTEMS
61
DATABASE MANAGEMENT SYSTEMS
STEPS:
4. Click add button and select oracle in ORA home 90, click finish
5. A window will appear given the data source home as oracle and select TNS source name as
lion and give the used id as SWTT
7. The above procedure must be follow except the table , A select the table as library
8. Write appropriate Program in form each from created in VB from each from created in VB
form project.
i. ADMINISTRATOR Table
This table holds the profile information of the application super users otherwise known as
system
administrators. They have control of the software meaning that they can perform additional
tasks that
other users cannot ordinarily perform. Every software of this nature has such users and this
one is no
exception. The table contains the following columns; ADMIN_ID, TITLE, FRIST_NAME,
LAST_NAME, and DEPARMENT_ID. The column ADMIN_ID is the primary key column
(primary
key disallows duplicate values and nulls in a column) every table should have a primary key
column,
62
DATABASE MANAGEMENT SYSTEMS
This table keeps the courses offered by students in different departments in the school. The
table
This table contains application login details for application administrators. The table columns
are;
USRNAME, PASSWD and ADMIN_ID. The column ADMIN_ID is the primary key
column.
This table contains application login details for application lecturers. The table columns are;
v. APP_USER_S Table
This table contains application login details for application students. The table columns are;
USRNAME, PASSWD and MATRIG_NO. The column MATRIG_NO is the primary key
column.
This table holds information about the schools departments. The table contains the following
columns;
This is more like the main table in the database as all other tables relate to this table directly
or in
some other way. This table holds students examination records. The table contains the
following columns; GRADES_ID, SESSION1, REG_NUMBER, DEPARTMENT_ID,
LEVEL1,
CREDIT_UNIT,
column.
This table holds the profile information of the application lecturers. The table contains the
following
This table contains student’s registration details i.e. if a student is registered for the semester
this table
is used to store that information. The table contains the following columns; REG_ID,
x. STUDENTS Table
This table holds the profile information of the application students. The table contains the
following
64
DATABASE MANAGEMENT SYSTEMS
65
DATABASE MANAGEMENT SYSTEMS
RESULT:
Thus the student information system was designed and implemented successfully.
66
DATABASE MANAGEMENT SYSTEMS
Problem Definition
Generation of reports using Reports Builder.
Problem Description
Oracle report consists of a design module, a conversion module and a runtime module. The report
server runs as a process in the background and queues up reports for execution. Oracle Reports uses
the same object navigator paradigm as Oracle form. The available object and structure differs, but
the Object navigator performs the same function that it does in Oracle form.
c). Double click on the Query Q-1 to bring up the query dialog box. Type the query in the query
dialog box in the
select clause write
select name, state, zip from customer; then
close it.
If you are selecting from tables then click apply button of dialog box.
e). Click on formula column icon on the tool palette to define a formula field. Drag the cursor to the
workspace and click inside the Query dialog box. A field CF-1 appears when the mouse is released.
f). Double click CF-1, change the data type to char in program unit of CF-1.
g). Click on apply button and close button to return to Data model screen. h). Click on default
layout tool icon, select one of the style.
i). Save the report by pressing ctrl+s. j). Click on Run icon to run the report.
Pseudo code
select name, state, zip from customer;
Problem validation
67
DATABASE MANAGEMENT SYSTEMS
Input
Query to select customer name, zipcode, state from customer table.
Problem Definition
Creating password and security features of application.
Problem Description
All the core of security in the Oracle database user account, without an Oracle username there is no way
that one can break into Oracle database. An Oracle database typically has Number of Oracle user
accounts that were either created when the database was created or created later by database
administrator. Once created an Oracle user has a number, and privileges that allow him to create and
modify database object.
Once an Oracle user is given a username and password he can connect to Oracle database using any to
Pseudo code
Create User: The create user is a SQL command that can be used to define an Oracle account in
database.
CREATE USER user_name IDENTIFIED {BY password | EXTERNALLY [AS 'certificate_DN'] |
GLOBALLY [ AS '[ directory_DN ]' ] } [ DEFAULT TABLESPACE tablespace | TEMPORARY
TABLESPACE { tablespace | tablespace_group } | QUOTA integer [ K | M | G | T | P | E ] |
UNLIMITED }ON tablespace [QUOTA integer [ K | M | G | T | P | E ] | UNLIMITED } ON tablespace
];
SQL > grant CREATE SESSION TO user_name;
The create session system privilege must also be given to a new user account.
The following code eg shows the quota currently assigned to user fowler and the account of storage
used.
Select * from dba_ts_quota where username = ‘FOWLER’;
Drop user
68
DATABASE MANAGEMENT SYSTEMS
Problem Validation
Input
Output
a) User-id
14
c) Username userid password default-tablespace temporary-tablespace created FOWLER 16 ABCD
user-data userdata 8-May-2000
Problem Definition
Usage of file locking, table locking facilities in application
Problem Description
Oracle uses lock to control concurrent access to data. Locks are mechanisms intended to prevent
destructive interaction between users accessing the same data. Table locks lock the entire tables, while
row locks lock just selected rows. Thus locks are used to ensure data integrity while allowing max
concurrent access to data by unlimited users.
Locks are used to achieve two important goals. 1. Data concurrency 2. Read consistency Oracle lock is
fully automatic and requires no user action .DBA locks the oracle data while executing SQL statement.
This type of locking is called implicit locking. When a lock is put by user it is called explicit locking.
Types of locks
Two levels of lock that a DBA can apply. They are
1. Shared : Multi user can hold various share lock on a single resource
2. Exclusive: It prohibits all sharing of resources i.e. only one use has the sole ability to alter the
resources until locks are released.
Pseudo code
LOCK TABLE [Table name] IN { ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE | SHARE |
SHARE ROW EXCLUSIVE | EXCLUSIVE } MODE [ NOWAIT]
ROW SHARE Row share locks all concurrent access to a table.
SHARE UPDATE They prohibit other users to lock entire table exclusively
ROW EXCLUSIVE Row exclusive locks the same as row share locks, but also prohibit locking in share
mode. These locks are acquired when updating, inserting or deleting.
69
DATABASE MANAGEMENT SYSTEMS
SHARE ROW EXCLUSIVE They are used to lock whole table to selective update and to allow other
Users to lock at row in the table but not lock the table in share mode or to update rows.
NO WAIT Indicates that you do not wish to wait if resources are unavailable. All locks are released
under the following circumstances:
Problem Validation
Input
Lock table emp in exclusive mode nowait;
Output
Table Locked
Problem Definition
Designing of Databases and its usage.
Problem Description
Creation of tables, Designing of forms and generation of reports.
Pseudo code
Refer to the previous exercises
Problem Validation
Creation of full fledged database
70
DATABASE MANAGEMENT SYSTEMS
71