Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Database Management System (BCA352)

Program 10: Data Control Language


Domain: Video Games

By
Kunal N Jain
(2141121, 3 BCA B)

mysql> #YELLOW HIGHLIGHTING FOR CONCEPTS AND EXPLANATIONS


mysql> #GREEN HIGHLIGHTING FOR INTENTIONAL ERRORS
mysql> #BLUE HIGHLIGHTING FOR OTHER USERS

Displaying the pre-existing users


mysql> select user from mysql.user;
+------------------+
| user |
+------------------+
| mysql.infoschema |
| mysql.session |
| mysql.sys |
| root |
+------------------+
4 rows in set (0.00 sec)

Displaying the current user


mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

Creating new users


mysql> create user kunal identified by '2144';
Query OK, 0 rows affected (0.12 sec)
mysql> select user from mysql.user;
+------------------+
| user |
+------------------+
| kunal |
| mysql.infoschema |
| mysql.session |
| mysql.sys |
| root |
+------------------+
5 rows in set (0.00 sec)

Displaying grants of a user


mysql> show grants for kunal;
+-----------------------------------+
| Grants for kunal@% |
+-----------------------------------+
| GRANT USAGE ON *.* TO `kunal`@`%` |
+-----------------------------------+
1 row in set (0.00 sec)

Creating user with a different host


mysql> create user 'harshith'@'jain' identified by '1234';
Query OK, 0 rows affected (0.02 sec)

mysql> select user from mysql.user;


+------------------+
| user |
+------------------+
| kunal |
| harshith |
| mysql.infoschema |
| mysql.session |
| mysql.sys |
| root |
+------------------+
6 rows in set (0.00 sec)

mysql> show grants for harshith@jain;


+-----------------------------------------+
| Grants for harshith@jain |
+-----------------------------------------+
| GRANT USAGE ON *.* TO `harshith`@`jain` |
+-----------------------------------------+
1 row in set (0.00 sec)
Trying to create a new user using grant keyword
mysql> grant select, insert on *.* to kubber;
ERROR 1410 (42000): You are not allowed to create a user with GRANT

Granting all privileges on a table to a user


mysql> grant all on video_games.game to kunal;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for kunal;


+-------------------------------------------------------------+
| Grants for kunal@% |
+-------------------------------------------------------------+
| GRANT USAGE ON *.* TO `kunal`@`%` |
| GRANT ALL PRIVILEGES ON `video_games`.`game` TO `kunal`@`%` |
+-------------------------------------------------------------+
2 rows in set (0.00 sec)

Granting specific privileges


mysql> grant insert on video_games.game to harshith@jain;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for harshith@jain;


+-----------------------------------------------------------+
| Grants for harshith@jain |
+-----------------------------------------------------------+
| GRANT USAGE ON *.* TO `harshith`@`jain` |
| GRANT INSERT ON `video_games`.`game` TO `harshith`@`jain` |
+-----------------------------------------------------------+
2 rows in set (0.00 sec)

Logging in with a different user (on command line)


PS C:\Users\jkuna> mysql -u kunal -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.30 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its


affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show grants for kunal;


+-------------------------------------------------------------+
| Grants for kunal@% |
+-------------------------------------------------------------+
| GRANT USAGE ON *.* TO `kunal`@`%` |
| GRANT ALL PRIVILEGES ON `video_games`.`game` TO `kunal`@`%` |
+-------------------------------------------------------------+
2 rows in set (0.00 sec)

Checking if the privileges provided to this user work.


mysql> select * from game where name = "Test";
Empty set (0.01 sec)

mysql> insert into game values (null, 'Test', 20, 12, '2002-2-2', 7, 1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from game where name = "Test";


+-----+------+------------------------+--------------+-------------+-------------+----------+
| id | name | number_of_achievements | age_required | upload_date | platform_id | genre_id |
+-----+------+------------------------+--------------+-------------+-------------+----------+
| 104 | Test | 20 | 12 | 2002-02-02 | 7 | 1 |
+-----+------+------------------------+--------------+-------------+-------------+----------+
1 row in set (0.00 sec)

mysql> update game set name = "Test2" where id = 104;


Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from game where name = "Test2";


+-----+-------+------------------------+--------------+-------------+-------------+----------+
| id | name | number_of_achievements | age_required | upload_date | platform_id | genre_id |
+-----+-------+------------------------+--------------+-------------+-------------+----------+
| 104 | Test2 | 20 | 12 | 2002-02-02 | 7 | 1 |
+-----+-------+------------------------+--------------+-------------+-------------+----------+
1 row in set (0.00 sec)

Revoking a permission from a user


mysql> revoke select on video_games.game from kunal;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for kunal;
+----------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for kunal@% |
+----------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `kunal`@`%` |
| GRANT INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE VIEW, SHOW VIEW, TRIGGER ON `video_games`.`game` TO `kunal`@`%` |
+----------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

Trying to perform select, when the privilege is not provided


mysql> select * from game where id = 5;
ERROR 1142 (42000): SELECT command denied to user 'kunal'@'localhost' for table 'game'

Revoking all privileges


mysql> revoke all on video_games.game from kunal;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for kunal;


+-----------------------------------+
| Grants for kunal@% |
+-----------------------------------+
| GRANT USAGE ON *.* TO `kunal`@`%` |
+-----------------------------------+
1 row in set (0.00 sec)

Trying to log into user with different host


PS C:\Users\jkuna> mysql -u harshith@jain -p
Enter password: ****
ERROR 1045 (28000): Access denied for user 'harshith@jain'@'localhost' (using password: YES)

mysql> select host, user from mysql.user;


+-----------+------------------+
| host | user |
+-----------+------------------+
| % | kunal |
| jain | harshith |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
6 rows in set (0.00 sec)
Granting all privileges on all tables of all databases
mysql> grant all on *.* to test;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for test;


+---------------------------------------------------------------------------------------------------------------------------------------
| Grants for test@%
----------------------------------------------------------------------------------------------------------------------------------------
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER,
CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER
ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `test`@`%`
|
| GRANT
APPLICATION_PASSWORD_ADMIN,AUDIT_ABORT_EXEMPT,AUDIT_ADMIN,AUTHENTICATION_POLICY_ADMIN,BACKUP_ADMIN,BINLOG_ADMIN,BINLOG_ENCRYPTION_ADMIN,
CLONE_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,FIREWALL_EXEMPT,FLUSH_OPTIMIZER_COSTS,FLUSH_STATUS,FLUSH_TABLES,FLUSH_USER_RESOURCES,G
ROUP_REPLICATION_ADMIN,GROUP_REPLICATION_STREAM,INNODB_REDO_LOG_ARCHIVE,INNODB_REDO_LOG_ENABLE,PASSWORDLESS_USER_ADMIN,PERSIST_RO_VARIAB
LES_ADMIN,REPLICATION_APPLIER,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SENSITIVE_VARIABLES_OBSERVER,S
ERVICE_CONNECTION_ADMIN,SESSION_VARIABLES_ADMIN,SET_USER_ID,SHOW_ROUTINE,SYSTEM_USER,SYSTEM_VARIABLES_ADMIN,TABLE_ENCRYPTION_ADMIN,XA_RE
COVER_ADMIN ON *.* TO `test`@`%` |
---------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

Revoking all privileges on all tables of all databases

mysql> revoke all on *.* from test;


Query OK, 0 rows affected (0.01 sec)

mysql> show grants for test;


+----------------------------------+
| Grants for test@% |
+----------------------------------+
| GRANT USAGE ON *.* TO `test`@`%` |
+----------------------------------+
1 row in set (0.00 sec)

Dropping users.
mysql> drop user test;
Query OK, 0 rows affected (0.00 sec)

mysql> drop user harshith@jain;


Query OK, 0 rows affected (0.01 sec)

You might also like