Oracle database is currently the most popular commercial relational database management system in the world. It is widely used for its efficiency, security, stability and other advantages. In Oracle database, all users have unique user names, but sometimes we need to delete some unnecessary or redundant user names. This article will introduce how to delete username in Oracle database.
SELECT USERNAME FROM ALL_USERS;
This SQL statement will return a result set that contains all user names. We need to find the username that needs to be deleted.
DROP USER username CASCADE;
Note that "username" here refers to the username that needs to be deleted. The CASCADE keyword means that before deleting the user, delete all objects owned by the user, including tables, views, stored procedures, etc. If the CASCADE keyword is not added, the system will prompt that all objects owned by the user must be deleted before the user can be deleted.
SELECT * FROM V$SESSION WHERE USERNAME='username';
This SQL statement will return all currently active sessions for this user. We need to terminate these sessions one by one. You can use the following SQL statement:
ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE;
Note that "sid,serial#" here refers to the identifier of the session that needs to be terminated, which can be found in the result of the previous SQL statement Get concentrated. If you want to terminate all the user's sessions at once, you can use the following SQL statement:
BEGIN FOR cur_rec IN (SELECT sid,serial# FROM V$SESSION WHERE username='username') LOOP EXECUTE IMMEDIATE 'ALTER SYSTEM KILL SESSION ''' || cur_rec.sid || ',' || cur_rec.serial# || ''' IMMEDIATE'; END LOOP; END;
After executing this PL/SQL code, all the user's sessions will be terminated.
In short, deleting a user name in the Oracle database requires first confirming whether the user still has an active session, and then deleting the user. Before deleting the user, you also need to back up the data to avoid data loss caused by misoperation.
The above is the detailed content of oracle delete username. For more information, please follow other related articles on the PHP Chinese website!