How do you create a user?
A user is created with the CREATE USER command.
General syntax:
sql
CREATE USER username WITH PASSWORD 'password';Example
sql
CREATE USER analyst WITH PASSWORD 'secure123';Once created, you can grant them access rights:
sql
GRANT CONNECT ON DATABASE company TO analyst;
GRANT SELECT, INSERT ON TABLE employees TO analyst;The syntax varies slightly by DBMS:
-
In PostgreSQL, you use
CREATE USERorCREATE ROLE; -
In MySQL, it looks like this:
sqlCREATE USER 'analyst'@'localhost' IDENTIFIED BY 'secure123';
Summary: the CREATE USER command creates the account, and GRANT sets its permissions and access to database objects.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.