Manage users & tables in Oracle Database

4
ORACLE DBMS: Managing Users & Tables Vazi Okhandiar Page 1 by Vazi Okhandiar, PMP, MSCS, MCT NR Computer Learning Center 1835 W. Orangewood . Suite 200 Orange, CA 92868 www.nrclc.com

description

Learn to create user and manage them in Oracle database. Also learn to create and manage table as a user in Oracle database.

Transcript of Manage users & tables in Oracle Database

Page 1: Manage users & tables in Oracle Database

ORACLE DBMS: Managing Users & Tables

Vazi Okhandiar Page 1

by Vazi Okhandiar, PMP, MSCS, MCT

NR Computer Learning Center 1835 W. Orangewood . Suite 200

Orange, CA 92868 www.nrclc.com

Page 2: Manage users & tables in Oracle Database

ORACLE DBMS: Managing Users & Tables

Vazi Okhandiar Page 2

The article consists of perform following 3 tasks using Oracle Relational Database:

Steps for create an account for a user

Steps for user to login into Oracle instance and manage data

Steps for Removing the account from the database instance

STEP 1: Create an account for a user: John 1. Log into oracle instance as SYSDBA

>> sqlplus / as sysdba

2. Create User john with password smith

SQL> CREATE USER john IDENTIIED BY smith;

3. Assign the user (john) to use users as the default tablespace.

SQL> ALTER USER john DEFAULT TABLESPACE users

4. Assign the user (john) to use temp as the temporary tablespace

SQL> ALTER USER john TEMPORARY TABLESPACE temp;

5. Allow user (john) to connect to the database

SQL> GRANT CONNECT TO john;

6. Verify that account is created by listing all the users in the database

SQL> Select * from all_users;

7. Check if the account is locked;

SQL> SELECT account_status FROM dba_users where username=’JOHN’;

If the account is locked then unlock using following command.

SQL> ALTER USER john ACCOUNT UNLOCK;

8. Assign John unlimited quota on tablespace users.

SQL> Alter user john quota unlimited on users;

9. Grant resource to john

SQL> GRANT RESOURCE TO user;

10. Log out of sysdba account

SQL> exit

Page 3: Manage users & tables in Oracle Database

ORACLE DBMS: Managing Users & Tables

Vazi Okhandiar Page 3

STEP 2: Login as john 1. Login as john

>> sqlplus john/smith

2. Create a table called mytable

SQL> create table mytable ( Id number(3), FirstName varchar2(15), LastName varchar2(15));

3. Verify the table is created

SQL> select table_name from all_tables order by table_name; Or SQL> select table_name from user_tables;

You should see your table in the output list.

4. Insert following records into a table.

SQL> INSERT INTO mytable (ID, FirstName, LastName) Values (1,‘John’, ‘Smith’); SQL> INSERT INTO mytable (ID, FirstName, LastName) Values (2,‘Jane’, ‘Doe’); SQL> INSERT INTO mytable (ID, FirstName, LastName) Values (3,‘Paul’, ‘Ryan’);

5. Use SELECT command to verify the data is inserted into the table

SQL> Select * from mytable;

ID FirstName LastName

1 John Smith 2 Jane Doe 3 Paul Ryan

6. Remove jane from the table

SQL > DELETE FROM mytable WHERE id=2;

7. Use SELECT command to verify the jane’s record has been deleted.

SQL> Select * from mytable;

ID FirstName LastName

1 John Smith 3 Paul Ryan

8. Remove the table from the database

Page 4: Manage users & tables in Oracle Database

ORACLE DBMS: Managing Users & Tables

Vazi Okhandiar Page 4

SQL> DROP TABLE mytable;

9. Verify the table is dropped SQL> select table_name from user_tables; Note: mytable should not be in the list of tables.

10. Exit out of oracle as john SQL > exit

STEP 3: Remove the user 1. Log in as sysdba

>> sqlplus / as sysdba

2. Verify john’s account is still in the database

SQL> select * from all_users where username=’JOHN’;

UserName User_ID CREATED

JOHN 91 03-FEB-13

3. Remove the user from the database

SQL> DROP USER john;

If the user’s schema contains objects, then you must use the CASCADE clause to drop the

user and the objects:

SQL> DROP USER john CASCADE;

4. Verify john’s account has been deleted from the database

SQL> select * from all_users where username=’JOHN’;

User dropped.

Reference: Oracle SQL*PLUS Command

http://docs.oracle.com/cd/B13789_01/server.101/b10758/sqlqraa.htm