Database 3rd Lab Notes
Database 3rd Lab Notes
Overall Notes
Oracle allows multiple user to use the database , Every user accesses the
database using their password and username , the later determines the access
rights to the objects of the database
The user that creates an object in the database in the owner of it , and only HE can
decide to grant or revoke certain acess rights to other users .
Commands :
this command gives the concerned privilege on the table or view to the user .
A user who has granted a privilege can take it back at any time by using the revoke
command .
WITH GRANT OPTION : A user who has received a privilege with this option can
pass it on to another user .
If a user is the owner of a resource, they automatically have access to it. For others to
access it, the creator's permissions are required, typically through the creator.table
format.
1. SYS Privileges:
Access: SYS can access any object in the database without explicit
permissions because it operates with unrestricted authority.
Granting Privileges: SYS can grant privileges (e.g., SELECT, INSERT) on
any object to other users without requiring permission from the object owner.
2. SYSTEM Privileges:
Access: SYSTEM cannot directly access objects owned by other users unless
explicitly granted the necessary privileges.
Granting Privileges: SYSTEM can grant privileges on other users' objects only
if:
SYSTEM has been granted the privileges explicitly with the WITH GRANT
OPTION clause, or
SYSTEM holds a system-level privilege like GRANT ANY OBJECT PRIVILEGE .
we fix this by :
Session altered.
User created.
CONNECT Admin
Enter password: admin123
When you log in with the "Admin" user, you'll likely notice that Admin does not have
access to create sessions, tables, or any specific objects in the database since new
users typically don’t have permissions by default.
After logging in, you should now be able to create tables and users with the Admin
account.
Checking :
we use this command to check all the priviledgees that a user have .
1. USER_SYS_PRIVS
Shows system-level privileges granted to the current user. These privileges control
what a user can do at the database level, such as logging in, creating users, or altering
the system.
2. USER_TAB_PRIVS
Shows object-level privileges granted on specific database objects (e.g., tables,
views). These privileges control what a user can do with specific objects.
Summary:
USER_SYS_PRIVS = Database-wide permissions (e.g., login, create users).
USER_TAB_PRIVS = Permissions on specific objects (e.g., read or modify a table).
Now, running query Q1 should display the contents of the Teacher table without
permission errors.
7. The school increases the salaries of Teachers by
3000 DA if their total number of courses taught is
greater than or equal to 3. What should be done?
What do you notice?
UPDATE DBASchool.Teacher
SET Salary = Salary + 3000
WHERE TeacherID IN (
SELECT TeacherID
FROM DBASchool.COURSE_ASSIGNMENT
GROUP BY TeacherID
HAVING COUNT(CourseID) >= 3
);
You will likely encounter an error due to insufficient privileges, as Admin does not have
UPDATE rights on the TEACHER table and SELECT rights on the COURSE_ASSIGNMENT
table .
Now, rerun the UPDATE query from step 7. The update should execute successfully.
9. Create a LastName_IX index on the LastName
attribute of the Teacher table. What do you notice?
What are indexes ?
Index : special data structures that improve the speed of data retrieval operations
on a database table. They function like the index of a book : they allow the
database to quickly locate specific rows without scanning the entire table.
Indexes are especially useful when dealing with large datasets and can significantly
enhance query performance, especially for SELECT queries involving search
conditions (e.g., WHERE clauses), sorting ( ORDER BY ), and joins.
--> fail because Admin does not have index creation privileges on the Teacher table.
REVOKE CREATE TABLE, CREATE USER, CREATE SESSION FROM Admin; --as
system
REVOKE SELECT, UPDATE ON Teacher FROM Admin; --as DBASchool
REVOKE SELECT ON COURSE_ASSIGNMENT FROM Admin; --as DBASchool
REVOKE INDEX ON Teacher FROM Admin ; --as DBASchool
12. Verify that the privileges have been revoked.
To verify, you can use the following query:
The output should show no entries for the privileges that were just revoked.
if you are in CDB make sure to switch to the correct PDB to create the profile in it
(that is the only correct way )
Now, you can verify that Admin has inherited the privileges by running:
The results should show Admin's access to the specified tables and permissions
provided by the School_MANAGER role.
Additional notes
To check all PDBs in the CDB
don't forget to ALTER SESSION SET "_ORACLE_SCRIPT"=true; for this cdb pdb
problem.
Here is a summary of the SQL syntax and steps involved in creating users,
profiles, roles, and addressing the "no privileges on the tablespace" issue:
1. Creating a User:
Syntax:
Key Options:
IDENTIFIED BY password : Assigns a password to a local user.
DEFAULT TABLESPACE : Assigns a default tablespace for storage.
TEMPORARY TABLESPACE : Assigns a temporary tablespace for temporary objects.
QUOTA : Limits or allows unlimited space on a tablespace.
PROFILE : Assigns a profile to manage user session and resource limits.
PASSWORD EXPIRE : Forces a user to change their password on first login.
ACCOUNT LOCK/UNLOCK : Controls access to the account.
2. Creating a Profile:
Syntax:
3. Creating a Role:
Syntax:
Purpose: Roles allow you to group multiple privileges and assign them to users.
This gives the user Admin unlimited space in the SCHOOL_TBS tablespace, allowing
them to create tables.