🔍
Back

Write SQL command for following
i)Create user
ii) Grant privileges to user.
Iii) Remove privileges from user.

0 like 0 dislike

1 Answer

Of course. Here are the SQL commands for creating a user, granting privileges, and removing privileges, along with explanations and examples.

These commands fall under DCL (Data Control Language) and are typically executed by a Database Administrator (DBA) or a user with sufficient administrative permissions.


i) Create User

The CREATE USER command is used to create a new user account in the database. A newly created user typically has no privileges, not even the ability to log in, until they are granted.

Syntax

`sql
CREATE USER username IDENTIFIED BY password;
`
username: The name you want to give the new user.
password: The password the user will use to log in.

Example

Let's create a new user named new_app_user.

`sql
CREATE USER new_app_user IDENTIFIED BY 'a_very_secure_password';
`
Explanation: This command creates a new user account named new_app_user with the password a_very_secure_password. At this point, new_app_user exists but cannot connect to the database.


ii) Grant Privileges to User

The GRANT command is used to give specific permissions (privileges) to a user or a role. This is how you control what a user is allowed to do.

There are two main types of privileges:
System Privileges: Permissions to perform database-wide actions (e.g., connect to the database, create tables).
Object Privileges: Permissions to perform actions on specific database objects (e.g., select from a table, execute a procedure).

Syntax

For System Privileges:
`sql
GRANT privilege_name [, ...] TO username;
`
* privilege_name: The system privilege, such as CONNECT or CREATE TABLE.

For Object Privileges:
`sql
GRANT privilege_name [, ...] ON object_name TO username;
`
privilege_name: The object privilege, such as SELECT, INSERT, UPDATE, DELETE.
object_name: The name of the table, view, or other object.

Example

Let's grant our new_app_user the ability to log in and then allow them to read and add data to a specific table called products.

1. Granting Login Permission (System Privilege):
The most basic privilege a user needs is CONNECT.
`sql
GRANT CONNECT TO new_app_user;
`
Explanation: This command gives new_app_user the ability to connect to the database.

2. Granting Table Permissions (Object Privileges):
Now, let's allow the user to work with the products table.
`sql
GRANT SELECT, INSERT ON products TO new_app_user;
`
Explanation: This command gives new_app_user two specific permissions on the products table:
SELECT: The ability to read data from the table.
INSERT: The ability to add new rows to the table.
The user still cannot UPDATE or DELETE data from this table.


iii) Remove Privileges from User

The REVOKE command is the opposite of GRANT. It is used to take away privileges from a user or a role.

Syntax

For System Privileges:
`sql
REVOKE privilege_name [, ...] FROM username;
`

For Object Privileges:
`sql
REVOKE privilege_name [, ...] ON object_name FROM username;
`

Example

Let's say the role of new_app_user has changed, and they should no longer be able to add new data to the products table. We need to remove their INSERT privilege.

`sql
REVOKE INSERT ON products FROM new_app_user;
`
Explanation: This command removes the INSERT permission on the products table from new_app_user. After this, the user can still SELECT data from the table (because we didn't revoke that privilege), but any attempt to INSERT a new row will result in a "permission denied" error.

0 like 0 dislike
Next ⇨Next ⇨

Related questions

i) Write a command to create table student(RNO,name marks, dept) with proper datatypes and RNo as primary key ii) Write a command to create and drop sequence.
Answer : Of course. Here are the commands and explanations for creating a table and managing a sequence. --- ### i) Command to Create a `student` Table This command will create a table named `student` ... Once dropped, the sequence cannot be recovered, and any code that refers to it will produce an error....

Show More

Write a PL/SQL program which accepts the customer_ID from the user. If the enters an invalid ID then the exception invalid_id is raised using exception handling.
Answer : Of course. Here is a complete, runnable PL/SQL program that accomplishes this task. The solution includes: 1. Setup steps to create a sample `Customers` table. 2. The PL/SQL block that accepts user input and handles the ... --------------------------- PL/SQL procedure successfully completed. ```...

Show More

Write step by step syntax to create, open and close cursor in PL/SQL.
Answer : Of course. Here is a step-by-step guide to the syntax for creating, opening, fetching from, and closing a cursor in PL/SQL, complete with a full, practical example. --- ### Introduction: The Cursor Lifecycle Working ... END IF; DBMS_OUTPUT.PUT_LINE('An error occurred.'); END; / ```...

Show More

List the SQL operations and explain range searching operations between and pattern matching operator ‘like’ with example.
Answer : Of course. Here is a comprehensive overview of SQL operations, followed by a detailed explanation of the `BETWEEN` and `LIKE` operators with examples. --- ### List of SQL Operations (Categories) SQL ... alternative to using two separate comparisons with `AND` (e.g., `price >= 50 AND price ...

Show More

Explain exception handling in PL/SQL with example.
Answer : Of course. Here is a comprehensive explanation of exception handling in PL/SQL, complete with its purpose, structure, types of exceptions, and a detailed, practical example. --- ### What is Exception Handling? **Exception Handling** is a powerful ... ------------------------------------ ```...

Show More
Welcome to Computer Engineering, where you can ask questions and receive answers from other members of the community.

Categories

...